From 7b35f32f3de012ccb0ccabe9086cffddcfe0bd00 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Thu, 21 Apr 2016 15:28:51 +0200 Subject: [PATCH] - Added the 'GiveInventory' method to the actor. This will help cleaning up the item giving code. Returns a bool, in case the pickup failure might turn to be interesting in the future. --- src/actor.h | 4 ++++ src/p_mobj.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/actor.h b/src/actor.h index 8741a9597f..a56d0d6d93 100644 --- a/src/actor.h +++ b/src/actor.h @@ -661,6 +661,10 @@ public: // Adds the item to this actor's inventory and sets its Owner. virtual void AddInventory (AInventory *item); + // Give an item to the actor and pick it up. + // Returns true if the item pickup succeeded. + virtual bool GiveInventory (PClassInventory *type, int amount, bool givecheat = false); + // Removes the item from the inventory list. virtual void RemoveInventory (AInventory *item); diff --git a/src/p_mobj.cpp b/src/p_mobj.cpp index 46bc320dfc..b7d8871951 100644 --- a/src/p_mobj.cpp +++ b/src/p_mobj.cpp @@ -612,6 +612,62 @@ void AActor::AddInventory (AInventory *item) Inventory->InventoryID = InventoryID++; } +//============================================================================ +// +// AActor :: GiveInventory +// +//============================================================================ + +bool AActor::GiveInventory(PClassInventory *type, int amount, bool givecheat) +{ + bool result = true; + + AWeapon *savedPendingWeap = player != NULL ? player->PendingWeapon : NULL; + bool hadweap = player != NULL ? player->ReadyWeapon != NULL : true; + + AInventory *item; + if (!givecheat) + { + item = static_cast(Spawn (type)); + } + else + { + item = static_cast(Spawn (type, Pos(), NO_REPLACE)); + if (item == NULL) return false; + } + + // This shouldn't count for the item statistics! + item->ClearCounters(); + if (type->IsDescendantOf (RUNTIME_CLASS(ABasicArmorPickup))) + { + static_cast(item)->SaveAmount *= amount; + } + else if (type->IsDescendantOf (RUNTIME_CLASS(ABasicArmorBonus))) + { + static_cast(item)->SaveAmount *= amount; + } + else + { + if (!givecheat) + item->Amount = amount; + else + item->Amount = MIN (amount, item->MaxAmount); + } + if (!item->CallTryPickup (this)) + { + item->Destroy (); + result = false; + } + // If the item was a weapon, don't bring it up automatically + // unless the player was not already using a weapon. + // Don't bring it up automatically if this is called by the give cheat. + if (!givecheat && player != NULL && savedPendingWeap != NULL && hadweap) + { + player->PendingWeapon = savedPendingWeap; + } + return result; +} + //============================================================================ // // AActor :: RemoveInventory