ACTOR ClipBoxSmall : ClipBox replaces ClipBox
{
	States
	{
		Spawn:
			PISP A -1
			Stop
	}
}

//------------------------------------------------------------------------------

ACTOR RandomPistolPickup : PickupBase
{
	States
	{
		Spawn:
			TNT1 A 0
			TNT1 A 0 A_SetUserVar("user_isBDPickupItem", 1)	// mark thing as BorderDoom pickup item
			TNT1 A 0 A_SetUserVar("user_pickupType", ItemType_Pistol)	// set pickup type to weapon
			TNT1 A 0 ACS_NamedExecuteAlways("CreateRandomPistolData")
			TNT1 A 0 A_SetSpecial(ACS_ExecuteAlways,901,0,0,0,0)
			goto Waiting
		Waiting:
			PIST A -1
			stop
		// called by script. makes the pickup "useable" for a short amount of time so it can be picked up.
		IsUseable:
			PIST A 0 A_ChangeFlag("USESPECIAL", TRUE)
			PIST A 7
			PIST A 0 A_ChangeFlag("USESPECIAL", FALSE)
			goto Waiting
	}
}

ACTOR FixedPistolPickup : RandomPistolPickup
{
	States
	{
		Spawn:
			TNT1 A 0
			TNT1 A 0 A_SetUserVar("user_isBDPickupItem", 1)	// mark thing as BorderDoom pickup item
			TNT1 A 0 A_SetUserVar("user_pickupType", ItemType_Pistol)	// set pickup type to weapon
			TNT1 A 0 A_SetSpecial(ACS_ExecuteAlways,901,0,0,0,0)	// script for picking up the item
			PIST A -1
			stop
	}
}

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

ACTOR RealPistol : Pistol
{
	Weapon.AmmoGive 0
	Weapon.BobSpeed 2.0

	+INVENTORY.UNDROPPABLE	// should only be dropped by script
	+Weapon.CHEATNOTWEAPON	// don't give this weapon when using cheats
	+Weapon.Ammo_Optional
	+Weapon.NOAUTOFIRE	// prevent holding down fire button to attack

	Decal BulletChip	// so this weapon also creates decals when fired at walls
	Tag "Pistol"

	States
	{
		Ready:
			PISG A 1 A_WeaponReady(WRF_ALLOWRELOAD)
			TNT1 A 0 A_JumpIfNoAmmo("Reload")	// auto-reload if player has more ammo
			Loop
		Select:
			TNT1 A 0 ACS_NamedExecuteAlways("SetWeaponDamageMultiplier")
//			TNT1 A 0 ACS_NamedExecute("PrintEquippedWeaponInfo", 0, CallACS("GetWeaponIndex"))
			PISG A 1 A_Raise
			Loop
		Fire:
			TNT1 A 0 A_JumpIfNoAmmo("NoAmmo")
			goto DoFire
			NoAmmo:
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "Reload")
			TNT1 A 0 A_PlaySound("player/noammo", CHAN_AUTO)
			goto Ready
			DoFire:
			TNT1 A 0 A_FireBullets (CallACS("GetCurrentSpread")/65536.0, (CallACS("GetCurrentSpread")/65536.0)/2, CallACS("GetCurrentBulletcount"), CallACS("GetCurrentDamage"), "BulletPuff", FBF_USEAMMO|FBF_NORANDOM)
			TNT1 A 0 A_PlaySound("pistol/fire_med", CHAN_AUTO)
			TNT1 A 0 A_GunFlash
			TNT1 A 0 ACS_NamedExecuteAlways("WeaponRecoil", 0, CallACS("GetCurrentRecoil"), 66, 4)
			PISG B 1 Offset(0, 40)
			PISG B 1 Offset(0, 36)
			PISG C 4 Offset(0, 32) A_WeaponReady(WRF_NOSECONDARY | WRF_NOBOB | WRF_NOSWITCH)
			PISG B 5 A_WeaponReady(WRF_NOSECONDARY | WRF_NOBOB | WRF_NOSWITCH)
			Goto Ready
		Flash:
			PISF A 2 Bright A_Light1
			Goto LightDone

		ReloadWork:
			PISG A 1 Offset(0,40)
			PISG A 1 Offset(0,44)
			PISG A 1 Offset(0,48)
			PISG A 1 Offset(0,52)
			PISG A 1 Offset(0,56)
			PISG A 1 Offset(0,60)
			PISG A 1 Offset(0,64)
			PISG A 1 Offset(0,68)
			TNT1 A 0 A_PlaySound("pistol/reload_clipout", CHAN_WEAPON)
			PISG A 40	// wait for reload
			TNT1 A 0 A_PlaySound("pistol/reload_clipin", CHAN_WEAPON)
			PISG A 0 A_Jump(256, "ReloadLoop")	// A_Jump jumps are virtual, "goto" is not
			//Goto ReloadLoop
		ReloadFinish:
			PISG A 1 Offset(0,68)
			PISG A 1 Offset(0,64)
			PISG A 1 Offset(0,60)
			PISG A 1 Offset(0,56)
			PISG A 1 Offset(0,52)
			PISG A 1 Offset(0,48)
			PISG A 1 Offset(0,44)
			PISG A 1 Offset(0,40)
			PISG A 1 Offset(0,36)
			TNT1 A 0 A_PlaySound("pistol/reload_slideforward", CHAN_WEAPON)
			Goto Ready
	}
}


///////////////////////////////
// EQUIPPABLE PISTOLS
///////////////////////////////

ACTOR RealPistol0 : RealPistol
{
	Weapon.SlotNumber 1
	Weapon.AmmoType "LoadedAmmoSlot0"

	States
	{
		Reload:
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot0", CallACS("GetCurrentClipSize"), "Ready")	// ignore if fully loaded
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "ReloadWork")	// do real reload
			Goto Ready
		ReloadLoop:
			TNT1 A 0 A_TakeInventory("Clip", 1)
			TNT1 A 0 A_GiveInventory("LoadedAmmoSlot0", 1)
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot0", CallACS("GetCurrentClipSize"), "ReloadFinish")
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "ReloadLoop")
			Goto ReloadFinish
	}
}

ACTOR RealPistol1 : RealPistol
{
	Weapon.SlotNumber 2
	Weapon.AmmoType "LoadedAmmoSlot1"

	States
	{
		Reload:
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot1", CallACS("GetCurrentClipSize"), "Ready")	// ignore if fully loaded
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "ReloadWork")	// do real reload
			Goto Ready
		ReloadLoop:
			TNT1 A 0 A_TakeInventory("Clip", 1)
			TNT1 A 0 A_GiveInventory("LoadedAmmoSlot1", 1)
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot1", CallACS("GetCurrentClipSize"), "ReloadFinish")
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "ReloadLoop")
			Goto ReloadFinish
	}
}

ACTOR RealPistol2 : RealPistol
{
	Weapon.SlotNumber 3
	Weapon.AmmoType "LoadedAmmoSlot2"

	States
	{
		Reload:
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot2", CallACS("GetCurrentClipSize"), "Ready")	// ignore if fully loaded
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "ReloadWork")	// do real reload
			Goto Ready
		ReloadLoop:
			TNT1 A 0 A_TakeInventory("Clip", 1)
			TNT1 A 0 A_GiveInventory("LoadedAmmoSlot2", 1)
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot2", CallACS("GetCurrentClipSize"), "ReloadFinish")
			TNT1 A 0 A_JumpIfInventory("Clip", 1, "ReloadLoop")
			Goto ReloadFinish
	}
}
