// 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 = 640				//Need 2.5 mana flow to cast spell
    tmpy = targetmanaflow
    IfXIsLessThanY
      // Give experience...
      IfUsageIsKnown
        DoNothing
      Else
        MakeUsageKnown
        MakeNameKnown
        tmpargument = 30
        tmpdistance = EXPSECRET
        GiveExperienceToTarget
      // Do the effect
      tmpargument = tmpx + 50
      CostTargetMana
        tmpx = rand & 2047 + targetint	//Intelligence determines control of spell
        tmpy = 5888     		// 23 int is perfect, 15 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...
        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...
        IfUsageIsKnown
          GetState // NOP
        Else
          MakeUsageKnown
          MakeNameKnown
          tmpargument = 30
          tmpdistance = EXPSECRET
          GiveExperienceToTarget
      Else
        tmpargument = 3
        SendMessageNear



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