CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

TEMPLATE.CPP (23132B)


      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:   F:\projects\c&c\vcs\code\template.cpv   2.18   16 Oct 1995 16:51:46   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 : TEMPLATE.CPP                                                 *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : May 17, 1994                                                 *
     28  *                                                                                             *
     29  *                  Last Update : January 23, 1995 [JLB]                                       *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   TemplateClass::As_Target -- Converts a template object into a target number.              *
     34  *   TemplateClass::Init -- Resets the template object system.                                 *
     35  *   TemplateClass::Mark -- Lifts or drops a template object.                                  *
     36  *   TemplateClass::Read_INI -- Reads the scenario control INI file.                           *
     37  *   TemplateClass::Select -- Select the template object.                                      *
     38  *   TemplateClass::TemplateClass -- Default constructor for template class objects.           *
     39  *   TemplateClass::TemplateClass -- Template object constructor.                              *
     40  *   TemplateClass::Unlimbo -- Places a template object into the game/map system.              *
     41  *   TemplateClass::Write_INI -- Writes the template objects to the INI file.                  *
     42  *   TemplateClass::delete -- Returns a template object to the pool.                           *
     43  *   TemplateClass::new -- Allocates a template object from pool                               *
     44  *   TemplateClass::Validate -- validates template pointer												  *
     45  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     46 
     47 #include	"function.h"
     48 #include	"template.h"
     49 
     50 
     51 /*
     52 ** This contains the value of the Virtual Function Table Pointer
     53 */
     54 void * TemplateClass::VTable;
     55 
     56 
     57 /***********************************************************************************************
     58  * TemplateClass::Validate -- validates template pointer													  *
     59  *                                                                                             *
     60  * INPUT:                                                                                      *
     61  *		none.																												  *
     62  *                                                                                             *
     63  * OUTPUT:                                                                                     *
     64  *		1 = ok, 0 = error																								  *
     65  *                                                                                             *
     66  * WARNINGS:                                                                                   *
     67  *		none.																												  *
     68  *                                                                                             *
     69  * HISTORY:                                                                                    *
     70  *   08/09/1995 BRR : Created.                                                                 *
     71  *=============================================================================================*/
     72 #ifdef CHEAT_KEYS
     73 int TemplateClass::Validate(void) const
     74 {
     75 	int num;
     76 
     77 	num = Templates.ID(this);
     78 	if (num < 0 || num >= TEMPLATE_MAX) {
     79 		Validate_Error("TEMPLATE");
     80 		return (0);
     81 	}
     82 	else
     83 		return (1);
     84 }
     85 #else
     86 #define	Validate()
     87 #endif
     88 
     89 
     90 /***********************************************************************************************
     91  * TemplateClass::Read_INI -- Reads the scenario control INI file.                             *
     92  *                                                                                             *
     93  *    This routine reads the scenario control INI file and creates all                         *
     94  *    templates specified therein.                                                             *
     95  *                                                                                             *
     96  * INPUT:   buffer   -- Pointer to the loaded scenario control INI file.                       *
     97  *                                                                                             *
     98  * OUTPUT:  none                                                                               *
     99  *                                                                                             *
    100  * WARNINGS:   none                                                                            *
    101  *                                                                                             *
    102  * HISTORY:                                                                                    *
    103  *   05/24/1994 JLB : Created.                                                                 *
    104  *=============================================================================================*/
    105 void TemplateClass::Read_INI(char *buffer)
    106 {
    107 	char	*tbuffer;	// Accumulation buffer of unit IDs.
    108 	int	len;			// Size of data in buffer.
    109 	CELL	cell;			// Cell of building.
    110 	char	buf[128];	// Working string staging buffer.
    111 
    112 	len = strlen(buffer) + 2;
    113 	tbuffer = buffer + len;
    114 
    115 	WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-len, buffer);
    116 	while (*tbuffer != '\0') {
    117 		TemplateType	temp;		// Terrain type.
    118 
    119 		cell = atoi(tbuffer);
    120 		WWGetPrivateProfileString(INI_Name(), tbuffer, NULL, buf, sizeof(buf)-1, buffer);
    121 		temp = TemplateTypeClass::From_Name(strtok(buf, ",\r\n"));
    122 		if (temp != TEMPLATE_NONE) {
    123 			new TemplateClass(temp, cell);
    124 		}
    125 		tbuffer += strlen(tbuffer)+1;
    126 	}
    127 }
    128 
    129 
    130 /***********************************************************************************************
    131  * TemplateClass::Write_INI -- Writes the template objects to the INI file.                    *
    132  *                                                                                             *
    133  *    This routine is used to write all the template objects out to the INI file specified.    *
    134  *    It is used by the scenario editor when saving the game.                                  *
    135  *                                                                                             *
    136  * INPUT:   buffer   -- Pointer to the INI file staging buffer.                                *
    137  *                                                                                             *
    138  * OUTPUT:  none                                                                               *
    139  *                                                                                             *
    140  * WARNINGS:   none                                                                            *
    141  *                                                                                             *
    142  * HISTORY:                                                                                    *
    143  *   05/28/1994 JLB : Created.                                                                 *
    144  *=============================================================================================*/
    145 void TemplateClass::Write_INI(char *buffer)
    146 {
    147 	char	uname[10];
    148 	char	buf[127];
    149 	char	*tbuffer;		// Accumulation buffer of unit IDs.
    150 
    151 	/*
    152 	**	First, clear out all existing template data from the ini file.
    153 	*/
    154 	tbuffer = buffer + strlen(buffer) + 2;
    155 	WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-strlen(buffer), buffer);
    156 	while (*tbuffer != '\0') {
    157 		WWWritePrivateProfileString(INI_Name(), tbuffer, NULL, buffer);
    158 		tbuffer += strlen(tbuffer)+1;
    159 	}
    160 
    161 	/*
    162 	**	Find all templates and write them to the file.
    163 	*/
    164 	for (int index = 0; index < MAP_CELL_TOTAL; index++) {
    165 		CellClass * ptr;
    166 
    167 		ptr = &Map[index];
    168 		if (ptr->TType != TEMPLATE_NONE && ptr->TIcon == 0) {
    169 			sprintf(uname, "%03d", index);
    170 			sprintf(buf, "%s", TemplateTypeClass::As_Reference(ptr->TType).IniName);
    171 			WWWritePrivateProfileString(INI_Name(), uname, buf, buffer);
    172 		}
    173 	}
    174 }
    175 
    176 
    177 /***********************************************************************************************
    178  * TemplateClass::TemplateClass -- Default constructor for template class objects.             *
    179  *                                                                                             *
    180  *    This is the default constructor for a template class object. This construction method    *
    181  *    should NEVER be used by the game except as a consequence of declaring an array of        *
    182  *    uninitialized template objects.                                                          *
    183  *                                                                                             *
    184  * INPUT:   none                                                                               *
    185  *                                                                                             *
    186  * OUTPUT:  none                                                                               *
    187  *                                                                                             *
    188  * WARNINGS:   none                                                                            *
    189  *                                                                                             *
    190  * HISTORY:                                                                                    *
    191  *   01/23/1995 JLB : Created.                                                                 *
    192  *=============================================================================================*/
    193 TemplateClass::TemplateClass(void) :
    194 	Class(0)
    195 {
    196 }
    197 
    198 
    199 /***********************************************************************************************
    200  * TemplateClass::As_Target -- Converts a template object into a target number.                *
    201  *                                                                                             *
    202  *    This routine will convert a template object into a target number. Because templates      *
    203  *    never exist as a template object in the game system, this routine will never be called.  *
    204  *                                                                                             *
    205  * INPUT:                                                                                      *
    206  *                                                                                             *
    207  * OUTPUT:                                                                                     *
    208  *                                                                                             *
    209  * WARNINGS:                                                                                   *
    210  *                                                                                             *
    211  * HISTORY:                                                                                    *
    212  *   09/25/1994 JLB : Created.                                                                 *
    213  *=============================================================================================*/
    214 TARGET TemplateClass::As_Target(void) const
    215 {
    216 	Validate();
    217 	return(Build_Target(KIND_TEMPLATE, Templates.ID(this)));
    218 }
    219 
    220 
    221 /***********************************************************************************************
    222  * TemplateClass::Init -- Resets the template object system.                                   *
    223  *                                                                                             *
    224  *    This routine resets the template object system. It is called                             *
    225  *    prior to loading a new scenario.                                                         *
    226  *                                                                                             *
    227  * INPUT:   none                                                                               *
    228  *                                                                                             *
    229  * OUTPUT:  none                                                                               *
    230  *                                                                                             *
    231  * WARNINGS:   none                                                                            *
    232  *                                                                                             *
    233  * HISTORY:                                                                                    *
    234  *   05/24/1994 JLB : Created.                                                                 *
    235  *=============================================================================================*/
    236 void TemplateClass::Init(void)
    237 {
    238 	TemplateClass *ptr;
    239 
    240 	Templates.Free_All();
    241 
    242 	ptr = new TemplateClass();
    243 	VTable = ((void **)(((char *)ptr) + sizeof(AbstractClass) - 4))[0];
    244 	delete ptr;
    245 }
    246 
    247 
    248 /***********************************************************************************************
    249  * TemplateClass::Mark -- Lifts or drops a template object.                                    *
    250  *                                                                                             *
    251  *    This routine handles placing or removing a template object. This                         *
    252  *    entails marking the map as appropriate and redisplaying affected                         *
    253  *    cells.                                                                                   *
    254  *                                                                                             *
    255  * INPUT:   mark  -- The marking operation to perform.                                         *
    256  *                                                                                             *
    257  * OUTPUT:  bool; Was the template successfully marked?                                        *
    258  *                                                                                             *
    259  * WARNINGS:   none                                                                            *
    260  *                                                                                             *
    261  * HISTORY:                                                                                    *
    262  *   05/17/1994 JLB : Created.                                                                 *
    263  *   12/23/1994 JLB : Examines low level legality before processing.                           *
    264  *=============================================================================================*/
    265 bool TemplateClass::Mark(MarkType mark)
    266 {
    267 	Validate();
    268 	static bool noup = false;
    269 	void const * iset = Class->Get_Image_Data();
    270 	if (iset && ObjectClass::Mark(mark)) {
    271 
    272 		void * map = Get_Icon_Set_Map(iset);
    273 
    274 		for (int y = 0; y < Class->Height; y++) {
    275 			for (int x = 0; x < Class->Width; x++) {
    276 				CELL cell = Coord_Cell(Coord) + y*MAP_CELL_W + x;
    277 				if (Map.In_Radar(cell)) {
    278 					CellClass * cellptr = &Map[cell];
    279 					int number = y*Class->Width + x;
    280 
    281 					/*
    282 					**	Determine if this logical icon actually maps to a real icon. If no real
    283 					**	icon is associated with this logical position, then don't do any action
    284 					**	since none is required.
    285 					*/
    286 					char * mapptr = (char*)map;
    287 					bool real = (mapptr[number] != -1);
    288 
    289 					if (real) {
    290 						/*
    291 						**	Lift the terrain object from the map.
    292 						*/
    293 						if (mark == MARK_UP && !noup) {
    294 							if (cellptr->TType == Class->Type && cellptr->TIcon == number) {
    295 								cellptr->TType = TEMPLATE_NONE;
    296 								cellptr->TIcon = 0;
    297 							}
    298 						}
    299 
    300 						/*
    301 						**	Place the terrain object down.
    302 						*/
    303 						if (mark == MARK_DOWN) {
    304 							if (*this == TEMPLATE_CLEAR1) {
    305 								cellptr->TType = TEMPLATE_NONE;
    306 								cellptr->TIcon = 0;
    307 							} else {
    308 								cellptr->TType = Class->Type;
    309 	//							cellptr->TIcon = real;
    310 								cellptr->TIcon = number;
    311 							}
    312 						}
    313 
    314 						cellptr->Redraw_Objects();
    315 						cellptr->Recalc_Attributes();
    316 					}
    317 				}
    318 			}
    319 		}
    320 
    321 		/*
    322 		**	When marking this template down onto the map, the map template numbers are update
    323 		**	but the template is removed from existence. Make sure that the deletion of the
    324 		**	template object doesn't also lift the template numbers up from the map.
    325 		*/
    326 		if (mark == MARK_DOWN) {
    327 			noup = true;
    328 			delete this;
    329 			noup = false;
    330 		}
    331 		return(true);
    332 	}
    333 	return(false);
    334 }
    335 
    336 
    337 /***********************************************************************************************
    338  * TemplateClass::new -- Allocates a template object from pool                                 *
    339  *                                                                                             *
    340  *    This routine is used to allocate a template object from the                              *
    341  *    template object pool.                                                                    *
    342  *                                                                                             *
    343  * INPUT:   size  -- The size of a template object (not used).                                 *
    344  *                                                                                             *
    345  * OUTPUT:  Returns with a pointer to an available template object.                            *
    346  *                                                                                             *
    347  * WARNINGS:   none                                                                            *
    348  *                                                                                             *
    349  * HISTORY:                                                                                    *
    350  *   05/17/1994 JLB : Created.                                                                 *
    351  *=============================================================================================*/
    352 void * TemplateClass::operator new(size_t )
    353 {
    354 	void * ptr = Templates.Allocate();
    355 	if (ptr) {
    356 		((TemplateClass *)ptr)->Set_Active();
    357 	}
    358 	return(ptr);
    359 }
    360 
    361 
    362 /***********************************************************************************************
    363  * TemplateClass::delete -- Returns a template object to the pool.                             *
    364  *                                                                                             *
    365  *    This routine will return a template object to the template object                        *
    366  *    pool. A template so returned is available for allocation again.                          *
    367  *                                                                                             *
    368  * INPUT:   ptr   -- Pointer to the object to be returned.                                     *
    369  *                                                                                             *
    370  * OUTPUT:  none                                                                               *
    371  *                                                                                             *
    372  * WARNINGS:   none                                                                            *
    373  *                                                                                             *
    374  * HISTORY:                                                                                    *
    375  *   05/17/1994 JLB : Created.                                                                 *
    376  *=============================================================================================*/
    377 void TemplateClass::operator delete(void *ptr)
    378 {
    379 	if (ptr) {
    380 		((TemplateClass *)ptr)->IsActive = false;
    381 	}
    382 	Templates.Free((TemplateClass *)ptr);
    383 }
    384 
    385 
    386 /***********************************************************************************************
    387  * TemplateClass::TemplateClass -- Template object constructor.                                *
    388  *                                                                                             *
    389  *    This is the constructor for a template object.                                           *
    390  *                                                                                             *
    391  * INPUT:   type  -- The template object this is to become.                                    *
    392  *                                                                                             *
    393  *          pos   -- The position on the map to place the object.                              *
    394  *                                                                                             *
    395  * OUTPUT:  none                                                                               *
    396  *                                                                                             *
    397  * WARNINGS:   none                                                                            *
    398  *                                                                                             *
    399  * HISTORY:                                                                                    *
    400  *   05/17/1994 JLB : Created.                                                                 *
    401  *=============================================================================================*/
    402 TemplateClass::TemplateClass(TemplateType type, CELL pos) :
    403 	Class(&TemplateTypeClass::As_Reference(type))
    404 {
    405 	if (pos != -1) {
    406 		Unlimbo(Cell_Coord(pos));
    407 	}
    408 }