//---------------------------------------------------------------------------
//THE DRACOLICH - by Zefz
//The dracolich can die more than once. Each time it dies its attacks become more deadly
//but it's max hp is lowered. Life and mana is fully healed. Each death will increase
//resistance to all physical attacks, but lower resistance against spells.
//Each time it is killed, the player must fight a miniboss before the bigboss fight
//continues. The boss must be killed 4 times before it is all over.
//Tactics:
//3 Right hand item holds spells too: (25% chance to always use one of the * list)
//  * Lightning bolt
//  * Deathcloud
//  * Cone of cold
//  * Disenchant enemies
//5  Charge and claw! (50% permanent life drain and 50% touch of death?)
//4  Teleport behind enemy when he is defending/not moving
//6  Life Drain
//7  Telekinesis (push target away, throw, minor damage)
//8  Paralysis spell
//9  Cast armageddon spell (360 degrees fireballs)
//10 Become invisible
//11 Summon attacking blades (telekinesis?)
//12 Mana Drain
//13 Disintergrate enemy items

//? Teleport enemy!

//SetXY and GetXY stores a couple of values:
//0 = invisibillity timer (how many ticks left)
//1 = last XY coordinates enemy was seen
//2 = have we met the player before? (true if X=1)
//3 = how many minibosses has the player defeated?
//4 = parry cooldown timer
//---------------------------------------------------------------------------
//Get past obstacles
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAlive
    IfTargetIsOnSameTeam
      tmpx = rand & 511 + targetx - 256		//Move around friends
      tmpy = rand & 511 + targety - 256
      ClearWaypoints
      AddWaypoint
      SetTargetToOldTarget
  Else
    tmpargument = LATCHJUMP	//Jump over bodies
    PressLatchButton
    SetTargetToOldTarget

//---------------------------------------------------------------------------
//Haha! Player was killed
IfTargetIsAPlayer
  IfTargetKilled
    tmpargument = rand % 6 + 15
    PlayFullSound
    tmpargument = tmpargument - 10
    SendMessage
    tmpargument = 0
    IfStateIsNot
      SetTargetToWideEnemy
        DoNothing
      Else
        tmpargument = 1
        SetState
    tmpargument = ACTIONMC
    DoAction			//Laugh
    tmpargument = 50
    SetTime

//---------------------------------------------------------------------------
//Say ouch
IfAttacked
  tmpargument = rand & 3 + 11
  PlaySound

  //Disrupt casting
  tmpx = selfstate
  tmpy = 2
  IfXIsMoreThanY
    tmpargument = 2
    SetState
    SetTurnModeToVelocity

//---------------------------------------------------------------------------
//Death is usually not permanent for this guy
IfKilled
  tmpargument = 50
  SetTime

  //Return to life! mohaha
  tmpargument = 3
  IfArmorIs
    tmpargument = 2000              //Final death!
    tmpdistance = EXPQUEST
    GiveExperienceToGoodTeam

    tmpargument = 28
    PlayFullSound
    tmpargument = 16
    SendMessage

    tmpargument = selfmoney
    DropMoney

    tmpargument = 255
    SetLight

    tmpargument = 30
    SetBumpHeight

    //This makes lord bishop saveable
    tmpargument = [VICT]
    tmpdistance = [VICT]
    OrderSpecialID

    //Credits music
    tmpargument = 19
    tmpdistance = 0
    PlayMusic
  Else
    SpawnPoof
    RespawnCharacter               //Just another death
    SpawnPoof
    tmpargument = 0
    PlayFullSound

    //Reduce max life
    SetTargetToSelf
    tmpargument = 0 - 256*10    //lose 10 life points
    GiveLifeToTarget

    //Reset some values
    tmpargument = 0
    SetState
    SetContent
    tmpx = selfspawnx
    tmpy = selfspawny
    ClearWaypoints
    AddWaypoint
    SetTurnModeToVelocity
    UnkeepAction

    //Change to the next state of death
    tmpargument = 0
    IfArmorIs
      tmpargument = 25
      PlayFullSound
      tmpargument = 13
      SendMessage
      tmpargument = 1       //Become weakened
    Else
      tmpargument = 1
      IfArmorIs
        tmpargument = 26
        PlayFullSound
        tmpargument = 14
        SendMessage
        tmpargument = 2     //Become frail skeleton
      Else
        tmpargument = 27
        PlayFullSound
        tmpargument = 15
        SendMessage
        tmpargument = 225
        SetLight
        tmpargument = 3     //Become a mere shade
    ChangeArmor

