//-------------------------------------------------------------------------------------------
IfSpawned
  tmpargument = STATEGUARD
  SetState
  KeepAction
  
//-------------------------------------------------------------------------------------------
IfOrdered
  tmpx = selforder
  tmpy = [CHES]
  IfXIsEqualToY
  
    //Activate combat AI
    tmpargument = STATECOMBAT
    SetState
    UnkeepAction
    
    //Allow death
    JoinEvilTeam
    tmpargument = 1
    ChangeArmor
    SetTargetToSelf
    EnchantTarget
    
    
  //Stop if the game is over
  tmpx = selforder
  tmpy = [DEAD]
  IfXIsEqualToY
    SetTargetToSelf
    KillTarget
    
//-------------------------------------------------------------------------------------------
IfKilled				// This reduces the height of the char
  tmpargument = 6
  PlaySound				//Death sound

  // Last words...
  IfTargetIsSelf
    DoNothing
  Else
    tmpargument = rand & 1 + 1
    IfTargetIsOnSameTeam
      tmpargument = 0
    SendMessageNear	

//-------------------------------------------------------------------------------------------
IfAttacked				// Attacked
  SetTargetToWhoeverAttacked		  //
    IfTargetIsAlive			    //
      IfTargetIsOnSameTeam
        SetTargetToOldTarget
    Else				    //Attacker died already
      SetTargetToOldTarget		      //

  //Ouch sound
  tmpargument = tmpdistance & 1 + 4
  PlaySound


//-------------------------------------------------------------------------------------------
IfTargetKilled				// Return to wander mode
  SetTargetToNearbyEnemy
    DoNothing
  Else
    tmpargument = STATERETREAT
    SetState

//-------------------------------------------------------------------------------------------
IfTimeOut				// This is done every so often

  IfStateIsGuard
    tmpx = selfx
    tmpy = selfy 
    ClearWaypoints
    AddWaypoint
    tmpargument = rand % 20 + 50
    SetTime
    
  IfStateIsCombat
    SetTurnModeToWatchTarget
    tmpargument = rand % 20
    SetTime
    IfTargetIsOnHatedTeam
      IfFacingTarget
        tmpx = targetdistance		    // Close enough to attack?
        tmpy = 128*4				    //
        IfXIsLessThanY			    //
          SetOldTarget
          SetTargetToNearestFriend
          IfFacingTarget
            DoNothing
          Else
            tmpargument = LATCHRIGHT		      // Right Attack == 2
            PressLatchButton
          SetTargetToOldTarget
          
      tmpx = targetx			    // Move towards enemy
      tmpy = targety
      tmpdistance = -128*2
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
    Else
      tmpargument = STATERETREAT
      SetState
      
    //Draw weapon
    IfHoldingRangedWeapon
      DoNothing
    Else
      tmpargument = LATCHPACKRIGHT
      PressLatchButton
    
  IfStateIsRetreat
    SetTurnModeToVelocity
    tmpx = selfspawnx
    tmpy = selfspawny 
    ClearWaypoints
    AddWaypoint
    tmpargument = rand % 20 + 50
    SetTime
    
    //Engage enemy
    SetTargetToWideEnemy
      tmpargument = STATECOMBAT
      SetState
      
//-------------------------------------------------------------------------------------------
//Attack bumpers
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnSameTeam
    SetTargetToOldTarget
  Else
    tmpargument = STATEGUARD
    IfStateIsNot
      tmpargument = STATECOMBAT
      SetState

End					// Finished with this character
