Added getAllEntsInRadius function, for better radius checking.

This commit is contained in:
Steve 2016-05-04 16:23:28 +01:00
parent eb225fefc9
commit fcbf18116c
17 changed files with 33 additions and 25 deletions

View File

@ -312,9 +312,9 @@ static void findTarget(void)
Entity *e, **candidates; Entity *e, **candidates;
unsigned int dist, closest; unsigned int dist, closest;
dist = closest = (battle.isEpic || (self->aiFlags & AIF_UNLIMITED_RANGE)) ? MAX_TARGET_RANGE : 1000; dist = closest = (battle.isEpic || (self->aiFlags & AIF_UNLIMITED_RANGE)) ? MAX_TARGET_RANGE : SCREEN_WIDTH;
candidates = getAllEntsWithin(self->x - (self->w / 2) - dist, self->y - (self->h / 2) - dist, self->w + (dist * 2), self->h + (dist * 2), self); candidates = getAllEntsInRadius(self->x, self->y, dist, self);
self->target = NULL; self->target = NULL;
@ -588,7 +588,7 @@ static int nearEnemies(void)
int i, numEnemies, x, y; int i, numEnemies, x, y;
Entity *e, **candidates; Entity *e, **candidates;
candidates = getAllEntsWithin(self->x - 500, self->y - 500, 1000, 1000, self); candidates = getAllEntsInRadius(self->x, self->y, SCREEN_WIDTH, self);
self->target = NULL; self->target = NULL;
x = y = 0; x = y = 0;
@ -604,7 +604,7 @@ static int nearEnemies(void)
continue; continue;
} }
if (getDistance(e->x, e->y, self->x, self->y) < 1000) if (getDistance(e->x, e->y, self->x, self->y) <= SCREEN_WIDTH)
{ {
x += e->x; x += e->x;
y += e->y; y += e->y;
@ -658,7 +658,7 @@ static int nearMines(void)
int i, numMines; int i, numMines;
Entity *e, **candidates; Entity *e, **candidates;
candidates = getAllEntsWithin(self->x - 500, self->y - 500, 1000, 1000, self); candidates = getAllEntsInRadius(self->x, self->y, SCREEN_HEIGHT, self);
self->targetLocation.x = self->targetLocation.y = 0; self->targetLocation.x = self->targetLocation.y = 0;
@ -666,7 +666,7 @@ static int nearMines(void)
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {
if (e->side != self->side && e->type == ET_MINE && getDistance(e->x, e->y, self->x, self->y) < 500) if (e->side != self->side && e->type == ET_MINE && getDistance(e->x, e->y, self->x, self->y) <= SCREEN_HEIGHT)
{ {
self->targetLocation.x += e->x; self->targetLocation.x += e->x;
self->targetLocation.y += e->y; self->targetLocation.y += e->y;
@ -774,7 +774,7 @@ static int nearItems(void)
closest = MAX_TARGET_RANGE; closest = MAX_TARGET_RANGE;
candidates = getAllEntsWithin(self->x - (self->w / 2) - (SCREEN_WIDTH / 4), self->y - (self->h / 2) - (SCREEN_HEIGHT / 4), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, self); candidates = getAllEntsInRadius(self->x, self->y, SCREEN_WIDTH / 2, self);
self->target = NULL; self->target = NULL;
@ -822,7 +822,7 @@ static int nearTowableCraft(void)
dist = closest = (battle.isEpic || (self->aiFlags & AIF_UNLIMITED_RANGE)) ? MAX_TARGET_RANGE : 2000; dist = closest = (battle.isEpic || (self->aiFlags & AIF_UNLIMITED_RANGE)) ? MAX_TARGET_RANGE : 2000;
candidates = getAllEntsWithin(self->x - (self->w / 2) - dist, self->y - (self->h / 2) - dist, self->w + (dist * 2), self->h + (dist * 2), self); candidates = getAllEntsInRadius(self->x, self->y, dist, self);
self->target = NULL; self->target = NULL;

View File

@ -36,7 +36,7 @@ extern float getAngle(int x1, int y1, int x2, int y2);
extern void applyFighterThrust(void); extern void applyFighterThrust(void);
extern void applyFighterBrakes(void); extern void applyFighterBrakes(void);
extern void addHudMessage(SDL_Color c, char *format, ...); extern void addHudMessage(SDL_Color c, char *format, ...);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);
extern Entity *spawnMine(int type); extern Entity *spawnMine(int type);

View File

@ -339,7 +339,7 @@ static void selectNewTarget(Bullet *b)
{ {
b->target = NULL; b->target = NULL;
candidates = getAllEntsWithin(b->x - (SCREEN_WIDTH / 2), b->y - (SCREEN_HEIGHT / 2), SCREEN_WIDTH, SCREEN_HEIGHT, NULL); candidates = getAllEntsInRadius(b->x, b->y, SCREEN_HEIGHT, NULL);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {

View File

@ -41,6 +41,7 @@ extern void addMissileEngineEffect(Bullet *b);
extern float mod(float n, float x); extern float mod(float n, float x);
extern void addMissileExplosion(Bullet *b); extern void addMissileExplosion(Bullet *b);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore);
extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void playSound(int id); extern void playSound(int id);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);

View File

@ -193,7 +193,7 @@ static int steer(void)
count = 0; count = 0;
force = 0; force = 0;
candidates = getAllEntsWithin(self->x - 1000, self->y - 1000, 2000, 2000, self); candidates = getAllEntsInRadius(self->x, self->y, 2000, self);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {

View File

@ -39,7 +39,7 @@ extern float mod(float n, float x);
extern void applyFighterThrust(void); extern void applyFighterThrust(void);
extern void addLargeEngineEffect(void); extern void addLargeEngineEffect(void);
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern void addDebris(int x, int y, int amount); extern void addDebris(int x, int y, int amount);
extern void runScriptFunction(char *format, ...); extern void runScriptFunction(char *format, ...);
extern void updateObjective(char *name, int type); extern void updateObjective(char *name, int type);

View File

@ -345,7 +345,7 @@ static void separate(void)
count = 0; count = 0;
force = 0; force = 0;
candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); candidates = getAllEntsInRadius(self->x, self->y, self->separationRadius, self);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {

View File

@ -33,7 +33,7 @@ extern void playBattleSound(int id, int x, int y);
extern void updateObjective(char *name, int type); extern void updateObjective(char *name, int type);
extern void updateCondition(char *name, int type); extern void updateCondition(char *name, int type);
extern void addHudMessage(SDL_Color c, char *format, ...); extern void addHudMessage(SDL_Color c, char *format, ...);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern Entity *spawnEntity(void); extern Entity *spawnEntity(void);
extern void adjustObjectiveTargetValue(char *name, int type, int amount); extern void adjustObjectiveTargetValue(char *name, int type, int amount);
extern void attachRope(void); extern void attachRope(void);

View File

@ -101,7 +101,7 @@ static void action(void)
Entity *e, **candidates; Entity *e, **candidates;
int i; int i;
candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); candidates = getAllEntsInRadius(self->x, self->y, MAX(self->w, self->h), self);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {

View File

@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern char *readFile(char *filename); extern char *readFile(char *filename);
extern SDL_Texture *getTexture(char *filename); extern SDL_Texture *getTexture(char *filename);
extern Entity *spawnEntity(void); extern Entity *spawnEntity(void);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern void playBattleSound(int id, int x, int y); extern void playBattleSound(int id, int x, int y);
extern void addHudMessage(SDL_Color c, char *format, ...); extern void addHudMessage(SDL_Color c, char *format, ...);

View File

@ -172,11 +172,11 @@ static void handleFleeingEntities(void)
Entity *e, **candidates; Entity *e, **candidates;
int i; int i;
candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); candidates = getAllEntsInRadius(self->x, self->y, ESCAPE_DISTANCE * 2, self);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {
if (e->health > 0 && e->flags & EF_RETREATING && getDistance(self->x, self->y, e->x, e->y) <= 255) if (e->health > 0 && e->flags & EF_RETREATING && getDistance(self->x, self->y, e->x, e->y) <= ESCAPE_DISTANCE)
{ {
e->alive = ALIVE_ESCAPED; e->alive = ALIVE_ESCAPED;

View File

@ -20,9 +20,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h" #include "../common.h"
#define ESCAPE_DISTANCE 256
extern SDL_Texture *getTexture(char *filename); extern SDL_Texture *getTexture(char *filename);
extern Entity *spawnEntity(void); extern Entity *spawnEntity(void);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern void playBattleSound(int id, int x, int y); extern void playBattleSound(int id, int x, int y);
extern void blitRotated(SDL_Texture *texture, int x, int y, float angle); extern void blitRotated(SDL_Texture *texture, int x, int y, float angle);

View File

@ -99,7 +99,7 @@ static void lookForFighters(void)
Entity *e, **candidates; Entity *e, **candidates;
int i; int i;
candidates = getAllEntsWithin(self->x - (self->w / 2) - DAMAGE_RANGE, self->y - (self->h / 2) - DAMAGE_RANGE, self->w + DAMAGE_RANGE, self->h + DAMAGE_RANGE, self); candidates = getAllEntsInRadius(self->x, self->y, DAMAGE_RANGE, self);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {
@ -199,7 +199,7 @@ static void doSplashDamage(void)
int i, dist, kills; int i, dist, kills;
float damage, percent; float damage, percent;
candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); candidates = getAllEntsInRadius(self->x, self->y, DAMAGE_RANGE, self);
kills = 0; kills = 0;

View File

@ -21,12 +21,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h" #include "../common.h"
#define TRIGGER_RANGE 150 #define TRIGGER_RANGE 150
#define DAMAGE_RANGE 275 #define DAMAGE_RANGE 250
#define SYSTEM_POWER 50 #define SYSTEM_POWER 50
extern Entity *spawnEntity(void); extern Entity *spawnEntity(void);
extern SDL_Texture *getTexture(char *filename); extern SDL_Texture *getTexture(char *filename);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern void addMineExplosion(void); extern void addMineExplosion(void);
extern void damageFighter(Entity *e, int amount, long flags); extern void damageFighter(Entity *e, int amount, long flags);

View File

@ -233,6 +233,11 @@ Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore)
return candidates; return candidates;
} }
Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore)
{
return getAllEntsWithin(x - radius / 2, y - radius / 2, radius, radius, ignore);
}
static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Quadtree *root) static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Quadtree *root)
{ {
int index, i; int index, i;

View File

@ -27,7 +27,7 @@ void attachRope(void)
if ((self->flags & EF_HAS_ROPE) && self->towing == NULL) if ((self->flags & EF_HAS_ROPE) && self->towing == NULL)
{ {
candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); candidates = getAllEntsInRadius(self->x, self->y, self->separationRadius, self);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {

View File

@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern float getAngle(int x1, int y1, int x2, int y2); extern float getAngle(int x1, int y1, int x2, int y2);
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsInRadius(int x, int y, int radius, Entity *ignore);
extern void addHudMessage(SDL_Color c, char *format, ...); extern void addHudMessage(SDL_Color c, char *format, ...);
extern void runScriptFunction(char *format, ...); extern void runScriptFunction(char *format, ...);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);