ACTOR Type97Primary : Ammo
{
  Inventory.Amount 1
  Inventory.MaxAmount 5
  Ammo.BackpackAmount 1
  Ammo.BackpackMaxAmount 5
  Inventory.Icon "CLIPA0"
  +INVENTORY.IGNORESKILL
}

Actor frag_timer : Inventory {Inventory.MaxAmount 10}

// Credits to Project MSX for this
ACTOR Thrown97
{
    Radius 5
	Height 5
	Speed 25
	Mass 50
	Damage (5)
	Scale 0.5
	RenderStyle Normal
    PROJECTILE
	-NOGRAVITY
	+RANDOMIZE
	+HEXENBOUNCE
	+NOEXTREMEDEATH
	+FORCERADIUSDMG
	+CANBOUNCEWATER
	-BOUNCEONACTORS
	+ROCKETTRAIL
	+FORCEXYBILLBOARD
	+BLOODLESSIMPACT
	BounceType "Doom"
	Gravity 0.4
	BounceFactor 0.4
	WallBounceFactor 0.5
	DamageType "ExplosiveDamage"
	BounceSound "type97/bounce"
	Obituary "%o was blown up by %k's frag grenade."
	   States
	   {
			Spawn:
				TNT1 A 0
				TNT1 A 0 A_StopSound
				FRGR A 0 A_JumpIfInventory("frag_timer", 10, "boom")
				FRTH ABCDEFGH 1 // A_SpawnItemEx("frag_grenadesmoke", 0,0,0, 0,random(-1,1),1)
				FRGR A 0 A_ScaleVelocity(0.90)
				FRGR A 0 A_GiveInventory("frag_timer", 1)
				goto Spawn+2
			Death:
				FRGG A 0
				//FRGG A 0 A_JumpIfInventory("frag_timer", 10, "Boom")
				FRTH IIIIIIIIII 6 // A_SpawnItemEx("frag_grenadesmoke_small", 0,0,0, 0,random(-1,1),1)
				//FRGG A 0 A_GiveInventory("frag_timer", 1)
				//Loop
				Goto Boom
			Boom:
				TNT1 A 0
				TNT1 A 0 A_AlertMonsters
				TNT1 A 0 Radius_Quake(2,12,0,8,0)
				TNT1 A 0 A_SpawnItemEx("UltraExplosionNade")
				TNT1 AA 0 A_Explode(75,170)
				TNT1 AA 0 A_Explode(75,300)
				TNT1 A 20
				stop
	   }
}

ACTOR Thrown97Slow : Thrown97
{
	Speed 10
}

ACTOR Type97Pickup : CustomInventory 11503
{
  Inventory.PickupMessage "Picked up a Type-97 grenade."
  Inventory.PickupSound "type97/bounce"
  Scale 0.50
  Radius 6
  Height 16
  //$Category ZT2 - Weapons
  //$Title Type-97 Grenade
  //$Sprite FRGTC0
  States
  {
  Spawn:
  FRGT C -1
  stop
  Pickup:
  TNT1 A 0
  TNT1 A 0 A_JumpIfInventory("Type-97 Grenade",1,"GiveAmmo")
  TNT1 A 0 A_GiveInventory("Type-97 Grenade",1)
  stop
  GiveAmmo:
  TNT1 A 0 A_JumpIfInventory("Type97Primary",5,"Failure")
  TNT1 A 0 A_GiveInventory("Type97Primary",1)
  TNT1 A 0
  stop
  Failure:
  TNT1 A 0
  Fail
  }
}

