library SaveXP /*
*************************************************************************************
*
* Saves hero level and hero xp
*
*************************************************************************************
*
* */uses/*
*
* */ NumberStack /* hiveworkshop.com/forums/1993458-post521.html
* */ optional UnitIndexer /*
*
************************************************************************************
*
* function SaveHeroXP takes NumberStack stack, unit whichUnit, integer accuracy returns nothing
* function LoadHeroXP takes NumberStack stack, unit whichUnit, integer accuracy returns nothing
*
*
* Accuracy
*
* Accuracy is how many decimals (up to 2)
*
* accuracy output
* ------------------------
* 0 0%
* 1 5%
* 2 50%
* 3 50.0%
* 4 50.00%
*
************************************************************************************/
globals
private unit dummyUnit
private integer maxLevel
endglobals
private function GetMaxXP takes integer level returns integer
local integer xp
call SetHeroLevel(dummyUnit, level+1, false)
set xp = GetHeroXP(dummyUnit) - 1
call UnitStripHeroLevel(dummyUnit, level+2)
return xp
endfunction
private function GetMinXP takes integer level returns integer
local integer xp
if (1 == level) then
return 0
endif
call SetHeroLevel(dummyUnit, level, false)
set xp = GetHeroXP(dummyUnit)
call UnitStripHeroLevel(dummyUnit, level+1)
return xp
endfunction
private function SaveXP takes NumberStack stack, unit whichUnit, integer accuracy returns nothing
local integer level = GetHeroLevel(whichUnit)
local real max = Pow(10, accuracy)
local integer maxi = R2I(max+.5)
local integer minXp = GetMinXP(level)
local integer maxXp = GetMaxXP(level) - minXp
local integer xp = GetHeroXP(whichUnit) - minXp
local integer percentXp = R2I((xp*max)/maxXp)
call stack.push(percentXp, maxi)
endfunction
private function LoadXP takes NumberStack stack, unit whichUnit, integer accuracy returns nothing
local integer level = GetHeroLevel(whichUnit)
local real max = Pow(10, accuracy)
local integer maxi = R2I(max+.5)
local integer minXp = GetMinXP(level)
local integer maxXp = GetMaxXP(level) - minXp
local integer percentXp = stack.pop(maxi)
local integer xp = R2I(percentXp*maxXp/max)
call AddHeroXP(whichUnit, xp, false)
endfunction
function SaveHeroXP takes NumberStack stack, unit whichUnit, integer accuracy returns nothing
if (GetHeroLevel(whichUnit) != maxLevel and 0 < accuracy) then
call SaveXP(stack, whichUnit, accuracy)
endif
call stack.push(GetHeroLevel(whichUnit)-1,maxLevel-1)
endfunction
function LoadHeroXP takes NumberStack stack, unit whichUnit, integer accuracy returns nothing
local integer level = stack.pop(maxLevel-1)+1
if (level != 1) then
call SetHeroLevel(whichUnit, level, false)
endif
if (level != maxLevel and 0 < accuracy) then
call LoadXP(stack, whichUnit,accuracy)
endif
endfunction
private module InitMod
private static method onInit takes nothing returns nothing
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = false
endif
set dummyUnit = CreateUnit(Player(15), 'Hpal', 0, 0, 270)
call UnitAddAbility(dummyUnit, 'Aloc')
call SetUnitPosition(dummyUnit, 64000, 64000)
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = true
endif
call SetHeroLevel(dummyUnit, 10000, false)
set maxLevel = GetHeroLevel(dummyUnit)
call UnitStripHeroLevel(dummyUnit, 10001)
endmethod
endmodule
private struct Init extends array
implement InitMod
endstruct
endlibrary