library ItemRefund initializer OnInit uses Table, GetItemCostById, TimerUtilsEx
/*
------------------------------------------------------------------------------------
Item Refund System v1.04 by Shadow Flux
------------------------------------------------------------------------------------
Importing this system to your map will make any item have a custom selling cost
based on the item's gold cost percentage for timeOut (10 by default) seconds
NOTE:
- If REFUND_RATE > RETURN_RATE in configurables, then a player selling an item
will have extra gold if it was sold within the allocated TIME_OUT. Useful if
players accidentally bought an unwanted item, allowing them to sell it immediately
for the same value (by default) or less (depending on REFUND_RATE).
- If REFUND_RATE < RETURN_RATE in configurables, then a player selling an item
will have less gold if it was sold within the allocated TIME_OUT. Useful if you
want players to own the item for certain time before they can sell it at a
much greater gold.
- If REFUND_RATE = RETURN_RATE, gold will not get affected.
- This system will not work for items having a less than zero gold cost.
---------------------------------------------------------------------
| SPECIAL THANKS TO weaaddar for the GetItemCostById library |
| SPECIAL THANKS TO Bribe for the New Table library |
| SPECIAL THANKS TO Magtheridon96 for the TimerUtilsEx library |
---------------------------------------------------------------------
HOW TO USE:
1. Just copy the 4 systems (Table, GetItemCostById, TimerUtilsEx, ItemRefund) to your map if they
are not existing yet.
2. Go to Advanced > Gameplay Constant > Inventory - Sell Item Return Rate.
Make sure that the variable RETURN_RATE below is the same as the Inventory - Sell Item Return Rate.
REQUIRES:
Jass NewGen Pack, Table, GetItemCostById, TimerUtilsEx
*/
//======================== CONFIGURABLES =================================
globals
//Players has 10 seconds before the item will no longer be refundable
private constant real TIME_OUT = 10
//Refund Rate of 1 means players will get 100% of the item's total cost
//Refund Rate of 0 means players will get 0% of the item's total cost
private constant real REFUND_RATE = 1.0
//Input the Return Rate defined in the Gameplay Constant
private constant real RETURN_RATE = 0.5
endglobals
//SYSTEM VARIABLES, DO NOT TOUCH UNLESS YOU KNOW WHAT YOU'RE DOING
globals
private Table IRS_Data //Item Refund System Data
private boolean b = REFUND_RATE > RETURN_RATE //boolean for determining what string to use
endglobals
private function GoldText takes integer goldToAdd, unit u, string s returns nothing
local texttag t = CreateTextTag()
call SetTextTagPermanent(t, false)
call SetTextTagLifespan(t, 3)
call SetTextTagFadepoint(t, 2.5)
call SetTextTagPos(t, GetUnitX(u), GetUnitY(u), -60)
call SetTextTagText(t, "|cffffcc00" + s + I2S(goldToAdd), 0.024)
call SetTextTagVelocity(t, 0, 0.03)
set t = null
endfunction
private function OnExpire takes nothing returns nothing
//Remove the value so that IRS_Data.has(GetHandleId(it)) = false
local timer t = GetExpiredTimer()
call IRS_Data.remove(GetTimerData(t))
call ReleaseTimer(t)
set t = null
endfunction
private function OnSellingToShop takes nothing returns boolean
local item it = GetSoldItem()
local integer hId = GetHandleId(it)
local integer currentGold
local integer goldToAdd
local player p
local integer itemCost
//IF ITEM IS STILL REFUNDALBE
if IRS_Data.has(hId) then
call IRS_Data.remove(hId)
set itemCost = GetItemGoldCost(it)
if itemCost > 0 then
set p = GetTriggerPlayer()
set currentGold = GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD)
//The equation below is not simplified because of Rounding Down of gold cost when an item is sold
set goldToAdd = R2I(itemCost*(REFUND_RATE)) - R2I(itemCost*(RETURN_RATE))
call SetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD, currentGold + goldToAdd)
if b then
call GoldText(goldToAdd, GetTriggerUnit(), "+")
else
call GoldText(goldToAdd, GetTriggerUnit(), "")
endif
set p = null
endif
endif
set it = null
return false
endfunction
private function OnPurchaseFromShop takes nothing returns boolean
local item it = GetSoldItem()
local integer hId = GetHandleId(it)
call TimerStart(NewTimerEx(hId), TIME_OUT, false, function OnExpire)
set IRS_Data[hId] = 1 //Having a value means item is refundable
set it = null
return false
endfunction
//===========================================================================
private function OnInit takes nothing returns nothing
local integer i
local trigger t1 = CreateTrigger()
local trigger t2 = CreateTrigger()
set i = 0
loop
call TriggerRegisterPlayerUnitEvent(t1, Player(i), EVENT_PLAYER_UNIT_SELL_ITEM, null)
call TriggerRegisterPlayerUnitEvent(t2, Player(i), EVENT_PLAYER_UNIT_PAWN_ITEM, null)
set i = i + 1
exitwhen i == bj_MAX_PLAYER_SLOTS
endloop
call TriggerAddCondition(t1, Condition(function OnPurchaseFromShop))
call TriggerAddCondition(t2, Condition(function OnSellingToShop))
set IRS_Data = Table.create()
set t1 = null
set t2 = null
endfunction
endlibrary