OnInit.global("Refund", function(require)
local REFUND_RETURN_RATE_GOLD = .50 -- Set the percentage return rate
local REFUND_RETURN_RATE_WOOD = .50 -- Set the percentage return rate
local HITPOINTS_REQUIRED = 100 -- Set the required life percentage of the building you are refunding
local MESSAGE = "|cff808080This building is hurt and may not be refunded." -- Set the failure message for the player
local SFX = "UI\\Feedback\\GoldCredit\\GoldCredit.mdl" -- Set the special effect model
local REFUND_ABILITY_ID = FourCC('A000') -- Set refund ability raw code
local WANT_HP_CHECK = true -- If you want to check for hitpoints, set this to true. If you do not want to check hitpoints, set it to false.
local t = CreateTrigger()
TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
TriggerAddAction(t, function()
local u = GetTriggerUnit()
if GetSpellAbilityId() ~= REFUND_ABILITY_ID then return end
local p = GetOwningPlayer(u)
local id = GetUnitTypeId(u)
local unitX = GetUnitX(u)
local unitY = GetUnitY(u)
if not WANT_HP_CHECK or GetUnitLifePercent(u) >= HITPOINTS_REQUIRED then
local goldAmount, woodAmount = math.floor((GetUnitGoldCost(id) * REFUND_RETURN_RATE_GOLD) + 0.5), math.floor((GetUnitWoodCost(id) * REFUND_RETURN_RATE_WOOD) + 0.5)
SetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD) + goldAmount)
SetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER) + woodAmount)
local sfx = AddSpecialEffectTarget(SFX, u, "overhead")
DestroyEffect(sfx)
KillUnit(u)
local tg, tl = CreateTextTag(), CreateTextTag()
-- Gold Text Tag
SetTextTagText(tg, "(+" .. goldAmount .. ")", 0.020)
SetTextTagPos(tg, unitX, unitY, 0.00)
SetTextTagColor(tg, 255, 220, 0, 255)
SetTextTagVelocity(tg, 0, .02)
SetTextTagVisibility(tg, true)
SetTextTagFadepoint(tg, 0.10)
SetTextTagLifespan(tg, 1.5)
SetTextTagPermanent(tg, false)
-- Lumber Text Tag
SetTextTagText(tl, "(+" .. woodAmount .. ")", 0.020)
SetTextTagPos(tl, unitX, unitY - 50, 0.00)
SetTextTagColor(tl, 0, 255, 0, 0)
SetTextTagVelocity(tl, 0, -0.02)
SetTextTagVisibility(tl, true)
SetTextTagFadepoint(tl, 0.10)
SetTextTagLifespan(tl, 1.5)
SetTextTagPermanent(tl, false)
else
DisplayTextToPlayer(p, 0, 0, MESSAGE)
end
end)
end)