- Joined
- Dec 12, 2008
- Messages
- 7,385
Instead of using your own forces and triggers to fire code and boolexprs, you can use this library.
It doesn't even affect efficiency! You can queue codes/boolexprs instead of firing them if you're going
to need to run a lot of code. That's only one Trigger Evaluation per X amount of code where X is the op-limit.
Also, instead of making your resource responsible for the way it executes/evaluates code/boolexprs, you can blame it
on this one. Pretty sweet, ey?
Feel free to comment.
It doesn't even affect efficiency! You can queue codes/boolexprs instead of firing them if you're going
to need to run a lot of code. That's only one Trigger Evaluation per X amount of code where X is the op-limit.
Also, instead of making your resource responsible for the way it executes/evaluates code/boolexprs, you can blame it
on this one. Pretty sweet, ey?
JASS:
/***********************************************
*
* FireCode
* v4.0.0.0
* By Magtheridon96
*
* - Executes Codes and Boolexprs
* - Allows the creation of both static and dynamic
* queues of code and boolexprs.
* - Allows all the standardized ways of executing
* codes and boolexprs to be the responsibilty of
* one resource and not a thousand.
*
* Notes:
* ------
*
* - You must never use EnqueueCode/EnqueueCondition without
* calling NewCodeQueue/NewConditionQueue.
* - Firing a Queue destroys it.
* - Using the Struct API allows you to create static queues.
* - You can never use TriggerSleepAction except while using FireCode.
* - The Jass API must only be used when in need of dynamic
* code/boolexpr executions.
*
* API:
* ----
*
* - struct ConditionQueue extends array
* - static method create takes nothing returns thistype
* - Creates a static ConditionQueue
* - method enqueue takes boolexpr b returns triggercondition
* - Enqueues a boolexpr to the ConditionQueue and returns it.
* - method dequeue takes triggercondition b returns nothing
* - Dequeues a boolexpr from the ConditionQueue
* - method evaluate takes nothing returns boolean
* - Executes all the boolexprs enqueued in the ConditionQueue
* - method destroy takes nothing returns nothing
* - Destroys a ConditionQueue
*
* - function FireCode takes code c returns nothing
* - Executes a code.
* - function NewCodeQueue takes nothing returns nothing
* - Creates a very dynamic code queue that gets destroyed after firing it.
* - function EnqueueCode takes code c returns nothing
* - Enqueues a code.
* - function FireCodeQueue takes nothing returns nothing
* - Fires all enqueued codes and destroys the dynamic queue.
* - function FireCondition takes boolexpr b returns boolean
* - Fires a boolexpr.
* - function NewConditionQueue takes nothing returns nothing
* - Creates a very dynamic boolexpr queue that gets destroyed after firing it.
* - function EnqueueCondition takes boolexpr b returns nothing
* - Enqueues a boolexpr.
* - function FireConditionQueue takes nothing returns boolean
* - Fires all enqueued boolexprs and destroys the dynamic queue.
*
***********************************************/
library FireCode
globals
public trigger array btQ
public trigger array ctQ
public integer bi = 0
public integer ci = 0
public trigger t = CreateTrigger()
endglobals
struct ConditionQueue extends array
private static integer stack = 1
private static trigger array triggers
// Removal stack
private static timer stackClearer = CreateTimer()
private static triggercondition array conditionStack
private static trigger array triggerStack
private static integer stackIndex = 0
static method create takes nothing returns thistype
local thistype this = stack
set stack = stack + 1
if triggers[this] == null then
set triggers[this] = CreateTrigger()
endif
return this
endmethod
method enqueue takes boolexpr b returns triggercondition
return TriggerAddCondition(triggers[this], b)
endmethod
private static method remove takes nothing returns nothing
loop
exitwhen stackIndex == 0
call TriggerRemoveCondition(triggerStack[stackIndex], conditionStack[stackIndex])
set triggerStack[stackIndex] = null
set conditionStack[stackIndex] = null
set stackIndex = stackIndex - 1
endloop
endmethod
method dequeue takes triggercondition b returns nothing
set stackIndex = stackIndex + 1
set conditionStack[stackIndex] = b
set triggerStack[stackIndex] = triggers[this]
if stackIndex == 1 then
call TimerStart(stackClearer, 0, false, function thistype.remove)
endif
endmethod
method evaluate takes nothing returns boolean
return TriggerEvaluate(triggers[this])
endmethod
method destroy takes nothing returns nothing
set stack = stack - 1
set triggers[this] = triggers[stack]
call TriggerClearConditions(triggers[this])
endmethod
endstruct
function FireCode takes code c returns nothing
call ForForce(bj_FORCE_PLAYER[0], c)
endfunction
function NewCodeQueue takes nothing returns nothing
set ci = ci + 1
if ctQ[ci] == null then
set ctQ[ci] = CreateTrigger()
endif
endfunction
function EnqueueCode takes code c returns nothing
call TriggerAddCondition(ctQ[ci], Filter(c))
endfunction
function FireCodeQueue takes nothing returns nothing
call TriggerEvaluate(ctQ[ci])
call TriggerClearConditions(ctQ[ci])
set ci = ci - 1
endfunction
function FireCondition takes boolexpr b returns nothing
// Do not change the order of these.
// That would kill recursion-safety.
call TriggerClearConditions(t)
call TriggerAddCondition(t, b)
call TriggerEvaluate(t)
endfunction
function NewConditionQueue takes nothing returns nothing
set bi = bi + 1
if btQ[bi] == null then
set btQ[bi] = CreateTrigger()
endif
endfunction
function EnqueueCondition takes boolexpr b returns nothing
call TriggerAddCondition(btQ[bi], b)
endfunction
function FireConditionQueue takes nothing returns nothing
call TriggerEvaluate(btQ[bi])
call TriggerClearConditions(btQ[bi])
set bi = bi - 1
endfunction
endlibrary
Feel free to comment.
Last edited: