tmpx = selfx
tmpy = selfy
tmpargument = WHITE
ShowBlipXY

//Ressurected?
IfUnarmed
  tmpargument = 3
  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

IfSpawned
  tmpargument = [BOOK]
  IfHoldingItemID
    tmpargument = LATCHLEFT
    PressLatchButton

IfTimeOut
  tmpargument = rand & 31 + 15
  SetTime
  
  //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
        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
        SetTurnModeToWatchTarget
        tmpargument = 2
        SetState
      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
  IfStateIs3
    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 = BLAHITEMS
      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
      
//Handle attacking and shield use (ressurected state only!)
IfStateIs3
  IfTargetIsOnHatedTeam
    IfHoldingMeleeWeapon
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          PressLatchButton
        IfTargetIsAttacking
          IfHoldingShield
            PressLatchButton
            
//Charge missile        
IfStateIs2
  GetContent
  tmpx = tmpargument
  tmpy = 100
  IfXIsLessThanY
    tmpargument = tmpargument +1
    SetContent
    tmpargument = LATCHRIGHT
    PressLatchButton
    tmpx = targetx
    tmpy = targety
    tmpdistance = 0 - 512
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint                   //Charge enemies 
    SetOldTarget
    SetTargetToSelf
    
    //Never really run out of mana (feary light!)
    tmpx = targetmana
    tmpy = 200
    IfXIsLessThanY
      tmpargument = 128
      PumpTarget
    SetTargetToOldTarget      
  Else
    tmpargument = 0
    SetContent
    tmpargument = 1
    SetState

End
