Name | Type | is_array | initial_value |
//TESH.scrollpos=0
//TESH.alwaysfold=0
library MeatMash requires SpellEffectEvent, Tt, Table, RegisterPlayerUnitEvent, Dummy
/*****************************************************
*
* MeatMash
* v1.1.0.1
* By Magtheridon96
*
* - This spell creates a Barbeque ward at the target
* point that calls down chunks of meat from the sky
* to damage nearby enemy units.
*
* Requires:
* ---------
*
* TimerTools by Nestharus
* - hiveworkshop.com/forums/jass-resources-412/system-timer-tools-201165/
* SpellEffectEvent by Bribe
* - hiveworkshop.com/forums/jass-resources-412/snippet-spelleffectevent-187193/
* RegisterPlayerUnitEvent by Magtheridon96
* - hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/
* Table by Bribe
* - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
* Dummy by Nestharus
* - hiveworkshop.com/forums/jass-resources-412/system-dummy-213908/
*
* Implementation Instructions:
* ----------------------------
*
* 1. Import all the required systems to your map.
* 2. Import this trigger into your map.
* 3. Import the object editor data to your map.
* (1 Ability, 2 Units)
* 4. Import dummy.mdx into your map.
* 5. Configure the spell any way you like.
* 6. Enjoy!
*
* Configuration:
* --------------
*
*****************************************************/
globals
/*****************************************************
*
* Object Raw Code Configuration
* -----------------------------
*
* This is where you should input all the
* raw codes of the objects of this spell.
*
* ABIL_CODE
* o The raw code of the ability.
*
* WARD_CODE
* o The raw code of the barbeque ward.
*
*****************************************************/
private constant integer ABIL_CODE = 'A000'
private constant integer WARD_CODE = 'o000'
/*****************************************************
*
* Projectile Speed Configuration
* ------------------------------
*
* This is where you could change how fast
* the meat chunks are falling from the sky.
*
* CHUNK_INITIAL_SPEED
* o The initial speed of the meat chunk.
*
* CHUNK_HEIGHT
* o The height at which the meat chunk is created.
*
* ACCELERATION
* o The acceleration of the falling speed of
* the meat chunk.
*
*****************************************************/
private constant real CHUNK_INITIAL_SPEED = 1
private constant real CHUNK_HEIGHT = 1300
private constant real ACCELERATION = 0.7
/*****************************************************
*
* Damage and Attack-type Configuration
* ------------------------------------
*
* This is where you would configure the attack
* type and the damage type of the damage that is
* dealt to units.
*
* ATTACK_TYPE
* o The attack type.
*
* DAMAGE_TYPE
* o The damage type.
*
*****************************************************/
private constant attacktype ATTACK_TYPE = ATTACK_TYPE_NORMAL
private constant damagetype DAMAGE_TYPE = DAMAGE_TYPE_NORMAL
/*****************************************************
*
* Damage Radius Configuration
* ---------------------------
*
* This is where you would configure
* the damage radius of one meat chunk.
*
* MEAT_DAMAGE_RADIUS
* o Damage radius of one meat chunk.
*
*****************************************************/
private constant real MEAT_DAMAGE_RADIUS = 120
/*****************************************************
*
* Special Effect Configuration
* ----------------------------
*
* This is where you would configure
* the special effects created by the spell.
*
* MEAT_EFFECT_PATH
* o This is the actual meat chunk.
*
* WARD_EFFECT_PATH
* o The effect created when a ward dies.
*
*****************************************************/
private constant string MEAT_EFFECT_PATH = "Abilities\\Weapons\\MeatwagonMissile\\MeatwagonMissile.mdl"
private constant string WARD_EFFECT_PATH = "Abilities\\Weapons\\SerpentWardMissile\\SerpentWardMissile.mdl"
endglobals
/*****************************************************
*
* Damage Configuration
* --------------------
*
* This is where you could configure
* the amount of damage that is dealt
* per chunk of meat.
*
* integer level
* o Level of the ability.
*
*****************************************************/
private function GetDamagePerChunk takes integer level returns real
return 30. + level * 15
endfunction
/*****************************************************
*
* Meat Chunk Amounts Configuration
* --------------------------------
*
* This is where you would configure
* the number of meat chunks spawned
* per second.
*
* integer level
* o Level of the ability.
*
*****************************************************/
private function GetChunksPerSecond takes integer level returns real
return 6. + level * 2
endfunction
/*****************************************************
*
* Spell Radius Configuration
* --------------------------
*
* This is where you would configure
* the radius of the chunk spawning area.
*
* integer level
* o Level of the ability.
*
*****************************************************/
private function GetRadius takes integer level returns real
return 400 + level * 50.
endfunction
/*****************************************************
*
* Spell Duration Configuration
* ----------------------------
*
* This is where you would configure
* the duration of the spell effect.
*
* integer level
* o Level of the ability.
*
*****************************************************/
private function GetDuration takes integer level returns real
return 6. + level * 2
endfunction
/*****************************************************
*
* Target Filter Configuration
* ---------------------------
*
* This is where you would configure
* the filter that decides whether a
* unit should take damage or not.
*
* player source
* o The owner of the caster.
*
* unit target
* o The unit we are filtering.
*
*****************************************************/
private function TargetFilter takes player source, unit target returns boolean
return not IsUnitType(target, UNIT_TYPE_MAGIC_IMMUNE) and not IsUnitType(target, UNIT_TYPE_STRUCTURE) and not IsUnitType(target, UNIT_TYPE_DEAD) and IsUnitEnemy(target, source)
endfunction
/*****************************************************
*
* Notes:
* ------
*
* - If you are facing performance problems, go
* to the top of the MeatMash struct and read
* the given text on top of the TIMEOUT constant.
* You may need to modify the TIMEOUT.
*
* Credits:
* --------
*
* - Skycraft (Inspiration and idea)
* - Nestharus (TimerTools and Dummy)
* - Bribe (SpellEffectEvent and Table)
*
*****************************************************/
/*
* This struct represents one chunk of meat.
* This struct can be avoided completely, but
* it would require me to make multiple tables
* of data inside the MeatMash struct which could
* get ugly. This is the more efficient method
* because I would only rely on one hashtable read
* to get the MeatChunk struct instance and the rest
* of the reads would be array reads.
*/
private struct MeatChunk extends array
/*
* These will be used for the
* dynamic indexing.
*/
private static integer array rn
private static integer ic = 0
/*
* The dummy unit and his
* current falling speed.
*/
Dummy dummy
real speed
/*
* The model of the dummy.
*/
effect model
/*
* The coordinates of the
* dummy unit.
*/
real x
real y
real z
/*
* This function creates an instance
* of the MeatChunk struct.
*/
static method create takes real centerX, real centerY, real distance, real angle returns thistype
/*
* First, we use dynamic indexing
* to allocate an instance.
*/
local thistype this = rn[0]
if this == 0 then
set ic = ic + 1
set this = ic
else
set rn[0] = rn[this]
endif
/*
* Next, we create the dummy unit
* and configure his coordinates and
* speed.
*/
set this.x = centerX + distance * Cos(angle)
set this.y = centerY + distance * Sin(angle)
set this.dummy = Dummy.create(this.x, this.y, 0)
set this.speed = CHUNK_INITIAL_SPEED
set this.z = CHUNK_HEIGHT
/*
* Creating the meat chunk effect on the dummy unit.
*/
set this.model = AddSpecialEffectTarget(MEAT_EFFECT_PATH, this.dummy.unit, "origin")
/*
* Finally, we put our meat in the sky.
*/
call SetUnitFlyHeight(this.dummy.unit, CHUNK_HEIGHT, 0)
return this
endmethod
method destroy takes nothing returns nothing
/*
* We deallocate the instance.
*/
set rn[this] = rn[0]
set rn[0] = this
/*
* We destroy the model.
*/
call DestroyEffect(this.model)
/*
* Then we destroy the dummy.
*/
call this.dummy.destroy()
endmethod
endstruct
/*
* This is the main spell struct
* in which I keep all my data.
*/
private struct MeatMash extends array
/*
* The timeout can be changed. The only
* timeouts I would recommend are as
* follows:
*
* 0.02 (50 iterations per second)
* 0.03125 (32 iterations per second)
* 0.04 (25 iterations per second)
* 0.05 (20 iterations per second)
* 0.0625 (16 iterations per second)
*
* 0.0625 is only for cases in which you are
* facing performance problems. 0.03125 is the
* most optimal because it allows you to maintain
* realism and is not too fast.
*/
private static constant real TIMEOUT = 0.03125
/*
* This is the global group that we will
* use to enumerate over units in range.
*/
private static group enumGroup = CreateGroup()
/*
* This is the table that will link
* wards to their instances.
*/
private static Table cache
/*
* These are all the spell
* data arrays.
*/
private static unit array caster
private static player array owner
private static unit array ward
private static real array wardX
private static real array wardY
private static real array damage
private static real array range
private static real array time
private static real array interval
private static integer array ticks
/*
* Each struct instance has one
* Table and an integer to act
* as a MEAT STACK.
*/
private static Table array stack
private static integer array count
/*
* This is where all the
* timer code begins.
*/
implement CTTC
/*
* Local declarations.
*/
local MeatChunk current
local integer index
local unit u
implement CTTCExpire
/*
* Checking if there is at least
* one tick left.
*/
if ticks[this] >= 1 then
/*
* Decrease the number of remaining
* ticks by 1.
*/
set ticks[this] = ticks[this] - 1
/*
* We increase the time member by the
* TIMEOUT. This time member is important
* because it allows us to have this spell
* function for any number of chunks per
* second. Without it, this spell would
* only function for up to (1/TIMEOUT)
* chunks per second and only certain
* numbers within the range
* [0 ... (1/TIMEOUT)]
*/
set time[this] = time[this] + TIMEOUT
/*
* Now we loop to see how many chunks
* of meat we need to create this time.
*/
loop
exitwhen time[this] < interval[this]
/*
* We create the meat chunk at a random
* point in range of the ward and push it
* to the MEAT STACK.
*/
set stack[this][count[this]] = MeatChunk.create(wardX[this], wardY[this], GetRandomReal(0, range[this]), GetRandomReal(0, 6.28))
set count[this] = count[this] + 1
/*
* We decrease the time passed by the
* interval at which we need to create
* a meat chunk.
*/
set time[this] = time[this] - interval[this]
endloop
else
/*
* If there are no more chunks of meat left,
* then we can destroy the current instance
* of the spell.
*/
if count[this] == 0 then
/*
* Removing the current instance
* from the timer list.
*/
call this.destroy()
/*
* Destroying the meat chunk stack.
*/
call stack[this].flush()
/*
* Killing the barbeque ward.
*/
call UnitApplyTimedLife(ward[this], 'BTLF', 0.01)
/*
* Unlinking the ward from this instance
*/
set cache[GetHandleId(ward[this])] = 0
/*
* Nulling the members of this instance.
*/
set caster[this] = null
set ward[this] = null
set owner[this] = null
set time[this] = 0
endif
endif
/*
* We reset our loop index.
*/
set index = 0
/*
* Now we are iterating over all
* the chunks of meat for the current
* instance.
*/
loop
exitwhen index == count[this]
set current = stack[this][index]
/*
* We change the height of the meat
* chunk and increase the speed.
*/
set current.z = current.z - current.speed
set current.speed = current.speed + ACCELERATION
call SetUnitFlyHeight(current.dummy.unit, current.z, 0)
/*
* Checking if the dummy unit
* is close to the ground.
*/
if current.z < 8 then
/*
* We pick all units in range of
* the meat chunk and iterate.
*/
call GroupEnumUnitsInRange(enumGroup, current.x, current.y, MEAT_DAMAGE_RADIUS, null)
loop
set u = FirstOfGroup(enumGroup)
exitwhen u == null
call GroupRemoveUnit(enumGroup, u)
/*
* We check if the picked unit passes
* the given target filter.
*/
if TargetFilter(owner[this], u) then
/*
* We deal damage to the picked unit.
*/
call UnitDamageTarget(caster[this], u, damage[this], false, false, ATTACK_TYPE, DAMAGE_TYPE, null)
endif
endloop
/*
* The current chunk has fell to the
* ground, so we can destroy it.
*/
call current.destroy()
/*
* Then we remove it from
* the current MEAT STACK.
*/
set count[this] = count[this] - 1
set stack[this][index] = stack[this][count[this]]
set index = index - 1
endif
/*
* Go to the next chunk of meat.
*/
set index = index + 1
endloop
implement CTTCEnd
private static method run takes nothing returns nothing
local thistype this = create()
local integer level
/*
* We first store the caster, his
* owner and the target coordinates.
*/
set caster[this] = GetTriggerUnit()
set owner[this] = GetTriggerPlayer()
set wardX[this] = GetSpellTargetX()
set wardY[this] = GetSpellTargetY()
/*
* Next, we create the barbeque ward.
* If this ward dies, the spell ends.
*/
set ward[this] = CreateUnit(owner[this], WARD_CODE, wardX[this], wardY[this], 270)
/*
* Now we cache the level which
* will be used in the formulas
* for damage, range, ticks
* and chunks per second.
*/
set level = GetUnitAbilityLevel(caster[this], ABIL_CODE)
set damage[this] = GetDamagePerChunk(level)
set range[this] = GetRadius(level)
set ticks[this] = R2I(GetDuration(level) / TIMEOUT)
set interval[this] = 1 / GetChunksPerSecond(level)
/*
* We apply timed life to the ward
* only to show the player how much
* time is left before the spell ends.
*/
call UnitApplyTimedLife(ward[this], 'BTLF', GetDuration(level))
/*
* We reset the stack for
* the current instance.
*/
if stack[this] == 0 then
set stack[this] = Table.create()
endif
/*
* And finally, we link this instance
* with the ward so that we can end it
* upon the death of this ward.
*/
set cache[GetHandleId(ward[this])] = this
endmethod
private static method onDeath takes nothing returns nothing
local integer handleId = GetHandleId(GetTriggerUnit())
local thistype this = thistype(cache[handleId])
/*
* If there was actually an instance of this associated with
* the dying unit, then it was in fact a Barbeque ward, and so,
* we ... the comments below detail what we are going to do.
*/
if this != 0 then
/*
* We set the ticks to 0 to disable
* meat chunk summoning and we unlink the
* ward from the current instance in case
* the handle id of the ward is not
* retrievable after all the meat chunks
* hit the ground when we want to destroy
* the spell instance.
*/
set ticks[this] = 0
set cache[handleId] = 0
/*
* We remove the ward from the
* game because we do not need it.
*/
call RemoveUnit(ward[this])
/*
* Finally, we create the special effect
* at the position of the ward.
*/
call DestroyEffect(AddSpecialEffect(WARD_EFFECT_PATH, wardX[this], wardY[this]))
endif
endmethod
private static method onInit takes nothing returns nothing
/*
* We register the spell effect event
* to our run function and we register
* the death event so we can detect
* the death of a Barbeque ward.
*/
call RegisterSpellEffectEvent(ABIL_CODE, function thistype.run)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function thistype.onDeath)
/*
* We create our instance table.
* This will be used to link wards to
* their respective instances.
*/
set cache = Table.create()
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
/**************************************************************
*
* RegisterPlayerUnitEvent
* v5.1.0.0
* 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 = 260 + 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=26
//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
//
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=130
//TESH.alwaysfold=0
library Tt /* v2.1.6.3
*************************************************************************************
*
* Timer tools timers use a special merging algorithm. When two timers merge, they
* both expire on the same timer. The first tick accuracy is based on the size of the timer's timeout.
* The larger the timeout, the more inaccurate the first tick can be. The tick only becomes inaccurate
* if it merges with another timer.
*
* Max Timeout for CTM: 25
* Min Timeout for CTM: .003125
*
* Specializes in repeating timers.
*
************************************************************************************
*
* SETTINGS
*/
/*
* Checks if any CTM modules appear to be constant
*/
static if DEBUG_MODE then
private struct DEBI extends array
static constant boolean SUGGEST_MODULE_CHANGES = true
endstruct
endif
globals
/*************************************************************************************
*
* RELATIVE_MERGE
*
* Effects the accuracy of first tick. The smaller the merge value, the less chance two
* timers have of sharing the same first tick, which leads to worse performance.
* However, a larger merge value decreases the accuracy of the first tick.
*
* Formula: Ln(RELATIVE_MERGE/timeout)/230.258509*3600*timeout
* Ln(64000/3600)/230.258509*3600=44.995589035797596625536391805959 seconds max off
*
*************************************************************************************/
private constant real RELATIVE_MERGE = 64000
/*************************************************************************************
*
* CONSTANT_MERGE
*
* Effects the accuracy of the first tick. This is a constant merge. If this value +.01 is
* greater than the value calculated from RELATIVE_MERGE, the timer auto merges.
*
* Constant merge must be greater than 0.
*
*************************************************************************************/
private constant real CONSTANT_MERGE = .1
endglobals
/*
************************************************************************************
*
* Functions
*
* function GetExpired takes nothing returns integer
* - Gets the expiring timer. This is not the expiring timer method instance! This is read
* - inside of GetTimerFirstInstance, which is why timer instances can only be retrieved inside of
* - an expiring timer method.
* -
* - Boolean expressions on the same timer will expire for that same timer. Use timer methods instead.
*
* function TimerMethodAddInstance takes TimerList list, integer timerMethod returns integer
* - Adds a new instance to a timer list. These must be looped through inside of the boolean expression method.
* - Returns a timer instance.
* function TimerMethodRemoveInstance takes integer timerInstance returns nothing
* - Removes an instance from a timer method. Timer method is destroyed after it is done expiring when no instances
* - are left.
* function CreateTimerMethod takes boolexpr method returns integer
* - Creates a new timer method from a boolean expression. This boolean expression is run whenever the timer
* - expires.
*
* function TimerGetElapsedEx takes integer timerInstance returns real
* function TimerGetRemainingEx takes integer timerInstance returns real
* function TimerGetTimeoutEx takes integer timerInstance returns real
*
* function GetTimerFirstInstance takes integer timerMethod returns integer
* - Gets first timer instance in a timer method. Can only be called within an expiring timer method.
* function GetTimerNext takes integer timerInstance returns integer
* - Gets next timer instance given a timer instance.
* - Sentinel is 0.
*
* function GetTimerFirst takes nothing returns integer
* - Can only be called right after adding a new instance to a timer method
* - Useful for storing first into an array
* function GetTimerFutureFirst takes nothing returns integer
* - Can only be called right after adding a new instance to a timer method
* - Useful for storing future first into an array so that the first
* - can be updated after the timer expires.
* function GetTimerParent takes nothing returns integer
* - Retrieves parent of newly added instance
* - Can only be used directly after adding instance
*
* - set first[GetTimerParent()] = GetTimerFirst()
* - set futureFirst[GetTimerParent()] = GetTimerFutureFirst()
*
* function TimerMethodAddInstanceC takes TimerList list, integer timerMethod returns integer
* - Adds a new instance to a timer method on a constant merging timer list.
* function TimerMethodRemoveInstanceC takes integer timerInstance returns nothing
* - Removes an instance from a timer method on a constant merging timer list.
*
* function GetTimerFirstC takes integer timerMethod returns integer
* - Retrieves the first node given a timer method on a constant merging timer list. Can be called at any point.
*
* set this = GetTimerFirstC(myTimerMethod)
* loop
* exitwhen 0 == this
* set this = GetTimerNext(this)
* endloop
*
************************************************************************************
*
* struct TimerList extends array
*
* method register takes boolexpr b returns integer
* - Registers a boolean expression to timer
* method unregister takes integer i returns nothing
* - Unregisters boolean expression from timer
* - Timer is automatically destroyed if no boolean expressions are left
*
************************************************************************************
*
* struct Timer extends array
* static method operator [] takes real timeout returns thistype
* - Converts a timeout to a timer group
* method operator list takes nothing returns TimerList
* - Creates a new timer in the timer group if there are no viable currently
* - existing timers. Will return an existing timer if that timer either expires soon enough
* - or has enough time left to be close enough to the timeout.
*
************************************************************************************
*
* Modules
*
* module CTM (optional)
* locals
* module CTMExpire (not optional)
* expiration code
* module CTMNull (optional)
* null locals
* module CTMEnd (not optional)
* static method create takes real timeout returns thistype
* method destroy takes nothing returns nothing
* method operator elapsed takes nothing returns real
* method operator remaining takes nothing returns real
* method operator timeout takes nothing returns real
*
* Method: Constant
* Timeout: Not Constant
* Timeout List: Not Constant
*
* module CTT (optional)
* locals
* module CTTExpire (not optional)
* expiration code
* module CTTNull (optional)
* null locals
* module CTTEnd (not optional)
* static method create takes nothing returns thistype
* method destroy takes nothing returns nothing
* method operator elapsed takes nothing returns real
* method operator remaining takes nothing returns real
* method operator timeout takes nothing returns real
*
* Requires: private static constant real TIMEOUT
*
* Method: Constant
* Timeout: Constant
* Timeout List: Not Constant
*
* module CTTC (optional)
* locals
* module CTTCExpire (not optional)
* expiration code
* module CTTCNull (optional)
* null locals
* module CTTCEnd (not optional)
* static method create takes nothing returns thistype
* method destroy takes nothing returns nothing
* method operator elapsed takes nothing returns real
* method operator remaining takes nothing returns real
* method operator timeout takes nothing returns real
*
* Requires: private static constant real TIMEOUT that will be constant merge
*
* Method: Constant
* Timeout: Constant
* Timeout List: Constant
*
************************************************************************************/
globals
//list
private integer array lf //timeout first node
private integer array ln //node next
private integer array lp //node previous
private integer array lr //node recycler
private integer lc = 0 //node instance count
private timer array tm //node timer
//nodes
private trigger array nt //node trigger
private integer array nc //node instance count
private integer array nh //node head (timeout*320)
private boolean array aa //node set to add after
private boolean array nr //node running
//trigger condition instancing
private integer ntcc = 0 //instance count
private integer array ntcr //recycler
private triggercondition array ntc //condition
private boolean array nprm //not permanent
//trigger condition add after
private integer array paf //first
private integer array pan //next
private integer array pap //prev
private boolexpr array pab //set to add after
//trigger condition destroy after
private integer array ds //to destroy
private integer dsc = 0 //destroy count
//expiring timer
private integer exp = 0
//module
//timer method instancing, stores boolean expressions
//timer method can't be destroyed
private integer mtfr = 0 //instance count
private boolexpr array mb //boolexpr
private integer mnf = 0 //module node first return on add
private integer mnft = 0 //module node future first return on add
private integer mnp = 0 //module node parent
//first[mnp] = mnf
//futureFirst[mnp] = mnft
//list
private hashtable mlt = InitHashtable() //stores firsts and counts
//instance count: + +
//method first: - -
//method future first: - +
//registered method instance: + -
//actual expiring nodes
private integer array mln //expiration next
private integer array mlp //expiration previous
//instantly updated list
private integer array mlnd //expiration next true
private integer array mlpd //expiration prev true
//add instances after expiration
private integer array mlnp //previous
private integer array mlpp //next
private boolean array mlap //is set to add after
private integer array mlfp //first to add after
//recycler
private integer array mlr //module node recycler
private integer mlc = 0 //module node instance count
//nodes
private integer array mnh //module node head (head is timeout node)
private integer array mno //module node origin (origin is timer method instance, boolexpr)
//destroy after
private integer array mds //node to destroy
private integer mdsc = 0 //node to destroy count
private boolean array mdb //is node marked to destroy
private integer array mpf //module perm first
private integer array mpff //module perm future first
private integer array mptc //module perm tc
private boolean array mp //module perm
endglobals
//hiveworkshop.com/forums/jass-functions-413/snippet-natural-logarithm-108059/
//credits to BlinkBoy
private function Ln takes real a returns real
local real s=0
loop
exitwhen a<2.71828
set a=a/2.71828
set s=s+1
endloop
return s+(a-1)*(1+8/(1+a)+1/a)/6
endfunction
static if DEBUG_MODE then
private struct DEB extends array
static boolean array al //is timeout node allocated
static boolean array alm //is module node allocated
static boolean en = true //is system enabled
static timer array tm2 //timer to verify paused/started timers don't freak out
endstruct
/*
* Disable System
*/
private function DIS takes nothing returns nothing
set DEB.en = false
//pause all running timers
loop
exitwhen 0 == lc
call PauseTimer(tm[lc])
debug call PauseTimer(DEB.tm2[lc])
set lc = lc - 1
endloop
endfunction
private function CMQ takes real a returns boolean
local real m = Ln(RELATIVE_MERGE/a)/230.258509*a
if (m < CONSTANT_MERGE) then
set m = CONSTANT_MERGE
endif
return CONSTANT_MERGE >= (a-m)/2
endfunction
endif
/*
* Allocate Timeout Node
*/
private function AL takes nothing returns integer
//standard allocation
local integer i = lr[0]
if (0 == i) then
set lc = lc + 1
debug if (8191 < lc) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"INSTANE OVERLOAD")
debug call DIS()
debug endif
debug set DEB.al[lc] = true
set tm[lc] = CreateTimer()
set nt[lc] = CreateTrigger()
return lc
endif
set lr[0] = lr[i]
debug set DEB.al[i] = true
return i
endfunction
function GetExpired takes nothing returns integer
return exp
endfunction
/*
* Delete Module Node
*/
private function DELM takes integer i returns nothing
debug if (DEB.en) then
debug if (DEB.alm[i]) then
set mlr[i] = mlr[0]
set mlr[0] = i
debug set DEB.alm[i] = false
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DOUBLE FREE: "+I2S(i))
debug call DIS()
debug endif
debug endif
endfunction
//! textmacro TIMER_TOOLS_ADD_NODE takes D
debug if (DEB.en) then
debug if (DEB.alm[i]) then
set mln$D$[i] = 0
if (0 == mlp$D$[t]) then
set mlp$D$[i] = t
set mln$D$[t] = i
else
set mlp$D$[i] = mlp$D$[t]
set mln$D$[mlp$D$[i]] = i
endif
set mlp$D$[t] = i
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"ATTEMPTED TO ADD NULL NODE: "+I2S(i)+" on "+I2S(mnh[i]))
debug call DIS()
debug endif
debug endif
//! endtextmacro
/*
* Add Module Node
*/
private function ADDM takes integer i, integer t returns nothing
//! runtextmacro TIMER_TOOLS_ADD_NODE("")
endfunction
/*
* Add Module Node To Instant Update True List
*/
private function ADDMD takes integer i, integer t returns nothing
//! runtextmacro TIMER_TOOLS_ADD_NODE("d")
endfunction
/*
* Add Module Node To Add After
*/
private function ADDMP takes integer i, integer t returns nothing
//! runtextmacro TIMER_TOOLS_ADD_NODE("p")
endfunction
//! textmacro TIMER_TOOLS_REMOVE_NODE takes D
debug if (DEB.en) then
debug if (DEB.alm[i]) then
if (0 != f) then
if (0 == mln$D$[i]) then
set mlp$D$[f] = mlp$D$[i]
else
set mlp$D$[mln$D$[i]] = mlp$D$[i]
endif
set mln$D$[mlp$D$[i]] = mln$D$[i]
if (f == mln$D$[f]) then
set mln$D$[f] = 0
set mlp$D$[f] = 0
else
set mln$D$[mlp$D$[f]] = 0
endif
endif
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"ATTEMPTED TO REMOVE NULL NODE: "+I2S(i)+" on "+I2S(mnh[i]))
debug call DIS()
debug endif
debug endif
//! endtextmacro
/*
* Remove Module Node
*/
private function REMM takes integer i, integer f returns nothing
//! runtextmacro TIMER_TOOLS_REMOVE_NODE("")
endfunction
/*
* Remove Module Node From Instant Update True List
*/
private function REMMD takes integer i, integer f returns nothing
//! runtextmacro TIMER_TOOLS_REMOVE_NODE("d")
endfunction
/*
* Remove Module Node From Add After List
*/
private function REMMP takes integer i, integer f returns nothing
//! runtextmacro TIMER_TOOLS_REMOVE_NODE("p")
endfunction
/*
* Add Module Node To Destroy After Stack
*/
private function DAF takes integer i returns nothing
debug if (DEB.en) then
debug if (not mdb[i]) then
set mdb[i] = true
set mds[mdsc] = i
set mdsc = mdsc + 1
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DOUBLE FREE ATTEMPT: "+I2S(i))
debug call DIS()
debug endif
debug endif
endfunction
/*
* On Expiration
*/
private function EXP takes nothing returns nothing
//retrieve timeout instance
local integer e = R2I(TimerGetTimeout(GetExpiredTimer())*320+.5)
local integer i = e
//retrieve first node on timeout
local integer n = lf[i]
local integer t
local integer f
//set first = first.next
set lf[i] = ln[n]
debug if (DEB.en) then
debug if (not DEB.al[n]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG NODE: "+I2S(n))
debug call DIS()
debug return
debug endif
debug if (0 != TimerGetRemaining(DEB.tm2[n])) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG TIMEOUT NODE: "+I2S(n)+" "+R2S(TimerGetRemaining(DEB.tm2[n])))
debug call DIS()
debug return
debug endif
set nr[n] = true //node running = true
set exp = n //expiring = node
call TriggerEvaluate(nt[n]) //evaluate all registered methods
set exp = 0 //expiring = 0
set nr[n] = false //node running = false
//add all to add after to node
set i = paf[n]
set paf[n] = 0
loop
exitwhen 0 == i
set ntc[i] = TriggerAddCondition(nt[n], pab[i])
set pab[i] = null
set ntcr[i] = ntcr[0]
set ntcr[0] = i
set i = pan[i]
endloop
//add all to add after to modules
set i = mlfp[n]
set mlfp[n] = 0
loop
exitwhen 0 == i
if (mp[i]) then
set t = mpf[mno[i]]
else
set t = LoadInteger(mlt, -mno[i], -mnh[i]) //mlf
endif
call ADDM(i, t)
set i = mlnp[i]
endloop
//destroy all to destroy after from node
set i = dsc
loop
exitwhen 0 == i
set i = i - 1
call TriggerRemoveCondition(nt[n], ntc[ds[i]])
set ntc[ds[i]] = null
endloop
set dsc = 0
//destroy all to destroy after from modules
set t = mdsc
loop
exitwhen 0 == t
set t = t - 1
set i = mds[t]
set mdb[i] = false
if (mp[i]) then
set f = mpf[mno[i]]
if (f == i) then
set f = mpff[mno[i]]
if (f == i) then
set f = 0
endif
set mpf[mno[i]] = f
endif
else
set f = LoadInteger(mlt, -mno[i], -mnh[i]) //mlf
if (f == i) then
set f = LoadInteger(mlt, -mno[i], mnh[i]) //mlf2
if (f == i) then
set f = 0
endif
call SaveInteger(mlt, -mno[i], -mnh[i], f) //mlf
endif
endif
call REMM(i, f)
call DELM(i)
set mlap[i] = false
endloop
set mdsc = 0
//if no methods are left on the node, destroy the node
if (0 == nc[n]) then
//handle timer
call PauseTimer(tm[n])
debug call PauseTimer(DEB.tm2[n])
if (nprm[e]) then
debug call DestroyTimer(DEB.tm2[n])
debug set DEB.tm2[n] = null
//remove from timeout list
if (lf[nh[n]] == n) then
set lf[nh[n]] = ln[n]
endif
set ln[lp[n]] = ln[n]
set lp[ln[n]] = lp[n]
if (ln[n] == n) then
set lf[nh[n]] = 0
endif
//recycler
set lr[n] = lr[0]
set lr[0] = n
endif
debug set DEB.al[n] = false
endif
debug endif
endfunction
struct TimerList extends array
method register takes boolexpr b returns integer
local integer h = nh[this]
//allocate new trigger condition
local integer i = ntcr[0]
if (0 == i) then
set i = ntcc + 1
set ntcc = i
else
set ntcr[0] = ntcr[i]
endif
if (0 == nc[this] and not nprm[h]) then
call TimerStart(tm[this], h/320., true, function EXP)
debug call TimerStart(DEB.tm2[this], h/320., true, null)
set ntc[i] = TriggerAddCondition(nt[this], b)
debug set DEB.al[this] = true
//if to add after (to add after is marked when TimerList is retrieved)
//only store TimerList for perm
elseif (aa[this]) then
//add it to add after list
if (0 == paf[this]) then
set paf[this] = i
set pan[i] = 0
set pap[i] = i
else
set pan[i] = 0
set pap[i] = pap[paf[this]]
set pan[pap[i]] = i
set pap[paf[this]] = i
endif
set pab[i] = b
else
//register condition
set ntc[i] = TriggerAddCondition(nt[this], b)
endif
//increase node count
set nc[this] = nc[this] + 1
return i
endmethod
method unregister takes integer i returns nothing
local integer h = nh[this]
//if the node has not been set to be added after
if (null == pab[i]) then
//if node running, add to destroy after stack
if (nr[this]) then
set ds[dsc] = i
set dsc = dsc + 1
else
//remove instantly
call TriggerRemoveCondition(nt[this], ntc[i])
set ntc[i] = null
endif
else
//remove from to add after list
if (paf[this] == i) then
set paf[this] = pan[i]
endif
if (0 == pan[i]) then
set pap[paf[this]] = pap[i]
else
set pap[pan[i]] = pap[i]
endif
if (0 == pap[i]) then
set pan[paf[this]] = pan[i]
else
set pan[pap[i]] = pan[i]
endif
//mark as not to add after
set pab[i] = null
//recycler
set ntcr[i] = ntcr[0]
set ntcr[0] = i
endif
//if the node isn't running, do instant update
set nc[this] = nc[this] - 1
//destroy node
if (not nr[this] and 0 == nc[this]) then
//handle timer
call PauseTimer(tm[this])
if (nprm[h]) then
debug call PauseTimer(DEB.tm2[this])
debug call DestroyTimer(DEB.tm2[this])
debug set DEB.tm2[this] = null
//remove node from timeout list
if (lf[nh[this]] == this) then
set lf[nh[this]] = ln[this]
endif
set ln[lp[this]] = ln[this]
set lp[ln[this]] = lp[this]
if (ln[this] == this) then
set lf[nh[this]] = 0
endif
//recycler
set lr[this] = lr[0]
set lr[0] = this
debug set DEB.al[this] = false
endif
endif
endmethod
endstruct
struct Timer extends array
static method operator [] takes real timeout returns thistype
local integer i = R2I(timeout*320)
if (0 == i) then
return 1
elseif (8191 < i) then
return 8191
endif
return i
endmethod
method operator list takes nothing returns TimerList
local integer t = lf[this] //first timeout node
local integer l
local real tl
local real a //timeout
local integer n
//if no first timeout node
if (0 == t) then
set a = this/320.
//allocate
set t = AL()
debug set DEB.tm2[this] = CreateTimer()
//initialize list
set lf[this] = t
set lp[t] = t
set ln[t] = t
if (not nprm[this]) then
set tl = Ln(RELATIVE_MERGE/a)/230.258509*a
if (tl < CONSTANT_MERGE) then
set tl = CONSTANT_MERGE
endif
set nprm[this] = CONSTANT_MERGE < (a-tl)/2
endif
if (nprm[this]) then
debug if (DEB.en) then
//start timer
call TimerStart(tm[t], a, true, function EXP)
debug call TimerStart(DEB.tm2[t], a, true, null)
debug endif
endif
//head is always timeout for a timeout node
set nh[t] = this
//not adding after (added now)
set aa[t] = false
return t
endif
if (nprm[this]) then
set a = this/320.
//calculate relative timeout limit
set tl = Ln(RELATIVE_MERGE/a)/230.258509*a
//if the relative limit is less than the constant limit
//make it the constant limit
if (tl < CONSTANT_MERGE) then
set tl = CONSTANT_MERGE
endif
set l = lp[t]
if (TimerGetElapsed(tm[t]) < tl) then
//merge first node before
set aa[t] = false
return t
elseif (TimerGetRemaining(tm[l]) < tl) then
//merge last node after
set aa[l] = true
return l
elseif (TimerGetElapsed(tm[l]) < tl) then
//merge last node before
set aa[l] = false
return l
endif
//create new node
set n = AL()
debug if (DEB.en) then
call TimerStart(tm[n], a, true, function EXP)
debug set DEB.tm2[n] = CreateTimer()
debug call TimerStart(DEB.tm2[n], a, true, null)
debug endif
//add to list
set ln[l] = n
set lp[t] = n
set ln[n] = t
set lp[n] = l
set nh[n] = this
//add immediately
set aa[n] = false
return n
else
set aa[t] = TimerGetElapsed(tm[t]) > integer(this)/640.
endif
return t
endmethod
endstruct
/*
* Allocate module node
*/
private function ALM takes nothing returns integer
local integer i = mlr[0]
debug if (DEB.alm[i]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DOUBLE ALLOCATE: "+I2S(i))
debug call DIS()
debug return 0
debug endif
if (0 == i) then
set mlc = mlc + 1
debug if (8191 < mlc) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"INSTANCE OVERLOAD")
debug call DIS()
debug endif
debug set DEB.alm[mlc] = true
return mlc
endif
set mlr[0] = mlr[i]
set mp[i] = false
debug set DEB.alm[i] = true
return i
endfunction
private function RegisterMethod takes integer this, integer t, integer i, boolean after returns nothing
//this: timeout head
//t: node timeout head
//i: module node
//always add to true list for an instant update
call ADDMD(i, t)
//if not adding after, can add to actual list as well
if (not after) then
call ADDM(i, t)
else
//add to add after list
set t = mlfp[this]
if (0 == t) then
set mlfp[this] = i
set mlnp[i] = 0
set mlpp[i] = 0
else
call ADDMP(i, t)
endif
endif
//only called when a timer method is initialized (no first)
endfunction
function TimerMethodAddInstance takes TimerList this, integer o returns integer
local integer t = LoadInteger(mlt, -o, this) //mlf2
local integer n
//return head (timeout)
set mnp = this
//if no first, initialize
if (0 == t) then
set t = ALM()
call SaveInteger(mlt, -o, -this, t) //mlf, first
call SaveInteger(mlt, -o, this, t) //mlf2, future first
call SaveInteger(mlt, o, -this, this.register(mb[o])) //mnt, condition
//initialize lists
set mlp[t] = 0
set mln[t] = 0
set mlpd[t] = 0
set mlnd[t] = 0
//head, origin (o = timer method)
set mnh[t] = this
set mno[t] = o
//first, future first
set mnf = t
set mnft = t
return t
endif
//first and future first will always be t as n is always added to t
set mnf = t
set mnft = t
set n = ALM()
//head is timeout, o is method
set mnh[n] = this
set mno[n] = o
//add n to t
call RegisterMethod(this, t, n, aa[this])
return n
endfunction
function TimerMethodRemoveInstance takes integer i returns nothing
local TimerList this = mnh[i] //timeout
local integer o = mno[i] //method
local integer f = LoadInteger(mlt, -o, this) //mlf2
debug if (not DEB.alm[i]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"Attempted To Deallocate Null: "+I2S(i))
debug call DIS()
debug return
debug endif
//update future*** first
if (f == i) then
set f = mlnd[i]
if (0 == f) then
call this.unregister(LoadInteger(mlt, o, -this))
endif
call SaveInteger(mlt, -o, this, f) //mlf2
endif
set mnft = f
//remove from true list
call REMMD(i, f)
//if set to add after, remove from add after list, first does not change
if (mlap[i]) then
set mnf = LoadInteger(mlt, -o, -this) //mlf, first
//remove from to add after list
call REMMP(i, mlfp[this])
//set to add after to false
set mlap[i] = false
//delete
call DELM(i)
else
//if node is running, mark to destroy after
if (nr[this]) then
call DAF(i)
//first doesn't change
set mnf = LoadInteger(mlt, -o, -this) //mlf
else
//first can** change
set f = LoadInteger(mlt, -o, -this) //mlf
//if deallocated node is first, set first to next
if (f == i) then
set f = mln[i]
endif
//remove from list
call REMM(i, f)
//delete
call DELM(i)
//update first
call SaveInteger(mlt, -o, -this, f) //mlf
//return possibly new first
set mnf = f
endif
endif
endfunction
function TimerMethodAddInstanceC takes TimerList this, integer o returns integer
local integer t = mpff[o]
local integer n
debug if (nprm[nh[this]]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"ATTEMPTED TO USE CONSTANT MERGE FOR NON CONSTANT MERGE TIMEOUT")
debug call DIS()
debug endif
//if no first, initialize
if (0 == t) then
set t = ALM()
set mp[t] = true
set mpf[o] = t
set mpff[o] = t
set mptc[o] = this.register(mb[o])
//initialize lists
set mlp[t] = 0
set mln[t] = 0
set mlpd[t] = 0
set mlnd[t] = 0
//head, origin (o = timer method)
set mnh[t] = this
set mno[t] = o
return t
endif
set n = ALM()
set mp[n] = true
//head is timeout, o is method
set mnh[n] = this
set mno[n] = o
//add n to t
call RegisterMethod(this, t, n, aa[this])
return n
endfunction
function TimerMethodRemoveInstanceC takes integer i returns nothing
local TimerList this = mnh[i] //timeout
local integer o = mno[i] //method
local integer f = mpff[o]
debug if (nprm[nh[this]]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"ATTEMPTED TO USE CONSTANT MERGE FOR NON CONSTANT MERGE TIMEOUT")
debug call DIS()
debug endif
debug if (not DEB.alm[i]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"Attempted To Deallocate Null: "+I2S(i))
debug call DIS()
debug return
debug endif
//update future*** first
if (f == i) then
set f = mlnd[i]
if (0 == f) then
call this.unregister(mptc[o])
endif
set mpff[o] = f
endif
//remove from true list
call REMMD(i, f)
//if set to add after, remove from add after list, first does not change
if (mlap[i]) then
//remove from to add after list
call REMMP(i, mlfp[this])
//set to add after to false
set mlap[i] = false
//delete
call DELM(i)
else
//if node is running, mark to destroy after
if (nr[this]) then
call DAF(i)
else
//first can** change
set f = mpf[o]
//if deallocated node is first, set first to next
if (f == i) then
set f = mln[i]
endif
//remove from list
call REMM(i, f)
//delete
call DELM(i)
//update first
set mpf[o] = f
endif
endif
endfunction
function CreateTimerMethod takes boolexpr b returns integer
//allocate new timer method (simple)
set mtfr = mtfr + 1
set mb[mtfr] = b
return mtfr
endfunction
function TimerGetElapsedEx takes integer i returns real
return TimerGetElapsed(tm[mnh[i]])
endfunction
function TimerGetRemainingEx takes integer i returns real
return TimerGetRemaining(tm[mnh[i]])
endfunction
function TimerGetTimeoutEx takes integer i returns real
return TimerGetTimeout(tm[mnh[i]])
endfunction
function GetTimerFirstInstance takes integer timerMethod returns integer
return LoadInteger(mlt, -timerMethod, -exp) //mlf
endfunction
function GetTimerNext takes integer timerInstance returns integer
return mln[timerInstance]
endfunction
function GetTimerFirst takes nothing returns integer
return mnf
endfunction
function GetTimerFutureFirst takes nothing returns integer
return mnft
endfunction
function GetTimerParent takes nothing returns integer
return mnp
endfunction
function GetTimerFirstC takes integer o returns integer
return mpf[o]
endfunction
module CTM
debug private static integer created = 0
debug private static boolean array timerH
debug private static integer timerCount = 0
static integer CTMf //timer method
static integer array CTMq //current first
static integer array CTMqt //future first
static method create takes real t returns thistype
local integer i
local integer n
debug if (DEB.en) then
//get new instance
set i = TimerMethodAddInstance(Timer[t].list, CTMf)
debug if (DEBI.SUGGEST_MODULE_CHANGES) then
debug set created = created + 1
debug if (not timerH[Timer[t]]) then
debug set timerH[Timer[t]] = true
debug set timerCount = timerCount + 1
debug endif
debug if (created > 30 and timerCount == 1) then
debug if (CMQ(t)) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"IT APPEARS THAT THIS MODULE IS A CONSTANT MERGE TIMEOUT, TRY USING CTTC INSTEAD OF CTM: "+R2S(t))
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"IT APPEARS THAT THIS MODULE IS A CONSTANT TIMEOUT, TRY USING CTT INSTEAD OF CTM: "+R2S(t))
debug endif
debug call DIS()
debug endif
debug endif
//update firsts
set CTMq[mnp] = mnf
set CTMqt[mnp] = mnft
debug if (mnf != LoadInteger(mlt, -CTMf, -mnh[i])) then //mlf
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"CREATE FIRST DESYNC: "+I2S(mnf)+"!="+I2S(LoadInteger(mlt, -CTMf, -mnh[i]))) //mlf
debug call DIS()
debug endif
debug if (mnft != LoadInteger(mlt, -CTMf, mnh[i])) then //mlf2
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"CREATE FIRST DESYNC TEMP: "+I2S(mnft)+"!="+I2S(LoadInteger(mlt, -CTMf, mnh[i]))) //mlf2
debug call DIS()
debug endif
return i
debug endif
debug return 0
endmethod
method destroy takes nothing returns nothing
local integer n
debug if (DEB.en) then
//destroy
call TimerMethodRemoveInstance(this)
//update firsts
set CTMq[mnh[this]] = mnf
set CTMqt[mnh[this]] = mnft
debug if (mnf != LoadInteger(mlt, -CTMf, -mnh[this])) then //mlf
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DESTROY FIRST DESYNC: "+I2S(mnf)+"!="+I2S(LoadInteger(mlt, -CTMf, -mnh[this]))) //mlf
debug call DIS()
debug endif
debug if (mnft != LoadInteger(mlt, -CTMf, mnh[this])) then //mlf2
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DESTROY FIRST DESYNC TEMP: "+I2S(mnft)+"!="+I2S(LoadInteger(mlt, -CTMf, mnh[this]))) //mlf2
debug call DIS()
debug endif
debug endif
endmethod
method operator elapsed takes nothing returns real
return TimerGetElapsedEx(this)
endmethod
method operator remaining takes nothing returns real
return TimerGetRemainingEx(this)
endmethod
method operator timeout takes nothing returns real
return TimerGetTimeoutEx(this)
endmethod
static method CTMe takes nothing returns boolean
//get first
local thistype this = CTMq[exp]
debug local boolean array hit
endmodule
module CTMExpire
implement CTM
debug if (not DEB.en) then
debug return false
debug endif
debug if (CTMq[exp] != LoadInteger(mlt, -CTMf, -exp)) then //mlf
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG FIRST DESYNC: "+I2S(CTMq[exp])+"!="+I2S(LoadInteger(mlt, -CTMf, -exp))+" from "+I2S(exp)) //mlf
debug call DIS()
debug return false
debug endif
debug if (CTMqt[exp] != LoadInteger(mlt, -CTMf, exp)) then //mlf2
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG FIRST DESYNC TEMP: "+I2S(CTMqt[exp])+"!="+I2S(LoadInteger(mlt, -CTMf, exp))+" from "+I2S(exp)) //mlf2
debug call DIS()
debug return false
debug endif
debug if (0 == this) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"NULL TIMER EXPIRED ON "+I2S(exp))
debug call DIS()
debug endif
//loop through all instances
loop
exitwhen 0 == this
debug if (hit[this]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG: HIT "+I2S(this)+" MORE THAN ONCE FROM "+I2S(exp))
debug call DIS()
debug return false
debug endif
debug set hit[this] = true
endmodule
module CTMNull
debug if (mnh[this] != exp) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG: "+I2S(this)+" "+I2S(mnh[this])+"!="+I2S(exp)+" from "+I2S(exp))
debug call DIS()
debug return false
debug endif
debug if (not DEB.alm[this]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG NOT ALLOCATED: "+I2S(this)+" from "+I2S(exp))
debug call DIS()
debug return false
debug endif
set this = mln[this]
endloop
//update first to future first
set CTMq[exp] = CTMqt[exp]
endmodule
module CTMEnd
implement CTMNull
return false
endmethod
private static method onInit takes nothing returns nothing
//initialize timer method
set CTMf = CreateTimerMethod(Condition(function thistype.CTMe))
endmethod
endmodule
module CTT
static Timer CTMt //timout
static integer CTMf //timer method
static integer array CTMq //current first
static integer array CTMqt //future first
static method create takes nothing returns thistype
local integer i
local integer n
debug if (DEB.en) then
//get new instance
set i = TimerMethodAddInstance(CTMt.list, CTMf)
//update firsts
set CTMq[mnp] = mnf
set CTMqt[mnp] = mnft
debug if (mnf != LoadInteger(mlt, -CTMf, -mnh[i])) then //mlf
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"CREATE FIRST DESYNC: "+I2S(mnf)+"!="+I2S(LoadInteger(mlt, -CTMf, -mnh[i]))) //mlf
debug call DIS()
debug endif
debug if (mnft != LoadInteger(mlt, -CTMf, mnh[i])) then //mlf2
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"CREATE FIRST DESYNC TEMP: "+I2S(mnft)+"!="+I2S(LoadInteger(mlt, -CTMf, mnh[i]))) //mlf2
debug call DIS()
debug endif
return i
debug endif
debug return 0
endmethod
method destroy takes nothing returns nothing
local integer n
debug if (DEB.en) then
//destroy
call TimerMethodRemoveInstance(this)
//update firsts
set CTMq[mnh[this]] = mnf
set CTMqt[mnh[this]] = mnft
debug if (mnf != LoadInteger(mlt, -CTMf, -mnh[this])) then //mlf
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DESTROY FIRST DESYNC: "+I2S(mnf)+"!="+I2S(LoadInteger(mlt, -CTMf, -mnh[this]))) //mlf
debug call DIS()
debug endif
debug if (mnft != LoadInteger(mlt, -CTMf, mnh[this])) then //mlf2
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DESTROY FIRST DESYNC TEMP: "+I2S(mnft)+"!="+I2S(LoadInteger(mlt, -CTMf, mnh[this]))) //mlf2
debug call DIS()
debug endif
debug endif
endmethod
method operator elapsed takes nothing returns real
return TimerGetElapsedEx(this)
endmethod
method operator remaining takes nothing returns real
return TimerGetRemainingEx(this)
endmethod
method operator timeout takes nothing returns real
return TIMEOUT
endmethod
static method CTMe takes nothing returns boolean
//get first
local thistype this = CTMq[exp]
debug local boolean array hit
endmodule
module CTTExpire
implement CTT
debug if (not DEB.en) then
debug return false
debug endif
debug if (CTMq[exp] != LoadInteger(mlt, -CTMf, -exp)) then //mlf
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG FIRST DESYNC: "+I2S(CTMq[exp])+"!="+I2S(LoadInteger(mlt, -CTMf, -exp))+" from "+I2S(exp)) //mlf
debug call DIS()
debug return false
debug endif
debug if (CTMqt[exp] != LoadInteger(mlt, -CTMf, exp)) then //mlf2
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG FIRST DESYNC TEMP: "+I2S(CTMqt[exp])+"!="+I2S(LoadInteger(mlt, -CTMf, exp))+" from "+I2S(exp)) //mlf2
debug call DIS()
debug return false
debug endif
debug if (0 == this) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"NULL TIMER EXPIRED ON "+I2S(exp))
debug call DIS()
debug endif
//loop through all instances
loop
exitwhen 0 == this
debug if (hit[this]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG: HIT "+I2S(this)+" MORE THAN ONCE FROM "+I2S(exp))
debug call DIS()
debug return false
debug endif
debug set hit[this] = true
endmodule
module CTTNull
debug if (mnh[this] != exp) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG: "+I2S(this)+" "+I2S(mnh[this])+"!="+I2S(exp)+" from "+I2S(exp))
debug call DIS()
debug return false
debug endif
debug if (not DEB.alm[this]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG NOT ALLOCATED: "+I2S(this)+" from "+I2S(exp))
debug call DIS()
debug return false
debug endif
set this = mln[this]
endloop
//update first to future first
set CTMq[exp] = CTMqt[exp]
endmodule
module CTTEnd
implement CTTNull
return false
endmethod
private static method onInit takes nothing returns nothing
//initialize timer method
set CTMf = CreateTimerMethod(Condition(function thistype.CTMe))
set CTMt = Timer[TIMEOUT]
debug if (CMQ(TIMEOUT)) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"THIS MODULE SHOULD BE CTTC AS TIMEOUT QUALIFIES FOR CONSTANT MERGE: "+R2S(TIMEOUT))
debug call DIS()
debug endif
endmethod
endmodule
module CTTC
static TimerList CTMt //timout list
static integer CTMf //timer method
static method create takes nothing returns thistype
local integer i
local integer n
debug if (DEB.en) then
//get new instance
set i = TimerMethodAddInstanceC(CTMt, CTMf)
return i
debug endif
debug return 0
endmethod
method destroy takes nothing returns nothing
local integer n
debug if (DEB.en) then
//destroy
call TimerMethodRemoveInstanceC(this)
debug endif
endmethod
method operator elapsed takes nothing returns real
return TimerGetElapsedEx(this)
endmethod
method operator remaining takes nothing returns real
return TimerGetRemainingEx(this)
endmethod
method operator timeout takes nothing returns real
return TIMEOUT
endmethod
static method CTMe takes nothing returns boolean
//get first
local thistype this = mpf[CTMf]
debug local boolean array hit
endmodule
module CTTCExpire
implement CTTC
debug if (not DEB.en) then
debug return false
debug endif
debug if (0 == this) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"NULL TIMER EXPIRED ON "+I2S(exp))
debug call DIS()
debug endif
//loop through all instances
loop
exitwhen 0 == this
debug if (hit[this]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG: HIT "+I2S(this)+" MORE THAN ONCE FROM "+I2S(exp))
debug call DIS()
debug return false
debug endif
debug set hit[this] = true
endmodule
module CTTCNull
debug if (mnh[this] != exp) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG: "+I2S(this)+" "+I2S(mnh[this])+"!="+I2S(exp)+" from "+I2S(exp))
debug call DIS()
debug return false
debug endif
debug if (not DEB.alm[this]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"BUG NOT ALLOCATED: "+I2S(this)+" from "+I2S(exp))
debug call DIS()
debug return false
debug endif
set this = mln[this]
endloop
endmodule
module CTTCEnd
implement CTTCNull
return false
endmethod
private static method onInit takes nothing returns nothing
//initialize timer method
set CTMf = CreateTimerMethod(Condition(function thistype.CTMe))
set CTMt = Timer[TIMEOUT].list
debug if (nprm[Timer[TIMEOUT]]) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"ATTEMPTED TO USE CONSTANT MERGE MODULE FOR NON CONSTANT MERGE TIMEOUT: "+R2S(TIMEOUT))
debug call DIS()
debug endif
endmethod
endmodule
endlibrary
//TESH.scrollpos=124
//TESH.alwaysfold=0
library Dummy /* v1.0.0.3
*************************************************************************************
*
* Allows one to create dummy units that are either at or are close
* to the angle specified.
*
* Dummy recycling minimizes the number of dummy units on the map while supporting near
* instant SetUnitFacing.
*
* Assigned dummy indexes are not unit indexes.
*
* Errors
* ----------------------------
*
* Any error will result in the system disabling itself and an error message
*
* -> May not kill dummies
* -> May not remove dummies
* -> May not attempt to recycle non dummies
*
*************************************************************************************
*
* Credits
*
* Vexorian for dummy.mdx
*
* Bribe
*
* Delayed recycling implemetation
* ----------------------------
*
* Bribe's delayed recycling implementation uses timestamps rather than timers, which
* helps improve performance.
*
* Stamps for queue node movement
* ----------------------------
*
* Convinced me that this was worth it
*
* Time it takes to rotate 180 degrees
* ----------------------------
*
* Supplied me with the number .73
*
*************************************************************************************
*
* */uses/*
*
* */ optional UnitIndexer /* hiveworkshop.com/forums/jass-functions-413/unit-indexer-172090/
*
************************************************************************************
*
* SETTINGS
*
*/
globals
/*
* The unit id of dummy.mdx
*/
private constant integer DUMMY_ID = 'h000'
/*
* The space between angles for the recycler
*
* Angles used are angles from 0 to 359 in intervals of ANGLE_SPACING
*
* Higher spacing means less units but lower accuracy when creating the facing
*
*/
private constant integer ANGLE_SPACING = 15
/*
* How many projectiles to preload per angle
*
* Preloaded projectile count is 360/ANGLE_SPACING*MAX_PROJECTILES
*
*/
private constant integer PRELOAD_PROJECTILES_PER_ANGLE = 50
/*
* How much to delay before recycling dummy
*/
private constant real RECYCLE_DELAY = 2
endglobals
/*
************************************************************************************
*
* library MissileRecycler uses Dummy
* ----------------------------
*
* For compatibility with Bribe's resource
*
* function GetRecycledMissile takes real x, real y, real z, real facing returns unit
* function RecycleMissile takes unit whichUnit returns nothing
*
************************************************************************************
*
* Functions
* ----------------------------
*
* function IsUnitDummy takes unit whichUnit returns boolean
* - This is necessary because dummy unit indexes are NOT unit indexes.
*
************************************************************************************
*
*
* struct Dummy extends array
*
* Creators/Destructors
* ----------------------------
*
* static method create takes real x, real y, real facing returns Dummy
* - For those of you who really want this to return a unit, getting
* - the unit from this is very easy, so don't whine
*
* - Dummy.create().unit -> unit
*
* method destroy takes nothing returns nothing
* - For those of you who really want this to take a unit, getting
* - the dummy index is very easy.
*
* - Dummy[whichUnit].destroy()
*
* Fields
* ----------------------------
*
* readonly unit unit
*
* Operators
* ----------------------------
*
* static method operator [] takes unit dummyUnit returns Dummy
*
************************************************************************************/
private keyword Queue
globals
/*
* Used for dummy instancing
* Dummy indexes are never destroyed, so there is no need for a recycler
*/
private Queue dummyCount = 0
/*
* Used to retrieve unit handle via dummy index
*/
private unit array dummies
/*
* The owner of all dummy units. This shouldn't be changed.
*/
private constant player DUMMY_OWNER = Player(15)
/*
* Used to apply time stamps to dummies for recycling
* purposes. A dummy is only considered recycled if its
* stamp is less than the elapsed time of stamp timer.
*/
private timer stampTimer
endglobals
function IsUnitDummy takes unit whichUnit returns boolean
return dummies[GetUnitUserData(whichUnit)] == whichUnit
endfunction
/*
* min == max - 1
* max == min + 1
*
* variance of counts must be 1
*/
private struct ArrayStack extends array
/*
* The minimum and maximum counts
*/
static thistype max = 0
static thistype min = 0
/*
* list[count].first
*/
thistype first
/*
* queue.size
*/
thistype count_p
/*
* list[count].next
*/
thistype next
/*
* list[count].prev
*/
thistype prev
/*
* list[count].first -> queue of dummies
*/
static method operator [] takes thistype index returns thistype
return index.first
endmethod
/*
* list[count].add(queue of dummies)
*/
private method add takes thistype node returns nothing
/*
* Update min/max
*/
if (integer(this) > integer(max)) then
set max = this
elseif (integer(this) < integer(min)) then
set min = this
endif
/*
* Push on to front of list like a stack
*/
set node.next = first
set node.next.prev = node
set node.prev = 0
set first = node
set node.count_p = this
endmethod
/*
* list[count].remove(list of dummies)
*/
private method remove takes thistype node returns nothing
/*
* If node is the first, update the first
*/
if (node == first) then
set first = node.next
/*
* If lis tis empty, update min/max
*/
if (0 == first) then
if (this == min) then
set min = max
else
set max = min
endif
endif
else
/*
* Simple removal
*/
set node.prev.next = node.next
set node.next.prev = node.prev
endif
endmethod
method operator count takes nothing returns integer
return count_p
endmethod
method operator count= takes thistype value returns nothing
/*
* Remove from list node was on
*/
call count_p.remove(this)
/*
* Add to new list
*/
call value.add(this)
endmethod
endstruct
/*
* queue = angle + 1
*/
private struct Queue extends array
private real stamp
thistype next
thistype last
/*
* Update dummy count for queue
*/
private method operator count takes nothing returns integer
return ArrayStack(this).count
endmethod
private method operator count= takes integer value returns nothing
set ArrayStack(this).count = value
endmethod
/*
* Queue with smallest number of dummies
*/
private static method operator min takes nothing returns thistype
return ArrayStack.min.first
endmethod
/*
* Queue with largest number of dummies
*/
private static method operator max takes nothing returns thistype
return ArrayStack.max.first
endmethod
static method add takes thistype dummy returns nothing
/*
* Always add to the queue with the least amount of dummies
*/
local thistype this = min
/*
* Add to end of queue
*/
set last.next = dummy
set last = dummy
set dummy.next = 0
/*
* Update queue count
*/
set count = count + 1
/*
* Match unit angle with queue
*/
call SetUnitFacing(dummies[dummy], this - 1)
/*
* Apply stamp so that dummy isn't used until the stamp is expired
*/
set dummy.stamp = TimerGetElapsed(stampTimer) + RECYCLE_DELAY - .01
endmethod
static method pop takes thistype this, real x, real y, real facing returns integer
/*
* Retrieve queue and first dummy on queue given angle
*/
local thistype dummy = next
local thistype this2
local thistype node
local real stamp
/*
* If the queue is empty, return new dummy
*/
if (0 == dummy or dummy.stamp > TimerGetElapsed(stampTimer)) then
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = false
endif
/*
* Allocate new dummy
*/
debug if (dummyCount == 8191) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DUMMY RECYCLER FATAL ERROR: DUMMY OVERLOAD")
debug set Dummy.enabled = false
debug set this = 1/0
debug endif
set dummy = dummyCount + 1
set dummyCount = dummy
/*
* Create and initialize new unit handle
*/
set dummies[dummy] = CreateUnit(DUMMY_OWNER, DUMMY_ID, x, y, facing)
call SetUnitUserData(dummies[dummy], dummyCount)
call PauseUnit(dummies[dummy], true)
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = true
endif
return dummy
endif
/*
* Remove the dummy from the queue
*/
set next = dummy.next
if (0 == next) then
set last = this
endif
/*
* Only remove from the count if the queue has most dummies in it
*
* If queue doesn't have most dummies in it, take a dummy from the queue
* with most dummies in it and keep count the same
*/
if (count == ArrayStack.max) then
set count = count - 1
else
/*
* Retrieve the queue with most dummies in it as well as the
* first dummy in that queue
*/
set this2 = max
set node = this2.next
/*
* Remove first dummy from largest queue
*/
if (0 == node.next) then
set this2.last = this2
else
set this2.next = node.next
endif
set this2.count = this2.count - 1
/*
* Add first dummy to current queue
*/
set last.next = node
set last = node
set node.next = 0
/*
* Match unit angle with queue
*/
call SetUnitFacing(dummies[node], this - 1)
/*
* .73 seconds is how long it takes for a dummy to rotate 180 degrees
*
* Credits to Bribe for these 4 lines of code and the .73 value
*/
set stamp = TimerGetElapsed(stampTimer) + .73
if (stamp > node.stamp) then
set node.stamp = stamp
endif
endif
/*
* Move dummy to target position
*/
call SetUnitX(dummies[dummy], x)
call SetUnitY(dummies[dummy], y)
call SetUnitFacing(dummies[dummy], facing)
/*
* Return first dummy from current queue
*/
return dummy
endmethod
endstruct
struct Dummy extends array
debug static boolean enabled = false
debug private boolean allocated
/*
* Retrieve index given unit handle
*/
static method operator [] takes unit dummyUnit returns thistype
debug if (not enabled) then
debug return 1/0
debug endif
return GetUnitUserData(dummyUnit)
endmethod
/*
* Retrieve unit handle given index
*/
method operator unit takes nothing returns unit
debug if (not enabled) then
debug set this = 1/0
debug endif
return dummies[this]
endmethod
/*
* Slightly faster than ModuloInteger due to less args + constants
*/
private static method getClosestAngle takes integer angle returns integer
set angle = angle - angle/360*360
if (0 > angle) then
set angle = angle + 360
endif
return angle/ANGLE_SPACING*ANGLE_SPACING
endmethod
/*
* Returns either a new or a recycled dummy index
*/
static method create takes real x, real y, real facing returns Dummy
static if DEBUG_MODE then
local thistype this
if (not enabled) then
set x = 1/0
endif
set this = Queue.pop(getClosestAngle(R2I(facing)) + 1, x, y, facing)
debug set allocated = true
return this
else
return Queue.pop(getClosestAngle(R2I(facing)) + 1, x, y, facing)
endif
endmethod
/*
* Recycles dummy index
*/
method destroy takes nothing returns nothing
debug if (not enabled) then
debug set this = 1/0
debug endif
/*
* If the recycled dummy was invalid, issue critical error
*/
debug if (0 == GetUnitTypeId(unit) or 0 == GetWidgetLife(unit) or not allocated) then
debug if (not allocated) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "DUMMY RECYCLER FATAL ERROR: DOUBLE FREE")
debug elseif (0 == GetWidgetLife(unit)) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "DUMMY RECYCLER FATAL ERROR: KILLED A DUMMY")
debug elseif (null != unit) then
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "DUMMY RECYCLER FATAL ERROR: REMOVED A DUMMY")
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "DUMMY RECYCLER FATAL ERROR: ATTEMPTED TO RECYCLE NON DUMMY UNIT")
debug endif
debug set enabled = false
debug set this = 1/0
debug endif
debug set allocated = false
call Queue.add(this)
endmethod
endstruct
/*
* Initialization
*/
private function Initialize takes nothing returns nothing
local unit dummy
local integer last
local integer angle
local ArrayStack queue
local integer count
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = false
endif
/*
* This timer
*/
set stampTimer = CreateTimer()
call TimerStart(stampTimer, 604800, false, null)
/*
* The highest possible angle
*/
set last = 360/ANGLE_SPACING*ANGLE_SPACING
if (360 == last) then
set last = last - ANGLE_SPACING
if (last < ANGLE_SPACING) then
set last = 0
endif
endif
/*
* The lowest possible angle
*/
set angle = 0
/*
* Start dummy count at the last possible angle so that
* angles don't overlap with dummy indexes. This is done
* to simplify queue algorithm and improve overall performance.
* At most 360 possible dummy unit indexes will be lost due to this.
*/
set dummyCount = last + 1
/*
* Initialize ArrayStack
*/
set ArrayStack.min = PRELOAD_PROJECTILES_PER_ANGLE
set ArrayStack.max = PRELOAD_PROJECTILES_PER_ANGLE
set ArrayStack(PRELOAD_PROJECTILES_PER_ANGLE).first = 1
loop
/*
* queue pointer is angle + 1
*/
set queue = angle + 1
/*
* Only add projectiles to queue if MAX_PROJECTILES < 0
*/
if (0 < PRELOAD_PROJECTILES_PER_ANGLE) then
set count = PRELOAD_PROJECTILES_PER_ANGLE
set queue.count_p = PRELOAD_PROJECTILES_PER_ANGLE
set dummyCount = dummyCount + 1
set Queue(queue).next = dummyCount
/*
* Create and add all dummies to queue
*/
loop
/*
* Create and initialize unit handle
*/
set dummy = CreateUnit(DUMMY_OWNER, DUMMY_ID, 0, 0, angle)
set dummies[dummyCount] = dummy
call SetUnitUserData(dummy, dummyCount)
call PauseUnit(dummy, true)
set count = count - 1
exitwhen 0 == count
/*
* Point to next
*/
set dummyCount.next = dummyCount + 1
set dummyCount = dummyCount + 1
endloop
set Queue(queue).last = dummyCount
else
set Queue(queue).last = queue
endif
exitwhen last == angle
/*
* Go to next angle
*/
set angle = angle + ANGLE_SPACING
/*
* Link queues together
*/
set queue.next = angle + 1
set ArrayStack(angle + 1).prev = queue
/*
* Go to next queue
*/
set queue = angle + 1
endloop
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = true
endif
set dummy = null
debug set Dummy.enabled = true
endfunction
private module Init
private static method onInit takes nothing returns nothing
static if DEBUG_MODE then
call ExecuteFunc(SCOPE_PRIVATE + "Initialize")
if (not Dummy.enabled) then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"DUMMY RECYCLER FATAL ERROR: INITIALIZATION CRASHED, LOWER PRELOAD DUMMY COUNT")
endif
else
call Initialize()
endif
endmethod
endmodule
private struct Inits extends array
implement Init
endstruct
endlibrary
library MissileRecycler uses Dummy
function GetRecycledMissile takes real x, real y, real z, real facing returns unit
local Dummy dummy = Dummy.create(x, y, facing)
call SetUnitFlyHeight(dummy.unit, z, 0)
return dummy.unit
endfunction
function RecycleMissile takes unit u returns nothing
call Dummy[u].destroy()
endfunction
endlibrary
//TESH.scrollpos=0
//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
struct Testmap extends array
private static unit testhero1
private static unit testhero2
private static method onInit takes nothing returns nothing
local integer index = 0
local trigger t = CreateTrigger()
set testhero1 = CreateUnit(Player(0), 'Hblm', 0, 0, 270)
set testhero2 = CreateUnit(Player(0), 'Hmkg', 0, 256, 90)
call IssueImmediateOrderById(testhero2, OrderId("holdposition"))
call SetHeroLevel(testhero1, 10, false)
loop
exitwhen index == 72
call CreateUnit(Player(1), 'hpea', 512 * Cos(index * 5), 512 * Sin(index * 5), (index * 5) - 180)
set index = index + 1
endloop
set index = 0
loop
exitwhen index == 90
call CreateUnit(Player(1), 'hpea', 625 * Cos(index * 4), 625 * Sin(index * 4), (index * 4) - 180)
set index = index + 1
endloop
endmethod
endstruct