// Create the character
IfSpawned
  IfStateIs0
    // It's an imported spell in hand
    tmpargument = 0
    SetState
    KeepAction
  Else
    // It's a falling skull
    MakeNameUnknown
    NotAnItem
    
    //Make it damage whatever it hits
    //tmpargument = 0
    //tmpdistance = SPAWNLAST
    //SpawnAttachedParticle
    
    //Life time
    tmpargument = selfstate < 4 + 1000
    SetTime
    
    // Spawn another skull?
    tmpargument = 1
    IfStateIsNot
      tmpx = selfx
      tmpy = selfy
      tmpdistance = 50
      tmpturn = selfturn
      Compass
      tmpdistance = selfz + 100
      SpawnCharacterXYZ
        tmpargument = selfstate - 1	// Child will spawn more skulls
        SetChildState
    
    //Move it a little further out so it doesnt hit the caster
    tmpx = selfx
    tmpy = selfy
    tmpdistance = 150
    tmpturn = selfturn
    Compass
    Teleport
    SetTargetToSelf
    tmpx = selfx
    tmpy = selfy
    tmpdistance = -150
    tmpturn = selfturn + 8000
    Compass
    tmpdistance = selfz
    tmpturn = selfturn - 16000        //also, face outwards!
    TeleportTarget
    
IfChanged
  // It's a spell in hand
  tmpargument = 0
  SetState
  KeepAction

// Skull AI
tmpargument = 0
IfStateIsNot
  
  IfTimeOut
    tmpargument = ACTIONKA
    DoAction
    tmpargument = 300
    SetTime
    
  IfHitGround
    tmpargument = 1
    PlaySound
    
  IfOrdered
    tmpx = selforder
    tmpy = [ESKU]
    IfXIsEqualToY
      tmpargument = ACTIONKA
      DoAction
      tmpargument = 300
      SetTime
  

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

        //Dispel any existing wall first
        tmpargument = [ESKU]
        tmpdistance = [ESKU]
        OrderSpecialID

        // Start dropping skulls
        tmpx = targetx
        tmpy = targety
        tmpturn = selfcontent > 8 + 1
        tmpdistance = tmpturn * 130 > 1
        tmpturn = targetturn - 16000
        Compass
        tmpdistance = targetz + 500
        tmpturn = targetturn + 16000
        SpawnCharacterXYZ
          // Set starting state
          tmpargument = selfcontent > 7 + 2	// Child will spawn more skulls
          SetChildState

          // Identify stuff
          MakeNameKnown
          MakeUsageKnown

          // Cast sound
          tmpargument = 0
          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
