85 lines
1.9 KiB
C
85 lines
1.9 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include "game.h"
|
||
|
#include "flag.h"
|
||
|
#include "effect.h"
|
||
|
|
||
|
void cause_effect(Game *g, Effect *e) {
|
||
|
if (e == NULL) return;
|
||
|
|
||
|
switch (e->type) {
|
||
|
case EFFECT_NOOP:
|
||
|
break;
|
||
|
case EFFECT_GOTO:
|
||
|
g->current_room = find_room(g->rooms, e->argument);
|
||
|
break;
|
||
|
case EFFECT_INCREMENT:
|
||
|
find_flag(g->flags, e->argument)->value++;
|
||
|
break;
|
||
|
case EFFECT_DECREMENT:
|
||
|
find_flag(g->flags, e->argument)->value--;
|
||
|
break;
|
||
|
case EFFECT_ENABLE:
|
||
|
find_flag(g->flags, e->argument)->value = 1;
|
||
|
break;
|
||
|
case EFFECT_DISABLE:
|
||
|
find_flag(g->flags, e->argument)->value = 0;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Effect *create_effect(Game *g, const char *string) {
|
||
|
Effect *e = malloc(sizeof(Effect));
|
||
|
char *buffer = malloc(strlen(string) + 1);
|
||
|
strcpy(buffer, string);
|
||
|
|
||
|
char *strtok_guy;
|
||
|
char *token = strtok_r(buffer, "(", &strtok_guy);
|
||
|
if (token == NULL) {
|
||
|
e->type = EFFECT_NOOP;
|
||
|
} else {
|
||
|
if (strcmp(token, "GOTO") == 0) {
|
||
|
e->type = EFFECT_GOTO;
|
||
|
} else if (strcmp(token, "INCREMENT") == 0) {
|
||
|
e->type = EFFECT_INCREMENT;
|
||
|
} else if (strcmp(token, "DECREMENT") == 0) {
|
||
|
e->type = EFFECT_DECREMENT;
|
||
|
} else if (strcmp(token, "ENABLE") == 0) {
|
||
|
e->type = EFFECT_ENABLE;
|
||
|
} else if (strcmp(token, "DISABLE") == 0) {
|
||
|
e->type = EFFECT_DISABLE;
|
||
|
}
|
||
|
|
||
|
token = strtok_r(NULL, ")", &strtok_guy);
|
||
|
e->argument = malloc(strlen(token) + 1);
|
||
|
strcpy(e->argument, token);
|
||
|
}
|
||
|
|
||
|
free(buffer);
|
||
|
return e;
|
||
|
}
|
||
|
|
||
|
void print_effect(Effect *e) {
|
||
|
switch (e->type) {
|
||
|
case EFFECT_NOOP:
|
||
|
printf("*");
|
||
|
break;
|
||
|
case EFFECT_GOTO:
|
||
|
printf("GOTO(%s)", e->argument);
|
||
|
break;
|
||
|
case EFFECT_INCREMENT:
|
||
|
printf("INCREMENT(%s)", e->argument);
|
||
|
break;
|
||
|
case EFFECT_DECREMENT:
|
||
|
printf("DECRMENT(%s)", e->argument);
|
||
|
break;
|
||
|
case EFFECT_ENABLE:
|
||
|
printf("ENABLE(%s)", e->argument);
|
||
|
break;
|
||
|
case EFFECT_DISABLE:
|
||
|
printf("DISABLE(%s)", e->argument);
|
||
|
break;
|
||
|
}
|
||
|
}
|