/***********************************************
*
* FireCode
* v2.1.1.1
* By Magtheridon96
*
* - Executes Codes and Boolexprs
* - Allows the creation of both static and dynamic
* queues of code and boolexprs.
*
* 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 only safely use TriggerSleepAction within static
* CodeQueues. Dynamic CodeQueues and both static and dynamic
* ConditionQueues would crash.
* - Never dequeue a condition or action from a static queue
* inside queued code/boolexpr. This will crash the trigger.
* - The Jass API must only be used when in need of dynamic
* code/boolexpr executions.
*
* API:
* ----
*
* - struct CodeQueue extends array
* - static method create takes nothing returns thistype
* - Creates a static CodeQueue
* - method enqueue takes code c returns nothing
* - Enqueues a code to the CodeQueue
* - method dequeue takes triggeraction c returns nothing
* - Dequeues a code from the CodeQueue
* - method execute takes nothing returns nothing
* - Executes all the codes enqueued in the CodeQueue
* - method destroy takes nothing returns nothing
* - Destroys a CodeQueue
*
* - struct ConditionQueue extends array
* - static method create takes nothing returns thistype
* - Creates a static ConditionQueue
* - method enqueue takes boolexpr b returns nothing
* - Enqueues a boolexpr to the ConditionQueue
* - 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.
* - 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.
*
* Extra SpeedFreak API:
* ---------------------
*
* - //!textmacro FIRE_CODE takes code
* - Executes a code.
* - //!textmacro FIRE_CONDITION takes boolexpr
* - Fires a boolexpr.
*
* - //!textmacro NEW_CODE_QUEUE
* - Creates a very dynamic code queue that gets destroyed after firing it.
* - //!textmacro ENQUEUE_CODE takes code
* - Enqueues a code.
* - //!textmacro FIRE_CODE_QUEUE
* - Fires all enqueued codes.
*
* - //!textmacro NEW_CONDITION_QUEUE
* - Creates a very dynamic boolexpr queue that gets destroyed after firing it.
* - //!textmacro ENQUEUE_CONDITION takes boolexpr
* - Enqueues a boolexpr.
* - //!textmacro FIRE_CONDITION_QUEUE
* - Fires all enqueued boolexprs.
*
***********************************************/
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 CodeQueue extends array
private static integer stack = 1
private static trigger array triggers
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 code c returns nothing
call TriggerAddAction(triggers[this], c)
endmethod
method dequeue takes triggeraction c returns nothing
call TriggerRemoveAction(triggers[this], c)
endmethod
method execute takes nothing returns nothing
call TriggerExecute(triggers[this])
endmethod
method destroy takes nothing returns nothing
set stack = stack - 1
set triggers[this] = triggers[stack]
call TriggerClearActions(triggers[this])
endmethod
endstruct
struct ConditionQueue extends array
private static integer stack = 1
private static trigger array triggers
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 nothing
call TriggerAddCondition(triggers[this], b)
endmethod
method dequeue takes triggercondition b returns nothing
call TriggerRemoveCondition(triggers[this], b)
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
/* For Speedfreaks/Nestharus: */
//! textmacro FIRE_CODE takes code
call ForForce(bj_FORCE_PLAYER[0], $code$)
//! endtextmacro
//! textmacro NEW_CODE_QUEUE
set FireCode_ci = FireCode_ci + 1
if FireCode_ctQ[FireCode_ci] == null then
set FireCode_ctQ[FireCode_ci] = CreateTrigger()
endif
//! endtextmacro
//! textmacro ENQUEUE_CODE takes code
call TriggerAddCondition(FireCode_ctQ[FireCode_ci], Filter($code$))
//! endtextmacro
//! textmacro FIRE_CODE_QUEUE
call TriggerEvaluate(FireCode_ctQ[FireCode_ci])
call TriggerClearConditions(FireCode_ctQ[FireCode_ci])
set FireCode_ci = FireCode_ci - 1
//! endtextmacro
//! textmacro FIRE_CONDITION takes boolexpr
call TriggerClearConditions(FireCode_t)
call TriggerAddCondition(FireCode_t, $boolexpr$)
call TriggerEvaluate(FireCode_t)
//! endtextmacro
//! textmacro NEW_CONDITION_QUEUE
set FireCode_bi = FireCode_bi + 1
if FireCode_btQ[FireCode_bi] == null then
set FireCode_btQ[FireCode_bi] = CreateTrigger()
endif
//! endtextmacro
//! textmacro ENQUEUE_CONDITION takes boolexpr
call TriggerAddCondition(FireCode_btQ[FireCode_bi], $boolexpr$)
//! endtextmacro
//! textmacro FIRE_CONDITION_QUEUE
call TriggerEvaluate(FireCode_btQ[FireCode_bi])
call TriggerClearConditions(FireCode_btQ[FireCode_bi])
set FireCode_bi = FireCode_bi - 1
//! endtextmacro
endlibrary