HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Spellbook System

07-25-2007, 12:31 PM#1
Antinoid
Hi everyone, i would like to share and improve my Spellbook System i made with you. It is the first stuff i have ever written i vJass(dont have too much experience with Tables and stuff though) but i think the result is pretty good and very usefull in some cases. I use dummy units (sold by trainer) to catch learned spells (if u want to use then normal leveling system u would have to rewrite some things, feel free to use or change my system but give credits to me if u use it in your map please :) . This shouldnt be necassary though cause this system is intended to support multiple spellsbooks on an hero. I use dummy units because the normal leveling system is limited to 11 spells)
Spells can easily be added to the spell array by calling spellid.create(ability,adder,dummy). Thx Anitarf for pointing out stupid faults and helping me to improve the code.
Note: this is written in vJass, so u will need a preprocessor like New Generation WE


here the library

Collapse JASS:
library spellbooksystem initializer ini

 globals
   spellid array spells_ar
   integer spells_total = 0
 endglobals

    struct spellid
      integer abi
      integer adder
      integer lvl=0
      integer id

        static method create takes integer abi, integer adder, integer id returns spellid
            local spellid sp = spellid.allocate()
            local integer i=0
            set spells_ar[spells_total]=sp
            set sp.abi = abi
            set sp.adder = adder
            set sp.id = id
            loop
                exitwhen i>15
                call SetPlayerAbilityAvailable(Player(i),adder,false)
                set i=i+1
            endloop
            set spells_total=spells_total+1
            return sp
        endmethod

        method lvlup takes unit u returns nothing
            set this.lvl=this.lvl+1
             if (this.lvl==1) then
                call UnitAddAbilityBJ(this.adder,u)
             else
                call SetUnitAbilityLevelSwapped(this.abi,u,this.lvl)
             endif
        endmethod
     endstruct

     public function GetSpellId takes unit u returns integer
       local integer i
       local integer n= spells_total
       local spellid spid

       loop
           exitwhen n==0  
           set spid = spells_ar[n]      
           if spid.id == GetUnitTypeId(u) then  
             return n
           endif
             set n=n-1
       endloop
       return 0
      endfunction

    function ini takes nothing returns nothing    
    set spells_ar[1] = spellid.create('A001','A004','u001') //poisonblade  
    set spells_ar[2] = spellid.create('A000','A005','u000') //bloodscalp  
   //add the rawcodes of your ability, adder and dummy here
    endfunction
endlibrary

and the event

Collapse JASS:
function register takes nothing returns nothing 
  local unit u = GetSoldUnit()
  local unit h = HERO[GetPlayerId(GetOwningPlayer(GetSoldUnit()))]
  local integer i = spellsystem_GetSpellId(GetSoldUnit())
  local spellid s = spells_ar[i]
  call s.lvlup(h)                                                                
endfunction

//===========================================================================
function InitTrig_registerlvl takes nothing returns nothing
    set gg_trg_registerlvl = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_registerlvl, EVENT_PLAYER_UNIT_SELL )
    call TriggerAddAction( gg_trg_registerlvl, function register)
endfunction

07-25-2007, 12:51 PM#2
Anitarf
This is really not something you would need textmacros for.
Collapse JASS:
    function setupSpellbookAbility takes integer abi, integer adder, integer id returns spellid
        local spellid cycle = spellid.create()
        local integer i = 0
        set cycle.abi = abi
        set cycle.adder = adder
        set cycle.id = id
        loop 
            exitwhen i>15 //number of max players in your map
            call SetPlayerAbilityAvailable( Player(i), adder, false )
            set i=i+1
        endloop
        return cycle
    endfunction

Also, why rely on users to set up things for the system when the system can do it itself?
Collapse JASS:
    globals
        private spellid array spells_ar
        private integer spells_total = 0
    endglobals

    struct spellid
        integer abi
        integer adder
        integer lvl=0
        integer id

        static method create takes nothing returns spellid
            local spellid sp = spellid.allocate()
            set spells_ar[spells_total]=sp
            set spells_total=spells_total+1
            return sp
        endmethod

        method lvlup takes unit u returns nothing
            set this.lvl=this.lvl+1
            if (this.lvl==1) then
                call UnitAddAbilityBJ(this.adder,u)
            else
                call SetUnitAbilityLevelSwapped(this.abi,u,this.lvl)
            endif
        endmethod
     endstruct

We could actualy move the whole setupSpellbookAbility function into the spellid's create method. Also, the name of the struct is not particularly fitting.
07-25-2007, 01:17 PM#3
Antinoid
Wow, big thx. As said, this is my first vJass script. Will change that when i have time. I didnt know youu could modify the .create method. I also had spells_total=spells_total+1 but forgot to save that or sth Oo. This really helped me :)
07-25-2007, 09:00 PM#4
Pyrogasm
I'll send this over to Triggers and Scripts, shall I?
07-25-2007, 09:12 PM#5
Antinoid
Hm didnt exactly know where i should have posted cause i knew its kinda bad code and not complete.
edit: will probably update tomorrow cause it was a stressy day and i need some sleep.
07-26-2007, 04:02 PM#6
Antinoid
Updated code. Much easier setup now (1 call for 1 new spell).