// REMOTE C4
// TODO: make the weapon sprite include the detonator!

actor RemoteC4Ammo : Ammo {
	inventory.amount 0
	inventory.maxamount 4
	ammo.backpackamount 1
	ammo.backpackmaxamount 4
}

actor RemoteC4 : AOWWeapon {
	+NOALERT
	Obituary "%o bonks %k with a 100kg Remote C4."
	Weapon.AmmoType "RemoteC4Ammo"
	Weapon.AmmoUse 1
	Weapon.AmmoGive 5
	States {
	Ready:
		RC4G B 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "ReadyFull")
		RC4G B 5 A_WeaponReady (WRF_NOPRIMARY)
		loop
	ReadyFull:
		RC4G A -1 A_WeaponReady
		wait
	Deselect:
		RC4G B 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "DeselectFull")
		RC4G B 0 A_Lower
		RC4G B 1 A_Lower
		goto Deselect+1
	DeselectFull:
		RC4G A 0 A_Lower
		RC4G A 1 A_Lower
		loop
	Select:
		RC4G B 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "SelectFull")
		RC4G B 1 A_Raise
		RC4G B 0 A_Raise
		loop
	SelectFull:
		RC4G A 1 A_Raise
		RC4G A 0 A_Raise
		loop
	Fire:
		TNT1 A 0 A_JumpIfInventory ("IsInSpawnRoom", 1, "Ready")
		TNT1 A 0 A_JumpIfInventory ("InMenu", 1, "Ready")
		RC4G A 1 offset (1, 31)
		RC4G A 1 offset (7, 37)
		RC4G A 1 offset (13, 43)
		RC4G A 1 offset (19, 49)
		RC4G A 1 offset (26, 56)
		RC4G A 1 offset (34, 64)
		RC4G A 1 offset (42, 72)
		RC4G A 1 offset (54, 84) A_PlaySound ("c4/remote", CHAN_WEAPON)
		RC4G A 1 offset (60, 90)
		RC4G A 1 offset (40, 70)
		RC4G B 0 A_JumpIfInventory ("IsInSpawnRoom", 1, "ReadyFull")
		RC4G B 5 A_FireCustomMissile ("RemoteC4Thrown")
		RC4G B 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "FireEndFull")
		goto FireEnd
	FireEnd:
		RC4G B 1 offset (30, 60)
		RC4G B 1 offset (10, 80)
		RC4G B 1 offset (0, 100)
		RC4G B 1 offset (0, 110)
		goto Ready
	FireEndFull:
		RC4G B 1 offset (30, 60)
		RC4G B 1 offset (10, 40)
		RC4G B 1 offset (0, 40)
		RC4G A 1 offset (0, 60)
		RC4G A 1 offset (0, 80)
		RC4G A 1 offset (0, 100)
		RC4G A 1 offset (0, 110)
		goto Ready
	AltFire:
		RC4E A 0 A_JumpIfInventory ("InMenu", 1, "Ready")
		RC4E A 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "AltFireFull")
		RC4E ABCDEFGH 1
		RC4E I 2
		RC4E J 2 A_PlaySound ("c4/remote2", CHAN_WEAPON)
		RC4E I 4 A_GiveInventory ("C4DetonateCheck", 1)
		RC4E H 6
		RC4E G 1 A_TakeInventory ("C4DetonateCheck", 666)
		RC4E FEDCBA 1
		goto Ready
	AltFireFull:
		RC4D ABCDEFGH 1
		RC4D I 2
		RC4D J 2 A_PlaySound ("c4/remote2", CHAN_WEAPON)
		RC4D I 4 A_GiveInventory ("C4DetonateCheck", 1)
		RC4D H 6
		RC4D G 1 A_TakeInventory ("C4DetonateCheck", 666)
		RC4D FEDCBA 1
		goto ReadyFull
	}
}

actor RemoteC4Thrown {
	PROJECTILE
	-NOGRAVITY
	Radius 6
	Height 8
	Speed 30
	Damage 0
	Scale 0.2
	Translation "224:246=16:31"
	states {
	Spawn:
		RC4T A -1
		stop
	Death:
		RC4T A 35 A_NoGravity // Wait one second and then become armed
		RC4T A 0 A_JumpIfInTargetInventory ("IsBlue", 1, "Death.Blue")
		RC4T A 0 A_SpawnItemEx ("RemoteC4Armed")
		stop
	Death.Blue:
		RC4T A 0 A_SpawnItemEx ("RemoteC4ArmedBlue")
		stop
	}
}

actor RemoteC4Armed {
	+NOEXTREMEDEATH
	+NOGRAVITY
	+NOBLOOD
	+SHOOTABLE
	+NOTARGETSWITCH
	Health 10
	Radius 12
	Height 12
	Mass 0x7FFFFFFF
	Speed 0
	Scale 0.2
	Damage 0
	DamageType "Explosion"
	Obituary "$OB_REMOTEC4"
	DamageFactor "Explosion", 0.0
	DamageFactor "Fire", 0.0
	DamageFactor "Chemical", 0.0
	DamageFactor "ChemicalSpecial", 0.0
	DamageFactor "Energy", 0.0
	DamageFactor "AntiArmor", 0.0
	DamageFactor "Time", 0.0
	DamageFactor "MassDriver", 0.0
	DamageFactor "Mechstomp", 0.0
	DamageFactor "Mine", 0.0
	DamageFactor "Monster", 0.0
	
	States {
	Spawn:
		RC4T A 0
		RC4T A 0 A_PlaySound ("terrorist/select", CHAN_VOICE) // *click*
		goto Idle
	Idle:
		RC4T A 0 A_JumpIfInTargetInventory ("RemoteC4Ammo", 4, "Death.Disarm")
		RC4T A 0 A_JumpIfInTargetInventory ("C4DetonateCheck", 1, "Death")
		RC4T A 5
		loop
	Death:
		RC4T A 0 A_NoGravity
		RC4T A 10 A_PlaySound ("c4/detonate")
		TNT1 A 0 A_Explode (500, 64)
		TNT1 A 0 A_Explode (400, 196)
		TNT1 A 0 A_Explode (500, 256)
		TNT1 A 0 A_Explode (250, 640)
		goto DeathFinish
	Death.Bullet:
	Death.Rifle:
		TNT1 A 0 A_Explode (500, 48)
		TNT1 A 0 A_Explode (400, 96)
		TNT1 A 0 A_Explode (500, 128)
		TNT1 A 0 A_Explode (250, 192)
		goto DeathFinish
	DeathFinish:
		TNT1 A 0 A_SpawnItemEx ("HeavyMissileQuake")
		TNT1 A 0 A_SpawnItemEx ("ExplosionMainLarge")
		TNT1 A 0 A_PlaySound ("weapons/rocklx", CHAN_BODY)
		TNT1 A 0 A_PlaySound ("weapons/rocklx", CHAN_WEAPON)
		TNT1 A 0 A_SpawnItemEx ("FlameWallDecay", random(-48,48), random(-48,48), 4, 0, 0, 0, 0, 0, 160)
		TNT1 A 5
		stop
	Death.Disarm:
		RC4T A 35 A_PlaySound ("c4/disarm")
		RC4T A 1 A_FadeOut (0.05)
		wait
	}
}

// Blue-colored remote C4 for distinguishment
actor RemoteC4ArmedBlue : RemoteC4Armed {
	Translation "224:246=193:207"
}

actor C4DetonateCheck : Inventory {}