tmpx = selfx
tmpy = selfy
tmpargument = WHITE
ShowBlipXY

//Ressurected?
IfUnarmed
  tmpargument = 2
  SetState

//Ouch
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    tmpargument = 3
  Else
    tmpargument = 4
  PlaySound
  
//Yah!
IfUsed
  tmpargument = 5
  PlaySound
  
//Death stuff
IfKilled
  tmpargument = selfmoney
  DropMoney
  
  //Sound
  tmpargument = 1
  PlaySound
  
  //Message
  IfTargetIsSelf
    tmpargument = 0
  Else
    tmpargument = 1
  SendMessage

//Use shield and attack
IfTargetIsOnHatedTeam
  IfFacingTarget
    tmpx = targetdistance
    tmpy = 130
    IfXIsLessThanY
      IfHoldingMeleeWeapon
        PressLatchButton
      IfTargetIsAttacking
        tmpargument = [SHIE]
        IfHoldingItemID
          PressLatchButton
 
          
//Main AI loop
IfTimeOut
  tmpargument = rand & 31 + 15
  SetTime
  
  tmpx = selflife
  tmpy = 1500           //Less than 6 hp
  IfXIsLessThanY
    tmpargument = 1 
  Else
    tmpargument = 0
  ChangeArmor

  //Follow boss
  IfStateIs0
    SetTurnModeToVelocity
    SetTargetToNearbyEnemy
      tmpargument = [LOCK]
      IfTargetHasID         //Dont attack lurking mimics
        GetTargetState
        tmpx = tmpargument
        tmpy = 1
        IfXIsEqualToY       //Its moving! Attack!
          tmpargument = 1
          SetState
        Else                //We know nothin..
          tmpx = leaderx
          tmpy = leadery
          ClearWaypoints
          AddWaypoint
      Else                  //It's not a mimic - Charge!
        tmpargument = 1
        SetState    
    Else
      tmpx = leaderdistance
      tmpy = 375
      IfXIsLessThanY
        
        //Drink healing potions, we have three to start with
        SetTargetToSelf
        tmpx = targetlife
        tmpy = 1024            //Only 4 hp left or less
        IfXIsLessThanY
          GetContent            //Content = number of potions
          tmpx = tmpargument
          tmpy = 0
          IfXIsMoreThanY
            tmpargument = tmpargument -1
            SetContent                      //Drink one
            tmpdistance = SPAWNORIGIN
            tmpargument = 2
            SpawnAttachedParticle
            tmpargument = rand & 1023 + 512
            HealTarget                     //Healing!
            tmpargument = 3
            SendMessageNear
        tmpx = rand & 511 + selfx - 256
        tmpy = rand & 511 + selfy - 256
      Else
        tmpx = leaderx
        tmpy = leadery
        tmpdistance = 1500
        SetTargetToLeader
        tmpturn = targetturnto
        Compass
      ClearWaypoints
      AddWaypoint
      
      
  //Combat
  IfStateIs1
    SetTargetToNearbyEnemy      //Don't blindly follow our boss while we are under attack
      tmpx = 0
    Else
      tmpx = leaderdistance
    tmpy = 800
    IfXIsLessThanY                //Follow boss if he is far away
      SetTargetToWideEnemy
        tmpx = targetx
        tmpy = targety
        tmpdistance = 1500
        tmpturn = targetturnto
        ClearWaypoints
        AddWaypoint
      Else
        tmpargument = 0
        SetState                  //No enemies around anymore
    Else
      tmpx = leaderx
      tmpy = leadery
      tmpdistance = 1500
      SetTargetToLeader
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint                     //Return to leader
      tmpargument = 0
      SetState

  //Ressurected AI
  IfStateIs2
    SetTurnModeToVelocity
    SetTargetToNearbyEnemy      //Don't blindly follow our boss while we are under attack
      tmpx = 0
    Else
      tmpx = leaderdistance
    tmpy = 375
    IfXIsLessThanY
      tmpx = rand & 511 + selfx - 256
      tmpy = rand & 511 + selfy - 256
      SetTargetToNearbyEnemy            //Go combat!
        tmpx = targetx
        tmpy = targety
        tmpdistance = 500
        tmpturn = targetturn
        Compass
    Else
      tmpx = leaderx
      tmpy = leadery
      tmpdistance = 1500
      SetTargetToLeader
      tmpturn = targetturnto
      Compass
    
    //Get a shield if possible
    IfHoldingShield
      DoNothing
    Else
      tmpargument = 0
      SetXY                 //Remember waypoint if fails
      tmpdistance = 8       //Items
      tmpargument = [SHIE]
      SetTargetToWideBlahID
        tmpx = targetdistance
        tmpy = 75
        IfXIsLessThanY
          tmpargument = LATCHALTLEFT
          PressLatchButton
        tmpx = targetx
        tmpy = targety
      Else
        tmpargument = 0
        GetXY                   //Get the previous waypoint, we failed

    //First priority is to find a weapon
    IfHoldingMeleeWeapon
      DoNothing
    Else
      tmpargument = 0
      SetXY                 //Store previous waypoint if fails
      tmpx = 0
      tmpdistance = 8           //Items
      tmpargument = [KNIF]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [HAMM]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [SPEA]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [TORC]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [AXEE]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [WHIP]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [MACE]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [POLE]
      SetTargetToWideBlahID
        tmpx = 1
      tmpargument = [SWOR]
      SetTargetToWideBlahID
        tmpx = 1
       
      //Found something?
      tmpy = 1
      IfXIsEqualToY
        tmpx = targetdistance
        tmpy = 75
        IfXIsLessThanY
          tmpargument = LATCHALTRIGHT
          PressLatchButton
        tmpx = targetx            //Move towards it
        tmpy = targety
      Else
        tmpargument = 0         //Get the previous stored waypoint if failed
        GetXY
          
    ClearWaypoints
    AddWaypoint
      

End
