Added interval-based script events.

This commit is contained in:
Steve 2016-03-11 23:43:50 +00:00
parent 553178128f
commit c66b734e7e
4 changed files with 83 additions and 5 deletions

View File

@ -159,11 +159,9 @@ static void doBattle(void)
{ {
doScript(); 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();
} }
} }
} }

View File

@ -84,6 +84,7 @@ extern void destroyEffects(void);
extern void initChallengeHome(void); extern void initChallengeHome(void);
extern void updateAccuracyStats(unsigned int *stats); extern void updateAccuracyStats(unsigned int *stats);
extern void clearInput(void); extern void clearInput(void);
extern void runScriptTimeFunctions(void);
extern App app; extern App app;
extern Battle battle; extern Battle battle;

View File

@ -28,10 +28,24 @@ static ScriptRunner *tail;
void initScript(cJSON *scriptNode) void initScript(cJSON *scriptNode)
{ {
cJSON *function;
memset(&head, 0, sizeof(ScriptRunner)); memset(&head, 0, sizeof(ScriptRunner));
tail = &head; tail = &head;
scriptJSON = scriptNode; 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) 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) static void executeNextLine(ScriptRunner *runner)
{ {
char *line; char *line;
char command[24]; char command[24];
char strParam[2][256]; char strParam[3][256];
int intParam[2]; int intParam[2];
line = runner->line->valuestring; line = runner->line->valuestring;
@ -183,6 +251,11 @@ static void executeNextLine(ScriptRunner *runner)
battle.isEpic = 0; battle.isEpic = 0;
retreatEnemies(); 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 else
{ {
printf("ERROR: Unrecognised script command '%s'\n", command); printf("ERROR: Unrecognised script command '%s'\n", command);
@ -196,6 +269,11 @@ static void executeNextLine(ScriptRunner *runner)
void destroyScript(void) void destroyScript(void)
{ {
ScriptRunner *scriptRunner; ScriptRunner *scriptRunner;
if (scriptJSON)
{
cJSON_Delete(scriptJSON);
}
while (head.next) while (head.next)
{ {

View File

@ -34,6 +34,7 @@ extern void activateLocations(char *locations);
void activateObjectives(char *objectives); void activateObjectives(char *objectives);
extern int showingMessageBoxes(void); extern int showingMessageBoxes(void);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);
extern void spawnScriptFighter(char *fighters, char *side, int num, char *location);
extern Battle battle; extern Battle battle;
extern Colors colors; extern Colors colors;