Class.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. /*
  21. Base class for all C++ objects. Provides fast run-time type checking and run-time
  22. instancing of objects.
  23. */
  24. #pragma hdrstop
  25. #include "../../idlib/precompiled.h"
  26. #include "../Game_local.h"
  27. #include "TypeInfo.h"
  28. /***********************************************************************
  29. idTypeInfo
  30. ***********************************************************************/
  31. // this is the head of a singly linked list of all the idTypes
  32. static idTypeInfo *typelist = NULL;
  33. static idHierarchy<idTypeInfo> classHierarchy;
  34. static int eventCallbackMemory = 0;
  35. /*
  36. ================
  37. idTypeInfo::idClassType()
  38. Constructor for class. Should only be called from CLASS_DECLARATION macro.
  39. Handles linking class definition into class hierarchy. This should only happen
  40. at startup as idTypeInfos are statically defined. Since static variables can be
  41. initialized in any order, the constructor must handle the case that subclasses
  42. are initialized before superclasses.
  43. ================
  44. */
  45. idTypeInfo::idTypeInfo( const char *classname, const char *superclass, idEventFunc<idClass> *eventCallbacks, idClass *( *CreateInstance )(),
  46. void ( idClass::*Spawn )(), void ( idClass::*Save )( idSaveGame *savefile ) const, void ( idClass::*Restore )( idRestoreGame *savefile ) ) {
  47. idTypeInfo *type;
  48. idTypeInfo **insert;
  49. this->classname = classname;
  50. this->superclass = superclass;
  51. this->eventCallbacks = eventCallbacks;
  52. this->eventMap = NULL;
  53. this->Spawn = Spawn;
  54. this->Save = Save;
  55. this->Restore = Restore;
  56. this->CreateInstance = CreateInstance;
  57. this->super = idClass::GetClass( superclass );
  58. this->freeEventMap = false;
  59. typeNum = 0;
  60. lastChild = 0;
  61. // Check if any subclasses were initialized before their superclass
  62. for( type = typelist; type != NULL; type = type->next ) {
  63. if ( ( type->super == NULL ) && !idStr::Cmp( type->superclass, this->classname ) &&
  64. idStr::Cmp( type->classname, "idClass" ) ) {
  65. type->super = this;
  66. }
  67. }
  68. // Insert sorted
  69. for ( insert = &typelist; *insert; insert = &(*insert)->next ) {
  70. assert( idStr::Cmp( classname, (*insert)->classname ) );
  71. if ( idStr::Cmp( classname, (*insert)->classname ) < 0 ) {
  72. next = *insert;
  73. *insert = this;
  74. break;
  75. }
  76. }
  77. if ( !*insert ) {
  78. *insert = this;
  79. next = NULL;
  80. }
  81. }
  82. /*
  83. ================
  84. idTypeInfo::~idTypeInfo
  85. ================
  86. */
  87. idTypeInfo::~idTypeInfo() {
  88. Shutdown();
  89. }
  90. /*
  91. ================
  92. idTypeInfo::Init
  93. Initializes the event callback table for the class. Creates a
  94. table for fast lookups of event functions. Should only be called once.
  95. ================
  96. */
  97. void idTypeInfo::Init() {
  98. idTypeInfo *c;
  99. idEventFunc<idClass> *def;
  100. int ev;
  101. int i;
  102. bool *set;
  103. int num;
  104. if ( eventMap ) {
  105. // we've already been initialized by a subclass
  106. return;
  107. }
  108. // make sure our superclass is initialized first
  109. if ( super && !super->eventMap ) {
  110. super->Init();
  111. }
  112. // add to our node hierarchy
  113. if ( super ) {
  114. node.ParentTo( super->node );
  115. } else {
  116. node.ParentTo( classHierarchy );
  117. }
  118. node.SetOwner( this );
  119. // keep track of the number of children below each class
  120. for( c = super; c != NULL; c = c->super ) {
  121. c->lastChild++;
  122. }
  123. // if we're not adding any new event callbacks, we can just use our superclass's table
  124. if ( ( !eventCallbacks || !eventCallbacks->event ) && super ) {
  125. eventMap = super->eventMap;
  126. return;
  127. }
  128. // set a flag so we know to delete the eventMap table
  129. freeEventMap = true;
  130. // Allocate our new table. It has to have as many entries as there
  131. // are events. NOTE: could save some space by keeping track of the maximum
  132. // event that the class responds to and doing range checking.
  133. num = idEventDef::NumEventCommands();
  134. eventMap = new (TAG_SYSTEM) eventCallback_t[ num ];
  135. memset( eventMap, 0, sizeof( eventCallback_t ) * num );
  136. eventCallbackMemory += sizeof( eventCallback_t ) * num;
  137. // allocate temporary memory for flags so that the subclass's event callbacks
  138. // override the superclass's event callback
  139. set = new (TAG_SYSTEM) bool[ num ];
  140. memset( set, 0, sizeof( bool ) * num );
  141. // go through the inheritence order and copies the event callback function into
  142. // a list indexed by the event number. This allows fast lookups of
  143. // event functions.
  144. for( c = this; c != NULL; c = c->super ) {
  145. def = c->eventCallbacks;
  146. if ( !def ) {
  147. continue;
  148. }
  149. // go through each entry until we hit the NULL terminator
  150. for( i = 0; def[ i ].event != NULL; i++ ) {
  151. ev = def[ i ].event->GetEventNum();
  152. if ( set[ ev ] ) {
  153. continue;
  154. }
  155. set[ ev ] = true;
  156. eventMap[ ev ] = def[ i ].function;
  157. }
  158. }
  159. delete[] set;
  160. }
  161. /*
  162. ================
  163. idTypeInfo::Shutdown
  164. Should only be called when DLL or EXE is being shutdown.
  165. Although it cleans up any allocated memory, it doesn't bother to remove itself
  166. from the class list since the program is shutting down.
  167. ================
  168. */
  169. void idTypeInfo::Shutdown() {
  170. // free up the memory used for event lookups
  171. if ( eventMap ) {
  172. if ( freeEventMap ) {
  173. delete[] eventMap;
  174. }
  175. eventMap = NULL;
  176. }
  177. typeNum = 0;
  178. lastChild = 0;
  179. }
  180. /***********************************************************************
  181. idClass
  182. ***********************************************************************/
  183. const idEventDef EV_Remove( "<immediateremove>", NULL );
  184. const idEventDef EV_SafeRemove( "remove", NULL );
  185. ABSTRACT_DECLARATION( NULL, idClass )
  186. EVENT( EV_Remove, idClass::Event_Remove )
  187. EVENT( EV_SafeRemove, idClass::Event_SafeRemove )
  188. END_CLASS
  189. // alphabetical order
  190. idList<idTypeInfo *, TAG_IDCLASS> idClass::types;
  191. // typenum order
  192. idList<idTypeInfo *, TAG_IDCLASS> idClass::typenums;
  193. bool idClass::initialized = false;
  194. int idClass::typeNumBits = 0;
  195. int idClass::memused = 0;
  196. int idClass::numobjects = 0;
  197. /*
  198. ================
  199. idClass::CallSpawn
  200. ================
  201. */
  202. void idClass::CallSpawn() {
  203. idTypeInfo *type;
  204. type = GetType();
  205. CallSpawnFunc( type );
  206. }
  207. /*
  208. ================
  209. idClass::CallSpawnFunc
  210. ================
  211. */
  212. classSpawnFunc_t idClass::CallSpawnFunc( idTypeInfo *cls ) {
  213. classSpawnFunc_t func;
  214. if ( cls->super ) {
  215. func = CallSpawnFunc( cls->super );
  216. if ( func == cls->Spawn ) {
  217. // don't call the same function twice in a row.
  218. // this can happen when subclasses don't have their own spawn function.
  219. return func;
  220. }
  221. }
  222. ( this->*cls->Spawn )();
  223. return cls->Spawn;
  224. }
  225. /*
  226. ================
  227. idClass::FindUninitializedMemory
  228. ================
  229. */
  230. void idClass::FindUninitializedMemory() {
  231. #ifdef ID_DEBUG_UNINITIALIZED_MEMORY
  232. unsigned long *ptr = ( ( unsigned long * )this ) - 1;
  233. int size = *ptr;
  234. assert( ( size & 3 ) == 0 );
  235. size >>= 2;
  236. for ( int i = 0; i < size; i++ ) {
  237. if ( ptr[i] == 0xcdcdcdcd ) {
  238. const char *varName = GetTypeVariableName( GetClassname(), i << 2 );
  239. gameLocal.Warning( "type '%s' has uninitialized variable %s (offset %d)", GetClassname(), varName, i << 2 );
  240. }
  241. }
  242. #endif
  243. }
  244. /*
  245. ================
  246. idClass::Spawn
  247. ================
  248. */
  249. void idClass::Spawn() {
  250. }
  251. /*
  252. ================
  253. idClass::~idClass
  254. Destructor for object. Cancels any events that depend on this object.
  255. ================
  256. */
  257. idClass::~idClass() {
  258. idEvent::CancelEvents( this );
  259. }
  260. /*
  261. ================
  262. idClass::DisplayInfo_f
  263. ================
  264. */
  265. void idClass::DisplayInfo_f( const idCmdArgs &args ) {
  266. gameLocal.Printf( "Class memory status: %i bytes allocated in %i objects\n", memused, numobjects );
  267. }
  268. /*
  269. ================
  270. idClass::ListClasses_f
  271. ================
  272. */
  273. void idClass::ListClasses_f( const idCmdArgs &args ) {
  274. int i;
  275. idTypeInfo *type;
  276. gameLocal.Printf( "%-24s %-24s %-6s %-6s\n", "Classname", "Superclass", "Type", "Subclasses" );
  277. gameLocal.Printf( "----------------------------------------------------------------------\n" );
  278. for( i = 0; i < types.Num(); i++ ) {
  279. type = types[ i ];
  280. gameLocal.Printf( "%-24s %-24s %6d %6d\n", type->classname, type->superclass, type->typeNum, type->lastChild - type->typeNum );
  281. }
  282. gameLocal.Printf( "...%d classes", types.Num() );
  283. }
  284. /*
  285. ================
  286. idClass::CreateInstance
  287. ================
  288. */
  289. idClass *idClass::CreateInstance( const char *name ) {
  290. const idTypeInfo *type;
  291. idClass *obj;
  292. type = idClass::GetClass( name );
  293. if ( !type ) {
  294. return NULL;
  295. }
  296. obj = type->CreateInstance();
  297. return obj;
  298. }
  299. /*
  300. ================
  301. idClass::Init
  302. Should be called after all idTypeInfos are initialized, so must be called
  303. manually upon game code initialization. Tells all the idTypeInfos to initialize
  304. their event callback table for the associated class. This should only be called
  305. once during the execution of the program or DLL.
  306. ================
  307. */
  308. void idClass::Init() {
  309. idTypeInfo *c;
  310. int num;
  311. gameLocal.Printf( "Initializing class hierarchy\n" );
  312. if ( initialized ) {
  313. gameLocal.Printf( "...already initialized\n" );
  314. return;
  315. }
  316. // init the event callback tables for all the classes
  317. for( c = typelist; c != NULL; c = c->next ) {
  318. c->Init();
  319. }
  320. // number the types according to the class hierarchy so we can quickly determine if a class
  321. // is a subclass of another
  322. num = 0;
  323. for( c = classHierarchy.GetNext(); c != NULL; c = c->node.GetNext(), num++ ) {
  324. c->typeNum = num;
  325. c->lastChild += num;
  326. }
  327. // number of bits needed to send types over network
  328. typeNumBits = idMath::BitsForInteger( num );
  329. // create a list of the types so we can do quick lookups
  330. // one list in alphabetical order, one in typenum order
  331. types.SetGranularity( 1 );
  332. types.SetNum( num );
  333. typenums.SetGranularity( 1 );
  334. typenums.SetNum( num );
  335. num = 0;
  336. for( c = typelist; c != NULL; c = c->next, num++ ) {
  337. types[ num ] = c;
  338. typenums[ c->typeNum ] = c;
  339. }
  340. initialized = true;
  341. gameLocal.Printf( "...%i classes, %i bytes for event callbacks\n", types.Num(), eventCallbackMemory );
  342. }
  343. /*
  344. ================
  345. idClass::Shutdown
  346. ================
  347. */
  348. void idClass::Shutdown() {
  349. idTypeInfo *c;
  350. for( c = typelist; c != NULL; c = c->next ) {
  351. c->Shutdown();
  352. }
  353. types.Clear();
  354. typenums.Clear();
  355. initialized = false;
  356. }
  357. /*
  358. ================
  359. idClass::new
  360. ================
  361. */
  362. void * idClass::operator new( size_t s ) {
  363. int *p;
  364. s += sizeof( int );
  365. p = (int *)Mem_Alloc( s, TAG_IDCLASS );
  366. *p = s;
  367. memused += s;
  368. numobjects++;
  369. return p + 1;
  370. }
  371. /*
  372. ================
  373. idClass::delete
  374. ================
  375. */
  376. void idClass::operator delete( void *ptr ) {
  377. int *p;
  378. if ( ptr ) {
  379. p = ( ( int * )ptr ) - 1;
  380. memused -= *p;
  381. numobjects--;
  382. Mem_Free( p );
  383. }
  384. }
  385. /*
  386. ================
  387. idClass::GetClass
  388. Returns the idTypeInfo for the name of the class passed in. This is a static function
  389. so it must be called as idClass::GetClass( classname )
  390. ================
  391. */
  392. idTypeInfo *idClass::GetClass( const char *name ) {
  393. idTypeInfo *c;
  394. int order;
  395. int mid;
  396. int min;
  397. int max;
  398. if ( !initialized ) {
  399. // idClass::Init hasn't been called yet, so do a slow lookup
  400. for( c = typelist; c != NULL; c = c->next ) {
  401. if ( !idStr::Cmp( c->classname, name ) ) {
  402. return c;
  403. }
  404. }
  405. } else {
  406. // do a binary search through the list of types
  407. min = 0;
  408. max = types.Num() - 1;
  409. while( min <= max ) {
  410. mid = ( min + max ) / 2;
  411. c = types[ mid ];
  412. order = idStr::Cmp( c->classname, name );
  413. if ( !order ) {
  414. return c;
  415. } else if ( order > 0 ) {
  416. max = mid - 1;
  417. } else {
  418. min = mid + 1;
  419. }
  420. }
  421. }
  422. return NULL;
  423. }
  424. /*
  425. ================
  426. idClass::GetType
  427. ================
  428. */
  429. idTypeInfo *idClass::GetType( const int typeNum ) {
  430. idTypeInfo *c;
  431. if ( !initialized ) {
  432. for( c = typelist; c != NULL; c = c->next ) {
  433. if ( c->typeNum == typeNum ) {
  434. return c;
  435. }
  436. }
  437. } else if ( ( typeNum >= 0 ) && ( typeNum < types.Num() ) ) {
  438. return typenums[ typeNum ];
  439. }
  440. return NULL;
  441. }
  442. /*
  443. ================
  444. idClass::GetClassname
  445. Returns the text classname of the object.
  446. ================
  447. */
  448. const char *idClass::GetClassname() const {
  449. idTypeInfo *type;
  450. type = GetType();
  451. return type->classname;
  452. }
  453. /*
  454. ================
  455. idClass::GetSuperclass
  456. Returns the text classname of the superclass.
  457. ================
  458. */
  459. const char *idClass::GetSuperclass() const {
  460. idTypeInfo *cls;
  461. cls = GetType();
  462. return cls->superclass;
  463. }
  464. /*
  465. ================
  466. idClass::CancelEvents
  467. ================
  468. */
  469. void idClass::CancelEvents( const idEventDef *ev ) {
  470. idEvent::CancelEvents( this, ev );
  471. }
  472. /*
  473. ================
  474. idClass::PostEventArgs
  475. ================
  476. */
  477. bool idClass::PostEventArgs( const idEventDef *ev, int time, int numargs, ... ) {
  478. idTypeInfo *c;
  479. idEvent *event;
  480. va_list args;
  481. assert( ev );
  482. if ( !idEvent::initialized ) {
  483. return false;
  484. }
  485. c = GetType();
  486. if ( !c->eventMap[ ev->GetEventNum() ] ) {
  487. // we don't respond to this event, so ignore it
  488. return false;
  489. }
  490. bool isReplicated = true;
  491. // If this is an entity with skipReplication, we want to process the event normally even on clients.
  492. if ( IsType( idEntity::Type ) ) {
  493. idEntity * thisEnt = static_cast< idEntity * >( this );
  494. if ( thisEnt->fl.skipReplication ) {
  495. isReplicated = false;
  496. }
  497. }
  498. // we service events on the client to avoid any bad code filling up the event pool
  499. // we don't want them processed usually, unless when the map is (re)loading.
  500. // we allow threads to run fine, though.
  501. if ( common->IsClient() && isReplicated && ( gameLocal.GameState() != GAMESTATE_STARTUP ) && !IsType( idThread::Type ) ) {
  502. return true;
  503. }
  504. va_start( args, numargs );
  505. event = idEvent::Alloc( ev, numargs, args );
  506. va_end( args );
  507. event->Schedule( this, c, time );
  508. return true;
  509. }
  510. /*
  511. ================
  512. idClass::PostEventMS
  513. ================
  514. */
  515. bool idClass::PostEventMS( const idEventDef *ev, int time ) {
  516. return PostEventArgs( ev, time, 0 );
  517. }
  518. /*
  519. ================
  520. idClass::PostEventMS
  521. ================
  522. */
  523. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1 ) {
  524. return PostEventArgs( ev, time, 1, &arg1 );
  525. }
  526. /*
  527. ================
  528. idClass::PostEventMS
  529. ================
  530. */
  531. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2 ) {
  532. return PostEventArgs( ev, time, 2, &arg1, &arg2 );
  533. }
  534. /*
  535. ================
  536. idClass::PostEventMS
  537. ================
  538. */
  539. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2, idEventArg arg3 ) {
  540. return PostEventArgs( ev, time, 3, &arg1, &arg2, &arg3 );
  541. }
  542. /*
  543. ================
  544. idClass::PostEventMS
  545. ================
  546. */
  547. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4 ) {
  548. return PostEventArgs( ev, time, 4, &arg1, &arg2, &arg3, &arg4 );
  549. }
  550. /*
  551. ================
  552. idClass::PostEventMS
  553. ================
  554. */
  555. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5 ) {
  556. return PostEventArgs( ev, time, 5, &arg1, &arg2, &arg3, &arg4, &arg5 );
  557. }
  558. /*
  559. ================
  560. idClass::PostEventMS
  561. ================
  562. */
  563. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6 ) {
  564. return PostEventArgs( ev, time, 6, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6 );
  565. }
  566. /*
  567. ================
  568. idClass::PostEventMS
  569. ================
  570. */
  571. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6, idEventArg arg7 ) {
  572. return PostEventArgs( ev, time, 7, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7 );
  573. }
  574. /*
  575. ================
  576. idClass::PostEventMS
  577. ================
  578. */
  579. bool idClass::PostEventMS( const idEventDef *ev, int time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6, idEventArg arg7, idEventArg arg8 ) {
  580. return PostEventArgs( ev, time, 8, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8 );
  581. }
  582. /*
  583. ================
  584. idClass::PostEventSec
  585. ================
  586. */
  587. bool idClass::PostEventSec( const idEventDef *ev, float time ) {
  588. return PostEventArgs( ev, SEC2MS( time ), 0 );
  589. }
  590. /*
  591. ================
  592. idClass::PostEventSec
  593. ================
  594. */
  595. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1 ) {
  596. return PostEventArgs( ev, SEC2MS( time ), 1, &arg1 );
  597. }
  598. /*
  599. ================
  600. idClass::PostEventSec
  601. ================
  602. */
  603. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2 ) {
  604. return PostEventArgs( ev, SEC2MS( time ), 2, &arg1, &arg2 );
  605. }
  606. /*
  607. ================
  608. idClass::PostEventSec
  609. ================
  610. */
  611. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2, idEventArg arg3 ) {
  612. return PostEventArgs( ev, SEC2MS( time ), 3, &arg1, &arg2, &arg3 );
  613. }
  614. /*
  615. ================
  616. idClass::PostEventSec
  617. ================
  618. */
  619. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4 ) {
  620. return PostEventArgs( ev, SEC2MS( time ), 4, &arg1, &arg2, &arg3, &arg4 );
  621. }
  622. /*
  623. ================
  624. idClass::PostEventSec
  625. ================
  626. */
  627. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5 ) {
  628. return PostEventArgs( ev, SEC2MS( time ), 5, &arg1, &arg2, &arg3, &arg4, &arg5 );
  629. }
  630. /*
  631. ================
  632. idClass::PostEventSec
  633. ================
  634. */
  635. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6 ) {
  636. return PostEventArgs( ev, SEC2MS( time ), 6, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6 );
  637. }
  638. /*
  639. ================
  640. idClass::PostEventSec
  641. ================
  642. */
  643. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6, idEventArg arg7 ) {
  644. return PostEventArgs( ev, SEC2MS( time ), 7, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7 );
  645. }
  646. /*
  647. ================
  648. idClass::PostEventSec
  649. ================
  650. */
  651. bool idClass::PostEventSec( const idEventDef *ev, float time, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6, idEventArg arg7, idEventArg arg8 ) {
  652. return PostEventArgs( ev, SEC2MS( time ), 8, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8 );
  653. }
  654. /*
  655. ================
  656. idClass::ProcessEventArgs
  657. ================
  658. */
  659. bool idClass::ProcessEventArgs( const idEventDef *ev, int numargs, ... ) {
  660. idTypeInfo *c;
  661. int num;
  662. int data[ D_EVENT_MAXARGS ];
  663. va_list args;
  664. assert( ev );
  665. assert( idEvent::initialized );
  666. c = GetType();
  667. num = ev->GetEventNum();
  668. if ( !c->eventMap[ num ] ) {
  669. // we don't respond to this event, so ignore it
  670. return false;
  671. }
  672. va_start( args, numargs );
  673. idEvent::CopyArgs( ev, numargs, args, data );
  674. va_end( args );
  675. ProcessEventArgPtr( ev, data );
  676. return true;
  677. }
  678. /*
  679. ================
  680. idClass::ProcessEvent
  681. ================
  682. */
  683. bool idClass::ProcessEvent( const idEventDef *ev ) {
  684. return ProcessEventArgs( ev, 0 );
  685. }
  686. /*
  687. ================
  688. idClass::ProcessEvent
  689. ================
  690. */
  691. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1 ) {
  692. return ProcessEventArgs( ev, 1, &arg1 );
  693. }
  694. /*
  695. ================
  696. idClass::ProcessEvent
  697. ================
  698. */
  699. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2 ) {
  700. return ProcessEventArgs( ev, 2, &arg1, &arg2 );
  701. }
  702. /*
  703. ================
  704. idClass::ProcessEvent
  705. ================
  706. */
  707. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2, idEventArg arg3 ) {
  708. return ProcessEventArgs( ev, 3, &arg1, &arg2, &arg3 );
  709. }
  710. /*
  711. ================
  712. idClass::ProcessEvent
  713. ================
  714. */
  715. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4 ) {
  716. return ProcessEventArgs( ev, 4, &arg1, &arg2, &arg3, &arg4 );
  717. }
  718. /*
  719. ================
  720. idClass::ProcessEvent
  721. ================
  722. */
  723. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5 ) {
  724. return ProcessEventArgs( ev, 5, &arg1, &arg2, &arg3, &arg4, &arg5 );
  725. }
  726. /*
  727. ================
  728. idClass::ProcessEvent
  729. ================
  730. */
  731. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6 ) {
  732. return ProcessEventArgs( ev, 6, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6 );
  733. }
  734. /*
  735. ================
  736. idClass::ProcessEvent
  737. ================
  738. */
  739. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6, idEventArg arg7 ) {
  740. return ProcessEventArgs( ev, 7, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7 );
  741. }
  742. /*
  743. ================
  744. idClass::ProcessEvent
  745. ================
  746. */
  747. bool idClass::ProcessEvent( const idEventDef *ev, idEventArg arg1, idEventArg arg2, idEventArg arg3, idEventArg arg4, idEventArg arg5, idEventArg arg6, idEventArg arg7, idEventArg arg8 ) {
  748. return ProcessEventArgs( ev, 8, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8 );
  749. }
  750. /*
  751. ================
  752. idClass::ProcessEventArgPtr
  753. ================
  754. */
  755. bool idClass::ProcessEventArgPtr( const idEventDef *ev, int *data ) {
  756. idTypeInfo *c;
  757. int num;
  758. eventCallback_t callback;
  759. assert( ev );
  760. assert( idEvent::initialized );
  761. SetTimeState ts;
  762. if ( IsType( idEntity::Type ) ) {
  763. idEntity *ent = (idEntity*)this;
  764. ts.PushState( ent->timeGroup );
  765. }
  766. if ( g_debugTriggers.GetBool() && ( ev == &EV_Activate ) && IsType( idEntity::Type ) ) {
  767. const idEntity *ent = *reinterpret_cast<idEntity **>( data );
  768. gameLocal.Printf( "%d: '%s' activated by '%s'\n", gameLocal.framenum, static_cast<idEntity *>( this )->GetName(), ent ? ent->GetName() : "NULL" );
  769. }
  770. c = GetType();
  771. num = ev->GetEventNum();
  772. if ( !c->eventMap[ num ] ) {
  773. // we don't respond to this event, so ignore it
  774. return false;
  775. }
  776. callback = c->eventMap[ num ];
  777. #if !CPU_EASYARGS
  778. /*
  779. on ppc architecture, floats are passed in a seperate set of registers
  780. the function prototypes must have matching float declaration
  781. http://developer.apple.com/documentation/DeveloperTools/Conceptual/MachORuntime/2rt_powerpc_abi/chapter_9_section_5.html
  782. */
  783. switch( ev->GetFormatspecIndex() ) {
  784. case 1 << D_EVENT_MAXARGS :
  785. ( this->*callback )();
  786. break;
  787. // generated file - see CREATE_EVENT_CODE
  788. #include "Callbacks.cpp"
  789. default:
  790. gameLocal.Warning( "Invalid formatspec on event '%s'", ev->GetName() );
  791. break;
  792. }
  793. #else
  794. assert( D_EVENT_MAXARGS == 8 );
  795. switch( ev->GetNumArgs() ) {
  796. case 0 :
  797. ( this->*callback )();
  798. break;
  799. case 1 :
  800. typedef void ( idClass::*eventCallback_1_t )( const int );
  801. ( this->*( eventCallback_1_t )callback )( data[ 0 ] );
  802. break;
  803. case 2 :
  804. typedef void ( idClass::*eventCallback_2_t )( const int, const int );
  805. ( this->*( eventCallback_2_t )callback )( data[ 0 ], data[ 1 ] );
  806. break;
  807. case 3 :
  808. typedef void ( idClass::*eventCallback_3_t )( const int, const int, const int );
  809. ( this->*( eventCallback_3_t )callback )( data[ 0 ], data[ 1 ], data[ 2 ] );
  810. break;
  811. case 4 :
  812. typedef void ( idClass::*eventCallback_4_t )( const int, const int, const int, const int );
  813. ( this->*( eventCallback_4_t )callback )( data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ] );
  814. break;
  815. case 5 :
  816. typedef void ( idClass::*eventCallback_5_t )( const int, const int, const int, const int, const int );
  817. ( this->*( eventCallback_5_t )callback )( data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ] );
  818. break;
  819. case 6 :
  820. typedef void ( idClass::*eventCallback_6_t )( const int, const int, const int, const int, const int, const int );
  821. ( this->*( eventCallback_6_t )callback )( data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] );
  822. break;
  823. case 7 :
  824. typedef void ( idClass::*eventCallback_7_t )( const int, const int, const int, const int, const int, const int, const int );
  825. ( this->*( eventCallback_7_t )callback )( data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ] );
  826. break;
  827. case 8 :
  828. typedef void ( idClass::*eventCallback_8_t )( const int, const int, const int, const int, const int, const int, const int, const int );
  829. ( this->*( eventCallback_8_t )callback )( data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ] );
  830. break;
  831. default:
  832. gameLocal.Warning( "Invalid formatspec on event '%s'", ev->GetName() );
  833. break;
  834. }
  835. #endif
  836. return true;
  837. }
  838. /*
  839. ================
  840. idClass::Event_Remove
  841. ================
  842. */
  843. void idClass::Event_Remove() {
  844. delete this;
  845. }
  846. /*
  847. ================
  848. idClass::Event_SafeRemove
  849. ================
  850. */
  851. void idClass::Event_SafeRemove() {
  852. // Forces the remove to be done at a safe time
  853. PostEventMS( &EV_Remove, 0 );
  854. }