From bfc20bdfce013a485e38ca6de9424d3e3c0a4c8c Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 19 Jan 2025 18:18:48 -0500 Subject: [PATCH] Print room description when first entering a room --- 01_text_adventure/effect.c | 3 +-- 01_text_adventure/game.c | 9 +++++++++ 01_text_adventure/game.h | 1 + 01_text_adventure/room.c | 3 ++- 01_text_adventure/room.h | 3 +++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/01_text_adventure/effect.c b/01_text_adventure/effect.c index 5b27144..46c17e7 100644 --- a/01_text_adventure/effect.c +++ b/01_text_adventure/effect.c @@ -13,7 +13,7 @@ void cause_effect(Game *g, Effect *e) { case EFFECT_NOOP: break; case EFFECT_GOTO: - g->current_room = find_room(g->rooms, e->argument); + change_current_room(g, find_room(g->rooms, e->argument)); break; case EFFECT_INCREMENT: find_flag(g->flags, e->argument)->value++; @@ -31,7 +31,6 @@ void cause_effect(Game *g, Effect *e) { g->should_close = true; break; case EFFECT_LOOK_ROOM: - // TODO push_line_to_log(g->input->log, find_room_in(g)->description); break; } diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index c3df0f1..75a5a8a 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -34,6 +34,7 @@ Game *game_create(void) { g->input = input; + change_current_room(g, &g->rooms->rooms[0]); return g; } @@ -85,3 +86,11 @@ void game_run_until_close(Game *g) { game_draw(g); } } + +void change_current_room(Game *g, Room *r) { + g->current_room = r; + if (!r->visited) { + push_line_to_log(g->input->log, find_room_in(g)->description); + } + r->visited = true; +} diff --git a/01_text_adventure/game.h b/01_text_adventure/game.h index 4985a76..62f864a 100644 --- a/01_text_adventure/game.h +++ b/01_text_adventure/game.h @@ -43,5 +43,6 @@ void game_run_until_close(Game *g); void game_handle_input(Game *g); void game_draw(Game *g); void free_game(Game *g); +void change_current_room(Game *g, Room *r); #endif diff --git a/01_text_adventure/room.c b/01_text_adventure/room.c index b01f98c..4f73962 100644 --- a/01_text_adventure/room.c +++ b/01_text_adventure/room.c @@ -11,6 +11,8 @@ void load_room(Game *g, char *line) { g->rooms->rooms[g->rooms->count].name = malloc(strlen(token) + 1); strcpy(g->rooms->rooms[g->rooms->count].name, token); + g->rooms->rooms[g->rooms->count].visited = false; + g->rooms->count++; } @@ -20,7 +22,6 @@ void game_load_rooms(Game *g) { g->rooms->count = 0; parse_multiline_string(g, data_rooms_txt, &load_room); printf("loaded rooms\n"); - g->current_room = &g->rooms->rooms[0]; } void free_room(Room r) { diff --git a/01_text_adventure/room.h b/01_text_adventure/room.h index 70d2d49..f8579f1 100644 --- a/01_text_adventure/room.h +++ b/01_text_adventure/room.h @@ -4,8 +4,11 @@ typedef struct Room Room; typedef struct Rooms Rooms; +#include + struct Room { char *name; + bool visited; }; struct Rooms {