// ------------------------------------------------------------
//   Chaingun
// ------------------------------------------------------------

/*
The way the chaingun used to work, you'd have to press altfire to turn it on to shoot properly, otherwise you'd fire really slow with a "hand crank" mechanism, so that you would have a choice between a hasty but inaccurate PDW or taking the time to set up a nasty killing machine. That was interesting in theory, awful in practice; however, I've decided to leave the code in there in case I wanted to revisit the idea.
*/


ACTOR Vulcanette : HDWeapon
{
	weapon.selectionorder 204
	Decal "BulletScratch"
	weapon.ammotype2 ZM66RifleMags//Clip
	weapon.ammotype ChaingunBox
	weapon.ammouse 1
	weapon.kickback 24
	Obituary "%o met the budda-budda-budda on the street, and %k killed %h."
	States
	{
	Select:
		CHGG A 0 A_TakeInventory ("FirstBullet", 999)
		CHGG A 0 A_TakeInventory ("Reloading", 999)
		CHGG A 1 A_Raise
		loop
	Deselect:
		CHGG A 0 A_JumpIfHealthLower (1, "DeselectForceOff")
		CHGG A 0 A_JumpIfInventory ("ChaingunOn", 1, "WindDown")
		CHGG A 0 A_TakeInventory ("Reloading", 999)
		CHGG A 0 A_TakeInventory ("FirstBullet", 999)
		CHGG A 1 A_Lower
		loop
	DeselectForceOff:
		CHGG A 0 A_TakeInventory ("ChaingunOn", 999)
		goto Deselect+1
	Ready:
		CHGG A 0 A_JumpIfInventory ("ChaingunOn", 1, "Ready_On")
		CHGG A 1 A_WeaponReady(16)
		goto ReadyEndOff
	ReadyEndOff:
		CHGG A 0 A_JumpIfInventory ("Reloading", 1, "Reload")
		goto ReadyEnd
	ReadyEnd:
		CHGG A 0 A_TakeInventory ("FirstBullet", 999)
		TNT1 A 0 A_TakeInventory("IsInterrupting",999)
		CHGG A 0 A_GiveInventory("NotShot",1)
		CHGG A 0 A_JumpIfInventory("ZM66RifleMags",0,"Ready")
		CHGG A 0 A_JumpIfInventory("NotShot",350,"FillMag")
		goto Ready
	Ready_On:
		CHGG AABB 1 A_WeaponReady(16)
		goto ReadyEnd_On
	ReadyEnd_On:
		CHGG A 0 A_JumpIfInventory ("Reloading", 1, "WinddowntoReload")
		goto ReadyEnd
	FillMag:
		CHGG A 0 A_TakeInventory("NotShot",9999)
		CHGG A 0 A_JumpIfInventory("Clip",30,1)
		goto Ready
		CHGG A 0 A_GiveInventory("ZM66RifleMags",1)
		CHGG A 0 A_TakeInventory("Clip",30)
		goto Ready
	Fire:
		CHGG B 0 A_JumpifInventory ("Reloading", 1,"Interrupt")
		CHGG B 0 A_JumpifInventory ("IsInterrupting", 1,"Interrupted")

		//trigger pull
		TNT1 A 0 A_JumpIfInventory("ChaingunOn",1,"FireOn")
		TNT1 A 0 A_PlaySound ("weapons/vulcanup")
		CHGG A 3
		CHGG B 2 A_GiveInventory("ChaingunOn",1)
		TNT1 A 0 A_AlertMonsters
	FireOn:
		CHGG B 2
		TNT1 A 0 A_Refire
		goto Ready
	Hold:
		CHGG B 0 A_JumpifInventory ("Reloading", 1,"Interrupt")
		CHGG B 0 A_JumpifInventory ("IsInterrupting", 1,"Interrupted")

		TNT1 A 0 A_JumpIfInventory("Reloading",1,"Reload3")
		TNT1 A 0 A_JumpIfInventory("ChaingunBox",1,1)
		goto Altfire
		CHGG A 0 A_JumpIfInventory ("ChaingunOn", 1, "Fire_Auto")
		goto Fire
	Fire_Auto:
		CHGG A 1 A_JumpIfInventory ("ChaingunBox", 1, 1)
		goto Winddown
		CHGG A 1 A_GunFlash

		CHGG A 0 A_JumpIfInventory ("FirstBullet", 18, 3)
		CHGG A 0 A_JumpIfInventory ("FirstBullet", 12, 3)
		CHGG A 0 A_JumpIfInventory ("FirstBullet", 6, 3)

		CHGG B 1 A_FireBullets (0.2, 0.3, -1, 12, "BulletPuffMedium", 0)
		goto PostShot

		CHGG B 1 A_FireBullets (0.3, 0.5, -1, 11, "BulletPuffMedium", 0)
		goto PostShot

		CHGG B 1 A_FireBullets (0.6, 0.8, -1, 11, "BulletPuffMedium", 0)
		goto PostShot
	PostShot:
		TNT1 A 0 A_TakeInventory ("ChaingunBox", 1)
		TNT1 A 0 A_GiveInventory ("FirstBullet", 1)
		TNT1 A 0 A_JumpIfInventory ("ChaingunOn", 1, "PostShotAuto")
		goto PostPostShot

	PostShotAuto:
		TNT1 A 0 A_JumpIfInventory ("FirstBullet", 7, 2)
		TNT1 A 0 A_Jump (116, "PostShotAuto2")
		TNT1 A 0 A_JumpIfInventory ("FirstBullet", 14, 1)
		goto PostShotAuto2
		TNT1 A 0 A_JumpIfInventory ("ChaingunBox",1,1)
		goto PostShotAuto2
		TNT1 A 0 A_GunFlash
		CHGG A 1 A_FireBullets (0.4, 0.6, -1, 11, "BulletPuffMedium", 0)
		TNT1 A 0 A_TakeInventory ("ChaingunBox", 1)
	PostShotAuto2:
		TNT1 A 0 A_Jump (48, "Fire_Auto")

	PostPostShot:
		CHGG AB 1 A_Refire
		CHGG ABB 1
		Goto Ready
	AltFire:
		TNT1 A 0 A_JumpIfInventory("ChaingunOn",1,"Winddown")
		TNT1 A 0 A_JumpIfInventory("ChaingunBox",1,"Fire")
	AltHold:
		TNT1 A 0 A_JumpIfInventory("ChaingunBox",1,1)
		goto Spin
		TNT1 A 0 A_JumpIfInventory("ChaingunOn",1,3)
		CHGG A 1
		CHGG A 0 A_Refire
		goto Ready
		CHGG AB 2
		CHGG A 0 A_Refire
		goto Ready
	Winddown:
		CHGG A 0 A_TakeInventory ("ChaingunOn", 999)
		CHGG A 0 A_JumpIfInventory ("ChaingunBox", 1, 2)
		CHGG A 0 A_TakeInventory ("FirstBullet", 999)
		CHGG A 0 A_GiveInventory ("FirstBullet", 1)
		CHGG A 0 A_PlaySound ("weapons/vulcandown")
		CHGG AB 1
		CHGG A 2 A_AlertMonsters
		CHGG B 3
		CHGG B 1 A_ReFire
		CHGG A 0 A_TakeInventory ("FirstBullet", 999)
		Goto Ready
	WinddowntoReload:
		CHGG A 0 A_JumpIfInventory ("Clip", 1, 2)
		CHGG A 0 A_JumpIfInventory ("ZM66RifleMags", 1, 1)
		goto Winddown
		CHGG A 0 A_JumpIfInventory ("ChaingunOn", 1, 1)
		goto Reload
		CHGG A 0 A_TakeInventory ("ChaingunOn", 999)
		CHGG A 0 A_TakeInventory ("FirstBullet", 999)
		CHGG A 0 A_GiveInventory ("FirstBullet", 1)
		CHGG A 0 A_PlaySound ("weapons/vulcandown")
		CHGG BAB 2
		CHGG AB 4 A_AlertMonsters
		CHGG AB 6
		CHGG AB 8
		Goto Reload
	Interrupted:
		CHGG AB 4
		CHGG A 0 A_Refire
		goto Ready
	Interrupt:
		TNT1 A 0 A_GiveInventory("IsInterrupting",1)
	Spin:
		//CHGG A 0 A_TakeInventory ("ChaingunOn", 999)
		CHGG A 1 A_PlaySound ("weapons/vulcbelt")
		CHGG B 1 A_TakeInventory ("Reloading", 999)
		CHGG AB 2
		CHGG AB 4
		CHGG A 0 A_Refire
		goto Ready
	Flash:
		TNT1 A 0 A_JumpIfInventory ("IsMoving", 2, 3)
		TNT1 A 0 A_Jump (256, 2)
		CHGG A 0 ACS_ExecuteAlways (553, 0, (random(0,1)*2-1)*random(6,34),(random(0,1)*2-1)*random(4,30))
		CHGF A 1 BRIGHT A_ZoomFactor(0.99,ZOOM_INSTANT|ZOOM_NOSCALETURNING)
		CHGF B 0 A_ZoomFactor(1.0,ZOOM_INSTANT|ZOOM_NOSCALETURNING)
		CHGF A 0 BRIGHT A_Light2
		CHGF B 0 ACS_ExecuteAlways (553, 0, random(4,12), random(1,6), 0)
		TNT1 B 0 A_SpawnItem ("DistantChaingun", 1, 1, 0, 0)

		TNT1 A 0 A_JumpIfInventory("ChaingunOddBullet",3,9)
		TNT1 A 0 A_JumpIfInventory("ChaingunOddBullet",2,6)
		TNT1 A 0 A_JumpIfInventory("ChaingunOddBullet",1,3)
		TNT1 A 0 A_PlaySoundEx("weapons/vulcanette","weapon")
		TNT1 A 0 A_GiveInventory("ChaingunOddBullet",1)
		goto FlashEnd
		TNT1 A 0 A_PlaySoundEx("weapons/vulcanette","soundslot7")
		TNT1 A 0 A_TakeInventory("ChaingunOddBullet",999)
		goto FlashEnd
		TNT1 A 0 A_PlaySoundEx("weapons/vulcanette","soundslot6")
		TNT1 A 0 A_GiveInventory("ChaingunOddBullet",1)
		goto FlashEnd
		TNT1 A 0 A_PlaySoundEx("weapons/vulcanette","soundslot5")
		TNT1 A 0 A_GiveInventory("ChaingunOddBullet",1)
	FlashEnd:
		TNT1 A 0 A_AlertMonsters
		CHGF B 1 BRIGHT A_Light1
		CHGF B 0 BRIGHT A_Light0
		Stop
	Reload:
		CHGG A 0 A_JumpIfInventory("ChaingunOn",1,"WinddowntoReload")
		CHGG A 0 A_JumpIfInventory ("ChaingunBox", 150, 2)
		CHGG A 0 A_Jump (256, 2)
		CHGG A 0 A_TakeInventory ("Reloading", 999)
		goto Ready

		CHGG A 4
		CHGG A 0 A_JumpIfInventory("ZM66RifleMags", 1, 5)
		CHGG A 0 A_JumpIfInventory("Clip", 1, 4)
		CHGG A 0 A_TakeInventory ("Reloading", 999)
		CHGG A 0 A_JumpIfInventory("ChaingunBox", 1, 1)
		goto Spin
		CHGG A 0
		goto Ready

		CHGG A 2 offset(0,36) A_TakeInventory("ChaingunCounter",999)
		CHGG A 2 offset(0,38) A_PlayWeaponSound ("weapons/rifleclick2")
		CHGG A 16 offset(0,40) A_PlayWeaponSound ("weapons/grenopen")
		CHGG A 3 offset(0,43)
		CHGG A 10 offset(0,42) A_PlaySound ("weapons/pocket")
		TNT1 A 0 A_JumpIfInventory("ZM66RifleMags",1,"Reload2")
		CHGG A 26 offset(0,40) A_PlaySound ("weapons/pocket")
		Goto Reload2
	Reload2:
		CHGG A 4 offset(0,40) A_JumpIfInventory ("ZM66RifleMags", 1, 3)
		CHGG A 4 offset(0,40) A_JumpIfInventory ("Clip", 1, 1)
		goto Reload3
		CHGG A 4 offset(0,42) A_PlaySound ("weapons/pocket")
		CHGG A 0 offset(0,40) A_JumpIfInventory ("ChaingunCounter", 5, "Reload3")
		CHGG A 0 offset(0,40) A_JumpIfInventory ("ChaingunBox", 150, "Reload3")
		CHGG A 4 offset(0,42)
		TNT1 A 0 A_JumpIfHealthLower(60,2)
		TNT1 A 0 A_Jump(256,2)
		CHGG A 6 offset(0,40) A_PlaySound ("weapons/pocket")
		CHGG A 12 offset(0,42) A_PlaySound ("weapons/pocket")
		CHGG A 8 offset(0,41) A_PlaySound ("weapons/rifleload")
		CHGG B 0 A_PlaySound ("weapons/rifleclick2")
//		CHGG B 4 offset(0,40) ACS_ExecuteAlways (554, 0, 2, 30, 0)

		TNT1 A 0 A_JumpIfInventory("ZM66RifleMags",1,"Reload2a")
		goto Reload2Shitty
	Reload2a:
		TNT1 A 0 A_TakeInventory("ShittyReloadDummy",999)
		TNT1 A 0 A_JumpIfInventory("ChaingunBox",121,"Reload2EmptyFirst")
		goto Reload2EmptyFirstEnd
			Reload2EmptyFirst:
			TNT1 A 0 A_JumpIfInventory("ChaingunBox",1,1)
			goto Reload2EmptyFirstEnd
			TNT1 A 0 A_TakeInventory("ChaingunBox",1)
			TNT1 A 0 A_GiveInventory("ShittyReloadDummy",1)
			TNT1 A 0 A_JumpIfInventory("ChaingunBox",121,"Reload2EmptyFirst")
			Reload2EmptyFirstEnd:
		TNT1 A 0 A_TakeInventory("ZM66RifleMags",1)
		TNT1 A 0 A_GiveInventory("ChaingunBox",30)
		goto Reload2ReturnToPool
	Reload2Shitty:
		TNT1 A 0 A_TakeInventory("ShittyReloadDummy",999)
		TNT1 A 0 A_JumpIfInventory("ChaingunBox",121,"Reload2EmptyFirst2")
		goto Reload2EmptyFirstEnd2
			Reload2EmptyFirst2:
			TNT1 A 0 A_JumpIfInventory("ChaingunBox",1,1)
			goto Reload2EmptyFirstEnd2
			TNT1 A 0 A_TakeInventory("ChaingunBox",1)
			TNT1 A 0 A_GiveInventory("ShittyReloadDummy",1)
			TNT1 A 0 A_JumpIfInventory("ChaingunBox",121,"Reload2EmptyFirst2")
			Reload2EmptyFirstEnd2:
		CHGG B 1 ACS_ExecuteAlways (554, 0, 2, random(1,29))
			Reload2ReturnToPool:
			TNT1 A 0 A_JumpIfInventory("ShittyReloadDummy",1,1)
			goto Reload2b
			TNT1 A 0 A_TakeInventory("ShittyReloadDummy",1)
			TNT1 A 0 A_GiveInventory("Clip",1)
			loop
	Reload2b:
		CHGG B 0 A_GiveInventory ("Reloading", 1)
		CHGG B 0 A_GiveInventory ("ChaingunCounter", 1)
		CHGG B 0 offset(0,40) A_JumpIfInventory ("ChaingunBox", 121, "Reload3")
		CHGG BB 2 offset(0,40) A_WeaponReady//(WRF_NOBOB)
		goto Reload2
	Reload3:
		CHGG A 2 offset(0,40) A_TakeInventory ("FirstBullet", 999)
		CHGG A 2 offset(0,38) A_TakeInventory ("Reloading", 999)
		CHGG B 2 offset(0,36) A_PlayWeaponSound ("weapons/vulcbelt")
		CHGG ABABAB 2 offset(0,34)
		CHGG AABB 2 offset(0,32)
		CHGG AABBAAABBB 2 offset(0,30) A_WeaponReady
		goto Ready
	Spawn:
		TNT1 A 0
		TNT1 A 0 A_SpawnItemEx ("ChaingunPickupDropped", 0,0,0,velx,vely,velz,0,32,0)
		stop
		MGUN A -1
		stop
	} 
}
actor ChaingunOn : InventoryFlag {}
actor ChaingunOddBullet : InventoryFlag
{
	inventory.maxamount 3
}
actor ChaingunBox : Ammo
{
	+inventory.ignoreskill
	+inventory.undroppable
	inventory.maxamount 150
	ammo.backpackmaxamount 150
	ammo.backpackamount 0
}