//---------------------------------------------------------------------------
//Main AI loop
IfTimeOut
  tmpargument = rand & 15 + 15
  SetTime

  //This engages a miniboss battle
  IfStateIs0
    tmpargument = 200
    SetFlyHeight
    tmpdistance = 128*6
    SetTargetToDistantEnemy
      SetTurnModeToWatchTarget
      tmpargument = 2
      GetXY
      tmpy = 0
      IfXIsMoreThanY
        tmpy = 2
        IfXIsEqualToY
          tmpargument = 0
          PlayFullSound
          tmpx = rand & 3 + 80 - 2 < 7
          tmpy = rand & 3 + 50 - 2 < 7
          tmpturn = targetturn
          tmpdistance = 800             //Teleport em in the middle of the boss room
          TeleportTarget

          tmpargument = 3
          GetXY
          tmpy = 0
          IfXIsEqualToY
            tmpargument = [SCOR]      //Bring alive the mana scoprion!
          tmpy = 1
          IfXIsEqualToY
            tmpargument = [WIER]      //Throw the bludwierd at them...
          tmpy = 2
          IfXIsEqualToY
            tmpargument = [WATC]      //Now the giga watcher
          tmpy = 3
          IfXIsEqualToY
            tmpargument = [DKNI]      //Finally the dark knight
          IssueOrder
        Else
          tmpx = 2
          SetXY
          tmpargument = 22
          PlayFullSound			//Witness my vast arcane energies (start battle!)
          tmpargument = 12
          SendMessage
          tmpargument = 350
          SetDamageTime
          SetTime
      Else
        tmpx = 1
        SetXY
        tmpargument = 21
        PlayFullSound
        tmpargument = 11		//Welcome to my domain mortals
        SendMessage
        tmpargument = 150
        SetDamageTime
        SetTime
  Else
    tmpargument = 0
    SetFlyHeight

  //Wait for enemy
  IfStateIs1
    SetTurnModeToVelocity
    tmpargument = 5
    SetTime
    SetTargetToWideEnemy		//Passage and all
      tmpargument = 2
      SetState
    Else
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint
      tmpx = gotodistance
      tmpy = 200
      IfXIsMoreThanY
        tmpx = rand & 511 + selfspawnx - 256
        tmpy = rand & 511 + selfspawny - 256
        ClearWaypoints
        AddWaypoint
        tmpargument = LATCHJUMP
        PressLatchButton

  //Engage enemy
  IfStateIs2
    IfTargetIsOnHatedTeam		//Remember enemies
      SetOldTarget
    Else
      SetTargetToNearestEnemy		//First timer
      SetOldTarget

    SetTargetToNearestEnemy
      IfFacingTarget			//Closer and in sight is always better
        DoNothing
      Else
        SetTargetToOldTarget		//Rather keep enemies in sight as the target
      tmpx = targetdistance
      tmpy = 350
      IfXIsMoreThanY
        tmpargument = LATCHJUMP	//Charge approach
        PressLatchButton
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 1500
        Compass
      Else
        tmpx = targetx		//Normal approach
        tmpy = targety
    Else
      tmpargument = 1
      GetXY
    ClearWaypoints
    AddWaypoint

    tmpargument = 1
    SetXY

    //Do a random attack
    IfTargetIsOnHatedTeam
      IfFacingTarget
        tmpx = rand & 3
        tmpy = 0
        IfXIsEqualToY			//Always 25% to use dracotoy
          tmpargument = 3
        Else
          tmpargument = 0
          IfArmorIs
            tmpargument = rand % 5 + 3		//Half of the spells
          Else
            tmpargument = 1
            IfArmorIs
              tmpargument = rand % 7 + 3	//66% of the spells
            Else
              tmpargument = 2
              IfArmorIs
                tmpargument = rand % 9 + 3	//88% of the spells
              Else
                tmpargument = rand % 11 + 3	//All the spells!
        SetState


          //Long timeout
          tmpargument = 75
          SetTime

          //This enables spellcasting
          tmpargument = 1
          SetContent

          //If already invisible, dont cast invisibillity
          IfInvisible
            IfStateIs10
              tmpargument = 4     //Do sneaky teleport instead
              SetState

          //Charge and claw!
          IfStateIs5
            tmpx = targetx
            tmpy = targety
            tmpturn = targetturnto
            tmpdistance = 1500
            Compass
            ClearWaypoints
            AddWaypoint

            tmpargument = 1
            SetState

          //Sneaky teleport
          IfStateIs4
            tmpx = targetx
            tmpy = targety
            tmpturn = targetturnto
            tmpdistance = 600
            Compass
            SpawnPoof
            tmpargument = 0
            PlaySound
            Teleport
            SpawnPoof
            tmpargument = 0
            PlaySound

            tmpargument = 1
            SetState

          //Mana Drain (do before life drain)
          IfStateIs12
            tmpx = targetmana
            tmpy = 512
            IfXIsMoreThanY            //Don't waste time if the target doesnt have mana
              tmpargument = 10
              tmpx = targetx
              tmpy = targety
              tmpdistance = targetz
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle            //10 particles
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              tmpargument = 6
              PlaySound

              //Now steal some mana
              tmpargument = 1536        //Drain 6 mana
              CostTargetMana
              tmpargument = 512         //Gain 2 life
              HealSelf

              tmpargument = 2
              SetState
            Else
              tmpargument = 6         //Target doesnt have mana
              SetState                //Do life drain instead

          //Life Drain
          IfStateIs6
            tmpargument = 9
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle            //10 particles
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle
            SpawnExactParticle
            tmpargument = 5
            PlaySound

            //Now steal some life
            tmpargument = DAMAGEEVIL
            SetDamageType
            tmpargument = 1024        //Deal 4 damage
            DamageTarget
            tmpargument = 512         //Gain 2 life
            HealSelf

            tmpargument = 2
            SetState

