Code clean up.

This commit is contained in:
Steve 2015-12-14 08:15:41 +00:00
parent 2012f87b39
commit c4dfb823ef
7 changed files with 118 additions and 119 deletions

View File

@ -20,8 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "ai.h" #include "ai.h"
static void faceTarget(Entity *f); static void faceTarget(Entity *e);
static int isInFOV(Entity *f, int fov); static int isInFOV(Entity *e, int fov);
static void preAttack(void); static void preAttack(void);
static void huntTarget(void); static void huntTarget(void);
static void huntAndAttackTarget(void); static void huntAndAttackTarget(void);
@ -31,7 +31,7 @@ static void findTarget(void);
static int hasClearShot(void); static int hasClearShot(void);
static void fallback(void); static void fallback(void);
static void moveToPlayer(void); static void moveToPlayer(void);
static int canAttack(Entity *f); static int canAttack(Entity *e);
static int selectWeapon(int type); static int selectWeapon(int type);
static int nearExtractionPoint(void); static int nearExtractionPoint(void);
static void moveToExtractionPoint(void); static void moveToExtractionPoint(void);
@ -295,13 +295,13 @@ static void findTarget(void)
} }
} }
static int canAttack(Entity *f) static int canAttack(Entity *e)
{ {
self->selectedGunType = self->guns[0].type; self->selectedGunType = self->guns[0].type;
if (f->flags & EF_MUST_DISABLE) if (e->flags & EF_MUST_DISABLE)
{ {
if (f->systemPower > 0) if (e->systemPower > 0)
{ {
return selectWeapon(BT_MAG); return selectWeapon(BT_MAG);
} }
@ -309,12 +309,12 @@ static int canAttack(Entity *f)
return 0; return 0;
} }
if (f->flags & EF_NO_KILL) if (e->flags & EF_NO_KILL)
{ {
return selectWeapon(BT_LASER) || selectWeapon(BT_MAG); return selectWeapon(BT_LASER) || selectWeapon(BT_MAG);
} }
if (f->shield > 0) if (e->shield > 0)
{ {
selectWeapon(BT_LASER); selectWeapon(BT_LASER);
} }
@ -338,10 +338,10 @@ static int selectWeapon(int type)
return 0; return 0;
} }
static void faceTarget(Entity *f) static void faceTarget(Entity *e)
{ {
int dir; int dir;
int wantedAngle = getAngle(self->x, self->y, f->x, f->y); int wantedAngle = getAngle(self->x, self->y, e->x, e->y);
wantedAngle %= 360; wantedAngle %= 360;
@ -357,13 +357,13 @@ static void faceTarget(Entity *f)
} }
} }
static int isInFOV(Entity *f, int fov) static int isInFOV(Entity *e, int fov)
{ {
int angle, a, b; int angle, a, b;
a = mod(self->angle - fov, 360); a = mod(self->angle - fov, 360);
b = mod(self->angle + fov, 360); b = mod(self->angle + fov, 360);
angle = getAngle(self->x, self->y, f->x, f->y); angle = getAngle(self->x, self->y, e->x, e->y);
return (a < b) ? (a <= angle && angle <= b) : (a <= angle || angle <= b); return (a < b) ? (a <= angle && angle <= b) : (a <= angle || angle <= b);
} }

View File