actor ChaingunPickup : HDWeaponPickup replaces Chaingun
{
	scale 0.7
	inventory.pickupmessage "You got the chaingun! Now it's coward killing time!"
	states
	{
	spawn:
		MGUN A -1
		stop
	Pickup:
		TNT1 A 0 A_JumpIfInventory ("CanPickUpItems", 1, 1)
		fail
		MGUN A 0 A_JumpIfInventory ("Vulcanette", 1, 1)
		goto Pickup0
		MGUN A 0 A_JumpIfInventory ("ZM66RifleMags", 0, 1)
		goto Pickup0
		MGUN A 0 A_JumpIfInventory ("Clip", 0, 1)
		goto Pickup0
		MGUN A 0
		fail
	Pickup0:
		MGUN A 0 A_GiveInventory ("ChaingunPickupRounds", random(1,5)*(28+random(0,1)*2))
	Pickup0a:
		MGUN A 0 A_JumpIfInventory ("Vulcanette", 1, 2)
		MGUN A 0 A_GiveInventory ("Vulcanette", 1)
		goto Pickup2
	Pickup1:
		MGUN A 0 A_JumpIfInventory ("ChaingunPickupRounds", 30, 1)
		goto Pickup1NoMag
		MGUN A 0 A_GiveInventory ("ZM66RifleMags", 1)
		MGUN A 0 A_TakeInventory ("ChaingunPickupRounds", 30)
		loop
	Pickup1NoMag:
		MGUN A 0 A_GiveInventory ("Clip", 1)
		MGUN A 0 A_TakeInventory ("ChaingunPickupRounds", 1)
		MGUN A 0 A_JumpIfInventory ("ChaingunPickupRounds", 1, 1)
		stop
		MGUN A 0
		loop
	Pickup2:
		MGUN A 0 A_GiveInventory ("ChaingunBox", 1)
		MGUN A 0 A_TakeInventory ("ChaingunPickupRounds", 1)
		MGUN A 0 A_JumpIfInventory ("ChaingunPickupRounds", 1, 1)
		stop
		MGUN A 0
		loop
	}
}
actor ChaingunPickupDropped : ChaingunPickup //replaces Vulcanette
{
	states
	{
	Pickup0:
		MGUN A 0 A_GiveInventory ("ChaingunPickupRounds", random(1,150))
		goto Pickup0a
	Pickup:
		TNT1 A 0 A_JumpIfInventory ("CanPickUpItems", 1, 1)
		fail
		MGUN A 0 A_JumpIfInventory ("Vulcanette", 1, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("ZM66RifleMags", 0, 1)
		goto Pickup0
		SHOT A 0 A_JumpIfInventory ("Clip", 0, 1)
		goto Pickup0
		MGUN A 0
		fail
	}
}
actor ChaingunPickupRounds : Ammo
{
	+inventory.undroppable
	inventory.maxamount 150
	ammo.backpackmaxamount 150
	ammo.backpackamount 0
}
actor ChaingunCounter : InventoryFlag
{
	inventory.maxamount 5
}

