actor SentryGun
{
  obituary "%o was ventilated by an auto-sentry."
  health 100
  radius 20
  height 56
  mass 10//100000
  speed 0
  MaxTargetRange 300
  MinMissileChance 0
  MeleeRange 0
  attacksound "sentry/attack"
  MONSTER
  +FLOORCLIP
  +FRIENDLY
  +NOBLOOD
  +USESPECIAL
  //+LOOKALLAROUND
  +NEVERRESPAWN
  +STANDSTILL
  states
  {
  Spawn:
    TNT1 A 0
    //Use door open script to make this drop a SentryBox when we activate it:
    SENT A 0 Thing_SetSpecial(tid, 226, 935, tid, 0)
  SpawnLook:
    TNT1 A 0
	//300 units max distance to see, 0 fov = 180 FOV
	SENT AAAAA 3 A_LookEx(0, 0, 300, 300, 0, "See")
	loop
  Active:
  Open:
    TNT1 A 0 //Dummy frame is needed for A_Jump to work
    //Don't actually open unless we have the player as our target, so
    //we can detect when they walk away.
    SENT A 0 Thing_SetSpecial(tid, 0, 0, 0, 0) //Prevent it from being activated again - Important!
    SENT C 0 A_PlaySoundEx ("Sentry/Death", "SoundSlot5")
    SENT C 3 A_SpawnItemEx("SentryBox", 0.0, 0.0, 0.0, 0.0, 0.0, 8.0)
    Stop
  See:
    TNT1 A 0
    SENT AAAAA 3 A_Chase(0, "Missile", CHF_DONTMOVE)
    SENT A 0 A_GiveInventory("Forgettimer", 1) // A dummy inventory for tracking how long the imp has been searching.
    SENT A 0 A_JumpIfInventory("Forgettimer", 4, "Forget") // Jump to the Forget state every 2 seconds
    loop
  Missile:
    SENT A 1 A_FaceTarget
    SENT B 2 bright A_CustomBulletAttack(3, 0, 2, random(1,5), "SentryPuff", 0, CBAF_NORANDOM)
	SENT A 1 A_MonsterRefire(0, "See")
    SENT A 0 A_GiveInventory("Forgettimer", 1) // A dummy inventory for tracking how long the imp has been searching.
    SENT A 0 A_JumpIfInventory("Forgettimer", 10, "Forget") // Jump to the Forget state every 5 seconds 
	goto Missile+1
	//SENT A 2 A_CposRefire
	//goto See
  Forget:
     SENT A 0 A_TakeInventory("Forgettimer", 0x7FFFFFFF)
     SENT A 3 A_ClearTarget
     Goto SpawnLook
  Death:
    SENT C 0
    SENT C 0 A_Die
    SENT C 0 Thing_SetSpecial(0,0,0,0,0) //Prevent activation again
    SENT C 1 A_PlaySoundEx ("Sentry/Death", "SoundSlot5")
    SENT C 0 A_PlaySoundEx ("weapons/rocklx", "SoundSlot6")
    SENT C 0 A_SpawnItem ("SentryBoom", 0, 48)
    SENT C 512
  FadeOut:
    SENT C 2 A_FadeOut (0.1)
    loop
  }
}

