| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- /*
- ** Command & Conquer Generals(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** 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 ***
- ***********************************************************************************************
- * *
- * Project Name : WWSaveLoad *
- * *
- * $Archive:: /Commando/Code/wwsaveload/twiddler.cpp $*
- * *
- * Author:: Patrick Smith *
- * *
- * $Modtime:: 6/27/00 2:34p $*
- * *
- * $Revision:: 2 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "twiddler.h"
- #include "random.h"
- #include "saveloadids.h"
- #include "simpledefinitionfactory.h"
- #include "persistfactory.h"
- #include "win.h"
- #include "wwhack.h"
- DECLARE_FORCE_LINK( Twiddler )
- //////////////////////////////////////////////////////////////////////////////////
- // Constants
- //////////////////////////////////////////////////////////////////////////////////
- enum
- {
- CHUNKID_VARIABLES = 0x00000100,
- CHUNKID_BASE_CLASS = 0x00000200,
- };
- enum
- {
- VARID_DEFINTION_ID = 0x01,
- VARID_INDIRECT_CLASSID,
- };
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Static factories
- //
- //////////////////////////////////////////////////////////////////////////////////
- DECLARE_DEFINITION_FACTORY(TwiddlerClass, CLASSID_TWIDDLERS, "Twiddler") _TwiddlerFactory;
- SimplePersistFactoryClass<TwiddlerClass, CHUNKID_TWIDDLER> _TwiddlerPersistFactory;
- //////////////////////////////////////////////////////////////////////////////////
- //
- // TwiddlerClass
- //
- //////////////////////////////////////////////////////////////////////////////////
- TwiddlerClass::TwiddlerClass (void)
- : m_IndirectClassID (0)
-
- {
- CLASSID_DEFIDLIST_PARAM (TwiddlerClass, m_DefinitionList, 0, m_IndirectClassID, "Preset List");
- return ;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // ~TwiddlerClass
- //
- //////////////////////////////////////////////////////////////////////////////////
- TwiddlerClass::~TwiddlerClass (void)
- {
- return ;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Twiddle
- //
- //////////////////////////////////////////////////////////////////////////////////
- DefinitionClass *
- TwiddlerClass::Twiddle (void) const
- {
- DefinitionClass *definition = NULL;
- if (m_DefinitionList.Count () > 0) {
-
- //
- // Get a random index into our definition list
- //
- RandomClass randomizer (::GetTickCount ());
- int index = randomizer (0, m_DefinitionList.Count () - 1);
- //
- // Lookup the definition this entry represents
- //
- int def_id = m_DefinitionList[index];
- definition = DefinitionMgrClass::Find_Definition (def_id);
- }
- return definition;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Create
- //
- //////////////////////////////////////////////////////////////////////////////////
- PersistClass *
- TwiddlerClass::Create (void) const
- {
- PersistClass *retval = NULL;
- //
- // Pick a random definition
- //
- DefinitionClass *definition = Twiddle ();
- if (definition != NULL) {
- //
- // Indirect the creation to the definition we randomly selected
- //
- retval = definition->Create ();
- }
- return retval;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Get_Factory
- //
- //////////////////////////////////////////////////////////////////////////////////
- const PersistFactoryClass &
- TwiddlerClass::Get_Factory (void) const
- {
- return _TwiddlerPersistFactory;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Save
- //
- //////////////////////////////////////////////////////////////////////////////////
- bool
- TwiddlerClass::Save (ChunkSaveClass &csave)
- {
- bool retval = true;
- csave.Begin_Chunk (CHUNKID_VARIABLES);
- retval &= Save_Variables (csave);
- csave.End_Chunk ();
- csave.Begin_Chunk (CHUNKID_BASE_CLASS);
- retval &= DefinitionClass::Save (csave);
- csave.End_Chunk ();
- return retval;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Load
- //
- //////////////////////////////////////////////////////////////////////////////////
- bool
- TwiddlerClass::Load (ChunkLoadClass &cload)
- {
- bool retval = true;
- while (cload.Open_Chunk ()) {
- switch (cload.Cur_Chunk_ID ()) {
-
- case CHUNKID_VARIABLES:
- retval &= Load_Variables (cload);
- break;
- case CHUNKID_BASE_CLASS:
- retval &= DefinitionClass::Load (cload);
- break;
- }
- cload.Close_Chunk ();
- }
- return retval;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Save_Variables
- //
- //////////////////////////////////////////////////////////////////////////////////
- bool
- TwiddlerClass::Save_Variables (ChunkSaveClass &csave)
- {
- WRITE_MICRO_CHUNK (csave, VARID_INDIRECT_CLASSID, m_IndirectClassID)
- for (int index = 0; index < m_DefinitionList.Count (); index ++) {
-
- //
- // Save this definition ID to the chunk
- //
- int def_id = m_DefinitionList[index];
- WRITE_MICRO_CHUNK (csave, VARID_DEFINTION_ID, def_id)
- }
-
- return true;
- }
- //////////////////////////////////////////////////////////////////////////////////
- //
- // Load_Variables
- //
- //////////////////////////////////////////////////////////////////////////////////
- bool
- TwiddlerClass::Load_Variables (ChunkLoadClass &cload)
- {
- //
- // Start fresh
- //
- m_DefinitionList.Delete_All ();
- //
- // Loop through all the microchunks that define the variables
- //
- while (cload.Open_Micro_Chunk ()) {
- switch (cload.Cur_Micro_Chunk_ID ()) {
- READ_MICRO_CHUNK (cload, VARID_INDIRECT_CLASSID, m_IndirectClassID)
-
- case VARID_DEFINTION_ID:
- {
- //
- // Read the definition ID from the chunk and add it
- // to our list
- //
- int def_id = 0;
- cload.Read (&def_id, sizeof (def_id));
- m_DefinitionList.Add (def_id);
- }
- break;
- }
- cload.Close_Micro_Chunk ();
- }
- return true;
- }
|