//------------------------------------------------------------------------------
//What the states actually mean:
//STATEWANDER - Default state, decide what to do next
//STATEPARRY - Summon groundspike
//STATECOMBAT - Attack in melee or ranged
//STATECHARGE - Summon monsters
//STATEGUARD - Imprisoned (can't do anything)
//STATERETREAT - Summoning a fire rain!

//------------------------------------------------------------------------------
//Special powers and tactics:
//Ranged Attack - Permanent Life Drain + poison! (also STR drain) <- cannot be dispelled without arch-magic
//Melee Attack - Drains life
//Summon spikes in the ground - Dazes and Grogs
//Moves in specific pattern once in a while
//Summon monsters
//Channels a meteor swarm or two
//Shoots missiles that drain mana

//------------------------------------------------------------------------------
//Handle opening and locking of the 4 locks
IfOrdered
  tmpx = selforder
  tmpy = [LOCK]
  IfXIsEqualToY
    tmpargument = 0
    GetXY
    tmpx = tmpx + 1
    SetXY
    
  tmpx = selforder
  tmpy = [OPEN]
  IfXIsEqualToY
    tmpargument = 0
    GetXY
    tmpx = tmpx - 1
    SetXY
    
    //They have released it!
    tmpy = 0
    IfXIsEqualToY
      tmpargument = 2
      PlayFullSound
      tmpargument = 250
      SetTime
      
//------------------------------------------------------------------------------
IfSpawned
  tmpargument = 16          //Boss music
  PlayMusic

  //Keep track of which locks are engaged
  tmpargument = 0
  tmpx = 2            //Curent locked
  tmpy = 4            //Goal
  SetXY
  
  //Introduce ourselves
  EnableInvictus
  tmpargument = 75
  SetTime
  
  //Imprisoned state
  tmpargument = STATEGUARD
  SetState
  DropItems
  tmpargument = 128
  SetAlpha
    
  //Set the timer
  tmpdistance = BLAHFRIENDS + BLAHENEMIES + BLAHQUEST
  tmpturn = [IMPD]
  SetTargetToNearestBlahID          
  IfTargetHasQuest                  //Get the current quest level
    tmpargument = tmpdistance       //quest level = time left
  Else
    tmpargument = 50*60*15          //Default to 15 minutes
  SetContent
    
//------------------------------------------------------------------------------
//What? No! I dont even...
IfKilled
  DropItems
  DropKeys
  tmpargument = selfmoney
  DropMoney
  
  //Beat it
  BeatModule
  tmpargument = [BEAT]
  AddIDSZ
  
  //Victory tune
  tmpargument = 19
  PlayMusic
  
  //Add end text for slaying the beast
  ClearEndMessage
  tmpargument = 14
  AddEndMessage
  tmpargument = 15
  AddEndMessage
  tmpargument = 12
  AddEndMessage
  tmpargument = 13
  AddEndMessage
  tmpargument = 17
  AddEndMessage
  
  //Tag them as Slayer of Demogorgon!
  tmpargument = [DEMO]    
  tmpdistance = 0
  AddQuestAllPlayers
    
//------------------------------------------------------------------------------
//Grunt in pain
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpargument = rand % 3 + 12       //Ouch sound, new enemy
    PlaySound
    IfStateIsWander
      tmpargument = STATECOMBAT
      SetState                      //uh oh, big dodo now!
  Else
    SetTargetToOldTarget
    
