From 4f47cd0bc674abeafd9d46e5ab046e4b2e3b0b00 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 31 Oct 2015 10:00:46 +0000 Subject: [PATCH] Allow player to switch fighters upon death, during an epic battle. --- src/battle/battle.c | 9 +++++- src/battle/battle.h | 1 + src/battle/player.c | 76 +++++++++++++++++++++++++++++++++++++++++--- src/battle/player.h | 3 ++ src/galaxy/mission.c | 5 +++ src/structs.h | 1 + 6 files changed, 90 insertions(+), 5 deletions(-) diff --git a/src/battle/battle.c b/src/battle/battle.c index 3811194..2ac8471 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -86,7 +86,14 @@ static void logic(void) if (show == SHOW_BATTLE) { - doBattle(); + if (!battle.epic || (battle.epic && player != NULL)) + { + doBattle(); + } + else if (battle.epic && player == NULL) + { + doPlayerSelect(); + } } } diff --git a/src/battle/battle.h b/src/battle/battle.h index 07e89f0..3961920 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -62,6 +62,7 @@ extern void drawOptions(void); extern void playSound(int id); extern void checkTrigger(char *name, int type); extern void resetWaypoints(void); +extern void doPlayerSelect(void); extern App app; extern Battle battle; diff --git a/src/battle/player.c b/src/battle/player.c index a0e170b..fcebfa8 100644 --- a/src/battle/player.c +++ b/src/battle/player.c @@ -23,8 +23,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void selectTarget(void); static void switchGuns(void); static void selectMissionTarget(void); +static void selectNewPlayer(int dir); +static void initPlayerSelect(void); +static int selectedPlayerIndex; static int availableGuns[BT_MAX]; +static Entity *availablePlayerUnits[MAX_SELECTABLE_PLAYERS]; void initPlayer(void) { @@ -126,12 +130,76 @@ void doPlayer(void) if (player->health <= 0 && battle.status == MS_IN_PROGRESS) { - failIncompleteObjectives(); - - battle.status = MS_FAILED; - battle.missionFinishedTimer = FPS; + if (!battle.epic || (battle.epic && battle.numAllies <= 1)) + { + failIncompleteObjectives(); + + battle.status = MS_FAILED; + battle.missionFinishedTimer = FPS; + } } } + else + { + initPlayerSelect(); + } +} + +void initPlayerSelect(void) +{ + Entity *e; + + memset(&availablePlayerUnits, 0, sizeof(Entity*) * MAX_SELECTABLE_PLAYERS); + + selectedPlayerIndex = 0; + + for (e = battle.entityHead.next ; e != NULL ; e = e->next) + { + if (e->type == ET_FIGHTER && e->health > 0 && e->side == SIDE_ALLIES && selectedPlayerIndex < MAX_SELECTABLE_PLAYERS) + { + availablePlayerUnits[selectedPlayerIndex++] = e; + } + } + + selectedPlayerIndex = 0; + + memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS); +} + +void doPlayerSelect(void) +{ + if (app.keyboard[SDL_SCANCODE_LEFT]) + { + selectNewPlayer(-1); + + app.keyboard[SDL_SCANCODE_LEFT] = 0; + } + + if (app.keyboard[SDL_SCANCODE_RIGHT]) + { + selectNewPlayer(1); + + app.keyboard[SDL_SCANCODE_RIGHT] = 0; + } + + if (app.keyboard[SDL_SCANCODE_RETURN]) + { + player = availablePlayerUnits[selectedPlayerIndex]; + + player->action = NULL; + player->defaultAction = NULL; + } +} + +static void selectNewPlayer(int dir) +{ + do + { + selectedPlayerIndex += dir; + + selectedPlayerIndex = mod(selectedPlayerIndex, MAX_SELECTABLE_PLAYERS); + } + while (availablePlayerUnits[selectedPlayerIndex] == NULL); } static void switchGuns(void) diff --git a/src/battle/player.h b/src/battle/player.h index 9b52d9c..bf00211 100644 --- a/src/battle/player.h +++ b/src/battle/player.h @@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../defs.h" #include "../structs.h" +#define MAX_SELECTABLE_PLAYERS 8 + extern void fireGuns(Entity *owner); extern void fireMissile(Entity *owner); extern void applyFighterThrust(void); @@ -30,6 +32,7 @@ extern void applyFighterBrakes(void); extern int getDistance(int x1, int y1, int x2, int y2); extern void failIncompleteObjectives(void); extern void addHudMessage(SDL_Color c, char *format, ...); +extern int mod(int n, int x); extern App app; extern Battle battle; diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index a40899a..de1ec8a 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -50,6 +50,11 @@ void loadMission(char *filename) battle.planet.x = rand() % SCREEN_WIDTH - rand() % SCREEN_WIDTH; battle.planet.y = rand() % SCREEN_HEIGHT - rand() % SCREEN_HEIGHT; + if (cJSON_GetObjectItem(root, "epic")) + { + battle.epic = cJSON_GetObjectItem(root, "epic")->valueint; + } + loadObjectives(cJSON_GetObjectItem(root, "objectives")); loadTriggers(cJSON_GetObjectItem(root, "triggers")); diff --git a/src/structs.h b/src/structs.h index 8f2fd94..5439e31 100644 --- a/src/structs.h +++ b/src/structs.h @@ -223,6 +223,7 @@ typedef struct { int numAllies; int numEnemies; int status; + int epic; int missionFinishedTimer; int numObjectivesComplete, numObjectivesTotal; Entity *missionTarget;