From b004dd95ab06d910728e59c529ea722d7a228301 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 21 Feb 2016 08:13:17 +0000 Subject: [PATCH] Resize quadtree entity capacity as needed. --- src/battle/battle.c | 2 -- src/battle/quadtree.c | 45 ++++++++++++++++++++++++++++++++++++++----- src/battle/quadtree.h | 4 ++++ src/defs.h | 4 ---- src/structs.h | 3 ++- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/battle/battle.c b/src/battle/battle.c index fd2e869..18b5313 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -52,8 +52,6 @@ void initBattle(void) app.delegate.draw = &draw; memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS); - battle.quadtree.w = BATTLE_AREA_WIDTH; - battle.quadtree.h = BATTLE_AREA_HEIGHT; initQuadtree(&battle.quadtree); initBullets(); diff --git a/src/battle/quadtree.c b/src/battle/quadtree.c index 15d4db8..fda0236 100644 --- a/src/battle/quadtree.c +++ b/src/battle/quadtree.c @@ -30,14 +30,22 @@ static void addCandidate(Entity *e); static int candidatesComparator(const void *a, const void *b); static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Quadtree *root); static void destroyQuadtreeNode(Quadtree *root); +static void resizeQTEntCapacity(Quadtree *root); void initQuadtree(Quadtree *root) { Quadtree *node; int i, w, h; + /* entire battlefield */ if (root->depth == 0) { + root->w = BATTLE_AREA_WIDTH; + root->h = BATTLE_AREA_HEIGHT; + root->capacity = QT_INITIAL_CAPACITY; + root->ents = malloc(sizeof(Entity*) * root->capacity); + memset(root->ents, 0, sizeof(Entity*) * root->capacity); + memory = 0; } @@ -55,6 +63,9 @@ void initQuadtree(Quadtree *root) root->node[i] = node; node->depth = root->depth + 1; + node->capacity = QT_INITIAL_CAPACITY; + node->ents = malloc(sizeof(Entity*) * node->capacity); + memset(node->ents, 0, sizeof(Entity*) * node->capacity); if (i == 0) { @@ -110,13 +121,35 @@ void addToQuadtree(Entity *e, Quadtree *root) } } - if (root->numEnts < QT_MAX_ENTS) + if (root->numEnts == root->capacity) { - root->ents[root->numEnts++] = e; - return; + resizeQTEntCapacity(root); } - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Couldn't add %s to quadtree - out of node ent space", e->name); + root->ents[root->numEnts++] = e; +} + +static void resizeQTEntCapacity(Quadtree *root) +{ + int i, n; + Entity **ents; + + n = root->capacity + QT_INITIAL_CAPACITY; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing QT node: %d -> %d\n", root->capacity, n); + + ents = malloc(sizeof(Entity*) * n); + memset(ents, 0, sizeof(Entity*) * n); + + for (i = 0 ; i < root->capacity ; i++) + { + ents[i] = root->ents[i]; + } + + free(root->ents); + + root->ents = ents; + root->capacity = n; } static int getIndex(Quadtree *root, int x, int y, int w, int h) @@ -184,7 +217,7 @@ static void removeEntity(Entity *e, Quadtree *root) n = root->numEnts; - for (i = 0 ; i < QT_MAX_ENTS ; i++) + for (i = 0 ; i < root->capacity ; i++) { if (root->ents[i] == e) { @@ -279,6 +312,8 @@ static void destroyQuadtreeNode(Quadtree *root) { destroyQuadtreeNode(root->node[i]); + free(root->node[i]->ents); + free(root->node[i]); root->node[i] = NULL; diff --git a/src/battle/quadtree.h b/src/battle/quadtree.h index b817670..b070001 100644 --- a/src/battle/quadtree.h +++ b/src/battle/quadtree.h @@ -20,4 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" +#define QT_MAX_DEPTH 6 +#define QT_MAX_CANDIDATES 256 +#define QT_INITIAL_CAPACITY 8 + extern Battle battle; diff --git a/src/defs.h b/src/defs.h index cbe47e7..9dababd 100644 --- a/src/defs.h +++ b/src/defs.h @@ -60,10 +60,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_FIGHTER_GUNS 12 #define MAX_TARGET_RANGE 65536 -#define QT_MAX_DEPTH 5 -#define QT_MAX_ENTS 96 -#define QT_MAX_CANDIDATES 256 - #define BATTLE_AREA_CELLS 50 #define BATTLE_AREA_WIDTH (640 * BATTLE_AREA_CELLS) #define BATTLE_AREA_HEIGHT (360 * BATTLE_AREA_CELLS) diff --git a/src/structs.h b/src/structs.h index 3dfc49b..3f63628 100644 --- a/src/structs.h +++ b/src/structs.h @@ -267,7 +267,8 @@ struct StarSystem { struct Quadtree { int depth; int x, y, w, h; - Entity *ents[QT_MAX_ENTS]; + Entity **ents; + int capacity; int numEnts; Quadtree *node[4]; };