Start of Grid.

This commit is contained in:
Steve 2015-11-01 22:19:39 +00:00
parent 144143a049
commit 7a6a6b26c0
7 changed files with 192 additions and 2 deletions

View File

@ -1,6 +1,6 @@
PROG = tbftss
VERSION = 0.2
VERSION = 0.3
CXXFLAGS += `sdl2-config --cflags` -DVERSION=$(VERSION) -DUNIX=1
CXXFLAGS += -DUNIX
@ -21,7 +21,7 @@ OBJS += challenges.o cJSON.o
OBJS += draw.o
OBJS += effects.o entities.o
OBJS += fighters.o fighterDefs.o
OBJS += galacticMap.o game.o
OBJS += galacticMap.o game.o grid.o
OBJS += hud.o
OBJS += init.o io.o
OBJS += load.o lookup.o

View File

@ -372,4 +372,6 @@ void destroyBattle(void)
free(t);
}
battle.triggerTail = &battle.triggerHead;
destroyGrid();
}

View File

@ -63,6 +63,7 @@ extern void playSound(int id);
extern void checkTrigger(char *name, int type);
extern void resetWaypoints(void);
extern void doPlayerSelect(void);
extern void destroyGrid(void);
extern App app;
extern Battle battle;

147
src/battle/grid.c Normal file
View File

@ -0,0 +1,147 @@
/*
Copyright (C) 2015 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "grid.h"
static Entity *candidates[MAX_GRID_CANDIDATES];
static void addCandidate(Entity *e);
void addToGrid(Entity *e)
{
GridCell *cell, *prev;
int x, y, x1, y1, x2, y2;
x1 = e->x / GRID_CELL_WIDTH;
y1 = e->y / GRID_CELL_HEIGHT;
x2 = (e->x + e->w) / GRID_CELL_WIDTH;
y2 = (e->y + e->h) / GRID_CELL_HEIGHT;
for (x = x1 ; x <= x2 ; x++)
{
for (y = y1 ; y <= y2 ; y++)
{
prev = &battle.grid[x][y];
for (cell = battle.grid[x][y].next ; cell != NULL ; cell = cell->next)
{
prev = cell;
}
cell = malloc(sizeof(GridCell));
memset(cell, 0, sizeof(GridCell));
prev->next = cell;
cell->entity = e;
}
}
}
void removeFromGrid(Entity *e)
{
GridCell *cell, *prev;
int x, y, x1, y1, x2, y2;
x1 = e->x / GRID_CELL_WIDTH;
y1 = e->y / GRID_CELL_HEIGHT;
x2 = (e->x + e->w) / GRID_CELL_WIDTH;
y2 = (e->y + e->h) / GRID_CELL_HEIGHT;
for (x = x1 ; x <= x2 ; x++)
{
for (y = y1 ; y <= y2 ; y++)
{
prev = &battle.grid[x][y];
for (cell = battle.grid[x][y].next ; cell != NULL ; cell = cell->next)
{
if (cell->entity == e)
{
prev->next = cell->next;
free(cell);
cell = prev;
}
prev = cell;
}
}
}
}
Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore)
{
GridCell *cell;
int x1, y1, x2, y2;
memset(candidates, 0, sizeof(Entity*) * MAX_GRID_CANDIDATES);
x1 = x / GRID_CELL_WIDTH;
y1 = y / GRID_CELL_HEIGHT;
x2 = w / GRID_CELL_WIDTH;
y2 = h / GRID_CELL_HEIGHT;
for (x = x1 ; x < x2 ; x++)
{
for (y = y1 ; y < y2 ; y++)
{
for (cell = battle.grid[x][y].next ; cell != NULL ; cell = cell->next)
{
addCandidate(battle.grid[x][y].entity);
}
}
}
return candidates;
}
static void addCandidate(Entity *e)
{
int i = 0;
for (i = 0 ; i < MAX_GRID_CANDIDATES ; i++)
{
if (candidates[i] == NULL || candidates[i] == e)
{
candidates[i] = e;
return;
}
}
printf("Out of grid candidate space!");
exit(1);
}
void destroyGrid(void)
{
int x, y;
GridCell *cell;
for (x = 0 ; x < GRID_SIZE ; x++)
{
for (y = 0 ; y < GRID_SIZE ; y++)
{
while (battle.grid[x][y].next)
{
cell = battle.grid[x][y].next;
battle.grid[x][y].next = cell->next;
free(cell);
}
}
}
}

26
src/battle/grid.h Normal file
View File

@ -0,0 +1,26 @@
/*
Copyright (C) 2015 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "SDL2/SDL.h"
#include "../defs.h"
#include "../structs.h"
extern Battle battle;

View File

@ -47,6 +47,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_FIGHTER_GUNS 12
#define MAX_TARGET_RANGE 65536
#define GRID_CELL_WIDTH 640
#define GRID_CELL_HEIGHT 360
#define GRID_SIZE 25
#define MAX_GRID_CANDIDATES 1024
#define BF_NONE 0
#define BF_ENGINE (2 << 0)
#define BF_SYSTEM_DAMAGE (2 << 1)

View File

@ -33,6 +33,7 @@ typedef struct Pulse Pulse;
typedef struct Widget Widget;
typedef struct HudMessage HudMessage;
typedef struct Trigger Trigger;
typedef struct GridCell GridCell;
typedef struct {
float x;
@ -77,6 +78,8 @@ struct Entity {
int side;
float x;
float y;
int w;
int h;
float dx;
float dy;
float thrust;
@ -218,6 +221,11 @@ struct StarSystem {
StarSystem *next;
};
struct GridCell {
Entity *entity;
GridCell *next;
};
typedef struct {
int entId;
SDL_Point camera;
@ -238,6 +246,7 @@ typedef struct {
Objective objectiveHead, *objectiveTail;
Trigger triggerHead, *triggerTail;
unsigned int stats[STAT_MAX];
GridCell grid[GRID_SIZE][GRID_SIZE];
} Battle;
typedef struct {