
ACTOR RLDemolitionAmmoChaingun : RLWeapon
{
  Obituary "%o was perforated by %k's chaingun."
  Decal RevenantScorch
  Tag "Demolition Ammo Chaingun"
  Weapon.SelectionOrder 25
  Weapon.SlotNumber 5
  Weapon.AmmoUse1 1
  Weapon.AmmoGive1 0
  Weapon.AmmoType1 "RLDemolitionAmmoChaingunClip"
  Weapon.AmmoType2 "Clip"
  Inventory.Icon "DCHGICON"
  States
  {
  Ready:
	TNT1 A 0 A_JumpIfInventory("RLMisfireSpamPreventionCooldown",1,"MisfirePrevention")
	TNT1 A 0 A_TakeInventory("RLDemolitionAmmoChaingunState",1)
    DCHG A 1 A_WeaponReady (WRF_ALLOWZOOM | WRF_ALLOWRELOAD)
    Goto Ready+2
	
  ScavengerDrop:
	TNT1 A 0 A_JumpIfInventory("RLScavengerDrop",1,"WeaponDropConfirm")
	TNT1 A 0 A_GiveInventory("RLScavengerDrop",1)
	TNT1 A 0 A_Print("Press again to drop current weapon,\nor press \crAlt Fire\c- to scrap it to retrieve a Mod Pack.")
	Goto AfterModDrop
  WeaponDropConfirm:
	TNT1 A 0 A_GiveInventory("RLMisfireSpamPreventionCooldown",1)
	TNT1 A 0 A_TakeInventory("RLScavengerDrop",1)
	TNT1 A 0 A_TakeInventory("RLWeaponDrop",1)
	TNT1 A 0 A_TakeInventory("PowerRLTechnicianMasterDamage", 1)
	TNT1 A 0 A_SpawnItemEx("RLDemolitionAmmoChaingunPickup", 0, 0, 40, 10, 0, 0, 0, SXF_SETMASTER)
	TNT1 A 0 A_GiveInventory("RLRarityTokenRemover", 1)
	TNT1 A 0 A_TakeInventory("RLDemolitionAmmoChaingunSelected", 1)
	TNT1 A 0 A_TakeInventory("RLWeaponLimit",1)
	TNT1 A 1 A_TakeInventory("RLDemolitionAmmoChaingun",1)
	Stop
	
  AfterModDrop:
    DCHG A 8 A_WeaponReady (16)
    Goto Ready
  MisfirePrevention:
    DCHG A 1 A_WeaponReady (WRF_NOFIRE | WRF_NOBOB)
    TNT1 A 0 A_JumpIfInventory("RLMisfireSpamPreventionCooldown", 1, "MisfirePrevention")
    Goto Ready
	
  Deselect:
	TNT1 A 0
	TNT1 A 0 A_GiveInventory("RLDeselectionFunction",1)
	TNT1 A 0 A_TakeInventory("RLDemolitionAmmoChaingunSelected", 1)
	TNT1 A 0 A_GiveInventory("RLRarityTokenRemover", 1)
	TNT1 A 0 A_TakeInventory("PowerRLTechnicianMasterDamage", 1)
  DeselectLoop:
    TNT1 A 0 A_JumpIfInventory("RLShellshockPerk", 1, "DeselectRenegade")
    TNT1 A 0 A_Lower
    DCHG A 1 A_Lower
    Loop
  DeselectRenegade:
    TNT1 A 0 A_Lower
    loop
  SelectRenegade: 
    TNT1 A 0 A_Raise
    loop
  SelectSetWeapon:
	TNT1 A 0 A_GiveInventory("RLMasterAssemblyTypeWeapon", 1)
	TNT1 A 0 A_GiveInventory("RLRapidfireTypeWeapon", 1)
	TNT1 A 0 A_GiveInventory("RLAssembledWeaponToken", 1)
	TNT1 A 0 A_GiveInventory("RLDemolitionAmmoChaingunSelected", 1)
	TNT1 A 0 A_JumpIfInventory("RLScavengerPerk",1, 1)
    Goto SelectLoop
	TNT1 A 0 A_GiveInventory("PowerRLTechnicianMasterDamage", 1)
    Goto SelectLoop
  SelectLoop:
    TNT1 A 0 A_JumpIfInventory("RLShellshockPerk", 1, "SelectRenegade")
    TNT1 A 0 A_Raise
    DCHG A 1 A_Raise
    Loop
	
  Fire:
	DCHG A 0 A_JumpIfInventory("RLRestrictionMeleeOnly",1,"MeleeRestricted") // Cannot fire in levels that restrict it
	DCHG A 0 A_JumpIfInventory("RLRestrictionSilencedOnly",1,"SilencedRestricted") // Cannot fire in levels that restrict it
	DCHG A 0 A_JumpIfInventory("RLRestrictionNoBoom",1,"NoBoomRestricted") // Cannot fire in levels that restrict it
	DCHG A 0 A_JumpIfInventory("RLRestrictionHighTechOnly",1,"HighTechRestricted") // Cannot fire in levels that restrict it
	DCHG A 0 A_JumpIfInventory("RLRestrictionCunningOnly",1,"CunningRestricted") // Cannot fire in levels that restrict it
	
	TNT1 A 0 A_JumpIfNoAmmo("Reload")
	TNT1 A 0 A_AlertMonsters
	TNT1 A 0 A_GiveInventory("RLDemolitionAmmoChaingunAmmochain",1)
	TNT1 A 0 A_JumpIfInventory("RLDemolitionAmmoChaingunAmmochain",0,2)
	TNT1 A 0 A_JumpIfInventory("RLAmmochainPerk",1,2)
	TNT1 A 0 A_FireBullets (6, 2, -1, 6, "RLDemoChaingunPuff")
    Goto FireFinish
	TNT1 A 0 A_FireBullets (6, 2, -1, 6, "RLDemoChaingunPuff", 0)
    Goto FireFinish
	
  FireFinish:
    TNT1 A 0 A_PlaySound("weapons/demochaingun", CHAN_WEAPON, 1, 0, 0.7)
    TNT1 A 0 A_PlaySound("weapons/demochaingun", 7, 0.05, 0, 0.20)
	TNT1 A 0 A_JumpIfInventory("RLAmmochainPerk",1,"FireFinishPowerupCheck") // Check for powerups.
  FireFinishPowerupCheckDone: // No valid powerups detected.
    TNT1 A 0 A_SpawnItemEx ("RLChaingunRecoil", 0,0,0, 0,0,0, 0, SXF_NOCHECKPOSITION | SXF_SETMASTER) // Pitch-based recoil
    DCHG A 1 A_GunFlash
    DCHG NCD 1
	Goto FireFinishContinue
  FireFinishContinue:
	TNT1 A 0 A_JumpIfInventory("RLDemolitionAmmoChaingunAmmochain",0,2)
	TNT1 A 0 A_Jump(256,2)
	TNT1 A 0 A_TakeInventory("RLDemolitionAmmoChaingunAmmochain",0)
    Goto FireFinishStateCheck
	TNT1 A 0 A_GiveInventory("RLDemolitionAmmoChaingunAmmochain",1)
    Goto FireFinishStateCheck
  FireFinishStateCheck:
	TNT1 A 0 A_JumpIfInventory("RLDemolitionAmmoChaingunState",1,2)
	TNT1 A 0 A_GiveInventory("RLDemolitionAmmoChaingunState",1)
    Goto Fire
	TNT1 A 0 A_TakeInventory("RLDemolitionAmmoChaingunState",1)
    TNT1 A 0 A_ReFire
	Goto WinddownMain

//-------------------------------------------------------------------------------------------------
  FireFinishPowerupCheck: // Marine fires one Firestorm's worth faster while affected by powerups.
	TNT1 A 0 A_JumpIfInventory("PowerInvulnerable",1,"FireFinishPowerupCheckConfirmed")
	TNT1 A 0 A_JumpIfInventory("PowerInvisibility",1,"FireFinishPowerupCheckConfirmed")
	TNT1 A 0 A_JumpIfInventory("PowerIronFeet",1,"FireFinishPowerupCheckConfirmed")
	TNT1 A 0 A_JumpIfInventory("PowerLightAmp",1,"FireFinishPowerupCheckConfirmed")
	TNT1 A 0 A_JumpIfInventory("PowerInfiniteAmmo",1,"FireFinishPowerupCheckConfirmed")
	TNT1 A 0 A_JumpIfInventory("PowerDrain",1,"FireFinishPowerupCheckConfirmed")
	TNT1 A 0 A_JumpIfInventory("PowerRLBerserkResistance",1,"FireFinishPowerupCheckConfirmed")
	Goto FireFinishPowerupCheckDone
  FireFinishPowerupCheckConfirmed:
    TNT1 A 0 A_SpawnItemEx ("RLUziRecoil", 0,0,0, 0,0,0, 0, SXF_NOCHECKPOSITION | SXF_SETMASTER) // Pitch-based recoil
    DCHG A 1 A_GunFlash
    DCHG NC 1
	Goto FireFinishContinue
//-------------------------------------------------------------------------------------------------
	
  WinddownMain:
    TNT1 A 0 A_PlaySound("weapons/demochaingunwinddown", 6, 1, 0, 1.8)
	TNT1 A 0 A_JumpIfNoAmmo("WinddownNoAmmo")
	DCHG A 1 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueA")
	DCHG B 1 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueB")
	DCHG C 2 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueC")
	DCHG D 2 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueD")
	DCHG A 2 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueA")
	DCHG B 3 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueB")
	DCHG C 3 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueC")
	DCHG D 3 A_JumpIfInventory("RLSynthFireLeft",1,"WinddownContinueD")
    Goto Ready
  WinddownNoAmmo:
	DCHG AB 1
	DCHG CDA 2
	DCHG BCD 3
    Goto Ready
  WinddownContinueA:
	DCHG ABCD 1
	TNT1 A 0 A_JumpIfInventory("RLSynthFireLeft",1,"Fire")
    Goto WinddownMain
  WinddownContinueB:
	DCHG BCD 1
	TNT1 A 0 A_JumpIfInventory("RLSynthFireLeft",1,"Fire")
    Goto WinddownMain
  WinddownContinueC:
	DCHG CD 1
	TNT1 A 0 A_JumpIfInventory("RLSynthFireLeft",1,"Fire")
    Goto WinddownMain
  WinddownContinueD:
	DCHG D 1
	TNT1 A 0 A_JumpIfInventory("RLSynthFireLeft",1,"Fire")
    Goto WinddownMain
	
  Reload:
	DCHG A 1 A_WeaponReady (WRF_NOPRIMARY | WRF_ALLOWZOOM)
	TNT1 A 0 A_JumpIfInventory("RLDemolitionAmmoChaingunClip", 40, "Ready")
	TNT1 A 0 A_JumpIfInventory("Clip", 1, 1)
	Goto Ready
	TNT1 A 0 // Play the reload animation.
    DCHG I 5
    DCHG J 5
    DCHG K 5
    TNT1 A 0 A_SpawnItemEx ("RLStandardReloadOutRecoil", 0,0,0, 0,0,0, 0, SXF_NOCHECKPOSITION | SXF_SETMASTER) // Pitch-based recoil
    DCHG L 5 A_PlaySound("weapons/demochaingununload", CHAN_ITEM)
    DCHG L 5
  ReloadWorking:
	TNT1 A 0 A_TakeInventory("Clip", 1)
	TNT1 A 0 A_GiveInventory("RLDemolitionAmmoChaingunClip", 1)
	TNT1 A 0 A_JumpIfInventory("RLDemolitionAmmoChaingunClip",40,2)
	TNT1 A 0 A_JumpIfInventory("Clip",1,2)
	TNT1 A 0
	Goto ReloadFinish
	TNT1 A 0
	Goto ReloadWorking
  ReloadFinish:
    TNT1 A 0 A_PlaySound("weapons/demochaingunload", 7)
    TNT1 A 0 A_SpawnItemEx ("RLStandardReloadInRecoil", 0,0,0, 0,0,0, 0, SXF_NOCHECKPOSITION | SXF_SETMASTER) // Pitch-based recoil
    DCHG M 5
    DCHG L 5
    DCHG K 5
    DCHG J 5
    DCHG I 5
	Goto ReloadSpin
  ReloadSpin:
    TNT1 A 0 A_PlaySound("weapons/demochaingunwinddown", 6)
	DCHG AB 3
	DCHG CD 4
	TNT1 A 0 A_JumpIfNoAmmo(2)
	TNT1 A 0 A_WeaponReady(WRF_NOBOB | WRF_NOSWITCH)
	DCHG AB 5
	DCHG CD 6
	Goto Ready
	
  Flash:
	TNT1 A 0 A_JumpIfInventory("RLDemolitionAmmoChaingunState",1,3)
    DCHG E 1 Bright A_Light2
    DCHG F 1 Bright A_Light1
    Goto LightDone
    DCHG G 1 Bright A_Light2
    DCHG H 1 Bright A_Light1
    Goto LightDone
	
  Spawn:
	TNT1 A 0 NoDelay A_SpawnItemEx("RLDemolitionAmmoChaingunPickup",0,0,0, 0,0,0, 0,SXF_NOCHECKPOSITION, 0, tid)
    Stop
  Altfire:
	TNT1 A 0 A_JumpIfInventory("RLArmorModItemActivate",1, "ArmorModConversion")
	TNT1 A 0 A_JumpIfInventory("RLScavengerDrop",1,"ScavengerTime")
	Goto Ready
  ScavengerTime:
	TNT1 A 0 A_GiveInventory("RLScavengingExtraEffects", 1)
    TNT1 A 0 A_GiveInventory("RLDemolitionAmmoAssemblyLearntToken", 1) // Assembly is now listed in the HUD menus
	TNT1 A 0 A_Print("Mod pack extracted from discarded weapon.")
	TNT1 A 0 A_TakeInventory("RLScavengerDrop",1)
	TNT1 A 0 A_GiveInventory("RLRarityTokenRemover", 1)
	TNT1 A 0 A_TakeInventory("RLDemolitionAmmoChaingunSelected", 1)
	TNT1 A 0 A_TakeInventory("RLWeaponLimit",1)
	TNT1 A 0 A_TakeInventory("PowerRLTechnicianMasterDamage", 1)
	TNT1 A 0 A_Jump(256,1,2,3,4)
	TNT1 A 0 A_FireCustomMissile("DroppedRLPowerMod",0,0,0)
	Goto ScavengerTimeEnd
	TNT1 A 0 A_FireCustomMissile("DroppedRLTechnicalMod",0,0,0)
	Goto ScavengerTimeEnd
	TNT1 A 0 A_FireCustomMissile("DroppedRLTechnicalMod",0,0,0)
	Goto ScavengerTimeEnd
	TNT1 A 0 A_FireCustomMissile("DroppedRLFirestormMod",0,0,0)
	Goto ScavengerTimeEnd
  ScavengerTimeEnd:
	TNT1 A 1 A_TakeInventory("RLDemolitionAmmoChaingun",1)
	Stop
  }
}

