From faccabcafc459513269e11675e2e6fb4fa71ba54 Mon Sep 17 00:00:00 2001
From: Bill Rossi <bassguitarbill@gmail.com>
Date: Tue, 21 Jan 2025 20:38:34 -0500
Subject: [PATCH] Items + showing them in rooms

---
 01_text_adventure/Makefile       |  2 +-
 01_text_adventure/data/items.txt |  4 +++
 01_text_adventure/data/words.txt |  3 ++
 01_text_adventure/effect.c       |  1 +
 01_text_adventure/game.c         |  4 +++
 01_text_adventure/game.h         |  2 ++
 01_text_adventure/item.c         | 52 ++++++++++++++++++++++++++++++++
 01_text_adventure/item.h         | 27 +++++++++++++++++
 8 files changed, 94 insertions(+), 1 deletion(-)
 create mode 100644 01_text_adventure/data/items.txt
 create mode 100644 01_text_adventure/item.c
 create mode 100644 01_text_adventure/item.h

diff --git a/01_text_adventure/Makefile b/01_text_adventure/Makefile
index 06e9a96..1733fae 100644
--- a/01_text_adventure/Makefile
+++ b/01_text_adventure/Makefile
@@ -3,7 +3,7 @@ CFLAGS=-Wall -lraylib -lm -lpthread -ldl -lX11
 
 .PHONY: clean run
 
-game: data/actions.c data/rooms.c data/room_ins.c data/words.c data/flags.c *.c
+game: data/actions.c data/rooms.c data/room_ins.c data/words.c data/flags.c data/items.c *.c
 	$(CC) *.c $(CFLAGS) -o game
 
 data/%.c: data/%.txt
diff --git a/01_text_adventure/data/items.txt b/01_text_adventure/data/items.txt
new file mode 100644
index 0000000..2897857
--- /dev/null
+++ b/01_text_adventure/data/items.txt
@@ -0,0 +1,4 @@
+UNLIT_TORCH | a torch | true | A sturdy torch. It's unlit.
+LIT_TORCH | a lit torch | true | A sturdy torch. It's on fire, and illuminates its surroundings.
+FLASK | a flask | true | An empty glass flask, with a cork.
+ANVIL | an anvil | false | A heavy iron anvil.
diff --git a/01_text_adventure/data/words.txt b/01_text_adventure/data/words.txt
index 900c322..ccb2368 100644
--- a/01_text_adventure/data/words.txt
+++ b/01_text_adventure/data/words.txt
@@ -9,3 +9,6 @@ WEST|WEST,W
 LOOK|LOOK,L
 INVENTORY|INVENTORY,I,INV
 HELP|HELP,H
+ANVIL|ANVIL
+FLASK|FLASK,BOTTLE,JAR
+TORCH|TORCH
diff --git a/01_text_adventure/effect.c b/01_text_adventure/effect.c
index 46c17e7..27dd075 100644
--- a/01_text_adventure/effect.c
+++ b/01_text_adventure/effect.c
@@ -32,6 +32,7 @@ void cause_effect(Game *g, Effect *e) {
     break;
   case EFFECT_LOOK_ROOM:
     push_line_to_log(g->input->log, find_room_in(g)->description);
+    log_items_in_room(g, g->current_room);
     break;
   }
 }
diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c
index 75a5a8a..2bbf213 100644
--- a/01_text_adventure/game.c
+++ b/01_text_adventure/game.c
@@ -11,6 +11,7 @@
 #include "predicate.h"
 #include "action.h"
 #include "parse.h"
+#include "item.h"
 
 Game *game_create(void) {
   Game *g = malloc(sizeof(Game));
@@ -21,6 +22,9 @@ Game *game_create(void) {
   game_load_actions(g);
   game_load_rooms(g);
   game_load_room_ins(g);
+  game_load_items(g);
+
+  g->items->items[0].location = &g->rooms->rooms[0];
 
   Log *log = create_log();
   g->log = log;
diff --git a/01_text_adventure/game.h b/01_text_adventure/game.h
index 62f864a..5f12547 100644
--- a/01_text_adventure/game.h
+++ b/01_text_adventure/game.h
@@ -22,6 +22,7 @@ typedef enum CommandType {
 #include "word.h"
 #include "flag.h"
 #include "action.h"
+#include "item.h"
 
 struct Game {
   bool should_close;
@@ -33,6 +34,7 @@ struct Game {
   Words *words;
   Flags *flags;
   Actions *actions;
+  Items *items;
 };
 
 Game *game_create(void);
diff --git a/01_text_adventure/item.c b/01_text_adventure/item.c
new file mode 100644
index 0000000..8d2c338
--- /dev/null
+++ b/01_text_adventure/item.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "game.h"
+#include "room.h"
+#include "item.h"
+#include "util.h"
+
+void load_item(Game *g, char *line) {
+  char *token = strtok(line, "|");
+  Item *item = &g->items->items[g->items->count];
+  item->name = malloc(strlen(token) + 1);
+  strcpy(item->name, token);
+
+  token = strtok(NULL, "|");
+  item->indefinite = malloc(strlen(token) + 1);
+  strcpy(item->indefinite, token);
+
+  token = strtok(NULL, "|");
+  item->pickuppable = strcmp("true", token) == 0;
+
+  token = strtok(NULL, "|");
+  item->description = malloc(strlen(token) + 1);
+  strcpy(item->description, token);
+
+  item->in_inventory = false;
+  item->location = NULL;
+
+  g->items->count++;
+}
+
+#include "data/items.c"
+void game_load_items(Game *g) {
+  g->items = malloc(sizeof(Items));
+  g->items->count = 0;
+  parse_multiline_string(g, data_items_txt, &load_item);
+  printf("loaded items\n");
+}
+
+#define ITEM_IN_ROOM "There is %s here."
+void log_item_in_room(Game *g, Item *i) {
+  char *response = malloc(strlen(ITEM_IN_ROOM) + strlen(i->indefinite) + 1);
+  sprintf(response, ITEM_IN_ROOM, 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]);
+  }
+}
diff --git a/01_text_adventure/item.h b/01_text_adventure/item.h
new file mode 100644
index 0000000..31f76e0
--- /dev/null
+++ b/01_text_adventure/item.h
@@ -0,0 +1,27 @@
+#ifndef _FD_ITEM_
+#define _FD_ITEM_
+
+typedef struct Item Item;
+typedef struct Items Items;
+
+#include "game.h"
+#include "room.h"
+
+struct Item {
+  char *name;
+  char *indefinite;
+  bool pickuppable;
+  char *description;
+  bool in_inventory;
+  Room *location;
+};
+
+struct Items {
+  Item items[200];
+  int count;
+};
+
+void game_load_items(Game *g);
+void log_items_in_room(Game *g, Room *r);
+
+#endif