//-------------------------------------------------------------------------------------------
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 = selfspawnx
    tmpy = selfspawny 
    ClearWaypoints
    AddWaypoint
    tmpargument = rand % 20 + 50
    SetTime
    
  IfStateIsCombat
    SetTurnModeToWatchTarget
    tmpargument = rand % 20
    SetTime
    IfTargetIsOnHatedTeam
      IfFacingTarget
        tmpx = targetdistance		    // Close enough to attack?
        tmpy = 200				    //
        IfXIsLessThanY			    //
          tmpargument = LATCHRIGHT		      // Right Attack == 2
          PressLatchButton
        
      tmpx = targetx			    // Move towards enemy
      tmpy = targety
      tmpdistance = -60
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
    Else
      tmpargument = STATERETREAT
      SetState
      
    //Draw weapon
    IfHoldingMeleeWeapon
      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
    
  //Jump over bodies
  IfTargetIsAlive
    DoNothing
  Else
    tmpargument = LATCHJUMP
    PressLatchButton
    
End					// Finished with this character