//-------------------------------------------------------------------
//		PROJECTILES

ACTOR RLDemoChaingunPuff : RLDemoPuffBase
{
	Scale 1.0
	States
	{
		Spawn:
			TNT1 A 0
			TNT1 A 0 A_GiveToTarget ("RLFireangelBonusCheck", 1)
			TNT1 A 0 A_RadiusGive ("RLFireangelRadiusBonusCheck", 16, RGF_PLAYERS)
			TNT1 A 0 A_JumpIf(CallACS("DRLA_QuakeNShake") == 0, 3)
			TNT1 A 0 A_Quake (1, 8, 0, 256, "")
			TNT1 A 0 A_Quake (3, 8, 0, 64, "")
			TNT1 A 0 A_PlaySound ("weapons/bulletexp", 7, 0.5, 0, 0.7)
			TNT1 A 0 A_PlaySound("weapons/bulletexp", CHAN_ITEM, 0.05, 0, 0.05)
			//TNT1 A 0 A_Explode (20, 16, 1, 1, 16)
			TNT1 A 0 A_Explode (40, 64, 1, 1, 64) // New experimental damage calculation
			Goto RLDemoPuffBase::Spawn.Normal
	}
}

//==============================================================================
//DEMOLITION AMMO FUN CHART OF MATHING
//SIDDOYLE IS A MATH FRISBEE
//==============================================================================
//					Firerate	Mag		Exp DMG		Exp DPS		Avg DMG		DPS		Ammo Eff.	Total DPS
//Pistol			14			6		77.33		193.325		12			30		5			223.325
//Chaingun			4			40		20.43		178.763		12			105		2.625		283.763
//Battle Rifle		4			20		25.43		222.513		10			87.5	4.375		310.013
//Uzi				3			32		18.82		219.567		8			93.333	2.917		312.9
//Minigun			1			200		7.86		275.1 		10			350		1.75		625.1
//Combat Pistol		10			15		45.9		160.65		14			49		3.267		209.65
//Marksman Pistol	13			10		61.14		164.608		12			32.308	3.231		196.916
//Handcannon		19			7		86.86		160.005		32			58.947	8.421		218.952
//==============================================================================

