2015-12-07 20:19:14 +01:00
|
|
|
/*
|
2016-02-21 16:50:27 +01:00
|
|
|
Copyright (C) 2015-2016 Parallel Realities
|
2015-12-07 20:19:14 +01:00
|
|
|
|
|
|
|
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 "capitalShips.h"
|
|
|
|
|
2016-02-22 13:47:30 +01:00
|
|
|
static int steer(void);
|
2015-12-08 23:42:31 +01:00
|
|
|
static void think(void);
|
|
|
|
static void gunThink(void);
|
2015-12-10 12:47:18 +01:00
|
|
|
static void gunDie(void);
|
2015-12-07 20:19:14 +01:00
|
|
|
static void componentDie(void);
|
2015-12-12 16:11:40 +01:00
|
|
|
static void engineThink(void);
|
2015-12-10 12:11:04 +01:00
|
|
|
static void engineDie(void);
|
2015-12-12 18:12:25 +01:00
|
|
|
static void findAITarget(void);
|
2015-12-07 20:19:14 +01:00
|
|
|
static void loadCapitalShipDef(char *filename);
|
|
|
|
static void loadComponents(Entity *parent, cJSON *components);
|
|
|
|
static void loadGuns(Entity *parent, cJSON *guns);
|
2015-12-10 12:11:04 +01:00
|
|
|
static void loadEngines(Entity *parent, cJSON *engines);
|
2015-12-07 20:19:14 +01:00
|
|
|
|
|
|
|
static Entity defHead, *defTail;
|
|
|
|
|
|
|
|
Entity *spawnCapitalShip(char *name, int x, int y, int side)
|
|
|
|
{
|
|
|
|
Entity *def, *e, *capitalShip;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-16 23:40:14 +01:00
|
|
|
capitalShip = NULL;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
for (def = defHead.next ; def != NULL ; def = def->next)
|
|
|
|
{
|
2015-12-09 08:25:24 +01:00
|
|
|
if ((strcmp(def->name, name) == 0) || (def->owner != NULL && strcmp(def->owner->name, name) == 0))
|
2015-12-07 20:19:14 +01:00
|
|
|
{
|
|
|
|
e = spawnEntity();
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
memcpy(e, def, sizeof(Entity));
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e->id = battle.entId;
|
|
|
|
e->next = NULL;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e->x = x;
|
|
|
|
e->y = y;
|
|
|
|
e->side = side;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
if (e->type == ET_CAPITAL_SHIP)
|
|
|
|
{
|
|
|
|
capitalShip = e;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
e->owner = capitalShip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-16 23:40:14 +01:00
|
|
|
if (!capitalShip)
|
|
|
|
{
|
|
|
|
printf("Error: no such capital ship '%s'\n", name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
return capitalShip;
|
|
|
|
}
|
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
void doCapitalShip(void)
|
|
|
|
{
|
|
|
|
if (self->alive == ALIVE_ALIVE)
|
|
|
|
{
|
|
|
|
if (self->health <= 0)
|
|
|
|
{
|
|
|
|
self->health = 0;
|
|
|
|
self->alive = ALIVE_DYING;
|
|
|
|
self->die();
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
if (self == battle.missionTarget)
|
|
|
|
{
|
|
|
|
battle.missionTarget = NULL;
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-19 17:15:57 +01:00
|
|
|
if (self->side == SIDE_ALLIES)
|
|
|
|
{
|
|
|
|
battle.stats[STAT_CAPITAL_SHIPS_LOST]++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
battle.stats[STAT_CAPITAL_SHIPS_DESTROYED]++;
|
|
|
|
}
|
2015-12-12 18:12:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
static void think(void)
|
|
|
|
{
|
2015-12-10 11:04:22 +01:00
|
|
|
float dir;
|
|
|
|
int wantedAngle;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
if (--self->aiActionTime <= 0)
|
|
|
|
{
|
2015-12-12 18:12:25 +01:00
|
|
|
findAITarget();
|
2015-12-10 11:04:22 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 13:47:30 +01:00
|
|
|
wantedAngle = steer();
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
if (fabs(wantedAngle - self->angle) > TURN_THRESHOLD)
|
|
|
|
{
|
|
|
|
dir = ((int)(wantedAngle - self->angle + 360)) % 360 > 180 ? -1 : 1;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
dir *= TURN_SPEED;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
self->angle += dir;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
self->angle = fmod(self->angle, 360);
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
applyFighterThrust();
|
2015-12-08 23:42:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
static void findAITarget(void)
|
|
|
|
{
|
|
|
|
Entity *e;
|
|
|
|
unsigned int dist, closest;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
self->target = NULL;
|
|
|
|
dist = closest = MAX_TARGET_RANGE;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
|
|
|
|
{
|
|
|
|
if (e->active && e->side != self->side && e->flags & EF_AI_TARGET)
|
|
|
|
{
|
|
|
|
dist = getDistance(self->x, self->y, e->x, e->y);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
if (!self->target || dist < closest)
|
|
|
|
{
|
|
|
|
self->target = e;
|
|
|
|
closest = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
if (self->target)
|
|
|
|
{
|
|
|
|
self->targetLocation.x = self->target->x + (rand() % 1000 - rand() % 1000);
|
|
|
|
self->targetLocation.y = self->target->y + (rand() % 1000 - rand() % 1000);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
self->aiActionTime = FPS * 15;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-21 08:48:22 +01:00
|
|
|
self->targetLocation.x = 500 + (rand() % (BATTLE_AREA_WIDTH - 1000));
|
|
|
|
self->targetLocation.y = 500 + (rand() % (BATTLE_AREA_HEIGHT - 1000));
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
self->aiActionTime = FPS * (30 + (rand() % 120));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 13:47:30 +01:00
|
|
|
static int steer(void)
|
|
|
|
{
|
|
|
|
int wantedAngle;
|
2016-02-22 22:46:46 +01:00
|
|
|
int angle;
|
|
|
|
int distance;
|
|
|
|
float dx, dy, force;
|
|
|
|
int count;
|
|
|
|
Entity *e, **candidates;
|
|
|
|
int i;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
dx = dy = 0;
|
|
|
|
count = 0;
|
|
|
|
force = 0;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
candidates = getAllEntsWithin(self->x - 1000, self->y - 1000, 2000, 2000, self);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
|
|
|
|
{
|
|
|
|
if (e->type == ET_CAPITAL_SHIP)
|
|
|
|
{
|
|
|
|
distance = getDistance(e->x, e->y, self->x, self->y);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
if (distance > 0 && distance < self->separationRadius)
|
|
|
|
{
|
|
|
|
angle = getAngle(self->x, self->y, e->x, e->y);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
dx += sin(TO_RAIDANS(angle));
|
|
|
|
dy += -cos(TO_RAIDANS(angle));
|
|
|
|
force += (self->separationRadius - distance);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
if (count > 0)
|
2016-02-22 13:47:30 +01:00
|
|
|
{
|
2016-02-22 22:46:46 +01:00
|
|
|
dx /= count;
|
|
|
|
dy /= count;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
dx *= force;
|
|
|
|
dy *= force;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
self->dx -= (dx * 0.001);
|
|
|
|
self->dy -= (dy * 0.001);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
self->targetLocation.x -= dx;
|
|
|
|
self->targetLocation.y -= dy;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
self->aiActionTime = FPS * 10;
|
2016-02-22 13:47:30 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
wantedAngle = getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 13:47:30 +01:00
|
|
|
wantedAngle %= 360;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 13:47:30 +01:00
|
|
|
return wantedAngle;
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
static void gunThink(void)
|
|
|
|
{
|
2015-12-10 11:04:22 +01:00
|
|
|
doAI();
|
2015-12-08 23:42:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
static void componentDie(void)
|
|
|
|
{
|
|
|
|
self->alive = ALIVE_DEAD;
|
|
|
|
addSmallExplosion();
|
|
|
|
playBattleSound(SND_EXPLOSION_1 + rand() % 4, self->x, self->y);
|
2015-12-18 13:02:45 +01:00
|
|
|
addDebris(self->x, self->y, 3 + rand() % 4);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
self->owner->health--;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-13 18:54:57 +01:00
|
|
|
if (self->owner->health > 0)
|
|
|
|
{
|
|
|
|
runScriptFunction("CAP_HEALTH %s %d", self->owner->name, self->owner->health);
|
|
|
|
}
|
2015-12-10 12:11:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 12:47:18 +01:00
|
|
|
static void gunDie(void)
|
|
|
|
{
|
|
|
|
self->alive = ALIVE_DEAD;
|
|
|
|
addSmallExplosion();
|
|
|
|
playBattleSound(SND_EXPLOSION_1 + rand() % 4, self->x, self->y);
|
2015-12-18 13:02:45 +01:00
|
|
|
addDebris(self->x, self->y, 3 + rand() % 4);
|
2015-12-10 12:47:18 +01:00
|
|
|
}
|
|
|
|
|
2015-12-12 16:11:40 +01:00
|
|
|
static void engineThink(void)
|
|
|
|
{
|
|
|
|
addLargeEngineEffect();
|
|
|
|
}
|
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
static void engineDie(void)
|
|
|
|
{
|
|
|
|
Entity *e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
self->alive = ALIVE_DEAD;
|
|
|
|
addSmallExplosion();
|
|
|
|
playBattleSound(SND_EXPLOSION_1 + rand() % 4, self->x, self->y);
|
2015-12-13 18:54:57 +01:00
|
|
|
addDebris(self->x, self->y, 4 + rand() % 9);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
|
2015-12-09 08:25:24 +01:00
|
|
|
{
|
2015-12-10 12:11:04 +01:00
|
|
|
if (e != self && e->owner == self->owner && e->type == ET_CAPITAL_SHIP_ENGINE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-12-09 08:25:24 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
/* no more engines - stop moving */
|
2015-12-13 18:54:57 +01:00
|
|
|
if (self->owner->health > 0)
|
|
|
|
{
|
|
|
|
self->owner->speed = 0;
|
|
|
|
self->owner->action = NULL;
|
|
|
|
self->owner->dx = self->owner->dy = 0;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-13 18:54:57 +01:00
|
|
|
runScriptFunction("CAP_ENGINES_DESTROYED %s", self->owner->name);
|
|
|
|
}
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void die(void)
|
|
|
|
{
|
2015-12-10 11:04:22 +01:00
|
|
|
Entity *e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 07:43:03 +01:00
|
|
|
self->alive = ALIVE_DEAD;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-13 18:54:57 +01:00
|
|
|
addDebris(self->x, self->y, 50);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 11:04:22 +01:00
|
|
|
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
|
|
|
|
{
|
|
|
|
if (e->owner == self)
|
|
|
|
{
|
|
|
|
e->health = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 13:02:45 +01:00
|
|
|
updateObjective(self->name, TT_DESTROY);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 13:02:45 +01:00
|
|
|
updateObjective(self->groupName, TT_DESTROY);
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loadCapitalShipDefs(void)
|
|
|
|
{
|
2016-02-27 17:14:14 +01:00
|
|
|
char **filenames;
|
|
|
|
char path[MAX_FILENAME_LENGTH];
|
|
|
|
int count, i;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
memset(&defHead, 0, sizeof(Entity));
|
|
|
|
defTail = &defHead;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-03-04 15:29:50 +01:00
|
|
|
filenames = getFileList("data/capitalShips", &count);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-27 17:14:14 +01:00
|
|
|
for (i = 0 ; i < count ; i++)
|
2015-12-07 20:19:14 +01:00
|
|
|
{
|
2016-02-27 17:14:14 +01:00
|
|
|
sprintf(path, "data/capitalShips/%s", filenames[i]);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-27 17:14:14 +01:00
|
|
|
loadCapitalShipDef(path);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-27 17:14:14 +01:00
|
|
|
free(filenames[i]);
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-27 17:14:14 +01:00
|
|
|
free(filenames);
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void loadCapitalShipDef(char *filename)
|
|
|
|
{
|
|
|
|
cJSON *root;
|
|
|
|
char *text;
|
|
|
|
Entity *e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-03-04 15:29:50 +01:00
|
|
|
text = readFile(filename);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e = malloc(sizeof(Entity));
|
|
|
|
memset(e, 0, sizeof(Entity));
|
|
|
|
defTail->next = e;
|
|
|
|
defTail = e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e->type = ET_CAPITAL_SHIP;
|
|
|
|
e->active = 1;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
root = cJSON_Parse(text);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
STRNCPY(e->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
|
|
|
|
STRNCPY(e->defName, e->name, MAX_NAME_LENGTH);
|
|
|
|
e->shield = e->maxShield = cJSON_GetObjectItem(root, "shield")->valueint;
|
|
|
|
e->shieldRechargeRate = cJSON_GetObjectItem(root, "shieldRechargeRate")->valueint;
|
|
|
|
e->texture = getTexture(cJSON_GetObjectItem(root, "texture")->valuestring);
|
2015-12-10 11:04:22 +01:00
|
|
|
e->speed = 1;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
e->action = think;
|
2015-12-07 20:19:14 +01:00
|
|
|
e->die = die;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 18:12:25 +01:00
|
|
|
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2016-02-22 22:46:46 +01:00
|
|
|
e->separationRadius = MAX(e->w, e->h);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
loadComponents(e, cJSON_GetObjectItem(root, "components"));
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
loadGuns(e, cJSON_GetObjectItem(root, "guns"));
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
loadEngines(e, cJSON_GetObjectItem(root, "engines"));
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
cJSON_Delete(root);
|
|
|
|
free(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadComponents(Entity *parent, cJSON *components)
|
|
|
|
{
|
|
|
|
Entity *e;
|
|
|
|
cJSON *component;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 09:26:21 +01:00
|
|
|
parent->health = 0;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
if (components)
|
|
|
|
{
|
|
|
|
component = components->child;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
while (component)
|
|
|
|
{
|
|
|
|
e = malloc(sizeof(Entity));
|
|
|
|
memset(e, 0, sizeof(Entity));
|
|
|
|
defTail->next = e;
|
|
|
|
defTail = e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e->active = 1;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e->type = ET_CAPITAL_SHIP_COMPONENT;
|
|
|
|
e->health = e->maxHealth = cJSON_GetObjectItem(component, "health")->valueint;
|
|
|
|
e->offsetX = cJSON_GetObjectItem(component, "x")->valueint;
|
|
|
|
e->offsetY = cJSON_GetObjectItem(component, "y")->valueint;
|
|
|
|
e->texture = getTexture(cJSON_GetObjectItem(component, "texture")->valuestring);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 19:24:23 +01:00
|
|
|
if (cJSON_GetObjectItem(component, "flags"))
|
2015-12-08 23:42:31 +01:00
|
|
|
{
|
2015-12-12 19:24:23 +01:00
|
|
|
e->flags = flagsToLong(cJSON_GetObjectItem(component, "flags")->valuestring, NULL);
|
2015-12-08 23:42:31 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 19:24:23 +01:00
|
|
|
if (cJSON_GetObjectItem(component, "aiFlags"))
|
2015-12-07 20:19:14 +01:00
|
|
|
{
|
2015-12-12 19:24:23 +01:00
|
|
|
e->aiFlags = flagsToLong(cJSON_GetObjectItem(component, "aiFlags")->valuestring, NULL);
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-09 08:25:24 +01:00
|
|
|
e->systemPower = 100;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
e->die = componentDie;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-09 08:25:24 +01:00
|
|
|
e->owner = parent;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
component = component->next;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 07:43:03 +01:00
|
|
|
parent->health++;
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 11:12:37 +01:00
|
|
|
parent->maxHealth = parent->health;
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void loadGuns(Entity *parent, cJSON *guns)
|
|
|
|
{
|
2015-12-08 23:42:31 +01:00
|
|
|
Entity *e;
|
|
|
|
cJSON *gun;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
if (guns)
|
|
|
|
{
|
|
|
|
gun = guns->child;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
while (gun)
|
|
|
|
{
|
|
|
|
e = malloc(sizeof(Entity));
|
|
|
|
memset(e, 0, sizeof(Entity));
|
|
|
|
defTail->next = e;
|
|
|
|
defTail = e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
e->active = 1;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
e->type = ET_CAPITAL_SHIP_GUN;
|
|
|
|
e->health = e->maxHealth = cJSON_GetObjectItem(gun, "health")->valueint;
|
|
|
|
e->reloadTime = cJSON_GetObjectItem(gun, "reloadTime")->valueint;
|
|
|
|
e->offsetX = cJSON_GetObjectItem(gun, "x")->valueint;
|
|
|
|
e->offsetY = cJSON_GetObjectItem(gun, "y")->valueint;
|
|
|
|
e->texture = getTexture(cJSON_GetObjectItem(gun, "texture")->valuestring);
|
|
|
|
e->guns[0].type = lookup(cJSON_GetObjectItem(gun, "type")->valuestring);
|
2016-03-04 15:14:04 +01:00
|
|
|
e->missiles = getJSONValue(gun, "missiles", 0);
|
|
|
|
|
2015-12-12 19:24:23 +01:00
|
|
|
if (cJSON_GetObjectItem(gun, "flags"))
|
2015-12-08 23:42:31 +01:00
|
|
|
{
|
2015-12-12 19:24:23 +01:00
|
|
|
e->flags = flagsToLong(cJSON_GetObjectItem(gun, "flags")->valuestring, NULL);
|
2015-12-08 23:42:31 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 19:24:23 +01:00
|
|
|
if (cJSON_GetObjectItem(gun, "aiFlags"))
|
2015-12-08 23:42:31 +01:00
|
|
|
{
|
2015-12-12 19:24:23 +01:00
|
|
|
e->aiFlags = flagsToLong(cJSON_GetObjectItem(gun, "aiFlags")->valuestring, NULL);
|
2015-12-08 23:42:31 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-09 08:25:24 +01:00
|
|
|
e->systemPower = 100;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-08 23:42:31 +01:00
|
|
|
e->action = gunThink;
|
2015-12-10 12:47:18 +01:00
|
|
|
e->die = gunDie;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-09 08:25:24 +01:00
|
|
|
e->owner = parent;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-09 08:25:24 +01:00
|
|
|
gun = gun->next;
|
2015-12-08 23:42:31 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-07 20:19:14 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
static void loadEngines(Entity *parent, cJSON *engines)
|
|
|
|
{
|
|
|
|
Entity *e;
|
|
|
|
cJSON *engine;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
if (engines)
|
|
|
|
{
|
|
|
|
engine = engines->child;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
while (engine)
|
|
|
|
{
|
|
|
|
e = malloc(sizeof(Entity));
|
|
|
|
memset(e, 0, sizeof(Entity));
|
|
|
|
defTail->next = e;
|
|
|
|
defTail = e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
e->active = 1;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
e->type = ET_CAPITAL_SHIP_ENGINE;
|
|
|
|
e->health = e->maxHealth = cJSON_GetObjectItem(engine, "health")->valueint;
|
|
|
|
e->offsetX = cJSON_GetObjectItem(engine, "x")->valueint;
|
|
|
|
e->offsetY = cJSON_GetObjectItem(engine, "y")->valueint;
|
|
|
|
e->texture = getTexture(cJSON_GetObjectItem(engine, "texture")->valuestring);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
if (cJSON_GetObjectItem(engine, "flags"))
|
|
|
|
{
|
2015-12-12 19:24:23 +01:00
|
|
|
e->flags = flagsToLong(cJSON_GetObjectItem(engine, "flags")->valuestring, NULL);
|
2015-12-10 12:11:04 +01:00
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
e->systemPower = 100;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-12 16:11:40 +01:00
|
|
|
e->action = engineThink;
|
2015-12-10 12:11:04 +01:00
|
|
|
e->die = engineDie;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
e->owner = parent;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-10 12:11:04 +01:00
|
|
|
engine = engine->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 19:43:47 +01:00
|
|
|
void updateCapitalShipComponentProperties(Entity *parent)
|
2015-12-18 13:02:45 +01:00
|
|
|
{
|
|
|
|
Entity *e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 13:02:45 +01:00
|
|
|
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
|
|
|
|
{
|
|
|
|
if (e->owner == parent)
|
|
|
|
{
|
|
|
|
switch (e->type)
|
|
|
|
{
|
|
|
|
case ET_CAPITAL_SHIP_ENGINE:
|
|
|
|
sprintf(e->name, "%s (Engine)", parent->name);
|
|
|
|
break;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 13:02:45 +01:00
|
|
|
case ET_CAPITAL_SHIP_COMPONENT:
|
|
|
|
sprintf(e->name, "%s (Component)", parent->name);
|
|
|
|
break;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-18 13:02:45 +01:00
|
|
|
case ET_CAPITAL_SHIP_GUN:
|
|
|
|
sprintf(e->name, "%s (Gun)", parent->name);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-30 19:43:47 +01:00
|
|
|
e->active = parent->active;
|
2015-12-18 13:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
void destroyCapitalShipDefs(void)
|
|
|
|
{
|
|
|
|
Entity *e;
|
2016-03-04 15:14:04 +01:00
|
|
|
|
2015-12-07 20:19:14 +01:00
|
|
|
while (defHead.next)
|
|
|
|
{
|
|
|
|
e = defHead.next;
|
|
|
|
defHead.next = e->next;
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
}
|