ACTOR QuickGrenading : Inventory
{
	Inventory.MaxAmount 1
}

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

ACTOR Thrown97 // Credits to Project MSX for this
{
    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 "doomcompat"
	// bouncecount 2
	gravity 0.4
  bouncefactor 0.4
  wallbouncefactor 0.5
	damagetype "ExplosiveDamage"
	seesound "type97/bounce"
	// deathsound "grenade/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 A_JumpIfInventory("frag_timer", 10, "boom")
			FRTH IIIIII 1 // A_SpawnItemEx("frag_grenadesmoke_small", 0,0,0, 0,random(-1,1),1)
			FRGG A 0 A_GiveInventory("frag_timer", 1)
			loop
		Boom:
		TNT1 A 0
    TNT1 A 0 A_AlertMonsters
    TNT1 A 0 Radius_Quake(2,12,0,8,0)
    TNT1 AA 0 A_Explode(75,170)
    TNT1 AA 0 A_Explode(75,300)
    TNT1 A 0 A_SpawnItemEx("ExplosionMain2")
    TNT1 AAAAAAAAAA 0 A_SpawnItemEx("ImpactSpark2", random(-32,32), random(-32,32), random(-32,32), random(-5,5), random(-5,5), random(-1,5), 0, 128, 0)
    TNT1 AAAAAAAAAA 0 A_SpawnItemEx("ImpactSpark2", random(-32,32), random(-32,32), random(-32,32), random(-5,5), random(-5,5), random(-1,5), 0, 128, 0)
    TNT1 A 20
    stop
   }
}

ACTOR Thrown97Slow : Thrown97
{
	Speed 10
}

actor frag_timer : Inventory {
	Inventory.MaxAmount 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.AmmoUse 0
  Weapon.AmmoGive 1
  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,"QuickReady")
	PPSS A 0 A_PlaySoundEx("orderpistol/select","SoundSlot6")
	97SS ABCDE 1
	PPSS A 0 A_JumpIfInventory("QuickGrenading",1,"Fire")
	PPSS A 0 A_JumpIfInventory("DoBlocking",1,"QuickMelee")
    97ID A 1 A_WeaponReady
    Goto Ready+7
  QuickReady:
	PPSS A 0 A_PlaySoundEx("orderpistol/select","SoundSlot6")
	97SS ACE 1
	PPSS A 0 A_JumpIfInventory("QuickGrenading",1,"Fire")
	PPSS A 0 A_JumpIfInventory("DoBlocking",1,"QuickMelee")
    97ID A 1 A_WeaponReady
    Goto Ready+7
  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+7
	NULL AAA 0
	NULL A 0
	Goto Ready+7
  Deselect:
	 97SS EDCBA 1
     TNT1 AAAAAAAAAA 0 A_Lower
	 TNT1 A 1 A_Lower
     Goto Deselect+5
  Select:
	 TNT1 A 0 A_SetCrosshair(16)
     TNT1 A 0 A_Raise
     Wait
  Fire:
	 PPFI A 0 A_TakeInventory("QuickGrenading",1)
	 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)
	 Goto TakeCheck
	 
  //=================================================================
  // WHETHER OR NOT TO TAKE A GRENADE
  //=================================================================
	 
  TakeCheck:
	TNT1 A 0 A_JumpIfInventory("Perk_Demo",1,1)
	Goto ActuallyTake
	TNT1 A 0
	TNT1 A 0 A_JumpIfInventory("PerkLevel",10,"LowerChance")
	TNT1 A 0 A_Jump(64,"HoldEnd")
	Goto ActuallyTake
	
    LowerChance:
	TNT1 A 0 A_Jump(128,"HoldEnd")
	Goto ActuallyTake

  ActuallyTake:
	TNT1 A 0 A_TakeInventory("Type97Primary",1)
	Goto HoldEnd
	 
  //=================================================================
  HoldEnd:
	 97FI MNOPQ 1
	 TNT1 A 6 A_TakeInventory("QuickGrenading",1)
	 TNT1 A 0 A_TakeInventory("HasEnteredHold",1)
	 TNT1 A 0
	 Goto Ready
  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)
	 Goto TakeCheck
  Spawn:
    FRGT C -1
    Stop
  }
}