Name | Type | is_array | initial_value |
dialog | dialog | No | |
dialogButton | button | Yes | |
IsUnitPreplaced | boolean | Yes | |
UDex | integer | No | |
UDexGen | integer | No | |
UDexNext | integer | Yes | |
UDexPrev | integer | Yes | |
UDexRecycle | integer | No | |
UDexUnits | unit | Yes | |
UDexWasted | integer | No | |
unit | unit | No | |
UnitIndexerEnabled | boolean | No | |
UnitIndexEvent | real | No | |
UnitRefundAbility | abilcode | No | |
UnitRefundGoldCost | integer | Yes | |
UnitRefundRatio | real | No | |
UnitRefundReqHP | real | No | |
UnitRefundWoodCost | integer | Yes |
library UnitRefundLib requires optional Table /*
v 1.0
Made by: Pinzu
A simple spell for selling units and return a portion of the investment cost.
*/
// ---------------------------------------------------------------------------------------- \\
// Configurable Block \\
// ---------------------------------------------------------------------------------------- \\
globals
private real refund_ratio = 0.1 // Default ratio
private constant integer REFUND_ABIL = 'A001' // Refund ability
private constant boolean KILL = true // If the unit should be killed or removed when sold
private constant real HP_REQ = 0.50 // Minimum required hp for selling unit
endglobals
native GetUnitGoldCost takes integer unitid returns integer
native GetUnitWoodCost takes integer unitid returns integer
//! novjass
API
------------
/*
Sets availability of refund spell.
*/
function SetRefundAvailable takes boolean avail returns nothing
/*
Sets the current refund ratio.
*/
function SetRefundRatio takes real ratio returns nothing
/*
Saves the unit types gold and lumber cost.
*/
function SaveUnitTypeCost takes integer unitType, integer gold, integer wood returns nothing
/*
Saves the units gold and lumber cost overriding the unit type costs.
*/
function SaveUnitCost takes unit u, integer gold, integer wood returns nothing
/*
Clears the gold and lumber cost saved for a specific unit, required to prevent leaks if costs are saved to specific units.
*/
function ClearUnitCost takes unit u returns nothing
/*
Returns the units gold cost based on the unit.
*/
function UnitGoldCost takes unit u returns integer
/*
Returns the units lumber cost based on the unit.
*/
function UnitWoodCost takes unit u returns integer
/*
Returns the amount of gold gained from selling a unit.
*/
function GetUnitRefundGold takes unit u returns integer
/*
Returns the amount of lumber gained from selling a unit
*/
function GetUnitRefundWood takes unit u returns integer
//! endnovjass
function SetRefundAvailable takes boolean avail returns nothing
local integer i = 0
loop
exitwhen i == bj_MAX_PLAYERS
call SetPlayerAbilityAvailable(Player(i), REFUND_ABIL, avail)
set i = i + 1
endloop
endfunction
function SetRefundRatio takes real ratio returns nothing
if refund_ratio == 0 and ratio != 0 then
call SetRefundAvailable(true)
else
if ratio == 0. then
call SetRefundAvailable(false)
endif
endif
set refund_ratio = ratio
// Changes the extended tooltip
if HP_REQ > 0. then
call BlzSetAbilityExtendedTooltip(REFUND_ABIL, "Refunds the unit for " + I2S(R2I(refund_ratio * 100)) + "% of its purchasing cost. Requires " + /*
*/ I2S(R2I(HP_REQ * 100)) + "% health.", 1)
else
call BlzSetAbilityExtendedTooltip(REFUND_ABIL, "Refunds the unit for " + I2S(R2I(refund_ratio * 100)) + "% of its purchasing cost.", 1)
endif
endfunction
static if LIBRARY_Table then
globals
private integer tableId
endglobals
function SaveUnitTypeCost takes integer unitType, integer gold, integer wood returns nothing
if gold > 0 then
set Table(tableId)[unitType] = gold
endif
if wood > 0 then
set Table(tableId)[-unitType] = wood
endif
endfunction
function SaveUnitCost takes unit u, integer gold, integer wood returns nothing
local integer id = GetHandleId(u)
set table[id] = gold
set table[-id] = wood
endfunction
function ClearUnitCost takes unit u returns nothing
local integer id = GetHandleId(u)
call table.remove(id)
call table.remove(-id)
endfunction
function UnitGoldCost takes unit u returns integer
local integer gold = Table(tableId)[GetHandleId(u)]
if gold != 0 then
return gold
endif
return Table(tableId)[GetUnitTypeId(u)]
endfunction
function UnitWoodCost takes unit u returns integer
local integer wood = Table(tableId)[-GetHandleId(u)]
if wood != 0 then
return wood
endif
return Table(tableId)[-GetUnitTypeId(u)]
endfunction
else
globals
private hashtable hash
endglobals
function SaveUnitTypeCost takes integer unitType, integer gold, integer wood returns nothing
if gold > 0 then
call SaveInteger(hash, 0, unitType, gold)
endif
if wood > 0 then
call SaveInteger(hash, 0, -unitType, wood)
endif
endfunction
function SaveUnitCost takes unit u, integer gold, integer wood returns nothing
local integer id = GetHandleId(u)
call SaveInteger(hash, id, 0, gold)
call SaveInteger(hash, -id, 0, wood)
endfunction
function ClearUnitCost takes unit u returns nothing
local integer id = GetHandleId(u)
call FlushChildHashtable(hash, id)
call FlushChildHashtable(hash, -id)
endfunction
function UnitGoldCost takes unit u returns integer
local integer gold = LoadInteger(hash, GetHandleId(u), 0)
if gold != 0 then
return gold
endif
return LoadInteger(hash, 0, GetUnitTypeId(u))
endfunction
function UnitWoodCost takes unit u returns integer
local integer wood = LoadInteger(hash, -GetHandleId(u), 0)
if wood != 0 then
return wood
endif
return LoadInteger(hash, 0, -GetUnitTypeId(u))
endfunction
endif
function GetUnitRefundGold takes unit u returns integer
return R2I(UnitGoldCost(u)*refund_ratio)
endfunction
function GetUnitRefundWood takes unit u returns integer
return R2I(UnitWoodCost(u)*refund_ratio)
endfunction
struct UnitRefund
private static method onSpellFinish takes nothing returns boolean
local unit u = GetFilterUnit()
local player p = GetOwningPlayer(u)
if GetUnitState(u, UNIT_STATE_LIFE)/GetUnitState(u, UNIT_STATE_MAX_LIFE) >= HP_REQ then
call SetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD) + GetUnitRefundGold(u))
call SetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER) + GetUnitRefundWood(u))
static if KILL then
call ShowUnit(u, false)
call KillUnit(u)
else
call RemoveUnit(u)
endif
else
// Invalid command
endif
set p = null
set u = null
return false
endmethod
private static method onInit takes nothing returns nothing
local integer i = 0
local trigger t = CreateTrigger()
loop
exitwhen i == bj_MAX_PLAYERS
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_SPELL_FINISH, function thistype.onSpellFinish)
set i = i + 1
endloop
set t = null
static if LIBRARY_Table then
set tableId = Table.create()
else
set hash = InitHashtable()
endif
call SetRefundRatio(refund_ratio) // configure default
endmethod
endstruct
endlibrary