- Joined
- Jul 14, 2011
- Messages
- 3,213
Hi!
I was helping kaizar with a request and I came up with this system that takes a string, and converts it into Ability Add/Upgrade.
The user inputs a string like this
Then the system checks if the Ability is already in the unit and, if the unit has the ability, it increases the ability level, else, adds the ability.
In the process, i found that War3 default func "S2I" only works when the string has numbers, and that StringHash doesn't returns the value anyone would think. After several time looking around i found AceHart functions to successfully convert Strings to Integers, the rest is just a simple string analisys.
I'd like some experienced users take a look at this and tell me if it's a valid/usefull enough resource as to share.
AceHartLibrary
EDIT1: Improved the library a bit a some details on the main func.
I was helping kaizar with a request and I came up with this system that takes a string, and converts it into Ability Add/Upgrade.
The user inputs a string like this
"03, 'Abi1', 'Abi2', 'Abi3'"
where "03" refers to the number of abilities to manipulate in that "level-up" event, and the rest are the abilities to handle.Then the system checks if the Ability is already in the unit and, if the unit has the ability, it increases the ability level, else, adds the ability.
In the process, i found that War3 default func "S2I" only works when the string has numbers, and that StringHash doesn't returns the value anyone would think. After several time looking around i found AceHart functions to successfully convert Strings to Integers, the rest is just a simple string analisys.
I'd like some experienced users take a look at this and tell me if it's a valid/usefull enough resource as to share.
JASS:
// No need to touch this
globals
hashtable Spart_AbilHash = InitHashtable()
endglobals
// Add your abilities here
function UnitLevelUpData takes nothing returns nothing
local hashtable h = Spart_AbilHash
// call SaveStr(h, // Must remain like it's. Don't touch it
// 'Hpal' references to a Unit-Type (In this case, 'Hpal' references to Human Paladin). You can see Object Id's by pressing CTRL+D in the Object Editor
// The "2", "7", "10", and "15" references to the level where the unit acquires/upgrades the abilities
// The string to save the abilities has the following composition:
// The first 2 characters MUST BE 2 NUMBERS between 00 and 99
// Then comes a coma and a blank space
// Then comes the first ability 'Abi1'
// If there are no more abilities, close the string with a double quote.
// Else, if there are more abilities, just add a coma, a blank space, and the other ability.
// | Function | Hash | Unit-TypeID | Level | ##, Abilities
call SaveStr(h, 'Hpal', 2, "01, 'Abi1'")
call SaveStr(h, 'Hpal', 7, "02, 'Abi2', 'Abi3'")
call SaveStr(h, 'Hpal', 10, "04, 'Abi4', 'Abi5', 'Abi6', 'Abi7'")
call SaveStr(h, 'Hpal', 15, "03, 'Abi8', 'Abi9', 'AbiX'")
set h = null
endfunction
// No need to touch this
function Trig_UnitLevelUp_Actions takes nothing returns nothing
local unit u = GetTriggerUnit()
local string s = LoadStr(Spart_AbilHash, GetUnitTypeId(u), GetUnitLevel(u))
local integer AbilCount = S2I(SubString(s, 0, 2))
local integer abil
local integer StrStart = 5
local integer StrEnd = 9
local integer looper = 1
loop
exitwhen looper > AbilCount
set abil = String2Id(SubString(s, StrStart, StrEnd))
if GetUnitAbilityLevel(u, abil) > 0 then
call IncUnitAbilityLevel(u, abil)
else
call UnitAddAbility(u, abil)
endif
set StrStart = StrEnd + 4
set StrEnd = StrStart + 4
set looper = looper + 1
endloop
set u = null
set s = null
endfunction
//===========================================================================
function InitTrig_UnitLevelUp takes nothing returns nothing
set gg_trg_UnitLevelUp = CreateTrigger( )
call TriggerRegisterAnyUnitEventBJ( gg_trg_UnitLevelUp, EVENT_PLAYER_HERO_LEVEL )
call TriggerAddAction( gg_trg_UnitLevelUp, function Trig_UnitLevelUp_Actions )
call UnitLevelUpData()
endfunction
AceHartLibrary
JASS:
// Credits to AceHart for this.
library Char2IdLib
// No need to touch this. Credits to AceHart for this.
function Char2Id takes string c returns integer
local integer i = 0
local string abc = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
local string t
loop
set t = SubString(abc,i,i + 1)
exitwhen t == null or t == c
set i = i + 1
endloop
if i < 10 then
return i + 48
elseif i < 36 then
return i + 55
endif
return i + 61
endfunction
// No need to touch this. Credits to AceHart for this.
function String2Id takes string s returns integer
return ((Char2Id(SubString(s,0,1)) * 256 + Char2Id(SubString(s,1,2))) * 256 + Char2Id(SubString(s,2,3))) * 256 + Char2Id(SubString(s,3,4))
endfunction
endlibrary
EDIT1: Improved the library a bit a some details on the main func.
Last edited: