tbftss/src/galaxy/mission.c

470 lines
10 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2016-02-21 16:50:27 +01:00
Copyright (C) 2015-2016 Parallel Realities
2015-10-20 13:51:49 +02: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 "mission.h"
2015-10-26 18:27:43 +01:00
static void loadEntities(cJSON *node);
2015-10-20 13:51:49 +02:00
static unsigned long hashcode(const char *str);
static void loadEpicData(cJSON *node);
static char *getAutoBackground(char *filename);
static char *getAutoPlanet(char *filename);
static char *getAutoMusic(char *filename);
2015-10-20 13:51:49 +02:00
2016-02-27 13:14:29 +01:00
Mission *loadMissionMeta(char *filename)
{
Mission *mission;
cJSON *root, *node;
char *text;
2016-02-27 13:14:29 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(filename);
2016-02-27 13:14:29 +01:00
root = cJSON_Parse(text);
mission = NULL;
if (root)
2016-02-27 13:14:29 +01:00
{
mission = malloc(sizeof(Mission));
memset(mission, 0, sizeof(Mission));
STRNCPY(mission->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(mission->description, _(cJSON_GetObjectItem(root, "description")->valuestring), MAX_DESCRIPTION_LENGTH);
STRNCPY(mission->filename, filename, MAX_DESCRIPTION_LENGTH);
mission->requires = getJSONValue(root, "requires", 0);
if (cJSON_GetObjectItem(root, "epic"))
{
mission->epic = 1;
}
node = cJSON_GetObjectItem(root, "player");
2016-02-28 14:02:57 +01:00
if (node)
2016-02-27 13:14:29 +01:00
{
STRNCPY(mission->pilot, cJSON_GetObjectItem(node, "pilot")->valuestring, MAX_NAME_LENGTH);
STRNCPY(mission->squadron, cJSON_GetObjectItem(node, "squadron")->valuestring, MAX_NAME_LENGTH);
STRNCPY(mission->craft, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH);
}
node = cJSON_GetObjectItem(root, "challenge");
if (node)
{
2016-04-13 12:04:28 +02:00
loadChallenge(mission, node);
2016-02-27 13:14:29 +01:00
}
cJSON_Delete(root);
}
2016-03-12 19:22:48 +01:00
else
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Failed to load '%s'", filename);
}
2016-02-27 13:14:29 +01:00
free(text);
2016-02-27 13:14:29 +01:00
return mission;
}
2015-10-20 13:51:49 +02:00
void loadMission(char *filename)
{
cJSON *root;
char *text, music[MAX_DESCRIPTION_LENGTH], *background, *planet;
2016-02-21 14:47:10 +01:00
float planetScale;
2015-10-20 13:51:49 +02:00
startSectionTransition();
2015-10-20 13:51:49 +02:00
stopMusic();
text = readFile(filename);
2015-10-20 13:51:49 +02:00
root = cJSON_Parse(text);
srand(hashcode(filename));
2015-10-20 13:51:49 +02:00
loadObjectives(cJSON_GetObjectItem(root, "objectives"));
2015-10-20 13:51:49 +02:00
loadPlayer(cJSON_GetObjectItem(root, "player"));
2015-10-20 13:51:49 +02:00
loadFighters(cJSON_GetObjectItem(root, "fighters"));
2015-12-07 20:19:14 +01:00
loadCapitalShips(cJSON_GetObjectItem(root, "capitalShips"));
2015-10-26 18:27:43 +01:00
loadEntities(cJSON_GetObjectItem(root, "entities"));
loadItems(cJSON_GetObjectItem(root, "items"));
loadLocations(cJSON_GetObjectItem(root, "locations"));
2016-03-27 12:21:23 +02:00
loadSpawners(cJSON_GetObjectItem(root, "spawners"));
if (cJSON_GetObjectItem(root, "epic"))
{
loadEpicData(cJSON_GetObjectItem(root, "epic"));
}
battle.manualComplete = getJSONValue(root, "manualComplete", 0);
battle.unwinnable = getJSONValue(root, "unwinnable", 0);
battle.waypointAutoAdvance = getJSONValue(root, "waypointAutoAdvance", 0);
2016-05-15 09:19:26 +02:00
initScript(root);
/* music, planet, and background loading must come last, so AUTO works properly */
STRNCPY(music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_DESCRIPTION_LENGTH);
if (strcmp(music, "AUTO") == 0)
{
STRNCPY(music, getAutoMusic(filename), MAX_DESCRIPTION_LENGTH);
}
background = cJSON_GetObjectItem(root, "background")->valuestring;
if (strcmp(background, "AUTO") == 0)
{
background = getAutoBackground(filename);
}
battle.background = getTexture(background);
planet = cJSON_GetObjectItem(root, "planet")->valuestring;
if (strcmp(planet, "AUTO") == 0)
{
planet = getAutoPlanet(filename);
}
battle.planetTexture = getTexture(planet);
battle.planet.x = (SCREEN_WIDTH / 2) - (rand() % SCREEN_WIDTH) + (rand() % SCREEN_WIDTH);
battle.planet.y = (SCREEN_HEIGHT / 2) - (rand() % SCREEN_HEIGHT) + (rand() % SCREEN_HEIGHT);
if (strcmp(planet, "gfx/planets/star.png") != 0)
{
SDL_QueryTexture(battle.planetTexture, NULL, NULL, &battle.planetWidth, &battle.planetHeight);
planetScale = 75 + (rand() % 125);
planetScale *= 0.01;
battle.planetWidth *= planetScale;
battle.planetHeight *= planetScale;
}
2015-10-20 13:51:49 +02:00
srand(time(NULL));
free(text);
2015-10-20 13:51:49 +02:00
endSectionTransition();
2016-02-28 14:02:57 +01:00
/* only increment num missions / challenges started if there are some (Free Flight excluded, for example) */
if (battle.objectiveHead.next)
{
game.stats[STAT_MISSIONS_STARTED]++;
}
else if (game.currentMission->challengeData.isChallenge)
2016-02-28 14:02:57 +01:00
{
game.stats[STAT_CHALLENGES_STARTED]++;
}
else
2015-10-20 13:51:49 +02:00
{
battle.status = MS_IN_PROGRESS;
}
if (battle.waypointAutoAdvance)
{
activateNextWaypoint();
}
countNumEnemies();
initPlayer();
2016-02-27 19:08:43 +01:00
initMissionInfo();
addAllToQuadtree();
2015-10-20 13:51:49 +02:00
playMusic(music);
}
static char *getAutoBackground(char *filename)
{
2016-05-15 09:19:26 +02:00
unsigned long hash;
if (!game.currentMission->challengeData.isChallenge)
{
hash = hashcode(game.selectedStarSystem);
}
else
{
hash = hashcode(filename);
}
return getBackgroundTextureName(hash);
}
static char *getAutoPlanet(char *filename)
{
2016-05-15 09:19:26 +02:00
unsigned long hash;
if (!game.currentMission->challengeData.isChallenge)
{
hash = hashcode(game.selectedStarSystem);
}
else
{
hash = hashcode(filename);
}
return getPlanetTextureName(hash);
}
static char *getAutoMusic(char *filename)
{
2016-05-15 09:19:26 +02:00
unsigned long hash;
if (!game.currentMission->challengeData.isChallenge)
{
hash = hashcode(game.selectedStarSystem);
}
else
{
2016-05-15 09:19:26 +02:00
hash = hashcode(game.currentMission->description);
}
return getMusicFilename(hash);
}
2015-10-26 09:10:13 +01:00
void completeMission(void)
{
if (battle.status == MS_IN_PROGRESS)
{
battle.status = MS_COMPLETE;
battle.missionFinishedTimer = FPS;
selectWidget("continue", "battleWon");
game.stats[STAT_MISSIONS_COMPLETED]++;
completeConditions();
2015-11-21 18:32:39 +01:00
retreatEnemies();
player->flags |= EF_IMMORTAL;
2016-05-15 09:19:26 +02:00
awardStatsTrophies();
if (!game.currentMission->challengeData.isChallenge)
{
awardPostMissionTrophies();
}
}
2015-10-26 09:10:13 +01:00
}
void failMission(void)
{
if (battle.status == MS_IN_PROGRESS)
{
battle.status = MS_FAILED;
battle.missionFinishedTimer = FPS;
selectWidget("retry", "battleLost");
failIncompleteObjectives();
player->flags |= EF_IMMORTAL;
2016-05-15 09:19:26 +02:00
awardStatsTrophies();
}
2015-10-26 09:10:13 +01:00
}
2015-10-26 18:27:43 +01:00
static void loadEntities(cJSON *node)
{
Entity *e;
char *name, *groupName;
int i, type, scatter, number, active, addFlags, side;
float x, y;
2016-03-12 19:22:48 +01:00
long flags;
if (node)
{
node = node->child;
while (node)
{
e = NULL;
type = lookup(cJSON_GetObjectItem(node, "type")->valuestring);
2016-02-21 08:48:22 +01:00
x = (cJSON_GetObjectItem(node, "x")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_WIDTH;
y = (cJSON_GetObjectItem(node, "y")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_HEIGHT;
2015-10-29 12:08:47 +01:00
name = NULL;
groupName = NULL;
2016-03-27 12:21:23 +02:00
flags = -1;
name = getJSONValueStr(node, "name", NULL);
groupName = getJSONValueStr(node, "groupName", NULL);
2016-03-04 23:11:13 +01:00
number = getJSONValue(node, "number", 1);
active = getJSONValue(node, "active", 1);
scatter = getJSONValue(node, "scatter", 1);
side = lookup(getJSONValueStr(node, "side", "SIDE_NONE"));
2016-03-12 19:22:48 +01:00
if (cJSON_GetObjectItem(node, "flags"))
{
flags = flagsToLong(cJSON_GetObjectItem(node, "flags")->valuestring, &addFlags);
}
for (i = 0 ; i < number ; i++)
{
switch (type)
{
case ET_WAYPOINT:
e = spawnWaypoint();
active = 0;
break;
case ET_JUMPGATE:
e = spawnJumpgate(side, flags);
break;
2016-03-30 08:22:58 +02:00
case ET_MINE:
2016-04-14 12:56:42 +02:00
case ET_SHADOW_MINE:
e = spawnMine(type);
2016-03-30 08:22:58 +02:00
break;
default:
printf("Error: Unhandled entity type: %s\n", cJSON_GetObjectItem(node, "type")->valuestring);
exit(1);
break;
}
if (name)
{
STRNCPY(e->name, name, MAX_NAME_LENGTH);
}
if (groupName)
{
STRNCPY(e->groupName, groupName, MAX_NAME_LENGTH);
}
if (type != ET_JUMPGATE && flags != -1)
2016-03-12 19:22:48 +01:00
{
if (addFlags)
{
e->flags |= flags;
}
else
{
e->flags = flags;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Flags for '%s' (%s) replaced", e->name, e->defName);
}
}
e->x = x;
e->y = y;
e->side = side;
2015-12-07 20:19:14 +01:00
if (scatter > 1)
{
e->x += (rand() % scatter) - (rand() % scatter);
e->y += (rand() % scatter) - (rand() % scatter);
}
e->active = active;
}
node = node->next;
}
}
}
static void loadEpicData(cJSON *node)
{
Entity *e;
int numFighters[SIDE_MAX];
memset(numFighters, 0, sizeof(int) * SIDE_MAX);
2016-02-28 14:02:57 +01:00
battle.isEpic = 1;
battle.epicFighterLimit = cJSON_GetObjectItem(node, "fighterLimit")->valueint;
for (e = battle.entityHead.next ; e != NULL ; e = e->next)
{
if (e->active && e->type == ET_FIGHTER && numFighters[e->side]++ >= battle.epicFighterLimit)
{
e->active = 0;
}
}
}
Mission *getMission(char *filename)
2015-10-20 13:51:49 +02:00
{
StarSystem *starSystem;
2015-10-20 13:51:49 +02:00
Mission *mission;
2016-02-27 17:16:21 +01:00
/* First, search the star systems */
for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next)
2015-10-20 13:51:49 +02:00
{
for (mission = starSystem->missionHead.next ; mission != NULL ; mission = mission->next)
2015-10-20 13:51:49 +02:00
{
if (strcmp(mission->filename, filename) == 0)
{
return mission;
}
2015-10-20 13:51:49 +02:00
}
}
2016-02-27 17:16:21 +01:00
/* now search the challenges */
for (mission = game.challengeMissionHead.next ; mission != NULL ; mission = mission->next)
{
if (strcmp(mission->filename, filename) == 0)
{
return mission;
}
}
2016-02-27 17:16:21 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "No such mission '%s'", filename);
2015-10-20 13:51:49 +02:00
return NULL;
}
2016-02-29 11:47:10 +01:00
void updateAllMissions(void)
{
updateStarSystemMissions();
2016-02-29 11:47:10 +01:00
updateChallengeMissions();
}
int isMissionAvailable(Mission *mission, Mission *prev)
{
2016-03-27 12:21:23 +02:00
return (prev->completed && mission->requires <= game.completedMissions) || dev.debug;
}
2015-10-20 13:51:49 +02:00
static unsigned long hashcode(const char *str)
{
unsigned long hash = 5381;
int c;
c = *str;
while (c)
{
hash = ((hash << 5) + hash) + c;
c = *str++;
}
2015-10-20 13:51:49 +02:00
return abs(hash);
}