From 7a6a6b26c06b29ec74174297778d430293abb338 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 1 Nov 2015 22:19:39 +0000 Subject: [PATCH] Start of Grid. --- makefile | 4 +- src/battle/battle.c | 2 + src/battle/battle.h | 1 + src/battle/grid.c | 147 ++++++++++++++++++++++++++++++++++++++++++++ src/battle/grid.h | 26 ++++++++ src/defs.h | 5 ++ src/structs.h | 9 +++ 7 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 src/battle/grid.c create mode 100644 src/battle/grid.h diff --git a/makefile b/makefile index d61b596..d7ac250 100644 --- a/makefile +++ b/makefile @@ -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 diff --git a/src/battle/battle.c b/src/battle/battle.c index 2a5f998..e20caef 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -372,4 +372,6 @@ void destroyBattle(void) free(t); } battle.triggerTail = &battle.triggerHead; + + destroyGrid(); } diff --git a/src/battle/battle.h b/src/battle/battle.h index 3961920..ac2a4d4 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -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; diff --git a/src/battle/grid.c b/src/battle/grid.c new file mode 100644 index 0000000..d0eac04 --- /dev/null +++ b/src/battle/grid.c @@ -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); + } + } + } +} diff --git a/src/battle/grid.h b/src/battle/grid.h new file mode 100644 index 0000000..68f4da4 --- /dev/null +++ b/src/battle/grid.h @@ -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; diff --git a/src/defs.h b/src/defs.h index 24e5717..7bfee73 100644 --- a/src/defs.h +++ b/src/defs.h @@ -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) diff --git a/src/structs.h b/src/structs.h index 98d67c9..798b6c1 100644 --- a/src/structs.h +++ b/src/structs.h @@ -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 {