//The different states:
//0: Guard
//1: Combat
//2: Turned to ice
//Note: This is a specific boss script! Uses bossicicle.obj to spawn icicles
//------------------------------------------------------------------------------
//Manage the destruction of icicles
IfOrdered
  tmpx = selforder
  tmpy = [ICIC]
  IfXIsEqualToY
    GetContent
    tmpargument = tmpargument -1
    SetContent

//------------------------------------------------------------------------------
//Combat/Guard
IfSpawned
  tmpargument = passage
  ClosePassage
  JoinEvilTeam
  tmpargument = 500
  SetTime

//------------------------------------------------------------------------------
//Combat/Guard
IfTimeOut
  tmpargument = rand & 15 + 25
  SetTime

  //Replenish icicles
  GetContent
  tmpx = tmpargument 
  tmpy = 12				//Limit to 7 icicles at one time
  IfXIsLessThanY
    tmpargument = 71
    tmpdistance = 450
    tmpx = rand & 255 + targetx - 256
    tmpx = rand & 240 + tmpx	- 120	// lessen the randomness
    tmpy = rand & 255 + targety - 256
    tmpy = rand & 240 + tmpy	- 120   // lessen the randomness
    tmpturn = rand & 65535
    SpawnExactCharacterXYZ
    GetContent
    tmpargument = tmpargument + 1	//keep track of how many we have
    SetContent

  
  //Guard mode
  IfStateIs0    
    tmpturn = rand	//Randomly...
    tmpx = selfspawnx
    tmpy = selfspawny
    tmpdistance = 500	//up to 200 distance from spawn point
    Compass
    ClearWaypoints
    AddWaypoint
    SetTargetToWideEnemy
      tmpargument = 1
      SetState
      tmpargument = 1		//Roar!
      SendMessageNear
      tmpargument = 6
      PlaySound

  //Combat mode
  IfStateIs1
    SetTargetToNearestEnemy
      DoNothing
    Else
      tmpargument = 0
      SetState
    tmpdistance = 200				//For Compass later
    tmpx = targetdistance
    tmpy = 1600
    IfXIsLessThanY
      tmpy = 650
      IfXIsLessThanY
        tmpy = 200
        IfXIsLessThanY                     //Close enough to attack?
          IfFacingTarget
            tmpx = rand & 2			//33% kick, 33% smash, 33% nothing
            tmpy = 0
            IfXIsEqualToY
              tmpargument = ACTIONUA         //Smash
              DoAction
            tmpy = 1
            IfXIsEqualToY
              tmpargument = ACTIONUB         //Kick
              DoAction

        //Jump bash them
        Else
          tmpx = rand % 55
          tmpy = 8
          IfXIsLessThanY              //Chance
            tmpargument = ACTIONJA
            DoAction
              tmpargument = 7
              PlaySound
              tmpdistance = selfz + 15
              tmpx = selfx
              tmpy = selfy
              SpawnExactParticle	//Spawn 8 clouds
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
              SpawnExactParticle
      Else
        tmpdistance = 1500		//Charge!
        Run
      tmpx = targetx			//Move towards enemy
      tmpy = targety
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
    Else
      tmpargument = 0			//Return to guard
      SetState

    //Combat music
    tmpargument = 20
    tmpdistance = 0
    PlayMusic

  //What to do if frozen
  IfStateIs2
    SetTargetToSelf
    tmpargument = 50
    HealTarget		//Remove poison and regenerate extra
    tmpx = selflife
    tmpy = 3000
    IfXIsMoreThanY	//Keep regeneration in ice form
      tmpargument = 0	//until you gain 20 life again
      ChangeArmor
      tmpargument = 0
      SetState
      tmpargument = 255
      SetLight
      UnkeepAction

//------------------------------------------------------------------------------
//Handle death by sending a message and other stuff
IfKilled
  tmpargument = 1		//Sound number
  PlaySound

  UnkeepAction

  //Messaging
  tmpargument = MESSAGEDEATH
  IfTargetIsOnSameTeam
    tmpargument = MESSAGEFRAG
    IfTargetIsSelf
      tmpargument = MESSAGEACCIDENT
  SendMessageNear

  // Drop goodies
  tmpargument = selfmoney		//All of it
  DropMoney

  //Destroyed ice cloud...
  SpawnPoof
  SpawnPoof
  SpawnPoof

  //Open the boss door
  tmpargument = passage
  OpenPassage

  //Victory tune
  tmpargument = 14
  tmpdistance = 0
  PlayMusic
  
  //Forgot spork ending
  ClearEndMessage
  tmpargument = 3
  AddEndMessage
  tmpargument = 4
  AddEndMessage

//------------------------------------------------------------------------------
//Handle being attacked by blocking or countering or running away
IfAttacked

  // Turn to ice if life is less than 6 blocks
  tmpargument = 2
  IfStateIsNot
    tmpx = selflife
    tmpy = 2560			//10 hp
    IfXIsLessThanY
      tmpargument = 2		//Ice form
      SetState
      tmpargument = 1		//Look like ice
      ChangeArmor
      tmpargument = ACTIONMK	//Ice mode
      DoActionOverride
      KeepAction
      tmpargument = 5		//Ice sound
      PlaySound
      tmpargument = 200
      SetLight

    //Fire Bonus damage
    Else
      GetDamageType
      tmpx = tmpargument
      tmpy = DAMAGEFIRE
      IfXIsEqualToY
        // Permanent life loss from fire
        SetOldTarget
        SetTargetToSelf
        tmpargument = 0 - 1024		//Lose 4 permanent life
        GiveLifeToTarget
        SetTargetToOldTarget
        tmpargument = 5
        SendMessageNear

      //Ouch sound
      tmpargument = 2
      PlaySound

  //Hit while ice form, spawn a ice dust cloud and clink
  Else
    tmpargument = 4
    PlaySound

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