//-------------------------------------------------------------------
//		MECHANICS

Actor RLDemolitionAmmoChaingunSelected : RLPermanentInventory {}
Actor RLDemolitionAmmoChaingunState : RLPermanentInventory {}
Actor RLDemolitionAmmoChaingunAmmochain : RLAmmochainDefault {}

ACTOR RLDemolitionAmmoChaingunClip : Ammo
{
   +INVENTORY.IGNORESKILL
   Inventory.MaxAmount 40
   Inventory.Icon ""
}

//-------------------------------------------------------------------
//		PICKUPS

ACTOR RLDemolitionAmmoChaingunPickup : RLMasterAssembledWeaponPickup
{
	//$Sprite DCHGX0
	Tag "Demolition Ammo Chaingun [Pickup]"
	States
	{
	SpawnFrame:
		DCHG X 4
		"####" "#" 0 A_Jump(256, "SpawnLoop")
		Goto SpawnLoop
	SpawnLoop:
		"####" "#" 0 A_JumpIf(Abs(VelZ) <= 0.01 && user_floorhit == 0, "FloorHit")
		"####" "#" 1
		Loop
	FloorHit:
		"####" "#" 0 A_PlaySound("weapons/gundropbig")
		"####" "#" 0 A_ScaleVelocity (0.25)
		"####" "#" 0 A_ChangeFlag("SPECIAL", true)
		"####" "#" 0 A_ChangeFlag("THRUACTORS", false)
		"####" "#" 0 A_SetUserVar("user_floorhit", 1)
		"####" "#" 0 A_Jump(256, "SpawnLoop")
		Stop
	Pickup:
		"####" "#" 0
		"####" "#" 0 A_JumpIfInventory("RLDemolitionAmmoChaingun",1,"NotPickup") // If the player already has this weapon, they are not given the weapon
		"####" "#" 0 A_JumpIfInventory("RLWeaponLimit",0,"NotPickupLimit") // The absolute max limit check for weapons
		"####" "#" 0 A_Jump(256, "GiveWeapon")
		Goto GiveWeapon
	GiveWeapon:
		"####" "#" 0 A_GiveInventory("RLDemolitionAmmoChaingun",1)
		"####" "#" 0 A_GiveInventory("RLWeaponLimit",1)
		"####" "#" 0 A_Print("\ccPicked up a \cvDemolition Ammo Chaingun.")
		Goto PickupSound
	NotPickupLimit:
		"####" "#" 0 A_Jump(256, "NotPickup")
		Goto NotPickup
	NotPickup:
		"####" "#" 0 A_JumpIfInventory("DRLA_WeaponAntiLogFloodCheck",1,"NotPickup_NoPrint")
		"####" "#" 0 A_GiveInventory("DRLA_WeaponAntiLogFloodCheck")
		"####" "#" 0 A_Print("\cv= Demolition Ammo Chaingun =", 2, "CONFONT")
		Goto NotPickup_NoPrint
	NotPickup_NoPrint:
		"####" "#" 0
		Fail
	PickupSound:
		"####" "#" 0 A_PlaySound("weapons/DemoChaingunpickup")
		Stop
//==============================================================================
	MarathonStuff:
		"####" "#" 0 A_SpawnItemEx("RLMarathonClip",0,0,0, 0,0,0, 0,SXF_NOCHECKPOSITION)
		Stop
	SuperweaponStuff:
		"####" "#" 0 A_Jump(256, "NoSpecialStartsStuff")
		Goto NoSpecialStartsStuff
	}
}

