SMUDGE.CPP 21.5 KB
Newer Older
P
PG-SteveT 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
//
// Copyright 2020 Electronic Arts Inc.
//
// TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
// software: you can redistribute it and/or modify it under the terms of 
// the GNU General Public License as published by the Free Software Foundation, 
// either version 3 of the License, or (at your option) any later version.

// TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
// in the hope that it will be useful, but with permitted additional restrictions 
// under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
// distributed with this program. You should have received a copy of the 
// GNU General Public License along with permitted additional restrictions 
// with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection

/* $Header:   F:\projects\c&c\vcs\code\smudge.cpv   2.18   16 Oct 1995 16:52:06   JOE_BOSTIC  $ */
/***********************************************************************************************
 ***             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               ***
 ***********************************************************************************************
 *                                                                                             *
 *                 Project Name : Command & Conquer                                            *
 *                                                                                             *
 *                    File Name : SMUDGE.CPP                                                   *
 *                                                                                             *
 *                   Programmer : Joe L. Bostic                                                *
 *                                                                                             *
 *                   Start Date : August 9, 1994                                               *
 *                                                                                             *
 *                  Last Update : July 24, 1995 [JLB]                                          *
 *                                                                                             *
 *---------------------------------------------------------------------------------------------*
 * Functions:                                                                                  *
 *   SmudgeClass::Init -- Initialize the smudge tracking system.                               *
 *   SmudgeClass::SmudgeClass -- Constructor for smudge objects.                               *
 *   SmudgeClass::operator delete -- Deletes the smudge from the tracking system.              *
 *   SmudgeClass::operator new -- Creator of smudge objects.                                   *
 *   SmudgeClass::Mark -- Marks a smudge down on the map.                                      *
 *   SmudgeClass::Read_INI -- Reads smudge data from an INI file.                              *
 *   SmudgeClass::Write_INI -- Writes the smudge data to an INI file.                          *
 *   SmudgeClass::Disown -- Disowns (removes) a building bib piece.                            *
 *   SmudgeClass::Validate -- validates smudge pointer													  *
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#include	"function.h"
#include	"smudge.h"


/*
** This contains the value of the Virtual Function Table Pointer
*/
void * SmudgeClass::VTable;

HousesType SmudgeClass::ToOwn = HOUSE_NONE;


/***********************************************************************************************
 * SmudgeClass::Validate -- validates smudge pointer														  *
 *                                                                                             *
 * INPUT:                                                                                      *
 *		none.																												  *
 *                                                                                             *
 * OUTPUT:                                                                                     *
 *		1 = ok, 0 = error																								  *
 *                                                                                             *
 * WARNINGS:                                                                                   *
 *		none.																												  *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   08/09/1995 BRR : Created.                                                                 *
 *=============================================================================================*/
#ifdef CHEAT_KEYS
int SmudgeClass::Validate(void) const
{
	int num;

	num = Smudges.ID(this);
	if (num < 0 || num >= SMUDGE_MAX) {
		Validate_Error("SMUDGE");
		return (0);
	}
	else
		return (1);
}
#else
#define	Validate()
#endif


/***********************************************************************************************
 * SmudgeClass::operator new -- Creator of smudge objects.                                     *
 *                                                                                             *
 *    This routine will allocate a smudge object from the smudge tracking pool.                *
 *                                                                                             *
 * INPUT:   none                                                                               *
 *                                                                                             *
 * OUTPUT:  Returns with a pointer to a newly allocated smudge object. If one couldn't be      *
 *          found, then NULL is returned.                                                      *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/01/1994 JLB : Created.                                                                 *
 *=============================================================================================*/
void * SmudgeClass::operator new(size_t )
{
	void * ptr = Smudges.Allocate();
	if (ptr) {
		((SmudgeClass *)ptr)->Set_Active();
	}
	return(ptr);
}


/***********************************************************************************************
 * SmudgeClass::operator delete -- Deletes the smudge from the tracking system.                *
 *                                                                                             *
 *    This routine is used to remove the smudge from the tracking system.                      *
 *                                                                                             *
 * INPUT:   ptr   -- Pointer to the smudge to delete.                                          *
 *                                                                                             *
 * OUTPUT:  none                                                                               *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/01/1994 JLB : Created.                                                                 *
 *=============================================================================================*/
void SmudgeClass::operator delete(void *ptr)
{
	if (ptr) {
		((SmudgeClass *)ptr)->IsActive = false;
	}
	Smudges.Free((SmudgeClass *)ptr);

	//Map.Validate();
}


