twiddler.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ** Command & Conquer Generals(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 : WWSaveLoad *
  23. * *
  24. * $Archive:: /Commando/Code/wwsaveload/twiddler.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 6/27/00 2:34p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "twiddler.h"
  36. #include "random.h"
  37. #include "saveloadids.h"
  38. #include "simpledefinitionfactory.h"
  39. #include "persistfactory.h"
  40. #include "win.h"
  41. #include "wwhack.h"
  42. DECLARE_FORCE_LINK( Twiddler )
  43. //////////////////////////////////////////////////////////////////////////////////
  44. // Constants
  45. //////////////////////////////////////////////////////////////////////////////////
  46. enum
  47. {
  48. CHUNKID_VARIABLES = 0x00000100,
  49. CHUNKID_BASE_CLASS = 0x00000200,
  50. };
  51. enum
  52. {
  53. VARID_DEFINTION_ID = 0x01,
  54. VARID_INDIRECT_CLASSID,
  55. };
  56. //////////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Static factories
  59. //
  60. //////////////////////////////////////////////////////////////////////////////////
  61. DECLARE_DEFINITION_FACTORY(TwiddlerClass, CLASSID_TWIDDLERS, "Twiddler") _TwiddlerFactory;
  62. SimplePersistFactoryClass<TwiddlerClass, CHUNKID_TWIDDLER> _TwiddlerPersistFactory;
  63. //////////////////////////////////////////////////////////////////////////////////
  64. //
  65. // TwiddlerClass
  66. //
  67. //////////////////////////////////////////////////////////////////////////////////
  68. TwiddlerClass::TwiddlerClass (void)
  69. : m_IndirectClassID (0)
  70. {
  71. CLASSID_DEFIDLIST_PARAM (TwiddlerClass, m_DefinitionList, 0, m_IndirectClassID, "Preset List");
  72. return ;
  73. }
  74. //////////////////////////////////////////////////////////////////////////////////
  75. //
  76. // ~TwiddlerClass
  77. //
  78. //////////////////////////////////////////////////////////////////////////////////
  79. TwiddlerClass::~TwiddlerClass (void)
  80. {
  81. return ;
  82. }
  83. //////////////////////////////////////////////////////////////////////////////////
  84. //
  85. // Twiddle
  86. //
  87. //////////////////////////////////////////////////////////////////////////////////
  88. DefinitionClass *
  89. TwiddlerClass::Twiddle (void) const
  90. {
  91. DefinitionClass *definition = NULL;
  92. if (m_DefinitionList.Count () > 0) {
  93. //
  94. // Get a random index into our definition list
  95. //
  96. RandomClass randomizer (::GetTickCount ());
  97. int index = randomizer (0, m_DefinitionList.Count () - 1);
  98. //
  99. // Lookup the definition this entry represents
  100. //
  101. int def_id = m_DefinitionList[index];
  102. definition = DefinitionMgrClass::Find_Definition (def_id);
  103. }
  104. return definition;
  105. }
  106. //////////////////////////////////////////////////////////////////////////////////
  107. //
  108. // Create
  109. //
  110. //////////////////////////////////////////////////////////////////////////////////
  111. PersistClass *
  112. TwiddlerClass::Create (void) const
  113. {
  114. PersistClass *retval = NULL;
  115. //
  116. // Pick a random definition
  117. //
  118. DefinitionClass *definition = Twiddle ();
  119. if (definition != NULL) {
  120. //
  121. // Indirect the creation to the definition we randomly selected
  122. //
  123. retval = definition->Create ();
  124. }
  125. return retval;
  126. }
  127. //////////////////////////////////////////////////////////////////////////////////
  128. //
  129. // Get_Factory
  130. //
  131. //////////////////////////////////////////////////////////////////////////////////
  132. const PersistFactoryClass &
  133. TwiddlerClass::Get_Factory (void) const
  134. {
  135. return _TwiddlerPersistFactory;
  136. }
  137. //////////////////////////////////////////////////////////////////////////////////
  138. //
  139. // Save
  140. //
  141. //////////////////////////////////////////////////////////////////////////////////
  142. bool
  143. TwiddlerClass::Save (ChunkSaveClass &csave)
  144. {
  145. bool retval = true;
  146. csave.Begin_Chunk (CHUNKID_VARIABLES);
  147. retval &= Save_Variables (csave);
  148. csave.End_Chunk ();
  149. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  150. retval &= DefinitionClass::Save (csave);
  151. csave.End_Chunk ();
  152. return retval;
  153. }
  154. //////////////////////////////////////////////////////////////////////////////////
  155. //
  156. // Load
  157. //
  158. //////////////////////////////////////////////////////////////////////////////////
  159. bool
  160. TwiddlerClass::Load (ChunkLoadClass &cload)
  161. {
  162. bool retval = true;
  163. while (cload.Open_Chunk ()) {
  164. switch (cload.Cur_Chunk_ID ()) {
  165. case CHUNKID_VARIABLES:
  166. retval &= Load_Variables (cload);
  167. break;
  168. case CHUNKID_BASE_CLASS:
  169. retval &= DefinitionClass::Load (cload);
  170. break;
  171. }
  172. cload.Close_Chunk ();
  173. }
  174. return retval;
  175. }
  176. //////////////////////////////////////////////////////////////////////////////////
  177. //
  178. // Save_Variables
  179. //
  180. //////////////////////////////////////////////////////////////////////////////////
  181. bool
  182. TwiddlerClass::Save_Variables (ChunkSaveClass &csave)
  183. {
  184. WRITE_MICRO_CHUNK (csave, VARID_INDIRECT_CLASSID, m_IndirectClassID)
  185. for (int index = 0; index < m_DefinitionList.Count (); index ++) {
  186. //
  187. // Save this definition ID to the chunk
  188. //
  189. int def_id = m_DefinitionList[index];
  190. WRITE_MICRO_CHUNK (csave, VARID_DEFINTION_ID, def_id)
  191. }
  192. return true;
  193. }
  194. //////////////////////////////////////////////////////////////////////////////////
  195. //
  196. // Load_Variables
  197. //
  198. //////////////////////////////////////////////////////////////////////////////////
  199. bool
  200. TwiddlerClass::Load_Variables (ChunkLoadClass &cload)
  201. {
  202. //
  203. // Start fresh
  204. //
  205. m_DefinitionList.Delete_All ();
  206. //
  207. // Loop through all the microchunks that define the variables
  208. //
  209. while (cload.Open_Micro_Chunk ()) {
  210. switch (cload.Cur_Micro_Chunk_ID ()) {
  211. READ_MICRO_CHUNK (cload, VARID_INDIRECT_CLASSID, m_IndirectClassID)
  212. case VARID_DEFINTION_ID:
  213. {
  214. //
  215. // Read the definition ID from the chunk and add it
  216. // to our list
  217. //
  218. int def_id = 0;
  219. cload.Read (&def_id, sizeof (def_id));
  220. m_DefinitionList.Add (def_id);
  221. }
  222. break;
  223. }
  224. cload.Close_Micro_Chunk ();
  225. }
  226. return true;
  227. }