OVERLAY.CPP 18 KB

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