// Create the character
IfSpawned
  KeepAction
  tmpargument = 0
  SetContent

// Say they pour over a book...
IfTakenOut
  tmpargument = 0
  SetContent
  SetTargetToWhoeverIsHolding
    IfTargetIsAPlayer
      tmpargument = 0
      SendMessageNear

IfTimeOut
  tmpargument = 500
  SetTime
  tmpargument = 0
  SetContent

// Allow it to be used
IfUsed
  tmpargument = 50
  SetReloadTime
  SetTargetToWhoeverIsHolding
    tmpx = 1024				//Need 4 mana flow to cast spell
    tmpy = targetmanaflow
    IfXIsLessThanY
      // Give experience...
      IfNameIsKnown
        DoNothing
      Else
        MakeUsageKnown
        MakeNameKnown
        tmpargument = 30
        tmpdistance = EXPSECRET
        GiveExperienceToTarget

      // Do the effect
      tmpargument = tmpx + 65
      CostTargetMana
        tmpx = rand & 3071 + targetint	//Intelligence determines control of spell
        tmpy = 6400    		// 25 int is perfect, 13 is near impossible
        IfXIsMoreThanY
          tmpargument = 1
          SendMessageNear
          SetTargetToNearestEnemy
            tmpx = targetdistance
            tmpy = 600
            IfXIsLessThanY		//Perfect hit - drop on an enemy
              tmpx = targetx
              tmpy = targety
            Else
              tmpx = targetx		//Slightly misses enemy
              tmpy = targety
              tmpturn = rand
              tmpdistance = 456
              Compass
          Else				//No target, drops randomly
            tmpx = targetx
            tmpy = targety
            tmpturn = rand
            tmpdistance = 456
            Compass
        Else
          tmpargument = 2		 //Not enough int, drop around the caster...
          SendMessageNear
          tmpturn = rand
          tmpdistance = rand % 455
          tmpx = targetx
          tmpy = targety
          Compass
        tmpdistance = 0
        tmpargument = 0
        SpawnExactParticle

        //Explosions are loud, so wake up sleepers...
        tmpargument = [WAKE]
        tmpdistance = [WAKE]
        OrderSpecialID
      Else
        // Not enough mana to cast...
        SetTargetToWhoeverIsHolding
        IfTargetIsAPlayer
          tmpargument = 3
          SendMessageNear
        tmpargument = 255
        SetReloadTime
        tmpargument = 1
        SetContent
        tmpargument = 50
        SetTime
    Else
      // Not enough flow to cast...
      tmpargument = 256
      CostTargetMana
        tmpargument = 3
        SendMessageNear
        tmpargument = 255
        SetReloadTime
        tmpargument = 1
        SetContent
        tmpargument = 50
        SetTime
        // Give experience...
        SetTargetToWhoeverIsHolding
        IfTargetIsAPlayer
          IfNameIsKnown
            DoNothing
          Else
            MakeUsageKnown
            MakeNameKnown
            tmpargument = 30
            tmpdistance = EXPSECRET
            GiveExperienceToTarget
      Else
        SetTargetToWhoeverIsHolding
        IfTargetIsAPlayer
          tmpargument = 3
          SendMessageNear

// Return to spellbook, Do last!
IfDropped
  GetContent
  tmpx = tmpargument
  tmpy = 1
  IfXIsEqualToY
    PlaySound
  tmpargument = 0
  SetContent
  BecomeSpellbook
  DisaffirmCharacter
  tmpargument = ACTIONJB
  DoAction
  KeepAction
End
