Compare commits
No commits in common. "9ef8e88b9a382dedd0e39877ef821e5bd6dbe52d" and "df0b65e0973e78f3471aa6c12c2819af65129586" have entirely different histories.
9ef8e88b9a
...
df0b65e097
@ -7,16 +7,12 @@ game: data/actions.c data/rooms.c data/transitions.c data/words.c data/flags.c *
|
||||
$(CC) *.c $(CFLAGS) -o game
|
||||
|
||||
data/%.c: data/%.txt
|
||||
echo "char *data_$*_txt = " > data/$*.c
|
||||
cat data/$*.txt | \
|
||||
perl -pe 's/ *\| */|/g' | \
|
||||
perl -pe 's/^/"/' | \
|
||||
perl -pe 's/$$/\\n"/' >> data/$*.c
|
||||
echo ";" >> data/$*.c
|
||||
echo -n "char *data_$*_txt = \"" > data/$*.c
|
||||
cat data/$*.txt | perl -pe 's/\n/\\n/g' >> data/$*.c
|
||||
echo "\";" >> data/$*.c
|
||||
|
||||
run: game
|
||||
./game
|
||||
|
||||
clean:
|
||||
rm -f ./game
|
||||
rm -f ./data/*.c
|
||||
|
@ -1,16 +1,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "action.h"
|
||||
#include "effect.h"
|
||||
#include "predicate.h"
|
||||
#include "game.h"
|
||||
#include "util.h"
|
||||
|
||||
void load_action(Game *g, char *line) {
|
||||
Action *action = &g->actions->actions[g->actions->count];
|
||||
action->words_count = 0;
|
||||
action->predicates_count = 0;
|
||||
action->effects_count = 0;
|
||||
|
||||
char *line_token_guy;
|
||||
char *line_token = strtok_r(line, "|", &line_token_guy);
|
||||
@ -27,13 +22,9 @@ void load_action(Game *g, char *line) {
|
||||
}
|
||||
|
||||
line_token = strtok_r(NULL, "|", &line_token_guy);
|
||||
strcpy(command_buffer, line_token);
|
||||
command_word = strtok_r(command_buffer, " &", &command_token_guy);
|
||||
while (command_word != NULL) {
|
||||
Predicate *p = create_predicate(g, command_word);
|
||||
action->predicates[action->predicates_count++] = p;
|
||||
command_word = strtok_r(NULL, " &", &command_token_guy);
|
||||
}
|
||||
|
||||
// predicate bullshit
|
||||
// char *command_predicate_strtok = strtok_r
|
||||
|
||||
line_token = strtok_r(NULL, "|", &line_token_guy);
|
||||
action->priority = atoi(line_token);
|
||||
@ -43,16 +34,8 @@ void load_action(Game *g, char *line) {
|
||||
strcpy(action->description, line_token);
|
||||
|
||||
line_token = strtok_r(NULL, "|", &line_token_guy);
|
||||
if (line_token == NULL) {
|
||||
} else {
|
||||
strcpy(command_buffer, line_token);
|
||||
command_word = strtok_r(command_buffer, " &", &command_token_guy);
|
||||
while (command_word != NULL) {
|
||||
Effect *e = create_effect(g, command_word);
|
||||
action->effects[action->effects_count++] = e;
|
||||
command_word = strtok_r(NULL, " &", &command_token_guy);
|
||||
}
|
||||
}
|
||||
|
||||
// action bullshit
|
||||
|
||||
g->actions->count++;
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ typedef struct Actions Actions;
|
||||
#include "game.h"
|
||||
#include "word.h"
|
||||
#include "predicate.h"
|
||||
#include "effect.h"
|
||||
|
||||
struct Action {
|
||||
Word *words[4];
|
||||
@ -16,8 +15,7 @@ struct Action {
|
||||
int predicates_count;
|
||||
int priority;
|
||||
char *description;
|
||||
Effect *effects[10];
|
||||
int effects_count;
|
||||
// Effect *effect;
|
||||
};
|
||||
|
||||
struct Actions {
|
||||
|
@ -1,4 +1,4 @@
|
||||
PULL | * | 1 | You don't see anything to pull |
|
||||
PULL | IN(LEVER_ROOM) | 10 | What do you want to pull? |
|
||||
PULL LEVER | IN(LEVER_ROOM) | 100 | You pull the lever. Nice. | ENABLE(LEVER_PULLED)
|
||||
PULL LEVER | IN(LEVER_ROOM) & ENABLED(LEVER_PULLED) | 1000 | You already pulled it. |
|
||||
PULL|*|1|You don't see anything to pull|
|
||||
PULL|IN(lever_room)|10|What do you want to pull?|
|
||||
PULL LEVER|IN(lever_room)|100|You pull the lever. Nice.|ENABLE(lever_pulled)
|
||||
PULL LEVER|IN(lever_room) & ENABLED(lever_pulled)|1000|You already pulled it.|
|
||||
|
@ -1,84 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
#ifndef _FD_EFFECT_
|
||||
#define _FD_EFFECT_
|
||||
|
||||
typedef struct Effect Effect;
|
||||
|
||||
typedef enum EffectType {
|
||||
EFFECT_NOOP,
|
||||
EFFECT_GOTO,
|
||||
EFFECT_INCREMENT,
|
||||
EFFECT_DECREMENT,
|
||||
EFFECT_ENABLE,
|
||||
EFFECT_DISABLE,
|
||||
} EffectType;
|
||||
|
||||
#include "game.h"
|
||||
|
||||
struct Effect {
|
||||
EffectType type;
|
||||
char *argument;
|
||||
};
|
||||
|
||||
void cause_effect(Game *g, Effect *e);
|
||||
Effect *create_effect(Game *g, const char *string);
|
||||
void print_effect(Effect *e);
|
||||
|
||||
#endif
|
@ -13,7 +13,6 @@ void load_flag(Game *g, char *line) {
|
||||
|
||||
token = strtok(NULL, "|");
|
||||
flag->value = atoi(token);
|
||||
printf("AGH %s\n", token);
|
||||
|
||||
g->flags->count++;
|
||||
}
|
||||
@ -24,15 +23,9 @@ void game_load_flags(Game *g) {
|
||||
}
|
||||
|
||||
int flag_value(Flags *f, char *key) {
|
||||
Flag *flag = find_flag(f, key);
|
||||
return flag ? flag->value : -1;
|
||||
}
|
||||
|
||||
Flag *find_flag(Flags *f, char *key) {
|
||||
for (int i = 0; i < f->count; i++) {
|
||||
if (strcmp(f->flags[i].key, key) == 0) return &f->flags[i];
|
||||
if (strcmp(f->flags[i].key, key) == 0) return f->flags[i].value;
|
||||
}
|
||||
|
||||
printf("Couldn't find flag %s\n", key);
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ struct Flags {
|
||||
};
|
||||
|
||||
void game_load_flags(Game *g);
|
||||
Flag *find_flag(Flags *f, char *key);
|
||||
int flag_value(Flags *f, char *key);
|
||||
|
||||
#endif
|
||||
|
@ -26,29 +26,11 @@ int main(void) {
|
||||
for (int j = 0; j < g->actions->actions[i].words_count; j++) {
|
||||
printf("%s ", g->actions->actions[i].words[j]->word);
|
||||
}
|
||||
|
||||
printf("|");
|
||||
for (int j = 0; j < g->actions->actions[i].predicates_count; j++) {
|
||||
if (j > 0) printf(" & ");
|
||||
print_predicate(g->actions->actions[i].predicates[j]);
|
||||
}
|
||||
|
||||
printf("|%d|%s|", g->actions->actions[i].priority, g->actions->actions[i].description);
|
||||
|
||||
for (int j = 0; j < g->actions->actions[i].effects_count; j++) {
|
||||
if (j > 0) printf(" & ");
|
||||
print_effect(g->actions->actions[i].effects[j]);
|
||||
}
|
||||
printf("|preds|%d|%s|effects", g->actions->actions[i].priority, g->actions->actions[i].description);
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("before action\n");
|
||||
printf("lever_pulled: %d\n", flag_value(g->flags, "LEVER_PULLED"));
|
||||
cause_effect(g, g->actions->actions[2].effects[0]);
|
||||
printf("after action\n");
|
||||
printf("lever_pulled: %d\n", flag_value(g->flags, "LEVER_PULLED"));
|
||||
|
||||
game_run_until_close(g);
|
||||
CloseWindow();
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "game.h"
|
||||
#include "flag.h"
|
||||
@ -18,42 +17,3 @@ bool predicate_fulfilled(Game *g, Predicate *p) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Predicate *create_predicate(Game *g, const char *string) {
|
||||
Predicate *p = malloc(sizeof(Predicate));
|
||||
char *buffer = malloc(strlen(string) + 1);
|
||||
strcpy(buffer, string);
|
||||
char *strtok_guy;
|
||||
|
||||
char *token = strtok_r(buffer, "(", &strtok_guy);
|
||||
if (strcmp(token, "*") == 0) {
|
||||
p->type = PREDICATE_TRUE;
|
||||
} else if (strcmp(token, "IN") == 0) {
|
||||
p->type = PREDICATE_IN_ROOM;
|
||||
token = strtok_r(NULL, ")", &strtok_guy);
|
||||
p->argument = malloc(strlen(token) + 1);
|
||||
strcpy(p->argument, token);
|
||||
} else if (strcmp(token, "ENABLED") == 0) {
|
||||
p->type = PREDICATE_FLAG_ENABLED;
|
||||
token = strtok_r(NULL, ")", &strtok_guy);
|
||||
p->argument = malloc(strlen(token) + 1);
|
||||
strcpy(p->argument, token);
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
return p;
|
||||
}
|
||||
|
||||
void print_predicate(Predicate *p) {
|
||||
switch (p->type) {
|
||||
case PREDICATE_TRUE:
|
||||
printf("*");
|
||||
break;
|
||||
case PREDICATE_IN_ROOM:
|
||||
printf("IN(%s)", p->argument);
|
||||
break;
|
||||
case PREDICATE_FLAG_ENABLED:
|
||||
printf("ENABLED(%s)", p->argument);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,5 @@ struct Predicate {
|
||||
};
|
||||
|
||||
bool predicate_fulfilled(Game *g, Predicate *p);
|
||||
Predicate *create_predicate(Game *g, const char *string);
|
||||
void print_predicate(Predicate *p);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user