Advertisement
JademusSreg

Action, a dedicated periodic function bus

May 10th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.58 KB | None | 0 0
  1. //================================================================
  2. // ACTION
  3. // a dedicated periodic function bus
  4. //
  5. // by JademusSreg
  6. //----------------------------------------------------------------
  7. //
  8. // This script doesn't do much by itself; it creates a dedicated
  9. // thread which periodically calls functions in a queued bus.
  10. //
  11. // API
  12. //
  13. //  Action_Queue function interface:
  14. //      bool IDENTIFIER_Action ();
  15. //
  16. //  int Action_QueueAdd (Action_Queue function)
  17. //      Add a function to the queue. Returns the index the new
  18. //      member occupies in the queue.
  19. //
  20. //  bool Action_QueueRemove (int index)
  21. //      Remove member at the specified queue index. Returns a
  22. //      bool representing the success of the operation.
  23. //
  24. //  int Action_QueueRemoveFunction (Action_Queue function, int max)
  25. //      Remove the specified function from the queue, to a specified
  26. //      limit. Returns a count of members removed from the queue.
  27. //
  28. //  int Action_QueueRemoveRange (int start, int end)
  29. //      Remove members from the queue within the inclusive start
  30. //      and end indexes specified. Returns a count of members
  31. //      removed from the queue.
  32. //
  33. //  int Action_QueueRemoveAll ()
  34. //      Remove all members from the queue. Returns a count of
  35. //      members removed from the queue.
  36. //
  37. //  int Action_QueueFreeIndex ()
  38. //      Returns the first free index in the queue.
  39. //
  40. //  int Action_QueueSize ()
  41. //      Returns the count of queue members.
  42. //
  43. //  bool Action_QueueIndexIsValid (int index)
  44. //      Returns true if the index is in range of 0 and the queue cap.
  45. //
  46. //================================================================
  47. // Data
  48.    
  49. const int ACTION_STACK_LIMIT = 2;
  50.    
  51. bool Action_Interface ();
  52. typedef funcref<Action_Interface> Action_Queue;
  53.    
  54. struct ActionData
  55. {
  56.     int  queueCap;
  57.     Action_Queue[ACTION_STACK_LIMIT] queueCall;
  58.     Action_Queue queueNull;
  59.    
  60.     int invalid;
  61.    
  62.     bool    threadStop;
  63.     bool    threadPause;
  64.     fixed   threadPeriod;
  65.     trigger threadTrigger;
  66. };
  67. ActionData Action;
  68.    
  69. //----------------------------------------------------------------
  70. // API
  71.    
  72. bool Action_QueueIndexIsValid (int i)
  73. {
  74.     return (i>=0 && i<=Action.queueCap);
  75. }
  76. bool Action_QueueRemove (int i)
  77. {
  78.     bool success;
  79.     if (!Action_QueueIndexIsValid(i)) {}
  80.     else if (Action.queueCall[i]!=Action.queueNull)
  81.     {
  82.         Action.queueCall[i]=Action.queueNull;
  83.         success=true;
  84.     }
  85.     return success;
  86. }
  87. int Action_QueueRemoveFunction (Action_Queue function, int max)
  88. {
  89.     int count;
  90.     int i;
  91.     if (function!=Action.queueNull)
  92.     {
  93.         for (i=0;i<=Action.queueCap;i+=1)
  94.         {
  95.             if (Action.queueCall[i]==function)
  96.             {
  97.                 Action_QueueRemove(i);
  98.                 count+=1;
  99.                 if (max>0 && count>=max) { break; }
  100.             }
  101.         }
  102.     }
  103.     return count;
  104. }
  105. int Action_QueueRemoveRange (int start, int end)
  106. {
  107.     int count;
  108.     int i;
  109.     for (i=start;i<=end;i+=1) { if (Action_QueueRemove(i)) { count+=1; } }
  110.     return count;
  111. }
  112. int Action_QueueRemoveAll ()
  113. {
  114.     Action.queueCap=-1;
  115.     return Action_QueueRemoveRange(0,ACTION_STACK_LIMIT-1);
  116. }
  117. int Action_QueueFreeIndex ()
  118. {
  119.     int free=Action.invalid;
  120.     int i;
  121.     for (i=0;i<ACTION_STACK_LIMIT && free<0;i+=1)
  122.         { if (Action.queueCall[i]==Action.queueNull) { free=i; } }
  123.     return free;
  124. }
  125. int Action_QueueSize ()
  126. {
  127.     int count;
  128.     int i;
  129.     for (i=0;i<ACTION_STACK_LIMIT;i+=1)
  130.         { if (Action.queueCall[i]!=Action.queueNull) { count+=1; } }
  131.     return count;
  132. }
  133. int Action_QueueAdd (Action_Queue function)
  134. {
  135.     int i=Action_QueueFreeIndex();
  136.     if (i!=Action.invalid)
  137.     {
  138.         if (i>Action.queueCap) { Action.queueCap+=1; }
  139.         Action.queueCall[i]=function;
  140.     }
  141.     return i;
  142. }
  143.    
  144. //----------------------------------------------------------------
  145. // Functions
  146.    
  147. void Action_CallQueue ()
  148. {
  149.     int i;
  150.     for (i=0;i<=Action.queueCap;i+=1)
  151.     {
  152.         if (Action.queueCall[i]!=Action.queueNull)
  153.         {
  154.             Action.queueCall[i]();
  155.         }
  156.     }
  157. }
  158. bool Action_Thread (bool testConds, bool runActions)
  159. {
  160.     while(!Action.threadStop)
  161.     {
  162.         if (!Action.threadPause)
  163.         {
  164.             Action_CallQueue();
  165.         }
  166.         Wait(Action.threadPeriod,c_timeGame);
  167.     }
  168.     return true;
  169. }
  170. void Action_Init ()
  171. {
  172.     Action.queueCap=-1;
  173.     Action.invalid=-1;
  174.     Action.threadPeriod=0.0625;
  175.     Action.threadTrigger=TriggerCreate("Action_Thread");
  176.     TriggerExecute(Action.threadTrigger,false,false);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement