dialogue.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/dialogue.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 9/14/01 12:10p $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "dialogue.h"
  36. #include "chunkio.h"
  37. #include "wwmath.h"
  38. ////////////////////////////////////////////////////////////////
  39. // Constants
  40. ////////////////////////////////////////////////////////////////
  41. enum
  42. {
  43. CHUNKID_OPTION_VARIABLES = 0x08040528,
  44. CHUNKID_DIALOGUE_VARIABLES = 0x08040529,
  45. CHUNKID_DIALOGUE_OPTION
  46. };
  47. enum
  48. {
  49. VARID_WEIGHT = 0,
  50. XXX_VARID_REMARK_TEXT_ID,
  51. VARID_CONVERSATION_ID,
  52. VARID_DIALOGUE_SILENCE = 0,
  53. };
  54. const char * const DIALOG_EVENT_NAMES[DIALOG_MAX] =
  55. {
  56. "TAKE_DAMAGE_FROM_FRIEND",
  57. "TAKE_DAMAGE_FROM_ENEMY",
  58. "DAMAGE_FRIEND",
  59. "DAMAGE_ENEMY",
  60. "KILLED_FRIEND",
  61. "KILLED_ENEMY",
  62. "SAW_FRIEND",
  63. "SAW_ENEMY",
  64. "OBSOLETE_01",
  65. "OBSOLETE_02",
  66. "DIE",
  67. "POKE_IDLE",
  68. "POKE_SEARCH",
  69. "POKE_COMBAT",
  70. "IDLE_TO_COMBAT",
  71. "IDLE_TO_SEARCH",
  72. "SEARCH_TO_COMBAT",
  73. "SEARCH_TO_IDLE",
  74. "COMBAT_TO_SEARCH",
  75. "COMBAT_TO_IDLE"
  76. };
  77. ////////////////////////////////////////////////////////////////
  78. //
  79. // DialogueOptionClass
  80. //
  81. ////////////////////////////////////////////////////////////////
  82. DialogueOptionClass::DialogueOptionClass (void)
  83. : Weight (1),
  84. ConversationID (0)
  85. {
  86. return ;
  87. }
  88. ////////////////////////////////////////////////////////////////
  89. //
  90. // DialogueOptionClass
  91. //
  92. ////////////////////////////////////////////////////////////////
  93. DialogueOptionClass::DialogueOptionClass (const DialogueOptionClass &src)
  94. : Weight (1),
  95. ConversationID (0)
  96. {
  97. (*this) = src;
  98. return ;
  99. }
  100. ////////////////////////////////////////////////////////////////
  101. //
  102. // ~DialogueOptionClass
  103. //
  104. ////////////////////////////////////////////////////////////////
  105. DialogueOptionClass::~DialogueOptionClass (void)
  106. {
  107. return ;
  108. }
  109. ////////////////////////////////////////////////////////////////
  110. //
  111. // operator=
  112. //
  113. ////////////////////////////////////////////////////////////////
  114. const DialogueOptionClass &
  115. DialogueOptionClass::operator= (const DialogueOptionClass &src)
  116. {
  117. Weight = src.Weight;
  118. ConversationID = src.ConversationID;
  119. return (*this);
  120. }
  121. ////////////////////////////////////////////////////////////////
  122. //
  123. // Save
  124. //
  125. ////////////////////////////////////////////////////////////////
  126. void
  127. DialogueOptionClass::Save (ChunkSaveClass &csave)
  128. {
  129. csave.Begin_Chunk (CHUNKID_OPTION_VARIABLES);
  130. WRITE_MICRO_CHUNK (csave, VARID_WEIGHT, Weight);
  131. WRITE_MICRO_CHUNK (csave, VARID_CONVERSATION_ID, ConversationID);
  132. csave.End_Chunk ();
  133. return ;
  134. }
  135. ////////////////////////////////////////////////////////////////
  136. //
  137. // Load
  138. //
  139. ////////////////////////////////////////////////////////////////
  140. void
  141. DialogueOptionClass::Load (ChunkLoadClass &cload)
  142. {
  143. while (cload.Open_Chunk ()) {
  144. switch (cload.Cur_Chunk_ID ()) {
  145. case CHUNKID_OPTION_VARIABLES:
  146. Load_Variables (cload);
  147. break;
  148. }
  149. cload.Close_Chunk ();
  150. }
  151. return ;
  152. }
  153. ///////////////////////////////////////////////////////////////////////
  154. //
  155. // Load_Variables
  156. //
  157. ///////////////////////////////////////////////////////////////////////
  158. void
  159. DialogueOptionClass::Load_Variables (ChunkLoadClass &cload)
  160. {
  161. //
  162. // Loop through all the microchunks that define the variables
  163. //
  164. while (cload.Open_Micro_Chunk ()) {
  165. switch (cload.Cur_Micro_Chunk_ID ()) {
  166. READ_MICRO_CHUNK (cload, VARID_WEIGHT, Weight);
  167. READ_MICRO_CHUNK (cload, VARID_CONVERSATION_ID, ConversationID);
  168. }
  169. cload.Close_Micro_Chunk ();
  170. }
  171. return ;
  172. }
  173. ////////////////////////////////////////////////////////////////
  174. //
  175. // DialogueClass
  176. //
  177. ////////////////////////////////////////////////////////////////
  178. DialogueClass::DialogueClass (void)
  179. : SilenceWeight (1)
  180. {
  181. return ;
  182. }
  183. ////////////////////////////////////////////////////////////////
  184. //
  185. // DialogueClass
  186. //
  187. ////////////////////////////////////////////////////////////////
  188. DialogueClass::DialogueClass (const DialogueClass &src)
  189. : SilenceWeight (1)
  190. {
  191. (*this) = src;
  192. return ;
  193. }
  194. ////////////////////////////////////////////////////////////////
  195. //
  196. // ~DialogueClass
  197. //
  198. ////////////////////////////////////////////////////////////////
  199. DialogueClass::~DialogueClass (void)
  200. {
  201. Free_Options ();
  202. return ;
  203. }
  204. ////////////////////////////////////////////////////////////////
  205. //
  206. // operator=
  207. //
  208. ////////////////////////////////////////////////////////////////
  209. const DialogueClass &
  210. DialogueClass::operator= (const DialogueClass &src)
  211. {
  212. SilenceWeight = src.SilenceWeight;
  213. //
  214. // Free any option objects we may contain
  215. //
  216. Free_Options ();
  217. //
  218. // Copy all the option objects from the src object
  219. //
  220. for (int index = 0; index < src.OptionList.Count (); index ++) {
  221. DialogueOptionClass *option = src.OptionList[index];
  222. if (option != NULL) {
  223. OptionList.Add (new DialogueOptionClass (*option));
  224. }
  225. }
  226. return (*this);
  227. }
  228. ////////////////////////////////////////////////////////////////
  229. //
  230. // Save
  231. //
  232. ////////////////////////////////////////////////////////////////
  233. void
  234. DialogueClass::Save (ChunkSaveClass &csave)
  235. {
  236. csave.Begin_Chunk (CHUNKID_DIALOGUE_VARIABLES);
  237. WRITE_MICRO_CHUNK (csave, VARID_DIALOGUE_SILENCE, SilenceWeight);
  238. csave.End_Chunk ();
  239. //
  240. // Save the options
  241. //
  242. for (int index = 0; index < OptionList.Count (); index ++) {
  243. csave.Begin_Chunk (CHUNKID_DIALOGUE_OPTION);
  244. OptionList[index]->Save (csave);
  245. csave.End_Chunk ();
  246. }
  247. return ;
  248. }
  249. ////////////////////////////////////////////////////////////////
  250. //
  251. // Load
  252. //
  253. ////////////////////////////////////////////////////////////////
  254. void
  255. DialogueClass::Load (ChunkLoadClass &cload)
  256. {
  257. Free_Options ();
  258. while (cload.Open_Chunk ()) {
  259. switch (cload.Cur_Chunk_ID ()) {
  260. case CHUNKID_DIALOGUE_VARIABLES:
  261. Load_Variables (cload);
  262. break;
  263. case CHUNKID_DIALOGUE_OPTION:
  264. {
  265. //
  266. // Create a new option object and add it to the list
  267. //
  268. DialogueOptionClass *option = new DialogueOptionClass;
  269. option->Load (cload);
  270. OptionList.Add (option);
  271. }
  272. break;
  273. }
  274. cload.Close_Chunk ();
  275. }
  276. return ;
  277. }
  278. ///////////////////////////////////////////////////////////////////////
  279. //
  280. // Load_Variables
  281. //
  282. ///////////////////////////////////////////////////////////////////////
  283. void
  284. DialogueClass::Load_Variables (ChunkLoadClass &cload)
  285. {
  286. //
  287. // Loop through all the microchunks that define the variables
  288. //
  289. while (cload.Open_Micro_Chunk ()) {
  290. switch (cload.Cur_Micro_Chunk_ID ()) {
  291. READ_MICRO_CHUNK (cload, VARID_DIALOGUE_SILENCE, SilenceWeight);
  292. }
  293. cload.Close_Micro_Chunk ();
  294. }
  295. return ;
  296. }
  297. ///////////////////////////////////////////////////////////////////////
  298. //
  299. // Free_Options
  300. //
  301. ///////////////////////////////////////////////////////////////////////
  302. void
  303. DialogueClass::Free_Options (void)
  304. {
  305. for (int index = 0; index < OptionList.Count (); index ++) {
  306. DialogueOptionClass *option = OptionList[index];
  307. if (option != NULL) {
  308. delete option;
  309. }
  310. }
  311. OptionList.Delete_All ();
  312. return ;
  313. }
  314. ///////////////////////////////////////////////////////////////////////
  315. //
  316. // Get_Conversation
  317. //
  318. ///////////////////////////////////////////////////////////////////////
  319. int
  320. DialogueClass::Get_Conversation (void)
  321. {
  322. int conv_id = 0;
  323. //
  324. // Make a number we can use to index linearly into the option list
  325. // to determine which one to use.
  326. //
  327. float total = SilenceWeight;
  328. for (int index = 0; index < OptionList.Count (); index ++) {
  329. total += OptionList[index]->Get_Weight ();
  330. }
  331. //
  332. // Choose a random value in this linear range
  333. //
  334. float value = WWMath::Random_Float (0, total);
  335. //
  336. // Now find the object this value corresponds to
  337. //
  338. float count = SilenceWeight;
  339. for (index = 0; value > count && index < OptionList.Count (); index ++) {
  340. conv_id = OptionList[index]->Get_Conversation_ID ();
  341. count += OptionList[index]->Get_Weight ();
  342. }
  343. return conv_id;
  344. }