Name | Type | is_array | initial_value |
//TESH.scrollpos=0
//TESH.alwaysfold=0
library TimeLapse /*
Time Lapse v1.20
by Flux
Warps backward to whatever position caster was in 4/5/6 seconds earlier--regaining the HP
and mana from that time.
*/ requires /*
(nothing)
*/ optional Table/*
If not found, the spell will create a hashtable. Hashtables are limited to 255 per map.
*/ optional RegisterPlayerUnitEvent /*
If not found, an extra trigger is created.
*/ optional SpellEffectEvent /*
If not found, an extra trigger is created and GetSpellAbilityId() check will occur.
*/ optional TimerUtils /*
If found, will recycle timers used preventing continuous creation & destruction of timers.
NOTES:
- You cannot kill yourself from using Time Lapse.
- Cannot go back in times that the spell is not learned yet.
CREDITS:
Bribe - Table, SpellEffectEvent
Magtheridon96 - RegisterPlayerUnitEvent
Vexorian - TimerUtils
Kino - TimeLapse Effect (edited)
*/
globals
//Rawcode of the Spell
private constant integer SPELL_ID = 'ATiL'
//Maximum Timing possible at any level
//Determines up to how long in the past data is saved before it is discarded.
private constant real MAX_TIMING = 6.0
//Periodic timeout for storing data
//Lesser value = Greater accuracy at the cost of performance
private constant real TIMEOUT = 0.1
//Visual Effects upon casting the spell
private constant string SFX_TARGET = "Models\\Effects\\TimeLapse.mdx"
private constant string SFX_SOURCE = "Models\\Effects\\TimeLapse.mdx"
private constant real SFX_DURATION = 1.0
//Is Time Lapse going to be a Hero Ability in your map?
private constant boolean HERO_ABILITY = true
//Is Time Lapse going to be a Unit Ability in your map? (Both can be true)
private constant boolean UNIT_ABILITY = true
//Refresh Rate for Units with abilities
//Only essential if UNIT_ABILILTY = true
private constant real UNIT_REFRESH = 60.0
endglobals
//How much time caster goes back in time.
private function Timing takes integer level returns real
return 3.0 + 1.0*level
endfunction
//Units that will have its data stored periodically
static if UNIT_ABILITY then
private function UnitFilter takes unit u returns boolean
return GetUnitAbilityLevel(u, SPELL_ID) > 0
endfunction
endif
private struct Sfx
private effect source
private effect target
private method destroy takes nothing returns nothing
call DestroyEffect(this.source)
call DestroyEffect(this.target)
set this.source = null
set this.target = null
call this.deallocate()
endmethod
private static method expires takes nothing returns nothing
local timer t = GetExpiredTimer()
static if LIBRARY_TimerUtils then
call thistype(GetTimerData(t)).destroy()
call ReleaseTimer(t)
else
local integer id = GetHandleId(t)
static if LIBRARY_Table then
call thistype(TimeLapse.tb[id]).destroy()
call TimeLapse.tb.remove(id)
else
call thistype(LoadInteger(TimeLapse.hash, id, 0)).destroy()
call RemoveSavedInteger(TimeLapse.hash, id, 0)
endif
call DestroyTimer(t)
endif
set t = null
endmethod
static method create takes real x1, real y1, real x2, real y2 returns thistype
local thistype this = thistype.allocate()
static if LIBRARY_TimerUtils then
call TimerStart(NewTimerEx(this), SFX_DURATION, false, function thistype.expires)
else
local timer t = CreateTimer()
static if LIBRARY_Table then
set TimeLapse.tb[GetHandleId(t)] = this
else
call SaveInteger(TimeLapse.hash, GetHandleId(t), 0, this)
endif
call TimerStart(t, SFX_DURATION, false, function thistype.expires)
set t = null
endif
set this.source = AddSpecialEffect(SFX_SOURCE, x1, y1)
set this.target = AddSpecialEffect(SFX_TARGET, x2, y2)
return this
endmethod
endstruct
struct TimeLapse
private unit u
private integer oldest
static if LIBRARY_Table then
private Table hp
private Table mana
private Table xPos
private Table yPos
readonly static Table tb
else
readonly static hashtable hash = InitHashtable()
endif
private thistype next
private thistype prev
private static integer ctr
private static timer t
private static constant integer INDEX_REMOVE_OFFSET = R2I(MAX_TIMING/TIMEOUT)
method destroy takes nothing returns nothing
static if LIBRARY_Table then
call thistype.tb.remove(GetHandleId(this.u))
call this.hp.destroy()
call this.mana.destroy()
call this.xPos.destroy()
call this.yPos.destroy()
else
call RemoveSavedInteger(thistype.hash, GetHandleId(this.u), 0)
call FlushChildHashtable(thistype.hash, this)
call FlushChildHashtable(thistype.hash, this + JASS_MAX_ARRAY_SIZE)
call FlushChildHashtable(thistype.hash, this + 2*JASS_MAX_ARRAY_SIZE)
call FlushChildHashtable(thistype.hash, this + 3*JASS_MAX_ARRAY_SIZE)
endif
set this.prev.next = this.next
set this.next.prev = this.prev
if thistype(0).next == 0 then
call PauseTimer(thistype.t)
endif
set this.u = null
call this.deallocate()
endmethod
private static method onPeriod takes nothing returns nothing
local thistype this = thistype(0).next
local integer oldIndex = thistype.ctr - thistype.INDEX_REMOVE_OFFSET
loop
exitwhen this == 0
static if LIBRARY_Table then
set this.hp.real[thistype.ctr] = GetWidgetLife(this.u)
set this.mana.real[thistype.ctr] = GetUnitState(this.u, UNIT_STATE_MANA)
set this.xPos.real[thistype.ctr] = GetUnitX(this.u)
set this.yPos.real[thistype.ctr] = GetUnitY(this.u)
if this.hp.real.has(oldIndex) then
call this.hp.real.remove(oldIndex)
call this.mana.real.remove(oldIndex)
call this.xPos.real.remove(oldIndex)
call this.yPos.real.remove(oldIndex)
set this.oldest = oldIndex + 1
endif
else
call SaveReal(thistype.hash, this, thistype.ctr, GetWidgetLife(this.u))
call SaveReal(thistype.hash, this + JASS_MAX_ARRAY_SIZE, thistype.ctr, GetUnitState(this.u, UNIT_STATE_MANA))
call SaveReal(thistype.hash, this + 2*JASS_MAX_ARRAY_SIZE, thistype.ctr, GetUnitX(this.u))
call SaveReal(thistype.hash, this + 3*JASS_MAX_ARRAY_SIZE, thistype.ctr, GetUnitY(this.u))
if HaveSavedReal(thistype.hash, this, oldIndex) then
call RemoveSavedReal(thistype.hash, this, oldIndex)
call RemoveSavedReal(thistype.hash, this + JASS_MAX_ARRAY_SIZE, oldIndex)
call RemoveSavedReal(thistype.hash, this + 2*JASS_MAX_ARRAY_SIZE, oldIndex)
call RemoveSavedReal(thistype.hash, this + 3*JASS_MAX_ARRAY_SIZE, oldIndex)
endif
endif
set this = this.next
endloop
set thistype.ctr = thistype.ctr + 1
endmethod
static method create takes unit u returns thistype
local thistype this
local integer id = GetHandleId(u)
static if LIBRARY_Table then
if thistype.tb.has(id) then
set this = thistype.tb[id]
else
set this = thistype.allocate()
set this.oldest = thistype.ctr
set this.u = u
set this.hp = Table.create()
set this.mana = Table.create()
set this.xPos = Table.create()
set this.yPos = Table.create()
set this.next = thistype(0)
set this.prev = thistype(0).prev
set this.next.prev = this
set this.prev.next = this
if this.prev == 0 then
call TimerStart(thistype.t, TIMEOUT, true, function thistype.onPeriod)
endif
set thistype.tb[id] = this
endif
else
if HaveSavedInteger(thistype.hash, id, 0) then
set this = LoadInteger(thistype.hash, id, 0)
else
set this = thistype.allocate()
set this.oldest = thistype.ctr
set this.u = u
set this.next = thistype(0)
set this.prev = thistype(0).prev
set this.next.prev = this
set this.prev.next = this
if this.prev == 0 then
call TimerStart(thistype.t, TIMEOUT, true, function thistype.onPeriod)
endif
call SaveInteger(thistype.hash, id, 0, this)
endif
endif
return this
endmethod
method doEffect takes nothing returns nothing
local integer index = thistype.ctr - R2I(Timing(GetUnitAbilityLevel(this.u, SPELL_ID))/TIMEOUT)
local real hp
local real mana
local real x
local real y
if index > this.oldest then
static if LIBRARY_Table then
set hp = this.hp.real[index]
set mana = this.mana.real[index]
set x = this.xPos.real[index]
set y = this.yPos.real[index]
else
set hp = LoadReal(thistype.hash, this, index)
set mana = LoadReal(thistype.hash, this + JASS_MAX_ARRAY_SIZE, index)
set x = LoadReal(thistype.hash, this + 2*JASS_MAX_ARRAY_SIZE, index)
set y = LoadReal(thistype.hash, this + 3*JASS_MAX_ARRAY_SIZE, index)
endif
else
static if LIBRARY_Table then
set hp = this.hp.real[this.oldest]
set mana = this.mana.real[this.oldest]
set x = this.xPos.real[this.oldest]
set y = this.yPos.real[this.oldest]
else
set hp = LoadReal(thistype.hash, this, this.oldest)
set mana = LoadReal(thistype.hash, this + JASS_MAX_ARRAY_SIZE, this.oldest)
set x = LoadReal(thistype.hash, this + 2*JASS_MAX_ARRAY_SIZE, this.oldest)
set y = LoadReal(thistype.hash, this + 3*JASS_MAX_ARRAY_SIZE, this.oldest)
endif
endif
if hp < 0.405 then
set hp = 1.0
endif
call Sfx.create(GetUnitX(this.u), GetUnitY(this.u), x, y)
call SetWidgetLife(this.u, hp)
call SetUnitState(this.u, UNIT_STATE_MANA, mana)
call SetUnitX(this.u, x)
call SetUnitY(this.u, y)
endmethod
private static method delay takes nothing returns nothing
local timer t = GetExpiredTimer()
static if LIBRARY_TimerUtils then
call thistype(GetTimerData(t)).doEffect()
call ReleaseTimer(t)
else
local integer id = GetHandleId(t)
static if LIBRARY_Table then
call thistype(thistype.tb[id]).doEffect()
call thistype.tb.remove(id)
else
call thistype(LoadInteger(thistype.hash, id, 0)).doEffect()
call RemoveSavedInteger(thistype.hash, id, 0)
endif
call DestroyTimer(t)
endif
set t = null
endmethod
private static method onCast takes nothing returns boolean
static if LIBRARY_Table then
local thistype this = thistype.tb[GetHandleId(GetTriggerUnit())]
else
local thistype this = LoadInteger(thistype.hash, GetHandleId(GetTriggerUnit()), 0)
endif
static if LIBRARY_TimerUtils then
call TimerStart(NewTimerEx(this), 0.0, false, function thistype.delay)
else
local timer t = CreateTimer()
static if LIBRARY_Table then
set thistype.tb[GetHandleId(t)] = this
else
call SaveInteger(thistype.hash, GetHandleId(t), 0, this)
endif
call TimerStart(t, 0.0, false, function thistype.delay)
set t = null
endif
return false
endmethod
static if UNIT_ABILITY then
private thistype unext
private thistype uprev
private static timer unitTimer = CreateTimer()
private static method unitRefresh takes nothing returns nothing
local thistype this = thistype(0).unext
loop
exitwhen this == 0
if GetUnitTypeId(this.u) == 0 then
set this.uprev.unext = this.unext
set this.unext.uprev = this.uprev
if thistype(0).unext == 0 then
call PauseTimer(thistype.unitTimer)
endif
call this.destroy()
endif
set this = this.unext
endloop
endmethod
private method insert takes nothing returns nothing
set this.unext = thistype(0)
set this.uprev = thistype(0).uprev
set this.unext.uprev = this
set this.uprev.unext = this
if this.uprev == 0 then
call TimerStart(thistype.unitTimer, UNIT_REFRESH, true, function thistype.unitRefresh)
endif
endmethod
private static method unitEnters takes nothing returns boolean
local unit u = GetTriggerUnit()
if UnitFilter(u) then
call thistype.create(u).insert()
endif
set u = null
return false
endmethod
private static method preplaced takes nothing returns nothing
local group g = CreateGroup()
local unit u
call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
loop
set u = FirstOfGroup(g)
exitwhen u == null
call GroupRemoveUnit(g, u)
if UnitFilter(u) then
call thistype.create(u).insert()
endif
endloop
call DestroyGroup(g)
call DestroyTimer(GetExpiredTimer())
set g = null
endmethod
endif
static if HERO_ABILITY then
private static method learn takes nothing returns boolean
if GetLearnedSkill() == SPELL_ID then
call thistype.create(GetTriggerUnit())
endif
return false
endmethod
endif
static if not LIBRARY_SpellEffectEvent then
private static method cond takes nothing returns boolean
return GetSpellAbilityId() == SPELL_ID and thistype.onCast()
endmethod
endif
private static method onInit takes nothing returns nothing
static if LIBRARY_RegisterPlayerUnitEvent then
static if UNIT_ABILITY then
local trigger enter = CreateTrigger()
local region rectRegion = CreateRegion()
call RegionAddRect(rectRegion, bj_mapInitialPlayableArea)
call TriggerRegisterEnterRegion(enter, rectRegion, null)
call TriggerAddCondition(enter, Condition(function thistype.unitEnters))
call TimerStart(CreateTimer(), 0.0, false, function thistype.preplaced)
endif
static if HERO_ABILITY then
call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL, function thistype.learn)
endif
static if LIBRARY_SpellEffectEvent then
call RegisterSpellEffectEvent(SPELL_ID, function thistype.onCast)
else
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT, function thistype.cond)
endif
else
local integer i = 0
local trigger t1 = CreateTrigger()
static if HERO_ABILITY then
local trigger t2 = CreateTrigger()
endif
static if UNIT_ABILITY then
local trigger enter = CreateTrigger()
local region rectRegion = CreateRegion()
call RegionAddRect(rectRegion, bj_mapInitialPlayableArea)
call TriggerRegisterEnterRegion(enter, rectRegion, null)
call TriggerAddCondition(enter, Condition(function thistype.unitEnters))
call TimerStart(CreateTimer(), 0.0, false, function thistype.preplaced)
endif
loop
exitwhen i == bj_MAX_PLAYER_SLOTS
call TriggerRegisterPlayerUnitEvent(t1, Player(i), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
static if HERO_ABILITY then
call TriggerRegisterPlayerUnitEvent(t2, Player(i), EVENT_PLAYER_HERO_SKILL, null)
endif
set i = i + 1
endloop
call TriggerAddCondition(t1, Condition(function thistype.cond))
static if HERO_ABILITY then
call TriggerAddCondition(t2, Condition(function thistype.learn))
endif
endif
static if LIBRARY_Table then
set thistype.tb = Table.create()
endif
set thistype.t = CreateTimer()
set thistype.ctr = 0
endmethod
endstruct
endlibrary
//TESH.scrollpos=23
//TESH.alwaysfold=0
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 4.1.0.1.
One map, one hashtable. Welcome to NewTable 4.1.0.1
This newest iteration of Table introduces the new HashTable struct.
You can now instantiate HashTables which enables the use of large
parent and large child keys, just like a standard hashtable. Previously,
the user would have to instantiate a Table to do this on their own which -
while doable - is something the user should not have to do if I can add it
to this resource myself (especially if they are inexperienced).
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
//NEW: Added in Table 4.0. A fairly simple struct but allows you to do more
//than that which was previously possible.
struct HashTable extends array
//Enables myHash[parentKey][childKey] syntax.
//Basically, it creates a Table in the place of the parent key if
//it didn't already get created earlier.
method operator [] takes integer index returns Table
local Table t = Table(this)[index]
if t == 0 then
set t = Table.create()
set Table(this)[index] = t //whoops! Forgot that line. I'm out of practice!
endif
return t
endmethod
//You need to call this on each parent key that you used if you
//intend to destroy the HashTable or simply no longer need that key.
method remove takes integer index returns nothing
local Table t = Table(this)[index]
if t != 0 then
call t.destroy()
call Table(this).remove(index)
endif
endmethod
//Added in version 4.1
method has takes integer index returns boolean
return Table(this).has(index)
endmethod
//HashTables are just fancy Table indices.
method destroy takes nothing returns nothing
call Table(this).destroy()
endmethod
//Like I said above...
static method create takes nothing returns thistype
return Table.create()
endmethod
endstruct
endlibrary
//TESH.scrollpos=32
//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
//TESH.scrollpos=0
//TESH.alwaysfold=0
//============================================================================
// SpellEffectEvent
// - Version 1.1.0.0
//
// API
// ---
// RegisterSpellEffectEvent(integer abil, code onCast)
//
//
// Requires
// --------
// RegisterPlayerUnitEvent: hiveworkshop.com/forums/showthread.php?t=203338
//
// Optional
// --------
// Table: hiveworkshop.com/forums/showthread.php?t=188084
//
/*
============= Why this is important? =================
1. Does not generate 16 events per spell.
2. This uses one trigger evaluation instead of one for each
individual spell (some maps have quite a few). This helps keep
framerate high and fluid when a spell is cast.
*/
library SpellEffectEvent requires RegisterPlayerUnitEvent, optional Table
//============================================================================
private module M
static if LIBRARY_Table then
static Table tb
else
static hashtable ht = InitHashtable()
endif
static method onCast takes nothing returns nothing
static if LIBRARY_Table then
call TriggerEvaluate(.tb.trigger[GetSpellAbilityId()])
else
call TriggerEvaluate(LoadTriggerHandle(.ht, 0, GetSpellAbilityId()))
endif
endmethod
private static method onInit takes nothing returns nothing
static if LIBRARY_Table then
set .tb = Table.create()
endif
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT, function thistype.onCast)
endmethod
endmodule
//============================================================================
private struct S extends array
implement M
endstruct
//============================================================================
function RegisterSpellEffectEvent takes integer abil, code onCast returns nothing
static if LIBRARY_Table then
if not S.tb.handle.has(abil) then
set S.tb.trigger[abil] = CreateTrigger()
endif
call TriggerAddCondition(S.tb.trigger[abil], Filter(onCast))
else
if not HaveSavedHandle(S.ht, 0, abil) then
call SaveTriggerHandle(S.ht, 0, abil, CreateTrigger())
endif
call TriggerAddCondition(LoadTriggerHandle(S.ht, 0, abil), Filter(onCast)
endif
endfunction
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library TimerUtils initializer init
//*********************************************************************
//* TimerUtils (red+blue+orange flavors for 1.24b+) 2.0
//* ----------
//*
//* To implement it , create a custom text trigger called TimerUtils
//* and paste the contents of this script there.
//*
//* To copy from a map to another, copy the trigger holding this
//* library to your map.
//*
//* (requires vJass) More scripts: htt://www.wc3c.net
//*
//* For your timer needs:
//* * Attaching
//* * Recycling (with double-free protection)
//*
//* set t=NewTimer() : Get a timer (alternative to CreateTimer)
//* set t=NewTimerEx(x) : Get a timer (alternative to CreateTimer), call
//* Initialize timer data as x, instead of 0.
//*
//* ReleaseTimer(t) : Relese a timer (alt to DestroyTimer)
//* SetTimerData(t,2) : Attach value 2 to timer
//* GetTimerData(t) : Get the timer's value.
//* You can assume a timer's value is 0
//* after NewTimer.
//*
//* Multi-flavor:
//* Set USE_HASH_TABLE to true if you don't want to complicate your life.
//*
//* If you like speed and giberish try learning about the other flavors.
//*
//********************************************************************
//================================================================
globals
//How to tweak timer utils:
// USE_HASH_TABLE = true (new blue)
// * SAFEST
// * SLOWEST (though hash tables are kind of fast)
//
// USE_HASH_TABLE = false, USE_FLEXIBLE_OFFSET = true (orange)
// * kinda safe (except there is a limit in the number of timers)
// * ALMOST FAST
//
// USE_HASH_TABLE = false, USE_FLEXIBLE_OFFSET = false (red)
// * THE FASTEST (though is only faster than the previous method
// after using the optimizer on the map)
// * THE LEAST SAFE ( you may have to tweak OFSSET manually for it to
// work)
//
private constant boolean USE_HASH_TABLE = true
private constant boolean USE_FLEXIBLE_OFFSET = false
private constant integer OFFSET = 0x100000
private integer VOFFSET = OFFSET
//Timers to preload at map init:
private constant integer QUANTITY = 256
//Changing this to something big will allow you to keep recycling
// timers even when there are already AN INCREDIBLE AMOUNT of timers in
// the stack. But it will make things far slower so that's probably a bad idea...
private constant integer ARRAY_SIZE = 8190
endglobals
//==================================================================================================
globals
private integer array data[ARRAY_SIZE]
private hashtable ht
endglobals
//It is dependent on jasshelper's recent inlining optimization in order to perform correctly.
function SetTimerData takes timer t, integer value returns nothing
static if(USE_HASH_TABLE) then
// new blue
call SaveInteger(ht,0,GetHandleId(t), value)
elseif (USE_FLEXIBLE_OFFSET) then
// orange
static if (DEBUG_MODE) then
if(GetHandleId(t)-VOFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
set data[GetHandleId(t)-VOFFSET]=value
else
// new red
static if (DEBUG_MODE) then
if(GetHandleId(t)-OFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
set data[GetHandleId(t)-OFFSET]=value
endif
endfunction
function GetTimerData takes timer t returns integer
static if(USE_HASH_TABLE) then
// new blue
return LoadInteger(ht,0,GetHandleId(t) )
elseif (USE_FLEXIBLE_OFFSET) then
// orange
static if (DEBUG_MODE) then
if(GetHandleId(t)-VOFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
return data[GetHandleId(t)-VOFFSET]
else
// new red
static if (DEBUG_MODE) then
if(GetHandleId(t)-OFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
return data[GetHandleId(t)-OFFSET]
endif
endfunction
//==========================================================================================
globals
private timer array tT[ARRAY_SIZE]
private integer tN = 0
private constant integer HELD=0x28829022
//use a totally random number here, the more improbable someone uses it, the better.
private boolean didinit = false
endglobals
private keyword init
//==========================================================================================
// I needed to decide between duplicating code ignoring the "Once and only once" rule
// and using the ugly textmacros. I guess textmacros won.
//
//! textmacro TIMERUTIS_PRIVATE_NewTimerCommon takes VALUE
// On second thought, no.
//! endtextmacro
function NewTimerEx takes integer value returns timer
if (tN==0) then
if (not didinit) then
//This extra if shouldn't represent a major performance drawback
//because QUANTITY rule is not supposed to be broken every day.
call init.evaluate()
set tN = tN - 1
else
//If this happens then the QUANTITY rule has already been broken, try to fix the
// issue, else fail.
debug call BJDebugMsg("NewTimer: Warning, Exceeding TimerUtils_QUANTITY, make sure all timers are getting recycled correctly")
set tT[0]=CreateTimer()
static if( not USE_HASH_TABLE) then
debug call BJDebugMsg("In case of errors, please increase it accordingly, or set TimerUtils_USE_HASH_TABLE to true")
static if( USE_FLEXIBLE_OFFSET) then
if (GetHandleId(tT[0])-VOFFSET<0) or (GetHandleId(tT[0])-VOFFSET>=ARRAY_SIZE) then
//all right, couldn't fix it
call BJDebugMsg("NewTimer: Unable to allocate a timer, you should probably set TimerUtils_USE_HASH_TABLE to true or fix timer leaks.")
return null
endif
else
if (GetHandleId(tT[0])-OFFSET<0) or (GetHandleId(tT[0])-OFFSET>=ARRAY_SIZE) then
//all right, couldn't fix it
call BJDebugMsg("NewTimer: Unable to allocate a timer, you should probably set TimerUtils_USE_HASH_TABLE to true or fix timer leaks.")
return null
endif
endif
endif
endif
else
set tN=tN-1
endif
call SetTimerData(tT[tN],value)
return tT[tN]
endfunction
function NewTimer takes nothing returns timer
return NewTimerEx(0)
endfunction
//==========================================================================================
function ReleaseTimer takes timer t returns nothing
if(t==null) then
debug call BJDebugMsg("Warning: attempt to release a null timer")
return
endif
if (tN==ARRAY_SIZE) then
debug call BJDebugMsg("Warning: Timer stack is full, destroying timer!!")
//stack is full, the map already has much more troubles than the chance of bug
call DestroyTimer(t)
else
call PauseTimer(t)
if(GetTimerData(t)==HELD) then
debug call BJDebugMsg("Warning: ReleaseTimer: Double free!")
return
endif
call SetTimerData(t,HELD)
set tT[tN]=t
set tN=tN+1
endif
endfunction
private function init takes nothing returns nothing
local integer i=0
local integer o=-1
local boolean oops = false
if ( didinit ) then
return
else
set didinit = true
endif
static if( USE_HASH_TABLE ) then
set ht = InitHashtable()
loop
exitwhen(i==QUANTITY)
set tT[i]=CreateTimer()
call SetTimerData(tT[i], HELD)
set i=i+1
endloop
set tN = QUANTITY
else
loop
set i=0
loop
exitwhen (i==QUANTITY)
set tT[i] = CreateTimer()
if(i==0) then
set VOFFSET = GetHandleId(tT[i])
static if(USE_FLEXIBLE_OFFSET) then
set o=VOFFSET
else
set o=OFFSET
endif
endif
if (GetHandleId(tT[i])-o>=ARRAY_SIZE) then
exitwhen true
endif
if (GetHandleId(tT[i])-o>=0) then
set i=i+1
endif
endloop
set tN = i
exitwhen(tN == QUANTITY)
set oops = true
exitwhen not USE_FLEXIBLE_OFFSET
debug call BJDebugMsg("TimerUtils_init: Failed a initialization attempt, will try again")
endloop
if(oops) then
static if ( USE_FLEXIBLE_OFFSET) then
debug call BJDebugMsg("The problem has been fixed.")
//If this message doesn't appear then there is so much
//handle id fragmentation that it was impossible to preload
//so many timers and the thread crashed! Therefore this
//debug message is useful.
elseif(DEBUG_MODE) then
call BJDebugMsg("There were problems and the new timer limit is "+I2S(i))
call BJDebugMsg("This is a rare ocurrence, if the timer limit is too low:")
call BJDebugMsg("a) Change USE_FLEXIBLE_OFFSET to true (reduces performance a little)")
call BJDebugMsg("b) or try changing OFFSET to "+I2S(VOFFSET) )
endif
endif
endif
endfunction
endlibrary