Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
twiddler.cpp
Go to the documentation of this file.
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/***********************************************************************************************
20 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWSaveLoad *
24 * *
25 * $Archive:: /Commando/Code/wwsaveload/twiddler.cpp $*
26 * *
27 * Author:: Patrick Smith *
28 * *
29 * $Modtime:: 12/10/01 12:40p $*
30 * *
31 * $Revision:: 3 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#include "twiddler.h"
39#include "random.h"
40#include "saveloadids.h"
42#include "persistfactory.h"
43#include "win.h"
44#include "wwhack.h"
45#include "systimer.h"
46
47
48DECLARE_FORCE_LINK( Twiddler )
49
50
51// Constants
53enum
54{
55 CHUNKID_VARIABLES = 0x00000100,
56 CHUNKID_BASE_CLASS = 0x00000200,
57};
58
59enum
60{
63};
64
65
67//
68// Static factories
69//
73
74
76//
77// TwiddlerClass
78//
81 : m_IndirectClassID (0)
82
83{
84 CLASSID_DEFIDLIST_PARAM (TwiddlerClass, m_DefinitionList, 0, m_IndirectClassID, "Preset List");
85 return ;
86}
87
88
90//
91// ~TwiddlerClass
92//
98
99
101//
102// Twiddle
103//
107{
108 DefinitionClass *definition = NULL;
109
110 if (m_DefinitionList.Count () > 0) {
111
112 //
113 // Get a random index into our definition list
114 //
115 RandomClass randomizer (TIMEGETTIME ());
116 int index = randomizer (0, m_DefinitionList.Count () - 1);
117
118 //
119 // Lookup the definition this entry represents
120 //
121 int def_id = m_DefinitionList[index];
122 definition = DefinitionMgrClass::Find_Definition (def_id);
123 }
124
125 return definition;
126}
127
128
130//
131// Create
132//
136{
137 PersistClass *retval = NULL;
138
139 //
140 // Pick a random definition
141 //
142 DefinitionClass *definition = Twiddle ();
143 if (definition != NULL) {
144
145 //
146 // Indirect the creation to the definition we randomly selected
147 //
148 retval = definition->Create ();
149
150 }
151
152 return retval;
153}
154
155
157//
158// Get_Factory
159//
163{
165}
166
167
169//
170// Save
171//
173bool
175{
176 bool retval = true;
177
179 retval &= Save_Variables (csave);
180 csave.End_Chunk ();
181
183 retval &= DefinitionClass::Save (csave);
184 csave.End_Chunk ();
185
186 return retval;
187}
188
189
191//
192// Load
193//
195bool
197{
198 bool retval = true;
199
200 while (cload.Open_Chunk ()) {
201 switch (cload.Cur_Chunk_ID ()) {
202
204 retval &= Load_Variables (cload);
205 break;
206
208 retval &= DefinitionClass::Load (cload);
209 break;
210 }
211
212 cload.Close_Chunk ();
213 }
214
215 return retval;
216}
217
218
220//
221// Save_Variables
222//
224bool
225TwiddlerClass::Save_Variables (ChunkSaveClass &csave)
226{
227 WRITE_MICRO_CHUNK (csave, VARID_INDIRECT_CLASSID, m_IndirectClassID)
228
229 for (int index = 0; index < m_DefinitionList.Count (); index ++) {
230
231 //
232 // Save this definition ID to the chunk
233 //
234 int def_id = m_DefinitionList[index];
236 }
237
238 return true;
239}
240
241
243//
244// Load_Variables
245//
247bool
248TwiddlerClass::Load_Variables (ChunkLoadClass &cload)
249{
250 //
251 // Start fresh
252 //
253 m_DefinitionList.Delete_All ();
254
255 //
256 // Loop through all the microchunks that define the variables
257 //
258 while (cload.Open_Micro_Chunk ()) {
259 switch (cload.Cur_Micro_Chunk_ID ()) {
260
261 READ_MICRO_CHUNK (cload, VARID_INDIRECT_CLASSID, m_IndirectClassID)
262
264 {
265 //
266 // Read the definition ID from the chunk and add it
267 // to our list
268 //
269 int def_id = 0;
270 cload.Read (&def_id, sizeof (def_id));
271 m_DefinitionList.Add (def_id);
272 }
273 break;
274 }
275
276 cload.Close_Micro_Chunk ();
277 }
278
279 return true;
280}
#define NULL
Definition BaseType.h:92
#define WRITE_MICRO_CHUNK(csave, id, var)
Definition chunkio.h:293
#define READ_MICRO_CHUNK(cload, id, var)
Definition chunkio.h:334
@ CHUNKID_BASE_CLASS
@ CHUNKID_VARIABLES
bool Close_Micro_Chunk()
Definition chunkio.cpp:585
bool Close_Chunk()
Definition chunkio.cpp:448
uint32 Cur_Chunk_ID()
Definition chunkio.cpp:484
uint32 Cur_Micro_Chunk_ID()
Definition chunkio.cpp:622
uint32 Read(void *buf, uint32 nbytes)
Definition chunkio.cpp:692
bool Open_Chunk()
Definition chunkio.cpp:412
bool Open_Micro_Chunk()
Definition chunkio.cpp:557
bool Begin_Chunk(uint32 id)
Definition chunkio.cpp:108
bool End_Chunk()
Definition chunkio.cpp:148
virtual PersistClass * Create(void) const =0
DefinitionClass(void)
Definition definition.h:139
virtual bool Save(ChunkSaveClass &csave)
virtual bool Load(ChunkLoadClass &cload)
static DefinitionClass * Find_Definition(uint32 id, bool twiddle=true)
int Count(void) const
Definition Vector.H:507
virtual DefinitionClass * Twiddle(void) const
Definition twiddler.cpp:106
bool Load(ChunkLoadClass &cload)
Definition twiddler.cpp:196
bool Save(ChunkSaveClass &csave)
Definition twiddler.cpp:174
PersistClass * Create(void) const
Definition twiddler.cpp:135
TwiddlerClass(void)
Definition twiddler.cpp:80
const PersistFactoryClass & Get_Factory(void) const
Definition twiddler.cpp:162
virtual ~TwiddlerClass(void)
Definition twiddler.cpp:94
@ CLASSID_TWIDDLERS
#define CLASSID_DEFIDLIST_PARAM(_class, data, root_class_id, class_id, name)
Definition editable.h:328
else return(RetVal)
@ CHUNKID_TWIDDLER
Definition saveloadids.h:58
#define DECLARE_DEFINITION_FACTORY(_class, _id, _name)
#define TIMEGETTIME
Definition systimer.h:44
SimplePersistFactoryClass< TwiddlerClass, CHUNKID_TWIDDLER > _TwiddlerPersistFactory
Definition twiddler.cpp:72
@ VARID_INDIRECT_CLASSID
Definition twiddler.cpp:62
@ VARID_DEFINTION_ID
Definition twiddler.cpp:61
#define DECLARE_FORCE_LINK(module)
Definition wwhack.h:48