From bd7e78ab94bc0480942bb75c91ba893ca6f9dafe Mon Sep 17 00:00:00 2001 From: Marco Cawthorne Date: Mon, 22 Apr 2024 21:47:02 -0700 Subject: [PATCH] NSNavAI: Inventory management functions. --- src/shared/NSNavAI.h | 5 ++ src/shared/NSNavAI.qc | 108 ++++++++++++++++++++++++++++++++++++++++++ src/shared/defs.h | 2 +- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/shared/NSNavAI.h b/src/shared/NSNavAI.h index dbebd1ba..5e8354ab 100644 --- a/src/shared/NSNavAI.h +++ b/src/shared/NSNavAI.h @@ -78,6 +78,10 @@ public: nonvirtual bool GiveItem(string); /** Removes a named NSItem from the inventory Returns `false` when impossible. */ nonvirtual bool RemoveItem(string); + /** Adds the specified NSItem to the inventory. Returns `false` when impossible. */ + nonvirtual bool AddItem(NSItem); + /** Returns `true` or `false` depending on if the entity has the named item. */ + nonvirtual bool HasItem(string); #ifdef SERVER /* overrides */ @@ -135,4 +139,5 @@ private: /* These are defined in side defs\*.def, ammo_types and ammo_names */ int m_iAmmoTypes[MAX_AMMO_TYPES]; + NSItem m_itemList; }; diff --git a/src/shared/NSNavAI.qc b/src/shared/NSNavAI.qc index fa607b69..bad2208d 100644 --- a/src/shared/NSNavAI.qc +++ b/src/shared/NSNavAI.qc @@ -526,14 +526,122 @@ NSNavAI::UseAmmo(int ammoType, int ammoAmount) return (true); } +bool +NSNavAI::HasItem(string itemName) +{ + NSItem linkedList = __NULL__; + + /* we do not have an item. */ + if (!m_itemList) { + return (false); + } + + /* since we have something in the inventory, start there */ + linkedList = m_itemList; + + /* iterate through the inventory, then figure out if we already have it*/ + while (linkedList) { + /* we already have the item. */ + if (linkedList.classname == itemName) { + return (true); + } + + linkedList = linkedList.chain; + } + + return (false); +} + bool NSNavAI::GiveItem(string itemName) { +#ifdef SERVER + NSItem linkedList = __NULL__; + NSItem lastItem = __NULL__; + + /* we do not have an item yet. */ + if (!m_itemList) { + m_itemList = (NSItem)Entity_CreateClass(itemName); + return (true); + } + + /* since we have something in the inventory, start there */ + linkedList = m_itemList; + + /* iterate through the inventory, then figure out if we already have it*/ + while (linkedList) { + /* we already have the item. */ + if (linkedList.classname == itemName) { + return (false); + } + + lastItem = linkedList; + linkedList = linkedList.chain; + } + + /* add it to the back of the chain, so it's part of our inventory. */ + if (lastItem && linkedList == __NULL__) { + NSItem newItem = (NSItem)Entity_CreateClass(itemName); + lastItem.chain = newItem; + } + + return (true); +#else return (false); +#endif } bool NSNavAI::RemoveItem(string itemName) +{ +#ifdef SERVER + NSItem linkedList = __NULL__; + NSItem frontItem = __NULL__; + NSItem lastItem = __NULL__; + NSItem itemToRemove = __NULL__; + bool removeItem = false; + + /* we posess nothing. auto return false. */ + if (!m_itemList) { + return (false); + } + + /* since we have something in the inventory, start there */ + linkedList = m_itemList; + + /* iterate through the inventory, then figure out if we already have it*/ + while (linkedList) { + /* found the item, mark as needing to be removed */ + if (linkedList.classname == itemName) { + removeItem = true; + frontItem = lastItem; /* the one before the current one */ + itemToRemove = linkedList; /* the item to destroy */ + } + + lastItem = linkedList; + linkedList = linkedList.chain; + } + + /* successfully remove the last item */ + if (removeItem == true) { + /* we had an item in front, bridge across the removed item. */ + if (frontItem) { + frontItem.chain = itemToRemove.chain; + } else { + /* this was the first item. set to chain (can be NULL) */ + m_itemList = itemToRemove.chain; + } + + itemToRemove.Destroy(); + return (true); + } +#endif + + return (false); +} + +bool +NSNavAI::AddItem(NSItem theItem) { return (false); } diff --git a/src/shared/defs.h b/src/shared/defs.h index 78a9c213..e70e9a9a 100644 --- a/src/shared/defs.h +++ b/src/shared/defs.h @@ -94,13 +94,13 @@ string __fullspawndata; #include "NSPhysicsEntity.h" #include "NSBrushTrigger.h" #include "NSPointTrigger.h" +#include "NSItem.h" #include "NSNavAI.h" #include "NSMonster.h" #include "NSSquadMonster.h" #include "NSTalkMonster.h" #include "NSSpawnPoint.h" #include "NSProjectile.h" -#include "NSItem.h" #include "NSSpraylogo.h" #include "NSPortal.h" #include "NSDebris.h"