BASE.CPP 29 KB

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