From eb439868761860b26b4aa61724290ce2c6349850 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Tue, 21 Jan 2025 21:08:05 -0500 Subject: [PATCH] See your inventory! --- 01_text_adventure/data/actions.txt | 2 +- 01_text_adventure/effect.c | 8 ++++++++ 01_text_adventure/effect.h | 1 + 01_text_adventure/item.c | 22 ++++++++++++++++++++++ 01_text_adventure/item.h | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/01_text_adventure/data/actions.txt b/01_text_adventure/data/actions.txt index 4484326..74896af 100644 --- a/01_text_adventure/data/actions.txt +++ b/01_text_adventure/data/actions.txt @@ -1,7 +1,7 @@ QUIT | * | 1000 | Bye! | QUIT_GAME() LOOK | * | 1 | * | LOOK_ROOM() HELP | * | 1 | Type commands to explore the environment. Use 'LOOK' to examine the room you're in, or 'LOOK ' to examine an item closely. 'N', 'S', 'E', and 'W' will move you in the four cardinal directions, and 'I' will let you check your inventory. The parser is not very advanced, so do your best. 'Q', 'QUIT', or 'EXIT' will let you quit. | -INVENTORY | * | 1 | Your inventory is empty. | +INVENTORY | * | 1 | * | CHECK_INVENTORY() NORTH | * | 1 | You can't go north from here. | SOUTH | * | 1 | You can't go south from here. | EAST | * | 1 | You can't go east from here. | diff --git a/01_text_adventure/effect.c b/01_text_adventure/effect.c index d07dc50..cc5af56 100644 --- a/01_text_adventure/effect.c +++ b/01_text_adventure/effect.c @@ -40,6 +40,9 @@ void cause_effect(Game *g, Effect *e) { case EFFECT_DROP_ITEM: drop_item(g, e->argument); break; + case EFFECT_CHECK_INVENTORY: + check_inventory(g); + break; } } @@ -71,6 +74,8 @@ Effect *create_effect(Game *g, const char *string) { e->type = EFFECT_TAKE_ITEM; } else if (strcmp(token, "DROP") == 0) { e->type = EFFECT_DROP_ITEM; + } else if (strcmp(token, "CHECK_INVENTORY") == 0) { + e->type = EFFECT_CHECK_INVENTORY; } token = strtok_r(NULL, ")", &strtok_guy); @@ -116,5 +121,8 @@ void print_effect(Effect *e) { case EFFECT_DROP_ITEM: printf("DROP(%s)", e->argument); break; + case EFFECT_CHECK_INVENTORY: + printf("CHECK_INVENTORY()"); + break; } } diff --git a/01_text_adventure/effect.h b/01_text_adventure/effect.h index 9099c9c..8f769a2 100644 --- a/01_text_adventure/effect.h +++ b/01_text_adventure/effect.h @@ -14,6 +14,7 @@ typedef enum EffectType { EFFECT_LOOK_ROOM, EFFECT_TAKE_ITEM, EFFECT_DROP_ITEM, + EFFECT_CHECK_INVENTORY, } EffectType; #include "game.h" diff --git a/01_text_adventure/item.c b/01_text_adventure/item.c index 8ec5739..92fd582 100644 --- a/01_text_adventure/item.c +++ b/01_text_adventure/item.c @@ -46,6 +46,14 @@ void log_item_in_room(Game *g, Item *i) { free(response); } +#define ITEM_IN_INVENTORY "You have %s." +void log_item_in_inventory(Game *g, Item *i) { + char *response = malloc(strlen(ITEM_IN_INVENTORY) + strlen(i->indefinite) + 1); + sprintf(response, ITEM_IN_INVENTORY, i->indefinite); + push_line_to_log(g->input->log, response); + free(response); +} + void log_items_in_room(Game *g, Room *r) { for (int i = 0; i < g->items->count; i++) { if (g->items->items[i].location == r) log_item_in_room(g, &g->items->items[i]); @@ -71,3 +79,17 @@ void drop_item(Game *g, char *item_name) { item->location = g->current_room; item->in_inventory = false; } + +void check_inventory(Game *g) { + bool empty = true; + for (int i = 0; i < g->items->count; i++) { + if (g->items->items[i].in_inventory) { + empty = false; + log_item_in_inventory(g, &g->items->items[i]); + } + } + + if (empty) { + push_line_to_log(g->input->log, "Your inventory is empty."); + } +} diff --git a/01_text_adventure/item.h b/01_text_adventure/item.h index 5d51804..bf4688b 100644 --- a/01_text_adventure/item.h +++ b/01_text_adventure/item.h @@ -26,5 +26,6 @@ void log_items_in_room(Game *g, Room *r); Item *find_item(Items *items, char *item_name); void take_item(Game *g, char *item_name); void drop_item(Game *g, char *item_name); +void check_inventory(Game *g); #endif