Quests ~ API Design Document - uchicago-cs/chiventure GitHub Wiki

Quests are not fully integrated into chiventure. The goals from spring 2021 were to incorporate quests into the game-state module and other RPG features like NPCs.

Integrating quests into the game

With the hash table quest_hash_t *quests quests can be finally incorporated into chiventure. The hash table is already included in the game struct found in game-state/game.h.

typedef struct game {
    /* an iteratable hashtable of players */
    /* using the macros provided in uthash.h */
    /* the expected size is 1 for single player games but this can change */
    player_hash_t *all_players;

    /* an iteratable hashtable of rooms */
    /* using the macros provided in uthash.h */
    room_hash_t *all_rooms;

    /* an iteratable hashtable of items */
    /* using the macros provided in uthash.h */
    item_hash_t *all_items;

    quest_hash_t *all_quests;
};

We are interested in delving into game-state/player.h and storing a current tree of quests that the player has there. As the player explores the area, they will add the quests they pick up along the way in quest_hash_t *quests. In particular, we think that the player struct could potentially look like this:

/* A player in game */
typedef struct player {
   /* hh is used for hashtable, as provided in uthash.h*/
   UT_hash_handle hh;

   char *player_id;
   int level;
   int health;
   int xp;
   class_t *player_class;
   item_hash_t *inventory;
   quest_hash_t *quests;
} player_t;

Loading the game

  1. Create a load_quest.c file within src/wdl/src/ with the following function: add_quests_to_game(obj_t* doc, game_t *g); This function would call extract_objects(doc, "QUESTS") to extract the parsed quest objects Then it would iterate through the quests using HASH_ITER and each iteration would call add_quest_to_game(g, quest);

We want this function to be called when a game is being loaded in so we plan to have it called by the load_yaml_game function:

/* See load_game.h for documentation */
game_t *load_yaml_game(obj_t *big_document)
{
    int rc;
    game_t *game = create_game(big_document);

    // call functions that parse items, actions, rooms, and game attributes
    // into a game pointer
    rc = add_rooms_to_game(big_document, game);
    if(rc != SUCCESS)
    {
        fprintf(stderr, "Error adding rooms to game.\n");
        return NULL;
    }

    rc = add_quests_to_game(big_document, game);
    if (rc != SUCCESS)
    {
        fprintf(stderr, "Error adding quests to game.\n");
        return NULL;
    }
}
  1. Add add_quest_to_game(obt_t* doc, quest_t* q); to game_state/game.c This function is called by add_quests_to_game, this function uses HASH_FIND to check if the quest has already been added to all_quests in the game struct and if not adds it using HASH_ADD_KEYPTR.