BASE.CPP 27 KB

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