[Request] Stackable backpacks in ZScript?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
StroggVorbis
Posts: 866
Joined: Wed Nov 08, 2017 4:23 pm
Graphics Processor: nVidia with Vulkan support
Location: Germany

[Request] Stackable backpacks in ZScript?

Post by StroggVorbis »

The wiki contains an example method on how to take advantage of SetAmmoCapacity to do this, but it consists of a combination of ACS & Decorate. Could someone show me how to do this in ZScript instead?

Here's the link to the article I mentioned for anyone curious:
https://zdoom.org/wiki/SetAmmoCapacity

Thx in advance.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: [Request] Stackable backpacks in ZScript?

Post by Apeirogon »

You mean backpack which "double" amount of max ammo capacity?

Anyway, here it is.

Code: Select all

version "2.4"

class packback : backpack
{

override bool HandlePickup (Inventory item)
{
	if(owner.FindInventory("backpack", true) == null) return super.HandlePickup(item);
	
	if(owner.FindInventory("backpack", true) != null)
	{//assuming that if player already have backpack in pocket it already have all necessary ammo
		for (let probe = Owner.Inv; probe != NULL; probe = probe.Inv)
		{
			if (probe.GetParentClass() == 'Ammo')
			{
				probe.MaxAmount += Ammo(probe).Default.BackpackMaxAmount;

				if (probe.Amount < probe.MaxAmount || sv_unlimited_pickup)
				{
					int amount = Ammo(probe).Default.BackpackAmount;
					// extra ammo in baby mode and nightmare mode
					if (!bIgnoreSkill)
					{
						amount = int(amount * G_SkillPropertyFloat(SKILLP_AmmoFactor));
					}
					probe.Amount += amount;
					if (probe.Amount > probe.MaxAmount && !sv_unlimited_pickup)
					{
						probe.Amount = probe.MaxAmount;
					}
				}
			}
		}
	}
	item.bPickupGood = true;
	return true;
}

}
User avatar
StroggVorbis
Posts: 866
Joined: Wed Nov 08, 2017 4:23 pm
Graphics Processor: nVidia with Vulkan support
Location: Germany

Re: [Request] Stackable backpacks in ZScript?

Post by StroggVorbis »

Stackable as in each additional backpack further increases max ammo by a specified configurable amount.

Also, thanks for the response, will test it out later :)
spectrefps
Posts: 26
Joined: Sat Jun 20, 2020 7:41 pm

Re: [Request] Stackable backpacks in ZScript?

Post by spectrefps »

Sorry for the necro, but I am also trying to create a stacking backpack. I used your code, and it seems to work *if* I spawn in a "packback" item first (then any subsequent 'normal' backpacks found will stack). However, it doesn't work if I collect a normal backpack first (even if I summon/give a "packback" afterwards, max ammo will never increase beyond the vanilla caps). After looking at the wiki, it looks like you are overriding the backpack base class correctly. Any idea how to override the default backpack's behavior (so you don't need to summon a "packback" first)?
spectrefps
Posts: 26
Joined: Sat Jun 20, 2020 7:41 pm

Re: [Request] Stackable backpacks in ZScript?

Post by spectrefps »

Hugh Honeyridge wrote:Sorry for the necro, but I am also trying to create a stacking backpack. I used your code, and it seems to work *if* I spawn in a "packback" item first (then any subsequent 'normal' backpacks found will stack). However, it doesn't work if I collect a normal backpack first (even if I summon/give a "packback" afterwards, max ammo will never increase beyond the vanilla caps). After looking at the wiki, it looks like you are overriding the backpack base class correctly. Any idea how to override the default backpack's behavior (so you don't need to summon a "packback" first)?
This previous post was mine before I created my account, as I felt inclined to do so considering how much I've been enjoying various doom mods from here. I'm not sure how to 'claim' that post, so it will forever be that randomly generated name I guess >_<. But I managed to use a zmapinfo script to replace backpacks with the "packback" item you created in your code snippet.

Code: (in zmapinfo, edited with notepad++)

Code: Select all

DoomEdNums
{
	   8 = packback
}

This replaces the item with DoomEdNum 8 (the default Backpack) with "packback" (the item that increases max ammo capacity for each additional backpack found). However, there seems to be a bug with it. Once found, it will indeed double the max ammo capacity. After that, grabbing *ANY* ammo pickup will then double the max ammo capacity. So every clip, shell, cellpack, etc, will each increase your max ammo capacity. I think it may be due to the "if (probe.GetParentClass() == 'Ammo')" if statement, since it seems to trigger the max capacity increase on every ammo item pickup after grabbing a backpack. Anyone have more insight on this?

Here's my current code (it also has a menu option to enable/disable it:

Code: Select all

Class packback : backpack
{

override bool HandlePickup (Inventory item)
{
   if(owner.FindInventory("backpack", true) == null) return super.HandlePickup(item);
   
   if(owner.FindInventory("backpack", true) != null && (GetCVar("ZD_StackingBackpacks")))
   {//assuming that if player already have backpack in pocket it already have all necessary ammo
      for (let probe = Owner.Inv; probe != NULL; probe = probe.Inv)
      {
         if (probe.GetParentClass() == 'Ammo')
         {
            probe.MaxAmount += Ammo(probe).Default.BackpackMaxAmount;

            if (probe.Amount < probe.MaxAmount || sv_unlimited_pickup)
            {
               int amount = Ammo(probe).Default.BackpackAmount;
               // extra ammo in baby mode and nightmare mode
               if (!bIgnoreSkill)
               {
                  amount = int(amount * G_SkillPropertyFloat(SKILLP_AmmoFactor));
               }
               probe.Amount += amount;
               if (probe.Amount > probe.MaxAmount && !sv_unlimited_pickup)
               {
                  probe.Amount = probe.MaxAmount;
               }
            }
         }
      }
   }
   item.bPickupGood = true;
   return true;
}

}
Any help with this would be appreciated, as I like the idea of backpacks from certain mods (like GunCaster) that add 10% to your max ammo capacity for each one after the first, but trying to implement it seems like a nightmare to far. XD
Blue Shadow
Posts: 4949
Joined: Sun Nov 14, 2010 12:59 am

Re: [Request] Stackable backpacks in ZScript?

Post by Blue Shadow »

spectrefps wrote:This previous post was mine before I created my account, as I felt inclined to do so considering how much I've been enjoying various doom mods from here. I'm not sure how to 'claim' that post, so it will forever be that randomly generated name I guess >_<.
You have ownership now.
Post Reply

Return to “Scripting”