//---------------------------------------------------------------------------
//Join the battle when one of the sub-bosses die
IfOrdered
  tmpx = selforder
  tmpy = [BDIE]
  IfXIsEqualToY         //Yup, someone died
    tmpargument = 3
    GetXY
    tmpy = 4
    IfXIsLessThanY          //The limit
      tmpx = tmpx + 1
      tmpy = 0
      SetXY
    tmpargument = 1         //Prepeare for combat
    SetState
//---------------------------------------------------------------------------
//Cannot be killed while player is doing miniboss battle
IfStateIs0
  tmpargument = 50      //1 second
  SetDamageTime

//---------------------------------------------------------------------------
//Charging a spell?
GetContent
tmpx = tmpargument
tmpy = 0
IfXIsMoreThanY          //Yes, something is charging

  //Casting armageddon
  IfStateIs9
    SetTurnModeToSpin
    tmpy = 50*3                //Charge 3 seconds
    IfXIsMoreThanY
      tmpargument = 2           //Done charging!
      SetState

      //Spawn the fireballs
      tmpargument = 6
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz + 75
      SpawnExactParticle

      tmpargument = 0
      SetContent
      SetTurnModeToVelocity
    Else
      tmpargument = tmpargument + 1 //Keep charging
      SetContent

      //Spawn particles around the caster
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 5
      SpawnExactParticle

      //Spawn charge particle
      GetContent
      tmpturn = tmpargument < 6 + 10000
      tmpargument = 5
      tmpdistance = GRIPLEFT
      SpawnAttachedSizedParticle
      tmpdistance = GRIPRIGHT
      SpawnAttachedSizedParticle

  //Casting telekinesis
  IfStateIs7
    IfTargetIsOnHatedTeam
      SetTurnModeToWatchTarget
      tmpy = 75                //Charge 1,5 seconds
      IfXIsMoreThanY
        tmpargument = 2           //Done charging!
        SetState

        //Throw the target at a speed of 75
        tmpx = 0
        tmpy = 0
        tmpturn = selfturn      //Direction
        tmpdistance = 75        //Speed
        Compass
        AccelerateTarget

        //Also deal 3-7 damage
        tmpargument = DAMAGECRUSH
        SetDamageType
        tmpargument = rand & 1023 + 768
        DamageTarget

        //Animation
        tmpargument = ACTIONTA
        DoAction

        tmpargument = 2
        PlayFullSound
        tmpargument = 1
        SendMessage
        tmpargument = 0
        SetContent
        SetTurnModeToVelocity
      Else
        tmpargument = tmpargument + 1 //Keep charging
        SetContent
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 4
        SpawnExactParticle

        //Spawn charge particle
        GetContent
        tmpturn = tmpargument < 6 + 10000
        tmpargument = 3
        tmpdistance = GRIPLEFT
        SpawnAttachedSizedParticle

  //Casting Paralysis
  IfStateIs8
    IfTargetIsOnHatedTeam
      SetTurnModeToWatchTarget
      tmpy = 50*2                 //Charge 2 full seconds
      IfXIsMoreThanY
        tmpargument = 2           //Done charging!
        SetState
        tmpargument = 300       //6 seconds
        BlackTarget
        SetTargetReloadTime
        SetTurnModeToVelocity

        //Sound and message
        tmpargument = 1
        PlayFullSound
        tmpargument = 0
        SendMessage

        //Animation
        tmpargument = ACTIONTA
        DoAction

        tmpargument = 0
        SetContent
      Else
        tmpargument = tmpargument + 1 //Keep charging
        SetContent
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 2
        SpawnExactParticle

        //Spawn charge particle
        GetContent
        tmpturn = tmpargument < 6 + 10000
        tmpargument = 1
        tmpdistance = GRIPLEFT
        SpawnAttachedSizedParticle

  //Casting invisibillity
  IfStateIs10
    tmpy = 50*4                 //Charge 4 full seconds
    IfXIsMoreThanY
      tmpargument = 2           //Done charging!
      SetState
      tmpargument = 0
      SetAlpha

      //Animation
      tmpargument = ACTIONTA
      DoAction

      //Sound and message
      tmpargument = 7
      PlayFullSound
      tmpargument = 2
      SendMessage
      tmpargument = 0
      SetContent
    Else
      tmpargument = tmpargument + 1 //Keep charging
      SetContent

      //Sparklies around caster
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 4
      SpawnExactParticle

      //Spawn charge particle
      GetContent
      tmpturn = tmpargument < 6 + 10000
      tmpargument = 11
      tmpdistance = GRIPLEFT
      SpawnAttachedSizedParticle

  //Summoning live weaponry
  IfStateIs11
    tmpy = 50*4                 //Charge 4 full seconds
    IfXIsMoreThanY
      tmpargument = 2           //Done charging!
      SetState

      tmpx = selfx
      tmpy = selfy
      tmpdistance = 120     //Distance between the swords
      tmpturn = rand

      Compass
      tmpargument = rand % 3 + 109  //Sword, xbow or mace
      SpawnExactCharacterXYZ
      tmpargument = 0
      SpawnExactParticle

      Compass
      tmpargument = rand % 3 + 109  //Sword, xbow or mace
      SpawnExactCharacterXYZ
      tmpargument = 0
      SpawnExactParticle

      Compass
      tmpargument = rand % 3 + 109  //Sword, xbow or mace
      SpawnExactCharacterXYZ
      tmpargument = 0
      SpawnExactParticle

      Compass
      tmpargument = rand % 3 + 109  //Sword, xbow or mace
      SpawnExactCharacterXYZ
      tmpargument = 0
      SpawnExactParticle

      Compass
      tmpargument = rand % 3 + 109  //Sword, xbow or mace
      SpawnExactCharacterXYZ
      tmpargument = 0
      SpawnExactParticle

      //And some spelly effects
      tmpargument = 0
      SpawnExactParticle
      tmpargument = 8
      PlayFullSound
      tmpargument = 0
      SetContent
    Else
      tmpargument = tmpargument + 1 //Keep charging
      SetContent

      //Spawn charge particle
      GetContent
      tmpturn = tmpargument < 6 + 10000
      tmpargument = 12
      tmpdistance = GRIPLEFT
      SpawnAttachedSizedParticle
      tmpdistance = GRIPRIGHT
      SpawnAttachedSizedParticle

      //Animation
      tmpargument = ACTIONMD
      DoAction

  //Disintergrate item
  IfStateIs13
    tmpy = 50*5                 //Charge 5 full seconds
    IfXIsMoreThanY
      tmpargument = 2           //Done charging!
      SetState

      //Find something to destroy
      tmpdistance = 0
      SetTargetToTargetLeftHand
        tmpdistance = 1
      Else
        SetTargetToTargetRightHand
          tmpdistance = 1

      tmpturn = 0
      IfDistanceIsMoreThanTurn
        tmpargument = 9
        PlayFullSound
        tmpargument = 0
        tmpx = targetx
        tmpy = targety
        SpawnExactParticle
        tmpargument = 4
        SendMessage
        PoofTarget              //Destroy the item!
          DoNothing

        //Somehow it failed!
        Else
          tmpargument = 3
          SendMessageNear
          tmpargument = 10
          PlaySound

        //Animation
        tmpargument = ACTIONTA
        DoAction

      //Nothing found
      Else
        tmpargument = 3
        SendMessageNear
        tmpargument = 10
        PlaySound
      tmpargument = 0
      SetContent
    Else
      tmpargument = tmpargument + 1 //Keep charging
      SetContent

      //Sparklies around target item
      SetOldTarget
      SetTargetToTargetLeftHand
        DoNothing
      Else
        SetTargetToTargetRightHand
      tmpx = targetx
      tmpy = targety
      tmpdistance = targetz
      tmpargument = 8
      SpawnExactParticle
      SetTargetToOldTarget

      //Spawn charge particle
      GetContent
      tmpturn = tmpargument < 6 + 10000
      tmpargument = 5
      tmpdistance = GRIPLEFT
      SpawnAttachedSizedParticle

      //Handle movement
      SetTurnModeToWatchTarget
      tmpx = targetx
      tmpy = targety
      tmpdistance = 0 - 400
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint

  //Using the dracolich toy
  IfStateIs3
    tmpy = 155                //Charge time
    IfXIsMoreThanY
      SetTurnModeToVelocity
      tmpargument = 2           //Done charging!
      SetState

      tmpargument = 0
      SetContent
    Else
      SetTurnModeToWatchTarget
      tmpargument = tmpargument + 1 //Keep charging
      SetContent

      //Keep charging it
      tmpargument = LATCHLEFT
      PressLatchButton

//End invisibillity
IfInvisible
  tmpargument = 1
  GetXY
  tmpy = 50*8             //End after 8 seconds
  IfXIsEqualToY
    tmpx = 0
    SetXY
    tmpargument = 255
    SetAlpha
  Else
    tmpx = tmpx +1        //Increase the invis timer
    SetXY

//Block attacks
tmpargument = 0
IfStateIsNot
  tmpargument = 3
  IfArmorIs
    DoNothing                   //Don't do it when in shade mode
  Else
    tmpargument = 4
    GetXY
    tmpy = 1
    IfXIsLessThanY
      IfTargetIsOnHatedTeam
        IfTargetIsAttacking
          IfFacingTarget
            tmpargument = ACTIONPA
            DoAction
            KeepAction
        Else
          UnkeepAction
      Else
        UnkeepAction
    Else
      tmpx = tmpx - 1
      SetXY

//Set cooldown for parry
IfBlocked
  tmpargument = 4
  tmpx = 75         //1,5 second cooldown
  tmpy = 0
  SetXY
  UnkeepAction

//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