@ -100,7 +100,6 @@ void doBullets(void)
if (--b->life <= 0) if (--b->life <= 0)
{ {
if (player != NULL && player->alive == ALIVE_ALIVE && b->type == BT_MISSILE && b->damage > 0 && b->target == player) if (player != NULL && player->alive == ALIVE_ALIVE && b->type == BT_MISSILE && b->damage > 0 && b->target == player)
{ {
battle.stats[STAT_MISSILES_EVADED]++; battle.stats[STAT_MISSILES_EVADED]++;
@ -117,7 +116,7 @@ void doBullets(void)
} }
else else
{ {
if (collision(b->x - b->w - battle.camera.x, b->y - b->h - battle.camera.y, b->w * 2, b->h * 2, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)) if (collision(b->x - (b->w / 2) - battle.camera.x, b->y - (b->h / 2) - battle.camera.y, b->w * 2, b->h * 2, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
{ {
bulletsToDraw[i++] = b; bulletsToDraw[i++] = b;
if (i >= MAX_BULLETS_TO_DRAW) if (i >= MAX_BULLETS_TO_DRAW)

View File

@ -29,7 +29,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern SDL_Texture *getTexture(char *filename); extern SDL_Texture *getTexture(char *filename);
extern void blitRotated(SDL_Texture *texture, int x, int y, int angle); extern void blitRotated(SDL_Texture *texture, int x, int y, int angle);
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 damageFighter(Entity *f, int damage, long flags); extern void damageFighter(Entity *e, int damage, long flags);
extern void playBattleSound(int id, int x, int y); extern void playBattleSound(int id, int x, int y);
extern long flagsToLong(char *flags, int *add); extern long flagsToLong(char *flags, int *add);
extern long lookup(char *name); extern long lookup(char *name);

View File

@ -34,68 +34,68 @@ static Entity defHead, *defTail;
Entity *spawnFighter(char *name, int x, int y, int side) Entity *spawnFighter(char *name, int x, int y, int side)
{ {
Entity *f, *def; Entity *e, *def;
f = spawnEntity(); e = spawnEntity();
def = getFighterDef(name); def = getFighterDef(name);
memcpy(f, def, sizeof(Entity)); memcpy(e, def, sizeof(Entity));
f->id = battle.entId; e->id = battle.entId;
f->next = NULL; e->next = NULL;
f->x = x; e->x = x;
f->y = y; e->y = y;
f->side = side; e->side = side;
switch (side) switch (side)
{ {
case SIDE_ALLIES: case SIDE_ALLIES:
f->aiAggression = 2 + rand() % 2; e->aiAggression = 2 + rand() % 2;
if (!(f->aiFlags & AIF_FOLLOWS_PLAYER)) if (!(e->aiFlags & AIF_FOLLOWS_PLAYER))
{ {
f->aiFlags |= AIF_MOVES_TO_PLAYER; e->aiFlags |= AIF_MOVES_TO_PLAYER;
} }
break; break;
case SIDE_PIRATE: case SIDE_PIRATE:
f->aiAggression = rand() % 3; e->aiAggression = rand() % 3;
break; break;
case SIDE_PANDORAN: case SIDE_PANDORAN:
f->aiAggression = 3 + rand() % 2; e->aiAggression = 3 + rand() % 2;
break; break;
case SIDE_REBEL: case SIDE_REBEL:
f->aiAggression = 1 + rand() % 3; e->aiAggression = 1 + rand() % 3;
break; break;
} }
if (strcmp(name, "ATAF") == 0) if (strcmp(name, "ATAF") == 0)
{ {
f->aiAggression = 4; e->aiAggression = 4;
} }
if (strcmp(name, "Dart") == 0) if (strcmp(name, "Dart") == 0)
{ {
randomizeDart(f); randomizeDart(e);
} }
if (strcmp(name, "Civilian") == 0 && rand() % 2 == 0) if (strcmp(name, "Civilian") == 0 && rand() % 2 == 0)
{ {
f->texture = getTexture("gfx/craft/civilian02.png"); e->texture = getTexture("gfx/craft/civilian02.png");
} }
if (f->aiFlags & AIF_AGGRESSIVE) if (e->aiFlags & AIF_AGGRESSIVE)
{ {
f->aiAggression = 4; e->aiAggression = 4;
} }
f->action = doAI; e->action = doAI;
f->die = die; e->die = die;
return f; return e;
} }
static void randomizeDart(Entity *dart) static void randomizeDart(Entity *dart)
@ -385,58 +385,58 @@ void applyFighterBrakes(void)
self->thrust = sqrt((self->dx * self->dx) + (self->dy * self->dy)); self->thrust = sqrt((self->dx * self->dx) + (self->dy * self->dy));
} }
void damageFighter(Entity *f, int amount, long flags) void damageFighter(Entity *e, int amount, long flags)
{ {
int prevShield = f->shield; int prevShield = e->shield;
if (flags & BF_SYSTEM_DAMAGE) if (flags & BF_SYSTEM_DAMAGE)
{ {
f->systemPower = MAX(0, f->systemPower - amount); e->systemPower = MAX(0, e->systemPower - amount);
f->systemHit = 255; e->systemHit = 255;
if (f->systemPower == 0) if (e->systemPower == 0)
{ {
f->shield = f->maxShield = 0; e->shield = e->maxShield = 0;
f->action = NULL; e->action = NULL;
} }
} }
else if (flags & BF_SHIELD_DAMAGE) else if (flags & BF_SHIELD_DAMAGE)
{ {
f->shield = MAX(-(FPS * 10), f->shield - amount); e->shield = MAX(-(FPS * 10), e->shield - amount);
if (f->shield <= 0 && prevShield > 0) if (e->shield <= 0 && prevShield > 0)
{ {
playBattleSound(SND_SHIELD_BREAK, f->x, f->y); playBattleSound(SND_SHIELD_BREAK, e->x, e->y);
addShieldSplinterEffect(f); addShieldSplinterEffect(e);
} }
} }
else else
{ {
if (f->shield > 0) if (e->shield > 0)
{ {
f->shield -= amount; e->shield -= amount;
if (f->shield < 0) if (e->shield < 0)
{ {
f->health += f->shield; e->health += e->shield;
f->shield = 0; e->shield = 0;
} }
} }
else else
{ {
f->health -= amount; e->health -= amount;
f->armourHit = 255; e->armourHit = 255;
playBattleSound(SND_ARMOUR_HIT, f->x, f->y); playBattleSound(SND_ARMOUR_HIT, e->x, e->y);
} }
} }
if (f->shield > 0) if (e->shield > 0)
{ {
f->shieldHit = 255; e->shieldHit = 255;
playBattleSound(SND_SHIELD_HIT, f->x, f->y); playBattleSound(SND_SHIELD_HIT, e->x, e->y);
} }
} }
@ -556,13 +556,13 @@ void retreatAllies(void)
static Entity *getFighterDef(char *name) static Entity *getFighterDef(char *name)
{ {
Entity *f; Entity *e;
for (f = defHead.next ; f != NULL ; f = f->next) for (e = defHead.next ; e != NULL ; e = e->next)
{ {
if (strcmp(f->name, name) == 0) if (strcmp(e->name, name) == 0)
{ {
return f; return e;
} }
} }
@ -594,33 +594,33 @@ static void loadFighterDef(char *filename)
{ {
cJSON *root, *node; cJSON *root, *node;
char *text; char *text;
Entity *f; Entity *e;
int i; int i;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(getFileLocation(filename)); text = readFile(getFileLocation(filename));
f = malloc(sizeof(Entity)); e = malloc(sizeof(Entity));
memset(f, 0, sizeof(Entity)); memset(e, 0, sizeof(Entity));
defTail->next = f; defTail->next = e;
defTail = f; defTail = e;
f->type = ET_FIGHTER; e->type = ET_FIGHTER;
f->active = 1; e->active = 1;
root = cJSON_Parse(text); root = cJSON_Parse(text);
STRNCPY(f->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(f->defName, f->name, MAX_NAME_LENGTH); STRNCPY(e->defName, e->name, MAX_NAME_LENGTH);
f->health = f->maxHealth = cJSON_GetObjectItem(root, "health")->valueint; e->health = e->maxHealth = cJSON_GetObjectItem(root, "health")->valueint;
f->shield = f->maxShield = cJSON_GetObjectItem(root, "shield")->valueint; e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint;
f->speed = cJSON_GetObjectItem(root, "speed")->valuedouble; e->speed = cJSON_GetObjectItem(root, "speed")->valuedouble;
f->reloadTime = cJSON_GetObjectItem(root, "reloadTime")->valueint; e->reloadTime = cJSON_GetObjectItem(root, "reloadTime")->valueint;
f->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint; e->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint;
f->texture = getTexture(cJSON_GetObjectItem(root, "textureName")->valuestring); e->texture = getTexture(cJSON_GetObjectItem(root, "textureName")->valuestring);
SDL_QueryTexture(f->texture, NULL, NULL, &f->w, &f->h); SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
if (cJSON_GetObjectItem(root, "guns")) if (cJSON_GetObjectItem(root, "guns"))
{ {
@ -628,9 +628,9 @@ static void loadFighterDef(char *filename)
for (node = cJSON_GetObjectItem(root, "guns")->child ; node != NULL ; node = node->next) for (node = cJSON_GetObjectItem(root, "guns")->child ; node != NULL ; node = node->next)
{ {
f->guns[i].type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); e->guns[i].type = lookup(cJSON_GetObjectItem(node, "type")->valuestring);
f->guns[i].x = cJSON_GetObjectItem(node, "x")->valueint; e->guns[i].x = cJSON_GetObjectItem(node, "x")->valueint;
f->guns[i].y = cJSON_GetObjectItem(node, "y")->valueint; e->guns[i].y = cJSON_GetObjectItem(node, "y")->valueint;
i++; i++;
@ -643,32 +643,32 @@ static void loadFighterDef(char *filename)
if (cJSON_GetObjectItem(root, "combinedGuns")) if (cJSON_GetObjectItem(root, "combinedGuns"))
{ {
f->combinedGuns = cJSON_GetObjectItem(root, "combinedGuns")->valueint; e->combinedGuns = cJSON_GetObjectItem(root, "combinedGuns")->valueint;
} }
} }
f->selectedGunType = f->guns[0].type; e->selectedGunType = e->guns[0].type;
if (cJSON_GetObjectItem(root, "missiles")) if (cJSON_GetObjectItem(root, "missiles"))
{ {
f->missiles = cJSON_GetObjectItem(root, "missiles")->valueint; e->missiles = cJSON_GetObjectItem(root, "missiles")->valueint;
} }
if (cJSON_GetObjectItem(root, "flags")) if (cJSON_GetObjectItem(root, "flags"))
{ {
f->flags = flagsToLong(cJSON_GetObjectItem(root, "flags")->valuestring, NULL); e->flags = flagsToLong(cJSON_GetObjectItem(root, "flags")->valuestring, NULL);
} }
if (cJSON_GetObjectItem(root, "aiFlags")) if (cJSON_GetObjectItem(root, "aiFlags"))
{ {
f->aiFlags = flagsToLong(cJSON_GetObjectItem(root, "aiFlags")->valuestring, NULL); e->aiFlags = flagsToLong(cJSON_GetObjectItem(root, "aiFlags")->valuestring, NULL);
} }
f->separationRadius = MAX(f->w, f->h); e->separationRadius = MAX(e->w, e->h);
f->separationRadius *= 3; e->separationRadius *= 3;
/* all craft default to 100 system power */ /* all craft default to 100 system power */
f->systemPower = 100; e->systemPower = 100;
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
@ -676,12 +676,12 @@ static void loadFighterDef(char *filename)
void destroyFighterDefs(void) void destroyFighterDefs(void)
{ {
Entity *f; Entity *e;
while (defHead.next) while (defHead.next)
{ {
f = defHead.next; e = defHead.next;
defHead.next = f->next; defHead.next = e->next;
free(f); free(e);
} }
} }

View File

@ -124,12 +124,12 @@ static void action(void)
void destroyItemDefs(void) void destroyItemDefs(void)
{ {
Entity *f; Entity *e;
while (defHead.next) while (defHead.next)
{ {
f = defHead.next; e = defHead.next;
defHead.next = f->next; defHead.next = e->next;
free(f); free(e);
} }
} }

View File

@ -33,7 +33,7 @@ void initRadar(void)
void drawRadar(void) void drawRadar(void)
{ {
SDL_Rect r; SDL_Rect r;
Entity *f; Entity *e;
blit(radarTexture, SCREEN_WIDTH - 85, SCREEN_HEIGHT - 85, 1); blit(radarTexture, SCREEN_WIDTH - 85, SCREEN_HEIGHT - 85, 1);
@ -41,20 +41,20 @@ void drawRadar(void)
r.w = r.h = 3; r.w = r.h = 3;
for (f = battle.entityHead.next ; f != NULL ; f = f->next) for (e = battle.entityHead.next ; e != NULL ; e = e->next)
{ {
if (f->active && getDistance(f->x, f->y, player->x, player->y) / radarRanges[battle.radarRange] < 70) if (e->active && getDistance(e->x, e->y, player->x, player->y) / radarRanges[battle.radarRange] < 70)
{ {
r.x = SCREEN_WIDTH - 85; r.x = SCREEN_WIDTH - 85;
r.y = SCREEN_HEIGHT - 85; r.y = SCREEN_HEIGHT - 85;
r.x -= (player->x - f->x) / radarRanges[battle.radarRange]; r.x -= (player->x - e->x) / radarRanges[battle.radarRange];
r.y -= (player->y - f->y) / radarRanges[battle.radarRange]; r.y -= (player->y - e->y) / radarRanges[battle.radarRange];
r.x--; r.x--;
r.y--; r.y--;
switch (f->side) switch (e->side)
{ {
case SIDE_ALLIES: case SIDE_ALLIES:
SDL_SetRenderDrawColor(app.renderer, 0, 255, 0, 255); SDL_SetRenderDrawColor(app.renderer, 0, 255, 0, 255);
@ -71,12 +71,12 @@ void drawRadar(void)
break; break;
} }
if (f == player->target) if (e == player->target)
{ {
SDL_SetRenderDrawColor(app.renderer, 255, 255, 0, 255); SDL_SetRenderDrawColor(app.renderer, 255, 255, 0, 255);
} }
if (f == battle.missionTarget) if (e == battle.missionTarget)
{ {
SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255);
} }

View File

@ -198,7 +198,7 @@ static void loadPlayer(cJSON *node)
static void loadFighters(cJSON *node) static void loadFighters(cJSON *node)
{ {
Entity *f; Entity *e;
char **types, *name, *groupName, *type; char **types, *name, *groupName, *type;
int side, scatter, number, active; int side, scatter, number, active;
int i, numTypes, addFlags, addAIFlags; int i, numTypes, addFlags, addAIFlags;
@ -263,27 +263,27 @@ static void loadFighters(cJSON *node)
{ {
type = types[rand() % numTypes]; type = types[rand() % numTypes];
f = spawnFighter(type, x, y, side); e = spawnFighter(type, x, y, side);
if (scatter > 1) if (scatter > 1)
{ {
f->x += (rand() % scatter) - (rand() % scatter); e->x += (rand() % scatter) - (rand() % scatter);
f->y += (rand() % scatter) - (rand() % scatter); e->y += (rand() % scatter) - (rand() % scatter);
} }
f->active = active; e->active = active;
if (flags != -1) if (flags != -1)
{ {
if (addFlags) if (addFlags)
{ {
f->flags |= flags; e->flags |= flags;
} }
else else
{ {
f->flags = flags; e->flags = flags;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Flags for '%s' (%s) replaced", f->name, f->defName); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Flags for '%s' (%s) replaced", e->name, e->defName);
} }
} }
@ -291,24 +291,24 @@ static void loadFighters(cJSON *node)
{ {
if (addAIFlags) if (addAIFlags)
{ {
f->aiFlags |= aiFlags; e->aiFlags |= aiFlags;
} }
else else
{ {
f->aiFlags = aiFlags; e->aiFlags = aiFlags;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "AI Flags for '%s' (%s) replaced", f->name, f->defName); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "AI Flags for '%s' (%s) replaced", e->name, e->defName);
} }
} }
if (name) if (name)
{ {
STRNCPY(f->name, name, MAX_NAME_LENGTH); STRNCPY(e->name, name, MAX_NAME_LENGTH);
} }
if (groupName) if (groupName)
{ {
STRNCPY(f->groupName, groupName, MAX_NAME_LENGTH); STRNCPY(e->groupName, groupName, MAX_NAME_LENGTH);
} }
} }