ACTOR RLDemolitionAmmoChaingunWorldSpawnPickup : RLDemolitionAmmoChaingunPickup 17822
{
	States
	{
	NoSpecialStartsStuff:
		TNT1 A 0 A_JumpIf(CallACS("DRLA_opengl"), 3) // If enabled, rarity beam will be cyan, rather than blue
		TNT1 A 0 A_SpawnItemEx("RLRarityBeamAssembledWorld", 0, 0, 0, 0, 0, 0, 0, SXF_SETMASTER)
		TNT1 A 0 A_Jump(256,2)
		TNT1 A 0 A_SpawnItemEx("RLRarityBeamAssembledWorldDRPG", 0, 0, 0, 0, 0, 0, 0, SXF_SETMASTER)
		TNT1 A 0 A_JumpIf(CallACS("DRPGCheckTiem"), "DRPGInit")
	SpawnFrame:
		DCHG X 4
		"####" "#" 0 A_GiveInventory("RLMasterItemSpawnMessageSender",1)
		"####" "#" 0 A_PlaySound("weapons/masterspawned", 7, 0.25, 0, 0)
		"####" "#" 0 A_Jump(256, "SpawnLoop")
		Goto SpawnLoop
		
	GiveWeapon:
		"####" "#" 0 A_GiveInventory("RLDemolitionAmmoChaingun",1)
		"####" "#" 0 A_GiveInventory("RLWeaponLimit",1)
		"####" "#" 0 A_Print("\ccPicked up a \cvDemolition Ammo Chaingun.")
		"####" "#" 0 A_GiveInventory("RLDangerLevelMasterGiver",1, AAPTR_PLAYER1)
		Goto PickupSound
	}
}
