From 023efc7685ad236737fc7399f61fd8485b6b822f Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 1 Dec 2018 17:09:23 +0100 Subject: [PATCH] - scriptified RemoveInventory and Inventory.OnDestroy. --- src/actor.h | 3 -- src/d_player.h | 1 - src/g_inventory/a_pickups.cpp | 20 ------- src/g_inventory/a_pickups.h | 1 - src/p_mobj.cpp | 42 --------------- src/p_user.cpp | 52 ------------------- wadsrc/static/zscript/actor.txt | 1 - wadsrc/static/zscript/actor_inventory.txt | 30 +++++++++++ wadsrc/static/zscript/inventory/inventory.txt | 40 ++++++++++++++ .../zscript/shared/player_inventory.txt | 52 +++++++++++++++++++ 10 files changed, 122 insertions(+), 120 deletions(-) diff --git a/src/actor.h b/src/actor.h index 9dae9a543f..5218b7fc3e 100644 --- a/src/actor.h +++ b/src/actor.h @@ -746,9 +746,6 @@ public: // APlayerPawn for some specific handling for players. None of this // should ever be overridden by custom classes. - // Removes the item from the inventory list. - virtual void RemoveInventory (AInventory *item); - // Take the amount value of an item from the inventory list. // If nothing is left, the item may be destroyed. // Returns true if the initial item count is positive. diff --git a/src/d_player.h b/src/d_player.h index 59b25814a5..e6137b53f5 100644 --- a/src/d_player.h +++ b/src/d_player.h @@ -89,7 +89,6 @@ public: virtual void PostBeginPlay() override; virtual void Tick() override; - virtual void RemoveInventory (AInventory *item) override; virtual bool UseInventory (AInventory *item) override; virtual void BeginPlay () override; virtual bool UpdateWaterLevel (bool splash) override; diff --git a/src/g_inventory/a_pickups.cpp b/src/g_inventory/a_pickups.cpp index 17a112a987..c1bd3c7016 100644 --- a/src/g_inventory/a_pickups.cpp +++ b/src/g_inventory/a_pickups.cpp @@ -180,26 +180,6 @@ DEFINE_ACTION_FUNCTION(AInventory, PrintPickupMessage) return 0; } -//=========================================================================== -// -// AInventory :: Destroy -// -//=========================================================================== - -void AInventory::OnDestroy () -{ - if (Owner != NULL) - { - Owner->RemoveInventory (this); - } - Inventory = NULL; - Super::OnDestroy(); - - // Although contrived it can theoretically happen that these variables still got a pointer to this item - if (SendItemUse == this) SendItemUse = NULL; - if (SendItemDrop == this) SendItemDrop = NULL; -} - //=========================================================================== // // AInventory :: DepleteOrDestroy diff --git a/src/g_inventory/a_pickups.h b/src/g_inventory/a_pickups.h index 82843f056e..61d764c623 100644 --- a/src/g_inventory/a_pickups.h +++ b/src/g_inventory/a_pickups.h @@ -71,7 +71,6 @@ class AInventory : public AActor public: virtual void Serialize(FSerializer &arc) override; - virtual void OnDestroy() override; virtual void Tick() override; virtual bool Massacre() override; diff --git a/src/p_mobj.cpp b/src/p_mobj.cpp index ee620c0883..2e9f109a6f 100644 --- a/src/p_mobj.cpp +++ b/src/p_mobj.cpp @@ -753,48 +753,6 @@ DEFINE_ACTION_FUNCTION(AActor, SetState) ACTION_RETURN_BOOL(self->SetState(state, nofunction)); }; -//============================================================================ -// -// AActor :: RemoveInventory -// -//============================================================================ - -void AActor::RemoveInventory(AInventory *item) -{ - AInventory *inv, **invp; - - if (item != NULL && item->Owner != NULL) // can happen if the owner was destroyed by some action from an item's use state. - { - invp = &item->Owner->Inventory; - for (inv = *invp; inv != NULL; invp = &inv->Inventory, inv = *invp) - { - if (inv == item) - { - *invp = item->Inventory; - - IFVIRTUALPTR(item, AInventory, DetachFromOwner) - { - VMValue params[1] = { item }; - VMCall(func, params, 1, nullptr, 0); - } - - item->Owner = NULL; - item->Inventory = NULL; - break; - } - } - } -} - -DEFINE_ACTION_FUNCTION(AActor, RemoveInventory) -{ - PARAM_SELF_PROLOGUE(AActor); - PARAM_OBJECT_NOT_NULL(item, AInventory); - self->RemoveInventory(item); - return 0; -} - - //============================================================================ // // AActor :: TakeInventory diff --git a/src/p_user.cpp b/src/p_user.cpp index d5103b6607..d440b5f2ed 100644 --- a/src/p_user.cpp +++ b/src/p_user.cpp @@ -938,58 +938,6 @@ void APlayerPawn::PostBeginPlay() } } -//=========================================================================== -// -// APlayerPawn :: RemoveInventory -// -//=========================================================================== - -void APlayerPawn::RemoveInventory (AInventory *item) -{ - bool pickWeap = false; - - // Since voodoo dolls aren't supposed to have an inventory, there should be - // no need to redirect them to the real player here as there is with AddInventory. - - // If the item removed is the selected one, select something else, either the next - // item, if there is one, or the previous item. - if (player != NULL) - { - if (InvSel == item) - { - InvSel = item->NextInv (); - if (InvSel == NULL) - { - InvSel = item->PrevInv (); - } - } - if (InvFirst == item) - { - InvFirst = item->NextInv (); - if (InvFirst == NULL) - { - InvFirst = item->PrevInv (); - } - } - if (item == player->PendingWeapon) - { - player->PendingWeapon = WP_NOCHANGE; - } - if (item == player->ReadyWeapon) - { - // If the current weapon is removed, clear the refire counter and pick a new one. - pickWeap = true; - player->ReadyWeapon = NULL; - player->refire = 0; - } - } - Super::RemoveInventory (item); - if (pickWeap && player->mo == this && player->PendingWeapon == WP_NOCHANGE) - { - PickNewWeapon (NULL); - } -} - //=========================================================================== // // APlayerPawn :: UseInventory diff --git a/wadsrc/static/zscript/actor.txt b/wadsrc/static/zscript/actor.txt index 39843f8520..43a5722d4f 100644 --- a/wadsrc/static/zscript/actor.txt +++ b/wadsrc/static/zscript/actor.txt @@ -741,7 +741,6 @@ class Actor : Thinker native native clearscope int GetAge() const; native bool CheckClass(class checkclass, int ptr_select = AAPTR_DEFAULT, bool match_superclass = false); - native void RemoveInventory(Inventory inv); native void ClearInventory(); protected native void DestroyAllInventory(); // This is not supposed to be called by user code! native bool SetInventory(class itemclass, int amount, bool beyondMax = false); diff --git a/wadsrc/static/zscript/actor_inventory.txt b/wadsrc/static/zscript/actor_inventory.txt index d28d756549..676fe7ba60 100644 --- a/wadsrc/static/zscript/actor_inventory.txt +++ b/wadsrc/static/zscript/actor_inventory.txt @@ -80,4 +80,34 @@ extend class Actor return result; } + //============================================================================ + // + // AActor :: RemoveInventory + // + //============================================================================ + + virtual void RemoveInventory(Inventory item) + { + Inventory invp; + + if (item != NULL && item.Owner != NULL) // can happen if the owner was destroyed by some action from an item's use state. + { + if (Inv == item) Inv = item.Inv; + else + { + for (invp = Inv; invp != null; invp = invp.Inv) + { + if (invp.Inv == item) + { + invp.Inv = item.Inv; + item.DetachFromOwner(); + break; + } + } + } + item.Owner = NULL; + item.Inv = NULL; + } + } + } \ No newline at end of file diff --git a/wadsrc/static/zscript/inventory/inventory.txt b/wadsrc/static/zscript/inventory/inventory.txt index 7672bf1c8b..4898f63ea4 100644 --- a/wadsrc/static/zscript/inventory/inventory.txt +++ b/wadsrc/static/zscript/inventory/inventory.txt @@ -98,6 +98,22 @@ class Inventory : Actor native bDropped = true; // [RH] Items are dropped by default } + //=========================================================================== + // + // AInventory :: Destroy + // + //=========================================================================== + + override void OnDestroy () + { + if (Owner != NULL) + { + Owner.RemoveInventory (self); + } + Inv = NULL; + Super.OnDestroy(); + } + //--------------------------------------------------------------------------- // // PROC A_RestoreSpecialThing1 @@ -990,6 +1006,30 @@ class Inventory : Actor native } + //=========================================================================== + // + // AInventory :: PrevInv + // + // Returns the previous item with IF_INVBAR set. + // + //=========================================================================== + + Inventory PrevInv () + { + Inventory lastgood = NULL; + Inventory item = Owner.Inv; + + while (item != NULL && item != self) + { + if (item.bInvBar) + { + lastgood = item; + } + item = item.Inv; + } + return lastgood; + } + //=========================================================================== // // AInventory :: OnDrop diff --git a/wadsrc/static/zscript/shared/player_inventory.txt b/wadsrc/static/zscript/shared/player_inventory.txt index c68c308140..748a293d3f 100644 --- a/wadsrc/static/zscript/shared/player_inventory.txt +++ b/wadsrc/static/zscript/shared/player_inventory.txt @@ -23,4 +23,56 @@ extend class PlayerPawn } } + //=========================================================================== + // + // APlayerPawn :: RemoveInventory + // + //=========================================================================== + + override void RemoveInventory (Inventory item) + { + bool pickWeap = false; + + // Since voodoo dolls aren't supposed to have an inventory, there should be + // no need to redirect them to the real player here as there is with AddInventory. + + // If the item removed is the selected one, select something else, either the next + // item, if there is one, or the previous item. + if (player != NULL) + { + if (InvSel == item) + { + InvSel = item.NextInv (); + if (InvSel == NULL) + { + InvSel = item.PrevInv (); + } + } + if (InvFirst == item) + { + InvFirst = item.NextInv (); + if (InvFirst == NULL) + { + InvFirst = item.PrevInv (); + } + } + if (item == player.PendingWeapon) + { + player.PendingWeapon = WP_NOCHANGE; + } + if (item == player.ReadyWeapon) + { + // If the current weapon is removed, clear the refire counter and pick a new one. + pickWeap = true; + player.ReadyWeapon = NULL; + player.refire = 0; + } + } + Super.RemoveInventory (item); + if (pickWeap && player.mo == self && player.PendingWeapon == WP_NOCHANGE) + { + PickNewWeapon (NULL); + } + } + } \ No newline at end of file