//This allows for a special ending
IfOrdered
  tmpx = selforder
  tmpy = [FAST]
  IfXIsEqualToY
    tmpargument = 1
    SetContent
    
//Do a cooldown on starting to talk
IfSpawned
  tmpargument = 150
  SetTime
  tmpargument = 0
  SetState
  SetContent

//Do the AI
IfTimeOut
  tmpx = selfstate

  //Find the player
  tmpy = 0
  IfXIsEqualToY
    tmpargument = 25
    SetTime
    tmpargument = [NONE]
    tmpdistance = BLAHFRIENDS + BLAHENEMIES + BLAHQUEST
    tmpturn = [AMAG]
    SetTargetToNearestBlahID
      tmpargument = [AMAG]
      IfTargetHasSkillID
        tmpargument = 0
        IfArmorIs
          tmpargument = 7           //An Archmage is trying again...
          SendMessage
          PlayFullSound
        tmpargument = 8
        SetState
        SetTurnModeToSpin
        tmpargument = 150
        SetTime
      Else
        tmpdistance = targetlevel
        tmpturn = 6                //Need to be level 8 at least
        IfDistanceIsMoreThanTurn
          SetTurnModeToWatchTarget
          tmpargument = 1
          SetState
        Else
          tmpargument = 0
          IfArmorIs
            tmpargument = 19      //Too low level!
            SendMessage
            tmpargument = 8
            PlayFullSound   //BAD: NEED SOUND!!
          tmpargument = 8
          SetState
          SetTurnModeToSpin
          tmpargument = 150
          SetTime
    Else
      tmpargument = 0
      IfArmorIs
        tmpargument = 8         //They don't have the quest!!
        SendMessage
        PlayFullSound
      tmpargument = 8
      SetState
      SetTurnModeToSpin
      tmpargument = 150
      SetTime

  //Introdouce
  tmpy = 1
  IfXIsEqualToY
    tmpargument = 2
    SetState
    tmpargument = 100
    SetTime
    
    tmpargument = 0
    IfArmorIs
      tmpargument = 0
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction

    
  //Introdouce part 2
  tmpy = 2
  IfXIsEqualToY
    tmpargument = 3
    SetState
    tmpargument = 150
    SetTime  
    
    tmpargument = 1
    IfArmorIs
      tmpargument = 1
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //Tell them what it's about
  tmpy = 3
  IfXIsEqualToY
    tmpargument = 4
    SetState
    tmpargument = 200
    SetTime  
    
    tmpargument = 2
    IfArmorIs
      tmpargument = 2
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //Tell them what it's about
  tmpy = 4
  IfXIsEqualToY
    tmpargument = 5
    SetState
    tmpargument = 175
    SetTime  
    
    tmpargument = 0
    IfArmorIs
      tmpargument = 3
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //Tell them what it's about (still doing it)
  tmpy = 5
  IfXIsEqualToY
    tmpargument = 6
    SetState
    tmpargument = 175
    SetTime  
    
    tmpargument = 1
    IfArmorIs
      tmpargument = 4
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction

  //Tell them what it's about (yep, still)
  tmpy = 6
  IfXIsEqualToY
    tmpargument = 7
    SetState
    tmpargument = 225
    SetTime  
    
    tmpargument = 2
    IfArmorIs
      tmpargument = 5
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //Tell the test I - addmitance
  tmpy = 7
  IfXIsEqualToY
    tmpargument = 8
    SetState
    tmpargument = 200
    SetTime
    
    SetTurnModeToSpin
    
    tmpargument = 0
    IfArmorIs
      tmpargument = 6
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
      //Spawn the exit switch
      tmpargument = 38
      tmpx = 2432
      tmpy = 6720
      tmpdistance = 0
      tmpturn = 32768   //East
      SpawnExactCharacterXYZ
      tmpargument = 1
      SetChildState
      
  //Teleport away
  tmpy = 8
  IfXIsEqualToY
    tmpargument = 9
    SetState
    tmpargument = 200
    SetTime
    
    //Teleport sound
    tmpargument = 0
    IfArmorIs
      tmpargument = 13
      PlaySound
      
    //Teleport to end room
    SpawnPoof
    tmpx = 4550
    tmpy = selfy - 1500
    Teleport
    SetTurnModeToVelocity

  //Wait for players to enter the final room
  tmpy = 9
  IfXIsEqualToY
    tmpargument = 50
    SetTime
 
    tmpdistance = 128*5
    SetTargetToDistantEnemy
      SetTurnModeToWatchTarget
      tmpargument = 10
      SetState
      
  //Congratulate them
  tmpy = 10
  IfXIsEqualToY
    tmpargument = 11
    SetState
    tmpargument = 100
    SetTime
    
    tmpargument = 0
    IfArmorIs
      tmpargument = 9
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
    
  //You can now become...
  tmpy = 11
  IfXIsEqualToY    
    tmpargument = 12
    SetState
    tmpargument = 100
    SetTime
    
    tmpargument = 1
    IfArmorIs
      tmpargument = 1
      IfContentIs
        tmpargument = 17      //Special ending
      Else
        tmpargument = 10
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //Archmage!
  tmpy = 12
  IfXIsEqualToY    
    tmpargument = 13
    SetState
    tmpargument = 200
    SetTime
    
    tmpargument = 2
    IfArmorIs
      tmpargument = 1
      IfContentIs
        tmpargument = 18      //Special ending
      Else
        tmpargument = 11
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //But it is permanent, so warn them!
  tmpy = 13
  IfXIsEqualToY    
    tmpargument = 14
    SetState
    tmpargument = 200
    SetTime
    
    tmpargument = 0
    IfArmorIs
      tmpargument = 12
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //Give some time to reconsider... who the heck does?
  tmpy = 14
  IfXIsEqualToY    
    tmpargument = 15
    SetState
    tmpargument = 200
    SetTime
    tmpargument = 0
    IfArmorIs
      tmpargument = 13
      SendMessage
    
  //LAST CHANCE TO QUIT
  tmpy = 15
  IfXIsEqualToY    
    tmpargument = 16
    SetState
    tmpargument = 250
    SetTime
    tmpargument = 0
    IfArmorIs
      tmpargument = 14
      SendMessage
      
  //Let it be done then
  tmpy = 16
  IfXIsEqualToY
    tmpargument = 17
    SetState
    tmpargument = 225
    SetTime
    SetTurnModeToSpin
    
    tmpargument = 1
    IfArmorIs
      tmpargument = 15
      SendMessage
      PlayFullSound
      tmpargument = ACTIONMC
      DoAction
      
  //ACTUALLY DO THE TRANSFORMATION AND GO AWAY
  tmpy = 17
  IfXIsEqualToY

    tmpargument = 0
    IfArmorIs

      tmpargument = [WIZA]      //Wizard Archmage
      IfTargetHasID
        tmpargument = 121
        ChangeTargetClass
        
      tmpargument = [ADVE]      //Adventurer Archmage
      IfTargetHasID
        tmpargument = 122
        ChangeTargetClass
        
      tmpargument = [ELFF]      //Elf Archmage
      IfTargetHasID
        tmpargument = 123
        ChangeTargetClass

      //Refill target life and mana first
      tmpargument = 12800   //50 should be enough
      HealTarget
      PumpTarget
      
      //------------------------------------------------------------------------
      //Permanently adjust some stats
      tmpargument = [ADVE]
      IfTargetHasID
        tmpargument = -768               //Adventurers lose 3 life
        GiveLifeToTarget
      
      tmpargument = 2560
      GiveIntelligenceToTarget            //Give 10 intelligence
      GiveWisdomToTarget                  //Give 10 wisdom
      
      tmpargument = 898                   //Give 3.5 friggin mana flow!
      GiveManaFlowToTarget
    
      tmpargument = [ELFF]
      IfTargetHasID
        tmpargument = -768                //Elves lose 3 mana return
        GiveManaReturnToTarget
      
      tmpargument = 1260                  //Give 5 mana
      GiveManaToTarget
      
      tmpargument = targetdex > 3
      tmpargument = -tmpargument
      GiveDexterityToTarget               //Reduce dexterity by 25%
      //------------------------------------------------------------------------
        
      //Victory tune
      tmpargument = 19
      PlayMusic
      
      //Actually beat it
      EnableExport
      BeatModule
      tmpargument = [BEAT]
      AddIDSZ
      tmpargument = [AMAG]
      BeatQuestAllPlayers
      
      //Award some XP
      tmpargument = EXPQUEST
      tmpdistance = 100
      GiveExperienceToTarget

      //Tell them it's done
      tmpargument = 16
      SendMessage
      PlayFullSound
      
      //End text
      ClearEndMessage
      tmpargument = 20
      AddEndMessage
      tmpargument = 21
      AddEndMessage
      tmpargument = 22
      AddEndMessage
      
      //Add special ending
      tmpargument = 1
      IfContentIs
        tmpargument = 23
        AddEndMessage
        tmpargument = 24
        AddEndMessage
      
      //Spelly effects (pillar of power)
      tmpargument = 0
      tmpx = targetx
      tmpy = targety
      tmpdistance = targetz
      SpawnExactParticle
    
    //Teleport sound
    tmpargument = 0
    IfArmorIs
      tmpargument = 13
      PlaySound

    SpawnPoof
    GoPoof
    
//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
