scripts.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. *
  22. * DESCRIPTION
  23. *
  24. * PROGRAMMER
  25. * Denzil E. Long, Jr.
  26. *
  27. * VERSION INFO
  28. * $Author: Byon_g $
  29. * $Revision: 85 $
  30. * $Modtime: 7/05/01 12:12p $
  31. * $Archive: /Commando/Code/Scripts/scripts.cpp $
  32. *
  33. ******************************************************************************/
  34. #include "scripts.h"
  35. #include "scriptfactory.h"
  36. #include "dprint.h"
  37. #include "strtrim.h"
  38. #include <string.h>
  39. #include <stdio.h>
  40. #define CHUNKID_SCRIPTHEADER 'shdr'
  41. #define CHUNKID_SCRIPTDATA 'sdat'
  42. #define CHUNKID_SCRIPT_AUTO_VARIABLES 'csav'
  43. enum {
  44. DATAID_PARAMETER_STRING = 1,
  45. DATAID_OWNERPTR,
  46. };
  47. // Script commands
  48. ScriptCommands* Commands = NULL;
  49. void (*ScriptImpClass::Request_Destroy_Script)(ScriptClass*) = NULL;
  50. void ScriptImpClass::Set_Request_Destroy_Func(void (*function)(ScriptClass*))
  51. {
  52. Request_Destroy_Script = function;
  53. }
  54. /******************************************************************************
  55. *
  56. * NAME
  57. * ScriptImpClass::ScriptImpClass
  58. *
  59. * DESCRIPTION
  60. * Constructor for concrete script implementation class
  61. *
  62. * INPUTS
  63. * NONE
  64. *
  65. * RESULTS
  66. * NONE
  67. *
  68. ******************************************************************************/
  69. ScriptImpClass::ScriptImpClass()
  70. : mOwner(NULL),
  71. mArgC(0),
  72. mArgV(NULL),
  73. AutoVariableList( NULL )
  74. {
  75. }
  76. /******************************************************************************
  77. *
  78. * NAME
  79. * ScriptImpClass::~ScriptImpClass
  80. *
  81. * DESCRIPTION
  82. * Destructor
  83. *
  84. * INPUTS
  85. * NONE
  86. *
  87. * RESULTS
  88. * NONE
  89. *
  90. ******************************************************************************/
  91. ScriptImpClass::~ScriptImpClass()
  92. {
  93. #ifdef _DEBUG
  94. if (Commands != NULL) {
  95. DebugPrint("Script '%s' for object '%d' deleted.\n",
  96. Get_Name(), ((mOwner != NULL) ? Commands->Get_ID(mOwner) : 0 ));
  97. }
  98. #endif
  99. Clear_Parameters();
  100. // Delete the auto variables
  101. while ( AutoVariableList != NULL ) {
  102. ScriptVariableClass * next = AutoVariableList->Get_Next();
  103. delete AutoVariableList;
  104. AutoVariableList = next;
  105. }
  106. }
  107. /******************************************************************************
  108. *
  109. * NAME
  110. * ScriptImpClass::Get_Name
  111. *
  112. * DESCRIPTION
  113. * Retrieve name of script
  114. *
  115. * INPUTS
  116. * NONE
  117. *
  118. * RESULTS
  119. * Name - Script name string.
  120. *
  121. ******************************************************************************/
  122. const char* ScriptImpClass::Get_Name(void)
  123. {
  124. return mFactory->GetName();
  125. }
  126. /******************************************************************************
  127. *
  128. * NAME
  129. * ScriptImpClass::Destroy
  130. *
  131. * DESCRIPTION
  132. * Destroy script
  133. *
  134. * INPUTS
  135. * NONE
  136. *
  137. * RESULTS
  138. * NONE
  139. *
  140. ******************************************************************************/
  141. void ScriptImpClass::Destroy_Script(void)
  142. {
  143. if (Request_Destroy_Script != NULL) {
  144. Request_Destroy_Script(this);
  145. }
  146. }
  147. /******************************************************************************
  148. *
  149. * NAME
  150. * ScriptImpClass::Attach
  151. *
  152. * DESCRIPTION
  153. * Handle attachment of script to an object
  154. *
  155. * INPUTS
  156. * Object - Object script is attaching to.
  157. *
  158. * RESULTS
  159. * NONE
  160. *
  161. ******************************************************************************/
  162. void ScriptImpClass::Attach(GameObject* obj)
  163. {
  164. #ifdef _DEBUG
  165. if (Commands != NULL) {
  166. DebugPrint("Attaching script '%s' to object %d\n",
  167. Get_Name(), Commands->Get_ID(obj));
  168. }
  169. #endif
  170. mOwner = obj;
  171. }
  172. /******************************************************************************
  173. *
  174. * NAME
  175. * ScriptImpClass::Detach
  176. *
  177. * DESCRIPTION
  178. * Handle detachment of script from an object.
  179. *
  180. * INPUTS
  181. * Object - Object script is detaching from.
  182. *
  183. * RESULTS
  184. * NONE
  185. *
  186. ******************************************************************************/
  187. void ScriptImpClass::Detach(GameObject* obj)
  188. {
  189. mOwner = NULL;
  190. Destroy_Script();
  191. }
  192. /******************************************************************************
  193. *
  194. * NAME
  195. * ScriptImpClass::Set_Parameters_String
  196. *
  197. * DESCRIPTION
  198. * Set script parameters.
  199. *
  200. * INPUTS
  201. * Params - Parameter string
  202. *
  203. * RESULTS
  204. * NONE
  205. *
  206. ******************************************************************************/
  207. void ScriptImpClass::Set_Parameters_String(const char* params)
  208. {
  209. if ((params == NULL) || (strlen(params) == 0)) {
  210. return;
  211. }
  212. DebugPrint("Setting parameters for script '%s', params = '%s'\n",
  213. Get_Name(), params);
  214. char* working = strdup(params);
  215. if ( strlen( working ) && working[ strlen( working ) - 1 ] == '\n' ) {
  216. working[ strlen( working ) - 1 ] = 0;
  217. }
  218. // Count the parameters
  219. int count = 0;
  220. char * param_ptr = working;
  221. if ( *param_ptr ) count++;
  222. for ( param_ptr = working; *param_ptr; param_ptr++ ) {
  223. if ( *param_ptr == ',' ) count++;
  224. }
  225. if (count == 0) {
  226. return;
  227. }
  228. // Release old argument vector
  229. Clear_Parameters();
  230. // Create new argument vector
  231. mArgC = count;
  232. mArgV = new char*[count];
  233. assert(mArgV != NULL);
  234. memset(mArgV, 0, (sizeof(char*) * count));
  235. // Copy the parameters
  236. strcpy(working, params);
  237. count = 0;
  238. param_ptr = working;
  239. while ( *param_ptr ) {
  240. // Find where this parameter ends
  241. char * next = param_ptr;
  242. while ( *next && *next != ',' ) next++;
  243. if ( *next ) *next++ = 0;
  244. // Set param
  245. Set_Parameter(count++, param_ptr);
  246. param_ptr = next;
  247. }
  248. if (count == mArgC - 1) {
  249. Set_Parameter(count, "");
  250. }
  251. free(working);
  252. }
  253. /******************************************************************************
  254. *
  255. * NAME
  256. * ScriptImpClass::Get_Parameters_String
  257. *
  258. * DESCRIPTION
  259. * Retrieve a copy of the scripts parameters string.
  260. *
  261. * INPUTS
  262. * Buffer - Buffer to copy parameter string into.
  263. * Size - Size of buffer.
  264. *
  265. * RESULTS
  266. * NONE
  267. *
  268. ******************************************************************************/
  269. void ScriptImpClass::Get_Parameters_String(char* buffer, unsigned int size)
  270. {
  271. assert(buffer != NULL);
  272. buffer[0] = '\0';
  273. // Build parameter string (comma seperated)
  274. int count = Get_Parameter_Count();
  275. int i = 0;
  276. unsigned int stringLen = 0;
  277. while ((i < count) && (stringLen < size)) {
  278. if (i > 0) {
  279. stringLen++;
  280. strcat(buffer, ",");
  281. }
  282. const char* param = Get_Parameter(i);
  283. stringLen += strlen(param);
  284. if (stringLen <= size) {
  285. strcat(buffer, param);
  286. i++;
  287. }
  288. }
  289. }
  290. /******************************************************************************
  291. *
  292. * NAME
  293. * ScriptImpClass::Clear_Parameters
  294. *
  295. * DESCRIPTION
  296. * Release script parameters
  297. *
  298. * INPUTS
  299. * NONE
  300. *
  301. * RESULTS
  302. * NONE
  303. *
  304. ******************************************************************************/
  305. void ScriptImpClass::Clear_Parameters(void)
  306. {
  307. if (mArgV != NULL) {
  308. while (mArgC--) {
  309. if (mArgV[mArgC] != NULL) {
  310. free((void*)mArgV[mArgC]);
  311. }
  312. }
  313. delete (void*)mArgV;
  314. }
  315. mArgC = 0;
  316. mArgV = NULL;
  317. }
  318. /******************************************************************************
  319. *
  320. * NAME
  321. * ScriptImpClass::Set_Parameter
  322. *
  323. * DESCRIPTION
  324. * Set a script parameter
  325. *
  326. * INPUTS
  327. * Index - Ordinal position of parameter
  328. * Param - Parameter string
  329. *
  330. * RESULTS
  331. * NONE
  332. *
  333. ******************************************************************************/
  334. void ScriptImpClass::Set_Parameter(int index, const char* str)
  335. {
  336. // Release old parameter
  337. if (mArgV[index] != NULL) {
  338. free((void*)mArgV[index]);
  339. }
  340. mArgV[index] = strdup(str);
  341. assert(mArgV[index] != NULL);
  342. }
  343. /******************************************************************************
  344. *
  345. * NAME
  346. * ScriptImpClass::Get_Parameter
  347. *
  348. * DESCRIPTION
  349. * Retrieve paramter by name
  350. *
  351. * INPUTS
  352. * Name - Paramter name
  353. *
  354. * RESULTS
  355. * Param - Parameter
  356. *
  357. ******************************************************************************/
  358. const char* ScriptImpClass::Get_Parameter(const char* name)
  359. {
  360. int index = Get_Parameter_Index(name);
  361. return Get_Parameter(index);
  362. }
  363. /******************************************************************************
  364. *
  365. * NAME
  366. * ScriptImpClass::Get_Parameter
  367. *
  368. * DESCRIPTION
  369. * Retrieve parameter at specified index.
  370. *
  371. * INPUTS
  372. * Index - Ordinal position of paramater
  373. *
  374. * RESULTS
  375. * Param - Parameter string.
  376. *
  377. ******************************************************************************/
  378. const char* ScriptImpClass::Get_Parameter(int index)
  379. {
  380. if ((index < 0) || (index >= mArgC)) {
  381. return "";
  382. }
  383. return mArgV[index];
  384. }
  385. /******************************************************************************
  386. *
  387. * NAME
  388. * ScriptImpClass::Get_Int_Parameter
  389. *
  390. * DESCRIPTION
  391. * Retrieve parameter as an integer.
  392. *
  393. * INPUTS
  394. * Name - Name of parameter
  395. *
  396. * RESULTS
  397. * Value - Integer value.
  398. *
  399. ******************************************************************************/
  400. int ScriptImpClass::Get_Int_Parameter(const char* parameterName)
  401. {
  402. int index = Get_Parameter_Index(parameterName);
  403. return Get_Int_Parameter(index);
  404. }
  405. /******************************************************************************
  406. *
  407. * NAME
  408. * ScriptImpClass::Get_Float_Parameter
  409. *
  410. * DESCRIPTION
  411. * Retrieve parameter as a floating point number.
  412. *
  413. * INPUTS
  414. * Name - Name of parameter
  415. *
  416. * RESULTS
  417. * Value - Floating point value
  418. *
  419. ******************************************************************************/
  420. float ScriptImpClass::Get_Float_Parameter(const char* parameterName)
  421. {
  422. int index = Get_Parameter_Index(parameterName);
  423. return Get_Float_Parameter(index);
  424. }
  425. /******************************************************************************
  426. **
  427. ******************************************************************************/
  428. Vector3 ScriptImpClass::Get_Vector3_Parameter( int index )
  429. {
  430. float x,y,z;
  431. ::sscanf( Get_Parameter( index ), "%f %f %f", &x, &y, &z );
  432. return Vector3( x,y,z );
  433. }
  434. Vector3 ScriptImpClass::Get_Vector3_Parameter(const char* parameterName)
  435. {
  436. int index = Get_Parameter_Index(parameterName);
  437. return Get_Vector3_Parameter(index);
  438. }
  439. /******************************************************************************
  440. *
  441. * NAME
  442. * ScriptImpClass::Get_Parameter_Index
  443. *
  444. * DESCRIPTION
  445. * Obtain index of parameter with specified name.
  446. *
  447. * INPUTS
  448. * Name - Name of parameter
  449. *
  450. * RESULTS
  451. * Index - Ordinal position of parameter
  452. *
  453. ******************************************************************************/
  454. int ScriptImpClass::Get_Parameter_Index(const char* parameterName)
  455. {
  456. const char* paramDesc = mFactory->GetParamDescription();
  457. // Make copy of parameter description string to work with.
  458. char string[512];
  459. strncpy(string, paramDesc, (sizeof(string) - 1));
  460. string[sizeof(string)-1] = '\0';
  461. // Search for the specified parameter
  462. int index = 0;
  463. char * param_ptr = string;
  464. while ( *param_ptr ) {
  465. // Find where this parameter ends
  466. char * next = param_ptr;
  467. while ( *next && *next != ',' ) next++;
  468. if ( *next ) *next++ = 0;
  469. // Strip the name portion from the parameter definition
  470. char* tokenEnd = strpbrk(param_ptr, "=:\n");
  471. if (tokenEnd != NULL) {
  472. *tokenEnd = '\0';
  473. }
  474. // Check for specified parameter
  475. strtrim(param_ptr);
  476. if (stricmp(param_ptr, parameterName) == 0) {
  477. return index;
  478. }
  479. // Advance to next parameter definition
  480. index++;
  481. param_ptr = next;
  482. }
  483. return -1;
  484. }
  485. /******************************************************************************
  486. *
  487. * NAME
  488. * ScriptImpClass::Save
  489. *
  490. * DESCRIPTION
  491. * Save script data
  492. *
  493. * INPUTS
  494. * ScriptSaver& saver
  495. *
  496. * RESULTS
  497. * NONE
  498. *
  499. ******************************************************************************/
  500. void ScriptImpClass::Save(ScriptSaver& saver)
  501. {
  502. /* Commands->Begin_Chunk(saver, CHUNKID_SCRIPTDATA);
  503. Save_Data(saver);
  504. Commands->End_Chunk(saver);
  505. */
  506. ScriptVariableClass * var = AutoVariableList;
  507. if ( var != NULL ) {
  508. Commands->Begin_Chunk(saver, CHUNKID_SCRIPT_AUTO_VARIABLES);
  509. while ( var != NULL ) {
  510. Commands->Save_Data(saver, var->Get_ID(), var->Get_Data_Size(), var->Get_Data_Ptr() );
  511. var = var->Get_Next();
  512. }
  513. Commands->End_Chunk(saver);
  514. }
  515. }
  516. /******************************************************************************
  517. *
  518. * NAME
  519. * ScriptImpClass::Load
  520. *
  521. * DESCRIPTION
  522. * Load script data
  523. *
  524. * INPUTS
  525. * ScriptLoader& loader
  526. *
  527. * RESULTS
  528. * NONE
  529. *
  530. ******************************************************************************/
  531. void ScriptImpClass::Load(ScriptLoader& loader)
  532. {
  533. unsigned int chunkID;
  534. while (Commands->Open_Chunk(loader, &chunkID)) {
  535. switch (chunkID) {
  536. /* case CHUNKID_SCRIPTDATA:
  537. DebugPrint("Encountered data chunk.\n");
  538. Load_Data(loader);
  539. break;
  540. */
  541. case CHUNKID_SCRIPT_AUTO_VARIABLES:
  542. {
  543. int id;
  544. while (Commands->Load_Begin(loader, &id)) {
  545. // If we find this ID, load it
  546. ScriptVariableClass * var = AutoVariableList;
  547. while ( var != NULL ) {
  548. if ( var->Get_ID() == id ) {
  549. Commands->Load_Data(loader, var->Get_Data_Size(), var->Get_Data_Ptr() );
  550. }
  551. var = var->Get_Next();
  552. }
  553. Commands->Load_End(loader);
  554. }
  555. }
  556. break;
  557. default:
  558. DebugPrint("***** ScriptImpClass::Load() - Unrecognized chunk %0x!\n", chunkID);
  559. break;
  560. }
  561. Commands->Close_Chunk(loader);
  562. }
  563. }
  564. /*
  565. **
  566. */
  567. void ScriptImpClass::Auto_Save_Variable( void * data_ptr, int data_size, int id )
  568. {
  569. if ( ( id < 0 ) || ( id > 255 ) ) {
  570. DebugPrint("***** ScriptImpClass::Auto_Save_Variable Bad ID %d\n", id );
  571. return;
  572. }
  573. // First be sure we don't already have this ID
  574. ScriptVariableClass * var = AutoVariableList;
  575. while ( var != NULL ) {
  576. if ( var->Get_ID() == id ) {
  577. DebugPrint("***** ScriptImpClass::Auto_Save_Variable Dupe ID %d\n", id );
  578. return;
  579. }
  580. var = var->Get_Next();
  581. }
  582. // Never save more than 250 bytes
  583. if ( data_size > 250 ) {
  584. DebugPrint("***** ScriptImpClass::Too Much Save Data on ID %d\n", id );
  585. return;
  586. }
  587. // Then create and add the variable;
  588. AutoVariableList = new ScriptVariableClass( data_ptr, data_size, id, AutoVariableList );
  589. }