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