From d0a3d0d6c52c3802ecad77a8b4fdcd19fa15d354 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 26 Oct 2015 08:10:13 +0000 Subject: [PATCH] Start of triggers. --- data/missions/test/test.json | 10 +++++- makefile | 2 +- src/battle/battle.c | 19 +++++++++- src/battle/battle.h | 4 +-- src/battle/fighters.c | 2 ++ src/battle/fighters.h | 1 + src/battle/objectives.c | 8 ++--- src/battle/objectives.h | 2 ++ src/battle/triggers.c | 67 ++++++++++++++++++++++++++++++++++++ src/battle/triggers.h | 31 +++++++++++++++++ src/defs.h | 12 +++++++ src/galaxy/galacticMap.h | 1 - src/galaxy/mission.c | 45 ++++++++++++++++++++++-- src/galaxy/stats.h | 2 -- src/main.h | 1 - src/structs.h | 10 ++++++ src/system/load.h | 1 - src/system/lookup.c | 6 ++++ 18 files changed, 205 insertions(+), 19 deletions(-) create mode 100644 src/battle/triggers.c create mode 100644 src/battle/triggers.h diff --git a/data/missions/test/test.json b/data/missions/test/test.json index 10b06f0..d09f980 100644 --- a/data/missions/test/test.json +++ b/data/missions/test/test.json @@ -7,5 +7,13 @@ "player" : { "type" : "Nymph", "side" : "SIDE_CSN" - } + }, + "triggers" : [ + { + "type" : "TRIGGER_TIME", + "targetName" : "TIME", + "targetValue" : 10, + "action" : "TA_COMPLETE_MISSION" + } + ] } diff --git a/makefile b/makefile index 415be9d..c6e149d 100644 --- a/makefile +++ b/makefile @@ -30,7 +30,7 @@ OBJS += objectives.o options.o OBJS += player.o OBJS += radar.o OBJS += save.o sound.o starfield.o starSystems.o stats.o -OBJS += testMission.o textures.o text.o title.o transition.o +OBJS += testMission.o textures.o text.o title.o transition.o triggers.o OBJS += util.o OBJS += widgets.o diff --git a/src/battle/battle.c b/src/battle/battle.c index 02497f6..1d6a0cb 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -44,6 +44,7 @@ void initBattle(void) battle.fighterTail = &battle.fighterHead; battle.effectTail = &battle.effectHead; battle.objectiveTail = &battle.objectiveHead; + battle.triggerTail = &battle.triggerHead; app.delegate.logic = &logic; app.delegate.draw = &draw; @@ -138,6 +139,12 @@ static void doBattle(void) } } } + + battle.stats[STAT_TIME]++; + if (battle.stats[STAT_TIME] % FPS == 0) + { + checkTrigger("TIME", TRIGGER_TIME); + } } static void draw(void) @@ -293,7 +300,8 @@ static void postBattle(void) { int i; - for (i = 0 ; i < STAT_MAX ; i++) + /* we don't want to count the time when adding up stats */ + for (i = 0 ; i < STAT_TIME ; i++) { game.stats[i] += battle.stats[i]; } @@ -311,6 +319,7 @@ void destroyBattle(void) Bullet *b; Effect *e; Objective *o; + Trigger *t; while (battle.fighterHead.next) { @@ -343,4 +352,12 @@ void destroyBattle(void) free(o); } battle.objectiveTail = &battle.objectiveHead; + + while (battle.triggerHead.next) + { + t = battle.triggerHead.next; + battle.triggerHead.next = t->next; + free(t); + } + battle.triggerTail = &battle.triggerHead; } diff --git a/src/battle/battle.h b/src/battle/battle.h index 808d065..a1cbaa8 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -48,7 +48,6 @@ extern void initGalacticMap(void); extern void drawWidgets(char *groupName); extern void selectWidget(const char *name, const char *group); extern Widget *getWidget(const char *name, const char *group); -extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void doWidgets(void); extern void loadMission(char *filename); extern void resetHud(void); @@ -61,10 +60,9 @@ extern void scrollBackground(float x, float y); extern void initOptions(void (*returnFromOptions)(void)); extern void drawOptions(void); extern void playSound(int id); -extern void initPlayer(void); +extern void checkTrigger(char *name, int type); extern App app; extern Battle battle; -extern Colors colors; extern Fighter *player; extern Game game; diff --git a/src/battle/fighters.c b/src/battle/fighters.c index 9a683b9..3a1934c 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -290,6 +290,8 @@ void doFighters(void) updateObjective(f->name, TT_DESTROY); updateCondition(f->name, TT_DESTROY); + + checkTrigger(f->name, TRIGGER_KILLS); } if (f == battle.fighterTail) diff --git a/src/battle/fighters.h b/src/battle/fighters.h index 151157c..eea1839 100644 --- a/src/battle/fighters.h +++ b/src/battle/fighters.h @@ -37,6 +37,7 @@ extern void updateObjective(char *name, int type); extern void updateCondition(char *name, int type); extern Fighter *getFighterDef(char *name); extern void addHudMessage(SDL_Color c, char *format, ...); +extern void checkTrigger(char *name, int type); extern App app; extern Battle battle; diff --git a/src/battle/objectives.c b/src/battle/objectives.c index bc7ff82..c02a006 100644 --- a/src/battle/objectives.c +++ b/src/battle/objectives.c @@ -60,10 +60,7 @@ void doObjectives(void) { if (battle.numObjectivesTotal > 0 && battle.numObjectivesComplete == battle.numObjectivesTotal) { - battle.status = MS_COMPLETE; - battle.missionFinishedTimer = FPS; - - game.stats[STAT_MISSIONS_COMPLETED]++; + completeMission(); completeConditions(); @@ -72,8 +69,7 @@ void doObjectives(void) if (objectiveFailed) { - battle.status = MS_FAILED; - battle.missionFinishedTimer = FPS; + failMission(); failIncompleteObjectives(); } diff --git a/src/battle/objectives.h b/src/battle/objectives.h index bc8252f..4688adf 100644 --- a/src/battle/objectives.h +++ b/src/battle/objectives.h @@ -25,6 +25,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void updateChallenges(void); extern void addHudMessage(SDL_Color c, char *format, ...); +extern void completeMission(void); +extern void failMission(void); extern Battle battle; extern Colors colors; diff --git a/src/battle/triggers.c b/src/battle/triggers.c new file mode 100644 index 0000000..b083eae --- /dev/null +++ b/src/battle/triggers.c @@ -0,0 +1,67 @@ +/* +Copyright (C) 2015 Parallel Realities + +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 "triggers.h" + +static int conditionMet(Trigger *trigger); +static void fireTrigger(Trigger *trigger); + +void checkTrigger(char *name, int type) +{ + Trigger *trigger; + + for (trigger = battle.triggerHead.next ; trigger != NULL ; trigger = trigger->next) + { + if (trigger->type == type && strcmp(trigger->targetName, name) == 0 && conditionMet(trigger)) + { + fireTrigger(trigger); + } + } +} + +static int conditionMet(Trigger *trigger) +{ + switch (trigger->type) + { + case TRIGGER_TIME: + return trigger->targetValue == battle.stats[STAT_TIME] / FPS; + + case TRIGGER_KILLS: + return trigger->targetValue == battle.stats[STAT_ENEMIES_KILLED]; + } + + return 0; +} + +static void fireTrigger(Trigger *trigger) +{ + switch (trigger->action) + { + case TA_COMPLETE_MISSION: + addHudMessage(colors.green, "Mission Complete!"); + completeMission(); + break; + + case TA_FAIL_MISSION: + addHudMessage(colors.red, "Mission Failed!"); + failMission(); + break; + } +} diff --git a/src/battle/triggers.h b/src/battle/triggers.h new file mode 100644 index 0000000..a513f65 --- /dev/null +++ b/src/battle/triggers.h @@ -0,0 +1,31 @@ +/* +Copyright (C) 2015 Parallel Realities + +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 "SDL2/SDL.h" + +#include "../defs.h" +#include "../structs.h" + +extern void completeMission(void); +extern void failMission(void); +extern void addHudMessage(SDL_Color c, char *format, ...); + +extern Battle battle; +extern Colors colors; diff --git a/src/defs.h b/src/defs.h index 31f15ac..8a285cd 100644 --- a/src/defs.h +++ b/src/defs.h @@ -143,6 +143,18 @@ enum TT_DISABLE }; +enum +{ + TRIGGER_TIME, + TRIGGER_KILLS +}; + +enum +{ + TA_COMPLETE_MISSION, + TA_FAIL_MISSION +}; + enum { MS_START, diff --git a/src/galaxy/galacticMap.h b/src/galaxy/galacticMap.h index 5ab9f4f..1e9388a 100644 --- a/src/galaxy/galacticMap.h +++ b/src/galaxy/galacticMap.h @@ -64,7 +64,6 @@ extern void playSound(int id); extern void blitRotated(SDL_Texture *texture, int x, int y, int angle); extern void initStatsDisplay(void); extern void handleStatsKB(void); -extern Mission *getMission(char *filename); extern void updateStarSystemMissions(void); extern App app; diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index b357596..53f7fdb 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "mission.h" static void loadObjectives(cJSON *node); +static void loadTriggers(cJSON *node); static void loadPlayer(cJSON *node); static void loadFighters(cJSON *node); static void loadFighterGroups(cJSON *node); @@ -48,6 +49,8 @@ void loadMission(char *filename) battle.planet.y = rand() % SCREEN_HEIGHT - rand() % SCREEN_HEIGHT; loadObjectives(cJSON_GetObjectItem(root, "objectives")); + + loadTriggers(cJSON_GetObjectItem(root, "triggers")); loadPlayer(cJSON_GetObjectItem(root, "player")); @@ -80,6 +83,20 @@ void loadMission(char *filename) playMusic(music); } +void completeMission(void) +{ + battle.status = MS_COMPLETE; + battle.missionFinishedTimer = FPS; + + game.stats[STAT_MISSIONS_COMPLETED]++; +} + +void failMission(void) +{ + battle.status = MS_FAILED; + battle.missionFinishedTimer = FPS; +} + static void loadObjectives(cJSON *node) { Objective *o; @@ -92,6 +109,8 @@ static void loadObjectives(cJSON *node) { o = malloc(sizeof(Objective)); memset(o, 0, sizeof(Objective)); + battle.objectiveTail->next = o; + battle.objectiveTail = o; STRNCPY(o->description, cJSON_GetObjectItem(node, "description")->valuestring, MAX_DESCRIPTION_LENGTH); STRNCPY(o->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH); @@ -108,8 +127,30 @@ static void loadObjectives(cJSON *node) o->isOptional = cJSON_GetObjectItem(node, "isOptional")->valueint; } - battle.objectiveTail->next = o; - battle.objectiveTail = o; + node = node->next; + } + } +} + +static void loadTriggers(cJSON *node) +{ + Trigger *t; + + if (node) + { + node = node->child; + + while (node) + { + t = malloc(sizeof(Trigger)); + memset(t, 0, sizeof(Trigger)); + battle.triggerTail->next = t; + battle.triggerTail = t; + + t->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + STRNCPY(t->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH); + t->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; + t->action = lookup(cJSON_GetObjectItem(node, "action")->valuestring); node = node->next; } diff --git a/src/galaxy/stats.h b/src/galaxy/stats.h index 627f123..89dda61 100644 --- a/src/galaxy/stats.h +++ b/src/galaxy/stats.h @@ -25,8 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_STAT_ITEMS 9 -extern void selectWidget(const char *name, const char *group); -extern Widget *getWidget(const char *name, const char *group); extern void drawWidgets(char *groupName); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void blit(SDL_Texture *texture, int x, int y, int centered); diff --git a/src/main.h b/src/main.h index a1f8145..71dffa7 100644 --- a/src/main.h +++ b/src/main.h @@ -27,7 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void cleanup(void); extern void initSDL(void); -extern void initBattle(void); extern void initGameSystem(void); extern void initTitle(void); extern void loadTestMission(char *filename); diff --git a/src/structs.h b/src/structs.h index 6b024b3..88b6fc5 100644 --- a/src/structs.h +++ b/src/structs.h @@ -32,6 +32,7 @@ typedef struct Mission Mission; typedef struct Pulse Pulse; typedef struct Widget Widget; typedef struct HudMessage HudMessage; +typedef struct Trigger Trigger; typedef struct { float x; @@ -174,6 +175,14 @@ struct Challenge { Challenge *next; }; +struct Trigger { + int type; + char targetName[MAX_NAME_LENGTH]; + int targetValue; + int action; + Trigger *next; +}; + struct Mission { char name[MAX_NAME_LENGTH]; char description[MAX_DESCRIPTION_LENGTH]; @@ -217,6 +226,7 @@ typedef struct { Bullet bulletHead, *bulletTail; Effect effectHead, *effectTail; Objective objectiveHead, *objectiveTail; + Trigger triggerHead, *triggerTail; unsigned int stats[STAT_MAX]; } Battle; diff --git a/src/system/load.h b/src/system/load.h index a6d160c..19d1c77 100644 --- a/src/system/load.h +++ b/src/system/load.h @@ -25,7 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../json/cJSON.h" extern char *readFile(char *filename); -extern StarSystem *getStarSystem(char *name); extern Mission *getMission(char *filename); extern Challenge *getChallenge(Mission *mission, int type); extern int lookup(char *lookup); diff --git a/src/system/lookup.c b/src/system/lookup.c index aed5e5c..2be68e3 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -91,6 +91,12 @@ void initLookups(void) addLookup("STAT_PLAYER_KILLED", STAT_PLAYER_KILLED); addLookup("STAT_DISABLED", STAT_DISABLED); addLookup("STAT_TIME", STAT_TIME); + + addLookup("TRIGGER_TIME", TRIGGER_TIME); + addLookup("TRIGGER_KILLS", TRIGGER_KILLS); + + addLookup("TA_COMPLETE_MISSION", TA_COMPLETE_MISSION); + addLookup("TA_FAIL_MISSION", TA_FAIL_MISSION); } static void addLookup(char *name, long value)