// ------------------------------------------------------------
//   Rifle
// ------------------------------------------------------------

ACTOR ZM66GrenadeRifle : HDWeapon
{
	Weapon.SelectionOrder 200
	weapon.ammotype1 ZM66RifleRounds
	weapon.ammotype2 ZM66RifleMags//Clip
	weapon.kickback 20
	Inventory.PickupSound "misc/w_pkup"
	Inventory.PickupMessage "You got the GL-mounted rifle!"
	radius 20
	height 16
	Decal "BulletScratch"
	Obituary "%o was shot up by %k."
	States 
	{
	Ready:
		RIFG A 0 A_JumpIfInventory ("ZM66GrenadeReloading", 1, "AltReload")
		RIFG A 1 offset(0,0) A_WeaponReady(48)
		RIFG A 0 A_GiveInventory("NotShot",1)
		RIFG A 0 A_JumpIfInventory("ZM66RifleMags",0,2)
		RIFG A 0 A_JumpIfInventory("NotShot",350,"FillMag")
		RIFG A 0 A_Jump(112,1)
		loop
		RIFG A 0 A_TakeInventory ("FirstBullet", 1)
		loop
	FillMag:
		RIFG A 0 A_TakeInventory("NotShot",9999)
		RIFG A 0 A_JumpIfInventory("Clip",30,1)
		goto Ready
		RIFG A 0 A_GiveInventory("ZM66RifleMags",1)
		RIFG A 0 A_TakeInventory("Clip",30)
		goto Ready
	Select:
		RIFL A 0 A_TakeInventory ("ZM66Rifle", 999)
		RIFG B 0 A_TakeInventory ("FirstBullet", 999)
		TNT1 A 0 A_JumpIfInventory("ZM66RocketUnderslung",1,"Select2")
		TNT1 A 0 A_JumpIfInventory("RocketAmmo",1, 1)
		goto Select2
		TNT1 A 0 A_GiveInventory("ZM66GrenadeReloader",1)
	Select2:
		RIFG B 0 A_TakeInventory ("Reloading", 999)
		RIFG B 0 A_TakeInventory ("ZM66GrenadeReloading", 999)
		RIFG B 1 A_Raise
		RIFG B 0 A_Raise
		loop
	Deselect:
		RIFG B 0 A_TakeInventory ("Reloading", 999)
		RIFG B 0 A_TakeInventory ("ZM66GrenadeReloading", 999)
		RIFG B 0 A_TakeInventory ("ZM66GrenadeReloader", 999)
		RIFG B 0 A_TakeInventory ("FirstBullet", 1)
		RIFG B 1 A_Lower
		RIFG B 0 A_Lower
		loop
	Flash:
		RIFF A 0 A_ZoomFactor(0.99,ZOOM_INSTANT|ZOOM_NOSCALETURNING)
		RIFF A 0 A_GiveInventory ("FirstBullet", 1)
		RIFF A 2 BRIGHT A_Light1

		TNT1 A 0 A_JumpIfInventory("OddBullet",1,4)
		TNT1 A 0 A_PlaySoundEx("weapons/rifle","SoundSlot5")
		TNT1 A 0 A_GiveInventory("OddBullet",1)
		TNT1 A 0 A_Jump(256,3)
		TNT1 A 0 A_PlaySoundEx("weapons/rifle","SoundSlot6")
		TNT1 A 0 A_TakeInventory("OddBullet",999)

		TNT1 A 0 A_SpawnItem ("DistantRifle", 1, 1, 0, 0)
		RIFF A 0 A_AlertMonsters
		RIFF A 0 A_TakeInventory ("ZM66RifleRounds", 1)
		RIFF A 0 A_TakeInventory ("NoChamberedRifle", 1)
		RIFF A 0 A_Light0
		RIFF A 0 A_ZoomFactor(1.0,ZOOM_INSTANT|ZOOM_NOSCALETURNING)
		Stop
	Spawn:
		TNT1 A 0
		TNT1 A 0 A_SpawnItemEx ("GrenadeRiflePickupDropped", 0,0,0,velx,vely,velz,0,32,0)
		stop
		RIFL B -1
		stop
	Fire:
		RIFG A 1 A_JumpIfInventory("ZM66FullAuto",1,1)
		goto HoldShoot
		RIFG A 3
		goto HoldShoot
	Hold:
		RIFG A 0 A_JumpIfInventory("ZM66FullAuto",1,"HoldShoot")
		RIFG A 1
		RIFG A 1 A_Refire
		goto Ready
	HoldShoot:
		RIFG B 4 A_JumpIfInventory("ZM66RifleRounds", 1, 3)
		RIFG B 4 A_Refire
		RIFG A 0 A_Jump (256, "Ready")

		RIFG A 0 A_JumpIfInventory("FirstBullet", 12, "Shoot4")
		RIFG A 0 A_JumpIfInventory("FirstBullet", 6, "Shoot3")
		RIFG A 0 A_JumpIfInventory("FirstBullet", 2, "Shoot2")
		goto Shoot1

	Shoot1:
		RIFG A 0 A_GunFlash
		RIFG A 2 A_FireBullets (0, 0, -1, 16, "BulletPuffBig", 0)
		goto Recoil
	Shoot2:
		RIFG A 0 A_GunFlash
		RIFG A 2 A_FireBullets (0.3, 0.3, -1, 12, "BulletPuffBig", 0)
		goto Recoil
	Shoot3:
		RIFG A 0 A_GunFlash
		RIFG A 2 A_FireBullets (0.6, 0.6, -1, 11, "BulletPuffBig", 0)
		goto Recoil
	Shoot4:
		RIFG A 0 A_GunFlash
		RIFG A 2 A_FireBullets (1.2, 1.2, -1, 10, "BulletPuffBig", 0)
		goto Recoil
	Recoil:
		RIFG B 1 ACS_ExecuteAlways (553, 0, random(16,32), random(8, 16), 0)
		RIFG A 2 A_Refire
		goto Ready
	Reload:
		RIFG A 0 A_GiveInventory("Reloading",1)
		RIFG A 0 A_JumpIfInventory ("ZM66RifleRounds", 0, "AltReloadFromReload")
		RIFG A 0 A_Jump (256, "Reload1")

	Reload1:
		RIFG A 1 A_TakeInventory ("FirstBullet", 4)
		RIFG A 0 A_JumpIfInventory("ZM66RifleMags", 1, 3)
		RIFG A 0 A_JumpIfInventory("Clip", 1, 2)
		RIFG A 0 A_Jump (256, "AltReloadFromReload")

		RIFG A 0 A_JumpIfInventory ("ZM66RifleRounds", 1, 2)
		RIFG A 0 A_GiveInventory ("NoChamberedRifle", 1)

		RIFG A 1 offset (0,34)
		RIFG A 1 offset (0,36)
		RIFG A 1 offset (0,38)
		RIFG B 12 offset (0,40)
		RIFG B 4 offset (0,40) A_PlayWeaponSound ("weapons/rifleclick2")
		RIFG B 24 offset (0,40) A_PlayWeaponSound ("weapons/rifleload")

		RIFG B 32 offset (0,42) A_PlayWeaponSound ("weapons/pocket")
		TNT1 A 0 A_JumpIfInventory("ZM66RifleMags",1,"Reload2")
		goto Reload2Shitty
	Reload2:
//		RIFG B 0 ACS_ExecuteAlways (554, 0, 0, 30)
		TNT1 A 0 A_TakeInventory("ShittyReloadDummy",999)
			Reload2EmptyFirst:
			TNT1 A 0 A_JumpIfInventory("ZM66RifleRounds",2,1)
			goto Reload2EmptyFirstEnd
			TNT1 A 0 A_TakeInventory("ZM66RifleRounds",1)
			TNT1 A 0 A_GiveInventory("ShittyReloadDummy",1)
			loop
			Reload2EmptyFirstEnd:
		TNT1 A 0 A_TakeInventory("ZM66RifleMags",1)
		TNT1 A 0 A_GiveInventory("ZM66RifleRounds",30)
		goto Reload2ReturnToPool
	Reload2Shitty:
		TNT1 A 0 A_TakeInventory("ShittyReloadDummy",999)
			Reload2EmptyFirst2:
			TNT1 A 0 A_JumpIfInventory("ZM66RifleRounds",2,1)
			goto Reload2EmptyFirstEnd2
			TNT1 A 0 A_TakeInventory("ZM66RifleRounds",1)
			TNT1 A 0 A_GiveInventory("ShittyReloadDummy",1)
			loop
			Reload2EmptyFirstEnd2:
		RIFG B 1 ACS_ExecuteAlways (554, 0, 0, random(1,29))
			Reload2ReturnToPool:
			TNT1 A 0 A_JumpIfInventory("ShittyReloadDummy",1,1)
			goto Reload3
			TNT1 A 0 A_TakeInventory("ShittyReloadDummy",1)
			TNT1 A 0 A_GiveInventory("Clip",1)
			loop
	Reload3:
		RIFG B 8 offset (0,40) A_PlayWeaponSound ("weapons/rifleload")
		RIFG B 12 offset (0,38) A_PlayWeaponSound ("weapons/rifleclick2")
		RIFG B 0 A_JumpIfInventory ("NoChamberedRifle", 1, 2)
		RIFG B 0 A_Jump (256, 2)
		RIFG A 2 offset (0,39) A_PlaySound ("weapons/rifleclick")
		RIFG A 5 offset (0,38) A_TakeInventory ("NoChamberedRifle", 999)
		RIFG A 1 offset (0,36) A_TakeInventory ("Reloading", 999)
		RIFG A 1 offset (0,34)
		TNT1 A 0 A_Jump(256, "Ready")
	AltFire:
		RIFG B 4 A_JumpIfInventory ("ZM66RocketUnderslung", 1, "AltShoot")
		RIFG B 0 A_Refire
		goto Ready
	AltShoot:
		RIFG A 3
		RIFG B 0 A_FireCustomMissile ("DelayedRocquette", 0,0,0,0,0)
		RIFG B 0 A_TakeInventory ("ZM66RocketUnderslung",1)
		RIFG B 0 A_JumpIfInventory("RocketAmmo",1,2)
		RIFG B 0 A_Jump(256,2)
		RIFG B 0 A_GiveInventory("ZM66GrenadeReloader",1)
		RIFG B 0 A_PlayWeaponSound ("weapons/grenadeshot")
		RIFG B 2 A_ZoomFactor(0.99,ZOOM_INSTANT|ZOOM_NOSCALETURNING)
		RIFG B 2 A_ZoomFactor(1.0,ZOOM_INSTANT|ZOOM_NOSCALETURNING)
		RIFG B 3 ACS_Execute (553, 0, 48, 16, 0)
		RIFG B 3 A_Refire
		TNT1 A 0 A_Jump(256, "Ready")
	AltHold:
		RIFG B 1
		RIFG B 0 A_Refire
		goto Ready
	AltReload:
	Zoom:
		RIFG B 2 A_JumpIfInventory ("ZM66RocketUnderslung", 1, 2)
		RIFG B 0 A_JumpIfInventory ("RocketAmmo", 1, 3)
		RIFG A 1 A_TakeInventory ("Reloading", 999)
		TNT1 A 0 A_Jump(256, "Ready")
		RIFG B 0 A_TakeInventory("ZM66GrenadeReloading",999)
		RIFG B 0 A_TakeInventory("ZM66GrenadeReloader",999)
		RIFG B 0 A_PlayWeaponSound ("weapons/grenopen")
		RIFG B 1 offset (0,34)
		RIFG B 1 offset (0,36)
		RIFG B 1 offset (0,38)
		RIFG B 0 ACS_ExecuteAlways(554, 0, 7, 1)
		RIFG B 0 A_TakeInventory ("ZM66GrenadeReloader", 999)
		RIFG B 28 offset (0,40) A_PlayWeaponSound ("weapons/pocket")
		RIFG B 18 offset (0,40) A_PlayWeaponSound ("weapons/rockreload")
		RIFG B 4 offset (0,38) A_PlayWeaponSound ("weapons/grenopen")
		RIFG B 1 offset (0,36) A_TakeInventory ("Reloading", 999)
		RIFG A 1 offset (0,34)
		TNT1 A 0 A_Jump(256, "Ready")
	AltReloadFromReload:
		RIFG A 0 A_JumpIfInventory ("ZM66RocketUnderslung", 1, 1)
		goto AltReload
		RIFG A 0 A_TakeInventory ("Reloading", 999)
		RIFG B 0 A_TakeInventory ("ZM66GrenadeReloader", 999)
		RIFG B 4 A_TakeInventory ("ZM66GrenadeReloading", 999)
		TNT1 A 0 A_Jump(256, "Ready")
	}
}
ACTOR OddBullet : InventoryFlag {}
ACTOR FirstBullet : InventoryFlag
{
	Inventory.MaxAmount 50
}
ACTOR ZM66GrenadeReloader : ActionItem
{
	+invbar
	inventory.icon ROQPB0
	States
	{
	Use:
		TNT1 A 0 A_GiveInventory("ZM66GrenadeReloading",1)
		stop
	}
}
ACTOR ZM66GrenadeReloading : InventoryFlag {}
ACTOR ZM66FullAuto : InventoryFlag {}
ACTOR ZM66RifleRounds : Ammo
{
	+inventory.ignoreskill
	+inventory.undroppable
	inventory.maxamount 31
	ammo.backpackmaxamount 31
	ammo.backpackamount 0
}
ACTOR ZM66RocketUnderslung : Ammo
{
	+inventory.ignoreskill
	+inventory.undroppable
	inventory.maxamount 1
	ammo.backpackmaxamount 1
	ammo.backpackamount 0
}
ACTOR ZM66RifleMags : Ammo
{
	+inventory.ignoreskill
	+inventory.undroppable
	inventory.maxamount 6
	ammo.backpackmaxamount 12
	ammo.backpackamount 1
}

ACTOR ZM66Rifle : ZM66GrenadeRifle
{
	Weapon.SelectionOrder 201
	+weapon.cheatnotweapon
	Decal "BulletScratch"
	Inventory.PickupMessage "You got the rifle!"
	states
	{
	Select:
		RIFG B 0 A_TakeInventory ("FirstBullet", 999)
		RIFG B 0 A_TakeInventory ("Reloading", 999)
		RIFG B 0 A_TakeInventory ("ZM66GrenadeReloader", 999)
		RIFG B 1 A_Raise
		RIFG BB 0 A_Raise
		loop
	DeselectCool:
		RIFG B 1
		RIFG B 1 A_TakeInventory ("FirstBullet", 1)
	Deselect:
		RIFG B 0 A_TakeInventory ("Reloading", 999)
		RIFG B 1 A_Lower
		RIFG B 0 A_JumpIfInventory("FirstBullet",14,"DeselectCool")
		RIFG B 0 A_Lower
		loop
	Ready:
		RIFG A 0 A_JumpIfInventory ("Reloading", 1, "Reload")
		RIFG A 1 offset(0,0) A_WeaponReady(16)
		RIFG A 0 A_GiveInventory("NotShot",1)
		RIFG A 0 A_JumpIfInventory("ZM66RifleMags",0,2)
		RIFG A 0 A_JumpIfInventory("NotShot",350,"FillMag")
		RIFG A 0 A_Jump(128,1)
		loop
		RIFG A 0 A_TakeInventory ("FirstBullet", 1)
		loop
	FillMag:
		RIFG A 0 A_TakeInventory("NotShot",9999)
		RIFG A 0 A_JumpIfInventory("Clip",30,1)
		goto Ready
		RIFG A 0 A_GiveInventory("ZM66RifleMags",1)
		RIFG A 0 A_TakeInventory("Clip",30)
		goto Ready
	AltFire:
	AltHold:
	AltReload:
	AltReloadFromReload:
		RIFG A 1
		RIFG A 0 A_Refire
		RIFG A 0 A_TakeInventory("Reloading",999)
		TNT1 A 0 A_Jump(256, "Ready")
	Reload:
		RIFG A 0 A_JumpIfInventory ("ZM66RifleRounds", 0, 1)
		goto Reload1
		RIFG A 0 A_TakeInventory ("Reloading", 999)
		TNT1 A 0 A_Jump(256, "Ready")
	Spawn:
		TNT1 A 0
		TNT1 A 0 A_SpawnItemEx ("RiflePickupDropped", 0,0,0,velx,vely,velz,0,32,0)
		stop
		RIFL A -1
		stop
	}
}

