// --------------------------------------------------------------------------
//
//	MP40
//
// --------------------------------------------------------------------------

ACTOR U_MP40 : BaseWeapon replaces MP40
{
	Weapon.AmmoUse1 0
	Weapon.AmmoUse2 0
	Weapon.AmmoGive1 20
	Weapon.AmmoGive2 0
	Weapon.AmmoType1 "9mmMagazine"
	Weapon.AmmoType2 "MP40Ammo"
	Weapon.SelectionOrder 5500
	Obituary "%o was shot down by %k's MP40."
    Inventory.PickupSound "CLIPIN"
	Inventory.Pickupmessage "You got the MP40!"
    Scale 0.4
	tag "MP40"
	States
	{
 	Spawn:
		MP40 J -1
		Stop

	Select:
		TNT1 A 0 A_JumpIfInventory("IsPlayingAsPurist", 1, "PuristGun")
		TNT1 A 0 A_JumpIf(ACS_ExecuteWithResult(1010,9)==0, "PuristGun")
		TNT1 A 0 A_Giveinventory("SubMachineGunSelected",1)
		TNT1 A 0 A_Giveinventory("GoSpecial",1)
		TNT1 A 0 A_Takeinventory("Unloading",1)
		TNT1 A 0 A_Takeinventory("Reloading",1)
		TNT1 A 0 A_TakeInventory("TossGrenade", 1)
		TNT1 A 0 A_Takeinventory("WeapSpecial",1)
		TNT1 A 0 A_Takeinventory("Taunting",1)
		TNT1 A 0 A_Takeinventory("IdleCounter",200)
		TNT1 A 3
		TNT1 A 0 A_Raise
		Wait

	Deselect:
		TNT1 A 0 A_PlaySound("weapons/changing")
		TNT1 A 0 A_Takeinventory("Zoomed",1)
		TNT1 A 0 A_ZoomFactor(1.0)
		TNT1 A 0 A_SetCrosshair(0)
		MP4Z GFEDCBA 1
		TNT1 A 0 A_Takeinventory("ADSmode",1)
		TNT1 A 0 A_Takeinventory("SubMachineGunSelected",1)
		TNT1 A 0 A_Lower
		Wait

	Ready:
		TNT1 A 0 A_JumpIfInventory("GoFatality", 1, "Steady")
        TNT1 A 0 A_PlaySound("CLIPIN")
	Ready1:
        MP4Z ABCDEFG 1
	Ready2:
		TNT1 A 0 A_JumpIfInventory("Zoomed",1,"ReadyZoom")
        TNT1 A 0 A_JumpIfInventory("Kicking",1,"DoKick")
        TNT1 A 0 A_JumpIfInventory("Taunting",1,"TauntL")
        TNT1 A 0 A_JumpIfInventory("Reloading",1,"Reload")
		TNT1 A 0 A_JumpIfInventory("Salute1",1,"Salute")
		TNT1 A 0 A_JumpIfInventory("Salute2",1,"Salute")
		TNT1 A 0 A_JumpIfInventory("Unloading",1,"Unload")
		TNT1 A 0 A_JumpIfInventory("TossGrenade",1,"TossGrenade")
		TNT1 A 0 A_JumpIfInventory("IdleCounter",200,"PlayerIsIdle")
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,2)
		MP40 A 1 A_WeaponReady
		Goto Ready2
		MP40 F 1 A_WeaponReady
		Goto Ready2

	ReadyZoom:
        TNT1 A 0 A_JumpIfInventory("Kicking",1,"ZoomOut")
        TNT1 A 0 A_JumpIfInventory("Taunting",1,"ZoomOut")
        TNT1 A 0 A_JumpIfInventory("Reloading",1,"Reload")
		TNT1 A 0 A_JumpIfInventory("Salute1",1,"ZoomOut")
		TNT1 A 0 A_JumpIfInventory("Salute2",1,"ZoomOut")
		TNT1 A 0 A_JumpIfInventory("Unloading",1,"ZoomOut")
		TNT1 A 0 A_JumpIfInventory("TossGrenade",1,"ZoomOut")
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,2)
		MP41 A 1 A_WeaponReady
		Goto ReadyZoom
		MP41 F 1 A_WeaponReady
		Goto ReadyZoom

	PuristGun:
		TNT1 A 0 A_TakeInventory("MP40", 1)
		Goto Deselect

    Fire:
        TNT1 A 0 A_JumpIfInventory("MP40Ammo",1,1)
        Goto DryFire
        TNT1 A 0 A_PlaySound("MP40")
		TNT1 A 0 A_Takeinventory("MP40Ammo",1)
        TNT1 A 0 A_FireCustomMissile("SmokeSpawner",0,0,0,5)
		TNT1 A 0 A_FireCustomMissile("YellowFlareSpawn",-5,0,0,0)
	    TNT1 A 0 A_JumpIfInventory("Zoomed",1,"FireZoom")
		TNT1 A 0 A_SpawnItemEx("PlayerMuzzle1",30,5,30)
		TNT1 A 0 A_ZoomFactor(0.99)
		MP40 B 1 BRIGHT A_FireBullets(3, 3, -1, 11, "HitPuff")
		TNT1 A 0 A_FireCustomMissile("ShakeYourAssMinor", 0, 0, 0, 0)
		TNT1 A 0 A_ZoomFactor(1.00)
		TNT1 A 0 A_SetPitch(-1.1 + pitch)
		MP40 C 1 A_AlertMonsters
		TNT1 A 0 A_FireCustomMissile("Tracer", frandom(-3,3), 0, -1, 0, 0, frandom(-3,3))
		TNT1 A 0 A_SetPitch(+0.4 + pitch)
		MP40 D 1 A_FireCustomMissile("9mmCaseSpawn",5,0,6,-10)
		TNT1 A 0 A_SetPitch(+0.3 + pitch)
        MP40 E 1
		TNT1 A 0 A_SetPitch(+0.2 + pitch)
        TNT1 A 0 A_Refire
		Goto Ready2

	FireZoom:
		TNT1 A 0 A_SpawnItemEx("PlayerMuzzle1",30,5,45)
		TNT1 A 0 A_ZoomFactor(1.48)
		MP41 B 1 BRIGHT A_AlertMonsters
		TNT1 A 0 A_FireCustomMissile("ShakeYourAssMinor", 0, 0, 0, 0)
		TNT1 A 0 A_SetPitch(-1.1 + pitch)
		MP41 C 1 A_FireBullets(1.5, 1.5, -1, 11, "HitPuff")
		TNT1 A 0 A_FireCustomMissile("Tracer", frandom(-1.5,1.5), 0, -1, 0, 0, frandom(-1.5,1.5))
		TNT1 A 0 A_SetPitch(+0.4 + pitch)
		TNT1 A 0 A_ZoomFactor(1.5)
		MP41 D 1 A_FireCustomMissile("9mmCaseSpawn",5,0,4,-2)
		TNT1 A 0 A_SetPitch(+0.3 + pitch)
        MP41 E 1
		TNT1 A 0 A_SetPitch(+0.2 + pitch)
        TNT1 A 0 A_Refire
		Goto ReadyZoom

	AltFire:
		TNT1 A 0 A_JumpIfInventory("Zoomed",1,"ZoomOut")
		TNT1 A 0 A_Giveinventory("Zoomed",1)
		TNT1 A 0 A_Giveinventory("ADSmode",1)
		TNT1 A 0 A_Giveinventory("GoSpecial",1)
        TNT1 A 0 A_ZoomFactor(1.5)
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,5)
        MP4D CBA 1
		TNT1 A 0 A_SetCrosshair(41)
        Goto ReadyZoom
        MP4D FED 1
		TNT1 A 0 A_SetCrosshair(41)
        Goto ReadyZoom

	ZoomOut:
		TNT1 A 0 A_Takeinventory("Zoomed",1)
		TNT1 A 0 A_Takeinventory("ADSmode",1)
		TNT1 A 0 A_Giveinventory("GoSpecial",1)
        TNT1 A 0 A_ZoomFactor(1.0)
		TNT1 A 0 A_SetCrosshair(0)
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,5)
        MP4D ABC 1
		TNT1 A 0 A_Jump(256,"Ready2")
        MP4D DEF 1
        Goto Ready2

	DryFire:
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,"Reload")
		TNT1 A 0 A_PlaySound("weapons/empty", 5)
		TNT1 A 0 A_Giveinventory("BoltInFront",1)
		TNT1 A 0 A_Refire
		Goto Ready2

	Reload:
		TNT1 A 0 A_Takeinventory("Reloading",1)
		TNT1 A 0 A_JumpIfInventory("MP40Ammo",32,"Ready2") //Check if weapon is full already.
        TNT1 A 0 A_JumpIfInventory("9mmMagazine",1,1) //Check if we have spare ammo.
        Goto Ready2
		TNT1 A 0 A_JumpIfInventory("TurboReload",1,"TurboReloadLoop")
		TNT1 A 0 A_JumpIfInventory("Zoomed",1,2) //Check if zoomed
		TNT1 A 0 A_Jump(256, 7)
		MP4D DEF 1
		TNT1 A 0 A_Takeinventory("Zoomed",1)
        TNT1 A 0 A_ZoomFactor(1.0)
		TNT1 A 0 A_SetCrosshair(0)
		TNT1 A 0 A_Takeinventory("ADSmode",1)
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,2) //Check bolt
		TNT1 A 0 A_PlaySound("weapons/empty", 5)
		MP40 F 1 A_WeaponReady
        TNT1 A 0 A_PlaySound("Reload")
		TNT1 A 0 A_GiveInventory("Pumping", 1)
		TNT1 A 0 A_JumpIfInventory("MP40Ammo",1, "Reload2") //Check if there is any bullet left in the magazine.
		TNT1 A 0 A_JumpIfInventory("HasUnloaded_MP40", 1, "Reload3") //Check if weapon has a magazine.
		MP4R ABJCDEF 1 //no bullet in magazine
		TNT1 A 0 A_FireCustomMissile("EmptyMag9mm",-5,0,8,-4)
		MP4R GGGGFHICC 1
		Goto ReloadLoop
	Reload2:
		MP4R ABJCIHF 1 //bullet in magazine
		TNT1 A 0 A_FireCustomMissile("EmptyMag9mm",-5,0,8,-4)
		MP4R GGGGFHICC 1
		Goto ReloadLoop
	Reload3:
		MP4U CBA 2 //reload when unloaded
		TNT1 A 0 A_Takeinventory("HasUnloaded_MP40",1)
		MP4R GGGGFHICC 1
		Goto ReloadLoop
		
	ReloadLoop:
		TNT1 A 0 A_JumpIfInventory("MP40Ammo",32,"ReloadDone")
		TNT1 A 0 A_JumpIfInventory("9mmMagazine",1,1)
		Goto ReloadDone
		TNT1 A 0 A_Giveinventory("MP40Ammo",1)
		TNT1 A 0 A_Takeinventory("9mmMagazine",1)
		Loop
				
	ReloadDone:
		MP4R CKLM 2
		TNT1 A 0 A_PlaySound("SLAPBABY")
		MP4R NOOPQRL 2
		MP4R KCJBA 1
		TNT1 A 0 A_Takeinventory("BoltInFront",1)
        Goto Ready2

	TurboReloadLoop:
		TNT1 A 0 A_JumpIfInventory("MP40Ammo",32,"TurboReloadDone")
        TNT1 A 0 A_JumpIfInventory("9mmMagazine",1,1)
        Goto TurboReloadDone
		TNT1 A 0 A_Takeinventory("9mmMagazine",1)
		TNT1 A 0 A_Giveinventory("MP40Ammo",1)
        Loop

	TurboReloadDone:
		TNT1 A 0 A_Takeinventory("BoltInFront",1)
		TNT1 A 0 A_Refire
		Goto Ready2

	Unload:
		TNT1 A 0 A_Takeinventory("Unloading",1)
        TNT1 A 0 A_JumpIfInventory("MP40Ammo",1,1)
        Goto Ready2
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,2)
		TNT1 A 0 A_PlaySound("weapons/empty", 5)
		MP40 F 1 A_WeaponReady
		TNT1 A 0 A_PlaySound("Reload")
		MP4R ABJCHIIFFFGGGG 1
		TNT1 A 0 A_GiveInventory("Pumping", 1)
		TNT1 A 0 A_Takeinventory("Unloading",1)

	UnloadLoop:
		TNT1 A 0 A_JumpIfInventory("MP40Ammo",1,1)
		Goto UnloadDone
		TNT1 A 0 A_Takeinventory("MP40Ammo",1)
		TNT1 A 0 A_Giveinventory("9mmMagazine",1)
		Loop

	UnloadDone:
		MP4U ABC 2
		TNT1 A 0 A_PlaySound("DryFire")
		TNT1 A 0 A_GiveInventory("HasUnloaded_MP40", 1)
		TNT1 A 0 A_Takeinventory("Unloading",1)
		Goto Ready2


// --------------------------------------------------------------------------
//
//	End stuff
//
// --------------------------------------------------------------------------

	KickFlash:
		MP4K ABCDE 1
		MP4K F 6
		MP4K EDCBA 1
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,2)
		MP40 A 2
		Stop
		MP40 F 2
		Stop

	AirKickFlash:
		MP4Z GFE 1
		MP4Z D 11
		MP4Z EFG 1
		TNT1 A 0 A_JumpIfInventory("BoltInFront",1,2)
		MP40 A 2
		Stop
		MP40 F 2
		Stop

	TauntFlash:
		MP4R ABJCK 1
		MP4R L 32
		MP4R KCJBA 1
		MP40 A 2
		Stop

	GrenadeFlash:
		MP4Z GFEDCBA 1
		Stop
	}
}

Actor BoltInFront : Inventory { inventory.maxamount 1 }
ACTOR HasUnloaded_MP40 : Inventory { Inventory.MaxAmount 1 }