From c66b734e7e862f9b56042705b8cae39ff165b471 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 11 Mar 2016 23:43:50 +0000 Subject: [PATCH] Added interval-based script events. --- src/battle/battle.c | 6 ++-- src/battle/battle.h | 1 + src/battle/script.c | 80 ++++++++++++++++++++++++++++++++++++++++++++- src/battle/script.h | 1 + 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/battle/battle.c b/src/battle/battle.c index 79c0692..62149b9 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -159,11 +159,9 @@ static void doBattle(void) { doScript(); - battle.stats[STAT_TIME]++; - - if (battle.stats[STAT_TIME] % FPS == 0) + if (battle.stats[STAT_TIME]++ % FPS == 0) { - runScriptFunction("TIME %d", battle.stats[STAT_TIME] / 60); + runScriptTimeFunctions(); } } } diff --git a/src/battle/battle.h b/src/battle/battle.h index c507df1..0662b49 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -84,6 +84,7 @@ extern void destroyEffects(void); extern void initChallengeHome(void); extern void updateAccuracyStats(unsigned int *stats); extern void clearInput(void); +extern void runScriptTimeFunctions(void); extern App app; extern Battle battle; diff --git a/src/battle/script.c b/src/battle/script.c index 6aafdb4..4cb3252 100644 --- a/src/battle/script.c +++ b/src/battle/script.c @@ -28,10 +28,24 @@ static ScriptRunner *tail; void initScript(cJSON *scriptNode) { + cJSON *function; + memset(&head, 0, sizeof(ScriptRunner)); tail = &head; scriptJSON = scriptNode; + + if (scriptJSON) + { + function = scriptJSON->child; + + while (function) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Script Function: '%s'", cJSON_GetObjectItem(function, "function")->valuestring); + + function = function->next; + } + } } void doScript(void) @@ -110,11 +124,65 @@ void runScriptFunction(const char *format, ...) } } +void runScriptTimeFunctions(void) +{ + ScriptRunner *scriptRunner; + cJSON *function; + char *functionName; + char funcNameBuffer[MAX_NAME_LENGTH]; + int intParam; + + if (scriptJSON) + { + function = scriptJSON->child; + + sprintf(funcNameBuffer, "TIME %d", battle.stats[STAT_TIME] / 60); + + while (function) + { + functionName = cJSON_GetObjectItem(function, "function")->valuestring; + + if (strcmp(functionName, funcNameBuffer) == 0) + { + scriptRunner = malloc(sizeof(ScriptRunner)); + memset(scriptRunner, 0, sizeof(ScriptRunner)); + + scriptRunner->line = cJSON_GetObjectItem(function, "lines")->child; + + tail->next = scriptRunner; + tail = scriptRunner; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Running script '%s'", funcNameBuffer); + } + + if (strstr(functionName, "INTERVAL")) + { + sscanf(functionName, "%*s %d", &intParam); + + if ((battle.stats[STAT_TIME] / 60) % intParam == 0) + { + scriptRunner = malloc(sizeof(ScriptRunner)); + memset(scriptRunner, 0, sizeof(ScriptRunner)); + + scriptRunner->line = cJSON_GetObjectItem(function, "lines")->child; + + tail->next = scriptRunner; + tail = scriptRunner; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Running script '%s'", funcNameBuffer); + } + } + + function = function->next; + } + } +} + static void executeNextLine(ScriptRunner *runner) { char *line; char command[24]; - char strParam[2][256]; + char strParam[3][256]; int intParam[2]; line = runner->line->valuestring; @@ -183,6 +251,11 @@ static void executeNextLine(ScriptRunner *runner) battle.isEpic = 0; retreatEnemies(); } + else if (strcmp(command, "SPAWN_FIGHTERS") == 0) + { + sscanf(line, "%*s %s %s %d %s", strParam[0], strParam[1], &intParam[0], strParam[2]); + spawnScriptFighter(strParam[0], strParam[1], intParam[0], strParam[2]); + } else { printf("ERROR: Unrecognised script command '%s'\n", command); @@ -196,6 +269,11 @@ static void executeNextLine(ScriptRunner *runner) void destroyScript(void) { ScriptRunner *scriptRunner; + + if (scriptJSON) + { + cJSON_Delete(scriptJSON); + } while (head.next) { diff --git a/src/battle/script.h b/src/battle/script.h index aadcba0..acbceb6 100644 --- a/src/battle/script.h +++ b/src/battle/script.h @@ -34,6 +34,7 @@ extern void activateLocations(char *locations); void activateObjectives(char *objectives); extern int showingMessageBoxes(void); extern char *getTranslatedString(char *string); +extern void spawnScriptFighter(char *fighters, char *side, int num, char *location); extern Battle battle; extern Colors colors;