Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //================================================================
- // ACTION
- // a dedicated periodic function bus
- //
- // by JademusSreg
- //----------------------------------------------------------------
- //
- // This script doesn't do much by itself; it creates a dedicated
- // thread which periodically calls functions in a queued bus.
- //
- // API
- //
- // Action_Queue function interface:
- // bool IDENTIFIER_Action ();
- //
- // int Action_QueueAdd (Action_Queue function)
- // Add a function to the queue. Returns the index the new
- // member occupies in the queue.
- //
- // bool Action_QueueRemove (int index)
- // Remove member at the specified queue index. Returns a
- // bool representing the success of the operation.
- //
- // int Action_QueueRemoveFunction (Action_Queue function, int max)
- // Remove the specified function from the queue, to a specified
- // limit. Returns a count of members removed from the queue.
- //
- // int Action_QueueRemoveRange (int start, int end)
- // Remove members from the queue within the inclusive start
- // and end indexes specified. Returns a count of members
- // removed from the queue.
- //
- // int Action_QueueRemoveAll ()
- // Remove all members from the queue. Returns a count of
- // members removed from the queue.
- //
- // int Action_QueueFreeIndex ()
- // Returns the first free index in the queue.
- //
- // int Action_QueueSize ()
- // Returns the count of queue members.
- //
- // bool Action_QueueIndexIsValid (int index)
- // Returns true if the index is in range of 0 and the queue cap.
- //
- //================================================================
- // Data
- const int ACTION_STACK_LIMIT = 2;
- bool Action_Interface ();
- typedef funcref<Action_Interface> Action_Queue;
- struct ActionData
- {
- int queueCap;
- Action_Queue[ACTION_STACK_LIMIT] queueCall;
- Action_Queue queueNull;
- int invalid;
- bool threadStop;
- bool threadPause;
- fixed threadPeriod;
- trigger threadTrigger;
- };
- ActionData Action;
- //----------------------------------------------------------------
- // API
- bool Action_QueueIndexIsValid (int i)
- {
- return (i>=0 && i<=Action.queueCap);
- }
- bool Action_QueueRemove (int i)
- {
- bool success;
- if (!Action_QueueIndexIsValid(i)) {}
- else if (Action.queueCall[i]!=Action.queueNull)
- {
- Action.queueCall[i]=Action.queueNull;
- success=true;
- }
- return success;
- }
- int Action_QueueRemoveFunction (Action_Queue function, int max)
- {
- int count;
- int i;
- if (function!=Action.queueNull)
- {
- for (i=0;i<=Action.queueCap;i+=1)
- {
- if (Action.queueCall[i]==function)
- {
- Action_QueueRemove(i);
- count+=1;
- if (max>0 && count>=max) { break; }
- }
- }
- }
- return count;
- }
- int Action_QueueRemoveRange (int start, int end)
- {
- int count;
- int i;
- for (i=start;i<=end;i+=1) { if (Action_QueueRemove(i)) { count+=1; } }
- return count;
- }
- int Action_QueueRemoveAll ()
- {
- Action.queueCap=-1;
- return Action_QueueRemoveRange(0,ACTION_STACK_LIMIT-1);
- }
- int Action_QueueFreeIndex ()
- {
- int free=Action.invalid;
- int i;
- for (i=0;i<ACTION_STACK_LIMIT && free<0;i+=1)
- { if (Action.queueCall[i]==Action.queueNull) { free=i; } }
- return free;
- }
- int Action_QueueSize ()
- {
- int count;
- int i;
- for (i=0;i<ACTION_STACK_LIMIT;i+=1)
- { if (Action.queueCall[i]!=Action.queueNull) { count+=1; } }
- return count;
- }
- int Action_QueueAdd (Action_Queue function)
- {
- int i=Action_QueueFreeIndex();
- if (i!=Action.invalid)
- {
- if (i>Action.queueCap) { Action.queueCap+=1; }
- Action.queueCall[i]=function;
- }
- return i;
- }
- //----------------------------------------------------------------
- // Functions
- void Action_CallQueue ()
- {
- int i;
- for (i=0;i<=Action.queueCap;i+=1)
- {
- if (Action.queueCall[i]!=Action.queueNull)
- {
- Action.queueCall[i]();
- }
- }
- }
- bool Action_Thread (bool testConds, bool runActions)
- {
- while(!Action.threadStop)
- {
- if (!Action.threadPause)
- {
- Action_CallQueue();
- }
- Wait(Action.threadPeriod,c_timeGame);
- }
- return true;
- }
- void Action_Init ()
- {
- Action.queueCap=-1;
- Action.invalid=-1;
- Action.threadPeriod=0.0625;
- Action.threadTrigger=TriggerCreate("Action_Thread");
- TriggerExecute(Action.threadTrigger,false,false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement