//States
//State 0: Going to waypoint
//State 1: In combat!
//State 2: Casting Animate spell
//State 3: Retreat!

//Cost mana for mini fireballs
IfUsed
  SetOldTarget
  tmpargument = 256
  SetTargetToSelf
  CostTargetMana
  SetTargetToOldTarget

//Show location on map
tmpx = selfx
tmpy = selfy
tmpargument = PURPLE
ShowBlipXY

//Important setup stuff
IfSpawned
  tmpargument = 325			//Give the player some time for waking up
  SetTime				//before moving around

  //Map stuff
  ShowMap
  ShowYouAreHere

  //Play interactive music
  tmpargument = 17
  tmpdistance = 0
  PlayMusic

  //be the boss!
  BecomeLeader
  
  //Set up all the waypoints
  tmpargument = 0
  tmpx = 3000
  tmpy = 4000
  SetXY 				//Out of start room

  tmpargument = 1
  tmpx = 3000
  tmpy = 5500
  SetXY 				//into first hall

  tmpargument = 2
  tmpx = 1600
  tmpy = 5500
  SetXY					//first battle

  tmpargument = 3
  tmpx = 3600
  tmpy = 5500
  SetXY					//into minihall

  tmpargument = 4
  tmpx = 3550
  tmpy = 6900
  SetXY					//Waterroom
 
  tmpargument = 5
  tmpx = 6000
  tmpy = 6800
  SetXY					//to treasure room
 
  tmpargument = 6
  tmpx = 6000
  tmpy = 6600
  SetXY					//from treasureroom

  tmpargument = 7
  tmpx = 5800
  tmpy = 5600
  SetXY					

  //peasant war
  tmpargument = 8
  tmpx = 4350
  tmpy = 5750
  SetXY					

  //First healer battle
  tmpargument = 9
  tmpx = 4350
  tmpy = 4250
  SetXY					

  //From healer battle
  tmpargument = 10
  tmpx = 5500
  tmpy = 4200
  SetXY					

  //To wizard mob
  tmpargument = 11
  tmpx = 5600
  tmpy = 2600
  SetXY					

  //To boss outside
  tmpargument = 12
  tmpx = 5600
  tmpy = 1300
  SetXY					

  //Outside boss room
  tmpargument = 13
  tmpx = 5400
  tmpy = 1300
  SetXY	

  //Bug room
  tmpargument = 14
  tmpx = 1700
  tmpy = 1300
  SetXY

//Say "take the key" in wait mode
IfStateIs4
  IfBumped
    SetTargetToWhoeverBumped
    IfTargetIsAPlayer
      tmpargument = 0
      IfContentIs
        tmpargument = 7
        PlayFullSound
        SendMessage
        tmpargument = 1
        SetContent 

IfTimeOut

  //STATE 4 - GO TO BOSS ROOM       (set before STATE 0 because of timeout)
  IfStateIs4
    tmpargument = 50
    SetTime
    tmpx = 4400
    tmpy = 1250
    ClearWaypoints
    AddWaypoint

    //reset "Get the key!"
    tmpargument = 0
    SetContent     

  //STATE 0 - WALK TROUGH DUNGEON
  IfStateIs0
    Walk
    tmpargument = 50
    SetTime

    //Scan for enemies
    tmpdistance = 128*5
    SetTargetToDistantEnemy
      tmpargument = 1
      SetState

    //Go to next waypoint
    GetContent			
    GetXY
    ClearWaypoints
    AddWaypoint

    //Ready next way to go
    tmpx = gotodistance
    tmpy = 75
    IfXIsLessThanY
      tmpargument = 10
      SetTime
        
      //Say "follow me!" at first waypoint
      tmpargument = 0
      IfContentIs
        tmpargument = 5
        PlayFullSound
        SendMessage

      //Final waypoint "take the key!"
      tmpargument = 14
      IfContentIs
        tmpargument = 4
        SetState
        tmpargument = 7
        PlayFullSound
        SendMessage
        tmpargument = 150
        SetTime

      //And do the usual stuff
      GetContent
      tmpargument = tmpargument + 1
      SetContent

    //Search for nearby bodies to animate
    tmpargument = [HUMA]
    tmpdistance = 3  			// Dead enemies
    SetTargetToWideBlahID
      tmpx = selfmana
      tmpy = 650
      IfXIsMoreThanY
        tmpargument = 2
        SetState			//Begin casting!
      Else
        SetTargetToOldTarget

  //STATE 1 - COMBAT
  IfStateIs1

    //Hold correct item in hand
    SetTargetToSelf
    tmpargument = [WAND]
    IfTargetHasItemID				//Wand is not expended
      IfHoldingItemID
        DoNothing
      Else
        tmpargument = LATCHPACKRIGHT		//Draw wand
        PressLatchButton
    Else
      SetTargetToTargetRightHand		//Put away animate spellbook
        tmpargument = LATCHPACKRIGHT
        PressLatchButton
    
    Run
    SetTargetToWideEnemy
      tmpx = targetx
      tmpy = targety
      ClearWaypoints
      AddWaypoint
      tmpargument = 10
      SetTime
      
      //Attack?
      tmpx = selfmana
      tmpy = 256
      IfXIsMoreThanY
        IfFacingTarget
          tmpx = targetdistance
          tmpy = 200
          IfXIsLessThanY
            tmpargument = LATCHLEFT	//Use shock in melee
            PressLatchButton
          Else
            tmpargument = LATCHRIGHT	//Use ranged weapon
            PressLatchButton
    
    Else
      GetContent
      tmpargument = tmpargument -1
      GetXY
      ClearWaypoints
      AddWaypoint			//Return to previous waypoint
      tmpargument = 0
      SetState
      tmpargument = rand & 63 + 130
      SetTime

    //Cast lightning bolt
    SetTargetToWideEnemy
      tmpx = rand & 31		
      tmpy = 2			//6% chance
      IfXIsLessThanY
        tmpx = selfmana
        tmpy = 512			//3 points of mana	
        IfXIsMoreThanY		//Enough mana to cast spell?
          tmpargument = 3
          tmpx = targetx
          tmpy = targety
          tmpdistance = 0
          SpawnExactParticle	//Strike him down!
          SetTargetToSelf
          tmpargument = 512
          CostTargetMana
          SetTargetToNearestEnemy

  //STATE 2 - CASTING ANIMATE SPELL
  IfStateIs2

    //We are in wrong mode!
    IfTargetIsOnHatedTeam
      tmpargument = 1
      SetState

    //If holding wand, change to book
    tmpargument = [WAND]
    IfHoldingItemID
      tmpargument = LATCHPACKRIGHT
      PressLatchButton
    Else
      SetOldTarget
      SetTargetToSelf
      SetTargetToTargetRightHand
        DoNothing
      Else
        tmpargument = LATCHPACKRIGHT
        PressLatchButton
      SetTargetToOldTarget

    //Move towards the body
    ClearWaypoints
    tmpx = targetx
    tmpy = targety
    tmpdistance = 0 - 200
    tmpturn = targetturnto
    Compass
    AddWaypoint

    //Sparklies
    tmpargument = 6
    tmpx = targetx
    tmpy = targety
    tmpdistance = targetz
    SpawnExactParticle			
    SetOldTarget
    SetTargetToSelf
    tmpargument = 13
    CostTargetMana				//Cost some mana
    SetTargetToOldTarget

    //tmpx in memory slot 15 is spell charge
    tmpargument = 15
    GetXY
    tmpx = tmpx + 12
    SetXY

    //Charge is finished
    tmpy = 600
    IfXIsMoreThanY
      tmpargument = 44					//Zombi
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturn
      tmpdistance = targetz
      PoofTarget
        // Replace the target with a Zombi
        SetTargetToWhoeverIsHolding
        SpawnExactCharacterXYZ
          tmpargument = ACTIONMD
          ChildDoActionOverride
          tmpargument = 3
          PlaySound
          SendMessageNear
      tmpargument = 0
      SetState
      tmpargument = 15				//Reset charge
      tmpx = 0
      SetXY

    //Abort if enemy closes in!
    SetTargetToNearbyEnemy
      tmpargument = 1
      SetState
      SetTime
      tmpargument = 15				//Reset charge
      tmpx = 0
      SetXY

  //STATE 3 - RETREAT
  IfStateIs3
    tmpx = targetx
    tmpy = targety
    tmpturn = targetturnfrom
    tmpdistance = 3000
    ClearWaypoints
    AddWaypoint
    tmpargument = 150
    SetTime
    tmpargument = 1
    SetState

  //STATE 5 - BOSS BATTLE! (OR VS. THE PLAYER)
  IfStateIs5
    tmpargument = rand & 15 + 10
    SetTime
    tmpx = 4400
    tmpy = 3300
    SetTargetToNearbyEnemy
      tmpx = targetx
      tmpy = targety
    ClearWaypoints
    AddWaypoint

    //We dont want book or wand anymore (fireballs)
    SetTargetToTargetRightHand
      tmpargument = LATCHPACKRIGHT
      PressLatchButton

    //Do combat stuff
    SetTargetToNearbyEnemy
      tmpx = rand & 31		
      tmpy = 4			//13% chance
      IfXIsLessThanY
        tmpx = selfmana
        tmpy = 738			//3 points of mana	
        IfXIsMoreThanY		//Enough mana to cast spell?
          tmpargument = 3
          tmpx = targetx
          tmpy = targety
          tmpdistance = 0
          SpawnExactParticle	//Strike him down!
          SetTargetToSelf
          tmpargument = 738
          CostTargetMana
          SetTargetToNearestEnemy

      //Attack?
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          tmpargument = LATCHLEFT	//Use shock in melee (or scythe?)
          PressLatchButton
        Else
          tmpargument = LATCHRIGHT	//Use fireball in ranged
          PressLatchButton

  //STATE 8 - GET ANGRY!
  IfStateIs8
    tmpargument = 12
    SendMessage
    tmpargument = 125
    SetTime
    JoinGoodTeam            //TODO: other team
    DisableRespawn

    tmpargument = 6		//Fool!
    PlayFullSound
    tmpargument = 1
    ChangeArmor
    
    tmpargument = 5     //Boss battle state again (but this time vs. player)
    SetState
    
    //Play the voice
    tmpargument = 14
    PlayFullSound

  //STATE 9 - WAIT FEW SECONDS FOR MINIONS TO DIE
  IfStateIs9
    //Kill the minions
    tmpargument = [UNDO]
    IssueOrder
    tmpargument = 100
    SetTime
    tmpargument = 8
    SetState
    
  //STATE 7 - DISMISSING THE MINIONS
  IfStateIs7
    tmpargument = 11
    SendMessage
    tmpargument = 200
    SetTime
    tmpargument = 9
    SetState
    tmpargument = 9		//Tell them what is going on
    SendMessage
        
    //Play the voice
    tmpargument = 13
    PlayFullSound
  
  //STATE 6 - PLEASED THE BATTLE HAS BEEN WON
  IfStateIs6
    tmpargument = 10
    SendMessage
    tmpargument = 200
    SetTime
    tmpargument = 7
    SetState
    
    //Play the voice
    tmpargument = 12
    PlayFullSound

//Say ouch
IfAttacked

  //Become bad if friendly fire
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    tmpx = selflife
    tmpy = 768
    IfXIsLessThanY       //Half life only left
      JoinGoodTeam		//TODO: Join other team

  IfStateIs2
    tmpargument = 15				//Reset charge
    tmpx = 0
    SetXY

  //Retreat (Disrupts spellcasting!)
  GetState
  tmpx = tmpargument
  tmpy = 4
  IfXIsLessThanY                 //Not in boss battle (or after)
    tmpargument = 3		
    SetState

  //Cast frost nova?
  tmpx = selfmana
  tmpy = 800
  IfXIsMoreThanY
    tmpx = rand & 31
    tmpy = 13			//40% chance to cast it
    IfXIsMoreThanY
      tmpargument = 1
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      SpawnExactParticle	//The nova is unleashed
      tmpargument = 768
      CostTargetMana	        //Cost 3 mana points

  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpargument = rand & 1
    tmpx = selfstate
    tmpy = 4
    IfXIsLessThanY            //Not in boss battle (or after)
      tmpargument = 1
      SetState
  Else
    tmpargument = 6
    SendMessageNear
    SetTargetToOldTarget
  PlaySound

//Dead
IfKilled

  //Check if player is winning
  tmpargument = 1
  IfArmorIs
    tmpargument = 100
    GiveExperienceToTarget
    EnableRespawn
    EnableExport
    BeatModule
    tmpargument = [BEAT]
    AddIDSZ
    tmpargument = selfmoney
    DropMoney
    DropItems
    tmpdistance = 0
    tmpargument = 14
    PlayMusic
  Else
    tmpargument = 8
    SendMessage
    tmpargument = 2
    PlayFullSound
    DisableRespawn
    tmpargument = 8          //*FAILED* sound
    PlayFullSound
    
    //Kill the minions
    tmpargument = [UNDO]
    IssueOrder

  
//Manage special orders
IfOrdered

  //Player used the boss key
  tmpx = selforder
  tmpy = [OPEN]
  IfXIsEqualToY
    tmpargument = 5
    SetState

  //Player killed the boss
  tmpx = selforder
  tmpy = [BOSS]
  IfXIsEqualToY
    tmpargument = 6
    SetState
    tmpargument = 250
    SetTime
End
