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

  // Last words...
  IfTargetIsSelf
    DoNothing
  Else
    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
    
  IfStateIsParry
    tmpargument = rand % 20
    SetTime
    
    //Move
    tmpx = targetx
    tmpy = targety
    tmpdistance = 600
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    
    //Do healing
    tmpargument = STATECOMBAT
    IfTargetIsOnSameTeam
      IfTargetIsHurt
        IfTargetIsAlive
          tmpargument = STATEPARRY
          tmpx = targetdistance
          tmpy = 150
          IfXIsLessThanY
            IfFacingTarget
              tmpargument = LATCHLEFT
              PressLatchButton
              tmpargument = 256*6   //heal 6 hp
              HealTarget
              tmpargument = STATECOMBAT
    SetState
    
    
  
  IfStateIsCombat
    SetTurnModeToWatchTarget
    tmpargument = rand % 20
    SetTime
    
    //Bash stuff
    IfTargetIsOnHatedTeam
      IfFacingTarget
        tmpx = targetdistance		    // Close enough to attack?
        tmpy = 200				    //
        IfXIsLessThanY			    //
          tmpargument = LATCHRIGHT		      // Right Attack == 2
          PressLatchButton 
    Else
      SetTargetToNearbyEnemy
        DoNothing
      Else
        tmpargument = STATERETREAT
        SetState        
      
    //Move towards target
    tmpx = targetx
    tmpy = targety
    tmpdistance = -90
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    
    //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
    IfTargetIsHurt
      tmpargument = STATEPARRY
      SetState
      tmpargument = 5
      SetTime
    Else
      SetTargetToOldTarget
  Else
    tmpargument = STATEGUARD
    IfStateIsNot
      tmpargument = STATECOMBAT
      SetState
      
  //Jump over bodies
  IfTargetIsAlive
    DoNothing
  Else
    tmpargument = LATCHJUMP
    PressLatchButton
  
End					// Finished with this character
