Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1857 lines
48 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ====
  2. /*
  3. Entity Data Descriptions
  4. Each entity has an array which defines it's data in way that is useful for
  5. entity communication, parsing initial values from the map, and save/restore.
  6. each entity has to have the following line in it's class declaration:
  7. DECLARE_DATADESC();
  8. this line defines that it has an m_DataDesc[] array, and declares functions through
  9. which various subsystems can iterate the data.
  10. In it's implementation, each entity has to have:
  11. typedescription_t CBaseEntity::m_DataDesc[] = { ... }
  12. in which all it's data is defined (see below), followed by:
  13. which implements the functions necessary for iterating through an entities data desc.
  14. There are several types of data:
  15. FIELD : this is a variable which gets saved & loaded from disk
  16. KEY : this variable can be read in from the map file
  17. GLOBAL : a global field is actually local; it is saved/restored, but is actually
  18. unique to the entity on each level.
  19. CUSTOM : the save/restore parsing functions are described by the user.
  20. ARRAY : an array of values
  21. OUTPUT : a variable or event that can be connected to other entities (see below)
  22. INPUTFUNC : maps a string input to a function pointer. Outputs connected to this input
  23. will call the notify function when fired.
  24. INPUT : maps a string input to a member variable. Outputs connected to this input
  25. will update the input data value when fired.
  26. INPUTNOTIFY : maps a string input to a member variable/function pointer combo. Outputs
  27. connected to this input will update the data value and call the notify
  28. function when fired.
  29. some of these can overlap. all the data descriptions usable are:
  30. DEFINE_FIELD( name, fieldtype )
  31. DEFINE_KEYFIELD( name, fieldtype, mapname )
  32. DEFINE_KEYFIELD_NOTSAVED( name, fieldtype, mapname )
  33. DEFINE_ARRAY( name, fieldtype, count )
  34. DEFINE_GLOBAL_FIELD(name, fieldtype )
  35. DEFINE_CUSTOM_FIELD(name, datafuncs, mapname )
  36. DEFINE_GLOBAL_KEYFIELD(name, fieldtype, mapname )
  37. where:
  38. type is the name of the class (eg. CBaseEntity)
  39. name is the name of the variable in the class (eg. m_iHealth)
  40. fieldtype is the type of data (FIELD_STRING, FIELD_INTEGER, etc)
  41. mapname is the string by which this variable is associated with map file data
  42. count is the number of items in the array
  43. datafuncs is a struct containing function pointers for a custom-defined save/restore/parse
  44. OUTPUTS:
  45. DEFINE_OUTPUT( outputvar, outputname )
  46. This maps the string 'outputname' to the COutput-derived member variable outputvar. In the VMF
  47. file these outputs can be hooked up to inputs (see above). Whenever the internal state
  48. of an entity changes it will often fire off outputs so that map makers can hook up behaviors.
  49. e.g. A door entity would have OnDoorOpen, OnDoorClose, OnTouched, etc outputs.
  50. */
  51. //=============================================================================
  52. #include "cbase.h"
  53. #include "entitylist.h"
  54. #include "mapentities_shared.h"
  55. #include "isaverestore.h"
  56. #include "eventqueue.h"
  57. #include "entitydefs.h"
  58. #include "entityinput.h"
  59. #include "entityoutput.h"
  60. #include "mempool.h"
  61. #include "tier1/strtools.h"
  62. #include "datacache/imdlcache.h"
  63. #include "env_debughistory.h"
  64. #include "tier0/vprof.h"
  65. // memdbgon must be the last include file in a .cpp file!!!
  66. #include "tier0/memdbgon.h"
  67. extern ISaveRestoreOps *variantFuncs; // function pointer set for save/restoring variants
  68. BEGIN_SIMPLE_DATADESC( CEventAction )
  69. DEFINE_FIELD( m_iTarget, FIELD_STRING ),
  70. DEFINE_FIELD( m_iTargetInput, FIELD_STRING ),
  71. DEFINE_FIELD( m_iParameter, FIELD_STRING ),
  72. DEFINE_FIELD( m_flDelay, FIELD_FLOAT ),
  73. DEFINE_FIELD( m_nTimesToFire, FIELD_INTEGER ),
  74. DEFINE_FIELD( m_iIDStamp, FIELD_INTEGER ),
  75. // This is dealt with by the Restore method
  76. // DEFINE_FIELD( m_pNext, CEventAction ),
  77. END_DATADESC()
  78. // ID Stamp used to uniquely identify every output
  79. int CEventAction::s_iNextIDStamp = 0;
  80. //-----------------------------------------------------------------------------
  81. // Purpose: Creates an event action and assigns it an unique ID stamp.
  82. // Input : ActionData - the map file data block descibing the event action.
  83. //-----------------------------------------------------------------------------
  84. CEventAction::CEventAction( const char *ActionData )
  85. {
  86. m_pNext = NULL;
  87. m_iIDStamp = ++s_iNextIDStamp;
  88. m_flDelay = 0;
  89. m_iTarget = NULL_STRING;
  90. m_iParameter = NULL_STRING;
  91. m_iTargetInput = NULL_STRING;
  92. m_nTimesToFire = EVENT_FIRE_ALWAYS;
  93. if (ActionData == NULL)
  94. return;
  95. char szToken[256];
  96. char chDelim = VMF_IOPARAM_STRING_DELIMITER;
  97. if (!strchr(ActionData, VMF_IOPARAM_STRING_DELIMITER))
  98. {
  99. chDelim = ',';
  100. }
  101. //
  102. // Parse the target name.
  103. //
  104. const char *psz = nexttoken(szToken, ActionData, chDelim);
  105. if (szToken[0] != '\0')
  106. {
  107. m_iTarget = AllocPooledString(szToken);
  108. }
  109. //
  110. // Parse the input name.
  111. //
  112. psz = nexttoken(szToken, psz, chDelim);
  113. if (szToken[0] != '\0')
  114. {
  115. m_iTargetInput = AllocPooledString(szToken);
  116. }
  117. else
  118. {
  119. m_iTargetInput = AllocPooledString("Use");
  120. }
  121. //
  122. // Parse the parameter override.
  123. //
  124. psz = nexttoken(szToken, psz, chDelim);
  125. if (szToken[0] != '\0')
  126. {
  127. m_iParameter = AllocPooledString(szToken);
  128. }
  129. //
  130. // Parse the delay.
  131. //
  132. psz = nexttoken(szToken, psz, chDelim);
  133. if (szToken[0] != '\0')
  134. {
  135. m_flDelay = atof(szToken);
  136. }
  137. //
  138. // Parse the number of times to fire.
  139. //
  140. nexttoken(szToken, psz, chDelim);
  141. if (szToken[0] != '\0')
  142. {
  143. m_nTimesToFire = atoi(szToken);
  144. if (m_nTimesToFire == 0)
  145. {
  146. m_nTimesToFire = EVENT_FIRE_ALWAYS;
  147. }
  148. }
  149. }
  150. CEventAction::CEventAction( const CEventAction &p_EventAction )
  151. {
  152. m_pNext = NULL;
  153. m_iIDStamp = ++s_iNextIDStamp;
  154. m_flDelay = p_EventAction.m_flDelay;
  155. m_iTarget = p_EventAction.m_iTarget;
  156. m_iParameter = p_EventAction.m_iParameter;
  157. m_iTargetInput = p_EventAction.m_iTargetInput;
  158. m_nTimesToFire = p_EventAction.m_nTimesToFire;
  159. }
  160. // this memory pool stores blocks around the size of CEventAction/inputitem_t structs
  161. // can be used for other blocks; will error if to big a block is tried to be allocated
  162. CUtlMemoryPool g_EntityListPool( MAX(sizeof(CEventAction),sizeof(CMultiInputVar::inputitem_t)), 512, CUtlMemoryPool::GROW_FAST, "g_EntityListPool" );
  163. #include "tier0/memdbgoff.h"
  164. void *CEventAction::operator new( size_t stAllocateBlock )
  165. {
  166. return g_EntityListPool.Alloc( stAllocateBlock );
  167. }
  168. void *CEventAction::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
  169. {
  170. return g_EntityListPool.Alloc( stAllocateBlock );
  171. }
  172. void CEventAction::operator delete( void *pMem )
  173. {
  174. g_EntityListPool.Free( pMem );
  175. }
  176. #include "tier0/memdbgon.h"
  177. //-----------------------------------------------------------------------------
  178. // Purpose: Returns the highest-valued delay in our list of event actions.
  179. //-----------------------------------------------------------------------------
  180. float CBaseEntityOutput::GetMaxDelay(void)
  181. {
  182. float flMaxDelay = 0;
  183. CEventAction *ev = m_ActionList;
  184. while (ev != NULL)
  185. {
  186. if (ev->m_flDelay > flMaxDelay)
  187. {
  188. flMaxDelay = ev->m_flDelay;
  189. }
  190. ev = ev->m_pNext;
  191. }
  192. return(flMaxDelay);
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose: Destructor.
  196. //-----------------------------------------------------------------------------
  197. CBaseEntityOutput::~CBaseEntityOutput()
  198. {
  199. CEventAction *ev = m_ActionList;
  200. while (ev != NULL)
  201. {
  202. CEventAction *pNext = ev->m_pNext;
  203. delete ev;
  204. ev = pNext;
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose: Fires the event, causing a sequence of action to occur in other ents.
  209. // Input : pActivator - Entity that initiated this sequence of actions.
  210. // pCaller - Entity that is actually causing the event.
  211. //-----------------------------------------------------------------------------
  212. void CBaseEntityOutput::FireOutput(variant_t Value, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
  213. {
  214. //
  215. // Iterate through all eventactions and fire them off.
  216. //
  217. CEventAction *ev = m_ActionList;
  218. CEventAction *prev = NULL;
  219. while (ev != NULL)
  220. {
  221. if (ev->m_iParameter == NULL_STRING)
  222. {
  223. //
  224. // Post the event with the default parameter.
  225. //
  226. g_EventQueue.AddEvent( STRING(ev->m_iTarget), STRING(ev->m_iTargetInput), Value, ev->m_flDelay + fDelay, pActivator, pCaller, ev->m_iIDStamp );
  227. }
  228. else
  229. {
  230. //
  231. // Post the event with a parameter override.
  232. //
  233. variant_t ValueOverride;
  234. ValueOverride.SetString( ev->m_iParameter );
  235. g_EventQueue.AddEvent( STRING(ev->m_iTarget), STRING(ev->m_iTargetInput), ValueOverride, ev->m_flDelay, pActivator, pCaller, ev->m_iIDStamp );
  236. }
  237. if ( developer.GetBool() )
  238. {
  239. if ( ev->m_flDelay )
  240. {
  241. char szBuffer[256];
  242. Q_snprintf( szBuffer, sizeof(szBuffer), "(%0.2f) output: (%s,%s) -> (%s,%s,%.1f)(%s)\n", gpGlobals->curtime, pCaller ? STRING(pCaller->m_iClassname) : "NULL", pCaller ? STRING(pCaller->GetEntityName()) : "NULL", STRING(ev->m_iTarget), STRING(ev->m_iTargetInput), ev->m_flDelay, STRING(ev->m_iParameter) );
  243. DevMsg( 2, "%s", szBuffer );
  244. ADD_DEBUG_HISTORY( HISTORY_ENTITY_IO, szBuffer );
  245. }
  246. else
  247. {
  248. char szBuffer[256];
  249. Q_snprintf( szBuffer, sizeof(szBuffer), "(%0.2f) output: (%s,%s) -> (%s,%s)(%s)\n", gpGlobals->curtime, pCaller ? STRING(pCaller->m_iClassname) : "NULL", pCaller ? STRING(pCaller->GetEntityName()) : "NULL", STRING(ev->m_iTarget), STRING(ev->m_iTargetInput), STRING(ev->m_iParameter) );
  250. DevMsg( 2, "%s", szBuffer );
  251. ADD_DEBUG_HISTORY( HISTORY_ENTITY_IO, szBuffer );
  252. }
  253. }
  254. if ( pCaller && pCaller->m_debugOverlays & OVERLAY_MESSAGE_BIT)
  255. {
  256. pCaller->DrawOutputOverlay(ev);
  257. }
  258. //
  259. // Remove the event action from the list if it was set to be fired a finite
  260. // number of times (and has been).
  261. //
  262. bool bRemove = false;
  263. if (ev->m_nTimesToFire != EVENT_FIRE_ALWAYS)
  264. {
  265. ev->m_nTimesToFire--;
  266. if (ev->m_nTimesToFire == 0)
  267. {
  268. char szBuffer[256];
  269. Q_snprintf( szBuffer, sizeof(szBuffer), "Removing from action list: (%s,%s) -> (%s,%s)\n", pCaller ? STRING(pCaller->m_iClassname) : "NULL", pCaller ? STRING(pCaller->GetEntityName()) : "NULL", STRING(ev->m_iTarget), STRING(ev->m_iTargetInput));
  270. DevMsg( 2, "%s", szBuffer );
  271. ADD_DEBUG_HISTORY( HISTORY_ENTITY_IO, szBuffer );
  272. bRemove = true;
  273. }
  274. }
  275. if (!bRemove)
  276. {
  277. prev = ev;
  278. ev = ev->m_pNext;
  279. }
  280. else
  281. {
  282. if (prev != NULL)
  283. {
  284. prev->m_pNext = ev->m_pNext;
  285. }
  286. else
  287. {
  288. m_ActionList = ev->m_pNext;
  289. }
  290. CEventAction *next = ev->m_pNext;
  291. delete ev;
  292. ev = next;
  293. }
  294. }
  295. }
  296. //-----------------------------------------------------------------------------
  297. // Purpose: Parameterless firing of an event
  298. // Input : pActivator -
  299. // pCaller -
  300. //-----------------------------------------------------------------------------
  301. void COutputEvent::FireOutput(CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
  302. {
  303. variant_t Val;
  304. Val.Set( FIELD_VOID, NULL );
  305. CBaseEntityOutput::FireOutput(Val, pActivator, pCaller, fDelay);
  306. }
  307. void CBaseEntityOutput::ParseEventAction( const char *EventData )
  308. {
  309. AddEventAction( new CEventAction( EventData ) );
  310. }
  311. void CBaseEntityOutput::AddEventAction( CEventAction *pEventAction )
  312. {
  313. pEventAction->m_pNext = m_ActionList;
  314. m_ActionList = pEventAction;
  315. }
  316. void CBaseEntityOutput::RemoveEventAction( CEventAction *pEventAction )
  317. {
  318. CEventAction *pAction = GetFirstAction();
  319. CEventAction *pPrevAction = NULL;
  320. while ( pAction )
  321. {
  322. if ( pAction == pEventAction )
  323. {
  324. if ( !pPrevAction )
  325. {
  326. m_ActionList = NULL;
  327. }
  328. else
  329. {
  330. pPrevAction->m_pNext = pAction->m_pNext;
  331. }
  332. return;
  333. }
  334. pAction = pAction->m_pNext;
  335. }
  336. }
  337. // save data description for the event queue
  338. BEGIN_SIMPLE_DATADESC( CBaseEntityOutput )
  339. DEFINE_CUSTOM_FIELD( m_Value, variantFuncs ),
  340. // This is saved manually by CBaseEntityOutput::Save
  341. // DEFINE_FIELD( m_ActionList, CEventAction ),
  342. END_DATADESC()
  343. int CBaseEntityOutput::Save( ISave &save )
  344. {
  345. // save that value out to disk, so we know how many to restore
  346. if ( !save.WriteFields( "Value", this, NULL, m_DataMap.dataDesc, m_DataMap.dataNumFields ) )
  347. return 0;
  348. for ( CEventAction *ev = m_ActionList; ev != NULL; ev = ev->m_pNext )
  349. {
  350. if ( !save.WriteFields( "EntityOutput", ev, NULL, ev->m_DataMap.dataDesc, ev->m_DataMap.dataNumFields ) )
  351. return 0;
  352. }
  353. return 1;
  354. }
  355. int CBaseEntityOutput::Restore( IRestore &restore, int elementCount )
  356. {
  357. // load the number of items saved
  358. if ( !restore.ReadFields( "Value", this, NULL, m_DataMap.dataDesc, m_DataMap.dataNumFields ) )
  359. return 0;
  360. m_ActionList = NULL;
  361. // read in all the fields
  362. CEventAction *lastEv = NULL;
  363. for ( int i = 0; i < elementCount; i++ )
  364. {
  365. CEventAction *ev = new CEventAction(NULL);
  366. if ( !restore.ReadFields( "EntityOutput", ev, NULL, ev->m_DataMap.dataDesc, ev->m_DataMap.dataNumFields ) )
  367. return 0;
  368. // add it to the list in the same order it was saved in
  369. if ( lastEv )
  370. {
  371. lastEv->m_pNext = ev;
  372. }
  373. else
  374. {
  375. m_ActionList = ev;
  376. }
  377. ev->m_pNext = NULL;
  378. lastEv = ev;
  379. }
  380. return 1;
  381. }
  382. const CEventAction *CBaseEntityOutput::GetActionForTarget( string_t iSearchTarget ) const
  383. {
  384. for ( CEventAction *ev = m_ActionList; ev != NULL; ev = ev->m_pNext )
  385. {
  386. if ( ev->m_iTarget == iSearchTarget )
  387. return ev;
  388. }
  389. return NULL;
  390. }
  391. int CBaseEntityOutput::NumberOfElements( void )
  392. {
  393. int count = 0;
  394. for ( CEventAction *ev = m_ActionList; ev != NULL; ev = ev->m_pNext )
  395. {
  396. count++;
  397. }
  398. return count;
  399. }
  400. /// Delete every single action in the action list.
  401. void CBaseEntityOutput::DeleteAllElements( void )
  402. {
  403. // walk front to back, deleting as we go. We needn't fix up pointers because
  404. // EVERYTHING will die.
  405. CEventAction *pNext = m_ActionList;
  406. // wipe out the head
  407. m_ActionList = NULL;
  408. while (pNext)
  409. {
  410. register CEventAction *strikeThis = pNext;
  411. pNext = pNext->m_pNext;
  412. delete strikeThis;
  413. }
  414. }
  415. /// EVENTS save/restore parsing wrapper
  416. class CEventsSaveDataOps : public ISaveRestoreOps
  417. {
  418. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  419. {
  420. AssertMsg( fieldInfo.pTypeDesc->fieldSize == 1, "CEventsSaveDataOps does not support arrays");
  421. CBaseEntityOutput *ev = (CBaseEntityOutput*)fieldInfo.pField;
  422. const int fieldSize = fieldInfo.pTypeDesc->fieldSize;
  423. for ( int i = 0; i < fieldSize; i++, ev++ )
  424. {
  425. // save out the number of fields
  426. int numElements = ev->NumberOfElements();
  427. pSave->WriteInt( &numElements, 1 );
  428. // save the event data
  429. ev->Save( *pSave );
  430. }
  431. }
  432. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  433. {
  434. AssertMsg( fieldInfo.pTypeDesc->fieldSize == 1, "CEventsSaveDataOps does not support arrays");
  435. CBaseEntityOutput *ev = (CBaseEntityOutput*)fieldInfo.pField;
  436. const int fieldSize = fieldInfo.pTypeDesc->fieldSize;
  437. for ( int i = 0; i < fieldSize; i++, ev++ )
  438. {
  439. int nElements = pRestore->ReadInt();
  440. Assert( nElements < 100 );
  441. ev->Restore( *pRestore, nElements );
  442. }
  443. }
  444. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  445. {
  446. AssertMsg( fieldInfo.pTypeDesc->fieldSize == 1, "CEventsSaveDataOps does not support arrays");
  447. // check all the elements of the array (usually only 1)
  448. CBaseEntityOutput *ev = (CBaseEntityOutput*)fieldInfo.pField;
  449. const int fieldSize = fieldInfo.pTypeDesc->fieldSize;
  450. for ( int i = 0; i < fieldSize; i++, ev++ )
  451. {
  452. // It's not empty if it has events or if it has a non-void variant value
  453. if (( ev->NumberOfElements() != 0 ) || ( ev->ValueFieldType() != FIELD_VOID ))
  454. return 0;
  455. }
  456. // variant has no data
  457. return 1;
  458. }
  459. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  460. {
  461. // Don't no how to. This is okay, since objects of this type
  462. // are always born clean before restore, and not reused
  463. }
  464. virtual bool Parse( const SaveRestoreFieldInfo_t &fieldInfo, char const* szValue )
  465. {
  466. CBaseEntityOutput *ev = (CBaseEntityOutput*)fieldInfo.pField;
  467. ev->ParseEventAction( szValue );
  468. return true;
  469. }
  470. };
  471. CEventsSaveDataOps g_EventsSaveDataOps;
  472. ISaveRestoreOps *eventFuncs = &g_EventsSaveDataOps;
  473. //-----------------------------------------------------------------------------
  474. // CMultiInputVar implementation
  475. //
  476. // Purpose: holds a list of inputs and their ID tags
  477. // used for entities that hold inputs from a set of other entities
  478. //-----------------------------------------------------------------------------
  479. //-----------------------------------------------------------------------------
  480. // Purpose: destructor, frees the data list
  481. //-----------------------------------------------------------------------------
  482. CMultiInputVar::~CMultiInputVar()
  483. {
  484. if ( m_InputList )
  485. {
  486. while ( m_InputList->next != NULL )
  487. {
  488. inputitem_t *input = m_InputList->next;
  489. m_InputList->next = input->next;
  490. delete input;
  491. }
  492. delete m_InputList;
  493. }
  494. }
  495. //-----------------------------------------------------------------------------
  496. // Purpose: Updates the data set with a new value
  497. // Input : newVal - the new value to add to or update in the list
  498. // outputID - the source of the value
  499. //-----------------------------------------------------------------------------
  500. void CMultiInputVar::AddValue( variant_t newVal, int outputID )
  501. {
  502. // see if it's already in the list
  503. inputitem_t *inp;
  504. for ( inp = m_InputList; inp != NULL; inp = inp->next )
  505. {
  506. // already in list, so just update this link
  507. if ( inp->outputID == outputID )
  508. {
  509. inp->value = newVal;
  510. return;
  511. }
  512. }
  513. // add to start of list
  514. inp = new inputitem_t;
  515. inp->value = newVal;
  516. inp->outputID = outputID;
  517. if ( !m_InputList )
  518. {
  519. m_InputList = inp;
  520. inp->next = NULL;
  521. }
  522. else
  523. {
  524. inp->next = m_InputList;
  525. m_InputList = inp;
  526. }
  527. }
  528. #include "tier0/memdbgoff.h"
  529. //-----------------------------------------------------------------------------
  530. // Purpose: allocates memory from the entitylist pool
  531. //-----------------------------------------------------------------------------
  532. void *CMultiInputVar::inputitem_t::operator new( size_t stAllocateBlock )
  533. {
  534. return g_EntityListPool.Alloc( stAllocateBlock );
  535. }
  536. void *CMultiInputVar::inputitem_t::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
  537. {
  538. return g_EntityListPool.Alloc( stAllocateBlock );
  539. }
  540. //-----------------------------------------------------------------------------
  541. // Purpose: frees memory from the entitylist pool
  542. //-----------------------------------------------------------------------------
  543. void CMultiInputVar::inputitem_t::operator delete( void *pMem )
  544. {
  545. g_EntityListPool.Free( pMem );
  546. }
  547. #include "tier0/memdbgon.h"
  548. //-----------------------------------------------------------------------------
  549. // CEventQueue implementation
  550. //
  551. // Purpose: holds and executes a global prioritized queue of entity actions
  552. //-----------------------------------------------------------------------------
  553. DEFINE_FIXEDSIZE_ALLOCATOR( EventQueuePrioritizedEvent_t, 128, CUtlMemoryPool::GROW_SLOW );
  554. CEventQueue g_EventQueue;
  555. CEventQueue::CEventQueue()
  556. {
  557. m_Events.m_flFireTime = -FLT_MAX;
  558. m_Events.m_pNext = NULL;
  559. Init();
  560. }
  561. CEventQueue::~CEventQueue()
  562. {
  563. Clear();
  564. }
  565. // Robin: Left here for backwards compatability.
  566. class CEventQueueSaveLoadProxy : public CLogicalEntity
  567. {
  568. DECLARE_CLASS( CEventQueueSaveLoadProxy, CLogicalEntity );
  569. int Save( ISave &save )
  570. {
  571. if ( !BaseClass::Save(save) )
  572. return 0;
  573. // save out the message queue
  574. return g_EventQueue.Save( save );
  575. }
  576. int Restore( IRestore &restore )
  577. {
  578. if ( !BaseClass::Restore(restore) )
  579. return 0;
  580. // restore the event queue
  581. int iReturn = g_EventQueue.Restore( restore );
  582. // Now remove myself, because the CEventQueue_SaveRestoreBlockHandler
  583. // will handle future saves.
  584. UTIL_Remove( this );
  585. return iReturn;
  586. }
  587. };
  588. LINK_ENTITY_TO_CLASS(event_queue_saveload_proxy, CEventQueueSaveLoadProxy);
  589. //-----------------------------------------------------------------------------
  590. // EVENT QUEUE SAVE / RESTORE
  591. //-----------------------------------------------------------------------------
  592. static short EVENTQUEUE_SAVE_RESTORE_VERSION = 1;
  593. class CEventQueue_SaveRestoreBlockHandler : public CDefSaveRestoreBlockHandler
  594. {
  595. public:
  596. const char *GetBlockName()
  597. {
  598. return "EventQueue";
  599. }
  600. //---------------------------------
  601. void Save( ISave *pSave )
  602. {
  603. g_EventQueue.Save( *pSave );
  604. }
  605. //---------------------------------
  606. void WriteSaveHeaders( ISave *pSave )
  607. {
  608. pSave->WriteShort( &EVENTQUEUE_SAVE_RESTORE_VERSION );
  609. }
  610. //---------------------------------
  611. void ReadRestoreHeaders( IRestore *pRestore )
  612. {
  613. // No reason why any future version shouldn't try to retain backward compatability. The default here is to not do so.
  614. short version;
  615. pRestore->ReadShort( &version );
  616. m_fDoLoad = ( version == EVENTQUEUE_SAVE_RESTORE_VERSION );
  617. }
  618. //---------------------------------
  619. void Restore( IRestore *pRestore, bool createPlayers )
  620. {
  621. if ( m_fDoLoad )
  622. {
  623. g_EventQueue.Restore( *pRestore );
  624. }
  625. }
  626. private:
  627. bool m_fDoLoad;
  628. };
  629. //-----------------------------------------------------------------------------
  630. CEventQueue_SaveRestoreBlockHandler g_EventQueue_SaveRestoreBlockHandler;
  631. //-------------------------------------
  632. ISaveRestoreBlockHandler *GetEventQueueSaveRestoreBlockHandler()
  633. {
  634. return &g_EventQueue_SaveRestoreBlockHandler;
  635. }
  636. void CEventQueue::Init( void )
  637. {
  638. Clear();
  639. }
  640. void CEventQueue::Clear( void )
  641. {
  642. // delete all the events in the queue
  643. EventQueuePrioritizedEvent_t *pe = m_Events.m_pNext;
  644. while ( pe != NULL )
  645. {
  646. EventQueuePrioritizedEvent_t *next = pe->m_pNext;
  647. delete pe;
  648. pe = next;
  649. }
  650. m_Events.m_pNext = NULL;
  651. }
  652. void CEventQueue::Dump( void )
  653. {
  654. EventQueuePrioritizedEvent_t *pe = m_Events.m_pNext;
  655. Msg("Dumping event queue. Current time is: %.2f\n", gpGlobals->curtime );
  656. while ( pe != NULL )
  657. {
  658. EventQueuePrioritizedEvent_t *next = pe->m_pNext;
  659. Msg(" (%.2f) Target: '%s', Input: '%s', Parameter '%s'. Activator: '%s', Caller '%s'. \n",
  660. pe->m_flFireTime,
  661. STRING(pe->m_iTarget),
  662. STRING(pe->m_iTargetInput),
  663. pe->m_VariantValue.String(),
  664. pe->m_pActivator ? pe->m_pActivator->GetDebugName() : "None",
  665. pe->m_pCaller ? pe->m_pCaller->GetDebugName() : "None" );
  666. pe = next;
  667. }
  668. Msg("Finished dump.\n");
  669. }
  670. //-----------------------------------------------------------------------------
  671. // Purpose: adds the action into the correct spot in the priority queue, targeting entity via string name
  672. //-----------------------------------------------------------------------------
  673. void CEventQueue::AddEvent( const char *target, const char *targetInput, variant_t Value, float fireDelay, CBaseEntity *pActivator, CBaseEntity *pCaller, int outputID )
  674. {
  675. // build the new event
  676. EventQueuePrioritizedEvent_t *newEvent = new EventQueuePrioritizedEvent_t;
  677. newEvent->m_flFireTime = gpGlobals->curtime + fireDelay; // priority key in the priority queue
  678. newEvent->m_iTarget = MAKE_STRING( target );
  679. newEvent->m_pEntTarget = NULL;
  680. newEvent->m_iTargetInput = MAKE_STRING( targetInput );
  681. newEvent->m_pActivator = pActivator;
  682. newEvent->m_pCaller = pCaller;
  683. newEvent->m_VariantValue = Value;
  684. newEvent->m_iOutputID = outputID;
  685. AddEvent( newEvent );
  686. }
  687. //-----------------------------------------------------------------------------
  688. // Purpose: adds the action into the correct spot in the priority queue, targeting entity via pointer
  689. //-----------------------------------------------------------------------------
  690. void CEventQueue::AddEvent( CBaseEntity *target, const char *targetInput, variant_t Value, float fireDelay, CBaseEntity *pActivator, CBaseEntity *pCaller, int outputID )
  691. {
  692. // build the new event
  693. EventQueuePrioritizedEvent_t *newEvent = new EventQueuePrioritizedEvent_t;
  694. newEvent->m_flFireTime = gpGlobals->curtime + fireDelay; // primary priority key in the priority queue
  695. newEvent->m_iTarget = NULL_STRING;
  696. newEvent->m_pEntTarget = target;
  697. newEvent->m_iTargetInput = MAKE_STRING( targetInput );
  698. newEvent->m_pActivator = pActivator;
  699. newEvent->m_pCaller = pCaller;
  700. newEvent->m_VariantValue = Value;
  701. newEvent->m_iOutputID = outputID;
  702. AddEvent( newEvent );
  703. }
  704. void CEventQueue::AddEvent( CBaseEntity *target, const char *action, float fireDelay, CBaseEntity *pActivator, CBaseEntity *pCaller, int outputID )
  705. {
  706. variant_t Value;
  707. Value.Set( FIELD_VOID, NULL );
  708. AddEvent( target, action, Value, fireDelay, pActivator, pCaller, outputID );
  709. }
  710. //-----------------------------------------------------------------------------
  711. // Purpose: private function, adds an event into the list
  712. // Input : *newEvent - the (already built) event to add
  713. //-----------------------------------------------------------------------------
  714. void CEventQueue::AddEvent( EventQueuePrioritizedEvent_t *newEvent )
  715. {
  716. // loop through the actions looking for a place to insert
  717. EventQueuePrioritizedEvent_t *pe;
  718. for ( pe = &m_Events; pe->m_pNext != NULL; pe = pe->m_pNext )
  719. {
  720. if ( pe->m_pNext->m_flFireTime > newEvent->m_flFireTime )
  721. {
  722. break;
  723. }
  724. }
  725. Assert( pe );
  726. // insert
  727. newEvent->m_pNext = pe->m_pNext;
  728. newEvent->m_pPrev = pe;
  729. pe->m_pNext = newEvent;
  730. if ( newEvent->m_pNext )
  731. {
  732. newEvent->m_pNext->m_pPrev = newEvent;
  733. }
  734. }
  735. void CEventQueue::RemoveEvent( EventQueuePrioritizedEvent_t *pe )
  736. {
  737. Assert( pe->m_pPrev );
  738. pe->m_pPrev->m_pNext = pe->m_pNext;
  739. if ( pe->m_pNext )
  740. {
  741. pe->m_pNext->m_pPrev = pe->m_pPrev;
  742. }
  743. }
  744. //-----------------------------------------------------------------------------
  745. // Purpose: fires off any events in the queue who's fire time is (or before) the present time
  746. //-----------------------------------------------------------------------------
  747. void CEventQueue::ServiceEvents( void )
  748. {
  749. if (!CBaseEntity::Debug_ShouldStep())
  750. {
  751. return;
  752. }
  753. EventQueuePrioritizedEvent_t *pe = m_Events.m_pNext;
  754. while ( pe != NULL && pe->m_flFireTime <= gpGlobals->curtime )
  755. {
  756. MDLCACHE_CRITICAL_SECTION();
  757. bool targetFound = false;
  758. // find the targets
  759. if ( pe->m_iTarget != NULL_STRING )
  760. {
  761. // In the context the event, the searching entity is also the caller
  762. CBaseEntity *pSearchingEntity = pe->m_pCaller;
  763. CBaseEntity *target = NULL;
  764. while ( 1 )
  765. {
  766. target = gEntList.FindEntityByName( target, pe->m_iTarget, pSearchingEntity, pe->m_pActivator, pe->m_pCaller );
  767. if ( !target )
  768. break;
  769. // pump the action into the target
  770. target->AcceptInput( STRING(pe->m_iTargetInput), pe->m_pActivator, pe->m_pCaller, pe->m_VariantValue, pe->m_iOutputID );
  771. targetFound = true;
  772. }
  773. }
  774. // direct pointer
  775. if ( pe->m_pEntTarget != NULL )
  776. {
  777. pe->m_pEntTarget->AcceptInput( STRING(pe->m_iTargetInput), pe->m_pActivator, pe->m_pCaller, pe->m_VariantValue, pe->m_iOutputID );
  778. targetFound = true;
  779. }
  780. if ( !targetFound )
  781. {
  782. // See if we can find a target if we treat the target as a classname
  783. if ( pe->m_iTarget != NULL_STRING )
  784. {
  785. CBaseEntity *target = NULL;
  786. while ( 1 )
  787. {
  788. target = gEntList.FindEntityByClassname( target, STRING(pe->m_iTarget) );
  789. if ( !target )
  790. break;
  791. // pump the action into the target
  792. target->AcceptInput( STRING(pe->m_iTargetInput), pe->m_pActivator, pe->m_pCaller, pe->m_VariantValue, pe->m_iOutputID );
  793. targetFound = true;
  794. }
  795. }
  796. }
  797. if ( !targetFound )
  798. {
  799. const char *pClass ="", *pName = "";
  800. // might be NULL
  801. if ( pe->m_pCaller )
  802. {
  803. pClass = STRING(pe->m_pCaller->m_iClassname);
  804. pName = STRING(pe->m_pCaller->GetEntityName());
  805. }
  806. char szBuffer[256];
  807. Q_snprintf( szBuffer, sizeof(szBuffer), "unhandled input: (%s) -> (%s), from (%s,%s); target entity not found\n", STRING(pe->m_iTargetInput), STRING(pe->m_iTarget), pClass, pName );
  808. DevMsg( 2, "%s", szBuffer );
  809. ADD_DEBUG_HISTORY( HISTORY_ENTITY_IO, szBuffer );
  810. }
  811. // remove the event from the list (remembering that the queue may have been added to)
  812. RemoveEvent( pe );
  813. delete pe;
  814. //
  815. // If we are in debug mode, exit the loop if we have fired the correct number of events.
  816. //
  817. if (CBaseEntity::Debug_IsPaused())
  818. {
  819. if (!CBaseEntity::Debug_Step())
  820. {
  821. break;
  822. }
  823. }
  824. // restart the list (to catch any new items have probably been added to the queue)
  825. pe = m_Events.m_pNext;
  826. }
  827. }
  828. //-----------------------------------------------------------------------------
  829. // Purpose: Dumps the contents of the Entity I/O event queue to the console.
  830. //-----------------------------------------------------------------------------
  831. void CC_DumpEventQueue()
  832. {
  833. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  834. return;
  835. g_EventQueue.Dump();
  836. }
  837. static ConCommand dumpeventqueue( "dumpeventqueue", CC_DumpEventQueue, "Dump the contents of the Entity I/O event queue to the console." );
  838. //-----------------------------------------------------------------------------
  839. // Purpose: Removes all pending events from the I/O queue that were added by the
  840. // given caller.
  841. //
  842. // TODO: This is only as reliable as callers are in passing the correct
  843. // caller pointer when they fire the outputs. Make more foolproof.
  844. //-----------------------------------------------------------------------------
  845. void CEventQueue::CancelEvents( CBaseEntity *pCaller )
  846. {
  847. if (!pCaller)
  848. return;
  849. EventQueuePrioritizedEvent_t *pCur = m_Events.m_pNext;
  850. while (pCur != NULL)
  851. {
  852. bool bDelete = false;
  853. if (pCur->m_pCaller == pCaller)
  854. {
  855. // Pointers match; make sure everything else matches.
  856. if (!stricmp(STRING(pCur->m_pCaller->GetEntityName()), STRING(pCaller->GetEntityName())) &&
  857. !stricmp(pCur->m_pCaller->GetClassname(), pCaller->GetClassname()))
  858. {
  859. // Found a matching event; delete it from the queue.
  860. bDelete = true;
  861. }
  862. }
  863. EventQueuePrioritizedEvent_t *pCurSave = pCur;
  864. pCur = pCur->m_pNext;
  865. if (bDelete)
  866. {
  867. RemoveEvent( pCurSave );
  868. delete pCurSave;
  869. }
  870. }
  871. }
  872. //-----------------------------------------------------------------------------
  873. // Purpose: Removes all pending events of the specified type from the I/O queue of the specified target
  874. //
  875. // TODO: This is only as reliable as callers are in passing the correct
  876. // caller pointer when they fire the outputs. Make more foolproof.
  877. //-----------------------------------------------------------------------------
  878. void CEventQueue::CancelEventOn( CBaseEntity *pTarget, const char *sInputName )
  879. {
  880. if (!pTarget)
  881. return;
  882. EventQueuePrioritizedEvent_t *pCur = m_Events.m_pNext;
  883. while (pCur != NULL)
  884. {
  885. bool bDelete = false;
  886. if (pCur->m_pEntTarget == pTarget)
  887. {
  888. if ( StringHasPrefixCaseSensitive( STRING(pCur->m_iTargetInput), sInputName ) )
  889. {
  890. // Found a matching event; delete it from the queue.
  891. bDelete = true;
  892. }
  893. }
  894. EventQueuePrioritizedEvent_t *pCurSave = pCur;
  895. pCur = pCur->m_pNext;
  896. if (bDelete)
  897. {
  898. RemoveEvent( pCurSave );
  899. delete pCurSave;
  900. }
  901. }
  902. }
  903. //-----------------------------------------------------------------------------
  904. // Purpose: Return true if the target has any pending inputs.
  905. // Input : *pTarget -
  906. // *sInputName - NULL for any input, or a specified one
  907. //-----------------------------------------------------------------------------
  908. bool CEventQueue::HasEventPending( CBaseEntity *pTarget, const char *sInputName )
  909. {
  910. if (!pTarget)
  911. return false;
  912. EventQueuePrioritizedEvent_t *pCur = m_Events.m_pNext;
  913. while (pCur != NULL)
  914. {
  915. if (pCur->m_pEntTarget == pTarget)
  916. {
  917. if ( !sInputName )
  918. return true;
  919. if ( StringHasPrefixCaseSensitive( STRING(pCur->m_iTargetInput), sInputName ) )
  920. return true;
  921. }
  922. pCur = pCur->m_pNext;
  923. }
  924. return false;
  925. }
  926. void ServiceEventQueue( void )
  927. {
  928. VPROF("ServiceEventQueue()");
  929. g_EventQueue.ServiceEvents();
  930. }
  931. // save data description for the event queue
  932. BEGIN_SIMPLE_DATADESC( CEventQueue )
  933. // These are saved explicitly in CEventQueue::Save below
  934. // DEFINE_FIELD( m_Events, EventQueuePrioritizedEvent_t ),
  935. DEFINE_FIELD( m_iListCount, FIELD_INTEGER ), // this value is only used during save/restore
  936. END_DATADESC()
  937. // save data for a single event in the queue
  938. BEGIN_SIMPLE_DATADESC( EventQueuePrioritizedEvent_t )
  939. DEFINE_FIELD( m_flFireTime, FIELD_TIME ),
  940. DEFINE_FIELD( m_iTarget, FIELD_STRING ),
  941. DEFINE_FIELD( m_iTargetInput, FIELD_STRING ),
  942. DEFINE_FIELD( m_pActivator, FIELD_EHANDLE ),
  943. DEFINE_FIELD( m_pCaller, FIELD_EHANDLE ),
  944. DEFINE_FIELD( m_pEntTarget, FIELD_EHANDLE ),
  945. DEFINE_FIELD( m_iOutputID, FIELD_INTEGER ),
  946. DEFINE_CUSTOM_FIELD( m_VariantValue, variantFuncs ),
  947. // DEFINE_FIELD( m_pNext, FIELD_??? ),
  948. // DEFINE_FIELD( m_pPrev, FIELD_??? ),
  949. END_DATADESC()
  950. int CEventQueue::Save( ISave &save )
  951. {
  952. // count the number of items in the queue
  953. EventQueuePrioritizedEvent_t *pe;
  954. m_iListCount = 0;
  955. for ( pe = m_Events.m_pNext; pe != NULL; pe = pe->m_pNext )
  956. {
  957. m_iListCount++;
  958. }
  959. // save that value out to disk, so we know how many to restore
  960. if ( !save.WriteFields( "EventQueue", this, NULL, m_DataMap.dataDesc, m_DataMap.dataNumFields ) )
  961. return 0;
  962. // cycle through all the events, saving them all
  963. for ( pe = m_Events.m_pNext; pe != NULL; pe = pe->m_pNext )
  964. {
  965. if ( !save.WriteFields( "PEvent", pe, NULL, pe->m_DataMap.dataDesc, pe->m_DataMap.dataNumFields ) )
  966. return 0;
  967. }
  968. return 1;
  969. }
  970. int CEventQueue::Restore( IRestore &restore )
  971. {
  972. // clear the event queue
  973. Clear();
  974. // rebuild the event queue by restoring all the queue items
  975. EventQueuePrioritizedEvent_t tmpEvent;
  976. // load the number of items saved
  977. if ( !restore.ReadFields( "EventQueue", this, NULL, m_DataMap.dataDesc, m_DataMap.dataNumFields ) )
  978. return 0;
  979. for ( int i = 0; i < m_iListCount; i++ )
  980. {
  981. if ( !restore.ReadFields( "PEvent", &tmpEvent, NULL, tmpEvent.m_DataMap.dataDesc, tmpEvent.m_DataMap.dataNumFields ) )
  982. return 0;
  983. // add the restored event into the list
  984. if ( tmpEvent.m_pEntTarget )
  985. {
  986. AddEvent( tmpEvent.m_pEntTarget, STRING(tmpEvent.m_iTargetInput), tmpEvent.m_VariantValue, tmpEvent.m_flFireTime - gpGlobals->curtime, tmpEvent.m_pActivator, tmpEvent.m_pCaller, tmpEvent.m_iOutputID );
  987. }
  988. else
  989. {
  990. AddEvent( STRING(tmpEvent.m_iTarget), STRING(tmpEvent.m_iTargetInput), tmpEvent.m_VariantValue, tmpEvent.m_flFireTime - gpGlobals->curtime, tmpEvent.m_pActivator, tmpEvent.m_pCaller, tmpEvent.m_iOutputID );
  991. }
  992. }
  993. return 1;
  994. }
  995. ////////////////////////// variant_t implementation //////////////////////////
  996. // BUGBUG: Add support for function pointer save/restore to variants
  997. // BUGBUG: Must pass datamap_t to read/write fields
  998. void variant_t::Set( fieldtype_t ftype, void *data )
  999. {
  1000. fieldType = ftype;
  1001. switch ( ftype )
  1002. {
  1003. case FIELD_BOOLEAN: bVal = *((bool *)data); break;
  1004. case FIELD_CHARACTER: iVal = *((char *)data); break;
  1005. case FIELD_SHORT: iVal = *((short *)data); break;
  1006. case FIELD_INTEGER: iVal = *((int *)data); break;
  1007. case FIELD_STRING: iszVal = *((string_t *)data); break;
  1008. case FIELD_FLOAT: flVal = *((float *)data); break;
  1009. case FIELD_COLOR32: rgbaVal = *((color32 *)data); break;
  1010. case FIELD_VECTOR:
  1011. case FIELD_POSITION_VECTOR:
  1012. {
  1013. vecVal[0] = ((float *)data)[0];
  1014. vecVal[1] = ((float *)data)[1];
  1015. vecVal[2] = ((float *)data)[2];
  1016. break;
  1017. }
  1018. case FIELD_EHANDLE: eVal = *((EHANDLE *)data); break;
  1019. case FIELD_CLASSPTR: eVal = *((CBaseEntity **)data); break;
  1020. case FIELD_VOID:
  1021. default:
  1022. iVal = 0; fieldType = FIELD_VOID;
  1023. break;
  1024. }
  1025. }
  1026. //-----------------------------------------------------------------------------
  1027. // Purpose: Copies the value in the variant into a block of memory
  1028. // Input : *data - the block to write into
  1029. //-----------------------------------------------------------------------------
  1030. void variant_t::SetOther( void *data )
  1031. {
  1032. switch ( fieldType )
  1033. {
  1034. case FIELD_BOOLEAN: *((bool *)data) = bVal != 0; break;
  1035. case FIELD_CHARACTER: *((char *)data) = iVal; break;
  1036. case FIELD_SHORT: *((short *)data) = iVal; break;
  1037. case FIELD_INTEGER: *((int *)data) = iVal; break;
  1038. case FIELD_STRING: *((string_t *)data) = iszVal; break;
  1039. case FIELD_FLOAT: *((float *)data) = flVal; break;
  1040. case FIELD_COLOR32: *((color32 *)data) = rgbaVal; break;
  1041. case FIELD_VECTOR:
  1042. case FIELD_POSITION_VECTOR:
  1043. {
  1044. ((float *)data)[0] = vecVal[0];
  1045. ((float *)data)[1] = vecVal[1];
  1046. ((float *)data)[2] = vecVal[2];
  1047. break;
  1048. }
  1049. case FIELD_EHANDLE: *((EHANDLE *)data) = eVal; break;
  1050. case FIELD_CLASSPTR: *((CBaseEntity **)data) = eVal; break;
  1051. }
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. // Purpose: Converts the variant to a new type. This function defines which I/O
  1055. // types can be automatically converted between. Connections that require
  1056. // an unsupported conversion will cause an error message at runtime.
  1057. // Input : newType - the type to convert to
  1058. // Output : Returns true on success, false if the conversion is not legal
  1059. //-----------------------------------------------------------------------------
  1060. bool variant_t::Convert( fieldtype_t newType )
  1061. {
  1062. if ( newType == fieldType )
  1063. {
  1064. return true;
  1065. }
  1066. //
  1067. // Converting to a null value is easy.
  1068. //
  1069. if ( newType == FIELD_VOID )
  1070. {
  1071. Set( FIELD_VOID, NULL );
  1072. return true;
  1073. }
  1074. //
  1075. // FIELD_INPUT accepts the variant type directly.
  1076. //
  1077. if ( newType == FIELD_INPUT )
  1078. {
  1079. return true;
  1080. }
  1081. switch ( fieldType )
  1082. {
  1083. case FIELD_INTEGER:
  1084. {
  1085. switch ( newType )
  1086. {
  1087. case FIELD_FLOAT:
  1088. {
  1089. SetFloat( (float) iVal );
  1090. return true;
  1091. }
  1092. case FIELD_BOOLEAN:
  1093. {
  1094. SetBool( iVal != 0 );
  1095. return true;
  1096. }
  1097. }
  1098. break;
  1099. }
  1100. case FIELD_FLOAT:
  1101. {
  1102. switch ( newType )
  1103. {
  1104. case FIELD_INTEGER:
  1105. {
  1106. SetInt( (int) flVal );
  1107. return true;
  1108. }
  1109. case FIELD_BOOLEAN:
  1110. {
  1111. SetBool( flVal != 0 );
  1112. return true;
  1113. }
  1114. }
  1115. break;
  1116. }
  1117. //
  1118. // Everyone must convert from FIELD_STRING if possible, since
  1119. // parameter overrides are always passed as strings.
  1120. //
  1121. case FIELD_STRING:
  1122. {
  1123. switch ( newType )
  1124. {
  1125. case FIELD_INTEGER:
  1126. {
  1127. if (iszVal != NULL_STRING)
  1128. {
  1129. SetInt(atoi(STRING(iszVal)));
  1130. }
  1131. else
  1132. {
  1133. SetInt(0);
  1134. }
  1135. return true;
  1136. }
  1137. case FIELD_FLOAT:
  1138. {
  1139. if (iszVal != NULL_STRING)
  1140. {
  1141. SetFloat(atof(STRING(iszVal)));
  1142. }
  1143. else
  1144. {
  1145. SetFloat(0);
  1146. }
  1147. return true;
  1148. }
  1149. case FIELD_BOOLEAN:
  1150. {
  1151. if (iszVal != NULL_STRING)
  1152. {
  1153. SetBool( atoi(STRING(iszVal)) != 0 );
  1154. }
  1155. else
  1156. {
  1157. SetBool(false);
  1158. }
  1159. return true;
  1160. }
  1161. case FIELD_VECTOR:
  1162. {
  1163. Vector tmpVec = vec3_origin;
  1164. if (sscanf(STRING(iszVal), "[%f %f %f]", &tmpVec[0], &tmpVec[1], &tmpVec[2]) == 0)
  1165. {
  1166. // Try sucking out 3 floats with no []s
  1167. sscanf(STRING(iszVal), "%f %f %f", &tmpVec[0], &tmpVec[1], &tmpVec[2]);
  1168. }
  1169. SetVector3D( tmpVec );
  1170. return true;
  1171. }
  1172. case FIELD_COLOR32:
  1173. {
  1174. int nRed = 0;
  1175. int nGreen = 0;
  1176. int nBlue = 0;
  1177. int nAlpha = 255;
  1178. sscanf(STRING(iszVal), "%d %d %d %d", &nRed, &nGreen, &nBlue, &nAlpha);
  1179. SetColor32( nRed, nGreen, nBlue, nAlpha );
  1180. return true;
  1181. }
  1182. case FIELD_EHANDLE:
  1183. {
  1184. // convert the string to an entity by locating it by classname
  1185. CBaseEntity *ent = NULL;
  1186. if ( iszVal != NULL_STRING )
  1187. {
  1188. // FIXME: do we need to pass an activator in here?
  1189. ent = gEntList.FindEntityByName( NULL, iszVal );
  1190. }
  1191. SetEntity( ent );
  1192. return true;
  1193. }
  1194. }
  1195. break;
  1196. }
  1197. case FIELD_EHANDLE:
  1198. {
  1199. switch ( newType )
  1200. {
  1201. case FIELD_STRING:
  1202. {
  1203. // take the entities targetname as the string
  1204. string_t iszStr = NULL_STRING;
  1205. if ( eVal != NULL )
  1206. {
  1207. SetString( eVal->GetEntityName() );
  1208. }
  1209. return true;
  1210. }
  1211. }
  1212. break;
  1213. }
  1214. }
  1215. // invalid conversion
  1216. return false;
  1217. }
  1218. //-----------------------------------------------------------------------------
  1219. // Purpose: All types must be able to display as strings for debugging purposes.
  1220. // Output : Returns a pointer to the string that represents this value.
  1221. //
  1222. // NOTE: The returned pointer should not be stored by the caller as
  1223. // subsequent calls to this function will overwrite the contents
  1224. // of the buffer!
  1225. //-----------------------------------------------------------------------------
  1226. const char *variant_t::ToString( void ) const
  1227. {
  1228. static char szBuf[512];
  1229. switch (fieldType)
  1230. {
  1231. case FIELD_STRING:
  1232. {
  1233. return(STRING(iszVal));
  1234. }
  1235. case FIELD_BOOLEAN:
  1236. {
  1237. if (bVal == 0)
  1238. {
  1239. Q_strncpy(szBuf, "false",sizeof(szBuf));
  1240. }
  1241. else
  1242. {
  1243. Q_strncpy(szBuf, "true",sizeof(szBuf));
  1244. }
  1245. return(szBuf);
  1246. }
  1247. case FIELD_INTEGER:
  1248. {
  1249. Q_snprintf( szBuf, sizeof( szBuf ), "%i", iVal );
  1250. return(szBuf);
  1251. }
  1252. case FIELD_FLOAT:
  1253. {
  1254. Q_snprintf(szBuf,sizeof(szBuf), "%g", flVal);
  1255. return(szBuf);
  1256. }
  1257. case FIELD_COLOR32:
  1258. {
  1259. Q_snprintf(szBuf,sizeof(szBuf), "%d %d %d %d", (int)rgbaVal.r, (int)rgbaVal.g, (int)rgbaVal.b, (int)rgbaVal.a);
  1260. return(szBuf);
  1261. }
  1262. case FIELD_VECTOR:
  1263. {
  1264. Q_snprintf(szBuf,sizeof(szBuf), "[%g %g %g]", (double)vecVal[0], (double)vecVal[1], (double)vecVal[2]);
  1265. return(szBuf);
  1266. }
  1267. case FIELD_VOID:
  1268. {
  1269. szBuf[0] = '\0';
  1270. return(szBuf);
  1271. }
  1272. case FIELD_EHANDLE:
  1273. {
  1274. const char *pszName = (Entity()) ? STRING(Entity()->GetEntityName()) : "<<null entity>>";
  1275. Q_strncpy( szBuf, pszName, 512 );
  1276. return (szBuf);
  1277. }
  1278. }
  1279. return("No conversion to string");
  1280. }
  1281. #define classNameTypedef variant_t // to satisfy DEFINE... macros
  1282. typedescription_t variant_t::m_SaveBool[] =
  1283. {
  1284. DEFINE_FIELD( bVal, FIELD_BOOLEAN ),
  1285. };
  1286. typedescription_t variant_t::m_SaveInt[] =
  1287. {
  1288. DEFINE_FIELD( iVal, FIELD_INTEGER ),
  1289. };
  1290. typedescription_t variant_t::m_SaveFloat[] =
  1291. {
  1292. DEFINE_FIELD( flVal, FIELD_FLOAT ),
  1293. };
  1294. typedescription_t variant_t::m_SaveEHandle[] =
  1295. {
  1296. DEFINE_FIELD( eVal, FIELD_EHANDLE ),
  1297. };
  1298. typedescription_t variant_t::m_SaveString[] =
  1299. {
  1300. DEFINE_FIELD( iszVal, FIELD_STRING ),
  1301. };
  1302. typedescription_t variant_t::m_SaveColor[] =
  1303. {
  1304. DEFINE_FIELD( rgbaVal, FIELD_COLOR32 ),
  1305. };
  1306. #undef classNameTypedef
  1307. //
  1308. // Struct for saving and restoring vector variants, since they are
  1309. // stored as float[3] and we want to take advantage of position vector
  1310. // fixup across level transitions.
  1311. //
  1312. #define classNameTypedef variant_savevector_t // to satisfy DEFINE... macros
  1313. struct variant_savevector_t
  1314. {
  1315. Vector vecSave;
  1316. };
  1317. typedescription_t variant_t::m_SaveVector[] =
  1318. {
  1319. // Just here to shut up ClassCheck
  1320. // DEFINE_ARRAY( vecVal, FIELD_FLOAT, 3 ),
  1321. // DEFINE_FIELD( vecSave, FIELD_CLASSCHECK_IGNORE ) // do this or else we get a warning about multiply-defined fields
  1322. DEFINE_FIELD( vecSave, FIELD_VECTOR ),
  1323. };
  1324. typedescription_t variant_t::m_SavePositionVector[] =
  1325. {
  1326. DEFINE_FIELD( vecSave, FIELD_POSITION_VECTOR ),
  1327. };
  1328. #undef classNameTypedef
  1329. #define classNameTypedef variant_savevmatrix_t // to satisfy DEFINE... macros
  1330. struct variant_savevmatrix_t
  1331. {
  1332. VMatrix matSave;
  1333. };
  1334. typedescription_t variant_t::m_SaveVMatrix[] =
  1335. {
  1336. // DEFINE_FIELD( matSave, FIELD_CLASSCHECK_IGNORE ) // do this or else we get a warning about multiply-defined fields
  1337. DEFINE_FIELD( matSave, FIELD_VMATRIX ),
  1338. };
  1339. typedescription_t variant_t::m_SaveVMatrixWorldspace[] =
  1340. {
  1341. DEFINE_FIELD( matSave, FIELD_VMATRIX_WORLDSPACE ),
  1342. };
  1343. #undef classNameTypedef
  1344. #define classNameTypedef variant_savevmatrix3x4_t // to satisfy DEFINE... macros
  1345. struct variant_savevmatrix3x4_t
  1346. {
  1347. matrix3x4_t matSave;
  1348. };
  1349. typedescription_t variant_t::m_SaveMatrix3x4Worldspace[] =
  1350. {
  1351. DEFINE_FIELD( matSave, FIELD_MATRIX3X4_WORLDSPACE ),
  1352. };
  1353. #undef classNameTypedef
  1354. class CVariantSaveDataOps : public CDefSaveRestoreOps
  1355. {
  1356. // saves the entire array of variables
  1357. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  1358. {
  1359. variant_t *var = (variant_t*)fieldInfo.pField;
  1360. int type = var->FieldType();
  1361. pSave->WriteInt( &type, 1 );
  1362. switch ( var->FieldType() )
  1363. {
  1364. case FIELD_VOID:
  1365. break;
  1366. case FIELD_BOOLEAN:
  1367. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveBool, 1 );
  1368. break;
  1369. case FIELD_INTEGER:
  1370. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveInt, 1 );
  1371. break;
  1372. case FIELD_FLOAT:
  1373. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveFloat, 1 );
  1374. break;
  1375. case FIELD_EHANDLE:
  1376. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveEHandle, 1 );
  1377. break;
  1378. case FIELD_STRING:
  1379. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveString, 1 );
  1380. break;
  1381. case FIELD_COLOR32:
  1382. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveColor, 1 );
  1383. break;
  1384. case FIELD_VECTOR:
  1385. {
  1386. variant_savevector_t Temp;
  1387. var->Vector3D(Temp.vecSave);
  1388. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, &Temp, NULL, variant_t::m_SaveVector, 1 );
  1389. break;
  1390. }
  1391. case FIELD_POSITION_VECTOR:
  1392. {
  1393. variant_savevector_t Temp;
  1394. var->Vector3D(Temp.vecSave);
  1395. pSave->WriteFields( fieldInfo.pTypeDesc->fieldName, &Temp, NULL, variant_t::m_SavePositionVector, 1 );
  1396. break;
  1397. }
  1398. default:
  1399. Warning( "Bad type %d in saved variant_t\n", var->FieldType() );
  1400. Assert(0);
  1401. }
  1402. }
  1403. // restores a single instance of the variable
  1404. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  1405. {
  1406. variant_t *var = (variant_t*)fieldInfo.pField;
  1407. *var = variant_t();
  1408. var->fieldType = (_fieldtypes)pRestore->ReadInt();
  1409. switch ( var->fieldType )
  1410. {
  1411. case FIELD_VOID:
  1412. break;
  1413. case FIELD_BOOLEAN:
  1414. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveBool, 1 );
  1415. break;
  1416. case FIELD_INTEGER:
  1417. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveInt, 1 );
  1418. break;
  1419. case FIELD_FLOAT:
  1420. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveFloat, 1 );
  1421. break;
  1422. case FIELD_EHANDLE:
  1423. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveEHandle, 1 );
  1424. break;
  1425. case FIELD_STRING:
  1426. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveString, 1 );
  1427. break;
  1428. case FIELD_COLOR32:
  1429. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, var, NULL, variant_t::m_SaveColor, 1 );
  1430. break;
  1431. case FIELD_VECTOR:
  1432. {
  1433. variant_savevector_t Temp;
  1434. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, &Temp, NULL, variant_t::m_SaveVector, 1 );
  1435. var->SetVector3D(Temp.vecSave);
  1436. break;
  1437. }
  1438. case FIELD_POSITION_VECTOR:
  1439. {
  1440. variant_savevector_t Temp;
  1441. pRestore->ReadFields( fieldInfo.pTypeDesc->fieldName, &Temp, NULL, variant_t::m_SavePositionVector, 1 );
  1442. var->SetPositionVector3D(Temp.vecSave);
  1443. break;
  1444. }
  1445. default:
  1446. Warning( "Bad type %d in saved variant_t\n", var->FieldType() );
  1447. Assert(0);
  1448. break;
  1449. }
  1450. }
  1451. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  1452. {
  1453. // check all the elements of the array (usually only 1)
  1454. variant_t *var = (variant_t*)fieldInfo.pField;
  1455. for ( int i = 0; i < fieldInfo.pTypeDesc->fieldSize; i++, var++ )
  1456. {
  1457. if ( var->FieldType() != FIELD_VOID )
  1458. return 0;
  1459. }
  1460. // variant has no data
  1461. return 1;
  1462. }
  1463. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  1464. {
  1465. // Don't no how to. This is okay, since objects of this type
  1466. // are always born clean before restore, and not reused
  1467. }
  1468. };
  1469. CVariantSaveDataOps g_VariantSaveDataOps;
  1470. ISaveRestoreOps *variantFuncs = &g_VariantSaveDataOps;
  1471. /////////////////////// entitylist /////////////////////
  1472. CUtlMemoryPool g_EntListMemPool( sizeof(entitem_t), 256, CUtlMemoryPool::GROW_NONE, "g_EntListMemPool" );
  1473. #include "tier0/memdbgoff.h"
  1474. void *entitem_t::operator new( size_t stAllocateBlock )
  1475. {
  1476. return g_EntListMemPool.Alloc( stAllocateBlock );
  1477. }
  1478. void *entitem_t::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
  1479. {
  1480. return g_EntListMemPool.Alloc( stAllocateBlock );
  1481. }
  1482. void entitem_t::operator delete( void *pMem )
  1483. {
  1484. g_EntListMemPool.Free( pMem );
  1485. }
  1486. #include "tier0/memdbgon.h"
  1487. CEntityList::CEntityList()
  1488. {
  1489. m_pItemList = NULL;
  1490. m_iNumItems = 0;
  1491. }
  1492. CEntityList::~CEntityList()
  1493. {
  1494. // remove all items from the list
  1495. entitem_t *next, *e = m_pItemList;
  1496. while ( e != NULL )
  1497. {
  1498. next = e->pNext;
  1499. delete e;
  1500. e = next;
  1501. }
  1502. m_pItemList = NULL;
  1503. }
  1504. void CEntityList::AddEntity( CBaseEntity *pEnt )
  1505. {
  1506. // check if it's already in the list; if not, add it
  1507. entitem_t *e = m_pItemList;
  1508. while ( e != NULL )
  1509. {
  1510. if ( e->hEnt == pEnt )
  1511. {
  1512. // it's already in the list
  1513. return;
  1514. }
  1515. if ( e->pNext == NULL )
  1516. {
  1517. // we've hit the end of the list, so tack it on
  1518. e->pNext = new entitem_t;
  1519. e->pNext->hEnt = pEnt;
  1520. e->pNext->pNext = NULL;
  1521. m_iNumItems++;
  1522. return;
  1523. }
  1524. e = e->pNext;
  1525. }
  1526. // empty list
  1527. m_pItemList = new entitem_t;
  1528. m_pItemList->hEnt = pEnt;
  1529. m_pItemList->pNext = NULL;
  1530. m_iNumItems = 1;
  1531. }
  1532. void CEntityList::DeleteEntity( CBaseEntity *pEnt )
  1533. {
  1534. // find the entry in the list and delete it
  1535. entitem_t *prev = NULL, *e = m_pItemList;
  1536. while ( e != NULL )
  1537. {
  1538. // delete the link if it's the matching entity OR if the link is NULL
  1539. if ( e->hEnt == pEnt || e->hEnt == NULL )
  1540. {
  1541. if ( prev )
  1542. {
  1543. prev->pNext = e->pNext;
  1544. }
  1545. else
  1546. {
  1547. m_pItemList = e->pNext;
  1548. }
  1549. delete e;
  1550. m_iNumItems--;
  1551. // REVISIT: Is this correct? Is this just here to clean out dead EHANDLEs?
  1552. // restart the loop
  1553. e = m_pItemList;
  1554. prev = NULL;
  1555. continue;
  1556. }
  1557. prev = e;
  1558. e = e->pNext;
  1559. }
  1560. }