// Create the character
IfSpawned
  IfStateIs0
    // It's an imported spell in hand
    tmpargument = 0
    SetState
    KeepAction
  Else
    // It's a falling sword
    MakeNameUnknown
    NotAnItem
    tmpargument = 0
    tmpdistance = SPAWNLAST
    SpawnAttachedParticle
    GetState
    tmpargument = tmpargument < 4 + 300
    SetTime
    // Spawn another sword?
    IfStateIs1
      GetState  // NOP
    Else
      tmpx = selfx
      tmpy = selfy
      tmpdistance = 100
      tmpturn = selfturn + 8192
      Compass
      tmpdistance = selfz + 100
      SpawnCharacterXYZ
        GetState
        tmpargument = tmpargument - 1	// Child will spawn more swords
        SetChildState


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



// Sword AI
IfStateIs0
  GetState // NOP
Else
  IfTimeOut
    tmpargument = ACTIONKA
    DoAction
    tmpargument = 300
    SetTime
  IfHitGround
    tmpargument = 1
    tmpdistance = 11025
    PlaySound



// 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 = 1024
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpx = 6
          tmpargument = tmpx
          CostTargetMana
            GetContent
            tmpargument = tmpargument + 4
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 1
            SpawnExactParticle
  Else
    GetContent
    tmpx = tmpargument
    tmpy = 255
    IfXIsMoreThanY
      // Make the holder cast it
      SetTargetToWhoeverIsHolding
        tmpargument = ACTIONZA
        CorrectActionForHand
        TargetDoAction



        // Start dropping swords
        tmpx = targetx
        tmpy = targety
        tmpdistance = 130
        GetContent
        tmpargument = tmpargument > 8 + 1 < 12
        tmpturn = targetturn - tmpargument
        Compass
        tmpdistance = targetz + 500
        tmpturn = tmpturn + 8192
        SpawnCharacterXYZ
          // Set starting state
          GetContent
          tmpargument = tmpargument > 8 + 2	// Child will spawn more swords
          SetChildState

          // Identify stuff
          MakeNameKnown
          MakeUsageKnown

          // Cast sound
          tmpargument = 0
          tmpdistance = 11025
          PlaySound
        Else
          // Tried to cast in a wall?
          tmpargument = 0
          SendMessageNear
    Else
      tmpy = 0
      IfXIsMoreThanY
        // Didn't pump it enough
        tmpargument = 0
        SendMessageNear
    // Reset the charge counter
    tmpargument = 0
    SetContent
  // Return to spellbook, Do last!
  IfDropped
    tmpargument = 0
    SetContent
    BecomeSpellbook
    DisaffirmCharacter
    tmpargument = ACTIONJB
    DoAction
    KeepAction
End
