TaskManager.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. // Task Manager
  2. //
  3. // -- jweier
  4. // this include must remain at the top of every Icarus CPP file
  5. #include "stdafx.h"
  6. #include "IcarusImplementation.h"
  7. #include "BlockStream.h"
  8. #include "Sequence.h"
  9. #include "TaskManager.h"
  10. #include "Sequencer.h"
  11. #define ICARUS_VALIDATE(a) if ( a == false ) return TASK_FAILED;
  12. #define STL_ITERATE( a, b ) for ( a = b.begin(); a != b.end(); a++ )
  13. #define STL_INSERT( a, b ) a.insert( a.end(), b );
  14. /*
  15. =================================================
  16. CTask
  17. =================================================
  18. */
  19. CTask::CTask( void )
  20. {
  21. }
  22. CTask::~CTask( void )
  23. {
  24. }
  25. CTask *CTask::Create( int GUID, CBlock *block )
  26. {
  27. CTask *task = new CTask;
  28. //TODO: Emit warning
  29. if ( task == NULL )
  30. return NULL;
  31. task->SetTimeStamp( 0 );
  32. task->SetBlock( block );
  33. task->SetGUID( GUID );
  34. return task;
  35. }
  36. /*
  37. -------------------------
  38. Free
  39. -------------------------
  40. */
  41. void CTask::Free( void )
  42. {
  43. //NOTENOTE: The block is not consumed by the task, it is the sequencer's job to clean blocks up
  44. delete this;
  45. }
  46. /*
  47. =================================================
  48. CTaskGroup
  49. =================================================
  50. */
  51. CTaskGroup::CTaskGroup( void )
  52. {
  53. Init();
  54. m_GUID = 0;
  55. m_parent = NULL;
  56. }
  57. CTaskGroup::~CTaskGroup( void )
  58. {
  59. m_completedTasks.clear();
  60. }
  61. /*
  62. -------------------------
  63. SetGUID
  64. -------------------------
  65. */
  66. void CTaskGroup::SetGUID( int GUID )
  67. {
  68. m_GUID = GUID;
  69. }
  70. /*
  71. -------------------------
  72. Init
  73. -------------------------
  74. */
  75. void CTaskGroup::Init( void )
  76. {
  77. m_completedTasks.clear();
  78. m_numCompleted = 0;
  79. m_parent = NULL;
  80. }
  81. /*
  82. -------------------------
  83. Add
  84. -------------------------
  85. */
  86. int CTaskGroup::Add( CTask *task )
  87. {
  88. m_completedTasks[ task->GetGUID() ] = false;
  89. return TASK_OK;
  90. }
  91. /*
  92. -------------------------
  93. MarkTaskComplete
  94. -------------------------
  95. */
  96. bool CTaskGroup::MarkTaskComplete( int id )
  97. {
  98. if ( (m_completedTasks.find( id )) != m_completedTasks.end() )
  99. {
  100. m_completedTasks[ id ] = true;
  101. m_numCompleted++;
  102. return true;
  103. }
  104. return false;
  105. }
  106. /*
  107. =================================================
  108. CTaskManager
  109. =================================================
  110. */
  111. CTaskManager::CTaskManager( void )
  112. {
  113. static int uniqueID = 0;
  114. m_id = uniqueID++;
  115. }
  116. CTaskManager::~CTaskManager( void )
  117. {
  118. }
  119. /*
  120. -------------------------
  121. Create
  122. -------------------------
  123. */
  124. CTaskManager *CTaskManager::Create( void )
  125. {
  126. return new CTaskManager;
  127. }
  128. /*
  129. -------------------------
  130. Init
  131. -------------------------
  132. */
  133. int CTaskManager::Init( CSequencer *owner )
  134. {
  135. //TODO: Emit warning
  136. if ( owner == NULL )
  137. return TASK_FAILED;
  138. m_tasks.clear();
  139. m_owner = owner;
  140. m_ownerID = owner->GetOwnerID();
  141. m_curGroup = NULL;
  142. m_GUID = 0;
  143. m_resident = false;
  144. return TASK_OK;
  145. }
  146. /*
  147. -------------------------
  148. Free
  149. -------------------------
  150. */
  151. int CTaskManager::Free( void )
  152. {
  153. taskGroup_v::iterator gi;
  154. tasks_l::iterator ti;
  155. assert(!m_resident); //don't free me, i'm currently running!
  156. //Clear out all pending tasks
  157. for ( ti = m_tasks.begin(); ti != m_tasks.end(); ti++ )
  158. {
  159. (*ti)->Free();
  160. }
  161. m_tasks.clear();
  162. //Clear out all taskGroups
  163. for ( gi = m_taskGroups.begin(); gi != m_taskGroups.end(); gi++ )
  164. {
  165. delete (*gi);
  166. }
  167. m_taskGroups.clear();
  168. m_taskGroupNameMap.clear();
  169. m_taskGroupIDMap.clear();
  170. return TASK_OK;
  171. }
  172. /*
  173. -------------------------
  174. Flush
  175. -------------------------
  176. */
  177. int CTaskManager::Flush( void )
  178. {
  179. //FIXME: Rewrite
  180. return true;
  181. }
  182. /*
  183. -------------------------
  184. AddTaskGroup
  185. -------------------------
  186. */
  187. CTaskGroup *CTaskManager::AddTaskGroup( const char *name, CIcarus* icarus )
  188. {
  189. CTaskGroup *group;
  190. //Collect any garbage
  191. taskGroupName_m::iterator tgni;
  192. tgni = m_taskGroupNameMap.find( name );
  193. if ( tgni != m_taskGroupNameMap.end() )
  194. {
  195. group = (*tgni).second;
  196. //Clear it and just move on
  197. group->Init();
  198. return group;
  199. }
  200. //Allocate a new one
  201. group = new CTaskGroup;
  202. //TODO: Emit warning
  203. assert( group );
  204. if ( group == NULL )
  205. {
  206. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Unable to allocate task group \"%s\"\n", name );
  207. return NULL;
  208. }
  209. //Setup the internal information
  210. group->SetGUID( m_GUID++ );
  211. //Add it to the list and associate it for retrieval later
  212. m_taskGroups.insert( m_taskGroups.end(), group );
  213. m_taskGroupNameMap[ name ] = group;
  214. m_taskGroupIDMap[ group->GetGUID() ] = group;
  215. return group;
  216. }
  217. /*
  218. -------------------------
  219. GetTaskGroup
  220. -------------------------
  221. */
  222. CTaskGroup *CTaskManager::GetTaskGroup( const char *name, CIcarus* icarus )
  223. {
  224. taskGroupName_m::iterator tgi;
  225. tgi = m_taskGroupNameMap.find( name );
  226. if ( tgi == m_taskGroupNameMap.end() )
  227. {
  228. icarus->GetGame()->DebugPrint(IGameInterface::WL_WARNING, "Could not find task group \"%s\"\n", name );
  229. return NULL;
  230. }
  231. return (*tgi).second;
  232. }
  233. CTaskGroup *CTaskManager::GetTaskGroup( int id, CIcarus* icarus )
  234. {
  235. taskGroupID_m::iterator tgi;
  236. tgi = m_taskGroupIDMap.find( id );
  237. if ( tgi == m_taskGroupIDMap.end() )
  238. {
  239. icarus->GetGame()->DebugPrint(IGameInterface::WL_WARNING, "Could not find task group \"%d\"\n", id );
  240. return NULL;
  241. }
  242. return (*tgi).second;
  243. }
  244. /*
  245. -------------------------
  246. Update
  247. -------------------------
  248. */
  249. int CTaskManager::Update( CIcarus* icarus )
  250. {
  251. if ( icarus->GetGame()->IsFrozen(m_ownerID) )
  252. {
  253. return TASK_FAILED;
  254. }
  255. m_count = 0; //Needed for runaway init
  256. m_resident = true;
  257. int returnVal = Go(icarus);
  258. m_resident = false;
  259. return returnVal;
  260. }
  261. /*
  262. -------------------------
  263. Check
  264. -------------------------
  265. */
  266. inline bool CTaskManager::Check( int targetID, CBlock *block, int memberNum ) const
  267. {
  268. if ( (block->GetMember( memberNum ))->GetID() == targetID )
  269. return true;
  270. return false;
  271. }
  272. /*
  273. -------------------------
  274. GetFloat
  275. -------------------------
  276. */
  277. int CTaskManager::GetFloat( int entID, CBlock *block, int &memberNum, float &value, CIcarus* icarus )
  278. {
  279. char *name;
  280. int type;
  281. //See if this is a get() command replacement
  282. if ( Check( CIcarus::ID_GET, block, memberNum ) )
  283. {
  284. //Update the member past the header id
  285. memberNum++;
  286. //get( TYPE, NAME )
  287. type = (int) (*(float *) block->GetMemberData( memberNum++ ));
  288. name = (char *) block->GetMemberData( memberNum++ );
  289. //TODO: Emit warning
  290. if ( type != CIcarus::TK_FLOAT )
  291. {
  292. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Get() call tried to return a non-FLOAT parameter!\n" );
  293. return false;
  294. }
  295. return icarus->GetGame()->GetFloat( entID, name, &value );
  296. }
  297. //Look for a random() inline call
  298. if ( Check( CIcarus::ID_RANDOM, block, memberNum ) )
  299. {
  300. float min, max;
  301. memberNum++;
  302. min = *(float *) block->GetMemberData( memberNum++ );
  303. max = *(float *) block->GetMemberData( memberNum++ );
  304. value = icarus->GetGame()->Random( min, max );
  305. return true;
  306. }
  307. //Look for a tag() inline call
  308. if ( Check( CIcarus::ID_TAG, block, memberNum ) )
  309. {
  310. icarus->GetGame()->DebugPrint(IGameInterface::WL_WARNING, "Invalid use of \"tag\" inline. Not a valid replacement for type FLOAT\n" );
  311. return false;
  312. }
  313. CBlockMember *bm = block->GetMember( memberNum );
  314. if ( bm->GetID() == CIcarus::TK_INT )
  315. {
  316. value = (float) (*(int *) block->GetMemberData( memberNum++ ));
  317. }
  318. else if ( bm->GetID() == CIcarus::TK_FLOAT )
  319. {
  320. value = *(float *) block->GetMemberData( memberNum++ );
  321. }
  322. else
  323. {
  324. assert(0);
  325. icarus->GetGame()->DebugPrint(IGameInterface::WL_WARNING, "Unexpected value; expected type FLOAT\n" );
  326. return false;
  327. }
  328. return true;
  329. }
  330. /*
  331. -------------------------
  332. GetVector
  333. -------------------------
  334. */
  335. int CTaskManager::GetVector( int entID, CBlock *block, int &memberNum, vec3_t &value, CIcarus* icarus )
  336. {
  337. char *name;
  338. int type, i;
  339. //See if this is a get() command replacement
  340. if ( Check( CIcarus::ID_GET, block, memberNum ) )
  341. {
  342. //Update the member past the header id
  343. memberNum++;
  344. //get( TYPE, NAME )
  345. type = (int) (*(float *) block->GetMemberData( memberNum++ ));
  346. name = (char *) block->GetMemberData( memberNum++ );
  347. //TODO: Emit warning
  348. if ( type != CIcarus::TK_VECTOR )
  349. {
  350. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Get() call tried to return a non-VECTOR parameter!\n" );
  351. }
  352. return icarus->GetGame()->GetVector( entID, name, value );
  353. }
  354. //Look for a random() inline call
  355. if ( Check( CIcarus::ID_RANDOM, block, memberNum ) )
  356. {
  357. float min, max;
  358. memberNum++;
  359. min = *(float *) block->GetMemberData( memberNum++ );
  360. max = *(float *) block->GetMemberData( memberNum++ );
  361. for ( i = 0; i < 3; i++ )
  362. {
  363. value[i] = (float) icarus->GetGame()->Random( min, max ); //FIXME: Just truncating it for now.. should be fine though
  364. }
  365. return true;
  366. }
  367. //Look for a tag() inline call
  368. if ( Check( CIcarus::ID_TAG, block, memberNum ) )
  369. {
  370. char *tagName;
  371. float tagLookup;
  372. memberNum++;
  373. ICARUS_VALIDATE ( Get( entID, block, memberNum, &tagName, icarus ) );
  374. ICARUS_VALIDATE ( GetFloat( entID, block, memberNum, tagLookup, icarus ) );
  375. if ( icarus->GetGame()->GetTag( entID, tagName, (int) tagLookup, value ) == false)
  376. {
  377. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Unable to find tag \"%s\"!\n", tagName );
  378. assert(0&&"Unable to find tag");
  379. return TASK_FAILED;
  380. }
  381. return true;
  382. }
  383. //Check for a real vector here
  384. type = (int) (*(float *) block->GetMemberData( memberNum ));
  385. if ( type != CIcarus::TK_VECTOR )
  386. {
  387. // icarus->GetGame()->DPrintf( WL_WARNING, "Unexpected value; expected type VECTOR\n" );
  388. return false;
  389. }
  390. memberNum++;
  391. for ( i = 0; i < 3; i++ )
  392. {
  393. if ( GetFloat( entID, block, memberNum, value[i], icarus ) == false )
  394. return false;
  395. }
  396. return true;
  397. }
  398. /*
  399. -------------------------
  400. Get
  401. -------------------------
  402. */
  403. int CTaskManager::GetID()
  404. {
  405. return m_id;
  406. }
  407. int CTaskManager::Get( int entID, CBlock *block, int &memberNum, char **value, CIcarus* icarus )
  408. {
  409. static char tempBuffer[128]; //FIXME: EEEK!
  410. vec3_t vector;
  411. char *name, *tagName;
  412. float tagLookup;
  413. int type;
  414. //Look for a get() inline call
  415. if ( Check( CIcarus::ID_GET, block, memberNum ) )
  416. {
  417. //Update the member past the header id
  418. memberNum++;
  419. //get( TYPE, NAME )
  420. type = (int) (*(float *) block->GetMemberData( memberNum++ ));
  421. name = (char *) block->GetMemberData( memberNum++ );
  422. //Format the return properly
  423. //FIXME: This is probably doing double formatting in certain cases...
  424. //FIXME: STRING MANAGEMENT NEEDS TO BE IMPLEMENTED, MY CURRENT SOLUTION IS NOT ACCEPTABLE!!
  425. switch ( type )
  426. {
  427. case CIcarus::TK_STRING:
  428. if ( icarus->GetGame()->GetString( entID, name, value ) == false )
  429. {
  430. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Get() parameter \"%s\" could not be found!\n", name );
  431. return false;
  432. }
  433. return true;
  434. break;
  435. case CIcarus::TK_FLOAT:
  436. {
  437. float temp;
  438. if ( icarus->GetGame()->GetFloat( entID, name, &temp ) == false )
  439. {
  440. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Get() parameter \"%s\" could not be found!\n", name );
  441. return false;
  442. }
  443. sprintf( (char *) tempBuffer, "%f", temp );
  444. *value = (char *) tempBuffer;
  445. }
  446. return true;
  447. break;
  448. case CIcarus::TK_VECTOR:
  449. {
  450. vec3_t vval;
  451. if ( icarus->GetGame()->GetVector( entID, name, vval ) == false )
  452. {
  453. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Get() parameter \"%s\" could not be found!\n", name );
  454. return false;
  455. }
  456. sprintf( (char *) tempBuffer, "%f %f %f", vval[0], vval[1], vval[2] );
  457. *value = (char *) tempBuffer;
  458. }
  459. return true;
  460. break;
  461. default:
  462. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Get() call tried to return an unknown type!\n" );
  463. return false;
  464. break;
  465. }
  466. }
  467. //Look for a random() inline call
  468. if ( Check( CIcarus::ID_RANDOM, block, memberNum ) )
  469. {
  470. float min, max, ret;
  471. memberNum++;
  472. min = *(float *) block->GetMemberData( memberNum++ );
  473. max = *(float *) block->GetMemberData( memberNum++ );
  474. ret = icarus->GetGame()->Random( min, max );
  475. sprintf( (char *) tempBuffer, "%f", ret );
  476. *value = (char *) tempBuffer;
  477. return true;
  478. }
  479. //Look for a tag() inline call
  480. if ( Check( CIcarus::ID_TAG, block, memberNum ) )
  481. {
  482. memberNum++;
  483. ICARUS_VALIDATE ( Get( entID, block, memberNum, &tagName, icarus ) );
  484. ICARUS_VALIDATE ( GetFloat( entID, block, memberNum, tagLookup, icarus ) );
  485. if (icarus->GetGame()->GetTag( entID, tagName, (int) tagLookup, vector ) == false)
  486. {
  487. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Unable to find tag \"%s\"!\n", tagName );
  488. assert(0 && "Unable to find tag");
  489. return false;
  490. }
  491. sprintf( (char *) tempBuffer, "%f %f %f", vector[0], vector[1], vector[2] );
  492. *value = (char *) tempBuffer;
  493. return true;
  494. }
  495. //Get an actual piece of data
  496. CBlockMember *bm = block->GetMember( memberNum );
  497. if ( bm->GetID() == CIcarus::TK_INT )
  498. {
  499. float fval = (float) (*(int *) block->GetMemberData( memberNum++ ));
  500. sprintf( (char *) tempBuffer, "%f", fval );
  501. *value = (char *) tempBuffer;
  502. return true;
  503. }
  504. else if ( bm->GetID() == CIcarus::TK_FLOAT )
  505. {
  506. float fval = *(float *) block->GetMemberData( memberNum++ );
  507. sprintf( (char *) tempBuffer, "%f", fval );
  508. *value = (char *) tempBuffer;
  509. return true;
  510. }
  511. else if ( bm->GetID() == CIcarus::TK_VECTOR )
  512. {
  513. vec3_t vval;
  514. memberNum++;
  515. for ( int i = 0; i < 3; i++ )
  516. {
  517. if ( GetFloat( entID, block, memberNum, vval[i], icarus ) == false )
  518. return false;
  519. sprintf( (char *) tempBuffer, "%f %f %f", vval[0], vval[1], vval[2] );
  520. *value = (char *) tempBuffer;
  521. }
  522. return true;
  523. }
  524. else if ( ( bm->GetID() == CIcarus::TK_STRING ) || ( bm->GetID() == CIcarus::TK_IDENTIFIER ) )
  525. {
  526. *value = (char *) block->GetMemberData( memberNum++ );
  527. return true;
  528. }
  529. //TODO: Emit warning
  530. assert( 0 );
  531. icarus->GetGame()->DebugPrint(IGameInterface::WL_WARNING, "Unexpected value; expected type STRING\n" );
  532. return false;
  533. }
  534. /*
  535. -------------------------
  536. Go
  537. -------------------------
  538. */
  539. int CTaskManager::Go( CIcarus* icarus )
  540. {
  541. CTask *task = NULL;
  542. bool completed = false;
  543. //Check for run away scripts
  544. if ( m_count++ > RUNAWAY_LIMIT )
  545. {
  546. assert(0);
  547. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Runaway loop detected!\n" );
  548. return TASK_FAILED;
  549. }
  550. //If there are tasks to complete, do so
  551. if ( m_tasks.empty() == false )
  552. {
  553. //Get the next task
  554. task = PopTask( CSequence::POP_BACK );
  555. assert( task );
  556. if ( task == NULL )
  557. {
  558. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Invalid task found in Go()!\n" );
  559. return TASK_FAILED;
  560. }
  561. //If this hasn't been stamped, do so
  562. if ( task->GetTimeStamp() == 0 )
  563. task->SetTimeStamp( icarus->GetGame()->GetTime() );
  564. //Switch and call the proper function
  565. switch( task->GetID() )
  566. {
  567. case CIcarus::ID_WAIT:
  568. Wait( task, completed, icarus );
  569. //Push it to consider it again on the next frame if not complete
  570. if ( completed == false )
  571. {
  572. PushTask( task, CSequence::PUSH_BACK );
  573. return TASK_OK;
  574. }
  575. Completed( task->GetGUID() );
  576. break;
  577. case CIcarus::ID_WAITSIGNAL:
  578. WaitSignal( task, completed , icarus);
  579. //Push it to consider it again on the next frame if not complete
  580. if ( completed == false )
  581. {
  582. PushTask( task, CSequence::PUSH_BACK );
  583. return TASK_OK;
  584. }
  585. Completed( task->GetGUID() );
  586. break;
  587. case CIcarus::ID_PRINT: //print( STRING )
  588. Print( task, icarus );
  589. break;
  590. case CIcarus::ID_SOUND: //sound( name )
  591. Sound( task, icarus );
  592. break;
  593. case CIcarus::ID_MOVE: //move ( ORIGIN, ANGLES, DURATION )
  594. Move( task, icarus );
  595. break;
  596. case CIcarus::ID_ROTATE: //rotate( ANGLES, DURATION )
  597. Rotate( task, icarus );
  598. break;
  599. case CIcarus::ID_KILL: //kill( NAME )
  600. Kill( task, icarus );
  601. break;
  602. case CIcarus::ID_REMOVE: //remove( NAME )
  603. Remove( task, icarus );
  604. break;
  605. case CIcarus::ID_CAMERA: //camera( ? )
  606. Camera( task, icarus );
  607. break;
  608. case CIcarus::ID_SET: //set( NAME, ? )
  609. Set( task, icarus );
  610. break;
  611. case CIcarus::ID_USE: //use( NAME )
  612. Use( task, icarus );
  613. break;
  614. case CIcarus::ID_DECLARE://declare( TYPE, NAME )
  615. DeclareVariable( task, icarus );
  616. break;
  617. case CIcarus::ID_FREE: //free( NAME )
  618. FreeVariable( task, icarus );
  619. break;
  620. case CIcarus::ID_SIGNAL: //signal( NAME )
  621. Signal( task, icarus );
  622. break;
  623. case CIcarus::ID_PLAY: //play ( NAME )
  624. Play( task, icarus );
  625. break;
  626. default:
  627. assert(0);
  628. task->Free();
  629. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Found unknown task type!\n" );
  630. return TASK_FAILED;
  631. break;
  632. }
  633. //Pump the sequencer for another task
  634. CallbackCommand( task, TASK_RETURN_COMPLETE , icarus);
  635. task->Free();
  636. }
  637. //FIXME: A command surge limiter could be implemented at this point to be sure a script doesn't
  638. // execute too many commands in one cycle. This may, however, cause timing errors to surface.
  639. return TASK_OK;
  640. }
  641. /*
  642. -------------------------
  643. SetCommand
  644. -------------------------
  645. */
  646. int CTaskManager::SetCommand( CBlock *command, int type, CIcarus* icarus )
  647. {
  648. CTask *task = CTask::Create( m_GUID++, command );
  649. //If this is part of a task group, add it in
  650. if ( m_curGroup )
  651. {
  652. m_curGroup->Add( task );
  653. }
  654. //TODO: Emit warning
  655. assert( task );
  656. if ( task == NULL )
  657. {
  658. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Unable to allocate new task!\n" );
  659. return TASK_FAILED;
  660. }
  661. PushTask( task, type );
  662. return TASK_OK;
  663. }
  664. /*
  665. -------------------------
  666. MarkTask
  667. -------------------------
  668. */
  669. int CTaskManager::MarkTask( int id, int operation, CIcarus* icarus )
  670. {
  671. CTaskGroup *group = GetTaskGroup( id, icarus );
  672. assert( group );
  673. if ( group == NULL )
  674. return TASK_FAILED;
  675. if ( operation == TASK_START )
  676. {
  677. //Reset all the completion information
  678. group->Init();
  679. group->SetParent( m_curGroup );
  680. m_curGroup = group;
  681. }
  682. else if ( operation == TASK_END )
  683. {
  684. assert( m_curGroup );
  685. if ( m_curGroup == NULL )
  686. return TASK_FAILED;
  687. m_curGroup = m_curGroup->GetParent();
  688. }
  689. #ifdef _DEBUG
  690. else
  691. {
  692. assert(0);
  693. }
  694. #endif
  695. return TASK_OK;
  696. }
  697. /*
  698. -------------------------
  699. Completed
  700. -------------------------
  701. */
  702. int CTaskManager::Completed( int id )
  703. {
  704. taskGroup_v::iterator tgi;
  705. //Mark the task as completed
  706. for ( tgi = m_taskGroups.begin(); tgi != m_taskGroups.end(); tgi++ )
  707. {
  708. //If this returns true, then the task was marked properly
  709. if ( (*tgi)->MarkTaskComplete( id ) )
  710. break;
  711. }
  712. return TASK_OK;
  713. }
  714. /*
  715. -------------------------
  716. CallbackCommand
  717. -------------------------
  718. */
  719. int CTaskManager::CallbackCommand( CTask *task, int returnCode, CIcarus* icarus )
  720. {
  721. if ( m_owner->Callback( this, task->GetBlock(), returnCode, icarus ) == CSequencer::SEQ_OK, icarus )
  722. return Go(icarus);
  723. assert(0);
  724. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Command callback failure!\n" );
  725. return TASK_FAILED;
  726. }
  727. /*
  728. -------------------------
  729. RecallTask
  730. -------------------------
  731. */
  732. CBlock *CTaskManager::RecallTask( void )
  733. {
  734. CTask *task;
  735. task = PopTask( CSequence::POP_BACK );
  736. if ( task )
  737. {
  738. // fixed 2/12/2 to free the task that has been popped (called from sequencer Recall)
  739. CBlock* retBlock = task->GetBlock();
  740. task->Free();
  741. return retBlock;
  742. // return task->GetBlock();
  743. }
  744. return NULL;
  745. }
  746. /*
  747. -------------------------
  748. PushTask
  749. -------------------------
  750. */
  751. int CTaskManager::PushTask( CTask *task, int flag )
  752. {
  753. assert( (flag == CSequence::PUSH_FRONT) || (flag == CSequence::PUSH_BACK) );
  754. switch ( flag )
  755. {
  756. case CSequence::PUSH_FRONT:
  757. m_tasks.insert(m_tasks.begin(), task);
  758. return TASK_OK;
  759. break;
  760. case CSequence::PUSH_BACK:
  761. m_tasks.insert(m_tasks.end(), task);
  762. return TASK_OK;
  763. break;
  764. }
  765. //Invalid flag
  766. return CSequencer::SEQ_FAILED;
  767. }
  768. /*
  769. -------------------------
  770. PopTask
  771. -------------------------
  772. */
  773. CTask *CTaskManager::PopTask( int flag )
  774. {
  775. CTask *task;
  776. assert( (flag == CSequence::POP_FRONT) || (flag == CSequence::POP_BACK) );
  777. if ( m_tasks.empty() )
  778. return NULL;
  779. switch ( flag )
  780. {
  781. case CSequence::POP_FRONT:
  782. task = m_tasks.front();
  783. m_tasks.pop_front();
  784. return task;
  785. break;
  786. case CSequence::POP_BACK:
  787. task = m_tasks.back();
  788. m_tasks.pop_back();
  789. return task;
  790. break;
  791. }
  792. //Invalid flag
  793. return NULL;
  794. }
  795. /*
  796. -------------------------
  797. GetCurrentTask
  798. -------------------------
  799. */
  800. CBlock *CTaskManager::GetCurrentTask( void )
  801. {
  802. CTask *task = PopTask( CSequence::POP_BACK );
  803. if ( task == NULL )
  804. return NULL;
  805. // fixed 2/12/2 to free the task that has been popped (called from sequencer Interrupt)
  806. CBlock* retBlock = task->GetBlock();
  807. task->Free();
  808. return retBlock;
  809. // return task->GetBlock();
  810. }
  811. /*
  812. =================================================
  813. Task Functions
  814. =================================================
  815. */
  816. int CTaskManager::Wait( CTask *task, bool &completed , CIcarus* icarus )
  817. {
  818. CBlockMember *bm;
  819. CBlock *block = task->GetBlock();
  820. char *sVal;
  821. float dwtime;
  822. int memberNum = 0;
  823. completed = false;
  824. bm = block->GetMember( 0 );
  825. //Check if this is a task completion wait
  826. if ( bm->GetID() == CIcarus::TK_STRING )
  827. {
  828. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  829. if ( task->GetTimeStamp() == icarus->GetGame()->GetTime() )
  830. {
  831. //Print out the debug info
  832. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d wait(\"%s\"); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  833. }
  834. CTaskGroup *group = GetTaskGroup( sVal , icarus);
  835. if ( group == NULL )
  836. {
  837. //TODO: Emit warning
  838. completed = false;
  839. return TASK_FAILED;
  840. }
  841. completed = group->Complete();
  842. }
  843. else //Otherwise it's a time completion wait
  844. {
  845. if ( Check( CIcarus::ID_RANDOM, block, memberNum ) )
  846. {//get it random only the first time
  847. float min, max;
  848. dwtime = *(float *) block->GetMemberData( memberNum++ );
  849. if ( dwtime == icarus->GetGame()->MaxFloat() )
  850. {//we have not evaluated this random yet
  851. min = *(float *) block->GetMemberData( memberNum++ );
  852. max = *(float *) block->GetMemberData( memberNum++ );
  853. dwtime = icarus->GetGame()->Random( min, max );
  854. //store the result in the first member
  855. bm->SetData( &dwtime, sizeof( dwtime ) , icarus);
  856. }
  857. }
  858. else
  859. {
  860. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, dwtime, icarus ) );
  861. }
  862. if ( task->GetTimeStamp() == icarus->GetGame()->GetTime() )
  863. {
  864. //Print out the debug info
  865. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d wait( %d ); [%d]", m_ownerID, (int) dwtime, task->GetTimeStamp() );
  866. }
  867. if ( (task->GetTimeStamp() + dwtime) < (icarus->GetGame()->GetTime()) )
  868. {
  869. completed = true;
  870. memberNum = 0;
  871. if ( Check( CIcarus::ID_RANDOM, block, memberNum ) )
  872. {//set the data back to 0 so it will be re-randomized next time
  873. dwtime = icarus->GetGame()->MaxFloat();
  874. bm->SetData( &dwtime, sizeof( dwtime ), icarus );
  875. }
  876. }
  877. }
  878. return TASK_OK;
  879. }
  880. /*
  881. -------------------------
  882. WaitSignal
  883. -------------------------
  884. */
  885. int CTaskManager::WaitSignal( CTask *task, bool &completed , CIcarus* icarus )
  886. {
  887. CBlock *block = task->GetBlock();
  888. char *sVal;
  889. int memberNum = 0;
  890. completed = false;
  891. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal , icarus) );
  892. if ( task->GetTimeStamp() == icarus->GetGame()->GetTime() )
  893. {
  894. //Print out the debug info
  895. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d waitsignal(\"%s\"); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  896. }
  897. if ( icarus->CheckSignal( sVal ) )
  898. {
  899. completed = true;
  900. icarus->ClearSignal( sVal );
  901. }
  902. return TASK_OK;
  903. }
  904. /*
  905. -------------------------
  906. Print
  907. -------------------------
  908. */
  909. int CTaskManager::Print( CTask *task , CIcarus* icarus)
  910. {
  911. CBlock *block = task->GetBlock();
  912. char *sVal;
  913. int memberNum = 0;
  914. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  915. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d print(\"%s\"); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  916. icarus->GetGame()->CenterPrint( sVal );
  917. Completed( task->GetGUID() );
  918. return TASK_OK;
  919. }
  920. /*
  921. -------------------------
  922. Sound
  923. -------------------------
  924. */
  925. int CTaskManager::Sound( CTask *task, CIcarus* icarus )
  926. {
  927. CBlock *block = task->GetBlock();
  928. char *sVal, *sVal2;
  929. int memberNum = 0;
  930. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  931. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal2, icarus ) );
  932. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d sound(\"%s\", \"%s\"); [%d]", m_ownerID, sVal, sVal2, task->GetTimeStamp() );
  933. //Only instantly complete if the user has requested it
  934. if( icarus->GetGame()->PlaySound( task->GetGUID(), m_ownerID, sVal2, sVal ) )
  935. Completed( task->GetGUID() );
  936. return TASK_OK;
  937. }
  938. /*
  939. -------------------------
  940. Rotate
  941. -------------------------
  942. */
  943. int CTaskManager::Rotate( CTask *task , CIcarus* icarus)
  944. {
  945. vec3_t vector;
  946. CBlock *block = task->GetBlock();
  947. char *tagName;
  948. float tagLookup, duration;
  949. int memberNum = 0;
  950. //Check for a tag reference
  951. if ( Check( CIcarus::ID_TAG, block, memberNum ) )
  952. {
  953. memberNum++;
  954. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &tagName, icarus ) );
  955. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, tagLookup, icarus ) );
  956. if ( icarus->GetGame()->GetTag( m_ownerID, tagName, (int) tagLookup, vector ) == false )
  957. {
  958. //TODO: Emit warning
  959. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Unable to find tag \"%s\"!\n", tagName );
  960. assert(0);
  961. return TASK_FAILED;
  962. }
  963. }
  964. else
  965. {
  966. //Get a normal vector
  967. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector, icarus ) );
  968. }
  969. //Find the duration
  970. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, duration, icarus ) );
  971. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d rotate( <%f,%f,%f>, %d); [%d]", m_ownerID, vector[0], vector[1], vector[2], (int) duration, task->GetTimeStamp() );
  972. icarus->GetGame()->Lerp2Angles( task->GetGUID(), m_ownerID, vector, duration );
  973. return TASK_OK;
  974. }
  975. /*
  976. -------------------------
  977. Remove
  978. -------------------------
  979. */
  980. int CTaskManager::Remove( CTask *task, CIcarus* icarus )
  981. {
  982. CBlock *block = task->GetBlock();
  983. char *sVal;
  984. int memberNum = 0;
  985. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  986. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d remove(\"%s\"); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  987. icarus->GetGame()->Remove( m_ownerID, sVal );
  988. Completed( task->GetGUID() );
  989. return TASK_OK;
  990. }
  991. /*
  992. -------------------------
  993. Camera
  994. -------------------------
  995. */
  996. int CTaskManager::Camera( CTask *task , CIcarus* icarus)
  997. {
  998. CBlock *block = task->GetBlock();
  999. vec3_t vector, vector2;
  1000. float type, fVal, fVal2, fVal3;
  1001. char *sVal;
  1002. int memberNum = 0;
  1003. //Get the camera function type
  1004. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, type, icarus ) );
  1005. switch ( (int) type )
  1006. {
  1007. case CIcarus::TYPE_PAN:
  1008. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector, icarus ) );
  1009. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector2, icarus ) );
  1010. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1011. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( PAN, <%f %f %f>, <%f %f %f>, %f); [%d]", m_ownerID, vector[0], vector[1], vector[2], vector2[0], vector2[1], vector2[2], fVal, task->GetTimeStamp() );
  1012. icarus->GetGame()->CameraPan( vector, vector2, fVal );
  1013. break;
  1014. case CIcarus::TYPE_ZOOM:
  1015. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1016. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1017. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( ZOOM, %f, %f); [%d]", m_ownerID, fVal, fVal2, task->GetTimeStamp() );
  1018. icarus->GetGame()->CameraZoom( fVal, fVal2 );
  1019. break;
  1020. case CIcarus::TYPE_MOVE:
  1021. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector, icarus ) );
  1022. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1023. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( MOVE, <%f %f %f>, %f); [%d]", m_ownerID, vector[0], vector[1], vector[2], fVal, task->GetTimeStamp() );
  1024. icarus->GetGame()->CameraMove( vector, fVal );
  1025. break;
  1026. case CIcarus::TYPE_ROLL:
  1027. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1028. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1029. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( ROLL, %f, %f); [%d]", m_ownerID, fVal, fVal2, task->GetTimeStamp() );
  1030. icarus->GetGame()->CameraRoll( fVal, fVal2 );
  1031. break;
  1032. case CIcarus::TYPE_FOLLOW:
  1033. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1034. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1035. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1036. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( FOLLOW, \"%s\", %f, %f); [%d]", m_ownerID, sVal, fVal, fVal2, task->GetTimeStamp() );
  1037. icarus->GetGame()->CameraFollow( (const char *) sVal, fVal, fVal2 );
  1038. break;
  1039. case CIcarus::TYPE_TRACK:
  1040. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1041. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1042. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1043. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( TRACK, \"%s\", %f, %f); [%d]", m_ownerID, sVal, fVal, fVal2, task->GetTimeStamp() );
  1044. icarus->GetGame()->CameraTrack( (const char *) sVal, fVal, fVal2 );
  1045. break;
  1046. case CIcarus::TYPE_DISTANCE:
  1047. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1048. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1049. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( DISTANCE, %f, %f); [%d]", m_ownerID, fVal, fVal2, task->GetTimeStamp() );
  1050. icarus->GetGame()->CameraDistance( fVal, fVal2 );
  1051. break;
  1052. case CIcarus::TYPE_FADE:
  1053. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector, icarus ) );
  1054. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1055. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector2, icarus ) );
  1056. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1057. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal3, icarus ) );
  1058. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( FADE, <%f %f %f>, %f, <%f %f %f>, %f, %f); [%d]", m_ownerID, vector[0], vector[1], vector[2], fVal, vector2[0], vector2[1], vector2[2], fVal2, fVal3, task->GetTimeStamp() );
  1059. icarus->GetGame()->CameraFade( vector[0], vector[1], vector[2], fVal, vector2[0], vector2[1], vector2[2], fVal2, fVal3 );
  1060. break;
  1061. case CIcarus::TYPE_PATH:
  1062. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1063. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( PATH, \"%s\"); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  1064. icarus->GetGame()->CameraPath( sVal );
  1065. break;
  1066. case CIcarus::TYPE_ENABLE:
  1067. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( ENABLE ); [%d]", m_ownerID, task->GetTimeStamp() );
  1068. icarus->GetGame()->CameraEnable();
  1069. break;
  1070. case CIcarus::TYPE_DISABLE:
  1071. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( DISABLE ); [%d]", m_ownerID, task->GetTimeStamp() );
  1072. icarus->GetGame()->CameraDisable();
  1073. break;
  1074. case CIcarus::TYPE_SHAKE:
  1075. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1076. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal2, icarus ) );
  1077. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d camera( SHAKE, %f, %f ); [%d]", m_ownerID, fVal, fVal2, task->GetTimeStamp() );
  1078. icarus->GetGame()->CameraShake( fVal, (int) fVal2 );
  1079. break;
  1080. }
  1081. Completed( task->GetGUID() );
  1082. return TASK_OK;
  1083. }
  1084. /*
  1085. -------------------------
  1086. Move
  1087. -------------------------
  1088. */
  1089. int CTaskManager::Move( CTask *task, CIcarus* icarus )
  1090. {
  1091. vec3_t vector, vector2;
  1092. CBlock *block = task->GetBlock();
  1093. float duration;
  1094. int memberNum = 0;
  1095. //Get the goal position
  1096. ICARUS_VALIDATE( GetVector( m_ownerID, block, memberNum, vector, icarus ) );
  1097. //Check for possible angles field
  1098. if ( GetVector( m_ownerID, block, memberNum, vector2, icarus ) == false )
  1099. {
  1100. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, duration, icarus ) );
  1101. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d move( <%f %f %f>, %f ); [%d]", m_ownerID, vector[0], vector[1], vector[2], duration, task->GetTimeStamp() );
  1102. icarus->GetGame()->Lerp2Pos( task->GetGUID(), m_ownerID, vector, NULL, duration );
  1103. return TASK_OK;
  1104. }
  1105. //Get the duration and make the call
  1106. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, duration, icarus ) );
  1107. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d move( <%f %f %f>, <%f %f %f>, %f ); [%d]", m_ownerID, vector[0], vector[1], vector[2], vector2[0], vector2[1], vector2[2], duration, task->GetTimeStamp() );
  1108. icarus->GetGame()->Lerp2Pos( task->GetGUID(), m_ownerID, vector, vector2, duration );
  1109. return TASK_OK;
  1110. }
  1111. /*
  1112. -------------------------
  1113. Kill
  1114. -------------------------
  1115. */
  1116. int CTaskManager::Kill( CTask *task, CIcarus* icarus )
  1117. {
  1118. CBlock *block = task->GetBlock();
  1119. char *sVal;
  1120. int memberNum = 0;
  1121. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1122. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d kill( \"%s\" ); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  1123. icarus->GetGame()->Kill( m_ownerID, sVal );
  1124. Completed( task->GetGUID() );
  1125. return TASK_OK;
  1126. }
  1127. /*
  1128. -------------------------
  1129. Set
  1130. -------------------------
  1131. */
  1132. int CTaskManager::Set( CTask *task, CIcarus* icarus )
  1133. {
  1134. CBlock *block = task->GetBlock();
  1135. char *sVal, *sVal2;
  1136. int memberNum = 0;
  1137. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1138. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal2, icarus ) );
  1139. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d set( \"%s\", \"%s\" ); [%d]", m_ownerID, sVal, sVal2, task->GetTimeStamp() );
  1140. icarus->GetGame()->Set( task->GetGUID(), m_ownerID, sVal, sVal2 );
  1141. return TASK_OK;
  1142. }
  1143. /*
  1144. -------------------------
  1145. Use
  1146. -------------------------
  1147. */
  1148. int CTaskManager::Use( CTask *task , CIcarus* icarus)
  1149. {
  1150. CBlock *block = task->GetBlock();
  1151. char *sVal;
  1152. int memberNum = 0;
  1153. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1154. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d use( \"%s\" ); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  1155. icarus->GetGame()->Use( m_ownerID, sVal );
  1156. Completed( task->GetGUID() );
  1157. return TASK_OK;
  1158. }
  1159. /*
  1160. -------------------------
  1161. DeclareVariable
  1162. -------------------------
  1163. */
  1164. int CTaskManager::DeclareVariable( CTask *task , CIcarus* icarus)
  1165. {
  1166. CBlock *block = task->GetBlock();
  1167. char *sVal;
  1168. int memberNum = 0;
  1169. float fVal;
  1170. ICARUS_VALIDATE( GetFloat( m_ownerID, block, memberNum, fVal, icarus ) );
  1171. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1172. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d declare( %d, \"%s\" ); [%d]", m_ownerID, (int) fVal, sVal, task->GetTimeStamp() );
  1173. icarus->GetGame()->DeclareVariable( (int) fVal, sVal );
  1174. Completed( task->GetGUID() );
  1175. return TASK_OK;
  1176. }
  1177. /*
  1178. -------------------------
  1179. FreeVariable
  1180. -------------------------
  1181. */
  1182. int CTaskManager::FreeVariable( CTask *task, CIcarus* icarus )
  1183. {
  1184. CBlock *block = task->GetBlock();
  1185. char *sVal;
  1186. int memberNum = 0;
  1187. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1188. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d free( \"%s\" ); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  1189. icarus->GetGame()->FreeVariable( sVal );
  1190. Completed( task->GetGUID() );
  1191. return TASK_OK;
  1192. }
  1193. /*
  1194. -------------------------
  1195. Signal
  1196. -------------------------
  1197. */
  1198. int CTaskManager::Signal( CTask *task, CIcarus* icarus )
  1199. {
  1200. CBlock *block = task->GetBlock();
  1201. char *sVal;
  1202. int memberNum = 0;
  1203. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1204. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d signal( \"%s\" ); [%d]", m_ownerID, sVal, task->GetTimeStamp() );
  1205. icarus->Signal( (const char *) sVal );
  1206. Completed( task->GetGUID() );
  1207. return TASK_OK;
  1208. }
  1209. /*
  1210. -------------------------
  1211. Play
  1212. -------------------------
  1213. */
  1214. int CTaskManager::Play( CTask *task, CIcarus* icarus )
  1215. {
  1216. CBlock *block = task->GetBlock();
  1217. char *sVal, *sVal2;
  1218. int memberNum = 0;
  1219. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal, icarus ) );
  1220. ICARUS_VALIDATE( Get( m_ownerID, block, memberNum, &sVal2, icarus ) );
  1221. icarus->GetGame()->DebugPrint(IGameInterface::WL_DEBUG, "%4d play( \"%s\", \"%s\" ); [%d]", m_ownerID, sVal, sVal2, task->GetTimeStamp() );
  1222. icarus->GetGame()->Play( task->GetGUID(), m_ownerID, (const char *) sVal, (const char *) sVal2 );
  1223. return TASK_OK;
  1224. }
  1225. /*
  1226. -------------------------
  1227. SaveCommand
  1228. -------------------------
  1229. */
  1230. //FIXME: ARGH! This is duplicated from CSequence because I can't directly link it any other way...
  1231. int CTaskManager::SaveCommand( CBlock *block )
  1232. {
  1233. CIcarus *pIcarus = (CIcarus *)IIcarusInterface::GetIcarus();
  1234. unsigned char flags;
  1235. int numMembers, bID, size;
  1236. CBlockMember *bm;
  1237. //Save out the block ID
  1238. bID = block->GetBlockID();
  1239. pIcarus->BufferWrite( &bID, sizeof( bID ) );
  1240. //Save out the block's flags
  1241. flags = block->GetFlags();
  1242. pIcarus->BufferWrite( &flags, sizeof( flags ) );
  1243. //Save out the number of members to read
  1244. numMembers = block->GetNumMembers();
  1245. pIcarus->BufferWrite( &numMembers, sizeof( numMembers ) );
  1246. for ( int i = 0; i < numMembers; i++ )
  1247. {
  1248. bm = block->GetMember( i );
  1249. //Save the block id
  1250. bID = bm->GetID();
  1251. pIcarus->BufferWrite( &bID, sizeof( bID ) );
  1252. //Save out the data size
  1253. size = bm->GetSize();
  1254. pIcarus->BufferWrite( &size, sizeof( size ) );
  1255. //Save out the raw data
  1256. pIcarus->BufferWrite( bm->GetData(), size );
  1257. }
  1258. return true;
  1259. }
  1260. /*
  1261. -------------------------
  1262. Save
  1263. -------------------------
  1264. */
  1265. void CTaskManager::Save()
  1266. {
  1267. CTaskGroup *taskGroup;
  1268. const char *name;
  1269. CBlock *block;
  1270. DWORD timeStamp;
  1271. bool completed;
  1272. int id, numCommands;
  1273. int numWritten;
  1274. // Data saved here.
  1275. // Taskmanager GUID.
  1276. // Number of Tasks.
  1277. // Tasks:
  1278. // - GUID.
  1279. // - Timestamp.
  1280. // - Block/Command.
  1281. // Number of task groups.
  1282. // Task groups ID's.
  1283. // Task groups (data).
  1284. // - Parent.
  1285. // - Number of Commands.
  1286. // - Commands:
  1287. // + ID.
  1288. // + State of Completion.
  1289. // - Number of Completed Commands.
  1290. // Currently active group.
  1291. // Task group names:
  1292. // - String Size.
  1293. // - String.
  1294. // - ID.
  1295. CIcarus *pIcarus = (CIcarus *)IIcarusInterface::GetIcarus();
  1296. //Save the taskmanager's GUID
  1297. pIcarus->BufferWrite( &m_GUID, sizeof( m_GUID ) );
  1298. //Save out the number of tasks that will follow
  1299. int iNumTasks = m_tasks.size();
  1300. pIcarus->BufferWrite( &iNumTasks, sizeof( iNumTasks ) );
  1301. //Save out all the tasks
  1302. tasks_l::iterator ti;
  1303. STL_ITERATE( ti, m_tasks )
  1304. {
  1305. //Save the GUID
  1306. id = (*ti)->GetGUID();
  1307. pIcarus->BufferWrite( &id, sizeof( id ) );
  1308. //Save the timeStamp (FIXME: Although, this is going to be worthless if time is not consistent...)
  1309. timeStamp = (*ti)->GetTimeStamp();
  1310. pIcarus->BufferWrite( &timeStamp, sizeof( timeStamp ) );
  1311. //Save out the block
  1312. block = (*ti)->GetBlock();
  1313. SaveCommand( block );
  1314. }
  1315. //Save out the number of task groups
  1316. int numTaskGroups = m_taskGroups.size();
  1317. pIcarus->BufferWrite( &numTaskGroups, sizeof( numTaskGroups ) );
  1318. //Save out the IDs of all the task groups
  1319. numWritten = 0;
  1320. taskGroup_v::iterator tgi;
  1321. STL_ITERATE( tgi, m_taskGroups )
  1322. {
  1323. id = (*tgi)->GetGUID();
  1324. pIcarus->BufferWrite( &id, sizeof( id ) );
  1325. numWritten++;
  1326. }
  1327. assert (numWritten == numTaskGroups);
  1328. //Save out the task groups
  1329. numWritten = 0;
  1330. STL_ITERATE( tgi, m_taskGroups )
  1331. {
  1332. //Save out the parent
  1333. id = ( (*tgi)->GetParent() == NULL ) ? -1 : ((*tgi)->GetParent())->GetGUID();
  1334. pIcarus->BufferWrite( &id, sizeof( id ) );
  1335. //Save out the number of commands
  1336. numCommands = (*tgi)->m_completedTasks.size();
  1337. pIcarus->BufferWrite( &numCommands, sizeof( numCommands ) );
  1338. //Save out the command map
  1339. CTaskGroup::taskCallback_m::iterator tci;
  1340. STL_ITERATE( tci, (*tgi)->m_completedTasks )
  1341. {
  1342. //Write out the ID
  1343. id = (*tci).first;
  1344. pIcarus->BufferWrite( &id, sizeof( id ) );
  1345. //Write out the state of completion
  1346. completed = (*tci).second;
  1347. pIcarus->BufferWrite( &completed, sizeof( completed ) );
  1348. }
  1349. //Save out the number of completed commands
  1350. id = (*tgi)->m_numCompleted;
  1351. pIcarus->BufferWrite( &id, sizeof( id ) );
  1352. numWritten++;
  1353. }
  1354. assert (numWritten == numTaskGroups);
  1355. //Only bother if we've got tasks present
  1356. if ( m_taskGroups.size() )
  1357. {
  1358. //Save out the currently active group
  1359. int curGroupID = ( m_curGroup == NULL ) ? -1 : m_curGroup->GetGUID();
  1360. pIcarus->BufferWrite( &curGroupID, sizeof( curGroupID ) );
  1361. }
  1362. //Save out the task group name maps
  1363. taskGroupName_m::iterator tmi;
  1364. numWritten = 0;
  1365. STL_ITERATE( tmi, m_taskGroupNameMap )
  1366. {
  1367. name = ((*tmi).first).c_str();
  1368. //Make sure this is a valid string
  1369. assert( ( name != NULL ) && ( name[0] != NULL ) );
  1370. int length = strlen( name ) + 1;
  1371. //Save out the string size
  1372. //icarus->GetGame()->WriteSaveData( 'TGNL', &length, sizeof ( length ) );
  1373. pIcarus->BufferWrite( &length, sizeof( length ) );
  1374. //Write out the string
  1375. pIcarus->BufferWrite( (void *) name, length );
  1376. taskGroup = (*tmi).second;
  1377. id = taskGroup->GetGUID();
  1378. //Write out the ID
  1379. pIcarus->BufferWrite( &id, sizeof( id ) );
  1380. numWritten++;
  1381. }
  1382. assert (numWritten == numTaskGroups);
  1383. }
  1384. /*
  1385. -------------------------
  1386. Load
  1387. -------------------------
  1388. */
  1389. void CTaskManager::Load( CIcarus* icarus )
  1390. {
  1391. unsigned char flags;
  1392. CTaskGroup *taskGroup;
  1393. CBlock *block;
  1394. CTask *task;
  1395. DWORD timeStamp;
  1396. bool completed;
  1397. void *bData;
  1398. int id, numTasks, numMembers;
  1399. int bID, bSize;
  1400. // Data expected/loaded here.
  1401. // Taskmanager GUID.
  1402. // Number of Tasks.
  1403. // Tasks:
  1404. // - GUID.
  1405. // - Timestamp.
  1406. // - Block/Command.
  1407. // Number of task groups.
  1408. // Task groups ID's.
  1409. // Task groups (data).
  1410. // - Parent.
  1411. // - Number of Commands.
  1412. // - Commands:
  1413. // + ID.
  1414. // + State of Completion.
  1415. // - Number of Completed Commands.
  1416. // Currently active group.
  1417. // Task group names:
  1418. // - String Size.
  1419. // - String.
  1420. // - ID.
  1421. CIcarus *pIcarus = (CIcarus *)IIcarusInterface::GetIcarus();
  1422. //Get the GUID
  1423. pIcarus->BufferRead( &m_GUID, sizeof( m_GUID ) );
  1424. //Get the number of tasks to follow
  1425. pIcarus->BufferRead( &numTasks, sizeof( numTasks ) );
  1426. //Reload all the tasks
  1427. for ( int i = 0; i < numTasks; i++ )
  1428. {
  1429. task = new CTask;
  1430. assert( task );
  1431. //Get the GUID
  1432. pIcarus->BufferRead( &id, sizeof( id ) );
  1433. task->SetGUID( id );
  1434. //Get the time stamp
  1435. pIcarus->BufferRead( &timeStamp, sizeof( timeStamp ) );
  1436. task->SetTimeStamp( timeStamp );
  1437. //
  1438. // BLOCK LOADING
  1439. //
  1440. //Get the block ID and create a new container
  1441. pIcarus->BufferRead( &id, sizeof( id ) );
  1442. block = new CBlock;
  1443. block->Create( id );
  1444. //Read the block's flags
  1445. pIcarus->BufferRead( &flags, sizeof( flags ) );
  1446. block->SetFlags( flags );
  1447. //Get the number of block members
  1448. pIcarus->BufferRead( &numMembers, sizeof( numMembers ) );
  1449. for ( int j = 0; j < numMembers; j++ )
  1450. {
  1451. //Get the member ID
  1452. pIcarus->BufferRead( &bID, sizeof( bID ) );
  1453. //Get the member size
  1454. pIcarus->BufferRead( &bSize, sizeof( bSize ) );
  1455. //Get the member's data
  1456. if ( ( bData = icarus->GetGame()->Malloc( bSize ) ) == NULL )
  1457. {
  1458. assert( 0 );
  1459. return;
  1460. }
  1461. //Get the actual raw data
  1462. pIcarus->BufferRead( bData, bSize );
  1463. //Write out the correct type
  1464. switch ( bID )
  1465. {
  1466. case CIcarus::TK_FLOAT:
  1467. block->Write( CIcarus::TK_FLOAT, *(float *) bData, icarus );
  1468. break;
  1469. case CIcarus::TK_IDENTIFIER:
  1470. block->Write( CIcarus::TK_IDENTIFIER, (char *) bData , icarus);
  1471. break;
  1472. case CIcarus::TK_STRING:
  1473. block->Write( CIcarus::TK_STRING, (char *) bData , icarus);
  1474. break;
  1475. case CIcarus::TK_VECTOR:
  1476. block->Write( CIcarus::TK_VECTOR, *(vec3_t *) bData, icarus );
  1477. break;
  1478. case CIcarus::ID_RANDOM:
  1479. block->Write( CIcarus::ID_RANDOM, *(float *) bData, icarus );//ID_RANDOM );
  1480. break;
  1481. case CIcarus::ID_TAG:
  1482. block->Write( CIcarus::ID_TAG, (float) CIcarus::ID_TAG , icarus);
  1483. break;
  1484. case CIcarus::ID_GET:
  1485. block->Write( CIcarus::ID_GET, (float) CIcarus::ID_GET , icarus);
  1486. break;
  1487. default:
  1488. icarus->GetGame()->DebugPrint(IGameInterface::WL_ERROR, "Invalid Block id %d\n", bID);
  1489. assert( 0 );
  1490. break;
  1491. }
  1492. //Get rid of the temp memory
  1493. icarus->GetGame()->Free( bData );
  1494. }
  1495. task->SetBlock( block );
  1496. STL_INSERT( m_tasks, task );
  1497. }
  1498. //Load the task groups
  1499. int numTaskGroups;
  1500. //icarus->GetGame()->ReadSaveData( 'TG#G', &numTaskGroups, sizeof( numTaskGroups ) );
  1501. pIcarus->BufferRead( &numTaskGroups, sizeof( numTaskGroups ) );
  1502. if ( numTaskGroups == 0 )
  1503. return;
  1504. int *taskIDs = new int[ numTaskGroups ];
  1505. //Get the task group IDs
  1506. for ( i = 0; i < numTaskGroups; i++ )
  1507. {
  1508. //Creat a new task group
  1509. taskGroup = new CTaskGroup;
  1510. assert( taskGroup );
  1511. //Get this task group's ID
  1512. pIcarus->BufferRead( &taskIDs[i], sizeof( taskIDs[i] ) );
  1513. taskGroup->m_GUID = taskIDs[i];
  1514. m_taskGroupIDMap[ taskIDs[i] ] = taskGroup;
  1515. STL_INSERT( m_taskGroups, taskGroup );
  1516. }
  1517. //Recreate and load the task groups
  1518. for ( i = 0; i < numTaskGroups; i++ )
  1519. {
  1520. taskGroup = GetTaskGroup( taskIDs[i], icarus );
  1521. assert( taskGroup );
  1522. //Load the parent ID
  1523. pIcarus->BufferRead( &id, sizeof( id ) );
  1524. if ( id != -1 )
  1525. taskGroup->m_parent = ( GetTaskGroup( id, icarus ) != NULL ) ? GetTaskGroup( id, icarus ) : NULL;
  1526. //Get the number of commands in this group
  1527. pIcarus->BufferRead( &numMembers, sizeof( numMembers ) );
  1528. //Get each command and its completion state
  1529. for ( int j = 0; j < numMembers; j++ )
  1530. {
  1531. //Get the ID
  1532. pIcarus->BufferRead( &id, sizeof( id ) );
  1533. //Write out the state of completion
  1534. pIcarus->BufferRead( &completed, sizeof( completed ) );
  1535. //Save it out
  1536. taskGroup->m_completedTasks[ id ] = completed;
  1537. }
  1538. //Get the number of completed tasks
  1539. pIcarus->BufferRead( &taskGroup->m_numCompleted, sizeof( taskGroup->m_numCompleted ) );
  1540. }
  1541. //Reload the currently active group
  1542. int curGroupID;
  1543. pIcarus->BufferRead( &curGroupID, sizeof( curGroupID ) );
  1544. //Reload the map entries
  1545. for ( i = 0; i < numTaskGroups; i++ )
  1546. {
  1547. char name[1024];
  1548. int length;
  1549. //Get the size of the string
  1550. pIcarus->BufferRead( &length, sizeof( length ) );
  1551. //Get the string
  1552. pIcarus->BufferRead( &name, length );
  1553. //Get the id
  1554. pIcarus->BufferRead( &id, sizeof( id ) );
  1555. taskGroup = GetTaskGroup( id, icarus );
  1556. assert( taskGroup );
  1557. m_taskGroupNameMap[ name ] = taskGroup;
  1558. m_taskGroupIDMap[ taskGroup->GetGUID() ] = taskGroup;
  1559. }
  1560. m_curGroup = ( curGroupID == -1 ) ? NULL : m_taskGroupIDMap[curGroupID];
  1561. delete[] taskIDs;
  1562. }