Name | Type | is_array | initial_value |
u | unit | No |
//TESH.scrollpos=0
//TESH.alwaysfold=0
library BagSystem requires Alloc, Table, RegisterPlayerUnitEvent
//
// The_Witcher 's
// Bag System
//
// With this System you're able to give as many bags as you want to EVERY unit you want!
// The unit only needs to have the Inventory(hero) ability for an inventory with 6 slots
// you just need to create two useable items: one for next bag and one for previous bag
//
// you can add and remove bags whenever you want
//
// Requirements:
// Alloc
// RegisterPlayerUnitEvent
// Table
//
// struct Bag:
// create takes unit u, integer maxbags
// if you want a unit to use bags, you need to init that with this function
//
// addBags takes integer bags
// with this function you can increase or decrease the amount of bags (add a negative number for the 'bags' value to lower it)
// if there are items in the removed bag they will be dropped
//
// destroy
// this function simply removes every bag from the unit
// if you want the unit to use bags again, you have to use create once again
//
// Bag[myUnit]
// the [] operator returns the Bag instance bound to the given unit
//---------------------------------------------------------
//---------------- SETUP --------------
//---------------------------------------------------------
globals
// this is the rawcode of the item for next bag
private constant integer NEXT_BAG_ID = 'I001'
// this is the rawcode of the item for previous bag
private constant integer PREV_BAG_ID = 'I002'
// false= on previous bag is the number of the previous and on next bag the number of the next bag
// true = on previous and next bag is the number of the current bag
private constant boolean DISPLAY_CURRENT_BAG = true
// whether to use a hook to destroy Bags automatically if the owner is removed
private constant boolean REMOVE_HOOK = true
endglobals
//---------------------------------------------------------
//-----------Don't modify anything below this line---------
//---------------------------------------------------------
struct Bag extends array
private static Table table
readonly unit unit
private Table items
readonly integer max
readonly integer bag
implement Alloc
static method operator [] takes unit u returns Bag
return table[GetHandleId(u)]
endmethod
private method saveCurrentBag takes nothing returns nothing
local integer i = 0
loop
exitwhen i >= 4
set items[bag * 8 + i * 2] = GetItemTypeId(UnitItemInSlot(unit, i))
set items[bag * 8 + i * 2 + 1] = GetItemCharges(UnitItemInSlot(unit, i))
call RemoveItem(UnitItemInSlot(unit, i))
set i = i + 1
endloop
endmethod
private method refreshBagNumbers takes nothing returns nothing
local integer next = bag + 1
local integer prev = bag + 1
call RemoveItem(UnitItemInSlot(unit, 4))
call RemoveItem(UnitItemInSlot(unit, 5))
call UnitAddItemToSlotById(unit, PREV_BAG_ID, 4)
call UnitAddItemToSlotById(unit, NEXT_BAG_ID, 5)
if not DISPLAY_CURRENT_BAG then
if bag >= max - 1 then
set prev = IMaxBJ(1, bag)
set next = 1
elseif bag <= 0 then
set prev = max
set next = 2
else
set prev = bag
set next = bag + 2
endif
endif
call SetItemCharges(UnitItemInSlot(unit, 4), prev)
call SetItemCharges(UnitItemInSlot(unit, 5), next)
endmethod
private method dropLastBag takes nothing returns nothing
local integer i = 0
local item it
local real x = GetUnitX(unit)
local real y = GetUnitY(unit)
loop
exitwhen i >= 8
set it = CreateItem(items[(max - 1) * 8 + i], x, y)
call SetItemCharges(it, items[(max - 1) * 8 + i + 1])
set items[(max - 1) * 8 + i] = 0
set items[(max - 1) * 8 + i + 1] = 0
set i = i + 2
endloop
set it = null
endmethod
method addBags takes integer bags returns nothing
local integer s = max + bags
if s < 1 then
set s = 1
endif
if (bags < 0) then
call saveCurrentBag()
if (bag >= s) then
set bag = s - 1
endif
loop
exitwhen max == s
call dropLastBag()
set max = max - 1
endloop
else
set max = max + bags
endif
call refreshBagNumbers()
endmethod
static method create takes unit u, integer maxbags returns Bag
local Bag this = allocate()
set unit = u
set max = maxbags
set bag = 0
set items = Table.create()
call refreshBagNumbers()
set table[GetHandleId(u)] = this
return this
endmethod
method destroy takes nothing returns nothing
local integer i = 0
call saveCurrentBag()
loop
exitwhen i > 5
call RemoveItem(UnitItemInSlot(unit, i))
set i = i + 1
endloop
loop
exitwhen max <= 0
call dropLastBag()
set max = max - 1
endloop
set table[GetHandleId(unit)] = 0
call items.destroy()
call deallocate()
endmethod
private static method onBagChange takes nothing returns nothing
local unit u = GetTriggerUnit()
local Bag b = table[GetHandleId(u)]
local integer i = 0
local integer itemID = GetItemTypeId(GetManipulatedItem())
if (itemID == PREV_BAG_ID) or (itemID == NEXT_BAG_ID) then
call b.saveCurrentBag()
if itemID == PREV_BAG_ID then
set b.bag = b.bag - 1
if b.bag < 0 then
set b.bag = b.max - 1
endif
else
set b.bag = b.bag + 1
if b.bag >= b.max then
set b.bag = 0
endif
endif
loop
exitwhen i >= 4
call UnitAddItemToSlotById(u, b.items[b.bag * 8 + i * 2], i)
call SetItemCharges(UnitItemInSlot(u, i), b.items[b.bag * 8 + i * 2 + 1])
set i = i + 1
endloop
call b.refreshBagNumbers()
endif
set u = null
endmethod
private static method onInit takes nothing returns nothing
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_USE_ITEM, function Bag.onBagChange)
set table = Table.create()
endmethod
endstruct
static if (REMOVE_HOOK) then
private function removeUnitHook takes unit u returns nothing
if (Bag[u] != 0) then
call Bag[u].destroy()
endif
endfunction
hook RemoveUnit removeUnitHook
endif
endlibrary
//Code indented using The_Witcher's Script Language Aligner
//Download the newest version and report bugs at www.hiveworkshop.com
//TESH.scrollpos=0
//TESH.alwaysfold=0
library AdvancedItemHandlingSystem requires Alloc, RegisterPlayerUnitEvent, Table
// The_Witcher's
//
// <<advanced item system>>
//
// Requirements:
// Alloc
// RegisterPlayerUnitEvent
// Table
//
// This system improves the Warcraft III item engine
//
// If a unit gets an item which it already has it stacks (only if item has charges!)
// In addition this works even if the inventory is full!!!
//
// If you double-right-click an item with charges in your inventory it splits up
// and if you move an item onto an equal one and they are stackable they stack
//
// No Setup Part! Don't touch anything -> fully automatic :)
private struct DataStruct extends array
private static DataStruct array all
private static integer total = 0
private static Table table
private static boolean systemOrder = false
private static timer timer
unit u
item ite
integer index
real x
real y
implement Alloc
private method destroy takes nothing returns nothing
set table[GetHandleId(u)] = 0
set total = total - 1
set all[index] = all[total]
set all[index].index = index
call deallocate()
if (total == 0) then
call PauseTimer(timer)
endif
endmethod
private static method isInventoryFull takes unit u returns boolean
local integer i = 0
loop
exitwhen i == 6
if UnitItemInSlot(u, i) == null then
return false
endif
set i = i + 1
endloop
return true
endmethod
private static method itemWalk takes nothing returns nothing
local integer i = 0
local unit u = GetTriggerUnit()
local item ite = GetOrderTargetItem()
local DataStruct this
if isInventoryFull(u) and GetItemCharges(ite) != 0 then
loop
exitwhen i > 5
if GetItemTypeId(ite) == GetItemTypeId(UnitItemInSlot(u, i)) and UnitHasItem(u, ite) == false then
call IssuePointOrder( u, "move", GetItemX(ite), GetItemY(ite) )
if table[GetHandleId(u)] == 0 then
set this = allocate()
set all[total] = this
set index = total
set total = total + 1
set .u = u
set .ite = ite
set x = GetItemX(ite)
set y = GetItemY(ite)
set table[GetHandleId(u)] = this
if (total == 1) then
call TimerStart( timer, 0.05, true, function DataStruct.itemTake )
endif
else
set this = table[GetHandleId(u)]
set .u = u
set .ite = ite
set x = GetItemX(ite)
set y = GetItemY(ite)
endif
set i = 100
endif
set i = i + 1
endloop
set this = table[GetHandleId(u)]
if i < 10 and this != 0 then
call destroy()
endif
endif
set u = null
set ite = null
endmethod
private static method itemStack takes nothing returns nothing
local integer i = 0
local integer id
local item k
local item m = GetManipulatedItem()
local unit u = GetTriggerUnit()
local integer ite = GetItemTypeId(m)
local integer c = GetItemCharges(m)
local integer it
if not systemOrder and GetItemCharges(GetManipulatedItem()) > 0 then
loop
exitwhen i > 6
set it = GetItemTypeId(UnitItemInSlot(u, i - 1))
set k = UnitItemInSlot(u, i - 1)
if ( ( it == ite ) and( m != k ) ) then
call SetItemCharges( k, GetItemCharges(k) + c )
call RemoveItem( m )
set i = 10
endif
set i = i + 1
endloop
endif
set k = null
set m = null
set u = null
endmethod
private static method itemTake takes nothing returns nothing
local real dx
local real dy
local item it
local integer i = 0
local integer j
local DataStruct this
loop
exitwhen i >= total
set this = all[i]
set dx = GetItemX(ite) - GetUnitX(u)
set dy = GetItemY(ite) - GetUnitY(u)
if ( dx * dx + dy * dy < 10000 ) and IsItemOwned(ite) == false then
set systemOrder = true
call IssueImmediateOrder(u, "stop")
set systemOrder = false
set j = 0
loop
exitwhen j > 5
set it = UnitItemInSlot(u, j)
if GetItemTypeId(ite) == GetItemTypeId(it) then
set j = 5
endif
set j = j + 1
endloop
call SetItemCharges( it, GetItemCharges(it) + GetItemCharges(ite) )
call RemoveItem( ite )
call destroy()
set i = i - 1
endif
set i = i + 1
endloop
set it = null
endmethod
private static method itemWalkAbort1 takes nothing returns nothing
local DataStruct this = table[GetHandleId(GetTriggerUnit())]
if this != 0 and( GetOrderPointX() != x or GetOrderPointY() != y ) then
call destroy()
endif
endmethod
private static method itemWalkAbort2 takes nothing returns nothing
local DataStruct this = table[GetHandleId(GetTriggerUnit())]
if not systemOrder and this != 0 then
call destroy()
endif
endmethod
private static method itemSplit takes nothing returns nothing
local item it = GetOrderTargetItem()
local integer c = GetItemCharges(it)
local integer oId = GetIssuedOrderId()
local unit u = GetOrderedUnit()
if (oId > 852001 and oId < 852008) then
if GetOrderTargetItem() == UnitItemInSlot(u, oId - 852002) then
if c > 1 then
set c = c / 2
call SetItemCharges(it, GetItemCharges(it) - c)
set systemOrder = true
set it = CreateItem(GetItemTypeId(it), GetUnitX(u), GetUnitY(u))
call UnitAddItem(u, it)
set systemOrder = false
call SetItemCharges(it, c)
endif
else
if c > 0 and GetItemTypeId(it) == GetItemTypeId(UnitItemInSlot(u, oId - 852002)) then
call SetItemCharges(it, GetItemCharges(it) + GetItemCharges(UnitItemInSlot(u, oId - 852002)))
call RemoveItem(UnitItemInSlot(u, oId - 852002))
endif
endif
endif
set it = null
set u = null
endmethod
private static method onInit takes nothing returns nothing
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, function DataStruct.itemWalkAbort1)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ISSUED_ORDER, function DataStruct.itemWalkAbort2)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, function DataStruct.itemWalk)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, function DataStruct.itemSplit)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_PICKUP_ITEM, function DataStruct.itemStack)
set table = Table.create()
set timer = CreateTimer()
endmethod
endstruct
endlibrary
//Code indented using The_Witcher's Script Language Aligner
//Download the newest version and report bugs at www.hiveworkshop.com
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope TestScope initializer Init
private function onPickup takes nothing returns nothing
local integer typ = GetItemTypeId(GetManipulatedItem())
if (typ == 'I003') then
call RemoveItem(GetManipulatedItem())
call Bag[GetTriggerUnit()].addBags(1)
elseif (typ == 'I000') then
call RemoveItem(GetManipulatedItem())
call Bag[GetTriggerUnit()].addBags(-1)
endif
endfunction
private function Init takes nothing returns nothing
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_PICKUP_ITEM, function onPickup)
call FogEnable(false)
call FogMaskEnable(false)
call Bag.create(gg_unit_Hpal_0000, 5)
call Bag.create(gg_unit_hfoo_0032, 2)
endfunction
endscope
//Code indented using The_Witcher's Script Language Aligner
//Download the newest version and report bugs at www.hiveworkshop.com
//TESH.scrollpos=0
//TESH.alwaysfold=0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ Alloc ~~ By Sevion ~~ Version 1.09 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// What is Alloc?
// - Alloc implements an intuitive allocation method for array structs
//
// =Pros=
// - Efficient.
// - Simple.
// - Less overhead than regular structs.
//
// =Cons=
// - Must use array structs (hardly a con).
// - Must manually call OnDestroy.
// - Must use Delegates for inheritance.
// - No default values for variables (use onInit instead).
// - No array members (use another Alloc struct as a linked list or type declaration).
//
// Methods:
// - struct.allocate()
// - struct.deallocate()
//
// These methods are used just as they should be used in regular structs.
//
// Modules:
// - Alloc
// Implements the most basic form of Alloc. Includes only create and destroy
// methods.
//
// Details:
// - Less overhead than regular structs
//
// - Use array structs when using Alloc. Put the implement at the top of the struct.
//
// - Alloc operates almost exactly the same as default structs in debug mode with the exception of onDestroy.
//
// How to import:
// - Create a trigger named Alloc.
// - Convert it to custom text and replace the whole trigger text with this.
//
// Thanks:
// - Nestharus for the method of allocation and suggestions on further merging.
// - Bribe for suggestions like the static if and method names.
// - PurgeandFire111 for some suggestions like the merging of Alloc and AllocX as well as OnDestroy stuff.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library Alloc
module Alloc
private static integer instanceCount = 0
private thistype recycle
static method allocate takes nothing returns thistype
local thistype this
if (thistype(0).recycle == 0) then
debug if (instanceCount == 8190) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to allocate too many instances!")
debug return 0
debug endif
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = thistype(0).recycle
set thistype(0).recycle = thistype(0).recycle.recycle
endif
debug set this.recycle = -1
return this
endmethod
method deallocate takes nothing returns nothing
debug if (this.recycle != -1) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to deallocate an invalid instance at [" + I2S(this) + "]!")
debug return
debug endif
set this.recycle = thistype(0).recycle
set thistype(0).recycle = this
endmethod
endmodule
endlibrary
//TESH.scrollpos=51
//TESH.alwaysfold=0
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.0.1
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
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")
//! 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 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)
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb)
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, 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
/**************************************************************
*
* RegisterPlayerUnitEvent
* v5.1.0.1
* By Magtheridon96
*
* I would like to give a special thanks to Bribe, azlier
* and BBQ for improving this library. For modularity, it only
* supports player unit events.
*
* Functions passed to RegisterPlayerUnitEvent must either
* return a boolean (false) or nothing. (Which is a Pro)
*
* Warning:
* --------
*
* - Don't use TriggerSleepAction inside registered code.
* - Don't destroy a trigger unless you really know what you're doing.
*
* API:
* ----
*
* - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
* - Registers code that will execute when an event fires.
* - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
* - Registers code that will execute when an event fires for a certain player.
* - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
* - Returns the trigger corresponding to ALL functions of a playerunitevent.
*
**************************************************************/
library RegisterPlayerUnitEvent // Special Thanks to Bribe and azlier
globals
private trigger array t
endglobals
function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
local integer i = GetHandleId(p)
local integer k = 15
if t[i] == null then
set t[i] = CreateTrigger()
loop
call TriggerRegisterPlayerUnitEvent(t[i], Player(k), p, null)
exitwhen k == 0
set k = k - 1
endloop
endif
call TriggerAddCondition(t[i], Filter(c))
endfunction
function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
if t[i] == null then
set t[i] = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(t[i], pl, p, null)
endif
call TriggerAddCondition(t[i], Filter(c))
endfunction
function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
return t[GetHandleId(p)]
endfunction
endlibrary