G_Timer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // leave this line at the top for all g_xxxx.cpp files...
  2. #include "g_headers.h"
  3. #include "G_Local.h"
  4. #include "../rufl/hstring.h"
  5. #define MAX_GTIMERS 16384
  6. typedef struct gtimer_s
  7. {
  8. hstring id; // Use handle strings, so that things work after loading
  9. int time;
  10. struct gtimer_s *next; // In either free list or current list
  11. } gtimer_t;
  12. gtimer_t g_timerPool[ MAX_GTIMERS ];
  13. gtimer_t *g_timers[ MAX_GENTITIES ];
  14. gtimer_t *g_timerFreeList;
  15. static int TIMER_GetCount(int num)
  16. {
  17. gtimer_t *p = g_timers[num];
  18. int count = 0;
  19. while (p)
  20. {
  21. count++;
  22. p = p->next;
  23. }
  24. return count;
  25. }
  26. /*
  27. -------------------------
  28. TIMER_RemoveHelper
  29. Scans an entities timer list to remove a given
  30. timer from the list and put it on the free list
  31. Doesn't do much error checking, only called below
  32. -------------------------
  33. */
  34. static void TIMER_RemoveHelper( int num, gtimer_t *timer )
  35. {
  36. gtimer_t *p = g_timers[num];
  37. // Special case: first timer in list
  38. if (p == timer)
  39. {
  40. g_timers[num] = g_timers[num]->next;
  41. p->next = g_timerFreeList;
  42. g_timerFreeList = p;
  43. return;
  44. }
  45. // Find the predecessor
  46. while (p->next != timer)
  47. {
  48. p = p->next;
  49. }
  50. // Rewire
  51. p->next = p->next->next;
  52. timer->next = g_timerFreeList;
  53. g_timerFreeList = timer;
  54. return;
  55. }
  56. /*
  57. -------------------------
  58. TIMER_Clear
  59. -------------------------
  60. */
  61. void TIMER_Clear( void )
  62. {
  63. int i;
  64. for (i = 0; i < MAX_GENTITIES; i++)
  65. {
  66. g_timers[i] = NULL;
  67. }
  68. for (i = 0; i < MAX_GTIMERS - 1; i++)
  69. {
  70. g_timerPool[i].next = &g_timerPool[i+1];
  71. }
  72. g_timerPool[MAX_GTIMERS-1].next = NULL;
  73. g_timerFreeList = &g_timerPool[0];
  74. }
  75. /*
  76. -------------------------
  77. TIMER_Clear
  78. -------------------------
  79. */
  80. void TIMER_Clear( int idx )
  81. {
  82. // rudimentary safety checks, might be other things to check?
  83. if ( idx >= 0 && idx < MAX_GENTITIES )
  84. {
  85. gtimer_t *p = g_timers[idx];
  86. // No timers at all -> do nothing
  87. if (!p)
  88. {
  89. return;
  90. }
  91. // Find the end of this ents timer list
  92. while (p->next)
  93. {
  94. p = p->next;
  95. }
  96. // Splice the lists
  97. p->next = g_timerFreeList;
  98. g_timerFreeList = g_timers[idx];
  99. g_timers[idx] = NULL;
  100. return;
  101. }
  102. }
  103. /*
  104. -------------------------
  105. TIMER_Save
  106. -------------------------
  107. */
  108. void TIMER_Save( void )
  109. {
  110. int j;
  111. gentity_t *ent;
  112. for ( j = 0, ent = &g_entities[0]; j < MAX_GENTITIES; j++, ent++ )
  113. {
  114. unsigned char numTimers = TIMER_GetCount(j);
  115. if ( !ent->inuse && numTimers)
  116. {
  117. // Com_Printf( "WARNING: ent with timers not inuse\n" );
  118. assert(numTimers);
  119. TIMER_Clear( j );
  120. numTimers = 0;
  121. }
  122. //Write out the timer information
  123. gi.AppendToSaveGame('TIME', (void *)&numTimers, sizeof(numTimers));
  124. gtimer_t *p = g_timers[j];
  125. assert ((numTimers && p) || (!numTimers && !p));
  126. while(p)
  127. {
  128. const char *timerID = p->id.c_str();
  129. const int length = strlen(timerID) + 1;
  130. const int time = p->time - level.time; //convert this back to delta so we can use SET after loading
  131. assert( length < 1024 );//This will cause problems when loading the timer if longer
  132. //Write out the id string
  133. gi.AppendToSaveGame('TMID', (void *) timerID, length);
  134. //Write out the timer data
  135. gi.AppendToSaveGame('TDTA', (void *) &time, sizeof( time ) );
  136. p = p->next;
  137. }
  138. }
  139. }
  140. /*
  141. -------------------------
  142. TIMER_Load
  143. -------------------------
  144. */
  145. void TIMER_Load( void )
  146. {
  147. int j;
  148. gentity_t *ent;
  149. for ( j = 0, ent = &g_entities[0]; j < MAX_GENTITIES; j++, ent++ )
  150. {
  151. unsigned char numTimers;
  152. gi.ReadFromSaveGame( 'TIME', (void *)&numTimers, sizeof(numTimers) );
  153. //Read back all entries
  154. for ( int i = 0; i < numTimers; i++ )
  155. {
  156. int time;
  157. char tempBuffer[1024]; // Still ugly. Setting ourselves up for 007 AUF all over again. =)
  158. assert (sizeof(g_timers[0]->time) == sizeof(time) );//make sure we're reading the same size as we wrote
  159. //Read the id string and time
  160. gi.ReadFromSaveGame( 'TMID', (char *) tempBuffer, 0 );
  161. gi.ReadFromSaveGame( 'TDTA', (void *) &time, sizeof( time ) );
  162. //this is odd, we saved all the timers in the autosave, but not all the ents are spawned yet from an auto load, so skip it
  163. if (ent->inuse)
  164. { //Restore it
  165. TIMER_Set(ent, tempBuffer, time);
  166. }
  167. }
  168. }
  169. }
  170. static gtimer_t *TIMER_GetNew(int num, const char *identifier)
  171. {
  172. assert(num < ENTITYNUM_MAX_NORMAL);//don't want timers on NONE or the WORLD
  173. gtimer_t *p = g_timers[num];
  174. // Search for an existing timer with this name
  175. while (p)
  176. {
  177. if (p->id == identifier)
  178. { // Found it
  179. return p;
  180. }
  181. p = p->next;
  182. }
  183. // No existing timer with this name was found, so grab one from the free list
  184. if (!g_timerFreeList)
  185. {//oh no, none free!
  186. assert(g_timerFreeList);
  187. return NULL;
  188. }
  189. p = g_timerFreeList;
  190. g_timerFreeList = g_timerFreeList->next;
  191. p->next = g_timers[num];
  192. g_timers[num] = p;
  193. return p;
  194. }
  195. gtimer_t *TIMER_GetExisting(int num, const char *identifier)
  196. {
  197. gtimer_t *p = g_timers[num];
  198. while (p)
  199. {
  200. if (p->id == identifier)
  201. { // Found it
  202. return p;
  203. }
  204. p = p->next;
  205. }
  206. return NULL;
  207. }
  208. /*
  209. -------------------------
  210. TIMER_Set
  211. -------------------------
  212. */
  213. void TIMER_Set( gentity_t *ent, const char *identifier, int duration )
  214. {
  215. assert(ent->inuse);
  216. gtimer_t *timer = TIMER_GetNew(ent->s.number, identifier);
  217. if (timer)
  218. {
  219. timer->id = identifier;
  220. timer->time = level.time + duration;
  221. }
  222. }
  223. /*
  224. -------------------------
  225. TIMER_Get
  226. -------------------------
  227. */
  228. int TIMER_Get( gentity_t *ent, const char *identifier )
  229. {
  230. gtimer_t *timer = TIMER_GetExisting(ent->s.number, identifier);
  231. if (!timer)
  232. {
  233. return -1;
  234. }
  235. return timer->time;
  236. }
  237. /*
  238. -------------------------
  239. TIMER_Done
  240. -------------------------
  241. */
  242. qboolean TIMER_Done( gentity_t *ent, const char *identifier )
  243. {
  244. gtimer_t *timer = TIMER_GetExisting(ent->s.number, identifier);
  245. if (!timer)
  246. {
  247. return qtrue;
  248. }
  249. return (timer->time < level.time);
  250. }
  251. /*
  252. -------------------------
  253. TIMER_Done2
  254. Returns false if timer has been
  255. started but is not done...or if
  256. timer was never started
  257. -------------------------
  258. */
  259. qboolean TIMER_Done2( gentity_t *ent, const char *identifier, qboolean remove )
  260. {
  261. gtimer_t *timer = TIMER_GetExisting(ent->s.number, identifier);
  262. qboolean res;
  263. if (!timer)
  264. {
  265. return qfalse;
  266. }
  267. res = (timer->time < level.time);
  268. if (res && remove)
  269. {
  270. // Put it back on the free list
  271. TIMER_RemoveHelper(ent->s.number, timer);
  272. }
  273. return res;
  274. }
  275. /*
  276. -------------------------
  277. TIMER_Exists
  278. -------------------------
  279. */
  280. qboolean TIMER_Exists( gentity_t *ent, const char *identifier )
  281. {
  282. return (qboolean)TIMER_GetExisting(ent->s.number, identifier);
  283. }
  284. /*
  285. -------------------------
  286. TIMER_Remove
  287. Utility to get rid of any timer
  288. -------------------------
  289. */
  290. void TIMER_Remove( gentity_t *ent, const char *identifier )
  291. {
  292. gtimer_t *timer = TIMER_GetExisting(ent->s.number, identifier);
  293. if (!timer)
  294. {
  295. return;
  296. }
  297. // Put it back on the free list
  298. TIMER_RemoveHelper(ent->s.number, timer);
  299. }
  300. /*
  301. -------------------------
  302. TIMER_Start
  303. -------------------------
  304. */
  305. qboolean TIMER_Start( gentity_t *self, const char *identifier, int duration )
  306. {
  307. if ( TIMER_Done( self, identifier ) )
  308. {
  309. TIMER_Set( self, identifier, duration );
  310. return qtrue;
  311. }
  312. return qfalse;
  313. }