//------------------------------------------------------------------------------
//Use shields
IfTargetIsAttacking
  IfHoldingShield
    IfTargetIsOnHatedTeam
      tmpx = targetdistance
      tmpy = 500
      IfXIsLessThanY
        IfFacingTarget
          tmpargument = LATCHLEFT
          PressLatchButton

//------------------------------------------------------------------------------
//Get out of those ugly cubes!
IfSitting
  SetOldTarget
  SetTargetToWhoeverIsHolding
  tmpargument = [CUBE]
  IfTargetHasID
    tmpargument = LATCHJUMP
    PressLatchButton
  SetTargetToOldTarget

//------------------------------------------------------------------------------
//Join the noble saviours!
IfOrdered
  tmpx = selforder
  tmpy = [FREE]
  IfXIsEqualToY
    JoinGoodTeam
    SetTargetToNearestFriend
    AddStat
    tmpargument = 8
    PlayFullSound
    SendMessage
    tmpargument = 3             //Follow mode
    SetState
    tmpargument = 70
    tmpdistance = EXPDARE
    GiveExperienceToGoodTeam
    SetOwnerToTarget            //Make the nearest saviour the new boss

//------------------------------------------------------------------------------
//Jump over bodies and obstacles
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAPlatform
    tmpargument = [CUBE]		//Dont jump into gelfeets!
    IfTargetHasID
      DoNothing
    Else
      tmpargument = LATCHJUMP
      PressLatchButton

//------------------------------------------------------------------------------
//Move out of lava
IfOverWater
  tmpx = ownerx
  tmpy = ownery
  ClearWaypoints
  AddWaypoint
  tmpargument = LATCHJUMP
  PressLatchButton

//------------------------------------------------------------------------------
//Show where you are on the map
tmpx = selfstate
tmpy = 2
IfXIsMoreThanY
  tmpx = selfx
  tmpy = selfy
  tmpargument = 4
  ShowBlipXY

//------------------------------------------------------------------------------
//Main AI loop
IfTimeOut
  tmpargument = 50
  SetTime

  //Tell the player to release him (or else!)
  tmpx = selfstate
  tmpy = 3
  IfXIsLessThanY
    tmpargument = 250
    SetTime
    tmpdistance = 128*7
    SetTargetToDistantEnemy                //SetTargetToWhoeverIsInPassage
      IfTargetIsAPlayer
        IfStateIs0
          tmpargument = 7
          PlayFullSound
          SendMessage
          tmpargument = 1
          SetState
        Else
          IfStateIs1
            tmpargument = rand & 2 + 4
            PlayFullSound
            SendMessage
            tmpargument = 0
            SetState
    Else
      IfStateIs1
        tmpargument = 9
        PlaySound
        SendMessage
    SetTurnModeToWatchTarget

  //When not imprisoned, draw our gear
  Else
    IfHoldingMeleeWeapon
      IfHoldingShield
        DoNothing
      Else
        tmpargument = LATCHPACKLEFT		//shield
        PressLatchButton
    Else
      tmpargument = LATCHPACKRIGHT		//weapon
      PressLatchButton


  //Follow
  IfStateIs3
    tmpargument = rand & 15 + 35
    SetTime
    SetTargetToOwner
    tmpx = targetdistance
    tmpy = 1500
    IfXIsLessThanY                  //Follow player if he is far away
      SetTargetToWideEnemy
        tmpargument = [CUBE]
        IfTargetHasID			//Don't attack cubes
          SetTargetToOwner
        Else
          tmpargument = 4
          SetState

    tmpx = rand & 255 + ownerx - 128
    tmpy = rand & 255 + ownery - 128
    tmpdistance = 0 - 100
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint

  //Combat Mode
  IfStateIs4
    tmpargument = rand & 15 + 15
    SetTime
    tmpx = targetdistance
    tmpy = 250
    IfXIsMoreThanY              //Never run away from melee
      SetTargetToOwner
      tmpx = targetdistance
      tmpy = 1500
      IfXIsMoreThanY            //See if we need to go after the player
        tmpargument = 3
        SetState

    //We are still in combat! Do the usual combat stuff
    IfStateIs4
      SetTargetToWideEnemy
      tmpargument = [CUBE]
      IfTargetHasID				//Keep away from cubes
        tmpx = rand & 511 + ownerx - 256
        tmpy = rand & 511 + ownery - 256
        tmpturn = targetturnto
        tmpdistance = 700
        Compass
        ClearWaypoints
        AddWaypoint
      Else
        tmpx = rand & 511 + targetx - 256
        tmpy = rand & 511 + targety - 256
        tmpturn = targetturnto
        tmpdistance = 700
        Compass
        ClearWaypoints
        AddWaypoint

        //Attack!
        IfHoldingMeleeWeapon
          IfFacingTarget
            tmpx = targetdistance
            tmpy = 200
            IfXIsLessThanY
              PressLatchButton

        //Randomly growl combat stuff (this guy has anger issues!)
        tmpx = rand & 255
        tmpy = 15
        IfXIsLessThanY
          tmpargument = rand & 3 + 11
          PlaySound
          SendMessageNear

      //No more enemies around
      Else
        tmpargument = 3
        SetState

        //End of combat cheer! (33% chance)
        tmpx = rand % 3
        tmpy = 0
        IfXIsEqualToY
          tmpargument = rand & 2 + 15
          PlaySound
          SendMessageNear

//------------------------------------------------------------------------------
//Handle death by sending a message and other stuff
IfKilled
  tmpargument = 1
  PlaySound			//death cry

  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
    IfTargetIsSelf
      tmpargument = 2
  SendMessage

  // Drop goodies
  tmpargument = selfmoney
  DropItems
  DropMoney
  DropKeys

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  //Make no body! (for ressurections)
  GoPoof

//------------------------------------------------------------------------------
//Say ouch
IfAttacked
  SetTargetToWhoeverAttacked
  tmpargument = rand & 1 + 1
  PlaySound

  //Become angry when friendly fire at low life
  IfTargetIsOnSameTeam
    tmpx = selflife
    tmpy = 1024
    IfXIsLessThanY
      tmpargument = TEAML
      JoinTeam
      tmpargument = 18
      SendMessage

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