thrive/01_text_adventure/item.c

53 lines
1.3 KiB
C
Raw Normal View History

2025-01-21 20:38:34 -05:00
#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]);
}
}