diff --git a/01_text_adventure/action.c b/01_text_adventure/action.c index f9330ce..4654695 100644 --- a/01_text_adventure/action.c +++ b/01_text_adventure/action.c @@ -1,9 +1,11 @@ #include #include +#include #include "action.h" #include "effect.h" #include "predicate.h" #include "game.h" +#include "parse.h" #include "util.h" void load_action(Game *g, char *line) { @@ -62,6 +64,27 @@ void game_load_actions(Game *g) { parse_multiline_string(g, data_actions_txt, &load_action); } -Action *find_action(Actions *actions, char *command) { - return NULL; +Action *find_action(Game *g, const char *command) { + Command *c = parse(g, command); + int priority = -1; + Action *a = NULL; + bool failed_predicate; + for (int i = 0; i < g->actions->count; i++) { + Action *ca = &g->actions->actions[i]; + if (ca->priority < priority) continue; + for (int j = 0; j < ca->words_count; j++) { + if (c->words[j] == NULL) break; + if (c->words[j] != ca->words[j]) break; + failed_predicate = false; + for (int k = 0; k < ca->predicates_count; k++) { + if (!predicate_fulfilled(g, ca->predicates[k])) failed_predicate = true; + } + if (failed_predicate) break; + if (j == ca->words_count - 1) { + priority = ca->priority; + a = ca; + } + } + } + return a; } diff --git a/01_text_adventure/action.h b/01_text_adventure/action.h index 206692b..93af715 100644 --- a/01_text_adventure/action.h +++ b/01_text_adventure/action.h @@ -26,6 +26,6 @@ struct Actions { }; void game_load_actions(Game *g); -Action *find_action(Actions *actions, char *command); +Action *find_action(Game *g, const char *command); #endif diff --git a/01_text_adventure/data/actions.txt b/01_text_adventure/data/actions.txt index 53c5ed5..63e694d 100644 --- a/01_text_adventure/data/actions.txt +++ b/01_text_adventure/data/actions.txt @@ -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 | IN(FIRST_ROOM) | 10 | What do you want to pull? | +PULL LEVER | IN(FIRST_ROOM) | 100 | You pull the lever. Nice. | ENABLE(LEVER_PULLED) +PULL LEVER | IN(FIRST_ROOM) & ENABLED(LEVER_PULLED) | 1000 | You already pulled it. | diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index a2ef2f6..762081c 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -11,6 +11,7 @@ #include "flag.h" #include "predicate.h" #include "action.h" +#include "parse.h" Game *game_create(void) { Game *g = malloc(sizeof(Game)); @@ -108,6 +109,20 @@ void game_handle_directional_command(Game *g, const char *c) { #define INVALID_COMMAND "I don't know how to %s" void game_handle_command(Game *g, const char *command) { + Action *a = find_action(g, command); + if (a) { + push_line_to_log(g->input->log, a->description); + for (int i = 0; i < a->effects_count; i++) { + cause_effect(g, a->effects[i]); + } + } else { + char *response = malloc(strlen(INVALID_COMMAND) + strlen(command) + 1); + sprintf(response, INVALID_COMMAND, command); + push_line_to_log(g->input->log, response); + free(response); + } + return; + Input *input = g->input; switch (command_from_string(command)) { diff --git a/01_text_adventure/parse.c b/01_text_adventure/parse.c index 7157e06..e3d160b 100644 --- a/01_text_adventure/parse.c +++ b/01_text_adventure/parse.c @@ -3,28 +3,27 @@ #include #include "game.h" #include "word.h" +#include "parse.h" + +Command *parse(Game *g, const char *typed_command) { + Command *c = malloc(sizeof(Command)); + for (int i = 0; i < MAX_WORDS_IN_COMMAND; i++) { + c->words[i] = NULL; + } -#define MAX_WORDS_IN_COMMAND 4 -void parse(Game *g, char *typed_command) { - printf("Typed command: %s\n", typed_command); char *bluh = malloc(strlen(typed_command) + 1); strcpy(bluh, typed_command); - Word **command = malloc(MAX_WORDS_IN_COMMAND * sizeof(Word)); int word_count = 0; char *token = strtok(bluh, " "); while (word_count < MAX_WORDS_IN_COMMAND && token != NULL) { - command[word_count] = find_word(g->words, token); + c->words[word_count] = find_word(g->words, token); word_count++; token = strtok(NULL, " "); } - for(int i = 0; i < word_count; i++) { - printf("%s ", command[i]->word); - } - printf("\n"); - free(bluh); + return c; } diff --git a/01_text_adventure/parse.h b/01_text_adventure/parse.h index a5ec3f6..db4e1ff 100644 --- a/01_text_adventure/parse.h +++ b/01_text_adventure/parse.h @@ -1,7 +1,15 @@ #ifndef _FD_PARSE_ #define _FD_PARSE_ -#include "game.h" -void parse(Game *g, char *typed_command); +typedef struct Command Command; + +#include "game.h" +#include "word.h" + +#define MAX_WORDS_IN_COMMAND 4 +struct Command { + Word *words[4]; +}; +Command *parse(Game *g, const char *typed_command); #endif