/***********************************************************************************************
 * SmudgeClass::SmudgeClass -- Constructor for smudge objects.                                 *
 *                                                                                             *
 *    This is the typical constructor for smudge objects. If the position to place the         *
 *    smudge is not given, then the smudge will be initialized in a limbo state. If the        *
 *    smudge is placed on the map, then this operation causes the smudge object itself to be   *
 *    deleted and special map values updated to reflect the presence of a smudge.              *
 *                                                                                             *
 * INPUT:   type  -- The type of smudge to construct.                                          *
 *                                                                                             *
 *          pos   -- The position to place the smudge. If -1, then the smudge is initialized   *
 *                   into a limbo state.                                                       *
 *                                                                                             *
 * OUTPUT:  none                                                                               *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/01/1994 JLB : Created.                                                                 *
 *=============================================================================================*/
SmudgeClass::SmudgeClass(SmudgeType type, COORDINATE pos, HousesType house) :
	Class(&SmudgeTypeClass::As_Reference(type))
{
	if (pos != -1) {
		ToOwn = house;
		if (!Unlimbo(pos)) {
			Delete_This();
		}
		ToOwn = HOUSE_NONE;
	}
}


/***********************************************************************************************
 * SmudgeClass::Init -- Initialize the smudge tracking system.                                 *
 *                                                                                             *
 *    This routine is used during the scenario clearing process to initialize the smudge       *
 *    object tracking system to a null state.                                                  *
 *                                                                                             *
 * INPUT:   none                                                                               *
 *                                                                                             *
 * OUTPUT:  none                                                                               *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/01/1994 JLB : Created.                                                                 *
 *=============================================================================================*/
void SmudgeClass::Init(void)
{
	SmudgeClass *ptr;

	Smudges.Free_All();

	ptr = new SmudgeClass();
	VTable = ((void **)(((char *)ptr) + sizeof(AbstractClass) - 4))[0];
	delete ptr;
}


/***********************************************************************************************
 * SmudgeClass::Mark -- Marks a smudge down on the map.                                        *
 *                                                                                             *
 *    This routine will place the smudge on the map. If the map cell allows.                   *
 *                                                                                             *
 * INPUT:   mark  -- The type of marking to perform. Only MARK_DOWN is supported.              *
 *                                                                                             *
 * OUTPUT:  bool; Was the smudge marked successfully? Failure occurs if the smudge isn't       *
 *                marked DOWN.                                                                 *
 *                                                                                             *
 * WARNINGS:   The smudge object is DELETED by this routine.                                   *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/22/1994 JLB : Created.                                                                 *
 *   12/23/1994 JLB : Checks low level legality before proceeding.                             *
 *=============================================================================================*/
bool SmudgeClass::Mark(MarkType mark)
{
	Validate();
	if (ObjectClass::Mark(mark)) {
		if (mark == MARK_DOWN) {
			CELL origin = Coord_Cell(Coord);

			for (int w = 0; w < Class->Width; w++) {
				for (int h = 0; h < Class->Height; h++) {
					CELL newcell = origin + w + (h*MAP_CELL_W);
					if (Map.In_Radar(newcell)) {
						CellClass * cell = &Map[newcell];

						if (Class->IsBib) {
							cell->Smudge = Class->Type;
							cell->SmudgeData = w + (h*Class->Width);
							cell->Owner = ToOwn;
						} else {
							if (cell->Is_Generally_Clear()) {
								if (Class->IsCrater && cell->Smudge != SMUDGE_NONE && SmudgeTypeClass::As_Reference(cell->Smudge).IsCrater) {
									cell->SmudgeData++;
									cell->SmudgeData = (int)MIN((int)cell->SmudgeData, (int)4);
								}

								if (cell->Smudge == SMUDGE_NONE) {

									/*
									**	Special selection of a crater that starts as close to the
									**	specified coordinate as possible.
									*/
									if (Class->IsCrater) {
										cell->Smudge = (SmudgeType)(SMUDGE_CRATER1 + CellClass::Spot_Index(Coord));
									} else {
										cell->Smudge = Class->Type;
									}
									cell->SmudgeData = 0;
								}
							}
						}

						/*
						**	Flag everything that might be overlapping this cell to redraw itself.
						*/
						cell->Redraw_Objects();
					}
				}
			}

			/*
			**	Whether it was successful in placing, or not, delete the smudge object. It isn't
			**	needed once the map has been updated with the proper smudge data.
			*/
			Delete_This();
			return(true);
		}
	}
	return(false);
}


