#include "mazes.h" #include <string.h>#include <stdio.h>#include "util.h"#include "datatypes.h"#include "parse.h"#include "gui.h"#include "audio.h" /*Copyright (c) 2013-2021 Devine Lu Linvega2018 web port by rezmason2021 C89 port by rezmason Permission to use, copy, modify, and distribute this software for anypurpose with or without fee is hereby granted, provided that the abovecopyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIESWITH REGARD TO THIS SOFTWARE.*/ static int goals[2]; static voidparseMazeGoal(char *line, int lineNumber){ MazeAxis axis; int value, numFound; char axisName[5]; strcpy(axisName, ""); /* example: goal(X, 123); */ numFound = sscanf(line, "goal(%[^,], %d);", axisName, &value); if(numFound < 2) { printf("Maze data error: only %d terms matched on line %d: %s\n", numFound, lineNumber, line); } parseMazeAxis(&axis, axisName); goals[axis] = value;} static intcanExit(MazeSubject *subject, GameState *state){ int i; if(!subject->isExit) { return 0; } for(i = 0; i < NumberOfMazeAxes; i++) { if(goals[subject->axis] != state->mazeCoord[subject->axis]) { return 0; } } return 1;} intMazes_Init(char *data){ const char *lineDelim = "\n"; char *lineSave = NULL; int lineNumber = 1; char *line; line = getNextToken(data, lineDelim, &lineSave); while(line != NULL) { parseMazeGoal(line, lineNumber); line = getNextToken(NULL, lineDelim, &lineSave); lineNumber++; } return 1;} voidMazes_Quit(){} voidMazes_Reset(GameState *state){ state->mazeCoord[MAZEAXIS_X] = 0; state->mazeCoord[MAZEAXIS_Y] = 0;} voidMazes_Setup(MazeSubject *subject, GameState *state){ if(canExit(subject, state)) { GUI_ShowNode(state->nodeName, state->orientation, MODIFIER_ENTENTE_EXIT, 0, 0); }} NodeSubject *Mazes_Action(MazeSubject *subject, GameState *state){ int diff; MazeAxis axis = subject->axis; Audio_PlayFootstep(); if(canExit(subject, state)) { Mazes_Reset(state); return &subject->altNode; } diff = goals[axis] - state->mazeCoord[axis]; if(diff == 0) { return &subject->altNode; } state->mazeCoord[axis] += subject->changeAmount; return &subject->node;} MazeInstructionMazes_GetInstruction(MazeAxis axis, GameState *state){ int diff = goals[axis] - state->mazeCoord[axis]; if(diff < 0) { return MAZEINSTRUCTION_LEFT; } else if(diff > 0) { return MAZEINSTRUCTION_RIGHT; } return MAZEINSTRUCTION_STRAIGHT;}
#include "mazes.h"
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "datatypes.h"
#include "parse.h"
#include "gui.h"
#include "audio.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 int goals[2];
static void
parseMazeGoal(char *line, int lineNumber)
{
MazeAxis axis;
int value, numFound;
char axisName[5];
strcpy(axisName, "");
/*
example: goal(X, 123);
*/
numFound = sscanf(line, "goal(%[^,], %d);", axisName, &value);
if(numFound < 2) {
printf("Maze data error: only %d terms matched on line %d: %s\n", numFound, lineNumber, line);
}
parseMazeAxis(&axis, axisName);
goals[axis] = value;
}
static int
canExit(MazeSubject *subject, GameState *state)
{
int i;
if(!subject->isExit) {
return 0;
}
for(i = 0; i < NumberOfMazeAxes; i++) {
if(goals[subject->axis] != state->mazeCoord[subject->axis]) {
return 0;
}
}
return 1;
}
int
Mazes_Init(char *data)
{
const char *lineDelim = "\n";
char *lineSave = NULL;
int lineNumber = 1;
char *line;
line = getNextToken(data, lineDelim, &lineSave);
while(line != NULL) {
parseMazeGoal(line, lineNumber);
line = getNextToken(NULL, lineDelim, &lineSave);
lineNumber++;
}
return 1;
}
void
Mazes_Quit()
{
}
void
Mazes_Reset(GameState *state)
{
state->mazeCoord[MAZEAXIS_X] = 0;
state->mazeCoord[MAZEAXIS_Y] = 0;
}
void
Mazes_Setup(MazeSubject *subject, GameState *state)
{
if(canExit(subject, state)) {
GUI_ShowNode(state->nodeName, state->orientation, MODIFIER_ENTENTE_EXIT, 0, 0);
}
}
NodeSubject *
Mazes_Action(MazeSubject *subject, GameState *state)
{
int diff;
MazeAxis axis = subject->axis;
Audio_PlayFootstep();
if(canExit(subject, state)) {
Mazes_Reset(state);
return &subject->altNode;
}
diff = goals[axis] - state->mazeCoord[axis];
if(diff == 0) {
return &subject->altNode;
}
state->mazeCoord[axis] += subject->changeAmount;
return &subject->node;
}
MazeInstruction
Mazes_GetInstruction(MazeAxis axis, GameState *state)
{
int diff = goals[axis] - state->mazeCoord[axis];
if(diff < 0) {
return MAZEINSTRUCTION_LEFT;
} else if(diff > 0) {
return MAZEINSTRUCTION_RIGHT;
}
return MAZEINSTRUCTION_STRAIGHT;
}