//------------------------------------------------------------------------------
//The main AI loop, here is where the most of the stuff happens...
IfTimeOut
  UnkeepAction
  tmpargument = rand & 15 + 30
  SetTime
  
  //Imprisoned
  IfStateIsGuard
    tmpx = rand & 255
    tmpy = 15
    IfXIsLessThanY
      //Break free animation
      tmpargument = ACTIONMC    
      DoAction
      
    //Introduce ourselves
    tmpargument = 1
    GetXY
    tmpy = 0
    IfXIsEqualToY
      tmpx = 1
      SetXY
      tmpargument = 2
      SendMessage
      tmpargument = 6
      PlayFullSound
      tmpargument = 1000
      SetTime
      
    //Randomly tell the player how to proceed
    Else
      tmpx = rand & 255
      tmpy = 35
      IfXIsLessThanY
        
        //Randomize between two different lines
        tmpx = rand & 1
        tmpy = 0
        IfXIsEqualToY
          tmpargument = 7
          PlayFullSound
          tmpargument = 4
          SendMessage
        Else 
          tmpargument = 8
          PlayFullSound
          tmpargument = 3
          SendMessage
        tmpargument = 300
        SetTime
      
    //Check for release
    tmpargument = 0
    IfContentIs                       //Has the time run out?
      //This disables the prison switches
      tmpargument = [DEMO]
      tmpdistance = [SWIT]
      OrderSpecialID

      //Taunt
      tmpargument = 1
      SendMessage
      tmpargument = 3
      PlayFullSound
      
      //Ready the "vengance" threats
      tmpargument = 1
      tmpx = 2
      tmpy = 0
      SetXY
            
      //Make ourselves visible
      tmpargument = 1
      ChangeArmor

      //Break free animation
      tmpargument = ACTIONMC    
      DoAction
      
      //Begin combat
      tmpargument = 300
      SetTime
      tmpargument = STATEWANDER
    Else
      GetXY
      IfXIsEqualToY             //All locks enganged?
        //Beat it
        BeatModule
        tmpargument = [BEAT]
        AddIDSZ
                          
        //Quest XP!
        tmpargument = 500
        tmpdistance = EXPQUEST
        GiveExperienceToGoodTeam

        //This disables the prison switches
        tmpargument = [DEMO]
        tmpdistance = [SWIT]
        OrderSpecialID
        
        //Death cry
        tmpargument = 9
        SendMessage
        tmpargument = 4
        PlayFullSound
        
        //Victory tune
        tmpargument = 19
        PlayMusic
        
        //Add end text for imprisoning the beast
        ClearEndMessage
        tmpargument = 11
        AddEndMessage
        tmpargument = 12
        AddEndMessage
        tmpargument = 13
        AddEndMessage
        tmpargument = 16
        AddEndMessage
        
        //Remember their victory
        tmpargument = [DEMO]
        tmpdistance = 0
        AddQuestAllPlayers

        GoPoof
      Else
        tmpy = 0
        IfXIsEqualToY                 //All locks open? (release the beast!)
          
          //Make ourselves visible
          tmpargument = 1
          ChangeArmor
          
          //Award for being a bit brave
          tmpargument = 200
          tmpdistance = EXPDARE
          GiveExperienceToGoodTeam
          
          //This disables the prison switches
          tmpargument = [DEMO]
          tmpdistance = [SWIT]
          OrderSpecialID
        
          //Break free animation
          tmpargument = ACTIONMC    
          DoAction
        
          //Talk a bit
          tmpargument = 5
          SendMessage
          PlayFullSound
          
          //Go crazy!
          tmpargument = 300
          SetTime
          tmpargument = STATEWANDER
        Else
          tmpargument = STATEGUARD    //Nope, still imprisoned
  
  //Attack either melee or ranged
  IfStateIsCombat
    SetTurnModeToWatchTarget
    tmpx = targetx
    tmpy = targety
    tmpturn = targetturnto
    tmpdistance = 600
    Compass
    ClearWaypoints
    AddWaypoint
    
    //Pick an appropiate attack maneuver
    IfFacingTarget
      tmpx = targetdistance
      tmpy = 250
      IfXIsLessThanY
        tmpargument = ACTIONUA        //Melee attack
        DoAction
      Else
        tmpx = rand & 255             //How often to do this attack
        tmpy = 128
        IfXIsLessThanY
          tmpargument = ACTIONLA        //Ranged attack
          DoAction
          
            //Spawn the ranged attack particle
            tmpargument = 1
            tmpx = selfx
            tmpy = selfy
            tmpdistance = 128
            tmpturn = selfturn
            Compass
            tmpdistance = selfz
            SpawnExactParticle
        Else
          //Shoot a mana draining homing missile
          tmpargument = ACTIONUA
          DoAction
          tmpargument = 5
          tmpdistance = selfz
          tmpturn = selfturn
          tmpx = selfx
          tmpy = selfy
          SpawnExactParticle
            
      tmpx = rand & 1
      tmpy = 0
      IfXIsEqualToY
        tmpargument = STATEWANDER             //We got an attack, decide next move
      Else
        tmpargument = STATECOMBAT
    Else
      tmpargument = STATECOMBAT
  Else
    SetTurnModeToVelocity
    
  //Summon a groundspike
  IfStateIsParry
    tmpx = selfx
    tmpy = selfy
    tmpdistance = 50
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
        
    //Spawn a demonic spike
    tmpargument = 39
