Classes:WeaponPiece

From ZDoom Wiki
Jump to navigation Jump to search
Note: Wait! Stop! You do not need to copy this actor's code into your project! Here's why:
  1. This actor is already defined in GZDoom, there's no reason to define it again.
  2. In fact, trying to define an actor with the same name will cause an error (because it already exists).
  3. If you want to make your own version of this actor, use inheritance.
  4. Definitions for existing actors are put on the wiki for reference purpose only.
Weapon piece
Actor type Internal Game (ZDoom)
DoomEd Number None Class Name WeaponPiece


Classes: Inventory→WeaponPiece

 →ClericWeaponPiece
 →FighterWeaponPiece
 →MageWeaponPiece


WeaponPieces are items that can be used to create weapons with multiple parts that have to be assembled, like the big weapons in Hexen. The base class WeaponPiece is never used directly. It is always the base class for items defined in DECORATE.


Using in DECORATE

There are a few points to observe when using weapon pieces:

  • The piece's pickup sound and message are only used when the weapon is still incomplete.
  • If picking up a piece that completes the weapon, the weapon's pickup sound and message are being used.
  • Picking up a piece the player already owns gives some ammo. The amount is taken from the respective fields in the weapon definition.
  • Any weapon used with weapon pieces must use the Health property to define the number of pieces the weapon has.
  • This class is designed with the assumption that the whole weapon does not exist as a complete pickup item. It wouldn't do any harm to do it anyway but some messages would be obviously wrong.


WeaponPiece uses some of the basic Inventory properties to define their behavior as inventory items. They also define a few new properties:

  • WeaponPiece.Number number
The piece number. This can be a value between 1 and 32. To complete a weapon all available pieces as specified in the weapon must be picked up.
  • WeaponPiece.Weapon weapontype
The weapon this item helps to assemble.

Examples

class Ferkulator : Weapon
{
    Default
    {
        Health 3;
        ... // Other properties, flags and states for the weapon
    }
}

class FerkWeaponPiece1 : WeaponPiece
{
    Default
    {
        Inventory.PickupSound "misc/w_pkup"; // SNDINFO sound
        Inventory.PickupMessage "A Piece of the Ferkulator"; // This is an example. It's recommended to use LANGUAGE for player-facing strings.
        WeaponPiece.Weapon "Ferkulator";
        WeaponPiece.Number 1;
    }
    States
    {
    Spawn:
        FRK1 A -1;
        Stop;
    }
}

class FerkWeaponPiece2 : FerkWeaponPiece1
{
    Default
    {
        WeaponPiece.Number 2;
    }
    States
    {
    Spawn:
        FRK2 A -1;
        Stop;
    }
}

class FerkWeaponPiece3 : FerkWeaponPiece1
{
    Default
    {
        WeaponPiece.Number 3;
    }
    States
    {
    Spawn:
        FRK3 A -1;
        Stop;
    }
}

ZScript definition

Note: The ZScript definition below is for reference and may be different in the current version of UZDoom. The most up-to-date version of this code can be found on UZDoom GitHub.

}

class WeaponPiece : Inventory
{
	Default
	{
		+WEAPONSPAWN;
	}

	int PieceValue;
	Class<Weapon> WeaponClass;
	Weapon FullWeapon;

	property number: PieceValue;
	property weapon: WeaponClass;

	// Account for weapon replacers, but make sure it's still a Weapon
	clearscope class<Weapon> GetWeaponClass() const
	{
		class<Weapon> type = WeaponClass ? (class<Weapon>)(GetReplacement(WeaponClass)) : null;
		return type ? type : WeaponClass;
	}

	override bool TryPickupRestricted (in out Actor toucher)
	{
		// Wrong class, but try to pick up for ammo
		if (ShouldStay())
		{ // Can't pick up weapons for other classes in coop netplay
			return false;
		}

		class<Weapon> type = GetWeaponClass();
		if (!type)
			return false;

		let Defaults = GetDefaultByType(type);

		bool gaveSome = !!(toucher.GiveAmmo (Defaults.AmmoType1, Defaults.AmmoGive1) +
						   toucher.GiveAmmo (Defaults.AmmoType2, Defaults.AmmoGive2));

		if (gaveSome)
		{
			GoAwayAndDie ();
		}
		return gaveSome;
	}

	override bool TryPickup (in out Actor toucher)
	{
		class<Weapon> type = GetWeaponClass();
		if (!type)
			return false;

		Inventory item;
		WeaponHolder hold = NULL;
		bool shouldStay = ShouldStay ();
		int gaveAmmo;
		let Defaults = GetDefaultByType(type);

		FullWeapon = NULL;
		for(item=toucher.Inv; item; item=item.Inv)
		{
			hold = WeaponHolder(item);
			if (hold != null)
			{
				// Intentionally check against the unreplaced class
				if (hold.PieceWeapon == WeaponClass)
				{
					break;
				}
				hold = NULL;
			}
		}
		if (!hold)
		{
			hold = WeaponHolder(Spawn("WeaponHolder"));
			hold.BecomeItem();
			hold.AttachToOwner(toucher);
			hold.PieceMask = 0;
			hold.PieceWeapon = WeaponClass;
		}

		int pieceval = 1 << (PieceValue - 1);
		if (shouldStay)
		{
			// Cooperative net-game
			if (hold.PieceMask & pieceval)
			{
				// Already has the piece
				return false;
			}
			toucher.GiveAmmo (Defaults.AmmoType1, Defaults.AmmoGive1);
			toucher.GiveAmmo (Defaults.AmmoType2, Defaults.AmmoGive2);
		}
		else
		{ // Deathmatch or singleplayer game
			gaveAmmo = toucher.GiveAmmo (Defaults.AmmoType1, Defaults.AmmoGive1) +
						toucher.GiveAmmo (Defaults.AmmoType2, Defaults.AmmoGive2);

			if (hold.PieceMask & pieceval)
			{
				// Already has the piece, check if mana needed
				if (!gaveAmmo) return false;
				GoAwayAndDie();
				return true;
			}
		}

		hold.PieceMask |= pieceval;

		// Check if  weapon assembled
		if (hold.PieceMask == (1 << Defaults.health) - 1)
		{
			if (!toucher.FindInventory (type))
			{
				FullWeapon= Weapon(Spawn(type));

				// The weapon itself should not give more ammo to the player.
				FullWeapon.AmmoGive1 = 0;
				FullWeapon.AmmoGive2 = 0;
				FullWeapon.AttachToOwner(toucher);
				FullWeapon.AmmoGive1 = Defaults.AmmoGive1;
				FullWeapon.AmmoGive2 = Defaults.AmmoGive2;
			}
		}
		GoAwayAndDie();
		return true;
	}

	override bool ShouldStay ()
	{
		// We want a weapon piece to behave like a weapon, so follow the exact
		// same logic as weapons when deciding whether or not to stay.
		return (((multiplayer &&
			(!deathmatch && !alwaysapplydmflags)) || sv_weaponstay) && !bDropped);
	}

	override String PickupMessage ()
	{
		if (FullWeapon)
		{
			return FullWeapon.PickupMessage();
		}
		else
		{
			return Super.PickupMessage();
		}
	}

	override void PlayPickupSound (Actor toucher)
	{
		if (FullWeapon)
		{
			FullWeapon.PlayPickupSound(toucher);
		}
		else
		{
			Super.PlayPickupSound(toucher);
		}
	}
}

DECORATE definition

{{DecorateDefinitionNote}

ACTOR WeaponPiece : Inventory native
{
  +WEAPONSPAWN
}

See also