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

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

actor RemoteC4 : AOWWeapon {
    Weapon.BobSpeed 1.75
    Weapon.BobRangeX 0.2
    Weapon.BobRangeY 0.4
    Weapon.BobStyle Alpha
	+NOALERT
	Obituary "%o bonks %k with a 100kg Remote C4."
	Weapon.AmmoType "RemoteC4Ammo"
	weapon.upsound "weapons/up/remotec4"
	Weapon.AmmoUse 1
	Weapon.AmmoGive 5
	States {
	Select:
	    TNT1 A 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "SelectFull")
		RC4G B 1 offset (90,100)
		RC4G B 1 offset (80,89)
		RC4G B 1 offset (70,79)
		RC4G B 1 offset (60,70)
		RC4G B 1 offset (50,62)
		RC4G B 1 offset (40,55)
		RC4G B 1 offset (30,49)
		RC4G B 1 offset (20,44)
		RC4G B 1 offset (15,40)
		RC4G B 1 offset (10,37)
		RC4G B 1 offset (5,34)
		RC4G B 1 offset (0,0)
		TNT1 A 0 A_TakeInventory("SelectAnimation",1)	
		goto ready
    SelectFull:
	    RC4G A 1 offset (90,100)
		RC4G A 1 offset (80,89)
		RC4G A 1 offset (70,79)
		RC4G A 1 offset (60,70)
		RC4G A 1 offset (50,62)
		RC4G A 1 offset (40,55)
		RC4G A 1 offset (30,49)
		RC4G A 1 offset (20,44)
		RC4G A 1 offset (15,40)
		RC4G A 1 offset (10,37)
		RC4G A 1 offset (5,34)
		RC4G A 1 offset (0,0)
		TNT1 A 0 A_TakeInventory("SelectAnimation",1)	
		goto ready
    Deselect:
	    TNT1 A 0 A_TakeInventory("ActionFragGrenade",1)	
        TNT1 A 0 A_TakeInventory("ActionFlashbang",1)
        TNT1 A 0 A_TakeInventory("ActionRemoteC4",1)	
        TNT1 A 0 A_TakeInventory("ActionMeleeAttack",1)
		TNT1 A 0 A_TakeInventory("ActionReload",1)	
		TNT1 A 0 A_JumpIfInventory ("RemoteC4Ammo", 1, "DeselectFull")
	    RC4G B 1 offset (0,34)
		RC4G B 1 offset (0,40)
		RC4G B 1 offset (0,49)
		RC4G B 1 offset (0,62)
		RC4G B 1 offset (0,79)
		RC4G B 1 offset (0,100)
		TNT1 A 0 offset (0,33)
		TNT1 A 0 A_Lower
		Wait
    DeselectFull:
	    RC4G A 1 offset (0,34)
		RC4G A 1 offset (0,40)
		RC4G A 1 offset (0,49)
		RC4G A 1 offset (0,62)
		RC4G A 1 offset (0,79)
		RC4G A 1 offset (0,100)
		TNT1 A 0 offset (0,33)
		TNT1 A 0 A_Lower
		Wait	
	Ready:
	    TNT1 A 0 A_JumpIfInventory("SelectAnimation",1,"Select")
		RC4G B 0 A_JumpIfInventory("RemoteC4Ammo", 1, "ReadyFull")
		TNT1 A 0 A_JumpIfInventory("ActionFragGrenade",1,"UseFragGrenade")
		TNT1 A 0 A_JumpIfInventory("ActionFlashbang",1,"UseFlashbang")
		TNT1 A 0 A_JumpIfInventory("ActionRemoteC4",1,"AltFire")
		TNT1 A 0 A_JumpIfInventory("ActionMeleeAttack",1,"UseMeleeAttack")
		RC4G BBBB 1 A_WeaponReady (WRF_NOPRIMARY)
		loop
	ReadyFull:
		TNT1 A 0 A_JumpIfInventory("ActionFragGrenade",1,"UseFragGrenade")
		TNT1 A 0 A_JumpIfInventory("ActionFlashbang",1,"UseFlashbang")
		TNT1 A 0 A_JumpIfInventory("ActionRemoteC4",1,"AltFire")
		TNT1 A 0 A_JumpIfInventory("ActionMeleeAttack",1,"UseMeleeAttack")
		RC4G AAAA 1 A_WeaponReady
		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 A_TakeInventory("ActionRemoteC4",1)
		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 A_TakeInventory("ActionRemoteC4",1)
		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 20 A_NoGravity // Wait a moment and then become armed
		RC4T A 0 A_JumpIfInTargetInventory ("IsBlue", 1, "Death.Blue")
		RC4T A 0 A_SpawnItemEx ("RemoteC4Armed", 0, 0, 0, 0, 0, 0, 0, SXF_TRANSFERPOINTERS)
		stop
	Death.Blue:
		RC4T A 0 A_SpawnItemEx ("RemoteC4ArmedBlue", 0, 0, 0, 0, 0, 0, 0, SXF_TRANSFERPOINTERS)
		stop
	}
}

