NSNavAI: Inventory management functions.

This commit is contained in:
Marco Cawthorne 2024-04-22 21:47:02 -07:00
parent 822a7221b2
commit bd7e78ab94
Signed by: eukara
GPG Key ID: CE2032F0A2882A22
3 changed files with 114 additions and 1 deletions

View File

@ -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;
};

View File

@ -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);
}

View File

@ -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"