From 7301bbc58edab5d177dbdf1c7bc2c801d375bf9a Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 1 Dec 2018 17:20:23 +0100 Subject: [PATCH] - scriptified GiveAmmo and the one remaining piece of native code still using it. --- src/actor.h | 3 -- src/fragglescript/t_func.cpp | 25 ++---------- src/p_mobj.cpp | 38 ------------------- wadsrc/static/zscript/actor.txt | 1 - wadsrc/static/zscript/actor_inventory.txt | 29 ++++++++++++++ .../static/zscript/scriptutil/scriptutil.txt | 23 +++++++++++ 6 files changed, 55 insertions(+), 64 deletions(-) diff --git a/src/actor.h b/src/actor.h index 58a8c2792..5f663323b 100644 --- a/src/actor.h +++ b/src/actor.h @@ -772,9 +772,6 @@ public: // Returns the first item held with IF_INVBAR set. AInventory *FirstInv (); - // Tries to give the actor some ammo. - bool GiveAmmo (PClassActor *type, int amount); - // Destroys all the inventory the actor is holding. void DestroyAllInventory (); diff --git a/src/fragglescript/t_func.cpp b/src/fragglescript/t_func.cpp index 472d525d5..abfec1033 100644 --- a/src/fragglescript/t_func.cpp +++ b/src/fragglescript/t_func.cpp @@ -2485,30 +2485,11 @@ void FParser::SF_PlayerKeys(void) void FParser::SF_PlayerAmmo(void) { - int playernum, amount; - PClassActor * ammotype; - if (CheckArgs(2)) { - playernum=T_GetPlayerNum(t_argv[0]); - if (playernum==-1) return; - - ammotype=T_GetAmmo(t_argv[1]); - if (!ammotype) return; - - if(t_argc >= 3) - { - AInventory * iammo = players[playernum].mo->FindInventory(ammotype); - amount = intvalue(t_argv[2]); - if(amount < 0) amount = 0; - if (iammo) iammo->Amount = amount; - else players[playernum].mo->GiveAmmo(ammotype, amount); - } - t_return.type = svt_int; - AInventory * iammo = players[playernum].mo->FindInventory(ammotype); - if (iammo) t_return.value.i = iammo->Amount; - else t_return.value.i = 0; + t_return.value.i = ScriptUtil::Exec("PlayerAmmo", ScriptUtil::Pointer, T_GetPlayerActor(t_argv[0]), ScriptUtil::Class, T_GetAmmo(t_argv[1]), + ScriptUtil::Int, t_argc >= 3 ? intvalue(t_argv[2]) : INT_MIN, ScriptUtil::End); } } @@ -2524,7 +2505,7 @@ void FParser::SF_MaxPlayerAmmo() if (CheckArgs(2)) { t_return.type = svt_int; - t_return.value.i = ScriptUtil::Exec("MaxPlayerAmmo", ScriptUtil::Pointer, T_GetPlayerActor(t_argv[0]), ScriptUtil::Class, T_ClassType(t_argv[1]), + t_return.value.i = ScriptUtil::Exec("MaxPlayerAmmo", ScriptUtil::Pointer, T_GetPlayerActor(t_argv[0]), ScriptUtil::Class, T_GetAmmo(t_argv[1]), ScriptUtil::Int, t_argc >= 3? intvalue(t_argv[2]) : INT_MIN, ScriptUtil::Int, t_argc >= 4 ? intvalue(t_argv[3]) : INT_MIN, ScriptUtil::End); } } diff --git a/src/p_mobj.cpp b/src/p_mobj.cpp index 6fdda1e4d..654e1f0ae 100644 --- a/src/p_mobj.cpp +++ b/src/p_mobj.cpp @@ -942,44 +942,6 @@ DEFINE_ACTION_FUNCTION(AActor, GiveInventoryType) ACTION_RETURN_OBJECT(self->GiveInventoryType(type)); } -//============================================================================ -// -// AActor :: GiveAmmo -// -// Returns true if the ammo was added, false if not. -// -//============================================================================ - -bool AActor::GiveAmmo (PClassActor *type, int amount) -{ - if (type != NULL) - { - if (!type->IsDescendantOf(RUNTIME_CLASS(AInventory))) return false; - - AInventory *item = static_cast(Spawn (type)); - if (item) - { - item->Amount = amount; - item->flags |= MF_DROPPED; - if (!item->CallTryPickup (this)) - { - item->Destroy (); - return false; - } - return true; - } - } - return false; -} - -DEFINE_ACTION_FUNCTION(AActor, GiveAmmo) -{ - PARAM_SELF_PROLOGUE(AActor); - PARAM_CLASS(type, AInventory); - PARAM_INT(amount); - ACTION_RETURN_BOOL(self->GiveAmmo(type, amount)); -} - //============================================================================ // // AActor :: ClearInventory diff --git a/wadsrc/static/zscript/actor.txt b/wadsrc/static/zscript/actor.txt index d782a093d..64d5b7937 100644 --- a/wadsrc/static/zscript/actor.txt +++ b/wadsrc/static/zscript/actor.txt @@ -746,7 +746,6 @@ class Actor : Thinker native native clearscope Inventory FindInventory(class itemtype, bool subclass = false) const; native Inventory GiveInventoryType(class itemtype); native void ObtainInventory(Actor other); - native bool GiveAmmo (Class type, int amount); native bool UsePuzzleItem(int PuzzleItemType); native float AccuracyFactor(); diff --git a/wadsrc/static/zscript/actor_inventory.txt b/wadsrc/static/zscript/actor_inventory.txt index c0b5b52c8..9641cf1f3 100644 --- a/wadsrc/static/zscript/actor_inventory.txt +++ b/wadsrc/static/zscript/actor_inventory.txt @@ -283,6 +283,35 @@ extend class Actor } + //============================================================================ + // + // AActor :: GiveAmmo + // + // Returns true if the ammo was added, false if not. + // + //============================================================================ + + bool GiveAmmo (Class type, int amount) + { + if (type != NULL && type is 'Ammo') + { + let item = Inventory(Spawn (type)); + if (item) + { + item.Amount = amount; + item.bDropped = true; + if (!item.CallTryPickup (self)) + { + item.Destroy (); + return false; + } + return true; + } + } + return false; + } + + //=========================================================================== // // DoGiveInventory diff --git a/wadsrc/static/zscript/scriptutil/scriptutil.txt b/wadsrc/static/zscript/scriptutil/scriptutil.txt index 0f22e8423..43393b049 100644 --- a/wadsrc/static/zscript/scriptutil/scriptutil.txt +++ b/wadsrc/static/zscript/scriptutil/scriptutil.txt @@ -229,5 +229,28 @@ class ScriptUtil play else return GetDefaultByType(ammotype).MaxAmount; } + //========================================================================== + // + // + // + //========================================================================== + + int PlayerAmmo(Actor activator, class type, int newamount = int.min) + { + if (activator == null) return 0; + let ammotype = (class)(type); + if (ammotype == null) return 0; + + if (newamount != int.min) + { + let iammo = activator.FindInventory(ammotype); + newamount = max(newamount, 0); + if (iammo) iammo.Amount = newamount; + else activator.GiveAmmo(ammotype, newamount); + } + let iammo = activator.FindInventory(ammotype); + if (iammo) return iammo.Amount; + else return 0; + } }