#ifndef WORLD_GRID_H #define WORLD_GRID_H /* * Where things are, by ground cell: asking "what's around here" looks at the * few things in the cells round about instead of everything on the site. * Built once a frame (things moving a little in between don't matter: each * thing sits in every cell its footprint touches, plus a margin). */ #include #include "world/object.h" #define NEAR_CELL 2.0f /* meters */ typedef struct Grid { int n; /* cells along a side */ int *start; /* cell k's things: items[start[k] .. start[k+1]) */ int *items; /* indices into the world's objects */ int items_cap; unsigned *seen; /* (each thing once per question) */ int seen_cap; unsigned stamp; int built_count; /* how many things it was built for */ } Grid; void Grid_build(Grid *g, Object *const *objects, int count); void Grid_free(Grid *g); /* Indices of things whose footprint comes within r of (x, z); returns how many, or -1 if they don't fit (or the grid isn't built for count things: look at all). */ int Grid_near(Grid *g, int count, float x, float z, float r, int *out, int max); /* Like Grid_near, but the things themselves, in the order they have in objects (so answers don't depend on the grid): returns how many, or -1 (look at all). */ int Grid_nearObjects(Grid *g, Object *const *objects, int count, float x, float z, float r, Object **out, int max); #endif /* WORLD_GRID_H */