#include "nodes.h" #include #include #include #include "util.h" #include "datatypes.h" #include "parse.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 Node (*nodes)[] = NULL; static int numNodes; static Subject defaultSubject; static char firstNodeName[10] = "xx\0"; void parseNodeSubject(Subject *sub, char *nodeName, char *orientationName, int mapIgnore) { sub->type = SUBJECTTYPE_NODE; sub->data.node.node = Nodes_Get(nodeName); parseOrientation(&sub->data.node.orientation, orientationName); sub->data.node.mapIgnore = mapIgnore; } void parseTerminalSubject(Subject *sub, char *name) { Terminal *terminal = Terminals_Get(name); sub->type = SUBJECTTYPE_TERMINAL; if(terminal == NULL) { printf("Node data error: no terminal with name %s\n", name); } else { sub->data.terminal.terminal = terminal; } } void parseDoorSubject(Subject *sub, char *name, char *nodeName, char *orientationName, char *altNodeName, char *altOrientationName) { Door *door = Doors_Get(name); sub->type = SUBJECTTYPE_DOOR; if(door == NULL) { printf("Node data error: no door with name %s\n", name); } else { sub->data.door.door = door; } sub->data.node.node = Nodes_Get(nodeName); parseOrientation(&sub->data.door.node.orientation, orientationName); sub->data.door.altNode.node = Nodes_Get(altNodeName); parseOrientation(&sub->data.door.altNode.orientation, altOrientationName); } void parseMazeSubject(Subject *sub, char *nodeName, char *orientationName, char *altNodeName, char *altOrientationName, char *axisName, int changeAmount, int isExit) { sub->type = SUBJECTTYPE_MAZE; sub->data.node.node = Nodes_Get(nodeName); parseOrientation(&sub->data.maze.node.orientation, orientationName); sub->data.door.altNode.node = Nodes_Get(altNodeName); parseOrientation(&sub->data.maze.altNode.orientation, altOrientationName); parseMazeAxis(&sub->data.maze.axis, axisName); sub->data.maze.changeAmount = changeAmount; sub->data.maze.isExit = isExit; } static void parseSubject(Subject *sub, char name[100], char params[200]) { int missing; char nodeName[10], altNodeName[10]; char orientation[15], altOrientation[15]; char entityName[40]; strcpy(nodeName, ""); strcpy(altNodeName, ""); strcpy(orientation, ""); strcpy(altOrientation, ""); strcpy(entityName, ""); *sub = defaultSubject; if(strcmp(name, "none") == 0) { /* example: none(0) */ missing = 0; } else if(strcmp(name, "to") == 0) { /* example: to("nodeName", NORTH) */ missing = 2 - sscanf(params, "\"%[^\"]\", %[^,]", nodeName, orientation); parseNodeSubject(sub, nodeName, orientation, 0); } else if(strcmp(name, "mapIgnoreTo") == 0) { /* example: mapIgnoreTo("nodeName") */ missing = 1 - sscanf(params, "\"%[^\"]\"", nodeName); parseNodeSubject(sub, nodeName, "STRAIGHT", 1); } else if(strcmp(name, "straightTo") == 0) { /* example: straightTo("nodeName") */ missing = 1 - sscanf(params, "\"%[^\"]\"", nodeName); parseNodeSubject(sub, nodeName, "STRAIGHT", 0); } else if(strcmp(name, "terminal") == 0) { /* example: terminal("name") */ missing = 1 - sscanf(params, "\"%[^\"]\"", entityName); parseTerminalSubject(sub, entityName); } else if(strcmp(name, "door") == 0) { /* example: door("name", "nodeName", STRAIGHT, "altNodeName", STRAIGHT) */ strcpy(altOrientation, "STRAIGHT"); missing = 3 - sscanf(params, "\"%[^\"]\", \"%[^\"]\", %[^,], \"%[^\"]\", %[^,]", entityName, nodeName, orientation, altNodeName, altOrientation); parseDoorSubject(sub, entityName, nodeName, orientation, altNodeName, altOrientation); } else if(strcmp(name, "maze") == 0) { /* example: maze("nodeName", STRAIGHT, "altNodeName", STRAIGHT, X, 123, 0) */ int changeAmount, isExit; char axisName[5]; strcpy(axisName, ""); missing = 7 - sscanf(params, "\"%[^\"]\", %[^,], \"%[^\"]\", %[^,], %[^,], %d, %d", nodeName, orientation, altNodeName, altOrientation, axisName, &changeAmount, &isExit); parseMazeSubject(sub, nodeName, orientation, altNodeName, altOrientation, axisName, changeAmount, isExit); } else { printf("Node data error: unknown subject constructor %s\n", name); return; } if(missing > 0) { printf("Node data error: missing %d params in subject %s(%s)\n", missing, name, params); } } static void parseNode(Node *node, int lineNumber, char *line) { int numFound; char name[10]; char zoneName[20]; char subjectNames[4][100]; char subjectParams[4][100]; char tag[10]; int isFirstNode, isNode; strcpy(name, ""); strcpy(zoneName, ""); strcpy(subjectNames[0], ""); strcpy(subjectNames[1], ""); strcpy(subjectNames[2], ""); strcpy(subjectNames[3], ""); strcpy(subjectParams[0], ""); strcpy(subjectParams[1], ""); strcpy(subjectParams[2], ""); strcpy(subjectParams[3], ""); strcpy(tag, ""); sscanf(line, "%[^(](", tag); isFirstNode = strcmp(tag, "first") == 0; isNode = isFirstNode || strcmp(tag, "node") == 0; if(!isNode) { return; } /* example: node("name", NATANIEV, straightTo("xx"), none(0), none(0), none(0)); */ numFound = sscanf(line, "%[^(](\"%[^\"]\", %[^,], %[^(](%[^)]), %[^(](%[^)]), %[^(](%[^)]), %[^(](%[^)]));", tag, name, zoneName, subjectNames[0], subjectParams[0], subjectNames[1], subjectParams[1], subjectNames[2], subjectParams[2], subjectNames[3], subjectParams[3]); if(numFound < 11) { printf("Node data error: only %d terms matched on line %d: %s\n", numFound, lineNumber, line); } else { int i; strcpy(node->name, name); if(isFirstNode) { strcpy(firstNodeName, name); } parseZone(&node->zone, zoneName); for(i = 0; i < 4; i++) { parseSubject(&node->subjects[i], subjectNames[i], subjectParams[i]); } } } int Nodes_Init(char *data) { const char *lineDelim = "\n"; if(nodes != NULL) { printf("Node data error: the nodes were already initialized.\n"); return 0; } numNodes = getNumLines(data); nodes = calloc(numNodes, sizeof(Node)); /* We populate the nodes' names before we parse the rest of them. That way, we can resolve pointers between nodes as we parse them. */ { char *dupe = duplicate(data); int i = 0; char *lineSave = NULL; char *line = getNextToken(dupe, lineDelim, &lineSave); while(line != NULL) { sscanf(line, "node(\"%[^\"]\",", (*nodes)[i].name); line = getNextToken(NULL, lineDelim, &lineSave); i++; } free(dupe); } { int i = 0, lineNumber = 1; char *lineSave = NULL; char *line = getNextToken(data, lineDelim, &lineSave); while(line != NULL) { parseNode(&(*nodes)[i], lineNumber, line); line = getNextToken(NULL, lineDelim, &lineSave); lineNumber++; i++; } } return 1; } void Nodes_Quit() { free(nodes); nodes = NULL; } void Nodes_Reset(GameState *state) { strcpy(state->nodeName, firstNodeName); state->orientation = ORIENTATION_NORTH; } Node * Nodes_Get(char *name) { int i; for(i = 0; i < numNodes; i++) { Node *node = &(*nodes)[i]; if(strcmp(node->name, name) == 0) { return node; } } return NULL; }