SMUDGE.CPP 22 KB

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