//Jester, an annoying enemy
//Sits in one place until attacked doing passive combat
//Teleports
//Shocks
//Cast whirlwind
//Creates illusions of himself
//Drops his wand and draw a spellbook instead
//Lot's of irritating traps and stuff around the room
//Fake potions... kursed items... toyboxes... mimics... mines...

//------------------------------------------------------------------------------
//This fills the room with sparklies
IfStateIs0
  tmpargument = 10
  tmpdistance = selfz
  tmpx = rand % 1500 - 750 + selfx
  tmpy = rand % 1500 - 750 + selfy
  SpawnExactParticle

  //Teleport if target comes too close
  IfTargetIsOnHatedTeam
    tmpx = targetdistance
    tmpy = 256      //2 tiles
    IfXIsLessThanY
      tmpargument = 1
      SetState
      tmpx = selfx - 425
      tmpy = selfy
      tmpdistance = selfz
      Teleport
      
        //Sound effect
        tmpargument = rand & 2 + 4
        PlaySound
        
        //Teleport cloud
        tmpargument = 0
        tmpx = selfx
        tmpy = selfy
        tmpdistance = selfz
        SpawnExactParticle
        
        //Since pathfinding does not work yet, add this manually to make AI look smarter
        tmpx = selfx
        tmpdistance = rand & 1
        tmpturn = 0
        IfDistanceIsMoreThanTurn      //Randomize if we walk left or right
          tmpy = selfy + 450
        Else
          tmpy = selfy - 450
        ClearWaypoints
        AddWaypoint
        tmpargument = 150   //Wait a bit before attacking
        SetTime

//------------------------------------------------------------------------------
//The main AI loop
IfTimeOut
  tmpargument = 20
  SetTime
  
  //Wait in one place (passive combat)
  IfStateIs0

    //movement
    tmpx = selfspawnx
    tmpy = selfspawny
    ClearWaypoints
    AddWaypoint
    
    //Handle music
    tmpdistance = 128*13      //13 tiles away
    SetTargetToDistantEnemy
      tmpargument = 17        //play funny song
      PlayMusic
    Else
      tmpargument = 11        //nope, reset normal music
      PlayMusic
      
    //Handle inventory and wand usage
    tmpargument = 0
    IfContentIs
      tmpargument = [WAND]
      IfHoldingItemID
        SetTargetToSelf             //Permanent charges with this wand!
        tmpargument = [WAND]
        RestockTargetAmmoIDFirst
        SetTargetToWideEnemy
          SetTurnModeToWatchTarget
          tmpargument = LATCHLEFT     //Jester's wand
          PressLatchButton
          tmpargument = 130     //dont spam
          SetTime
      Else
        tmpargument = LATCHPACKLEFT    //Put away the spellbook and draw the wand
        PressLatchButton
    Else
      tmpargument = 0
      SetContent
      tmpargument = LATCHLEFT     //Dispel the whirlwind
      PressLatchButton
    
  //Active combat
  IfStateIs1  
  
    //Get the Whirlwind spellbook
    tmpargument = [STRM]
    IfHoldingItemID
      DoNothing
    Else
      tmpargument = LATCHPACKLEFT    //Put away the wand
      PressLatchButton
      
    //Movement
    tmpdistance = 128*10
    SetTargetToDistantEnemy
      SetTurnModeToWatchTarget
      
      //Either close in or keep distance
      tmpdistance = 200                         //Not casting whirlwind
      GetContent
      tmpx = tmpargument
      tmpy = 0
      IfXIsMoreThanY
        tmpdistance = -400                      //Casting whirlwind, keep distance
        tmpy = 1600
        IfXIsMoreThanY
          tmpdistance = 200                     //Finished casting whirlwind
        
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      
      //Retreat if we are at low mana
      tmpx = selfmana
      tmpy = 256
      IfXIsLessThanY
        tmpargument = 0
        GetXY
        IfXIsEqualToY
          tmpargument = 0
          SetState
          tmpx = selfspawnx
          tmpy = selfspawny
          Teleport
            tmpargument = 0
            tmpx = selfx
            tmpy = selfy
            tmpdistance = selfz
            SpawnExactParticle
    
    //No enemies around, teleport to spawn
    Else
      tmpargument = 0
      SetState
      tmpx = selfspawnx
      tmpy = selfspawny
      Teleport
        tmpargument = 0
        tmpx = selfx
        tmpy = selfy
        tmpdistance = selfz
        SpawnExactParticle
    
    //Attack
    IfTargetIsOnHatedTeam
      IfFacingTarget
      
        //Cast Whirlwind
        tmpx = selfmana
        tmpy = 256*7                //Only summon if we have at least 6 mana ready
        IfXIsMoreThanY
          tmpargument = 0
          IfContentIs               //Only if not already summoned
            tmpargument = [STRM]
            IfHoldingItemID         //holding correct spell?
              tmpargument = LATCHLEFT
              PressLatchButton
              tmpargument = 12      //start charging
              SetContent
             
        //Cast shock             
        tmpx = targetdistance
        tmpy = 100
        IfXIsLessThanY
          GetContent
          tmpx = 1600
          IfXIsMoreThanY
            tmpargument = LATCHRIGHT
            PressLatchButton
          Else
            tmpargument = 0
            IfContentIs
              tmpargument = LATCHRIGHT
              PressLatchButton

      //Create mirage
      tmpx = rand % 100
      tmpy = 30           //30% chance
      IfXIsLessThanY
        tmpx = selfmana
        tmpy = 256*4
        IfXIsMoreThanY            //enough mana?
          tmpargument = 0
          GetXY
          IfXIsEqualToY         //No cooldown left?
          
            //Sound effect
            tmpargument = 13
            PlaySound
            SpawnPoof
            
            //Mana cost
            SetTargetToSelf
            tmpargument = 256*4     //4 mana
            CostTargetMana
          
            //Spawn a mirage
            tmpx = selfx
            tmpy = selfy
            tmpturn = rand
            tmpdistance = 15
            SpawnCharacter
            tmpargument = 2
            SetChildState
            
            //Spawn a mirage
            tmpx = selfx
            tmpy = selfy
            tmpturn = rand
            tmpdistance = 15
            SpawnCharacter
            tmpargument = 2
            SetChildState
            
            //Spawn a mirage
            tmpx = selfx
            tmpy = selfy
            tmpturn = rand
            tmpdistance = 15
            SpawnCharacter
            tmpargument = 2
            SetChildState
            
            //Spawn a mirage
            tmpx = selfx
            tmpy = selfy
            tmpturn = rand
            tmpdistance = 15
            SpawnCharacter
            tmpargument = 2
            SetChildState
            
            //Make them wonder who it is
            SetTargetToSelf
            tmpx = 0
            tmpy = 0
            tmpdistance = 15
            tmpturn = rand
            Compass
            AccelerateTarget
            
            //Set cooldown before doing this again
            tmpargument = 0
            tmpx = 650          //7 seconds
            tmpy = 0
            SetXY
      
  //Illusion AI
  IfStateIs2
    SetTargetToNearestEnemy
    IfTargetCanSeeInvisible
      tmpargument = 128         //Make us visible as a mirage for those who can really see
      SetAlpha
      
    //Movement
    SetTurnModeToVelocity
    tmpx = rand & 1023 + targetx - 511
    tmpy = rand & 1023 + targety - 511    
    ClearWaypoints
    AddWaypoint
    
    //Fake attacks
    IfTargetIsOnHatedTeam
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 160
        IfXIsLessThanY
          tmpargument = ACTIONZB      //zap right
          DoAction
          tmpargument = 9
          tmpdistance = GRIPRIGHT
          tmpx = 0
          tmpy = 0
          SpawnParticle             //spawn fake electricity
    
    //Illusion life time
    GetContent
    tmpargument = tmpargument + 1
    SetContent
    tmpargument = 20
    IfContentIs         //Time is up! Go poof
      GoPoof
      SpawnPoof
    tmpargument = 30
    SetTime
    
    
