encyclopediamgr.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : combat *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/encyclopediamgr.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/16/02 12:07p $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "encyclopediamgr.h"
  36. #include "chunkio.h"
  37. #include "saveload.h"
  38. #include "ini.h"
  39. #include "inisup.h"
  40. #include "assets.h"
  41. #include "damageablegameobj.h"
  42. #include "globalsettings.h"
  43. #include "soldier.h"
  44. #include "gametype.h"
  45. ///////////////////////////////////////////////////////////////////////
  46. // Global singleton instance
  47. ///////////////////////////////////////////////////////////////////////
  48. EncyclopediaMgrClass _TheEncyclopediaMgrSaveLoadSubsystem;
  49. ////////////////////////////////////////////////////////////////
  50. // Local constants
  51. ////////////////////////////////////////////////////////////////
  52. enum
  53. {
  54. CHUNKID_VARIABLES = 0x09221214,
  55. CHUNKID_TYPE_DATA
  56. };
  57. enum
  58. {
  59. VARID_TEXTURE_NAME = 0x01,
  60. VARID_CENTER_POINT,
  61. VARID_SCALE,
  62. };
  63. const char *ENCY_INI_FILENAMES[EncyclopediaMgrClass::TYPE_COUNT] =
  64. {
  65. "characters.ini",
  66. "weapons.ini",
  67. "vehicles.ini",
  68. "buildings.ini",
  69. };
  70. ///////////////////////////////////////////////////////////////////////
  71. // Static member initialization
  72. ///////////////////////////////////////////////////////////////////////
  73. BooleanVectorClass EncyclopediaMgrClass::KnownObjectVector[TYPE_COUNT];
  74. BooleanVectorClass EncyclopediaMgrClass::CopyOfKnownObjectVector[TYPE_COUNT];
  75. //////////////////////////////////////////////////////////////////////
  76. //
  77. // Initialize
  78. //
  79. //////////////////////////////////////////////////////////////////////
  80. void
  81. EncyclopediaMgrClass::Initialize (void)
  82. {
  83. //
  84. // Initialize the bit vector for each type
  85. //
  86. for (int index = 0; index < TYPE_COUNT; index ++) {
  87. Build_Bit_Vector ((TYPE)index);
  88. }
  89. Store_Data ();
  90. return ;
  91. }
  92. //////////////////////////////////////////////////////////////////////
  93. //
  94. // Shutdown
  95. //
  96. //////////////////////////////////////////////////////////////////////
  97. void
  98. EncyclopediaMgrClass::Shutdown (void)
  99. {
  100. //
  101. // Reset the bit vectors
  102. //
  103. for (int index = 0; index < TYPE_COUNT; index ++) {
  104. KnownObjectVector[index].Clear ();
  105. }
  106. return ;
  107. }
  108. //////////////////////////////////////////////////////////////////////
  109. //
  110. // Build_Bit_Vector
  111. //
  112. //////////////////////////////////////////////////////////////////////
  113. void
  114. EncyclopediaMgrClass::Build_Bit_Vector (TYPE type)
  115. {
  116. int max_id = 0;
  117. //
  118. // Get the INI file which contains the data for this type
  119. //
  120. INIClass *ini_file = ::Get_INI (ENCY_INI_FILENAMES[type]);
  121. if (ini_file != NULL) {
  122. //
  123. // Loop over all the sections in the INI
  124. //
  125. List<INISection *> &section_list = ini_file->Get_Section_List ();
  126. for ( INISection *section = section_list.First ();
  127. section != NULL;
  128. section = section->Next_Valid ())
  129. {
  130. //
  131. // Read this object's data from the INI file
  132. //
  133. int id = ini_file->Get_Int (section->Section, "ID");
  134. max_id = max (max_id, id);
  135. }
  136. // ST - 9/6/2001 11:48PM
  137. delete ini_file;
  138. }
  139. //
  140. // Ensure the bit vector is large enough to hold this data
  141. //
  142. if (KnownObjectVector[type].Length () < max_id + 1) {
  143. KnownObjectVector[type].Resize (max_id + 1);
  144. }
  145. return ;
  146. }
  147. //////////////////////////////////////////////////////////////////////
  148. //
  149. // Reveal_Object
  150. //
  151. //////////////////////////////////////////////////////////////////////
  152. bool
  153. EncyclopediaMgrClass::Reveal_Object (TYPE type, int object_id)
  154. {
  155. bool retval = false;
  156. //
  157. // Don't set the data if the object ID is outside our range
  158. //
  159. //if (object_id < KnownObjectVector[type].Length ()) {
  160. if (IS_MISSION && object_id < KnownObjectVector[type].Length ()) {
  161. retval = (KnownObjectVector[type][object_id] != true);
  162. KnownObjectVector[type][object_id] = true;
  163. }
  164. return retval;
  165. }
  166. //////////////////////////////////////////////////////////////////////
  167. //
  168. // Is_Object_Revealed
  169. //
  170. //////////////////////////////////////////////////////////////////////
  171. bool
  172. EncyclopediaMgrClass::Is_Object_Revealed (TYPE type, int object_id)
  173. {
  174. bool retval = false;
  175. //
  176. // Lookup the bit for this object...
  177. //
  178. if (object_id < KnownObjectVector[type].Length ()) {
  179. retval = KnownObjectVector[type][object_id];
  180. }
  181. return retval;
  182. }
  183. //////////////////////////////////////////////////////////////////////
  184. //
  185. // Reveal_Objects
  186. //
  187. //////////////////////////////////////////////////////////////////////
  188. void
  189. EncyclopediaMgrClass::Reveal_Objects (TYPE type)
  190. {
  191. KnownObjectVector[type].Set ();
  192. return ;
  193. }
  194. //////////////////////////////////////////////////////////////////////
  195. //
  196. // Reveal_All_Objects
  197. //
  198. //////////////////////////////////////////////////////////////////////
  199. void
  200. EncyclopediaMgrClass::Reveal_All_Objects (void)
  201. {
  202. //
  203. // Simply set all bits to true in each cateogry
  204. //
  205. for (int index = 0; index < TYPE_COUNT; index ++) {
  206. KnownObjectVector[index].Set ();
  207. }
  208. return ;
  209. }
  210. //////////////////////////////////////////////////////////////////////
  211. //
  212. // Hide_Objects
  213. //
  214. //////////////////////////////////////////////////////////////////////
  215. void
  216. EncyclopediaMgrClass::Hide_Objects (TYPE type)
  217. {
  218. KnownObjectVector[type].Reset ();
  219. return ;
  220. }
  221. //////////////////////////////////////////////////////////////////////
  222. //
  223. // Hide_All_Objects
  224. //
  225. //////////////////////////////////////////////////////////////////////
  226. void
  227. EncyclopediaMgrClass::Hide_All_Objects (void)
  228. {
  229. //
  230. // Simply set all bits to false in each cateogry
  231. //
  232. for (int index = 0; index < TYPE_COUNT; index ++) {
  233. KnownObjectVector[index].Reset ();
  234. }
  235. return ;
  236. }
  237. ////////////////////////////////////////////////////////////////
  238. //
  239. // Save
  240. //
  241. ////////////////////////////////////////////////////////////////
  242. bool
  243. EncyclopediaMgrClass::Save (ChunkSaveClass &csave)
  244. {
  245. //
  246. // Loop over and save the bit vector for each type to disk
  247. //
  248. for (int index = 0; index < TYPE_COUNT; index ++) {
  249. csave.Begin_Chunk (CHUNKID_TYPE_DATA);
  250. const VectorClass<unsigned char> &bit_vector = KnownObjectVector[index].Get_Bit_Array();
  251. if (bit_vector.Length () > 0) {
  252. csave.Write (&bit_vector[0], sizeof (bit_vector[0]) * bit_vector.Length ());
  253. }
  254. csave.End_Chunk ();
  255. }
  256. //
  257. // Write the variables
  258. //
  259. csave.Begin_Chunk (CHUNKID_VARIABLES);
  260. csave.End_Chunk ();
  261. return true;
  262. }
  263. ////////////////////////////////////////////////////////////////
  264. //
  265. // Load
  266. //
  267. ////////////////////////////////////////////////////////////////
  268. bool
  269. EncyclopediaMgrClass::Load (ChunkLoadClass &cload)
  270. {
  271. //
  272. // Start clean...
  273. //
  274. Hide_All_Objects ();
  275. int type_index = 0;
  276. while (cload.Open_Chunk ()) {
  277. switch (cload.Cur_Chunk_ID ()) {
  278. //
  279. // Load all the variables from this chunk
  280. //
  281. case CHUNKID_VARIABLES:
  282. Load_Variables (cload);
  283. break;
  284. case CHUNKID_TYPE_DATA:
  285. {
  286. //
  287. // Read the bit vector from its chunk
  288. //
  289. int size = cload.Cur_Chunk_Length ();
  290. int vector_size = max (size, KnownObjectVector[type_index].Get_Bit_Array().Length ());
  291. //unsigned char *bit_vector = new unsigned char[vector_size];
  292. //
  293. // Initialize the known object array from the saved bit vector
  294. //
  295. int bitcount = vector_size * 8;
  296. KnownObjectVector[type_index].Init (bitcount);
  297. //
  298. // Now read the data straight into the vector
  299. //
  300. const VectorClass<unsigned char> &bit_array = KnownObjectVector[type_index].Get_Bit_Array ();
  301. cload.Read (const_cast<unsigned char*>(&bit_array[0]), size);
  302. //
  303. // Advance to the next entry
  304. //
  305. type_index ++;
  306. break;
  307. }
  308. }
  309. cload.Close_Chunk ();
  310. }
  311. return true;
  312. }
  313. ////////////////////////////////////////////////////////////////
  314. //
  315. // Load_Variables
  316. //
  317. ////////////////////////////////////////////////////////////////
  318. void
  319. EncyclopediaMgrClass::Load_Variables (ChunkLoadClass &cload)
  320. {
  321. /*while (cload.Open_Micro_Chunk ()) {
  322. switch (cload.Cur_Micro_Chunk_ID ()) {
  323. //READ_MICRO_CHUNK_WWSTRING (cload, VARID_TEXTURE_NAME, MapTextureName);
  324. //READ_MICRO_CHUNK (cload, VARID_CENTER_POINT, MapCenterPoint);
  325. //READ_MICRO_CHUNK (cload, VARID_SCALE, MapScale);
  326. default:
  327. break;
  328. }
  329. cload.Close_Micro_Chunk ();
  330. }*/
  331. //SaveLoadSystemClass::Register_Post_Load_Callback (this);
  332. return ;
  333. }
  334. ////////////////////////////////////////////////////////////////
  335. //
  336. // Reveal_Object
  337. //
  338. ////////////////////////////////////////////////////////////////
  339. bool
  340. EncyclopediaMgrClass::Reveal_Object (DamageableGameObj *game_obj)
  341. {
  342. if (game_obj == NULL) {
  343. return false;
  344. }
  345. if (!IS_MISSION) {
  346. // TSS092601 - we don't want to display datalink updated msg in MP.
  347. return false;
  348. }
  349. //
  350. // Get information about this entry
  351. //
  352. EncyclopediaMgrClass::TYPE type = game_obj->Get_Definition ().Get_Encyclopedia_Type ();
  353. int id = game_obj->Get_Definition ().Get_Encyclopedia_ID ();
  354. //
  355. // If this is an expected type, then reveal the object to the user
  356. //
  357. bool retval = false;
  358. if (type != TYPE_UNKNOWN) {
  359. //
  360. // Display some text and play a sound if the player hasn't gotten
  361. // this entry yet.
  362. //
  363. if (Is_Object_Revealed (type, id) == false) {
  364. Display_Event_UI ();
  365. }
  366. retval = Reveal_Object (type, id);
  367. }
  368. return retval;
  369. }
  370. ////////////////////////////////////////////////////////////////
  371. //
  372. // Display_Event_UI
  373. //
  374. ////////////////////////////////////////////////////////////////
  375. void
  376. EncyclopediaMgrClass::Display_Event_UI (void)
  377. {
  378. //
  379. // Get the current encyclopedia event string and sound ID
  380. //
  381. int strings_id = GlobalSettingsDef::Get_Global_Settings ()->Get_Encyclopedia_Event_String_ID ();
  382. if (strings_id != 0) {
  383. //
  384. // Display the text and play the sound
  385. //
  386. SoldierGameObj::Say_Dynamic_Dialogue( strings_id, NULL );
  387. }
  388. return ;
  389. }
  390. ////////////////////////////////////////////////////////////////
  391. //
  392. // Store_Data
  393. //
  394. ////////////////////////////////////////////////////////////////
  395. void
  396. EncyclopediaMgrClass::Store_Data (void)
  397. {
  398. for (int index = 0; index < TYPE_COUNT; index ++) {
  399. CopyOfKnownObjectVector[index] = KnownObjectVector[index];
  400. }
  401. return ;
  402. }
  403. ////////////////////////////////////////////////////////////////
  404. //
  405. // Restore_Data
  406. //
  407. ////////////////////////////////////////////////////////////////
  408. void
  409. EncyclopediaMgrClass::Restore_Data (void)
  410. {
  411. for (int index = 0; index < TYPE_COUNT; index ++) {
  412. KnownObjectVector[index] = CopyOfKnownObjectVector[index];
  413. }
  414. return ;
  415. }