diff --git a/src/fragglescript/t_func.cpp b/src/fragglescript/t_func.cpp index ce46daeb3..886b735a0 100644 --- a/src/fragglescript/t_func.cpp +++ b/src/fragglescript/t_func.cpp @@ -2623,7 +2623,7 @@ void FParser::SF_MaxPlayerAmmo() } else if(t_argc >= 3) { - AAmmo * iammo = (AAmmo*)players[playernum].mo->FindInventory(ammotype); + auto iammo = players[playernum].mo->FindInventory(ammotype); amount = intvalue(t_argv[2]); if(amount < 0) amount = 0; if (!iammo) @@ -2644,7 +2644,7 @@ void FParser::SF_MaxPlayerAmmo() break; } } - iammo->BackpackMaxAmount=amount; + ((AAmmo*)iammo)->BackpackMaxAmount=amount; } t_return.type = svt_int; diff --git a/src/g_inventory/a_ammo.cpp b/src/g_inventory/a_ammo.cpp index 6ef2c64d4..ef8d8ab6e 100644 --- a/src/g_inventory/a_ammo.cpp +++ b/src/g_inventory/a_ammo.cpp @@ -84,117 +84,12 @@ PClassActor *AAmmo::GetParentAmmo () const return static_cast(type); } -//=========================================================================== -// -// AAmmo :: HandlePickup -// -//=========================================================================== -EXTERN_CVAR(Bool, sv_unlimited_pickup) - -bool AAmmo::HandlePickup (AInventory *item) +DEFINE_ACTION_FUNCTION(AAmmo, GetParentAmmo) { - if (GetClass() == item->GetClass() || - (item->IsKindOf (RUNTIME_CLASS(AAmmo)) && static_cast(item)->GetParentAmmo() == GetClass())) - { - if (Amount < MaxAmount || sv_unlimited_pickup) - { - int receiving = item->Amount; - - if (!(item->ItemFlags & IF_IGNORESKILL)) - { // extra ammo in baby mode and nightmare mode - receiving = int(receiving * G_SkillProperty(SKILLP_AmmoFactor)); - } - int oldamount = Amount; - - if (Amount > 0 && Amount + receiving < 0) - { - Amount = 0x7fffffff; - } - else - { - Amount += receiving; - } - if (Amount > MaxAmount && !sv_unlimited_pickup) - { - Amount = MaxAmount; - } - item->ItemFlags |= IF_PICKUPGOOD; - - // If the player previously had this ammo but ran out, possibly switch - // to a weapon that uses it, but only if the player doesn't already - // have a weapon pending. - - assert (Owner != NULL); - - if (oldamount == 0 && Owner != NULL && Owner->player != NULL) - { - barrier_cast(Owner)->CheckWeaponSwitch(GetClass()); - } - } - return true; - } - return false; + PARAM_SELF_PROLOGUE(AAmmo); + ACTION_RETURN_OBJECT(self->GetParentAmmo()); } -//=========================================================================== -// -// AAmmo :: CreateCopy -// -//=========================================================================== - -AInventory *AAmmo::CreateCopy (AActor *other) -{ - AInventory *copy; - int amount = Amount; - - // extra ammo in baby mode and nightmare mode - if (!(ItemFlags&IF_IGNORESKILL)) - { - amount = int(amount * G_SkillProperty(SKILLP_AmmoFactor)); - } - - if (GetClass()->ParentClass != RUNTIME_CLASS(AAmmo) && GetClass() != RUNTIME_CLASS(AAmmo)) - { - PClassActor *type = GetParentAmmo(); - if (!GoAway ()) - { - Destroy (); - } - - copy = static_cast(Spawn (type)); - copy->Amount = amount; - copy->BecomeItem (); - } - else - { - copy = Super::CreateCopy (other); - copy->Amount = amount; - } - if (copy->Amount > copy->MaxAmount) - { // Don't pick up more ammo than you're supposed to be able to carry. - copy->Amount = copy->MaxAmount; - } - return copy; -} - -//=========================================================================== -// -// AAmmo :: CreateTossable -// -//=========================================================================== - -AInventory *AAmmo::CreateTossable() -{ - AInventory *copy = Super::CreateTossable(); - if (copy != NULL) - { // Do not increase ammo by dropping it and picking it back up at - // certain skill levels. - copy->ItemFlags |= IF_IGNORESKILL; - } - return copy; -} - - // Backpack ----------------------------------------------------------------- IMPLEMENT_CLASS(ABackpackItem, false, false) @@ -214,6 +109,7 @@ void ABackpackItem::Serialize(FSerializer &arc) arc("bdepleted", bDepleted, def->bDepleted); } +EXTERN_CVAR(Bool, sv_unlimited_pickup) //=========================================================================== // // ABackpackItem :: CreateCopy diff --git a/src/g_inventory/a_ammo.h b/src/g_inventory/a_ammo.h index fbe96e4bc..f00bbe8d2 100644 --- a/src/g_inventory/a_ammo.h +++ b/src/g_inventory/a_ammo.h @@ -7,9 +7,6 @@ class AAmmo : public AInventory public: virtual void Serialize(FSerializer &arc) override; - virtual AInventory *CreateCopy (AActor *other) override; - virtual bool HandlePickup (AInventory *item) override; - virtual AInventory *CreateTossable () override; PClassActor *GetParentAmmo () const; int BackpackAmount, BackpackMaxAmount, DropAmount; diff --git a/src/g_inventory/a_pickups.cpp b/src/g_inventory/a_pickups.cpp index 1b26a2db4..38568bb5e 100644 --- a/src/g_inventory/a_pickups.cpp +++ b/src/g_inventory/a_pickups.cpp @@ -486,6 +486,12 @@ bool AInventory::GoAway () return true; } +DEFINE_ACTION_FUNCTION(AInventory, GoAway) +{ + PARAM_SELF_PROLOGUE(AInventory); + ACTION_RETURN_BOOL(self->GoAway()); +} + //=========================================================================== // // AInventory :: GoAwayAndDie diff --git a/src/p_user.cpp b/src/p_user.cpp index 98bc1ca16..ba187ea29 100644 --- a/src/p_user.cpp +++ b/src/p_user.cpp @@ -1048,6 +1048,13 @@ void APlayerPawn::CheckWeaponSwitch(PClassInventory *ammotype) } } +DEFINE_ACTION_FUNCTION(APlayerPawn, CheckWeaponSwitch) +{ + PARAM_SELF_PROLOGUE(APlayerPawn); + PARAM_OBJECT(ammotype, PClassInventory); + self->CheckWeaponSwitch(ammotype); + return 0; +} //=========================================================================== // // APlayerPawn :: GiveDeathmatchInventory diff --git a/wadsrc/static/zscript.txt b/wadsrc/static/zscript.txt index d18f42089..6352dd0b8 100644 --- a/wadsrc/static/zscript.txt +++ b/wadsrc/static/zscript.txt @@ -6,11 +6,13 @@ #include "zscript/actor.txt" #include "zscript/actor_checks.txt" -#include "zscript/shared/inventory.txt" -#include "zscript/shared/inv_misc.txt" -#include "zscript/shared/weapons.txt" -#include "zscript/shared/armor.txt" -#include "zscript/shared/powerups.txt" +#include "zscript/inventory/inventory.txt" +#include "zscript/inventory/inv_misc.txt" +#include "zscript/inventory/weapons.txt" +#include "zscript/inventory/armor.txt" +#include "zscript/inventory/ammo.txt" +#include "zscript/inventory/powerups.txt" + #include "zscript/shared/player.txt" #include "zscript/shared/morph.txt" #include "zscript/shared/botstuff.txt" diff --git a/wadsrc/static/zscript/inventory/ammo.txt b/wadsrc/static/zscript/inventory/ammo.txt new file mode 100644 index 000000000..0a36e0b35 --- /dev/null +++ b/wadsrc/static/zscript/inventory/ammo.txt @@ -0,0 +1,129 @@ + +class Ammo : Inventory native +{ + native int BackpackAmount; + native int BackpackMaxAmount; + + Default + { + +INVENTORY.KEEPDEPLETED + Inventory.PickupSound "misc/ammo_pkup"; + } + + native Class GetParentAmmo (); + + //=========================================================================== + // + // AAmmo :: HandlePickup + // + //=========================================================================== + + override bool HandlePickup (Inventory item) + { + let ammoitem = Ammo(item); + if (ammoitem != null && ammoitem.GetParentAmmo() == GetClass()) + { + if (Amount < MaxAmount || sv_unlimited_pickup) + { + int receiving = item.Amount; + + if (!item.bIgnoreSkill) + { // extra ammo in baby mode and nightmare mode + receiving = int(receiving * G_SkillPropertyFloat(SKILLP_AmmoFactor)); + } + int oldamount = Amount; + + if (Amount > 0 && Amount + receiving < 0) + { + Amount = 0x7fffffff; + } + else + { + Amount += receiving; + } + if (Amount > MaxAmount && !sv_unlimited_pickup) + { + Amount = MaxAmount; + } + item.bPickupGood = true; + + // If the player previously had this ammo but ran out, possibly switch + // to a weapon that uses it, but only if the player doesn't already + // have a weapon pending. + + if (oldamount == 0 && Owner != null && Owner.player != null) + { + PlayerPawn(Owner).CheckWeaponSwitch(GetClass()); + } + } + return true; + } + return false; + } + + //=========================================================================== + // + // AAmmo :: CreateCopy + // + //=========================================================================== + + override Inventory CreateCopy (Actor other) + { + Inventory copy; + int amount = Amount; + + // extra ammo in baby mode and nightmare mode + if (!bIgnoreSkill) + { + amount = int(amount * G_SkillPropertyFloat(SKILLP_AmmoFactor)); + } + + let type = GetParentAmmo(); + if (GetClass() == type) + { + if (!GoAway ()) + { + Destroy (); + } + + copy = Inventory(Spawn (type)); + copy.Amount = amount; + copy.BecomeItem (); + } + else + { + copy = Super.CreateCopy (other); + copy.Amount = amount; + } + if (copy.Amount > copy.MaxAmount) + { // Don't pick up more ammo than you're supposed to be able to carry. + copy.Amount = copy.MaxAmount; + } + return copy; + } + + //=========================================================================== + // + // AAmmo :: CreateTossable + // + //=========================================================================== + + override Inventory CreateTossable() + { + Inventory copy = Super.CreateTossable(); + if (copy != null) + { // Do not increase ammo by dropping it and picking it back up at + // certain skill levels. + copy.bIgnoreSkill = true; + } + return copy; + } + + +} + +class BackpackItem : Inventory native +{ + native bool bDepleted; +} + diff --git a/wadsrc/static/zscript/shared/armor.txt b/wadsrc/static/zscript/inventory/armor.txt similarity index 100% rename from wadsrc/static/zscript/shared/armor.txt rename to wadsrc/static/zscript/inventory/armor.txt diff --git a/wadsrc/static/zscript/shared/inv_misc.txt b/wadsrc/static/zscript/inventory/inv_misc.txt similarity index 100% rename from wadsrc/static/zscript/shared/inv_misc.txt rename to wadsrc/static/zscript/inventory/inv_misc.txt diff --git a/wadsrc/static/zscript/shared/inventory.txt b/wadsrc/static/zscript/inventory/inventory.txt similarity index 99% rename from wadsrc/static/zscript/shared/inventory.txt rename to wadsrc/static/zscript/inventory/inventory.txt index d33b5e112..b2fc6a236 100644 --- a/wadsrc/static/zscript/shared/inventory.txt +++ b/wadsrc/static/zscript/inventory/inventory.txt @@ -42,6 +42,7 @@ class Inventory : Actor native virtual bool GetNoTeleportFreeze() { return false; } virtual void ModifyDamage(int damage, Name damageType, out int newdamage, bool passive) {} + native bool GoAway(); native void GoAwayAndDie(); native void BecomeItem(); native void BecomePickup(); diff --git a/wadsrc/static/zscript/shared/powerups.txt b/wadsrc/static/zscript/inventory/powerups.txt similarity index 100% rename from wadsrc/static/zscript/shared/powerups.txt rename to wadsrc/static/zscript/inventory/powerups.txt diff --git a/wadsrc/static/zscript/shared/weapons.txt b/wadsrc/static/zscript/inventory/weapons.txt similarity index 93% rename from wadsrc/static/zscript/shared/weapons.txt rename to wadsrc/static/zscript/inventory/weapons.txt index c3ebf299d..f1d75d5dd 100644 --- a/wadsrc/static/zscript/shared/weapons.txt +++ b/wadsrc/static/zscript/inventory/weapons.txt @@ -125,20 +125,3 @@ class WeaponPiece : Inventory native } } -class Ammo : Inventory native -{ - native int BackpackAmount; - native int BackpackMaxAmount; - - Default - { - +INVENTORY.KEEPDEPLETED - Inventory.PickupSound "misc/ammo_pkup"; - } -} - -class BackpackItem : Inventory native -{ - native bool bDepleted; -} - diff --git a/wadsrc/static/zscript/shared/player.txt b/wadsrc/static/zscript/shared/player.txt index f9a576006..2d8564164 100644 --- a/wadsrc/static/zscript/shared/player.txt +++ b/wadsrc/static/zscript/shared/player.txt @@ -105,6 +105,7 @@ class PlayerPawn : Actor native native int GetMaxHealth(); native bool ResetAirSupply (bool playgasp = false); + native void CheckWeaponSwitch(class item); } class PlayerChunk : PlayerPawn native