//    tmpx = rand & 127 + targetx - 64
//    tmpy = rand & 127 + targety - 64
    tmpx = targetx + targetspeedx               //Calculate where our enemy is going to move next!
    tmpy = targety + targetspeedy
    tmpdistance = 0
    SpawnExactCharacterXYZ
    
    //Do the correct animation
    tmpargument = ACTIONZA
    DoActionOverride
    KeepAction
    
    tmpargument = 75
    SetTime
    
    tmpargument = STATEWANDER
    
  //Summon monster
  IfStateIsCharge
    tmpargument = 90
    SetTime
        
    //Summon another monster
    tmpargument = rand % 9 + 40     //randomize monster type
    tmpx = selfx
    tmpy = selfy
    tmpturn = selfturn
    tmpdistance = 200
    Compass
    SpawnExactCharacterXYZ
    
      //Summon monster animation
      tmpargument = ACTIONMJ
      DoActionOverride

      //Spelly effect
      tmpargument = 0
      SpawnExactParticle
      SetTargetToChild
      tmpargument = 0
      SendMessageNear
      tmpargument = 1
      PlaySound
    
    tmpargument = STATEWANDER
  
  //Firestorm!
  IfStateIsRetreat
    tmpargument = 52
    tmpx = selfx
    tmpy = selfy
    tmpdistance = selfz
    SpawnExactCharacterXYZ
      tmpargument = ACTIONMC    
      DoActionOverride
      tmpargument = 10
      SendMessage
      
      tmpargument = 100
      SetTime
    
    tmpargument = STATEWANDER
   
  //Default state
  IfStateIsWander
    tmpx = rand & 511 + selfx - 256
    tmpy = rand & 511 + selfy - 256
    ClearWaypoints
    AddWaypoint
    
    //Randomly growl
    tmpx = rand & 255
    tmpy = 35
    IfXIsLessThanY
      tmpargument = 15
      PlaySound
    
    //Declare our intentions
    tmpargument = 1
    GetXY
    tmpy = 1
    IfXIsEqualToY                       //Tell them of their "reward"
      tmpx = 3
      SetXY
      tmpargument = 7
      SendMessage
      tmpargument = 9
      PlayFullSound
      tmpargument = 150
      SetTime
      DisableInvictus
    Else
      tmpy = 2
      IfXIsEqualToY                     //Threaten the players
        tmpx = 3
        SetXY
        tmpargument = 6
        SendMessage
        tmpargument = 10
        PlayFullSound
        tmpargument = 150
        SetTime 
        DisableInvictus
    
    //Here we decide what tactic we use if we encounter a enemy
    tmpargument = [NONE]
    tmpdistance = BLAHENEMIES + BLAHSKILL
    tmpturn = [WMAG]
    SetTargetToWideBlahID             //Get those juciy arcane casters first
      DoNothing
    Else
      tmpturn = [HMAG]
      SetTargetToWideBlahID          //Then go for the healers
        DoNothing
      Else
        SetTargetToNearestEnemy       //Last we finish what is left
    
    
    //Did we find an enemy to attack?
    IfTargetIsOnHatedTeam
    
      //Pick either a spike or melee/ranged attack
      tmpx = rand & 255
      tmpy = 80
      IfXIsLessThanY
        tmpargument = STATEPARRY        //Spike
      Else
        tmpargument = STATECOMBAT       //Melee/Ranged
        
      //Decide if we summon another monster
      tmpx = rand % 10
      tmpy = 5 - selfmorale
      IfXIsLessThanY
        tmpargument = STATECHARGE
        
      //Decide if we summon a firestorm!
      tmpx = rand & 255
      tmpy = 50
      IfXIsLessThanY
        tmpargument = STATERETREAT
        
    //No enemy found, continue
    Else
      tmpargument = STATEWANDER         
      
  SetState
  
//------------------------------------------------------------------------------
//Do the countdown timer
IfStateIsGuard
  tmpx = selfcontent
  tmpy = 0
  IfXIsMoreThanY
    tmpargument = selfcontent - 1
    SetContent
    ShowTimer
    
//------------------------------------------------------------------------------
//This throws enemies back when they get hit
IfScoredAHit
  SetTargetToWhoeverWasHit
  IfTargetIsOnHatedTeam
    tmpx = targetdistance
    tmpy = 300
    IfXIsLessThanY
      tmpx = 0
      tmpy = 0
      tmpturn = selfturn
      tmpdistance = 400
      Compass
      AccelerateTarget
  Else
    SetTargetToOldTarget            //bah, friendly fire
//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
