OVERLAY.CPP 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/OVERLAY.CPP 1 3/03/97 10:25a 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::Init -- Resets the overlay object system. *
  37. * OverlayClass::Mark -- Marks the overlay down on the map. *
  38. * OverlayClass::OverlayClass -- Overlay object constructor. *
  39. * OverlayClass::delete -- Returns a overlay object to the pool. *
  40. * OverlayClass::new -- Allocates a overlay object from pool *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "function.h"
  43. #include "overlay.h"
  44. HousesType OverlayClass::ToOwn = HOUSE_NONE;
  45. /***********************************************************************************************
  46. * OverlayClass::Init -- Resets the overlay object system. *
  47. * *
  48. * This routine resets the overlay object system. It is called *
  49. * prior to loading a new scenario. *
  50. * *
  51. * INPUT: none *
  52. * *
  53. * OUTPUT: none *
  54. * *
  55. * WARNINGS: none *
  56. * *
  57. * HISTORY: *
  58. * 05/24/1994 JLB : Created. *
  59. *=============================================================================================*/
  60. void OverlayClass::Init(void)
  61. {
  62. Overlays.Free_All();
  63. }
  64. /***********************************************************************************************
  65. * OverlayClass::new -- Allocates a overlay object from pool *
  66. * *
  67. * This routine is used to allocate a overlay object from the *
  68. * overlay object pool. *
  69. * *
  70. * INPUT: size -- The size of a overlay object (not used). *
  71. * *
  72. * OUTPUT: Returns with a pointer to an available overlay object. *
  73. * *
  74. * WARNINGS: none *
  75. * *
  76. * HISTORY: *
  77. * 05/17/1994 JLB : Created. *
  78. *=============================================================================================*/
  79. void * OverlayClass::operator new(size_t )
  80. {
  81. void * ptr = Overlays.Allocate();
  82. if (ptr) {
  83. ((OverlayClass *)ptr)->IsActive = true;
  84. }
  85. return(ptr);
  86. }
  87. /***********************************************************************************************
  88. * OverlayClass::delete -- Returns a overlay object to the pool. *
  89. * *
  90. * This routine will return a overlay object to the overlay object *
  91. * pool. A overlay so returned is available for allocation again. *
  92. * *
  93. * INPUT: ptr -- Pointer to the object to be returned. *
  94. * *
  95. * OUTPUT: none *
  96. * *
  97. * WARNINGS: none *
  98. * *
  99. * HISTORY: *
  100. * 05/17/1994 JLB : Created. *
  101. *=============================================================================================*/
  102. void OverlayClass::operator delete(void * ptr)
  103. {
  104. if (ptr) {
  105. ((OverlayClass *)ptr)->IsActive = false;
  106. }
  107. Overlays.Free((OverlayClass *)ptr);
  108. }
  109. /***********************************************************************************************
  110. * OverlayClass::OverlayClass -- Overlay object constructor. *
  111. * *
  112. * This is the constructor for a overlay object. *
  113. * *
  114. * INPUT: type -- The overlay object this is to become. *
  115. * *
  116. * pos -- The position on the map to place the object. *
  117. * *
  118. * OUTPUT: none *
  119. * *
  120. * WARNINGS: none *
  121. * *
  122. * HISTORY: *
  123. * 05/17/1994 JLB : Created. *
  124. *=============================================================================================*/
  125. OverlayClass::OverlayClass(OverlayType type, CELL pos, HousesType house) :
  126. ObjectClass(RTTI_OVERLAY, Overlays.ID(this)),
  127. Class(OverlayTypes.Ptr((int)type))
  128. {
  129. if (pos != -1) {
  130. ToOwn = house;
  131. Unlimbo(Cell_Coord(pos));
  132. ToOwn = HOUSE_NONE;
  133. }
  134. }
  135. /***********************************************************************************************
  136. * OverlayClass::Mark -- Marks the overlay down on the map. *
  137. * *
  138. * This routine will place the overlay onto the map. The overlay object is deleted by this *
  139. * operation. The map is updated to reflect the presence of the overlay. *
  140. * *
  141. * INPUT: mark -- The type of marking to perform. Only MARK_DOWN is supported. *
  142. * *
  143. * OUTPUT: bool; Was the overlay successfully marked? Failure occurs if it is not being *
  144. * marked down. *
  145. * *
  146. * WARNINGS: none *
  147. * *
  148. * HISTORY: *
  149. * 09/24/1994 JLB : Created. *
  150. * 12/23/1994 JLB : Checks low level legality before proceeding. *
  151. *=============================================================================================*/
  152. bool OverlayClass::Mark(MarkType mark)
  153. {
  154. assert(Overlays.ID(this) == ID);
  155. assert(IsActive);
  156. if (ObjectClass::Mark(mark)) {
  157. if (mark == MARK_DOWN) {
  158. CELL cell = Coord_Cell(Coord);
  159. CellClass * cellptr = &Map[cell];
  160. /*
  161. ** Walls have special logic when they are marked down.
  162. */
  163. if (Class->IsWall) {
  164. if (cellptr->Is_Clear_To_Build()) {
  165. cellptr->Overlay = Class->Type;
  166. cellptr->OverlayData = 0;
  167. cellptr->Redraw_Objects();
  168. cellptr->Wall_Update();
  169. Map.Zone_Reset(Class->IsCrushable ? MZONE_NORMAL : MZONE_NORMAL|MZONE_CRUSHER);
  170. /*
  171. ** Flag ownership of the cell if the 'global' ownership flag indicates that this
  172. ** is necessary for the overlay.
  173. */
  174. if (ToOwn != HOUSE_NONE) {
  175. cellptr->Owner = ToOwn;
  176. }
  177. } else {
  178. delete this;
  179. return(false);
  180. }
  181. } else {
  182. bool clear = false;
  183. if (!ScenarioInit) {
  184. if (Class->Type == OVERLAY_WATER_CRATE) {
  185. clear = cellptr->Is_Clear_To_Move(SPEED_FLOAT, false, false);
  186. } else {
  187. if (Class->Type == OVERLAY_STEEL_CRATE || Class->Type == OVERLAY_WOOD_CRATE) {
  188. clear = cellptr->Is_Clear_To_Move(SPEED_TRACK, false, false);
  189. } else {
  190. clear = cellptr->Is_Clear_To_Move(SPEED_TRACK, true, true);
  191. }
  192. }
  193. } else {
  194. clear = true;
  195. }
  196. if ((ScenarioInit || cellptr->Overlay == OVERLAY_NONE) && clear) {
  197. cellptr->Overlay = Class->Type;
  198. cellptr->OverlayData = 0;
  199. cellptr->Redraw_Objects();
  200. if (Class->Land == LAND_TIBERIUM) {
  201. cellptr->OverlayData = 1;
  202. cellptr->Tiberium_Adjust();
  203. }
  204. }
  205. }
  206. /*
  207. ** ***** Is this really needed?
  208. */
  209. cellptr->Recalc_Attributes();
  210. /*
  211. ** Remove the overlay and make sure the system thinks it was never placed down!
  212. */
  213. Map.Overlap_Up(Coord_Cell(Coord), this);
  214. IsDown = false;
  215. IsInLimbo = true;
  216. delete this;
  217. return(true);
  218. }
  219. }
  220. return(false);
  221. }
  222. /***********************************************************************************************
  223. * OverlayClass::Read_INI -- Reads the overlay data from an INI file. *
  224. * *
  225. * This routine is used to load a scenario's overlay data. The overlay objects are read *
  226. * from the INI file and then created on the map. *
  227. * *
  228. * INPUT: buffer -- Pointer to the INI file staging buffer. *
  229. * *
  230. * OUTPUT: none *
  231. * *
  232. * WARNINGS: Requires that all the buildings be placed first, so the scan for assigning wall *
  233. * ownership to the nearest building will work. *
  234. * HISTORY: *
  235. * 09/01/1994 JLB : Created. *
  236. * 07/24/1995 JLB : Specifically forbid manual crates in multiplayer scenarios. *
  237. *=============================================================================================*/
  238. void OverlayClass::Read_INI(CCINIClass & ini)
  239. {
  240. if (NewINIFormat > 1) {
  241. int len = ini.Get_UUBlock("OverlayPack", _staging_buffer, sizeof(_staging_buffer));
  242. if (len > 0) {
  243. BufferStraw bpipe(_staging_buffer, len);
  244. LCWStraw uncomp(LCWStraw::DECOMPRESS);
  245. uncomp.Get_From(&bpipe);
  246. for (CELL cell = 0; cell < MAP_CELL_TOTAL; cell++) {
  247. OverlayType classid;
  248. uncomp.Get(&classid, sizeof(classid));
  249. if (classid != OVERLAY_NONE) {
  250. if (Session.Type == GAME_NORMAL || !OverlayTypeClass::As_Reference(classid).IsCrate) {
  251. /*
  252. ** Don't allow placement of overlays on the top or bottom rows of
  253. ** the map.
  254. */
  255. if (cell >= MAP_CELL_W && cell <= MAP_CELL_TOTAL - MAP_CELL_W) {
  256. new OverlayClass(classid, cell);
  257. // Assign house ownership to cells with walls in 'em.
  258. if (OverlayTypeClass::As_Reference(classid).IsWall) {
  259. HousesType owner = HOUSE_NONE;
  260. int distance = 0x7FFFFFFF;
  261. for (int index = 0; index < Buildings.Count(); index++) {
  262. BuildingClass * building = Buildings.Ptr(index);
  263. int newdist = ::Distance(building->Center_Coord(), Cell_Coord(cell));
  264. if (newdist < distance) {
  265. distance = newdist;
  266. owner = building->Owner();
  267. }
  268. }
  269. Map[cell].Owner = owner;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. if (NewINIFormat < 2 || ini.Is_Present("Overlay")) {
  278. int len = ini.Entry_Count(INI_Name());
  279. for (int index = 0; index < len; index++) {
  280. char const * entry = ini.Get_Entry(INI_Name(), index);
  281. CELL cell = atoi(entry);
  282. OverlayType classid = ini.Get_OverlayType(INI_Name(), entry, OVERLAY_NONE);
  283. /*
  284. ** Don't allow placement of crates in the multiplayer scenarios.
  285. */
  286. if (classid != OVERLAY_NONE && (Session.Type == GAME_NORMAL || !OverlayTypeClass::As_Reference(classid).IsCrate)) {
  287. /*
  288. ** Don't allow placement of overlays on the top or bottom rows of
  289. ** the map.
  290. */
  291. if (cell >= MAP_CELL_W && cell <= MAP_CELL_TOTAL - MAP_CELL_W) {
  292. new OverlayClass(classid, cell);
  293. // Assign house ownership to cells with walls in 'em.
  294. if (OverlayTypeClass::As_Reference(classid).IsWall) {
  295. HousesType owner = HOUSE_NONE;
  296. int distance = 0x7FFFFFFF;
  297. for (int index = 0; index < Buildings.Count(); index++) {
  298. BuildingClass * building = Buildings.Ptr(index);
  299. int newdist = ::Distance(building->Center_Coord(), Cell_Coord(cell));
  300. if (newdist < distance) {
  301. distance = newdist;
  302. owner = building->Owner();
  303. }
  304. }
  305. Map[cell].Owner = owner;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. void OverlayClass::Write_INI(CCINIClass & ini)
  313. {
  314. /*
  315. ** First, clear out all existing unit data from the ini file.
  316. */
  317. ini.Clear(INI_Name());
  318. ini.Clear("OverlayPack");
  319. BufferPipe bpipe(_staging_buffer, sizeof(_staging_buffer));
  320. LCWPipe comppipe(LCWPipe::COMPRESS);
  321. comppipe.Put_To(&bpipe);
  322. int total = 0;
  323. CellClass * cellptr = &Map[(CELL)0];
  324. for (CELL index = 0; index < MAP_CELL_TOTAL; index++) {
  325. total += comppipe.Put(&cellptr->Overlay, sizeof(cellptr->Overlay));
  326. cellptr++;
  327. }
  328. if (total) {
  329. ini.Put_UUBlock("OverlayPack", _staging_buffer, total);
  330. }
  331. // for (CELL index = 0; index < MAP_CELL_TOTAL; index++) {
  332. // CellClass * cellptr = &Map[index];
  333. // if (cellptr->Overlay != OVERLAY_NONE) {
  334. // char uname[10];
  335. // sprintf(uname, "%d", index);
  336. // ini.Put_Overlay(INI_Name(), uname, cellptr->Overlay);
  337. // }
  338. // }
  339. }