//------------------------------------------------------------------------------
IfSpawned
  tmpargument = STATEGUARD
  SetState
  
  //Save our guard position
  tmpargument = 0
  tmpx = selfspawnx
  tmpy = selfspawny
  SetXY
  
//------------------------------------------------------------------------------
//Someone healed us
IfHealed
  tmpargument = 1
  SendMessageNear

//------------------------------------------------------------------------------
//Get mean if characters wanna be bad
IfAttacked
  Run
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    CallForHelp
    tmpargument = TEAME     //Team Evil
    TargetJoinTeam
    tmpargument = STATECOMBAT
    SetState
  tmpargument = rand & 1 + 3
  PlaySound
IfKilled
  CallForHelp			//Warn friends
  IfTargetIsAPlayer
    DisableExport
    tmpargument = TEAME
    TargetJoinTeam
  IfTargetIsOnSameTeam
    tmpargument = 3
  Else
    tmpargument = 0
  SendMessageNear
  
//------------------------------------------------------------------------------
//Somebody needs us!
IfCalledForHelp
  SetTargetToWhoeverCalledForHelp
  tmpargument = 0
  tmpx = targetx
  tmpy = targety
  SetXY               //Remember where to go
  
  tmpargument = STATECOMBAT
  SetState
  
//------------------------------------------------------------------------------
//Handle the AI
IfTimeOut
  tmpargument = 20 + rand & 15
  SetTime

  //Guard
  IfStateIsGuard
    Sneak
    SetTargetToWideEnemy		//Enemy near, go combat mode
      tmpargument = STATECOMBAT
      SetState
      CallForHelp
    Else
      tmpx = spawndistance
      tmpy = 64
      IfXIsMoreThanY              //Get back to position quickly
        Walk
        tmpx = selfspawnx
        tmpy = selfspawny
        tmpdistance = 400
        tmpturn = xyturnto
        Compass
      Else
        tmpx = selfspawnx           //Stand ground
        tmpy = selfspawny
      ClearWaypoints
      AddWaypoint
      
  //Look for enemies
  IfStateIsWander
    Walk
    SetTargetToWideEnemy
      CallForHelp
      tmpargument = STATECOMBAT
      SetState
    Else
      tmpargument = 0
      GetXY
      tmpx = rand & 2047 + tmpx - 1024
      tmpy = rand & 2047 + tmpy - 1024
      ClearWaypoints
      AddWaypoint
      
    //Check if the danger is gone or killed
    SetTargetToNearestEnemy
      tmpx = targetdistance
      tmpy = 128*15
      IfXIsMoreThanY
        tmpargument = STATEGUARD
        SetState
    Else
      tmpargument = STATEGUARD
      SetState
      
  //Combat!
  IfStateIsCombat
    Run
    SetTargetToWideEnemy
    
    //Do melee combat
    IfHoldingMeleeWeapon
      tmpx = rand & 511 + targetx - 256
      tmpy = rand & 511 + targety - 256
      tmpdistance = 600
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 300
        IfXIsLessThanY
          PressLatchButton
    
    //Do ranged combat    
    IfHoldingRangedWeapon
      SetTurnModeToWatchTarget
      tmpx = targetx
      tmpy = targety
      tmpdistance = -600
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      
      IfFacingTarget
        PressLatchButton
          
    //Return to guard mode if everything seems normal again
    SetTargetToNearestEnemy
      tmpx = targetdistance
      tmpy = 128*8
      IfXIsMoreThanY
        tmpargument = STATEWANDER
        SetState
    Else
      tmpargument = STATEWANDER
      SetState
  Else
    SetTurnModeToVelocity  

//------------------------------------------------------------------------------
//What to do if bumped
IfBumped
  SetTargetToWhoeverBumped
  IfStateIsWander
    IfTargetIsOnSameTeam
      SetTargetToOldTarget
      tmpx = rand & 511 + targetx - 256
      tmpy = rand & 511 + targety - 256
      ClearWaypoints
      AddWaypoint

//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
