stringtwiddler.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 : wwtranslatedb *
  23. * *
  24. * $Archive:: /Commando/Code/wwtranslatedb/stringtwiddler.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 8/01/01 4:26p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stringtwiddler.h"
  36. #include "chunkio.h"
  37. #include "persistfactory.h"
  38. #include "translatedbids.h"
  39. #include "translatedb.h"
  40. #include "tdbcategories.h"
  41. //////////////////////////////////////////////////////////////////////////////
  42. // Persist factory
  43. //////////////////////////////////////////////////////////////////////////////
  44. SimplePersistFactoryClass<StringTwiddlerClass, CHUNKID_STRING_TWIDDLER> _StringTwiddlerPersistFactory;
  45. //////////////////////////////////////////////////////////////////////////////
  46. // Constants
  47. //////////////////////////////////////////////////////////////////////////////
  48. enum
  49. {
  50. CHUNKID_VARIABLES = 0x07310319,
  51. CHUNKID_BASE_CLASS,
  52. };
  53. enum
  54. {
  55. VARID_STRING_ID = 0x01,
  56. };
  57. ///////////////////////////////////////////////////////////////////////
  58. //
  59. // StringTwiddlerClass
  60. //
  61. ///////////////////////////////////////////////////////////////////////
  62. StringTwiddlerClass::StringTwiddlerClass (void)
  63. {
  64. return ;
  65. }
  66. ///////////////////////////////////////////////////////////////////////
  67. //
  68. // StringTwiddlerClass
  69. //
  70. ///////////////////////////////////////////////////////////////////////
  71. StringTwiddlerClass::StringTwiddlerClass (const StringTwiddlerClass &src) :
  72. TDBObjClass (src)
  73. {
  74. (*this) = src;
  75. return ;
  76. }
  77. ///////////////////////////////////////////////////////////////////////
  78. //
  79. // ~StringTwiddlerClass
  80. //
  81. ///////////////////////////////////////////////////////////////////////
  82. StringTwiddlerClass::~StringTwiddlerClass (void)
  83. {
  84. return ;
  85. }
  86. /////////////////////////////////////////////////////////////////
  87. //
  88. // operator=
  89. //
  90. /////////////////////////////////////////////////////////////////
  91. const StringTwiddlerClass &
  92. StringTwiddlerClass::operator= (const StringTwiddlerClass &src)
  93. {
  94. TDBObjClass::operator= (src);
  95. StringList = src.StringList;
  96. return *this;
  97. }
  98. ///////////////////////////////////////////////////////////////////////
  99. //
  100. // Get_Factory
  101. //
  102. ///////////////////////////////////////////////////////////////////////
  103. const PersistFactoryClass &
  104. StringTwiddlerClass::Get_Factory (void) const
  105. {
  106. return _StringTwiddlerPersistFactory;
  107. }
  108. /////////////////////////////////////////////////////////////////
  109. //
  110. // Save
  111. //
  112. /////////////////////////////////////////////////////////////////
  113. bool
  114. StringTwiddlerClass::Save (ChunkSaveClass &csave)
  115. {
  116. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  117. TDBObjClass::Save (csave);
  118. csave.End_Chunk ();
  119. csave.Begin_Chunk (CHUNKID_VARIABLES);
  120. //
  121. // Write each string ID to its own microchunk
  122. //
  123. for (int index = 0; index < StringList.Count (); index ++) {
  124. int string_id = StringList[index];
  125. WRITE_MICRO_CHUNK (csave, VARID_STRING_ID, string_id);
  126. }
  127. csave.End_Chunk ();
  128. return true;
  129. }
  130. /////////////////////////////////////////////////////////////////
  131. //
  132. // Load
  133. //
  134. /////////////////////////////////////////////////////////////////
  135. bool
  136. StringTwiddlerClass::Load (ChunkLoadClass &cload)
  137. {
  138. while (cload.Open_Chunk ()) {
  139. switch (cload.Cur_Chunk_ID ()) {
  140. case CHUNKID_BASE_CLASS:
  141. TDBObjClass::Load (cload);
  142. break;
  143. case CHUNKID_VARIABLES:
  144. Load_Variables (cload);
  145. break;
  146. }
  147. cload.Close_Chunk ();
  148. }
  149. return true;
  150. }
  151. /////////////////////////////////////////////////////////////////
  152. //
  153. // Load_Variables
  154. //
  155. /////////////////////////////////////////////////////////////////
  156. void
  157. StringTwiddlerClass::Load_Variables (ChunkLoadClass &cload)
  158. {
  159. while (cload.Open_Micro_Chunk ()) {
  160. switch (cload.Cur_Micro_Chunk_ID ()) {
  161. case VARID_STRING_ID:
  162. {
  163. //
  164. // Read the data from the chunk
  165. //
  166. int string_id = 0;
  167. LOAD_MICRO_CHUNK (cload, string_id);
  168. //
  169. // Store this string ID in our list if its valid
  170. //
  171. if (string_id != 0) {
  172. StringList.Add (string_id);
  173. }
  174. }
  175. break;
  176. }
  177. cload.Close_Micro_Chunk ();
  178. }
  179. return ;
  180. }
  181. /////////////////////////////////////////////////////////////////
  182. //
  183. // Get_String
  184. //
  185. /////////////////////////////////////////////////////////////////
  186. const WideStringClass &
  187. StringTwiddlerClass::Get_String (uint32 lang_id)
  188. {
  189. //
  190. // Copy the contents of one of the string objects
  191. // into our internal data
  192. //
  193. Randomize (lang_id);
  194. return TDBObjClass::Get_String (lang_id);
  195. }
  196. /////////////////////////////////////////////////////////////////
  197. //
  198. // Randomize
  199. //
  200. /////////////////////////////////////////////////////////////////
  201. void
  202. StringTwiddlerClass::Randomize (int lang_id)
  203. {
  204. //
  205. // Are there any entries in our twiddler list?
  206. //
  207. int count = StringList.Count ();
  208. if (count > 0) {
  209. //
  210. // Randomly pick a string from our list
  211. //
  212. int index = (rand () % count);
  213. TDBObjClass *object = TranslateDBClass::Find_Object (StringList[index]);
  214. if (object != NULL && object->As_StringTwiddlerClass () == NULL) {
  215. //
  216. // Copy the string contents into ourselves
  217. //
  218. Set_String (lang_id, object->Get_String ());
  219. EnglishString = object->Get_English_String ();
  220. SoundID = object->Get_Sound_ID ();
  221. AnimationName = object->Get_Animation_Name ();
  222. }
  223. }
  224. return ;
  225. }