Action Management ~ Item Stacking Design - uchicago-cs/chiventure GitHub Wiki
Action Management/Game State - Item Stacking Design
Introduction
Currently, an item has to have a unique item_id to be added to the following:
- A game
- A player's inventory
- A room
- An NPC's inventory
This wiki concerns itself with developing a design to allow multiple items with identical item_ids to be added to these containers.
Some notes:
- Items with the same item_id should NOT have to be otherwise identical; for example, there could be two items with item_id "apple," but one has the effect of increasing HP when eaten and one has the effect of poisoning the player when eaten
- That being said, two completely identical objects should be allowed in the same container
- Contact the NPC team to inform them of any changes that may become necessary for their functions.
- For reference while coding: uthash User Guide, utlist information
Design Plan
- Add a field to the
item_t
struct - a pointer to another item- This pointer points to an item with the same
item_id
as the current item
- This pointer points to an item with the same
- Add two new functions to item.h:
add_item_to_hash
andremove_item_from_hash
- When adding an item, remove the current hashtable entry, have the new item point to this item, and store the new item in the hashtable
- Note that if there is not already an item at this hashtable entry, the new item will just be pointing to NULL
- When removing an item, store what the item is pointing to in the hashtable
- Note that if the item is not pointing to another item, the new hashtable entry will just be NULL
- Afterwards, set the item being removed to be pointing to NULL
- When adding an item, remove the current hashtable entry, have the new item point to this item, and store the new item in the hashtable
- Replace the body
add_item_to_[blank]
functions with a call toadd_item_to_hash
- Will be necessary to unwrap the item hashtable from the passed struct first
- Add functions to remove an
item_t
from a game/room/inventory:- Similar to the
add_item_to_[blank]
functions, simply callremove_item_from_hash
after unwrapping the item hashtable
- Similar to the
- Add a function to convert an item hashtable to an item linked-list
- Add item to list
- Check if item's next != NULL
- If so, iterate to next item and repeat
- When performing an action on an item:
- Try action on head of the list (passed as the item to
do_[blank]_action
) - If
possible_action
,all_conditions_met
, anddo_all_effects all fail
, and item's next != NULL, try again with the next pointer
- Try action on head of the list (passed as the item to
To-Do:
- Merge PR
Completed:
- REFACTORING:
- Wrote
add_item_to_hash
function & updated code that uses identical functionality - Wrote
remove_item_from_hash
functions - Wrote functions to remove item from game/room/player inventory/NPC inventory (using
remove_item_from_hash
functions) - Wrote
get_all_items_in_hash
function & updated code that uses identical functionality - Wrote/updated tests for refactored code
- Wrote
- ITEM STACKING IMPLEMENTATION:
- Edited item struct with a pointer to another item
- Updated
add_item_to_hash
to take pointer into account - Updated
get_all_items_in_hash
to take pointer into account - Updated
remove_item_from_hash
to take pointer into account - Reworked
kind1_action_operation
andkind3_action_operation
to check all identical items if anything fails - Updated
look_operation
(operations.c
) to mention there being multiple of an item - Wrote/updated tests for handling items with identical ids in the various item hashtable functions