From 44818d295a6812a1bf8b4340dc50fab741df75d1 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 29 Oct 2015 11:08:47 +0000 Subject: [PATCH] Integrated triggers. --- data/missions/test/test.json | 48 +++++++++----- src/battle/entities.c | 123 +++++++++++++++++++---------------- src/battle/missionInfo.c | 45 +++++++------ src/battle/objectives.c | 37 +++++++++-- src/battle/triggers.c | 11 ++++ src/battle/triggers.h | 2 + src/defs.h | 4 +- src/galaxy/mission.c | 27 +++++++- src/structs.h | 2 + src/system/lookup.c | 3 + 10 files changed, 202 insertions(+), 100 deletions(-) diff --git a/data/missions/test/test.json b/data/missions/test/test.json index de4da55..dd674c0 100644 --- a/data/missions/test/test.json +++ b/data/missions/test/test.json @@ -8,8 +8,31 @@ { "description" : "Reach Waypoints", "targetName" : "Waypoint", - "targetValue" : 5, + "targetValue" : 1, "targetType" : "TT_WAYPOINT" + }, + { + "description" : "Destroy Fighters", + "targetName" : "Dart", + "targetValue" : 1, + "targetType" : "TT_DESTROY", + "active" : 0 + } + ], + "triggers" : [ + { + "type" : "TRIGGER_WAYPOINT", + "targetName" : "Waypoint #1", + "targetValue" : "1", + "action" : "TA_ACTIVE_ENTITY", + "actionValue" : "Dart" + }, + { + "type" : "TRIGGER_WAYPOINT", + "targetName" : "Waypoint #1", + "targetValue" : "1", + "action" : "TA_ACTIVE_OBJECTIVE", + "actionValue" : "1" } ], "player" : { @@ -18,27 +41,20 @@ }, "fighters" : [ { - "name" : "Sphinx", - "type" : "Sphinx", - "side" : "SIDE_CSN", - "x" : 1800, - "y" : 200 - }, - { - "name" : "Nymph", - "type" : "Nymph", - "side" : "SIDE_CSN", - "x" : 1800, - "y" : 200 + "name" : "Dart", + "type" : "UnarmedDart", + "side" : "SIDE_PIRATE", + "x" : 640, + "y" : 0, + "active" : 0 } ], "entityGroups" : [ { "type" : "ET_WAYPOINT", - "number" : 5, + "number" : 1, "x" : 0, - "y" : 1000, - "scatter" : 1000 + "y" : 1000 } ] } diff --git a/src/battle/entities.c b/src/battle/entities.c index 9a20ff4..88025bb 100644 --- a/src/battle/entities.c +++ b/src/battle/entities.c @@ -46,68 +46,66 @@ void doEntities(void) for (e = battle.entityHead.next ; e != NULL ; e = e->next) { - self = e; - - if (!e->active) + if (e->active) { - continue; - } + self = e; - if (e->target != NULL && e->target->health <= 0) - { - e->action = e->defaultAction; - e->target = NULL; - } - - e->x += e->dx; - e->y += e->dy; - - if (e != player) - { - e->x -= battle.ssx; - e->y -= battle.ssy; - } - - if (e->action != NULL) - { - if (--e->thinkTime <= 0) + if (e->target != NULL && e->target->health <= 0) { - e->thinkTime = 0; - e->action(); + e->action = e->defaultAction; + e->target = NULL; } - } - - switch (e->type) - { - case ET_FIGHTER: - doFighter(); - break; + + e->x += e->dx; + e->y += e->dy; + + if (e != player) + { + e->x -= battle.ssx; + e->y -= battle.ssy; + } + + if (e->action != NULL) + { + if (--e->thinkTime <= 0) + { + e->thinkTime = 0; + e->action(); + } + } + + switch (e->type) + { + case ET_FIGHTER: + doFighter(); + break; + + default: + doEntity(); + break; + } + + if (e->alive == ALIVE_DEAD) + { + if (e == battle.entityTail) + { + battle.entityTail = prev; + } - default: - doEntity(); - break; - } - - if (e->alive == ALIVE_DEAD) - { - if (e == battle.entityTail) - { - battle.entityTail = prev; + if (e == battle.missionTarget) + { + battle.missionTarget = NULL; + } + + if (e == player) + { + player = NULL; + } + + prev->next = e->next; + free(e); + e = prev; } - - if (e == battle.missionTarget) - { - battle.missionTarget = NULL; - } - - if (e == player) - { - player = NULL; - } - - prev->next = e->next; - free(e); - e = prev; } prev = e; @@ -150,3 +148,16 @@ static void drawEntity(Entity *e) { blitRotated(e->texture, e->x, e->y, e->angle); } + +void activateEntities(char *name) +{ + Entity *e; + + for (e = battle.entityHead.next ; e != NULL ; e = e->next) + { + if (strcmp(e->name, name) == 0) + { + e->active = 1; + } + } +} diff --git a/src/battle/missionInfo.c b/src/battle/missionInfo.c index b0930ae..6e76d7b 100644 --- a/src/battle/missionInfo.c +++ b/src/battle/missionInfo.c @@ -88,29 +88,32 @@ static void drawMissionSummary(SDL_Texture *header) for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) { - y += 50; - - switch (o->status) + if (o->active) { - case OS_INCOMPLETE: - color = colors.white; - break; - - case OS_COMPLETE: - color = colors.green; - break; - - case OS_FAILED: - color = colors.red; - break; + y += 50; + + switch (o->status) + { + case OS_INCOMPLETE: + color = colors.white; + break; + + case OS_COMPLETE: + color = colors.green; + break; + + case OS_FAILED: + color = colors.red; + break; + } + + drawText(SCREEN_WIDTH / 2 - 100, y, 22, TA_RIGHT, colors.white, o->description); + if (o->targetValue > 1) + { + drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "%d / %d", o->currentValue, o->targetValue); + } + drawText(SCREEN_WIDTH / 2 + 100, y, 22, TA_LEFT, color, objectiveStatus[o->status]); } - - drawText(SCREEN_WIDTH / 2 - 100, y, 22, TA_RIGHT, colors.white, o->description); - if (o->targetValue > 1) - { - drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "%d / %d", o->currentValue, o->targetValue); - } - drawText(SCREEN_WIDTH / 2 + 100, y, 22, TA_LEFT, color, objectiveStatus[o->status]); } if (!battle.objectiveHead.next) diff --git a/src/battle/objectives.c b/src/battle/objectives.c index c02a006..9b6302d 100644 --- a/src/battle/objectives.c +++ b/src/battle/objectives.c @@ -26,16 +26,25 @@ void failIncompleteObjectives(void); void doObjectives(void) { int objectiveFailed; + int numHiddenObjectives; Objective *o; battle.numObjectivesComplete = battle.numObjectivesTotal = 0; objectiveFailed = 0; + numHiddenObjectives = 0; for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) { - if (!o->isCondition) + if (o->active) { - battle.numObjectivesTotal++; + if (!o->isCondition) + { + battle.numObjectivesTotal++; + } + } + else + { + numHiddenObjectives++; } if (o->currentValue == o->targetValue) @@ -58,7 +67,7 @@ void doObjectives(void) if (battle.status == MS_IN_PROGRESS) { - if (battle.numObjectivesTotal > 0 && battle.numObjectivesComplete == battle.numObjectivesTotal) + if (numHiddenObjectives == 0 && battle.numObjectivesTotal > 0 && battle.numObjectivesComplete == battle.numObjectivesTotal) { completeMission(); @@ -82,7 +91,7 @@ void updateObjective(char *name, int type) for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) { - if (!o->isCondition && o->targetType == type && o->currentValue < o->targetValue && strcmp(o->targetName, name) == 0) + if (o->active && !o->isCondition && o->targetType == type && o->currentValue < o->targetValue && strcmp(o->targetName, name) == 0) { o->currentValue++; @@ -110,7 +119,7 @@ void updateCondition(char *name, int type) for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) { - if (o->isCondition && o->targetType == type && o->currentValue < o->targetValue && strcmp(o->targetName, name) == 0) + if (o->active && o->isCondition && o->targetType == type && o->currentValue < o->targetValue && strcmp(o->targetName, name) == 0) { o->currentValue++; @@ -149,3 +158,21 @@ void failIncompleteObjectives(void) } } } + +void activateObjective(int num) +{ + int i = 0; + Objective *o; + + for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) + { + if (i == num) + { + addHudMessage(colors.cyan, "New Objectives : %s", o->description); + o->active = 1; + return; + } + + i++; + } +} diff --git a/src/battle/triggers.c b/src/battle/triggers.c index b083eae..97ae924 100644 --- a/src/battle/triggers.c +++ b/src/battle/triggers.c @@ -45,6 +45,9 @@ static int conditionMet(Trigger *trigger) case TRIGGER_KILLS: return trigger->targetValue == battle.stats[STAT_ENEMIES_KILLED]; + + case TRIGGER_WAYPOINT: + return 1; } return 0; @@ -63,5 +66,13 @@ static void fireTrigger(Trigger *trigger) addHudMessage(colors.red, "Mission Failed!"); failMission(); break; + + case TA_ACTIVE_ENTITY: + activateEntities(trigger->actionValue); + break; + + case TA_ACTIVE_OBJECTIVE: + activateObjective(atoi(trigger->actionValue)); + break; } } diff --git a/src/battle/triggers.h b/src/battle/triggers.h index a513f65..af86fc5 100644 --- a/src/battle/triggers.h +++ b/src/battle/triggers.h @@ -26,6 +26,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void completeMission(void); extern void failMission(void); extern void addHudMessage(SDL_Color c, char *format, ...); +extern void activateEntities(char *name); +extern void activateObjective(int num); extern Battle battle; extern Colors colors; diff --git a/src/defs.h b/src/defs.h index 149e82c..cbbe8dd 100644 --- a/src/defs.h +++ b/src/defs.h @@ -160,7 +160,9 @@ enum enum { TA_COMPLETE_MISSION, - TA_FAIL_MISSION + TA_FAIL_MISSION, + TA_ACTIVE_ENTITY, + TA_ACTIVE_OBJECTIVE }; enum diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index c41a48c..a40899a 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -119,11 +119,17 @@ static void loadObjectives(cJSON *node) battle.objectiveTail->next = o; battle.objectiveTail = o; + o->active = 1; STRNCPY(o->description, cJSON_GetObjectItem(node, "description")->valuestring, MAX_DESCRIPTION_LENGTH); STRNCPY(o->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH); o->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; o->targetType = lookup(cJSON_GetObjectItem(node, "targetType")->valuestring); + if (cJSON_GetObjectItem(node, "active")) + { + o->active = cJSON_GetObjectItem(node, "active")->valueint; + } + if (cJSON_GetObjectItem(node, "isCondition")) { o->isCondition = cJSON_GetObjectItem(node, "isCondition")->valueint; @@ -159,6 +165,11 @@ static void loadTriggers(cJSON *node) t->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; t->action = lookup(cJSON_GetObjectItem(node, "action")->valuestring); + if (cJSON_GetObjectItem(node, "actionValue")) + { + STRNCPY(t->actionValue, cJSON_GetObjectItem(node, "actionValue")->valuestring, MAX_NAME_LENGTH); + } + node = node->next; } } @@ -201,6 +212,11 @@ static void loadFighters(cJSON *node) { f->flags = flagsToLong(cJSON_GetObjectItem(node, "flags")->valuestring); } + + if (cJSON_GetObjectItem(node, "active")) + { + f->active = cJSON_GetObjectItem(node, "active")->valueint; + } node = node->next; } @@ -211,10 +227,11 @@ static void loadFighterGroups(cJSON *node) { Entity *f; char **types, *name, *type; - int side, x, y, scatter, number; + int side, x, y, scatter, number, active; int i, numTypes; scatter = 1; + active = 1; if (node) { @@ -234,6 +251,11 @@ static void loadFighterGroups(cJSON *node) scatter = cJSON_GetObjectItem(node, "scatter")->valueint; } + if (cJSON_GetObjectItem(node, "active")) + { + active = cJSON_GetObjectItem(node, "active")->valueint; + } + for (i = 0 ; i < number ; i++) { type = types[rand() % numTypes]; @@ -243,6 +265,8 @@ static void loadFighterGroups(cJSON *node) f->x += (rand() % scatter) - (rand() % scatter); f->y += (rand() % scatter) - (rand() % scatter); + f->active = active; + STRNCPY(f->name, name, MAX_NAME_LENGTH); } @@ -309,6 +333,7 @@ static void loadEntityGroups(cJSON *node) number = cJSON_GetObjectItem(node, "number")->valueint; x = cJSON_GetObjectItem(node, "x")->valueint; y = cJSON_GetObjectItem(node, "y")->valueint; + name = NULL; if (cJSON_GetObjectItem(node, "name")) { diff --git a/src/structs.h b/src/structs.h index 4a5cd4d..71ef9e0 100644 --- a/src/structs.h +++ b/src/structs.h @@ -160,6 +160,7 @@ struct Effect { }; struct Objective { + int active; char description[MAX_DESCRIPTION_LENGTH]; char targetName[MAX_NAME_LENGTH]; int targetType; @@ -183,6 +184,7 @@ struct Trigger { char targetName[MAX_NAME_LENGTH]; int targetValue; int action; + char actionValue[MAX_NAME_LENGTH]; Trigger *next; }; diff --git a/src/system/lookup.c b/src/system/lookup.c index 247d0e3..bc8fd04 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -97,9 +97,12 @@ void initLookups(void) addLookup("TRIGGER_TIME", TRIGGER_TIME); addLookup("TRIGGER_KILLS", TRIGGER_KILLS); + addLookup("TRIGGER_WAYPOINT", TRIGGER_WAYPOINT); addLookup("TA_COMPLETE_MISSION", TA_COMPLETE_MISSION); addLookup("TA_FAIL_MISSION", TA_FAIL_MISSION); + addLookup("TA_ACTIVE_ENTITY", TA_ACTIVE_ENTITY); + addLookup("TA_ACTIVE_OBJECTIVE", TA_ACTIVE_OBJECTIVE); } static void addLookup(char *name, long value)