/***********************************************************************************************
 * SmudgeClass::Read_INI -- Reads smudge data from an INI file.                                *
 *                                                                                             *
 *    This routine is used by the scenario loader to read the smudge data in an INI file and   *
 *    create the appropriate smudge objects on the map.                                        *
 *                                                                                             *
 * INPUT:   buffer   -- Pointer to the INI file staging buffer.                                *
 *                                                                                             *
 * OUTPUT:  none                                                                               *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/01/1994 JLB : Created.                                                                 *
 *   07/24/1995 JLB : Sets the smudge data value as well.                                      *
 *=============================================================================================*/
void SmudgeClass::Read_INI(char *buffer)
{
	char	buf[128];	// Working string staging buffer.

	int len = strlen(buffer) + 2;
	char * tbuffer = buffer + len;

	WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-len, buffer);
	while (*tbuffer != '\0') {
		SmudgeType	smudge;		// Smudge type.

		WWGetPrivateProfileString(INI_Name(), tbuffer, NULL, buf, sizeof(buf)-1, buffer);
		smudge = SmudgeTypeClass::From_Name(strtok(buf, ","));
		if (smudge != SMUDGE_NONE) {
			char * ptr = strtok(NULL, ",");
			if (ptr) {
				int data = 0;
				CELL cell = atoi(ptr);
				ptr = strtok(NULL, ",");
				if (ptr) data = atoi(ptr);
				new SmudgeClass(smudge, Cell_Coord(cell));
				if (Map[cell].Smudge == smudge && data) {
					Map[cell].SmudgeData = data;
				}
			}
		}
		tbuffer += strlen(tbuffer)+1;
	}
}


/***********************************************************************************************
 * SmudgeClass::Write_INI -- Writes the smudge data to an INI file.                            *
 *                                                                                             *
 *    This routine is used by the scenario editor to write the smudge data to an INI file.     *
 *                                                                                             *
 * INPUT:   buffer   -- Pointer to the INI file staging buffer.                                *
 *                                                                                             *
 * OUTPUT:  none                                                                               *
 *                                                                                             *
 * WARNINGS:   none                                                                            *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   09/01/1994 JLB : Created.                                                                 *
 *   07/24/1995 JLB : Records the smudge data as well.                                         *
 *=============================================================================================*/
void SmudgeClass::Write_INI(char *buffer)
{
	char	uname[10];
	char	buf[127];
	char	*tbuffer;		// Accumulation buffer of unit IDs.

	/*
	**	First, clear out all existing template data from the ini file.
	*/
	tbuffer = buffer + strlen(buffer) + 2;
	WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-strlen(buffer), buffer);
	while (*tbuffer != '\0') {
		WWWritePrivateProfileString(INI_Name(), tbuffer, NULL, buffer);
		tbuffer += strlen(tbuffer)+1;
	}

	/*
	**	Find all templates and write them to the file.
	*/
	for (CELL index = 0; index < MAP_CELL_TOTAL; index++) {
		CellClass * ptr;

		ptr = &Map[index];
		if (ptr->Smudge != SMUDGE_NONE) {
			SmudgeTypeClass const * stype = &SmudgeTypeClass::As_Reference(ptr->Smudge);
			if (!stype->IsBib) {
				sprintf(uname, "%03d", index);
				sprintf(buf, "%s,%d,%d", stype->IniName, index, ptr->SmudgeData);
				WWWritePrivateProfileString(INI_Name(), uname, buf, buffer);
			}
		}
	}
}


/***********************************************************************************************
 * SmudgeClass::Disown -- Disowns (removes) a building bib piece.                              *
 *                                                                                             *
 *    This routine is used when a building is removed from the game. If there was any bib      *
 *    attached, this routine will be called to disown the cells and remove the bib artwork.    *
 *                                                                                             *
 * INPUT:   cell  -- The origin cell for this bib removal.                                     *
 *                                                                                             *
 * OUTPUT:  none                                                                               *
 *                                                                                             *
 * WARNINGS:   This is actually working on a temporary bib object. It is created for the sole  *
 *             purpose of calling this routine. It will be deleted immediately afterward.      *
 *                                                                                             *
 * HISTORY:                                                                                    *
 *   07/04/1995 JLB : Created.                                                                 *
 *=============================================================================================*/
void SmudgeClass::Disown(CELL cell)
{
	Validate();
	if (Class->IsBib) {
		for (int w = 0; w < Class->Width; w++) {
			for (int h = 0; h < Class->Height; h++) {
				CellClass & cellptr = Map[cell + w + (h*MAP_CELL_W)];

				if (cellptr.Overlay == OVERLAY_NONE || !OverlayTypeClass::As_Reference(cellptr.Overlay).IsWall) {
					cellptr.Smudge = SMUDGE_NONE;
					cellptr.SmudgeData = 0;
					cellptr.Owner = HOUSE_NONE;
					cellptr.Redraw_Objects();
				}
			}
		}
	}
}