BASE.CPP 27 KB

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