Skip to content

NPC ~ Action Documentation

junasano edited this page May 31, 2022 · 6 revisions

Introduction

Action is an aspect of the NPC module which allows the player to interact with the NPCs by talking to and trading with them. The current actions can be split into three larger groups, called KIND 5, KIND 6, and KIND 7 ACTIONS. The name of these groups is inspired by, but not directly related to, actions KIND 1, 2, 3, 4 in the action management code. With KIND 5 ACTIONS, the player can either TALK_TO or IGNORE to the NPCs. In KIND 6, the player can GIVE or STEAL an item from the NPCs. In KIND 7, the player can TRADE or BUY with the NPCs. In the actual implementation, the functions are called do_npc_action, do_npc_item_action, and do_npc_exchange_action respectively.

Structure

In order to implement these npc specific action functions, a new struct called agent_t was introduced to Chiventure. The agent_t can hold an item or npc, meaning that a new layer above items and npcs is added.Many helper functions that will be used in the future implementation of npc/actions overlap with the item functions, and thus agent_t was created. agent_t is in npc.h, however, there were many files that were affected due to this new struct including files in actionmanagement and game_state.

/* Agent: a struct of things that you can perform actions upon
 * - item: an item
 * - npc: an NPC
 */
typedef struct agent
{
   item_t *item;
   npc_t *npc;
} agent_t;

Furthermore, the enum actions_t and action_kind (both in action_structs.h) were modified so that they contain the respective KIND actions related to npcs.

Lastly, an important structure is the linked list called list_action_t in action_structs.h. It is simply a linked list of actions_t. This was added as a new variable in the npc_t struct to represent all of the possible actions the player can initiate with the npc.

/* A linked list struct that contains the following:
 * - action: the action at the head of the list
 * - next: the next item in the linked list
 */
 typedef struct list_action {
    enum actions *npc_action;
    struct list_action *next;
} list_action_t;

Implementation

The three functions of this module consist of do_npc_action, do_npc_item_action, and do_npc_item_item_action. In order to assist with the implementation of these, a helper function called contains_action was also introduced. It checks if the action is listed on the list_npc_action_t of an npc (which is a represent all of the possible actions the player can initiate with the npc).

With this helper function, in mind, they all share the same basic parameters of context struct encapsulating the shared state in chiventure, an NPC action type struct, an npc struct, and a string describing the result of the function. The latter two contains an item parameter which is the item to exchange hands for KIND 6 and which is the item in the npc's inventory for KIND 7. The first few include structures from other modules and can be found in the respective modules. The last parameter of the string is especially useful for integrating the npc actions aspect with the npc dialogue aspect, which is explained more in depth in the action in the NPC Dialogue and Action Implementation documentation. If all conditions are met, the player is able to successfully interact with the npc and a 0 is returned, while errors will be returned if successful

Future work

KIND 5 and KIND 6 are fully complete. However, with KIND 7, the BUY part cannot be implemented yet since there needs to be a currency-like feature first. Additionally, for TRADE for KIND 7 the player can’t choose which item to give away and it just takes the first item on the list which has a higher price than the wanted item. KIND 5 and KIND 6 are fully complete. However, with KIND 7, the BUY part cannot be implemented yet since there needs to be a currency-like feature first. Additionally, for TRADE for KIND 7 the player can’t choose which item to give away and it just takes the first item on the list which has a higher price than the wanted item. These two issues will are also outlined in the backlog.

Currency: https://github.com/uchicago-cs/chiventure/projects/1#card-82635563

Choosing item: https://github.com/uchicago-cs/chiventure/projects/1#card-82635632

Clone this wiki locally