thrive/01_text_adventure/action.c
2025-01-18 19:34:45 -05:00

68 lines
2.0 KiB
C

#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);
char command_buffer[200];
strcpy(command_buffer, line_token);
char *command_token_guy;
char *command_word = strtok_r(command_buffer, " ", &command_token_guy);
while (command_word != NULL) {
Word *word = find_word(g->words, command_word);
action->words[action->words_count++] = word;
command_word = strtok_r(NULL, " ", &command_token_guy);
}
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);
}
line_token = strtok_r(NULL, "|", &line_token_guy);
action->priority = atoi(line_token);
line_token = strtok_r(NULL, "|", &line_token_guy);
action->description = malloc(strlen(line_token) + 1);
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);
}
}
g->actions->count++;
}
#include "data/actions.c"
void game_load_actions(Game *g) {
parse_multiline_string(g, data_actions_txt, &load_action);
}
Action *find_action(Actions *actions, char *command) {
return NULL;
}