//State 0 = mounted
IfDismounted
  tmpargument = 1
  SetState
IfSitting
  IfStateIs1
    tmpargument = 0
    SetState
    SetTargetToWhoeverIsHolding
    SetOwnerToTarget            //Make this your mount

IfSpawned
  tmpargument = 150
  SetTime                       //Give some time to get on the mount
  tmpargument = 1
  SetState
  
  //Assume that we spawn on a mount
  SetTargetToNearestFriend
  SetOwnerToTarget

//------------------------------------------------------------------------------
// Jump over barriers, onto mounts
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAlive
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      IfTargetIsAlive
        IfTargetIsAPlatform
          // Jump over some stuff
          tmpargument = LATCHJUMP
          PressLatchButton
        IfTargetIsAMount
          // Jump onto mounts
          tmpx = targetx
          tmpy = targety
          ClearWaypoints
          AddWaypoint
          tmpargument = 30
          SetTime
    Else
      // Jump over corpses
      tmpargument = LATCHJUMP
      PressLatchButton
  SetTargetToOldTarget

//------------------------------------------------------------------------------
IfKilled
  // Tell the players
  tmpargument = 0
  SendMessageNear

  //Death sound
  tmpargument = 0
  PlaySound

  // Make the character body
  tmpargument = 40
  SetBumpHeight

  GoPoof
  SpawnPoof

//If someone blocks, there is a chance we get thrown of the mount
IfTargetIsDefending
  IfScoredAHit
    SetTargetToNearbyEnemy
    tmpx = targetstr > 8
    tmpy = selfstr > 8
    tmpx = rand & 15 + tmpx
    tmpy = rand & 15 + tmpy
    IfXIsMoreThanY
      tmpargument = LATCHJUMP
      PressLatchButton

//------------------------------------------------------------------------------
IfAttacked
  SetTargetToWhoeverAttacked
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      SetTargetToOldTarget
  //Play ouch sound
  tmpargument = rand & 1 + 1
  PlaySound

//Don't hack dead bodies
IfTargetIsAlive
  DoNothing
Else
  tmpargument = 0
  SetState

//------------------------------------------------------------------------------
//This is the Main AI loop
IfTimeOut

  // Riding some type of animal...
  IfStateIs0
    tmpargument = rand & 15 + 15 // Time
    SetTime

    // Acquire a target
    SetTargetToNearbyEnemy
      SetTurnModeToWatchTarget
    Else
      SetTurnModeToVelocity
    SetTargetToWideEnemy
      tmpx = targetdistance
      tmpy = 250
      IfXIsMoreThanY
        // Charge towards enemy (lance!)
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 1500
        tmpargument = 75
        SetTime
      Else
        //Move away for a mounted charge
        tmpx = targetx
        tmpy = targety
        tmpturn = rand
        tmpdistance = -1500
      Compass
      ClearWaypoints
      AddWaypoint

      // Check for special mount attacks
      tmpx = targetdistance
      tmpy = 200
      IfXIsLessThanY
        tmpargument = [XFUN]
        SetOldTarget
        SetTargetToWhoeverIsHolding
        IfTargetHasAnyID
          tmpargument = LATCHRIGHT
          PressLatchButton
          SetTargetToOldTarget
    Else
      // Guard the start position
      tmpx = rand & 511 - 256 + selfspawnx
      tmpy = rand & 511 - 256 + selfspawny
      ClearWaypoints
      AddWaypoint

  //Unmounted!
  IfStateIs1
    tmpargument = rand & 15 + 15 // Time
    SetTime

    //Find a mount!
    tmpdistance = 0         //If tmpdistance = 1 we go foot soldier tactic
    SetTargetToOwner
    IfTargetIsAlive
      IfTargetIsOnHatedTeam           //They took our mount!
        SetTargetToTargetLeftHand
          tmpdistance = 1
    Else
      tmpdistance = 1                 //They killed our mount!
    tmpturn = 0
    IfDistanceIsMoreThanTurn
      SetTargetToWideEnemy
        tmpx = targetdistance
        tmpy = 350
        IfXIsLessThanY
          IfHoldingMeleeWeapon
            PressLatchButton
        tmpx = targetx
        tmpy = targety
      Else
        tmpx = rand & 511 + selfx - 256
        tmpy = rand & 511 + selfy - 256
      ClearWaypoints
      AddWaypoint
    Else
      tmpx = ownerx
      tmpy = ownery
      tmpdistance = 1500
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
