BASE.CPP 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/BASE.CPP 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : BASE.CPP *
  22. * *
  23. * Programmer : Bill Randolph *
  24. * *
  25. * Start Date : 03/27/95 *
  26. * *
  27. * Last Update : July 30, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * BaseClass::Get_Building -- Returns ptr to the built building for the given node *
  32. * BaseClass::Get_Node -- Finds the node that matches the cell specified. *
  33. * BaseClass::Get_Node -- Returns ptr to the node corresponding to given object *
  34. * BaseClass::Is_Built -- Tells if given item in the list has been built yet *
  35. * BaseClass::Is_Node -- Tells if the given building is part of our base list *
  36. * BaseClass::Load -- loads from a saved game file *
  37. * BaseClass::Next_Buildable -- returns ptr to the next node that needs to be built *
  38. * BaseClass::Read_INI -- INI reading routine *
  39. * BaseClass::Save -- saves to a saved game file *
  40. * BaseClass::Write_INI -- Writes all the base information to the INI database. *
  41. * BaseNodeClass::operator != -- inequality operator *
  42. * BaseNodeClass::operator == -- equality operator *
  43. * BaseNodeClass::operator > -- greater-than operator *
  44. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  45. #include "function.h"
  46. /***********************************************************************************************
  47. * BaseNodeClass::operator == -- equality operator *
  48. * *
  49. * INPUT: *
  50. * node node to test against *
  51. * *
  52. * OUTPUT: *
  53. * true = equal, false = not equal *
  54. * *
  55. * WARNINGS: *
  56. * none. *
  57. * *
  58. * HISTORY: *
  59. * 03/24/1995 BRR : Created. *
  60. *=============================================================================================*/
  61. int BaseNodeClass::operator == (BaseNodeClass const & node)
  62. {
  63. return(Type == node.Type && Cell == node.Cell);
  64. }
  65. /***********************************************************************************************
  66. * BaseNodeClass::operator != -- inequality operator *
  67. * *
  68. * INPUT: *
  69. * node node to test against *
  70. * *
  71. * OUTPUT: *
  72. * comparison result *
  73. * *
  74. * WARNINGS: *
  75. * none. *
  76. * *
  77. * HISTORY: *
  78. * 03/24/1995 BRR : Created. *
  79. *=============================================================================================*/
  80. int BaseNodeClass::operator !=(BaseNodeClass const & node)
  81. {
  82. return(!(*this == node));
  83. }
  84. /***********************************************************************************************
  85. * BaseNodeClass::operator > -- greater-than operator *
  86. * *
  87. * INPUT: *
  88. * node node to test against *
  89. * *
  90. * OUTPUT: *
  91. * comparison result *
  92. * *
  93. * WARNINGS: *
  94. * none. *
  95. * *
  96. * HISTORY: *
  97. * 03/24/1995 BRR : Created. *
  98. *=============================================================================================*/
  99. int BaseNodeClass::operator > (BaseNodeClass const & )
  100. {
  101. return(true);
  102. }
  103. /***********************************************************************************************
  104. * BaseClass::Load -- loads from a saved game file *
  105. * *
  106. * INPUT: *
  107. * file open file *
  108. * *
  109. * OUTPUT: *
  110. * true = success, false = failure *
  111. * *
  112. * WARNINGS: *
  113. * none. *
  114. * *
  115. * HISTORY: *
  116. * 03/24/1995 BRR : Created. *
  117. * 07/04/1996 JLB : Converted to demand driven data source. *
  118. *=============================================================================================*/
  119. bool BaseClass::Load(Straw & file)
  120. {
  121. int num_struct;
  122. int i;
  123. BaseNodeClass node;
  124. /*
  125. ** Read in & check the size of this class
  126. */
  127. if (file.Get(&i, sizeof(i)) != sizeof(i)) {
  128. return(false);
  129. }
  130. if (i != sizeof(*this)) {
  131. return(false);
  132. }
  133. /*
  134. ** Read in the House & the number of structures in the base
  135. */
  136. if (file.Get(&House, sizeof(House)) != sizeof(House)) {
  137. return(false);
  138. }
  139. if (file.Get(&num_struct, sizeof(num_struct)) != sizeof(num_struct)) {
  140. return(false);
  141. }
  142. /*
  143. ** Read each node entry & add it to the list
  144. */
  145. for (i = 0; i < num_struct; i++) {
  146. if (file.Get(&node, sizeof(node)) != sizeof(node)) {
  147. return(false);
  148. }
  149. Nodes.Add(node);
  150. }
  151. return(true);
  152. }
  153. /***********************************************************************************************
  154. * BaseClass::Save -- saves to a saved game file *
  155. * *
  156. * INPUT: *
  157. * file open file *
  158. * *
  159. * OUTPUT: *
  160. * true = success, false = failure *
  161. * *
  162. * WARNINGS: *
  163. * none. *
  164. * *
  165. * HISTORY: *
  166. * 03/24/1995 BRR : Created. *
  167. * 07/04/1996 JLB : Converted to supply driven data output. *
  168. *=============================================================================================*/
  169. bool BaseClass::Save(Pipe & file) const
  170. {
  171. int num_struct;
  172. int i;
  173. BaseNodeClass node;
  174. /*
  175. ** Write the size of this class
  176. */
  177. i = sizeof(*this);
  178. file.Put(&i, sizeof(i));
  179. /*
  180. ** Write the House & the number of structures in the base
  181. */
  182. file.Put(&House, sizeof(House));
  183. num_struct = Nodes.Count();
  184. file.Put(&num_struct, sizeof(num_struct));
  185. /*
  186. ** Write each node entry
  187. */
  188. for (i = 0; i < num_struct; i++) {
  189. node = Nodes[i];
  190. file.Put(&node, sizeof(node));
  191. }
  192. return(true);
  193. }
  194. /***********************************************************************************************
  195. * BaseClass::Is_Built -- Tells if given item in the list has been built yet *
  196. * *
  197. * INPUT: *
  198. * index index into base list *
  199. * *
  200. * OUTPUT: *
  201. * true = yes, false = no *
  202. * *
  203. * WARNINGS: *
  204. * none. *
  205. * *
  206. * HISTORY: *
  207. * 03/24/1995 BRR : Created. *
  208. *=============================================================================================*/
  209. bool BaseClass::Is_Built(int index) const
  210. {
  211. if (Get_Building(index) != NULL) {
  212. return(true);
  213. } else {
  214. return(false);
  215. }
  216. }
  217. /***********************************************************************************************
  218. * BaseClass::Get_Building -- Returns ptr to the built building for the given node *
  219. * *
  220. * INPUT: *
  221. * obj pointer to building to test *
  222. * *
  223. * OUTPUT: *
  224. * ptr to already-built building, NULL if none *
  225. * *
  226. * WARNINGS: *
  227. * none. *
  228. * *
  229. * HISTORY: *
  230. * 03/24/1995 BRR : Created. *
  231. * 07/30/1996 JLB : Handle arbitrary overlapper list length. *
  232. *=============================================================================================*/
  233. BuildingClass * BaseClass::Get_Building(int index) const
  234. {
  235. ObjectClass * obj[1 + ARRAY_SIZE(Map[(CELL)0].Overlapper)];
  236. /*
  237. ** Check the location on the map where this building should be; if it's
  238. ** there, return a pointer to it.
  239. */
  240. CELL cell = Nodes[index].Cell;
  241. obj[0] = Map[cell].Cell_Building();
  242. int count = 1;
  243. for (int xindex = 0; xindex < ARRAY_SIZE(Map[cell].Overlapper); xindex++) {
  244. if (Map[cell].Overlapper[xindex] != NULL) {
  245. obj[count++] = Map[cell].Overlapper[xindex];
  246. }
  247. }
  248. BuildingClass * bldg = NULL;
  249. for (int i = 0; i < count; i++) {
  250. if (obj[i] &&
  251. Coord_Cell(obj[i]->Coord) == Nodes[index].Cell &&
  252. obj[i]->What_Am_I() == RTTI_BUILDING &&
  253. ((BuildingClass *)obj[i])->Class->Type == Nodes[index].Type) {
  254. bldg = (BuildingClass *)obj[i];
  255. break;
  256. }
  257. }
  258. return(bldg);
  259. }
  260. /***********************************************************************************************
  261. * BaseClass::Is_Node -- Tells if the given building is part of our base list *
  262. * *
  263. * INPUT: *
  264. * obj pointer to building to test *
  265. * *
  266. * OUTPUT: *
  267. * true = building is a node in the list, false = isn't *
  268. * *
  269. * WARNINGS: *
  270. * none. *
  271. * *
  272. * HISTORY: *
  273. * 03/24/1995 BRR : Created. *
  274. *=============================================================================================*/
  275. bool BaseClass::Is_Node(BuildingClass const * obj)
  276. {
  277. if (Get_Node(obj) != NULL) {
  278. return(true);
  279. } else {
  280. return(false);
  281. }
  282. }
  283. /***********************************************************************************************
  284. * BaseClass::Get_Node -- Returns ptr to the node corresponding to given object *
  285. * *
  286. * INPUT: *
  287. * obj pointer to building to test *
  288. * *
  289. * OUTPUT: *
  290. * ptr to node *
  291. * *
  292. * WARNINGS: *
  293. * none. *
  294. * *
  295. * HISTORY: *
  296. * 03/24/1995 BRR : Created. *
  297. *=============================================================================================*/
  298. BaseNodeClass * BaseClass::Get_Node(BuildingClass const * obj)
  299. {
  300. for (int i = 0; i < Nodes.Count(); i++) {
  301. if (obj->Class->Type == Nodes[i].Type && Coord_Cell(obj->Coord) == Nodes[i].Cell) {
  302. return(&Nodes[i]);
  303. }
  304. }
  305. return(NULL);
  306. }
  307. /***********************************************************************************************
  308. * BaseClass::Get_Node -- Finds the node that matches the cell specified. *
  309. * *
  310. * This routine is used to find a matching node the corresponds to the cell specified. *
  311. * *
  312. * INPUT: cell -- The cell to use in finding a match. *
  313. * *
  314. * OUTPUT: Returns a pointer to the matching node if found. If not found, then NULL is *
  315. * returned. *
  316. * *
  317. * WARNINGS: none *
  318. * *
  319. * HISTORY: *
  320. * 03/12/1996 JLB : Created. *
  321. *=============================================================================================*/
  322. BaseNodeClass * BaseClass::Get_Node(CELL cell)
  323. {
  324. for (int index = 0; index < Nodes.Count(); index++) {
  325. if (cell == Nodes[index].Cell) {
  326. return(&Nodes[index]);
  327. }
  328. }
  329. return(NULL);
  330. }
  331. /***********************************************************************************************
  332. * BaseClass::Next_Buildable -- returns ptr to the next node that needs to be built *
  333. * *
  334. * If 'type' is not NONE, returns ptr to the next "hole" in the list of the given type. *
  335. * Otherwise, returns ptr to the next hole in the list of any type. *
  336. * *
  337. * INPUT: *
  338. * type type of building to check for *
  339. * *
  340. * OUTPUT: *
  341. * ptr to a BaseNodeClass, NULL if none *
  342. * *
  343. * WARNINGS: *
  344. * none. *
  345. * *
  346. * HISTORY: *
  347. * 03/24/1995 BRR : Created. *
  348. *=============================================================================================*/
  349. BaseNodeClass * BaseClass::Next_Buildable(StructType type)
  350. {
  351. /*
  352. ** Loop through all node entries, returning a pointer to the first
  353. ** un-built one that matches the requested type.
  354. */
  355. for (int i = 0; i < Nodes.Count(); i++) {
  356. /*
  357. ** For STRUCT_NONE, return the first hole found
  358. */
  359. if (type == STRUCT_NONE) {
  360. if (!Is_Built(i)) {
  361. return(&Nodes[i]);
  362. }
  363. } else {
  364. /*
  365. ** For a "real" building type, return the first hold for that type
  366. */
  367. if (Nodes[i].Type==type && !Is_Built(i)) {
  368. return(&Nodes[i]);
  369. }
  370. }
  371. }
  372. // If no entry could be found, then create a fake one that will allow
  373. // placement of the building. Make it static and reuse the next time this
  374. // routine is called.
  375. return(NULL);
  376. }
  377. /***********************************************************************************************
  378. * BaseClass::Read_INI -- INI reading routine *
  379. * *
  380. * INI entry format: *
  381. * BLDG=COORDINATE *
  382. * BLDG=COORDINATE *
  383. * ... *
  384. * *
  385. * INPUT: *
  386. * buffer pointer to loaded INI file *
  387. * *
  388. * OUTPUT: *
  389. * none. *
  390. * *
  391. * WARNINGS: *
  392. * This routines assumes there is only one base defined for the scenario. *
  393. * *
  394. * HISTORY: *
  395. * 03/24/1995 BRR : Created. *
  396. * 02/20/1996 JLB : Fixed to know what house to build base from. *
  397. *=============================================================================================*/
  398. void BaseClass::Read_INI(CCINIClass & ini)
  399. {
  400. char buf[128];
  401. char uname[10];
  402. BaseNodeClass node; // node to add to list
  403. Mono_Clear_Screen();
  404. /*
  405. ** First, determine the house of the human player, and set the Base's house
  406. ** accordingly.
  407. */
  408. House = ini.Get_HousesType(INI_Name(), "Player", PlayerPtr->Class->House);
  409. /*
  410. ** Read the number of buildings that will go into the base node list
  411. */
  412. int count = ini.Get_Int(INI_Name(), "Count", 0);
  413. /*
  414. ** Read each entry in turn, in the same order they were written out.
  415. */
  416. for (int i = 0; i < count; i++) {
  417. /*
  418. ** Get an INI entry
  419. */
  420. sprintf(uname,"%03d",i);
  421. ini.Get_String(INI_Name(), uname, NULL, buf, sizeof(buf));
  422. /*
  423. ** Set the node's building type
  424. */
  425. node.Type = BuildingTypeClass::From_Name(strtok(buf,","));
  426. /*
  427. ** Read & set the node's coordinate
  428. */
  429. node.Cell = atoi(strtok(NULL,","));
  430. /*
  431. ** Add this node to the Base's list
  432. */
  433. Nodes.Add(node);
  434. }
  435. }
  436. /***********************************************************************************************
  437. * BaseClass::Write_INI -- Writes all the base information to the INI database. *
  438. * *
  439. * Use this routine to write all prebuild base information to the INI database specified. *
  440. * *
  441. * INPUT: ini -- Reference to the INI database to store the data to. *
  442. * *
  443. * OUTPUT: none *
  444. * *
  445. * WARNINGS: If there was any preexisting prebuild base data in the database, it will be *
  446. * be erased by this routine. *
  447. * *
  448. * HISTORY: *
  449. * 07/30/1996 JLB : Created. *
  450. *=============================================================================================*/
  451. void BaseClass::Write_INI(CCINIClass & ini)
  452. {
  453. /*
  454. ** Clear out all existing base data from the ini file.
  455. */
  456. ini.Clear(INI_Name());
  457. if (House != HOUSE_NONE) {
  458. /*
  459. ** Write out the owner of this buildable list.
  460. */
  461. ini.Put_HousesType(INI_Name(), "Player", House);
  462. /*
  463. ** Save the # of buildings in the Nodes list. This is essential because
  464. ** they must be read in the same order they were created, so "000" must be
  465. ** read first, etc.
  466. */
  467. ini.Put_Int(INI_Name(), "Count", Nodes.Count());
  468. /*
  469. ** Write each entry into the INI
  470. */
  471. for (int i = 0; i < Nodes.Count(); i++) {
  472. char buf[128];
  473. char uname[10];
  474. sprintf(uname,"%03d",i);
  475. sprintf(buf,"%s,%d",
  476. BuildingTypeClass::As_Reference(Nodes[i].Type).IniName,
  477. Nodes[i].Cell);
  478. ini.Put_String(INI_Name(), uname, buf);
  479. }
  480. }
  481. }