Name | Type | is_array | initial_value |
//TESH.scrollpos=102
//TESH.alwaysfold=0
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.1.0
One map, one hashtable. Welcome to NewTable 3.1
This library was originally called NewTable so it didn't conflict with
the API of Table by Vexorian. However, the damage is done and it's too
late to change the library name now. To help with damage control, I
have provided an extension library called TableBC, which bridges all
the functionality of Vexorian's Table except for 2-D string arrays &
the ".flush(integer)" method. I use ".flush()" to flush a child hash-
table, because I wanted the API in NewTable to reflect the API of real
hashtables (I thought this would be more intuitive).
API
------------
struct Table
| static method create takes nothing returns Table
| create a new Table
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush all stored values inside of it
|
| method remove takes integer key returns nothing
| remove the value at index "key"
|
| method operator []= takes integer key, $TYPE$ value returns nothing
| assign "value" to index "key"
|
| method operator [] takes integer key returns $TYPE$
| load the value at index "key"
|
| method has takes integer key returns boolean
| whether or not the key was assigned
|
----------------
struct TableArray
| static method operator [] takes integer array_size returns TableArray
| create a new array of Tables of size "array_size"
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush and destroy it
|
| method operator size takes nothing returns integer
| returns the size of the TableArray
|
| method operator [] takes integer key returns Table
| returns a Table accessible exclusively to index "key"
*/
globals
private integer less = 0 //Index generation for TableArrays (below 0).
private integer more = 8190 //Index generation for Tables.
//Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
private hashtable ht = InitHashtable()
private key sizeK
private key listK
endglobals
private struct dex extends array
static method operator size takes nothing returns Table
return sizeK
endmethod
static method operator list takes nothing returns Table
return listK
endmethod
endstruct
private struct handles extends array
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private struct agents extends array
method operator []= takes integer key, agent value returns nothing
call SaveAgentHandle(ht, this, key, value)
endmethod
endstruct
//! textmacro NEW_ARRAY_BASIC takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSaved$SUPER$(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSaved$SUPER$(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//! textmacro NEW_ARRAY takes FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$Handle(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$Handle(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY_BASIC("Real", "Real", "real")
//! runtextmacro NEW_ARRAY_BASIC("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY_BASIC("String", "Str", "string")
//New textmacro to allow table.integer[] syntax for compatibility with textmacros that might desire it.
//! runtextmacro NEW_ARRAY_BASIC("Integer", "Integer", "integer")
//! runtextmacro NEW_ARRAY("Player", "player")
//! runtextmacro NEW_ARRAY("Widget", "widget")
//! runtextmacro NEW_ARRAY("Destructable", "destructable")
//! runtextmacro NEW_ARRAY("Item", "item")
//! runtextmacro NEW_ARRAY("Unit", "unit")
//! runtextmacro NEW_ARRAY("Ability", "ability")
//! runtextmacro NEW_ARRAY("Timer", "timer")
//! runtextmacro NEW_ARRAY("Trigger", "trigger")
//! runtextmacro NEW_ARRAY("TriggerCondition", "triggercondition")
//! runtextmacro NEW_ARRAY("TriggerAction", "triggeraction")
//! runtextmacro NEW_ARRAY("TriggerEvent", "event")
//! runtextmacro NEW_ARRAY("Force", "force")
//! runtextmacro NEW_ARRAY("Group", "group")
//! runtextmacro NEW_ARRAY("Location", "location")
//! runtextmacro NEW_ARRAY("Rect", "rect")
//! runtextmacro NEW_ARRAY("BooleanExpr", "boolexpr")
//! runtextmacro NEW_ARRAY("Sound", "sound")
//! runtextmacro NEW_ARRAY("Effect", "effect")
//! runtextmacro NEW_ARRAY("UnitPool", "unitpool")
//! runtextmacro NEW_ARRAY("ItemPool", "itempool")
//! runtextmacro NEW_ARRAY("Quest", "quest")
//! runtextmacro NEW_ARRAY("QuestItem", "questitem")
//! runtextmacro NEW_ARRAY("DefeatCondition", "defeatcondition")
//! runtextmacro NEW_ARRAY("TimerDialog", "timerdialog")
//! runtextmacro NEW_ARRAY("Leaderboard", "leaderboard")
//! runtextmacro NEW_ARRAY("Multiboard", "multiboard")
//! runtextmacro NEW_ARRAY("MultiboardItem", "multiboarditem")
//! runtextmacro NEW_ARRAY("Trackable", "trackable")
//! runtextmacro NEW_ARRAY("Dialog", "dialog")
//! runtextmacro NEW_ARRAY("Button", "button")
//! runtextmacro NEW_ARRAY("TextTag", "texttag")
//! runtextmacro NEW_ARRAY("Lightning", "lightning")
//! runtextmacro NEW_ARRAY("Image", "image")
//! runtextmacro NEW_ARRAY("Ubersplat", "ubersplat")
//! runtextmacro NEW_ARRAY("Region", "region")
//! runtextmacro NEW_ARRAY("FogState", "fogstate")
//! runtextmacro NEW_ARRAY("FogModifier", "fogmodifier")
//! runtextmacro NEW_ARRAY("Hashtable", "hashtable")
struct Table extends array
// Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
implement realm
implement integerm
implement booleanm
implement stringm
implement playerm
implement widgetm
implement destructablem
implement itemm
implement unitm
implement abilitym
implement timerm
implement triggerm
implement triggerconditionm
implement triggeractionm
implement eventm
implement forcem
implement groupm
implement locationm
implement rectm
implement boolexprm
implement soundm
implement effectm
implement unitpoolm
implement itempoolm
implement questm
implement questitemm
implement defeatconditionm
implement timerdialogm
implement leaderboardm
implement multiboardm
implement multiboarditemm
implement trackablem
implement dialogm
implement buttonm
implement texttagm
implement lightningm
implement imagem
implement ubersplatm
implement regionm
implement fogstatem
implement fogmodifierm
implement hashtablem
method operator handle takes nothing returns handles
return this
endmethod
method operator agent takes nothing returns agents
return this
endmethod
//set this = tb[GetSpellAbilityId()]
method operator [] takes integer key returns Table
return LoadInteger(ht, this, key) //return this.integer[key]
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb) //set this.integer[key] = tb
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key) //return this.integer.has(key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, key) //call this.integer.remove(key)
endmethod
//Remove all data from a Table instance
method flush takes nothing returns nothing
call FlushChildHashtable(ht, this)
endmethod
//local Table tb = Table.create()
static method create takes nothing returns Table
local Table this = dex.list[0]
if this == 0 then
set this = more + 1
set more = this
else
set dex.list[0] = dex.list[this]
call dex.list.remove(this) //Clear hashed memory
endif
debug set dex.list[this] = -1
return this
endmethod
// Removes all data from a Table instance and recycles its index.
//
// call tb.destroy()
//
method destroy takes nothing returns nothing
debug if dex.list[this] != -1 then
debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
debug return
debug endif
call this.flush()
set dex.list[this] = dex.list[0]
set dex.list[0] = this
endmethod
//! runtextmacro optional TABLE_BC_METHODS()
endstruct
//! runtextmacro optional TABLE_BC_STRUCTS()
struct TableArray extends array
//Returns a new TableArray to do your bidding. Simply use:
//
// local TableArray ta = TableArray[array_size]
//
static method operator [] takes integer array_size returns TableArray
local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
local TableArray this = tb[0] //The last-destroyed TableArray that had this array size
debug if array_size <= 0 then
debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
debug return 0
debug endif
if this == 0 then
set this = less - array_size
set less = this
else
set tb[0] = tb[this] //Set the last destroyed to the last-last destroyed
call tb.remove(this) //Clear hashed memory
endif
set dex.size[this] = array_size //This remembers the array size
return this
endmethod
//Returns the size of the TableArray
method operator size takes nothing returns integer
return dex.size[this]
endmethod
//This magic method enables two-dimensional[array][syntax] for Tables,
//similar to the two-dimensional utility provided by hashtables them-
//selves.
//
//ta[integer a].unit[integer b] = unit u
//ta[integer a][integer c] = integer d
//
//Inline-friendly when not running in debug mode
//
method operator [] takes integer key returns Table
static if DEBUG_MODE then
local integer i = this.size
if i == 0 then
call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
return 0
elseif key < 0 or key >= i then
call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
return 0
endif
endif
return this + key
endmethod
//Destroys a TableArray without flushing it; I assume you call .flush()
//if you want it flushed too. This is a public method so that you don't
//have to loop through all TableArray indices to flush them if you don't
//need to (ie. if you were flushing all child-keys as you used them).
//
method destroy takes nothing returns nothing
local Table tb = dex.size[this.size]
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
debug return
debug endif
if tb == 0 then
//Create a Table to index recycled instances with their array size
set tb = Table.create()
set dex.size[this.size] = tb
endif
call dex.size.remove(this) //Clear the array size from hash memory
set tb[this] = tb[0]
set tb[0] = this
endmethod
private static Table tempTable
private static integer tempEnd
//Avoids hitting the op limit
private static method clean takes nothing returns nothing
local Table tb = .tempTable
local integer end = tb + 0x1000
if end < .tempEnd then
set .tempTable = end
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
else
set end = .tempEnd
endif
loop
call tb.flush()
set tb = tb + 1
exitwhen tb == end
endloop
endmethod
//Flushes the TableArray and also destroys it. Doesn't get any more
//similar to the FlushParentHashtable native than this.
//
method flush takes nothing returns nothing
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
debug return
debug endif
set .tempTable = this
set .tempEnd = this + this.size
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
call this.destroy()
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library TimerUtilsEx requires optional Table
/*************************************************
*
* TimerUtilsEx
* v2.1.0.2
* By Vexorian, Bribe & Magtheridon96
*
* Original version by Vexorian.
*
* Flavors:
* Hashtable:
* - RAM: Minimal
* - TimerData: Slow
*
* Array:
* - RAM: Maximal
* - TimerData: Fast
*
* All the functions have O(1) complexity.
* The Array version is the fastest, but the hashtable
* version is the safest. The Array version is still
* quite safe though, and I would recommend using it.
* The system is much slower in debug mode.
*
* Optional Requirement:
* - Table by Bribe
* - hiveworkshop.com/forums/showthread.php?t=188084
*
* API:
* ----
* - function NewTimer takes nothing returns timer
* - Returns a new timer from the stack.
* - function NewTimerEx takes integer i returns timer
* - Returns a new timer from the stack and attaches a value to it.
* - function ReleaseTimer takes timer t returns integer
* - Throws a timer back into the stack. Also returns timer data.
* - function SetTimerData takes timer t, integer value returns nothing
* - Attaches a value to a timer.
* - function GetTimerData takes timer t returns integer
* - Returns the attached value.
*
*************************************************/
// Configuration
globals
// Use hashtable, or fast array?
private constant boolean USE_HASH = false
// Max Number of Timers Held in Stack
private constant integer QUANTITY = 256
endglobals
globals
private timer array tT
private integer tN = 0
endglobals
private module Init
private static method onInit takes nothing returns nothing
static if not USE_HASH then
local integer i = QUANTITY
loop
set i = i - 1
set tT[i] = CreateTimer()
exitwhen i == 0
endloop
set tN = QUANTITY
elseif LIBRARY_Table then
set tb = Table.create()
endif
endmethod
endmodule
// JassHelper doesn't support static ifs for globals.
private struct Data extends array
static if not USE_HASH then
static integer array data
endif
static if LIBRARY_Table then
static Table tb = 0
else
static hashtable ht = InitHashtable()
endif
implement Init
endstruct
// Double free protection
private function ValidTimer takes integer i returns boolean
static if LIBRARY_Table then
return Data.tb.boolean[-i]
else
return LoadBoolean(Data.ht, i, 1)
endif
endfunction
private function Get takes integer id returns integer
debug if not ValidTimer(id) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[TimerUtils]Error: Tried to get data from invalid timer.")
debug endif
static if USE_HASH then
static if LIBRARY_Table then
return Data.tb[id]
else
return LoadInteger(Data.ht, id, 0)
endif
else
return Data.data[id - 0x100000]
endif
endfunction
private function Set takes integer id, integer data returns nothing
debug if not ValidTimer(id) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[TimerUtils]Error: Tried to attach data to invalid timer.")
debug endif
static if USE_HASH then
static if LIBRARY_Table then
set Data.tb[id] = data
else
call SaveInteger(Data.ht, id, 0, data)
endif
else
set Data.data[id - 0x100000] = data
endif
endfunction
function SetTimerData takes timer t, integer data returns nothing
call Set(GetHandleId(t), data)
endfunction
function GetTimerData takes timer t returns integer
return Get(GetHandleId(t))
endfunction
function NewTimerEx takes integer data returns timer
local integer id
if tN == 0 then
static if USE_HASH then
set tT[0] = CreateTimer()
else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[TimerUtils]Error: No Timers In The Stack! You must increase 'QUANTITY'")
return null
endif
else
set tN = tN - 1
endif
set id = GetHandleId(tT[tN])
static if LIBRARY_Table then
set Data.tb.boolean[-id] = true
else
call SaveBoolean(Data.ht, id, 1, true)
endif
call Set(id, data)
return tT[tN]
endfunction
function NewTimer takes nothing returns timer
return NewTimerEx(0)
endfunction
function ReleaseTimer takes timer t returns integer
local integer id = GetHandleId(t)
local integer data = 0
// Pause the timer just in case.
call PauseTimer(t)
// Make sure the timer is valid.
if ValidTimer(id) then
// Get the timer's data.
set data = Get(id)
// Unmark handle id as a valid timer.
static if LIBRARY_Table then
call Data.tb.boolean.remove(-id)
else
call RemoveSavedBoolean(Data.ht, id, 1)
endif
//If it's not run in USE_HASH mode, this next block is useless.
static if USE_HASH then
//At least clear hash memory while it's in the recycle stack.
static if LIBRARY_Table then
call Data.tb.remove(id)
else
call RemoveSavedInteger(Data.ht, id, 0)
endif
// If the recycle limit is reached
if tN == QUANTITY then
// then we destroy the timer.
call DestroyTimer(t)
return data
endif
endif
//Recycle the timer.
set tT[tN] = t
set tN = tN + 1
//Tried to pass a bad timer.
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[TimerUtils]Error: Tried to release non-active timer!")
endif
//Return Timer Data.
return data
endfunction
endlibrary
//TESH.scrollpos=48
//TESH.alwaysfold=0
//===========================================================================
// vJASS version of weaaddar's GetItemCostById system
//===========================================================================
//
// * Uses Table system
// * Fixed an issue that could happen if item cost was zero for both gold and lumber
// * This version is compatible with original one
// * Added some helper functions: GetItemGoldCost, GetItemLumberCost, GetItemGoldCostById, GetItemLumberCostById
//
//===========================================================================
library GetItemCostById initializer Init uses Table
globals
private constant player BuyerPlayer = Player(15)
private constant integer BuyerTypeId = 'hpal'
private constant integer ShopTypeId = 'nshe'
private constant integer MAX_PRICE = 50000
// check Init function
private Table GoldPrice = 0
private Table LumberPrice = 0
private unit Buyer = null
private unit Shop = null
endglobals
//===========================================================================
// whichtype == true --> gold
// whichtype == false --> lumber
//===========================================================================
function GetItemCostById takes integer id, boolean whichtype returns integer
local integer gold
local integer lumber
if GoldPrice.has(id) then
set gold = GoldPrice[id]
set lumber = LumberPrice[id]
else
call SetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_GOLD, MAX_PRICE)
call SetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_LUMBER, MAX_PRICE)
call AddItemToStock(Shop, id, 1, 1)
call IssueNeutralImmediateOrderById(BuyerPlayer, Shop, id)
call RemoveItem(UnitItemInSlot(Buyer, 0))
call RemoveItemFromStock(Shop, id)
set gold = MAX_PRICE - GetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_GOLD)
set lumber = MAX_PRICE - GetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_LUMBER)
set GoldPrice[id] = gold
set LumberPrice[id] = lumber
endif
if whichtype then
return gold
else
return lumber
endif
endfunction
//===========================================================================
function GetItemGoldCost takes item i returns integer
return GetItemCostById(GetItemTypeId(i), true)
endfunction
//===========================================================================
function GetItemLumberCost takes item i returns integer
return GetItemCostById(GetItemTypeId(i), false)
endfunction
//===========================================================================
function GetItemGoldCostById takes integer id returns integer
return GetItemCostById(id, true)
endfunction
//===========================================================================
function GetItemLumberCostById takes integer id returns integer
return GetItemCostById(id, false)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local rect r = GetWorldBounds()
set Buyer = CreateUnit(BuyerPlayer, BuyerTypeId, 0, 0, 0)
call SetUnitX(Buyer, GetRectMaxX(r))
call SetUnitY(Buyer, GetRectMaxY(r))
set Shop = CreateUnit(BuyerPlayer, ShopTypeId, 0, 0, 0)
call SetUnitX(Shop,GetRectMaxX(r))
call SetUnitY(Shop,GetRectMaxY(r))
call UnitAddAbility(Shop, 'Asid')
call UnitRemoveAbility(Shop, 'Awan')
call UnitAddAbility(Shop, 'Aloc')
set GoldPrice = Table.create()
set LumberPrice = Table.create()
call RemoveRect(r)
set r = null
endfunction
endlibrary
//TESH.scrollpos=57
//TESH.alwaysfold=0
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