ACTOR "Type-97 Grenade" : Weapon
{
  +WEAPON.NOAUTOAIM
  Weapon.AmmoUse 0
  Weapon.AmmoGive 1
  Weapon.BobStyle Smooth
  Weapon.BobSpeed 2.5
  Weapon.BobRangeX 0.5
  Weapon.BobRangeY 0.3
  Weapon.AmmoType "Type97Primary"
  Obituary "\cJ%k \cG> \cKType-97 Grenade \cG> \cJ%o"
  Inventory.Pickupmessage "You picked up a Type-97 grenade."
  Inventory.PickupSound "type97/bounce"
  Tag "Type-97 Grenade"
  +INVENTORY.UNDROPPABLE
  Scale 0.5
  +WEAPON.NOAUTOFIRE
  States
  {
  Ready:
	PPSS A 0 A_JumpIfInventory("QuickGrenading",1,"Fire")
	PPSS A 0 A_JumpIfInventory("DoBlocking",1,"QuickMelee")
    97ID A 1 A_WeaponReady
    Loop

  QuickMelee:
	97BL A 1
	PPBL B 0 A_PlaySoundEx("fist/swing","Weapon")
	97BL BC 1
	PPBL D 0 A_CustomPunch (random(4,5),1,0,"BlockPuff")
	97BL D 2
	97BL E 4
	97BL DCBA 2
	PPBL A 0 A_TakeInventory("DoBlocking",1)
	Goto Ready+7
	
  ToReady:
	NULL A 0
	Goto Ready
	NULL AAA 0
	NULL A 0
	Goto Ready
	
  Purgatory:
	TNT1 A 1 A_WeaponReady
	Loop
	
  Deselect:
	 97SS A 0 A_JumpIfInventory("QuickGrenading",1,6)
	 97SS EDCBA 1
	 Goto InstantDeselect
	 97SS A 0 A_TakeInventory("QuickGrenading", 1)
  InstantDeselect:
     TNT1 AAAAAAAAAA 0 A_Lower
	 TNT1 A 1 A_Lower
     Loop
	 
  Select:
	 TNT1 A 0 A_TakeInventory("PendingQuickness", 1)
	 TNT1 A 0 A_SetCrosshair(16)
	 TNT1 A 0 A_Raise
	 TNT1 A 1 A_WeaponReady(WRF_NOSWITCH|WRF_NOFIRE)
	 97SS A 0 A_PlaySoundEx("orderpistol/select","SoundSlot6")
	 97SS ABCDE 1 A_WeaponReady(WRF_NOSWITCH|WRF_NOFIRE)
     Goto Ready
	 
  Fire:
	 PPFI A 0 A_JumpIfInventory("Type97Primary",1,1)
	 Goto ToReady+2
	 97FI ABC 1
	 97FI D 1
	 97FI E 0 A_GiveInventory("HasEnteredHold",1)
	 97FI A 0 A_TakeInventory("IsReloading",1)
	 Goto Hold
	 
  Nevermind:
	 97FI A 0 A_TakeInventory("IsReloading",1)
	 97FI E 0 A_TakeInventory("HasEnteredHold",1)
	 97FI DCBA 1
	 Goto Ready+7
	 
  Hold:
	 97FI D 0 A_JumpIfInventory("HasEnteredHold",1,1)
	 Goto Fire
	 97FI D 0 A_JumpIfInventory("IsReloading",1,"Nevermind")
	 97FI D 1 A_WeaponReady(WRF_NOPRIMARY | WRF_NOSECONDARY)
	 97FI D 0 A_JumpIfInventory("QuickGrenading",1,1)
	 97FI D 0 A_ReFire
	 97FI E 0 A_PlaySoundEx("type97/pinpull","SoundSlot6")
	 97FI EG 1
	 97FI I 3
	 97FI J 0 A_PlaySoundEx("type97/throw","SoundSlot6")
	 97FI JKL 1
	 FRGN A 0 A_FireCustomMissile("Thrown97",random(-200,200)/100.00,0,0,16,0,random(-150,150)/100.00)
	 FRGN A 0 ACS_NamedExecuteAlways("ConsumeNade",0)
  HoldEnd:
	 97FI MNOPQ 1
	 TNT1 A 0 A_JumpIfInventory("QuickGrenading",1,"BackToLast")
	 TNT1 A 6 A_TakeInventory("QuickGrenading",1)
	 TNT1 A 0 A_TakeInventory("HasEnteredHold",1)
	 TNT1 A 0
	 Goto Ready
  BackToLast:
	 TNT1 A 0 ACS_NamedExecute("LastWeapon",0,0,0,0)
	 Goto Purgatory
  AltFire:
	 PPFI A 0 A_JumpIfInventory("Type97Primary",1,1)
	 Goto ToReady+2
	 97FI ABC 1
	 97FI D 1
	 97FI E 0 A_GiveInventory("HasEnteredHold",1)
	 97FI A 0 A_TakeInventory("IsReloading",1)
	 Goto AltHold
  AltHold:
	 97FI D 0 A_JumpIfInventory("HasEnteredHold",1,1)
	 Goto AltFire
	 97FI D 0 A_JumpIfInventory("IsReloading",1,"Nevermind")
	 97FI D 1 A_WeaponReady(WRF_NOPRIMARY | WRF_NOSECONDARY)
	 97FI D 0 A_ReFire
	 97FI E 0 A_PlaySoundEx("type97/pinpull","SoundSlot6")
	 97FI EG 1
	 97FI I 3
	 97FI J 0 A_PlaySoundEx("type97/throw","SoundSlot6")
	 97FI JKL 1
	 FRGN A 0 A_FireCustomMissile("Thrown97Slow",random(-200,200)/100.00,0,0,16,0,random(-150,150)/100.00)
	 FRGN A 0 ACS_NamedExecuteAlways("ConsumeNade",0)
	 Goto TakeCheck
  Spawn:
    FRGT C -1
    Stop
  }
}