actor RiflePickup : HDWeaponPickup
{
	scale 0.65
	inventory.pickupmessage "You got the rifle!"
	inventory.pickupsound "misc/w_pkup"
	states
	{
	spawn:
		RIFL A -1
		stop
	Pickup:
		TNT1 A 0 A_JumpIfInventory ("CanPickUpItems", 1, 1)
		fail
		RIFL A 0 A_JumpIfInventory ("ZM66GrenadeRifle", 1, 2)
		RIFL A 0 A_JumpIfInventory ("ZM66Rifle", 1, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("ZM66RifleMags", 0, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("Clip", 0, "FullAmmo")
		goto Pickup0
	FullAmmo:
		RIFL A 0
		fail
	Pickup0:
		RIFL A 0 A_GiveInventory ("RiflePickupRounds", (28+random(0,1)*2+random(0,1)))
	Pickup0a:
		RIFL A 0 A_JumpIfInventory ("ZM66GrenadeRifle", 1, 3)
		RIFL A 0 A_JumpIfInventory ("ZM66Rifle", 1, 2)
		RIFL A 0 A_GiveInventory ("ZM66Rifle", 1)
		goto Pickup2
		//if the mag is full, add a full mag
		RIFL A 0 A_JumpIfInventory ("ZM66RifleMags", 0, "Pickup1")
		RIFL A 0 A_JumpIfInventory ("RiflePickupRounds", 30, "PickupMagOnly")
		goto Pickup1
	PickupMagOnly:
		RIFL A 0 A_GiveInventory("ZM66RifleMags",1)
		stop
	Pickup1:
		RIFL A 0 A_GiveInventory ("Clip", 1)
		RIFL A 0 A_TakeInventory ("RiflePickupRounds", 1)
		RIFL A 0 A_JumpIfInventory ("RiflePickupRounds", 1, 1)
		stop
		RIFL A 0
		loop
	Pickup2:
		RIFL A 0 A_GiveInventory ("ZM66RifleRounds", 1)
		RIFL A 0 A_TakeInventory ("RiflePickupRounds", 1)
		RIFL A 0 A_JumpIfInventory ("RiflePickupRounds", 1, 1)
		stop
		RIFL A 0
		loop
	}
}
actor RiflePickupRounds : Ammo
{
	+inventory.undroppable
	inventory.maxamount 31
	ammo.backpackmaxamount 31
	ammo.backpackamount 0
}

actor GrenadeRiflePickup : RiflePickup
{
	inventory.pickupmessage "You got the GL-mounted rifle!"
	states
	{
	spawn:
		RIFL B -1
		stop
	Pickup:
		TNT1 A 0 A_JumpIfInventory ("CanPickUpItems", 1, 1)
		fail
		RIFL A 0 A_JumpIfInventory ("ZM66GrenadeRifle", 1, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("Clip", 0, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("RocketAmmo", 0, 1)
		goto Pickup0
	FullAmmo:
		TNT1 A 0
		fail
	Pickup0:
		TNT1 A 0 A_TakeInventory("RiflePickupRounds",999)
		RIFL A 0 A_GiveInventory("RiflePickupRounds", (28+random(0,1)*2+random(0,1)))
	Pickup0a:
		RIFL A 0 A_JumpIfInventory ("ZM66GrenadeRifle", 1, "Pickup1")
		RIFL A 0 A_GiveInventory ("ZM66GrenadeRifle", 1)
		RIFL A 0 A_GiveInventory ("ZM66RocketUnderslung", 1)
		goto Pickup2
		//if the mag is full, add a full mag
		RIFL A 0 A_JumpIfInventory ("ZM66RifleMags", 0, "Pickup1")
		RIFL A 0 A_JumpIfInventory ("RiflePickupRounds", 30, "PickupMagOnly")
		goto Pickup1
	PickupMagOnly:
		RIFL A 0 A_GiveInventory("ZM66RifleMags",1)
		stop
	Pickup1:
		RIFL A 0 A_GiveInventory("ZM66RifleMags",1)
		RIFL A 0 A_GiveInventory ("Clip", 1)
		RIFL A 0 A_TakeInventory ("RiflePickupRounds", 1)
		RIFL A 0 A_JumpIfInventory ("RiflePickupRounds", 1, 2)
		RIFL A 0 A_GiveInventory ("RocketAmmo", 1)
		stop
		RIFL A 0
		loop
	Pickup2:
		RIFL A 0 A_GiveInventory ("ZM66RifleRounds", 1)
		RIFL A 0 A_TakeInventory ("RiflePickupRounds", 1)
		RIFL A 0 A_JumpIfInventory ("RiflePickupRounds", 1, 1)
		stop
		RIFL A 0
		loop
	}
}

actor RiflePickupDropped : RiflePickup //replaces ZM66Rifle
{
	states
	{
	Pickup0:
		TNT1 A 0 A_TakeInventory("RiflePickupRounds",999)
		RIFL A 0 A_GiveInventory ("RiflePickupRounds", random(1,31))
		goto Pickup0a
	Pickup:
		TNT1 A 0 A_JumpIfInventory ("CanPickUpItems", 1, 1)
		fail
		RIFL A 0 A_JumpIfInventory ("ZM66GrenadeRifle", 1, 2)
		RIFL A 0 A_JumpIfInventory ("ZM66Rifle", 1, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("ZM66RifleMags", 0, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("Clip", 0, "FullAmmo")
		goto Pickup0
	FullAmmo:
		TNT1 A 0
		fail
	}
}
actor GrenadeRiflePickupDropped : GrenadeRiflePickup //replaces ZM66GrenadeRifle
{
	states
	{
	Pickup0:
		TNT1 A 0 A_TakeInventory("RiflePickupRounds",999)
		RIFL A 0 A_GiveInventory ("RiflePickupRounds", random(1,31))
		goto Pickup0a
	Pickup:
		TNT1 A 0 A_JumpIfInventory ("CanPickUpItems", 1, 1)
		fail
		RIFL A 0 A_JumpIfInventory ("ZM66GrenadeRifle", 1, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("ZM66RifleMags", 0, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("Clip", 0, 1)
		goto Pickup0
		RIFL A 0 A_JumpIfInventory ("RocketAmmo", 0, 1)
		goto Pickup0
	FullAmmo:
		TNT1 A 0
		fail
	}
}


ACTOR ZM66RifleSpawner
{
	health 1
	+invisible
	states
	{
	spawn:
		TNT1 A 0
		TNT1 A 0 A_Jump (256, 1, 3, 5, 2, 4, 6, 2, 4, 6)
		TNT1 A 0 A_SpawnItemEx ("Clip", random(-8,8), random(-8,8), 0, 0,0,0, 0,32)
		TNT1 A 0 A_SpawnItemEx ("RiflePickup", random(-4,4), random(-4,4), 0, 0,0,0, 0,32)
		stop
		TNT1 A 0 A_SpawnItemEx ("RocketAmmoPickup", random(-8,8), random(-8,8), 0, 0,0,0, 0,32)
		TNT1 A 0 A_SpawnItemEx ("GrenadeRiflePickup", random(-4,4), random(-4,4), 0, 0,0,0, 0,32)
		stop
		TNT1 A 0 A_SpawnItemEx ("RocketAmmoPickup", random(-8,8), random(-8,8), 0, 0,0,0, 0,32)
		TNT1 A 0 A_SpawnItemEx ("RocketAmmoPickup", random(-8,8), random(-8,8), 0, 0,0,0, 0,32)
		stop
	}
}


