SMUDGE.CPP 19 KB

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