// Icicle spell
//  by Pteromys of Melon Island... http://melonisland.net
//  based on the "Sword Wall" spell
// Usage: Hold down attack to summon icicles in front of you.
//   Release to send the icicles into the sky
// Tips:  The slightest footstep under the icicles will bring
//   them crashing down.  Avoid triggering your own traps by
//   using a levitation spell.
//     Icicles will pin down whoever they hit.
//     Icicles can be destroyed by fire.
// Guide to object states:
//  0 Spellbook
//  1 Icicle floating in front of you
//  2 Icicle in the sky
//  3 Icicle falling
//  4 Icicle pinned somebody down
//  5 Icicle shattered and shortly to disappear
// Content variable usage:
//  As spellbook, records the charge.
//  As icicle, tells when it has reached the proper height.
// Known Issues:
//  Icicles will, for some reason, slide down slopes.

IfSpawned
  IfStateIs0
    // It's an imported spell in hand or a spellbook
    tmpargument = 0
    SetState
    KeepAction
    tmpargument = 255
    SetLight
  Else
    // Watch out for the falling icicles!
    MakeNameUnknown
    NotAnItem
    tmpargument = 60
    SetFlyHeight
    MakeCrushValid


IfChanged
  // It's a spell in hand
  tmpargument = 0
  SetState
  KeepAction



// Icicle event handling
IfStateIs4				// 4. Pinned somebody and waiting
  tmpx = 0
  tmpy = 1
  IfTimeOut
    tmpx = 1
  IfReaffirmed
    tmpx = 1
  IfCrushed
    tmpx = 1
  IfXIsEqualToY
    UndoEnchant
    tmpx = selfx
    tmpy = selfy
    tmpdistance = selfz
    tmpargument = 3
    SpawnExactParticle
    tmpargument = 2			// *CRASH! tinkle-tinkle-tinkle...*
    tmpdistance = 22050
    PlaySound
    tmpargument = ACTIONKA
    DoActionOverride
    KeepAction
    tmpargument = 5
    SetState
IfStateIs3				// 3. Falling
  IfScoredAHit
    SetTargetToSelf
    SetOwnerToTarget
    SetTargetToWhoeverWasHit
    StopTargetMovement
    EnchantTarget
    tmpargument = 4			// Don't shatter on impact if I've pinned somebody
    SetState
    IfTargetIsAlive
      tmpargument = 3
      SendMessageNear
      tmpargument = 3
      tmpdistance = 11025
      PlaySound
  Else
    tmpargument = 0
    IfReaffirmed
      tmpargument = 1
    SetTargetToSelf
    tmpx = targetspeedz
    tmpy = 0 - 2
    IfXIsMoreThanY
      tmpargument = 1
    IfTimeOut
      tmpargument = 1
    tmpx = tmpargument
    tmpy = 1
    IfXIsEqualToY
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 3
      SpawnExactParticle
      tmpargument = 2			// *CRASH! tinkle-tinkle-tinkle...*
      tmpdistance = 22050
      PlaySound
      tmpargument = ACTIONKA
      DoActionOverride
      KeepAction
      tmpargument = 5
      SetState
IfStateIs2				// 2. Floating on high
  tmpx = 500
  tmpy = selfaltitude
  IfXIsLessThanY
    tmpargument = 0
    IfContentIs
      tmpargument = 1
      SetContent
  Else
    tmpargument = 0
    IfContentIs
      SetTargetToSelf
      StopTargetMovement
      tmpargument = 500
    Else
      SetTargetToSelf
      tmpargument = 1 - targetspeedz
      tmpargument = tmpargument < 1 + tmpargument < 3 + tmpargument < 2 //long-way multiply by 100
    AccelerateUp
  tmpargument = 1			// Don't fall until I'm actually floating on high
  IfContentIs
    tmpargument = 3
    SetTime
    SetTargetToNearestLifeform		// Anybody to fall on?
      tmpx = targetdistance
      tmpy = 50
      IfXIsLessThanY
        IfTargetIsFlying
          GetState //nop
        Else
          SetTargetToSelf
          StopTargetMovement
          tmpargument = 0 - 400
          AccelerateUp
          tmpargument = 0
          tmpdistance = SPAWNLAST
          SpawnAttachedParticle
          tmpargument = 320
          SetTime
          tmpargument = 1
          tmpdistance = 11025
          PlaySound
          tmpargument = 3
          SetState
  IfReaffirmed
    tmpx = selfx
    tmpy = selfy
    tmpdistance = selfz
    tmpargument = 3
    SpawnExactParticle
    tmpargument = 2			// *CRASH! tinkle-tinkle-tinkle...*
    tmpdistance = 22050
    PlaySound
    tmpargument = ACTIONKA
    DoActionOverride
    KeepAction
    tmpargument = 5
    SetState
IfStateIs1				// 1. Being summoned
  IfOrdered
    tmpx = selforder
    tmpy = 462				// Unique order ID for icicles to float up (hex 1CE)
    IfXIsEqualToY
      tmpargument = 0			// Rely on "AccelerateUp" for flying now
      SetFlyHeight
      tmpargument = 2
      SetState
      tmpargument = 0
      SetContent
  SetTargetToOwner
    tmpx = targetdistance
    tmpy = 256
    IfXIsMoreThanY
      tmpx = 0
      tmpy = 0
      tmpturn = targetturnto
      tmpdistance = 3
      Compass
      SetTargetToSelf
      AccelerateTarget


// Spell AI
IfStateIs0
  // Remove the charge
  IfTakenOut
    tmpargument = 0
    SetContent
    SetTargetToWhoeverIsHolding
      IfTargetIsAPlayer
        tmpargument = 1
        IfNameIsKnown
          tmpargument = 2
        SendMessageNear
  // Allow it to be used
  IfUsed
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      GetContent
      tmpx = tmpargument
      tmpy = 2049
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpargument = 4
          CostTargetMana
            GetContent
            tmpargument = tmpargument + 8
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 1
            SpawnExactParticle

            GetContent				// Is it time to spawn another icicle?
            tmpx = tmpargument & 255
            tmpy = 0
            IfXIsEqualToY
              tmpx = targetx
              tmpy = targety
              tmpdistance = rand & 63 + 63
              tmpturn = rand & 8191
              tmpturn = tmpturn + targetturn - 4096
              Compass
              tmpdistance = selfz + 30
              tmpargument = 3
              SpawnExactParticle		// Make a little smoke puffie
              tmpturn = rand & 65535
              SpawnCharacterXYZ
                GetState
                tmpargument = 1
                SetChildState
  Else
    GetContent
    tmpx = tmpargument
    tmpy = 255
    IfXIsMoreThanY
      SetTargetToWhoeverIsHolding
        tmpargument = ACTIONZA
        CorrectActionForHand
        TargetDoAction
        tmpargument = 462			// Unique order to make icicles fly up
        tmpdistance = [ICIC]
        OrderSpecialID
        tmpargument = 0
        tmpdistance = 11025
        PlaySound
        MakeNameKnown
    Else
      tmpy = 0
      IfXIsMoreThanY
        // Didn't pump it enough
        tmpargument = 0
        SendMessageNear
    tmpargument = 0
    SetContent

  // Return to spellbook, Do last!
  IfDropped
    tmpargument = 255
    SetLight
    tmpargument = 0
    SetFlyHeight
    tmpargument = 0
    SetContent
    BecomeSpellbook
    DisaffirmCharacter
    tmpargument = ACTIONJB
    DoAction
    KeepAction
End
