#include "chapters.h" #include #include #include #include "datatypes.h" #include "parse.h" #include "util.h" #include "nodes.h" #include "doors.h" #include "terminals.h" /* Copyright (c) 2013-2021 Devine Lu Linvega 2018 web port by rezmason 2021 C89 port by rezmason Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ static ChapterLock (*chapterLocks)[] = NULL; static int numChapterLocks; static int testNode(ChapterLock *lock, Subject *subject, GameState *state) { (void)state; return subject->type == SUBJECTTYPE_NODE && strcmp(subject->data.node.node->name, lock->name) == 0; } static int testDoor(ChapterLock *lock, Subject *subject, GameState *state) { Door *door; if(subject->type != SUBJECTTYPE_DOOR || strcmp(subject->data.door.door->name, lock->name) != 0) { return 0; } door = subject->data.door.door; if(lock->alt) { return door->altTest(&subject->data.door.door->data, state); } return door->test(&subject->data.door.door->data, state); } static int testTerminal(ChapterLock *lock, Subject *subject, GameState *state) { (void)state; return subject->type == SUBJECTTYPE_TERMINAL && strcmp(subject->data.terminal.terminal->name, lock->name) == 0; } static int no(ChapterLock *lock, Subject *subject, GameState *state) { (void)lock; (void)subject; (void)state; return 0; } static void parseChapterLock(ChapterLock *lock, char *line, int lineNumber) { char name[40] = "", type[30] = ""; int numFound; strcpy(name, ""); strcpy(type, ""); lock->test = no; /* example: type("name", alt); */ numFound = sscanf(line, "%[^(](\"%[^\"]\", %d);", type, name, &lock->alt); if(numFound < 2) { printf("Chapter data error: only %d terms matched on line %d: %s\n", numFound, lineNumber, line); return; } strcpy(lock->name, name); if(strcmp(type, "nodeLock") == 0) { lock->test = testNode; } else if(strcmp(type, "doorLock") == 0) { lock->test = testDoor; } else if(strcmp(type, "terminalLock") == 0) { lock->test = testTerminal; } else { printf("Chapter data error: unknown lock type %s\n", type); } } int Chapters_Init(char *data) { const char *lineDelim = "\n"; char *lineSave = NULL; int lineNumber = 1; int i = 0; char *line; numChapterLocks = getNumLines(data); chapterLocks = calloc(numChapterLocks, sizeof(ChapterLock)); if(numChapterLocks < NumberOfChapters) { printf("Chapter data error: not enough entries, expecting %d but found %d\n", NumberOfChapters, numChapterLocks); return 0; } line = getNextToken(data, lineDelim, &lineSave); while(line != NULL) { parseChapterLock(&(*chapterLocks)[i], line, lineNumber); line = getNextToken(NULL, lineDelim, &lineSave); lineNumber++; i++; } return 1; } void Chapters_Quit() { } void Chapters_Reset(GameState *state) { state->chapter = CHAPTER_ACT1; } int Chapters_Update(Subject *subject, GameState *state) { int nextChapter = state->chapter + 1; if((int)nextChapter < numChapterLocks) { ChapterLock *lock = &(*chapterLocks)[nextChapter]; if(lock->test(lock, subject, state)) { #ifdef DEBUG printf("Chapter %d\n", nextChapter + 1); #endif state->chapter = nextChapter; return 1; } } return 0; }