//------------------------------------------------------------------------------
//Charge whirlwind
IfStateIs1
  GetContent
  tmpx = tmpargument
  tmpy = 0
  IfXIsMoreThanY
    tmpy = 1600
    IfXIsLessThanY
      tmpargument = LATCHLEFT
      PressLatchButton
      tmpx = selfmana
      tmpy = 11
      IfXIsMoreThanY
        GetContent
        tmpargument = tmpargument + 12
        SetContent

//------------------------------------------------------------------------------
//For illusions, spawns a spelly effect
IfSpawned
  IfStateIs2
    SpawnPoof
  
//------------------------------------------------------------------------------
//Ouchies (check before IfKilled so that mirages pop away)
IfAttacked
  IfStateIs0
    tmpargument = 1
    SetState
  tmpargument = rand & 2 + 1
  PlaySound
  
  //Illusions die away in 1 pop
  IfStateIs2
    tmpargument = 3
    SendMessageNear
    GoPoof
    SpawnPoof

//------------------------------------------------------------------------------
//Death
IfKilled
  IfTargetIsOnHatedTeam
    tmpargument = 0
  Else
    tmpargument = 1
  SendMessageNear
  
  //Loot!
  DropKeys
  DropItems
  tmpargument = selfmoney
  DropMoney
  
  //Death cry
  tmpargument = 10
  PlaySound
  
  //Award some bonus xp
  tmpargument = 50
  tmpdistance = EXPQUEST
  GiveExperienceToGoodTeam
  
  //reset normal music
  tmpargument = 11        
  PlayMusic
  
  //open the prison
  tmpargument = passage
  OpenPassage
  
//------------------------------------------------------------------------------
//Teleport away
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAPlatform
    tmpargument = LATCHJUMP   //Jump over obstacles
    PressLatchButton

  //Teleport?
  IfTargetIsAlive
    IfTargetIsOnHatedTeam
      tmpargument = 2
      IfStateIsNot
        tmpx = rand & 3
        tmpy = 0
        
        //Yes, teleport!
        IfXIsEqualToY
          tmpx = rand % 512 + selfx - 256
          tmpy = rand % 512 + selfy - 256
          Teleport
            tmpargument = 0
            tmpx = selfx
            tmpy = selfy
            tmpdistance = selfz
            SpawnExactParticle
//------------------------------------------------------------------------------
//Healers!
IfHealed
  tmpargument = 7
  SendMessageNear
  tmpargument = rand & 2 + 4
  PlaySound           //That annoying laughter
  
//------------------------------------------------------------------------------
//Countdown for mirror image cooldown
tmpargument = 0
GetXY
IfXIsMoreThanY
  tmpx = tmpx - 1
  SetXY

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