//Mount level         Bonus
//1 (2 mana)          Normal
//2 (4 mana)          Bite attack
//3 (6 mana)          EVIL resist, Rider Regeneration
//4 (8 mana)          EVIL Immune, Fly

// Create the character
IfSpawned
  IfStateIs0
    // It's an imported spell in hand
    KeepAction
  Else			//Setup monster!
    
    //This is a monster
    MakeNameUnknown
    SetTargetToSelf
    NotAnItem
    
    //Spell effect
    SpawnPoof		  
    
    //Use summoner stats to determine power
    tmpargument = selfstate
    tmpdistance = 255               //255 = Direct XP, ignore any modifications
    GiveExperienceToTarget
        
    //Level 3 or 4 mounts get 25% bonus speed!
    SetTargetToOwner
    tmpdistance = targetwis > 8           //Every point of wisdom adds 1% speed
    tmpx = selfstate
    tmpy = 3
    IfXIsMoreThanY
      tmpdistance = tmpdistance + 25       
    tmpargument = 100 + tmpdistance
    SetContent
    
    //Make him look right
    tmpargument = selfstate - 1
    ChangeArmor
    
    //Tell them what they got
    SetTargetToOwner
    tmpargument = selfstate - 1
    SendMessageNear
    
    //Slow reaction time when summoned
    tmpargument = 50
    SetTime
    
    //Summon sound effect
    tmpargument = 2
    PlaySound
    
    //Show our life and stats
    AddStat

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

//--------------------------------------------------------------------------------------------
// MONSTER AI
tmpargument = 0
IfStateIsNot		//State 0 is spellbook

  // Mount got hurt
  IfAttacked
    tmpargument = rand & 1
    PlaySound
    
    //33% per level not to buckle rider
    tmpx = rand % 100
    tmpy = selfstate * 33
    IfXIsMoreThanY
      DropWeapons

  // Character death
  IfKilled
    tmpargument = 30
    SetBumpHeight		// Make the character body
    tmpargument = 4
    PlaySound

    //Get rid of our rider
    DropWeapons
    
    //Death message
    tmpargument = 7
    SendMessageNear
    
    //Owner gets hurt too!
    SetTargetToOwner
    JoinEvilTeam
    tmpargument = selfstate * 256     //1 damage per level
    DamageTarget

    //Unsummon body
    SpawnPoof
    GoPoof
    
  //Natural attack
  IfUsed
    tmpargument = 3
    PlaySound
    tmpargument = 25
    SetReloadTime

  //New round
  IfTimeOut				// This is done every so often
    SetTargetToOwner
    
    //We are always on our creators side
    tmpargument = targetteam
    JoinTeam
    SetTargetToRider
    IfTargetIsOnHatedTeam
      DropWeapons
    
    tmpx = targetdistance
    tmpy = 512
    IfXIsMoreThanY
      tmpx = rand & 1023 - 512 + ownerx
      tmpy = rand & 1023 - 512 + ownery
      ClearWaypoints
      AddWaypoint
    tmpargument = rand & 63 + 20		  // Wait a while and try again
    SetTime
        
    //Regeneration at level 3 or 4
    tmpx = selfstate
    tmpy = 2
    IfXIsMoreThanY
      tmpargument = 8*selfstate
      HealSelf
      SetTargetToRider
        HealTarget
        PumpTarget
      
    //Movement speed
    GetContent
    SetSpeedPercent
    
  //Bite attack at level 2, 3 and 4
  IfBumped
    tmpx = selfstate
    tmpy = 1
    IfXIsMoreThanY
      SetTargetToWhoeverBumped
      IfTargetIsOnHatedTeam
        IfFacingTarget
          tmpargument = selfstate * 256 + 256     //1 damage +1 per level
          DamageTarget
          tmpargument = 3
          PlaySound

  //Holy shit it flies as well!?
  IfStateIs4
    SetTargetToRider
      
      //Magic sparkles
      tmpx = rand & 1
      tmpy = 0
      IfXIsEqualToY
        tmpargument = 5
        tmpx = selfx
        tmpy = selfy
        tmpdistance = selfz
        SpawnExactParticle
        
      tmpargument = 30
    Else
      tmpargument = 0
    SetFlyHeight


//--------------------------------------------------------------------------------------------
//SPELL AI
IfStateIs0

  // Remove the charge
  IfTakenOut
    tmpargument = 0
    SetContent
    SetTargetToWhoeverIsHolding
      IfTargetIsAPlayer
        tmpargument = 5
        IfNameIsKnown
          tmpargument = 6
        SendMessageNear

  // Allow it to be used
  IfUsed
  
    //Unsummon any existing mount
    UndoEnchant
      tmpargument = 50
      SetReloadTime
      UnsparkleIcon
      
    Else    
      SetTargetToWhoeverIsHolding
      tmpargument = [HMAG]
      IfTargetHasSkillID
        tmpx = selfcontent			//Current spellpower
        tmpy = 2048				      //8 spellpower is max
        IfXIsLessThanY				//Not already max power?
          tmpy = targetmanaflow			//High enough level to continue casting?
          IfXIsLessThanY
            tmpargument = 6				//Casting cost
            CostTargetMana
              GetContent
              tmpargument = tmpargument + 6		//Casting speed
              SetContent

        //Spell effect around caster
        SetTargetToWhoeverIsHolding
          tmpx = targetx
          tmpy = targety
          tmpdistance = targetz
          tmpargument = 3
          SpawnExactParticle
          
      //Tell them they need HMAG
      Else
        tmpargument = 8
        SendMessageNear
        tmpargument = 50
        SetReloadTime
  Else				//Spell released
    tmpx = selfcontent
    tmpy = 512			//Minimum needed casting
    IfXIsMoreThanY
      
      // Make the holder cast it
      SetTargetToWhoeverIsHolding
        tmpargument = ACTIONMC // MC because ZA/ZC spawns an attack... ZA03AL
        TargetDoAction
      
      SetTargetToSelf
          
        //New owner
        SetTargetToWhoeverIsHolding
        SetOwnerToTarget
        
        //Identify as spellbook
        IfTargetIsAPlayer
          IfNameIsKnown
            DoNothing
          Else
            MakeNameKnown
            MakeUsageKnown
            tmpargument = 15
            tmpdistance = EXPDARE
            GiveExperienceToTarget
        
        //Where to summon it
        tmpx = targetx
        tmpy = targety
        tmpturn = rand
        tmpdistance = 256
        Compass
        tmpdistance = targetz
        SpawnCharacterXYZ		//Summon!
          EnchantChild			//Set the drain
            tmpargument = 0 - selfcontent > 4	//How much mana to drain
            tmpdistance = 0
            tmpx = 0
            tmpy = 0
            SetEnchantBoostValues		//Drain is determined by amount of monsters
            tmpargument = selfcontent > 9	//Monster level = (charge-1)/2
            SetChildState
            
            //Sparklies!
            tmpargument = YELLOW
            SparkleIcon
            
          tmpargument = 50
          SetReloadTime		//Delay before next use
    Else
      tmpy = 0
      IfXIsMoreThanY
        // Didn't pump it enough
        IfTargetIsAPlayer
          tmpargument = 4
          SendMessageNear
    
    // Reset the charge counter
    tmpargument = 0
    SetContent
    
  // Return to spellbook, Do last!
  IfDropped
    tmpargument = 0
    SetContent
    BecomeSpellbook
    DisaffirmCharacter
    tmpargument = ACTIONJB
    DoAction
    KeepAction

//--------------------------------------------------------------------------------------------
End
//--------------------------------------------------------------------------------------------
