-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventmanager.cpp
More file actions
548 lines (446 loc) · 14.2 KB
/
Copy patheventmanager.cpp
File metadata and controls
548 lines (446 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// CEventManager.cpp: implementation of the CEventManager class.
//
//////////////////////////////////////////////////////////////////////
#include "common.h"
#include "EventManager.h"
#include "Thing.h"
#include "DebugLog.h"
#include "ActiveReader.h"
#include "HLCollisionDetector.h"
#include "CollisionSeekingRound.h"
#include "Profile.h"
#include "thing.h"
#include "cliparams.h"
#include <stdio.h>
CEventManager EVENT_MANAGER ;
//#define GENERATE_DEBUG_REPORT
static float last_debug_time = 0.0f ;
//******************************************************************************************
CEventManager::CEventManager()
{
mOverflowEventListBuffer = NULL;
mValid = FALSE ;
}
//******************************************************************************************
CEventManager::~CEventManager()
{
ASSERT( mOverflowEventListBuffer == NULL );
Shutdown();
}
//******************************************************************************************
void CEventManager::Init()
{
mTime=0.0;
mCurrentBufferNum = 0 ;
mFrameCount = 0;
mNumEventsInEventManager = 0;
mEventsProcessedThisUpdate = 0;
mTotalEventProcessedNum = 0;
mReadyToFlushBuffer = 0 ;
mCurrentProcessOverflowEventNum = 0 ;
mOverflowEventListBuffer = new( MEMTYPE_EVENTMANAGER ) OPtrSet<CScheduledEvent> ;
LOG.AddMessage("Size of scheduled event = %d", sizeof(CScheduledEvent) ) ;
mEventsPool = new ( MEMTYPE_EVENTMANAGER )CScheduledEvent[MAX_NUM_EVENTS] ;
int i ;
for (i=0;i<MAX_NUM_EVENTS-1;i++)
{
mEventsPool[i].SetNextFreeSE(&mEventsPool[i+1]);
}
mEventsPool[MAX_NUM_EVENTS-1].SetNextFreeSE(NULL);
mEventFreeList = &mEventsPool[0] ;
last_debug_time = 0.0f;
mValid = TRUE;
LogEventManager() ;
}
//******************************************************************************************
void CEventManager::Shutdown()
{
if (mEventsPool)
{
char* cunt = ((char*)&(*mEventsPool))-0x44 ;
if (*cunt != 'C' )
{
int v = 2 ;
}
}
mValid = FALSE ;
// clean event list buffers
int i;
int j;
for (j=0 ; j < NUM_PRIORITY; j++)
{
for (i=0 ; i < NUM_EVENT_LIST_BUFFERS; i++)
{
mEventListBuffer[i][j].RemoveAll() ;
}
}
// clean overflow buffer
if (mOverflowEventListBuffer != NULL)
{
mOverflowEventListBuffer->reset();
delete mOverflowEventListBuffer ;
mOverflowEventListBuffer = NULL;
}
delete [] mEventsPool ;
mEventsPool= NULL ;
}
//******************************************************************************************
CScheduledEvent* CEventManager::GetNextFreeEvent()
{
CScheduledEvent* r =mEventFreeList ;
if (mEventFreeList)
{
mEventFreeList = mEventFreeList->GetNextFreeSE() ;
}
else
{
LOG.AddMessage("FATAL ERROR: Run out of free scheduled events!!");
}
return r;
}
//******************************************************************************************
void CEventManager::FreeEvent(CScheduledEvent* event)
{
event->SetNextFreeSE(mEventFreeList) ;
event->SetData(NULL);
event->SetToCall(NULL);
mEventFreeList = event ;
}
//******************************************************************************************
// Add an event to the event manager
// note event manager will have a readers point to the 'data' part in AddEvent
void CEventManager::AddEvent(const float& time_from_now, int event_num, CMonitor* to_call, const int start_or_end, CMonitor* data, CScheduledEvent* re_use_event)
{
AddEvent(event_num, to_call, time_from_now + mTime, start_or_end, data, re_use_event) ;
}
//******************************************************************************************
// Add an schuled event to the event manager. NOTE the time part means from time from now.
// Also note the event manager the takes ownership of the event
void CEventManager::AddEvent(CScheduledEvent* event)
{
if (event == NULL) return ;
AddEvent(event->GetEventNum(),
event->GetToCall(),
event->GetTime() + mTime,
START_OF_FRAME,
event->GetData(),
NULL) ;
FreeEvent(event);
}
//******************************************************************************************
// Add an event to the event manager
// note event manager will have a readers pointer of 'data' part in AddEvent
void CEventManager::AddEvent(const int event_num, CMonitor* to_call, const float& time, const int start_or_end, CMonitor* data, CScheduledEvent* re_use_event)
{
// PROFILE_START(AddEvent) ;
if (mValid == FALSE)
{
LOG.AddMessage("FATAL ERROR: Trying to add an event when event manager was invalid") ;
return ;
}
if (to_call == NULL) return ;
float next_time = time ;
int offset_buffer=0;
// ok quick case for events which are to be executed in the next frame
if (time <= (mTime+0.051f))
{
offset_buffer = mCurrentBufferNum ;
if (time < 0.0)
{
next_time = mTime +0.0001f;
}
else
{
next_time = time ;
}
}
else
{
// ignore event if to big
if (time > 1000000.0f)
{
//PROFILE_END(AddEvent) ;
return ;
}
// calculate witch offset buffer to add the event into
float delay = time - mTime - 0.001f ; //(ensures up to and including )
delay*=GAME_FR ;
delay=(float)floorf(delay) ;
offset_buffer = (int)delay ;
// if buffer is outside our range then add the event to the overflow buffer
if (offset_buffer >= (NUM_EVENT_LIST_BUFFERS -2) )
{
int size = mOverflowEventListBuffer->size() ;
int add_point = mCurrentProcessOverflowEventNum ;
// make sure we add the event is time order
while (add_point < size &&
mOverflowEventListBuffer->at(add_point)->GetTime() <= next_time)
{
add_point++;
}
CScheduledEvent* se = re_use_event ;
if (se == NULL)
{
se = GetNextFreeEvent() ;
if (se == NULL) return ;
se->Set(event_num, next_time, to_call, data);
}
else
{
se->SetTime(next_time) ;
se->SetNum(event_num);
se->SetReuse(TRUE) ;
if (data != NULL)
{
se->SetData(data) ;
}
}
mOverflowEventListBuffer->insert_at(add_point,se) ;
mNumEventsInEventManager++ ;
//PROFILE_END(AddEvent) ;
return ;
}
offset_buffer += mCurrentBufferNum ;
offset_buffer %= NUM_EVENT_LIST_BUFFERS ;
}
CScheduledEvent* se = re_use_event ;
if (se == NULL)
{
se = GetNextFreeEvent() ;
if (se == NULL) return ;
se->Set(event_num, next_time, to_call, data);
}
else
{
se->SetTime(next_time) ;
se->SetNum(event_num);
se->SetReuse(TRUE) ;
if (data != NULL)
{
se->SetData(data) ;
}
}
mEventListBuffer[offset_buffer][start_or_end].Append(se) ;
mNumEventsInEventManager++ ;
//PROFILE_END(AddEvent) ;
}
//******************************************************************************************
// this can be called instaed of 'AdvanceTime' and then 'Flush'
void CEventManager::Update()
{
AdvanceTime() ;
Flush() ;
}
//******************************************************************************************
// advance time to the next game code time
void CEventManager::AdvanceTime()
{
mFrameCount++ ;
mTime = mFrameCount * CLOCK_TICK ;
// advance the current event buffer so new events will be added from there on.
mReadyToFlushBuffer = mCurrentBufferNum ;
mCurrentBufferNum++ ;
mCurrentBufferNum%=NUM_EVENT_LIST_BUFFERS ;
}
//******************************************************************************************
// This function is called after advance time. It flushes out all the events in the current event
// buffer. It should also checks that if any events need to be executed in the overflow buffer
void CEventManager::Flush()
{
#ifdef GENERATE_DEBUG_REPORT
// do debug stuff
if ((mTime - last_debug_time) > 60.0f)
{
last_debug_time = mTime ;
LogEventManager() ;
}
#endif
ULONG previus_num_events_called = mTotalEventProcessedNum ;
int next_bufffer = mReadyToFlushBuffer;
mCurrentProcessOverflowEventNum = 0 ;
// execute all the events int the current buffer
CScheduledEvent * next_event;
int j = 0 ;
for (j=0 ; j< NUM_PRIORITY; j++)
{
int cunt = 0 ;
FOR_ALL_ITEMS_IN(mEventListBuffer[next_bufffer][j], next_event)
{
cunt++;
IListener* to_call = next_event->GetToCall() ;
next_event->SetReuse(FALSE);
if (to_call)
{
to_call->HandleEvent(next_event);
}
mTotalEventProcessedNum++;
}
}
// Check the overflow buffer incase any of the events need to be executed
int size = mOverflowEventListBuffer->size() ;
while (size > mCurrentProcessOverflowEventNum &&
mOverflowEventListBuffer->at(mCurrentProcessOverflowEventNum)->GetTime() < mTime )
{
CScheduledEvent* next_event = mOverflowEventListBuffer->at(mCurrentProcessOverflowEventNum) ;
IListener* to_call = next_event->GetToCall() ;
next_event->SetReuse(FALSE);
mCurrentProcessOverflowEventNum++;
if (to_call)
{
to_call->HandleEvent(next_event);
}
mTotalEventProcessedNum++;
}
// clean up
// delete events from the current buffer
for (j=0 ; j<NUM_PRIORITY;j++)
{
FOR_ALL_ITEMS_IN(mEventListBuffer[next_bufffer][j], next_event)
{
if (next_event->GetReuse() == FALSE)
{
FreeEvent(next_event) ;
}
mNumEventsInEventManager--;
}
mEventListBuffer[next_bufffer][j].RemoveAll() ;
}
// delete events from the overflow buffer
if (mCurrentProcessOverflowEventNum > 0)
{
for (int i=mCurrentProcessOverflowEventNum-1;i>=0;i--)
{
CScheduledEvent* ev = mOverflowEventListBuffer->at(i) ;
if (ev->GetReuse() == FALSE)
{
FreeEvent(mOverflowEventListBuffer->at(i));
}
mNumEventsInEventManager--;
}
mOverflowEventListBuffer->remove(0, mCurrentProcessOverflowEventNum-1);
}
mCurrentProcessOverflowEventNum = 0 ;
mEventsProcessedThisUpdate = mTotalEventProcessedNum - previus_num_events_called;
// ok sanity check to make sure overflow buffer has not be corrupted
if (CLIPARAMS.mDeveloperMode == TRUE)
{
int size1 = mOverflowEventListBuffer->size() ;
for (int i=0 ;i < size1-1 ; i++)
{
if (mOverflowEventListBuffer->at(i)->GetTime() > mOverflowEventListBuffer->at(i+1)->GetTime() )
{
LOG.AddMessage("FATAL ERROR: event manager overflow buffer corrupt!") ;
}
}
}
}
//******************************************************************************************
// DEBUG STUFF
#include "UnitAi.h"
#include "Unit.h"
//******************************************************************************************
void CEventManager::LogEvent(CScheduledEvent* event)
{
// need to switch RTTI on for this code to work
#ifdef GENERATE_DEBUG_REPORT
char temp[100] ;
IListener* to_call = event->GetToCall() ;
if (to_call == NULL)
{
sprintf(temp,"Event time: %2.2f, Object to call has died", event->GetTime() ) ;
LOG.AddMessage(temp);
return ;
}
while(to_call)
{
CThing* thing = dynamic_cast<CThing*>(to_call) ;
if (thing)
{
sprintf(temp,"Event time: %2.2f, to call %s, event no:%d", event->GetTime(), thing->_GetClassName(), event->GetEventNum() ) ;
LOG.AddMessage(temp);
break ;
}
CHLCollisionDetector* ct = dynamic_cast<CHLCollisionDetector*>(to_call) ;
if (ct)
{
BOOL do_special = FALSE ;
if (ct->GetCST()->GetThing()->IsA(THING_TYPE_UNIT))
{
CUnit* unit = (CUnit*)ct->GetCST()->GetThing() ;
if (unit->GetBehaviour())
{
CCSPersistentThing* cspt = (CCSPersistentThing*)((CScheduledEvent*)event)->GetData() ;
if ( cspt != NULL)
{
sprintf(temp,"Event time: %2.2f, HL collision for %s %08x %s with %s", event->GetTime(), unit->_GetClassName(), unit, unit->GetBehaviour()->mName, cspt->GetThing()->_GetClassName() ) ;
do_special = TRUE ;
}
else
{
sprintf(temp,"Event time: %2.2f, HL collision for %s %08x %s with NULL", event->GetTime(), unit->_GetClassName(), unit, unit->GetBehaviour()->mName ) ;
do_special = TRUE ;
}
}
}
if (do_special == FALSE)
{
sprintf(temp,"Event time: %2.2f, HL collision for %s %08x ", event->GetTime(), ct->GetCST()->GetThing()->_GetClassName(), ct->GetCST()->GetThing() ) ;
}
LOG.AddMessage(temp);
break ;
}
CUnitAI* ai = dynamic_cast<CUnitAI*>(to_call) ;
if (ai)
{
sprintf(temp,"Event time: %2.2f, Unit AI for %s event no:%d ", event->GetTime(), ai->GetForUnit()->_GetClassName(), event->GetEventNum() ) ;
LOG.AddMessage(temp);
break ;
}
sprintf(temp,"Event time: %2.2f, dunno ?? no = %d", event->GetTime(), event->GetEventNum()) ;
LOG.AddMessage(temp) ;
break ;
}
#endif
}
//******************************************************************************************
void CEventManager::LogEventManager()
{
#ifdef GENERATE_DEBUG_REPORT
// debug utility
char temp[100] ;
LOG.AddMessage("_________________________________________________________") ;
LOG.AddMessage("___________________ EVENT LIST STATUS ___________________\n") ;
sprintf(temp,"Time : %2.2f Seconds \n", mTime) ;
LOG.AddMessage(temp) ;
int i;
int j;
for (i=0 ; i < NUM_EVENT_LIST_BUFFERS; i++)
{
sprintf(temp,"______________________TIME + %2.2f______________________________", i*CLOCK_TICK) ;
for (j=0 ;j < NUM_PRIORITY;j++)
{
int buff = mCurrentBufferNum + i ;
buff %= NUM_EVENT_LIST_BUFFERS ;
LOG.AddMessage(temp) ;
CScheduledEvent* next_event;
FOR_ALL_ITEMS_IN(mEventListBuffer[buff][j], next_event)
{
LogEvent(next_event);
}
}
}
LOG.AddMessage("_____________________ NORMAL BUFFER ______________________________") ;
int size = mOverflowEventListBuffer->size() ;
for (i=0 ; i<size ;i++)
{
CScheduledEvent* next_event = mOverflowEventListBuffer->at(i) ;
LogEvent(next_event);
}
LOG.AddMessage("\n_________________________________________________________\n") ;
sprintf(temp,"HL Collision checks per sec = %2.2f", CHLCollisionDetector::GetChecksDone() / mTime) ;
LOG.AddMessage(temp) ;
sprintf(temp,"LL Round Collision checks per sec = %2.2f", CCollisionSeekingRound::GetChecksDone() / mTime) ;
LOG.AddMessage(temp) ;
LOG.AddMessage("\n_________________________________________________________\n") ;
#endif
}