//Healed
IfHealed
  tmpargument = 3
  SendMessageNear

//-------------------------------------------------------------------------------------------
//Setup the level
IfSpawned
  tmpargument = passage
  ClosePassage

  //Play music theme
  tmpdistance = 0
  tmpargument = 7
  PlayMusic

//Show where we are!
tmpx = selfx
tmpy = selfy
tmpargument = RED
ShowBlipXY

//-------------------------------------------------------------------------------------------
//Die and all that dramatic stuff
IfKilled
  tmpargument = passage
  OpenPassage				//Open the treasure gate

  //Tell what happened
  tmpargument = 0
  SendMessage
  tmpargument = 5
  PlaySound

  //Yummy!
  tmpargument = selfmoney
  DropItems
  DropMoney
  DropKeys

  //victory tune
  tmpargument = 14
  tmpdistance = 0
  PlayMusic

  //Beat Quest and award xp
  tmpargument = 150
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam
  BeatModule
  tmpargument = [BEAT]
  AddIDSZ
  EnableExport
  
  //Add the end text
  ClearEndMessage
  tmpargument = 5
  AddEndMessage
  tmpargument = 6
  AddEndMessage
  tmpargument = 7
  AddEndMessage


//-------------------------------------------------------------------------------------------
//Ouch and teleport away
IfAttacked
  tmpargument = rand & 3 + 2
  PlaySound

  
  //See if mask falls off
  tmpargument = 0
  IfArmorIs
    SetTargetToSelf
    tmpx = targetlife
    tmpy = 3072				//Half life
    IfXIsLessThanY
      tmpargument = 1
      ChangeArmor
      tmpargument = 4
      SendMessageNear      
  SetTargetToWhoeverAttacked
  

  //Teleport away
  IfTargetIsOnHatedTeam
    tmpargument = 0
    tmpdistance = SPAWNORIGIN
    SpawnParticle
    tmpx = rand & 511 - 256 + selfx		//Randomize position
    tmpy = rand & 511 - 256 + selfy
    Teleport
    tmpx = rand & 511 - 256 + selfx		//Randomize position 2nd time
    tmpy = rand & 511 - 256 + selfy
    Teleport
    tmpx = targetx
    tmpy = targety
    ClearWaypoints				//Face target
    AddWaypoint
  Else
    SetTargetToNearestEnemy

//-------------------------------------------------------------------------------------------
//Main AI loop
IfTimeOut

  //Wait for good guys
  IfStateIs0
    tmpargument = 0
    SetContent
    tmpargument = 30
    SetTime
    tmpx = selfspawnx
    tmpy = selfspawny			//Return to start point
    ClearWaypoints
    AddWaypoint
    SetTargetToWideEnemy		//Found one!
      tmpargument = 1
      SetState
      tmpargument = 6
      PlayFullSound			//"I will kill you!"
      tmpargument = 1
      SendMessage
      tmpargument = 16			//Play boss tune
      tmpdistance = 0
      PlayMusic
    Else
      tmpargument = 7			//No enemies, play normal music
      tmpdistance = 0
      PlayMusic

  //Combat mode
  IfStateIs1
    tmpargument = rand & 15 + 25
    SetTime
    SetTargetToNearestEnemy
      tmpx = targetx
      tmpy = targety			//Move towards them
      ClearWaypoints
      AddWaypoint

      tmpx = targetdistance		//If far away, cast bind spell
      tmpy = 400
      IfXIsMoreThanY
        tmpargument = 0
        IfContentIs
          tmpargument = 2
          SetState
      Else
        tmpx = rand & 1
        tmpy = 0			//50% chance to cast
        IfXIsEqualToY
          tmpargument = LATCHRIGHT	//Burning hands
          PressLatchButton
    Else
      tmpargument = 0			//Enemies are gone
      SetState

  //Casting bind spell
  IfStateIs2
    SetTurnModeToWatchTarget
    tmpx = targetx
    tmpy = targety			//Move towards them
    tmpdistance = 0 - 500
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint

    tmpargument = 0
    SetTime
    tmpargument = LATCHLEFT		//Keep chargin
    PressLatchButton
    GetContent
    tmpargument = tmpargument + 1
    SetContent				//Keep track on how much time has elasped
    tmpx = tmpargument
    tmpy = 195
    IfXIsMoreThanY			//Enough charge now already!
      tmpargument = 1
      SetState
  Else
    SetTurnModeToVelocity
    GetContent
    tmpx = tmpargument
    tmpy = 0
    IfXIsMoreThanY			//reduce charge
      tmpargument = tmpx - 2
      SetContent

  //Drink mana potion when needed
  tmpx = selfmana
  tmpy = 256
  IfXIsLessThanY
    SetTargetToSelf
    tmpargument = rand & 2047 + 2048
    PumpTarget				//regain mana
    tmpargument = 0 - 32
    GiveManaToTarget			//lose max mana
    SetTargetToOldTarget
    tmpargument = 8
    PlaySound				//trinkle
    tmpargument = 2
    SendMessageNear
    tmpargument = ACTIONMC
    TargetDoAction
    tmpdistance = SPAWNORIGIN		//sparkle
    tmpargument = 1
    SpawnAttachedHolderParticle

End
