SMUDGE.CPP 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/SMUDGE.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 : SMUDGE.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : August 9, 1994 *
  26. * *
  27. * Last Update : July 3, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * SmudgeClass::Disown -- Disowns (removes) a building bib piece. *
  32. * SmudgeClass::Init -- Initialize the smudge tracking system. *
  33. * SmudgeClass::Mark -- Marks a smudge down on the map. *
  34. * SmudgeClass::Read_INI -- Reads smudge data from an INI file. *
  35. * SmudgeClass::SmudgeClass -- Constructor for smudge objects. *
  36. * SmudgeClass::Write_INI -- Store all the smudge data to the INI database. *
  37. * SmudgeClass::operator delete -- Deletes the smudge from the tracking system. *
  38. * SmudgeClass::operator new -- Creator of smudge objects. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "function.h"
  41. #include "smudge.h"
  42. HousesType SmudgeClass::ToOwn = HOUSE_NONE;
  43. /***********************************************************************************************
  44. * SmudgeClass::operator new -- Creator of smudge objects. *
  45. * *
  46. * This routine will allocate a smudge object from the smudge tracking pool. *
  47. * *
  48. * INPUT: none *
  49. * *
  50. * OUTPUT: Returns with a pointer to a newly allocated smudge object. If one couldn't be *
  51. * found, then NULL is returned. *
  52. * *
  53. * WARNINGS: none *
  54. * *
  55. * HISTORY: *
  56. * 09/01/1994 JLB : Created. *
  57. *=============================================================================================*/
  58. void * SmudgeClass::operator new(size_t )
  59. {
  60. void * ptr = Smudges.Allocate();
  61. if (ptr != NULL) {
  62. ((SmudgeClass *)ptr)->Set_Active();
  63. }
  64. return(ptr);
  65. }
  66. /***********************************************************************************************
  67. * SmudgeClass::operator delete -- Deletes the smudge from the tracking system. *
  68. * *
  69. * This routine is used to remove the smudge from the tracking system. *
  70. * *
  71. * INPUT: ptr -- Pointer to the smudge to delete. *
  72. * *
  73. * OUTPUT: none *
  74. * *
  75. * WARNINGS: none *
  76. * *
  77. * HISTORY: *
  78. * 09/01/1994 JLB : Created. *
  79. *=============================================================================================*/
  80. void SmudgeClass::operator delete(void * ptr)
  81. {
  82. if (ptr != NULL) {
  83. ((SmudgeClass *)ptr)->IsActive = false;
  84. }
  85. Smudges.Free((SmudgeClass *)ptr);
  86. }
  87. /***********************************************************************************************
  88. * SmudgeClass::SmudgeClass -- Constructor for smudge objects. *
  89. * *
  90. * This is the typical constructor for smudge objects. If the position to place the *
  91. * smudge is not given, then the smudge will be initialized in a limbo state. If the *
  92. * smudge is placed on the map, then this operation causes the smudge object itself to be *
  93. * deleted and special map values updated to reflect the presence of a smudge. *
  94. * *
  95. * INPUT: type -- The type of smudge to construct. *
  96. * *
  97. * pos -- The position to place the smudge. If -1, then the smudge is initialized *
  98. * into a limbo state. *
  99. * *
  100. * OUTPUT: none *
  101. * *
  102. * WARNINGS: none *
  103. * *
  104. * HISTORY: *
  105. * 09/01/1994 JLB : Created. *
  106. *=============================================================================================*/
  107. SmudgeClass::SmudgeClass(SmudgeType type, COORDINATE pos, HousesType house) :
  108. ObjectClass(RTTI_SMUDGE, Smudges.ID(this)),
  109. Class(SmudgeTypes.Ptr((int)type))
  110. {
  111. if (pos != -1) {
  112. ToOwn = house;
  113. if (!Unlimbo(pos)) {
  114. delete this;
  115. } else {
  116. ToOwn = HOUSE_NONE;
  117. }
  118. }
  119. }
  120. /***********************************************************************************************
  121. * SmudgeClass::Init -- Initialize the smudge tracking system. *
  122. * *
  123. * This routine is used during the scenario clearing process to initialize the smudge *
  124. * object tracking system to a null state. *
  125. * *
  126. * INPUT: none *
  127. * *
  128. * OUTPUT: none *
  129. * *
  130. * WARNINGS: none *
  131. * *
  132. * HISTORY: *
  133. * 09/01/1994 JLB : Created. *
  134. *=============================================================================================*/
  135. void SmudgeClass::Init(void)
  136. {
  137. Smudges.Free_All();
  138. }
  139. /***********************************************************************************************
  140. * SmudgeClass::Mark -- Marks a smudge down on the map. *
  141. * *
  142. * This routine will place the smudge on the map. If the map cell allows. *
  143. * *
  144. * INPUT: mark -- The type of marking to perform. Only MARK_DOWN is supported. *
  145. * *
  146. * OUTPUT: bool; Was the smudge marked successfully? Failure occurs if the smudge isn't *
  147. * marked DOWN. *
  148. * *
  149. * WARNINGS: The smudge object is DELETED by this routine. *
  150. * *
  151. * HISTORY: *
  152. * 09/22/1994 JLB : Created. *
  153. * 12/23/1994 JLB : Checks low level legality before proceeding. *
  154. *=============================================================================================*/
  155. bool SmudgeClass::Mark(MarkType mark)
  156. {
  157. assert(Smudges.ID(this) == ID);
  158. assert(IsActive);
  159. if (ObjectClass::Mark(mark)) {
  160. if (mark == MARK_DOWN) {
  161. CELL origin = Coord_Cell(Coord);
  162. for (int w = 0; w < Class->Width; w++) {
  163. for (int h = 0; h < Class->Height; h++) {
  164. CELL newcell = origin + w + (h*MAP_CELL_W);
  165. if (Map.In_Radar(newcell)) {
  166. CellClass * cell = &Map[newcell];
  167. if (Class->IsBib) {
  168. cell->Smudge = Class->Type;
  169. cell->SmudgeData = w + (h*Class->Width);
  170. cell->Owner = ToOwn;
  171. } else {
  172. if (cell->Is_Clear_To_Move(SPEED_TRACK, true, true)) {
  173. if (Class->IsCrater && cell->Smudge != SMUDGE_NONE && SmudgeTypeClass::As_Reference(cell->Smudge).IsCrater) {
  174. cell->SmudgeData++;
  175. cell->SmudgeData = (int)min((int)cell->SmudgeData, (int)4);
  176. }
  177. if (cell->Smudge == SMUDGE_NONE) {
  178. /*
  179. ** Special selection of a crater that starts as close to the
  180. ** specified coordinate as possible.
  181. */
  182. if (Class->IsCrater) {
  183. cell->Smudge = (SmudgeType)(SMUDGE_CRATER1 + CellClass::Spot_Index(Coord));
  184. } else {
  185. cell->Smudge = Class->Type;
  186. }
  187. cell->SmudgeData = 0;
  188. }
  189. }
  190. }
  191. /*
  192. ** Flag everything that might be overlapping this cell to redraw itself.
  193. */
  194. cell->Redraw_Objects();
  195. }
  196. }
  197. }
  198. /*
  199. ** Whether it was successful in placing, or not, delete the smudge object. It isn't
  200. ** needed once the map has been updated with the proper smudge data. Fake this object
  201. ** as if it were never placed down!
  202. */
  203. Map.Overlap_Up(Coord_Cell(Coord), this);
  204. IsDown = false;
  205. IsInLimbo = true;
  206. delete this;
  207. return(true);
  208. }
  209. }
  210. return(false);
  211. }
  212. /***********************************************************************************************
  213. * SmudgeClass::Disown -- Disowns (removes) a building bib piece. *
  214. * *
  215. * This routine is used when a building is removed from the game. If there was any bib *
  216. * attached, this routine will be called to disown the cells and remove the bib artwork. *
  217. * *
  218. * INPUT: cell -- The origin cell for this bib removal. *
  219. * *
  220. * OUTPUT: none *
  221. * *
  222. * WARNINGS: This is actually working on a temporary bib object. It is created for the sole *
  223. * purpose of calling this routine. It will be deleted immediately afterward. *
  224. * *
  225. * HISTORY: *
  226. * 07/04/1995 JLB : Created. *
  227. *=============================================================================================*/
  228. void SmudgeClass::Disown(CELL cell)
  229. {
  230. assert(Smudges.ID(this) == ID);
  231. assert(IsActive);
  232. if (Class->IsBib) {
  233. for (int w = 0; w < Class->Width; w++) {
  234. for (int h = 0; h < Class->Height; h++) {
  235. CellClass & cellptr = Map[(CELL)(cell + w + (h*MAP_CELL_W))];
  236. if (cellptr.Overlay == OVERLAY_NONE || !OverlayTypeClass::As_Reference(cellptr.Overlay).IsWall) {
  237. cellptr.Smudge = SMUDGE_NONE;
  238. cellptr.SmudgeData = 0;
  239. if (!cellptr.IsFlagged) {
  240. cellptr.Owner = HOUSE_NONE;
  241. }
  242. cellptr.Redraw_Objects();
  243. }
  244. }
  245. }
  246. }
  247. }
  248. /***********************************************************************************************
  249. * SmudgeClass::Read_INI -- Reads smudge data from an INI file. *
  250. * *
  251. * This routine is used by the scenario loader to read the smudge data in an INI file and *
  252. * create the appropriate smudge objects on the map. *
  253. * *
  254. * INPUT: buffer -- Pointer to the INI file staging buffer. *
  255. * *
  256. * OUTPUT: none *
  257. * *
  258. * WARNINGS: none *
  259. * *
  260. * HISTORY: *
  261. * 09/01/1994 JLB : Created. *
  262. * 07/24/1995 JLB : Sets the smudge data value as well. *
  263. *=============================================================================================*/
  264. void SmudgeClass::Read_INI(CCINIClass & ini)
  265. {
  266. char buf[128]; // Working string staging buffer.
  267. int len = ini.Entry_Count(INI_Name());
  268. for (int index = 0; index < len; index++) {
  269. char const * entry = ini.Get_Entry(INI_Name(), index);
  270. SmudgeType smudge; // Smudge type.
  271. ini.Get_String(INI_Name(), entry, NULL, buf, sizeof(buf));
  272. smudge = SmudgeTypeClass::From_Name(strtok(buf, ","));
  273. if (smudge != SMUDGE_NONE) {
  274. char * ptr = strtok(NULL, ",");
  275. if (ptr != NULL) {
  276. int data = 0;
  277. CELL cell = atoi(ptr);
  278. ptr = strtok(NULL, ",");
  279. if (ptr != NULL) data = atoi(ptr);
  280. new SmudgeClass(smudge, Cell_Coord(cell));
  281. if (Map[cell].Smudge == smudge && data != 0) {
  282. Map[cell].SmudgeData = data;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. /***********************************************************************************************
  289. * SmudgeClass::Write_INI -- Store all the smudge data to the INI database. *
  290. * *
  291. * This routine will output all the smudge data to the INI database. *
  292. * *
  293. * INPUT: ini -- Reference to the INI database object. *
  294. * *
  295. * OUTPUT: none *
  296. * *
  297. * WARNINGS: none *
  298. * *
  299. * HISTORY: *
  300. * 07/03/1996 JLB : Created. *
  301. *=============================================================================================*/
  302. void SmudgeClass::Write_INI(CCINIClass & ini)
  303. {
  304. /*
  305. ** First, clear out all existing template data from the ini file.
  306. */
  307. ini.Clear(INI_Name());
  308. /*
  309. ** Find all templates and write them to the file.
  310. */
  311. for (CELL index = 0; index < MAP_CELL_TOTAL; index++) {
  312. CellClass * ptr;
  313. ptr = &Map[index];
  314. if (ptr->Smudge != SMUDGE_NONE) {
  315. SmudgeTypeClass const * stype = &SmudgeTypeClass::As_Reference(ptr->Smudge);
  316. if (!stype->IsBib) {
  317. char uname[10];
  318. char buf[127];
  319. sprintf(uname, "%d", index);
  320. sprintf(buf, "%s,%d,%d", stype->IniName, index, ptr->SmudgeData);
  321. ini.Put_String(INI_Name(), uname, buf);
  322. }
  323. }
  324. }
  325. }