OVERLAY.CPP 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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\overlay.cpv 2.17 16 Oct 1995 16:50:44 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 : OVERLAY.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : May 17, 1994 *
  26. * *
  27. * Last Update : July 24, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * OverlayClass::Read_INI -- Reads the overlay data from an INI file. *
  32. * OverlayClass::Write_INI -- Writes the overlay data to an INI file. *
  33. * OverlayClass::delete -- Returns a overlay object to the pool. *
  34. * OverlayClass::Init -- Resets the overlay object system. *
  35. * OverlayClass::new -- Allocates a overlay object from pool *
  36. * OverlayClass::OverlayClass -- Overlay object constructor. *
  37. * OverlayClass::Mark -- Marks the overlay down on the map. *
  38. * OverlayClass::Validate -- validates overlay *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "function.h"
  41. #include "overlay.h"
  42. /*
  43. ** This contains the value of the Virtual Function Table Pointer
  44. */
  45. void * OverlayClass::VTable;
  46. HousesType OverlayClass::ToOwn = HOUSE_NONE;
  47. OverlayClass::OverlayClass(void) : Class(0) {ToOwn = HOUSE_NONE;};
  48. /***********************************************************************************************
  49. * OverlayClass::Validate -- validates overlay *
  50. * *
  51. * INPUT: *
  52. * none. *
  53. * *
  54. * OUTPUT: *
  55. * 1 = ok, 0 = error *
  56. * *
  57. * WARNINGS: *
  58. * none. *
  59. * *
  60. * HISTORY: *
  61. * 08/09/1995 BRR : Created. *
  62. *=============================================================================================*/
  63. #ifdef CHEAT_KEYS
  64. int OverlayClass::Validate(void) const
  65. {
  66. int num;
  67. num = Overlays.ID(this);
  68. if (num < 0 || num >= OVERLAY_MAX) {
  69. Validate_Error("OVERLAY");
  70. return (0);
  71. }
  72. else
  73. return (1);
  74. }
  75. #else
  76. #define Validate()
  77. #endif
  78. /***********************************************************************************************
  79. * OverlayClass::Init -- Resets the overlay object system. *
  80. * *
  81. * This routine resets the overlay object system. It is called *
  82. * prior to loading a new scenario. *
  83. * *
  84. * INPUT: none *
  85. * *
  86. * OUTPUT: none *
  87. * *
  88. * WARNINGS: none *
  89. * *
  90. * HISTORY: *
  91. * 05/24/1994 JLB : Created. *
  92. *=============================================================================================*/
  93. void OverlayClass::Init(void)
  94. {
  95. OverlayClass *ptr;
  96. Overlays.Free_All();
  97. ptr = new OverlayClass();
  98. VTable = ((void **)(((char *)ptr) + sizeof(AbstractClass) - 4))[0];
  99. delete ptr;
  100. }
  101. /***********************************************************************************************
  102. * OverlayClass::new -- Allocates a overlay object from pool *
  103. * *
  104. * This routine is used to allocate a overlay object from the *
  105. * overlay object pool. *
  106. * *
  107. * INPUT: size -- The size of a overlay object (not used). *
  108. * *
  109. * OUTPUT: Returns with a pointer to an available overlay object. *
  110. * *
  111. * WARNINGS: none *
  112. * *
  113. * HISTORY: *
  114. * 05/17/1994 JLB : Created. *
  115. *=============================================================================================*/
  116. void * OverlayClass::operator new(size_t )
  117. {
  118. void * ptr = Overlays.Allocate();
  119. if (ptr) {
  120. ((OverlayClass *)ptr)->Set_Active();
  121. }
  122. return(ptr);
  123. }
  124. /***********************************************************************************************
  125. * OverlayClass::delete -- Returns a overlay object to the pool. *
  126. * *
  127. * This routine will return a overlay object to the overlay object *
  128. * pool. A overlay so returned is available for allocation again. *
  129. * *
  130. * INPUT: ptr -- Pointer to the object to be returned. *
  131. * *
  132. * OUTPUT: none *
  133. * *
  134. * WARNINGS: none *
  135. * *
  136. * HISTORY: *
  137. * 05/17/1994 JLB : Created. *
  138. *=============================================================================================*/
  139. void OverlayClass::operator delete(void *ptr)
  140. {
  141. if (ptr) {
  142. ((OverlayClass *)ptr)->IsActive = false;
  143. }
  144. Overlays.Free((OverlayClass *)ptr);
  145. //Map.Validate();
  146. }
  147. /***********************************************************************************************
  148. * OverlayClass::OverlayClass -- Overlay object constructor. *
  149. * *
  150. * This is the constructor for a overlay object. *
  151. * *
  152. * INPUT: type -- The overlay object this is to become. *
  153. * *
  154. * pos -- The position on the map to place the object. *
  155. * *
  156. * OUTPUT: none *
  157. * *
  158. * WARNINGS: none *
  159. * *
  160. * HISTORY: *
  161. * 05/17/1994 JLB : Created. *
  162. *=============================================================================================*/
  163. OverlayClass::OverlayClass(OverlayType type, CELL pos, HousesType house) :
  164. Class(&OverlayTypeClass::As_Reference(type))
  165. {
  166. if (pos != -1) {
  167. ToOwn = house;
  168. Unlimbo(Cell_Coord(pos));
  169. ToOwn = HOUSE_NONE;
  170. }
  171. }
  172. /***********************************************************************************************
  173. * OverlayClass::Mark -- Marks the overlay down on the map. *
  174. * *
  175. * This routine will place the overlay onto the map. The overlay object is deleted by this *
  176. * operation. The map is updated to reflect the presence of the overlay. *
  177. * *
  178. * INPUT: mark -- The type of marking to perform. Only MARK_DOWN is supported. *
  179. * *
  180. * OUTPUT: bool; Was the overlay successfully marked? Failure occurs if it is not being *
  181. * marked down. *
  182. * *
  183. * WARNINGS: none *
  184. * *
  185. * HISTORY: *
  186. * 09/24/1994 JLB : Created. *
  187. * 12/23/1994 JLB : Checks low level legality before proceeding. *
  188. *=============================================================================================*/
  189. bool OverlayClass::Mark(MarkType mark)
  190. {
  191. Validate();
  192. if (ObjectClass::Mark(mark)) {
  193. if (mark == MARK_DOWN) {
  194. CELL cell = Coord_Cell(Coord);
  195. CellClass * cellptr = &Map[cell];
  196. /*
  197. ** Road placement occurs in two steps. First the foundation is placed, but only
  198. ** on buildable terrain. Second, the road is completed, but only if the foundation
  199. ** was previously placed.
  200. */
  201. if (*this == OVERLAY_ROAD) {
  202. if ((cellptr->Overlay == OVERLAY_ROAD && cellptr->OverlayData == 0) ||
  203. (cellptr->Overlay == OVERLAY_NONE && cellptr->Is_Generally_Clear())) {
  204. if (cellptr->Overlay == OVERLAY_ROAD) {
  205. cellptr->OverlayData = 1;
  206. } else {
  207. cellptr->OverlayData = 0;
  208. }
  209. cellptr->Overlay = Class->Type;
  210. cellptr->Redraw_Objects();
  211. }
  212. } else {
  213. /*
  214. ** Walls have special logic when they are marked down.
  215. */
  216. if (Class->IsWall) {
  217. if (cellptr->Is_Generally_Clear() && cellptr->Overlay != OVERLAY_FLAG_SPOT) {
  218. cellptr->Overlay = Class->Type;
  219. cellptr->OverlayData = 0;
  220. cellptr->Redraw_Objects();
  221. cellptr->Wall_Update();
  222. /*
  223. ** Flag ownership of the cell if the 'global' ownership flag indicates that this
  224. ** is necessary for the overlay.
  225. */
  226. if (ToOwn != HOUSE_NONE) {
  227. cellptr->Owner = ToOwn;
  228. }
  229. } else {
  230. Delete_This();
  231. return(false);
  232. }
  233. } else {
  234. if ((cellptr->Overlay == OVERLAY_NONE || cellptr->Overlay == OVERLAY_SQUISH) && !cellptr->Cell_Terrain() && Ground[cellptr->Land_Type()].Build) {
  235. /*
  236. ** Increment the global crate counter. This is used to regulate
  237. ** the crate generation.
  238. */
  239. if (Class->IsCrate) CrateCount++;
  240. /*
  241. ** Don't show the squish unless the gross flag is active.
  242. */
  243. if (!Special.IsGross && Class->Type != OVERLAY_SQUISH) {
  244. cellptr->Overlay = Class->Type;
  245. cellptr->OverlayData = 0;
  246. }
  247. cellptr->Redraw_Objects();
  248. if (Class->Land == LAND_TIBERIUM) {
  249. cellptr->OverlayData = 1;
  250. cellptr->Tiberium_Adjust();
  251. } else {
  252. if (*this == OVERLAY_CONCRETE) {
  253. CELL newcell;
  254. /*
  255. ** Smudges go away when concrete is laid down.
  256. */
  257. cellptr->Smudge = SMUDGE_NONE;
  258. cellptr->SmudgeData = 0;
  259. cellptr->Concrete_Calc();
  260. /*
  261. ** Possibly add concrete to adjacent cells depending on whether this
  262. ** concrete is in an odd or even row.
  263. */
  264. if (Cell_X(cell) & 0x01) {
  265. newcell = Adjacent_Cell((CELL)(cellptr->Cell_Number()), FACING_W);
  266. } else {
  267. newcell = Adjacent_Cell((CELL)(cellptr->Cell_Number()), FACING_E);
  268. }
  269. if (Map[newcell].Overlay != OVERLAY_CONCRETE) {
  270. Class->Create_And_Place(newcell);
  271. }
  272. /*
  273. ** The display attributes must be recalculated for all adjacent
  274. ** cells since their shape can be altered by the presence of
  275. ** concrete at this location.
  276. */
  277. static FacingType _face[4] = {FACING_N, FACING_E, FACING_S, FACING_W};
  278. for (int index = 0; index < (sizeof(_face)/sizeof(_face[0])); index++) {
  279. CellClass * adjcell = cellptr->Adjacent_Cell(_face[index]);
  280. if (adjcell) adjcell->Concrete_Calc();
  281. }
  282. }
  283. }
  284. }
  285. }
  286. /*
  287. ** ***** Is this really needed?
  288. */
  289. cellptr->Recalc_Attributes();
  290. }
  291. Delete_This();
  292. return(true);
  293. }
  294. }
  295. return(false);
  296. }
  297. /***********************************************************************************************
  298. * OverlayClass::Read_INI -- Reads the overlay data from an INI file. *
  299. * *
  300. * This routine is used to load a scenario's overlay data. The overlay objects are read *
  301. * from the INI file and then created on the map. *
  302. * *
  303. * INPUT: buffer -- Pointer to the INI file staging buffer. *
  304. * *
  305. * OUTPUT: none *
  306. * *
  307. * WARNINGS: none *
  308. * *
  309. * HISTORY: *
  310. * 09/01/1994 JLB : Created. *
  311. * 07/24/1995 JLB : Specifically forbid manual crates in multiplayer scenarios. *
  312. *=============================================================================================*/
  313. void OverlayClass::Read_INI(char *buffer)
  314. {
  315. char *tbuffer;
  316. int len; // Length of data in buffer.
  317. char buf[128];
  318. len = strlen(buffer) + 2;
  319. tbuffer = buffer + len;
  320. WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-len, buffer);
  321. while (*tbuffer != '\0') {
  322. CELL cell;
  323. OverlayType classid;
  324. cell = atoi(tbuffer);
  325. WWGetPrivateProfileString(INI_Name(), tbuffer, NULL, buf, sizeof(buf)-1, buffer);
  326. classid = OverlayTypeClass::From_Name(strtok(buf, ",\n\r"));
  327. /*
  328. ** Don't allow placement of crates in the multiplayer scenarios.
  329. */
  330. if (classid != OVERLAY_NONE && (GameToPlay == GAME_NORMAL || !OverlayTypeClass::As_Reference(classid).IsCrate)) {
  331. /*
  332. ** Don't allow placement of overlays on the top or bottom rows of
  333. ** the map.
  334. */
  335. if (cell >= MAP_CELL_W && cell <= MAP_CELL_TOTAL - MAP_CELL_W) {
  336. new OverlayClass(classid, cell);
  337. }
  338. }
  339. tbuffer += strlen(tbuffer)+1;
  340. }
  341. }
  342. /***********************************************************************************************
  343. * OverlayClass::Write_INI -- Writes the overlay data to an INI file. *
  344. * *
  345. * This is used to output the overlay data to a scenario INI file. Typically, this is *
  346. * only used by the scenario editor. *
  347. * *
  348. * INPUT: buffer -- Pointer to the INI file staging buffer. *
  349. * *
  350. * OUTPUT: none *
  351. * *
  352. * WARNINGS: none *
  353. * *
  354. * HISTORY: *
  355. * 09/01/1994 JLB : Created. *
  356. *=============================================================================================*/
  357. void OverlayClass::Write_INI(char *buffer)
  358. {
  359. int index;
  360. char uname[10];
  361. char buf[128];
  362. char *tbuffer; // Accumulation buffer of unit IDs.
  363. /*
  364. ** First, clear out all existing unit data from the ini file.
  365. */
  366. tbuffer = buffer + strlen(buffer) + 2;
  367. WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-strlen(buffer), buffer);
  368. while (*tbuffer != '\0') {
  369. WWWritePrivateProfileString(INI_Name(), tbuffer, NULL, buffer);
  370. tbuffer += strlen(tbuffer)+1;
  371. }
  372. /*
  373. ** Write the unit data out.
  374. */
  375. for (index = 0; index < MAP_CELL_TOTAL; index++) {
  376. CellClass * cellptr = &Map[index];
  377. if (cellptr->Overlay != OVERLAY_NONE) {
  378. sprintf(uname, "%03d", index);
  379. sprintf(buf, "%s", OverlayTypeClass::As_Reference(cellptr->Overlay).IniName);
  380. WWWritePrivateProfileString(INI_Name(), uname, buf, buffer);
  381. }
  382. }
  383. }