actor RemoteC4Armed : SwitchableDecoration {
	+NOEXTREMEDEATH
	+NOGRAVITY
	+NOBLOOD
	+SHOOTABLE
	+NOTARGETSWITCH
    +USESPECIAL
    Activation THINGSPEC_Activate
	Health 10
	Radius 12
	Height 12
	Mass 0x7FFFFFFF
	Speed 0
	Scale 0.2
	Damage 0
	DamageType "Explosion"
	Obituary "$OB_REMOTEC4"

	DamageFactor "Flash",			0.0
	DamageFactor "Fist",			0.0
	DamageFactor "Knife",			1.0
	DamageFactor "Bullet",			0.0
	DamageFactor "Rifle",			0.0
	DamageFactor "Explosion",		0.0
	DamageFactor "MassDriver",		0.0
	DamageFactor "AntiArmor",		0.0
	DamageFactor "Mine",			0.0
	DamageFactor "Fire",			0.0
	DamageFactor "FireSpecial",		0.0
	DamageFactor "Chemical",		0.0
	DamageFactor "ChemicalSpecial",	0.0
	DamageFactor "Energy",			1.0
	DamageFactor "Plasma",			0.0
	DamageFactor "EnergyDecay",		0.0
	DamageFactor "Ice",				0.0
	DamageFactor "Time",			0.0
	DamageFactor "Suicide",         0.0
	DamageFactor "Superweapon",		0.0
	DamageFactor "Disarm",			1.0
	DamageFactor "Deconstruction",	0.0
	DamageFactor "Mechstomp",		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
	Active:
	    TNT1 A 0 A_JumpIfInTargetInventory ("IsDemoExpert", 1, 2)
		TNT1 A 0
		goto Denied
		RC4T A 2
		TNT1 A 0 A_ChangeFLag ("SHOOTABLE", 0)
		TNT1 A 0 A_PlaySound("terrorist/select")
		TNT1 A 0 A_SpawnItemEx ("RemoteC4Pickup",0,0,0,0,0,0,0,SXF_NOCHECKPOSITION,0)
		TNT1 A 0
		Stop
	Denied:
	    RC4T A 20 A_NoGravity // Wait a moment and then become armed
		RC4T A 0 A_SpawnItemEx ("RemoteC4Armed", 0, 0, 0, 0, 0, 0, 0, SXF_TRANSFERPOINTERS)
		stop
	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, SXF_TRANSFERPOINTERS, 160)
		TNT1 A 5
		stop
	Death.Knife:
	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"
	
	States {
	Active:
	    TNT1 A 0 A_JumpIfInTargetInventory ("IsDemoExpert", 1, 2)
		TNT1 A 0
		goto Denied
		RC4T A 2
		TNT1 A 0 A_ChangeFLag ("SHOOTABLE", 0)
		TNT1 A 0 A_PlaySound("terrorist/select")
		TNT1 A 0 A_SpawnItemEx ("RemoteC4Pickup",0,0,0,0,0,0,0,SXF_NOCHECKPOSITION,0)
		TNT1 A 0
		Stop
	Denied:
	    RC4T A 20 A_NoGravity // Wait a moment and then become armed
		RC4T A 0 A_SpawnItemEx ("RemoteC4ArmedBlue", 0, 0, 0, 0, 0, 0, 0, SXF_TRANSFERPOINTERS)
		stop
	}
}

actor C4DetonateCheck : Inventory {}

actor RemoteC4Pickup : CustomInventory {
	inventory.pickupmessage "Remote C4 charge"
	inventory.maxamount 0
	inventory.pickupsound "c4/remote"
	Scale 0.2
	Translation "224:246=16:31"
	states {
    Spawn:
		RC4T AAAAAAAAAA 35
		goto Death.Disarm
	Pickup:
		TNT1 A 0 A_GiveInventory ("RemoteC4Ammo", 1)
		stop
	Death.Disarm:
		RC4T A 35 A_PlaySound ("c4/disarm")
		RC4T A 1 A_FadeOut (0.05)
		wait
	}
}
