See your inventory!

This commit is contained in:
Bill Rossi 2025-01-21 21:08:05 -05:00
parent 868c2211be
commit eb43986876
5 changed files with 33 additions and 1 deletions

View File

@ -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 <item>' 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. |

View File

@ -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;
}
}

View File

@ -14,6 +14,7 @@ typedef enum EffectType {
EFFECT_LOOK_ROOM,
EFFECT_TAKE_ITEM,
EFFECT_DROP_ITEM,
EFFECT_CHECK_INVENTORY,
} EffectType;
#include "game.h"

View File

@ -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.");
}
}

View File

@ -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