From ad6a921beff21518d866e51850b17fb1d1073501 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 19 Jan 2025 09:56:33 -0500 Subject: [PATCH] Remove transitions --- 01_text_adventure/Makefile | 2 +- 01_text_adventure/data/transitions.txt | 4 --- 01_text_adventure/game.c | 44 -------------------------- 01_text_adventure/game.h | 2 -- 01_text_adventure/main.c | 2 -- 01_text_adventure/transition.c | 37 ---------------------- 01_text_adventure/transition.h | 25 --------------- 7 files changed, 1 insertion(+), 115 deletions(-) delete mode 100644 01_text_adventure/data/transitions.txt delete mode 100644 01_text_adventure/transition.c delete mode 100644 01_text_adventure/transition.h diff --git a/01_text_adventure/Makefile b/01_text_adventure/Makefile index a738293..a1d96d4 100644 --- a/01_text_adventure/Makefile +++ b/01_text_adventure/Makefile @@ -3,7 +3,7 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 .PHONY: clean run -game: data/actions.c data/rooms.c data/transitions.c data/words.c data/flags.c *.c +game: data/actions.c data/rooms.c data/words.c data/flags.c *.c $(CC) *.c $(CFLAGS) -o game data/%.c: data/%.txt diff --git a/01_text_adventure/data/transitions.txt b/01_text_adventure/data/transitions.txt deleted file mode 100644 index 6be51c6..0000000 --- a/01_text_adventure/data/transitions.txt +++ /dev/null @@ -1,4 +0,0 @@ -FIRST_ROOM|N|SECOND_ROOM|You head through the door. -SECOND_ROOM|S|FIRST_ROOM|You head back through the door. -FIRST_ROOM|E|LAST_ROOM|You crouch under the beam and enter the room. -LAST_ROOM|W|FIRST_ROOM|You crouch under the beam and return to the room. diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index 1ec2294..d243b9d 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -3,7 +3,6 @@ #include #include #include "game.h" -#include "transition.h" #include "input.h" #include "log.h" #include "util.h" @@ -20,9 +19,6 @@ Game *game_create(void) { g->rooms = malloc(sizeof(Rooms)); g->rooms->count = 0; - g->transitions = malloc(sizeof(Transitions)); - g->transitions->count = 0; - Log *log = create_log(); g->log = log; @@ -91,27 +87,10 @@ CommandType command_from_string(const char *string) { return COMMAND_UNKNOWN; } -#define INVALID_DIRECTIONAL_COMMAND "I can't go %s from here" -void game_handle_directional_command(Game *g, const char *c) { - Room *r = g->current_room; - Transition *transition = find_transition(g->transitions, r, command_from_string(c)); - - if (transition) { - push_line_to_log(g->input->log, transition->description); - g->current_room = transition->to; - } else { - char *response = malloc(strlen(INVALID_DIRECTIONAL_COMMAND) + strlen(c) + 1); - sprintf(response, INVALID_DIRECTIONAL_COMMAND, c); - push_line_to_log(g->input->log, response); - free(response); - } -} - #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) { - if (strcmp(a->description, "*") != 0) push_line_to_log(g->input->log, a->description); for (int i = 0; i < a->effects_count; i++) { cause_effect(g, a->effects[i]); @@ -123,29 +102,6 @@ void game_handle_command(Game *g, const char *command) { free(response); } return; - - Input *input = g->input; - - switch (command_from_string(command)) { - case COMMAND_QUIT: - g->should_close = true; - break; - case COMMAND_LOOK: - push_line_to_log(input->log, g->current_room->description); - break; - case COMMAND_NORTH: - case COMMAND_SOUTH: - case COMMAND_EAST: - case COMMAND_WEST: - game_handle_directional_command(g, command); - break; - default: - char *response = malloc(strlen(INVALID_COMMAND) + strlen(command) + 1); - sprintf(response, INVALID_COMMAND, command); - push_line_to_log(input->log, response); - free(response); - break; - } } void game_handle_input(Game *g) { diff --git a/01_text_adventure/game.h b/01_text_adventure/game.h index 38b5c57..d7ddbb1 100644 --- a/01_text_adventure/game.h +++ b/01_text_adventure/game.h @@ -16,7 +16,6 @@ typedef enum CommandType { #include #include "input.h" #include "room.h" -#include "transition.h" #include "log.h" #include "word.h" #include "flag.h" @@ -28,7 +27,6 @@ struct Game { Log *log; Rooms *rooms; Room *current_room; - Transitions *transitions; Words *words; Flags *flags; Actions *actions; diff --git a/01_text_adventure/main.c b/01_text_adventure/main.c index 666cd18..1f7e697 100644 --- a/01_text_adventure/main.c +++ b/01_text_adventure/main.c @@ -17,8 +17,6 @@ int main(void) { Game *g = game_create(); game_load_rooms(g); g->current_room = &g->rooms->rooms[0]; - game_load_transitions(g); - printf("loaded transitions\n"); game_load_words(g); printf("loaded words\n"); game_load_flags(g); diff --git a/01_text_adventure/transition.c b/01_text_adventure/transition.c deleted file mode 100644 index 7fccd79..0000000 --- a/01_text_adventure/transition.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -#include "transition.h" -#include "game.h" -#include "util.h" - -void load_transition(Game *g, char *line) { - char *token = strtok(line, "|"); - g->transitions->transitions[g->transitions->count].from = find_room(g->rooms, token); - - token = strtok(NULL, "|"); - g->transitions->transitions[g->transitions->count].via = command_from_string(token); - - token = strtok(NULL, "|"); - g->transitions->transitions[g->transitions->count].to = find_room(g->rooms, token); - - token = strtok(NULL, "\n"); - g->transitions->transitions[g->transitions->count].description = malloc(strlen(token) + 1); - strcpy(g->transitions->transitions[g->transitions->count].description, token); - - g->transitions->count++; -} - -#include "data/transitions.c" -void game_load_transitions(Game *g) { - parse_multiline_string(g, data_transitions_txt, &load_transition); -} - -Transition *find_transition(Transitions *t, Room *from, CommandType via) { - for (int i = 0; i < t->count; i++) { - Transition *candidate = &t->transitions[i]; - if (candidate->from == from && candidate->via == via) return candidate; - } - - return NULL; -} diff --git a/01_text_adventure/transition.h b/01_text_adventure/transition.h deleted file mode 100644 index 4accf45..0000000 --- a/01_text_adventure/transition.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef _FD_TRANSITION_ -#define _FD_TRANSITION_ - -typedef struct Transition Transition; -typedef struct Transitions Transitions; - -#include "game.h" -#include "room.h" - -struct Transition { - Room *from; - CommandType via; - Room *to; - char *description; -}; - -struct Transitions { - Transition transitions[200]; - int count; -}; - -void game_load_transitions(Game *g); -Transition *find_transition(Transitions *t, Room *from, CommandType via); - -#endif