actor AdvancedSentryGun
{
  obituary "%o was ventilated by an auto-sentry."
  health 300
  radius 20
  height 56
  mass 200
  speed 0
  MaxTargetRange 500
  MinMissileChance 0
  MeleeRange 0
  attacksound "sentry/attack"
  DamageType "SentryTurret"
  MONSTER
  +FLOORCLIP
  +FRIENDLY
  +NOBLOOD
  +USESPECIAL
  //+LOOKALLAROUND
  +NEVERRESPAWN
  +STANDSTILL
  states
  {
  Spawn:
    TNT1 A 0
    //Use door open script to make this drop a SentryBox when we activate it:
    SENT A 0 Thing_SetSpecial(tid, 226, 935, tid, 0)
  SpawnReset:
    TNT1 A 0 A_SetArg(4, 100)
  SpawnLook:
    //Oscillate back and forth
    SENT A 3 A_SetAngle(360*sin(args[4]))
    SENT A 0 A_SetArg(4, args[4] + 5)
    SENT A 0 A_JumpIf(args[4] == 0, "Spawn")
    //600 units max distance to see for advanced turrets
	SENT AAAAA 3 A_LookEx(0, 0, 600, 600, 360, "See") //360 degree FOV for advanced turrets
	loop
  Active:
  Open:
    TNT1 A 0 //Dummy frame is needed for A_Jump to work
    //Don't actually open unless we have the player as our target, so
    //we can detect when they walk away.
    SENT A 0 Thing_SetSpecial(tid, 0, 0, 0, 0) //Prevent it from being activated again - Important!
    SENT C 0 A_PlaySoundEx ("Sentry/Death", "SoundSlot5")
    SENT C 3 A_SpawnItemEx("AdvancedSentryBox", 0.0, 0.0, 0.0, 0.0, 0.0, 8.0)
    Stop
  See:
    TNT1 A 0
    SENT AAAAA 3 A_Chase(0, "Missile", CHF_DONTMOVE)
    SENT A 0 A_GiveInventory("Forgettimer", 1) // A dummy inventory for tracking how long the imp has been searching.
    SENT A 0 A_JumpIfInventory("Forgettimer", 4, "Forget") // Jump to the Forget state every 2 seconds
    loop
  Missile:
    SENT A 1 A_FaceTarget
    SENT B 2 bright A_CustomBulletAttack(3, 0, 2, random(3,8), "SentryPuff", 0, CBAF_NORANDOM)
	SENT A 1 A_MonsterRefire(0, "See")
    SENT A 0 A_GiveInventory("Forgettimer", 1) // A dummy inventory for tracking how long the imp has been searching.
    SENT A 0 A_JumpIfInventory("Forgettimer", 10, "Forget") // Jump to the Forget state every 5 seconds 
	goto Missile+1
	//SENT A 2 A_CposRefire
	//goto See
  Forget:
     SENT A 0 A_TakeInventory("Forgettimer", 0x7FFFFFFF)
     SENT A 3 A_ClearTarget
     Goto SpawnReset
  Death:
    SENT C 0 
    SENT C 0 A_Die
    SENT C 0 Thing_SetSpecial(0,0,0,0,0) //Prevent activation again
    SENT C 1 A_PlaySoundEx ("Sentry/Death", "SoundSlot5")
    SENT C 0 A_PlaySoundEx ("weapons/rocklx", "SoundSlot6")
    SENT C 0 A_SpawnItem ("SentryBoom", 0, 48)
    SENT C 512
  FadeOut:
    SENT C 2 A_FadeOut (0.1)
    loop
  }
}

actor SentryPuff : BulletPuff
{
  +PUFFONACTORS
  +NOEXTREMEDEATH //prevent gibs
  //SeeSound "weapons/axecut"
  //AttackSound "weapons/axebang"
  ActiveSound "silent"
  DamageType "SentryTurret"

  States
  {
  Spawn:
    PUFF ABCD 4
    Stop
  }
}

actor EnemySentryPuff : SentryPuff
{
  DamageType "EnemySentryTurret"
}

actor SentryBoom
{
  radius 2
  height 2
  renderstyle Add
  +NOINTERACTION
  +CLIENTSIDEONLY
  states
  {
  Spawn:
    XPL1 ABCDEF 3 bright
    stop
  }
}

ACTOR SentryBox : CustomInventory 12369
{
  +COUNTITEM
  +INVENTORY.INVBAR
  Inventory.MaxAmount 5
  Inventory.Icon "SENBB0"
  Inventory.PickupMessage "You picked up a sentry gun! Nice!"
  States
  {
  Spawn:
    SENB A -1
    Stop
  Use:
    TNT1 A 1 A_SpawnItemEx ("SentryGun", 56, 0, 8, 0, 0, 0, 0, 1)
    Stop
  }
}

ACTOR AdvancedSentryBox : CustomInventory 12369
{
  +COUNTITEM
  +INVENTORY.INVBAR
  Inventory.MaxAmount 5
  Inventory.Icon "SENCB0"
  Inventory.PickupMessage "You picked up an advanced sentry gun! Gnarly!"
  States
  {
  Spawn:
    SENC A -1
    Stop
  Use:
    TNT1 A 1 A_SpawnItemEx ("AdvancedSentryGun", 56, 0, 8, 0, 0, 0, 0, 1)
    Stop
  }
}

actor Forgettimer : Inventory 
{
	Inventory.MaxAmount 0x7FFFFFFF
} /* Dummy item */



actor EnemySentryGun : SentryGun
{
  obituary "%o was ventilated by an enemy auto-sentry."
  ActiveSound "Sentry/Active"

  Translation "112:127=152:191"


  States
  {
  //Need to redefine this state to use EnemySentryPuff, which does damage to players, unlike
  //the regular SentryPuff.

  See:
    TNT1 A 0
    SENT A 12 A_ActiveSound
    goto Super::See
  Missile:
    SENT A 1 A_FaceTarget
    SENT B 2 bright A_CustomBulletAttack(3, 0, 2, random(1,3), "EnemySentryPuff", 0, CBAF_NORANDOM)
	SENT A 1 A_MonsterRefire(0, "See")
    SENT A 0 A_GiveInventory("Forgettimer", 1) // A dummy inventory for tracking how long the imp has been searching.
    SENT A 0 A_JumpIfInventory("Forgettimer", 10, "Forget") // Jump to the Forget state every 5 seconds 
	goto Missile+1  
  }
}
