twiddler.cpp 7.6 KB

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