OVERLAY.CPP 21 KB

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