//---------------------------------------------------------------------------------------
IfSpawned
  tmpargument = STATEGUARD
  SetState

//---------------------------------------------------------------------------------------
// Handle death
IfKilled
  IfTargetIsSelf
    tmpargument = 1
  Else
    tmpargument = 0
  SendMessageNear

  tmpargument = 30
  SetBumpSize
  tmpargument = 40
  SetBumpHeight
  tmpargument = 25
  SetShadowSize
  tmpargument = 1
  ChangeArmor

  tmpargument = 0
  PlaySound
//---------------------------------------------------------------------------------------
// Make noise
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    SetTargetToOldTarget        //ooops friendly
  tmpargument = rand & 1 + 1
  PlaySound

//---------------------------------------------------------------------------------------
//Recharge brain attack
tmpx = selfcontent
tmpy = 0
IfXIsMoreThanY
  tmpargument = selfcontent -1
  SetContent
  
//---------------------------------------------------------------------------------------
//Handle Backstabs
IfBackstabbed
  SetTargetToWhoeverAttacked
  GetDamageType
  tmpdistance = tmpargument       //Same damage type as the stab
  tmpargument = targetdex         //+1 damage per dex
  TargetDamageSelf
  tmpargument = 4
  SendMessageNear
  
//---------------------------------------------------------------------------------------
IfBumped
  SetTargetToWhoeverBumped
  
  // Scooch around
  tmpx = rand & 255 + targetx - 128
  tmpy = rand & 255 + targety - 128
  ClearWaypoints
  AddWaypoint

  //Suck their brains
  IfTargetIsOnHatedTeam
    tmpargument = 0
    IfContentIs
      tmpargument = ACTIONUA
      DoAction
      EnchantTarget     
      tmpargument = 50
      SetContent
      tmpargument = 3
      PlaySound
      tmpargument = rand & 255 + 128
      DamageTarget
      
      tmpx = targetx
      tmpy = targety
      tmpdistance = targetz + 64
      tmpargument = 2
      SpawnExactParticle
      SpawnExactParticle
      
      //Did we suck it all out?
      tmpx = targetint
      tmpy = 256
      IfXIsLessThanY
        tmpargument = 2
        SendMessageNear
        KillTarget                      //insta kill.. ouch!
        tmpargument = STATEGUARD
        SetState
        tmpargument = 1+targetlevel*128   //Heal 1/2 hp per level
        HealSelf
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz + 64
        tmpargument = 2
        SpawnExactParticle
        SpawnExactParticle
        SpawnExactParticle
        SpawnExactParticle
        SpawnExactParticle
        SpawnExactParticle
      Else
        tmpargument = 3
        SendMessageNear           //Still working on it...
  Else
    SetTargetToOldTarget

//---------------------------------------------------------------------------------------
// Find enemies
IfTimeOut
  tmpargument = rand & 15 + 25
  SetTime

  IfStateIsGuard
    SetTurnModeToVelocity
    tmpx = rand & 511 - 256 + selfspawnx
    tmpy = rand & 511 - 256 + selfspawny      
    ClearWaypoints
    AddWaypoint

    SetTargetToWideEnemy
      tmpargument = STATECOMBAT
      SetState
      tmpargument = 4     //battle cry
      PlaySound

  IfStateIsCombat
    SetTurnModeToWatchTarget
    tmpx = targetdistance
    tmpy = 200
    IfXIsLessThanY
      tmpargument = STATECHARGE         //attack if they come too close
      SetState
    Else
      tmpx = targetx
      tmpy = targety
      tmpturn = rand % 32000 + targetturnto - 16000
      tmpdistance = -600                //stay at a distance and bombard them
      Compass
      ClearWaypoints
      AddWaypoint
      
    //Charge em if they are confuzzled
    tmpx = 0
    GetTargetDazeTime
      tmpx = 1
    GetTargetGrogTime
      tmpx = 1
    tmpy = 0
    IfXIsMoreThanY
      tmpargument = STATECHARGE
      SetState
      
    //Confuse ray
    tmpargument = 0
    IfContentIs
      tmpx = targetdistance
      tmpy = 256
      IfXIsMoreThanY
        tmpargument = 0
        tmpdistance = SPAWNORIGIN
        tmpx = 0
        tmpy = 0
        SpawnParticle
        tmpargument = 60
        SetContent
        
    //Target enemies with the biggest brain (most INT)
    SetOldTarget
    tmpx = targetint
    SetTargetToWideEnemy
      tmpy = targetint
      IfXIsMoreThanY
        SetTargetToOldTarget      
      
  IfStateIsCharge
    SetTurnModeToVelocity
    tmpx = targetx
    tmpy = targety
    tmpturn = targetturnto
    tmpdistance = 1500      
    Compass
    ClearWaypoints
    AddWaypoint
    
    //Keep distance and confuse them again!
    tmpx = 0
    GetTargetDazeTime
      tmpx = 1
    GetTargetGrogTime
      tmpx = 1
    tmpy = 0
    IfXIsEqualToY
      tmpx = targetdistance
      tmpy = 200
      IfXIsMoreThanY
        tmpargument = STATECOMBAT
        SetState                            //they ran away
      Else
        tmpx = selflife
        tmpy = 256*3
        IfXIsLessThanY
          tmpargument = STATECOMBAT         //Retreat
          SetState
      
//---------------------------------------------------------------------------------------
//Hit someone with confusion ray
IfScoredAHit
  SetTargetToWhoeverWasHit
  IfTargetIsAPlayer
    tmpargument = 5
    SendMessageNear
  tmpargument = STATECHARGE
  SetState
  
//---------------------------------------------------------------------------------------
//We killed em
IfTargetKilled
  tmpargument = STATEGUARD
  SetState  
    
// All done
End
