CnC_Remastered_Collection

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

TEMPLATE.CPP (13524B)


      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/TEMPLATE.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 : 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::Init -- Resets the template object system.                                 *
     34  *   TemplateClass::Mark -- Lifts or drops a template object.                                  *
     35  *   TemplateClass::Select -- Select the template object.                                      *
     36  *   TemplateClass::TemplateClass -- Template object constructor.                              *
     37  *   TemplateClass::Unlimbo -- Places a template object into the game/map system.              *
     38  *   TemplateClass::delete -- Returns a template object to the pool.                           *
     39  *   TemplateClass::new -- Allocates a template object from pool                               *
     40  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     41 
     42 #include	"function.h"
     43 #include	"template.h"
     44 
     45 
     46 /***********************************************************************************************
     47  * TemplateClass::Init -- Resets the template object system.                                   *
     48  *                                                                                             *
     49  *    This routine resets the template object system. It is called                             *
     50  *    prior to loading a new scenario.                                                         *
     51  *                                                                                             *
     52  * INPUT:   none                                                                               *
     53  *                                                                                             *
     54  * OUTPUT:  none                                                                               *
     55  *                                                                                             *
     56  * WARNINGS:   none                                                                            *
     57  *                                                                                             *
     58  * HISTORY:                                                                                    *
     59  *   05/24/1994 JLB : Created.                                                                 *
     60  *=============================================================================================*/
     61 void TemplateClass::Init(void)
     62 {
     63 	Templates.Free_All();
     64 }
     65 
     66 
     67 /***********************************************************************************************
     68  * TemplateClass::Mark -- Lifts or drops a template object.                                    *
     69  *                                                                                             *
     70  *    This routine handles placing or removing a template object. This                         *
     71  *    entails marking the map as appropriate and redisplaying affected                         *
     72  *    cells.                                                                                   *
     73  *                                                                                             *
     74  * INPUT:   mark  -- The marking operation to perform.                                         *
     75  *                                                                                             *
     76  * OUTPUT:  bool; Was the template successfully marked?                                        *
     77  *                                                                                             *
     78  * WARNINGS:   none                                                                            *
     79  *                                                                                             *
     80  * HISTORY:                                                                                    *
     81  *   05/17/1994 JLB : Created.                                                                 *
     82  *   12/23/1994 JLB : Examines low level legality before processing.                           *
     83  *=============================================================================================*/
     84 bool TemplateClass::Mark(MarkType mark)
     85 {
     86 	assert(Templates.ID(this) == ID);
     87 	assert(IsActive);
     88 
     89 	static bool noup = false;
     90 	void const * iset = Get_Image_Data();
     91 	if (iset && ObjectClass::Mark(mark)) {
     92 
     93 		void * map = Get_Icon_Set_Map(iset);
     94 
     95 		for (int y = 0; y < Class->Height; y++) {
     96 			for (int x = 0; x < Class->Width; x++) {
     97 				CELL cell = Coord_Cell(Coord) + y*MAP_CELL_W + x;
     98 				if (Map.In_Radar(cell)) {
     99 					CellClass * cellptr = &Map[cell];
    100 					int number = y*Class->Width + x;
    101 
    102 					/*
    103 					**	Determine if this logical icon actually maps to a real icon. If no real
    104 					**	icon is associated with this logical position, then don't do any action
    105 					**	since none is required.
    106 					*/
    107 					char * mapptr = (char*)map;
    108 					bool real = (mapptr[number] != -1);
    109 
    110 					if (real) {
    111 						/*
    112 						**	Lift the terrain object from the map.
    113 						*/
    114 						if (mark == MARK_UP && !noup) {
    115 							if (cellptr->TType == Class->Type && cellptr->TIcon == number) {
    116 								cellptr->TType = TEMPLATE_NONE;
    117 								cellptr->TIcon = 0;
    118 							}
    119 						}
    120 
    121 						/*
    122 						**	Place the terrain object down.
    123 						*/
    124 						if (mark == MARK_DOWN) {
    125 							if (*this == TEMPLATE_CLEAR1) {
    126 								cellptr->TType = TEMPLATE_NONE;
    127 								cellptr->TIcon = 0;
    128 							} else {
    129 								cellptr->TType = Class->Type;
    130 								cellptr->TIcon = number;
    131 							}
    132 
    133 							/*
    134 							**	Make sure that no overlays or smudges exist after
    135 							**	placing the template down.
    136 							*/
    137 							cellptr->Smudge = SMUDGE_NONE;
    138 							cellptr->SmudgeData = 0;
    139 							cellptr->Overlay = OVERLAY_NONE;
    140 							cellptr->OverlayData = 0;
    141 						}
    142 
    143 						cellptr->Redraw_Objects();
    144 						cellptr->Recalc_Attributes();
    145 					}
    146 				}
    147 			}
    148 		}
    149 
    150 		/*
    151 		**	When marking this template down onto the map, the map template numbers are update
    152 		**	but the template is removed from existence. Make sure that the deletion of the
    153 		**	template object doesn't also lift the template numbers up from the map.
    154 		*/
    155 		if (mark == MARK_DOWN) {
    156 			noup = true;
    157 			delete this;
    158 			noup = false;
    159 		}
    160 		return(true);
    161 	}
    162 	return(false);
    163 }
    164 
    165 
    166 /***********************************************************************************************
    167  * TemplateClass::new -- Allocates a template object from pool                                 *
    168  *                                                                                             *
    169  *    This routine is used to allocate a template object from the                              *
    170  *    template object pool.                                                                    *
    171  *                                                                                             *
    172  * INPUT:   size  -- The size of a template object (not used).                                 *
    173  *                                                                                             *
    174  * OUTPUT:  Returns with a pointer to an available template object.                            *
    175  *                                                                                             *
    176  * WARNINGS:   none                                                                            *
    177  *                                                                                             *
    178  * HISTORY:                                                                                    *
    179  *   05/17/1994 JLB : Created.                                                                 *
    180  *=============================================================================================*/
    181 void * TemplateClass::operator new(size_t )
    182 {
    183 	void * ptr = Templates.Allocate();
    184 	if (ptr) {
    185 		((TemplateClass *)ptr)->IsActive = true;
    186 	}
    187 	return(ptr);
    188 }
    189 
    190 
    191 /***********************************************************************************************
    192  * TemplateClass::delete -- Returns a template object to the pool.                             *
    193  *                                                                                             *
    194  *    This routine will return a template object to the template object                        *
    195  *    pool. A template so returned is available for allocation again.                          *
    196  *                                                                                             *
    197  * INPUT:   ptr   -- Pointer to the object to be returned.                                     *
    198  *                                                                                             *
    199  * OUTPUT:  none                                                                               *
    200  *                                                                                             *
    201  * WARNINGS:   none                                                                            *
    202  *                                                                                             *
    203  * HISTORY:                                                                                    *
    204  *   05/17/1994 JLB : Created.                                                                 *
    205  *=============================================================================================*/
    206 void TemplateClass::operator delete(void * ptr)
    207 {
    208 	if (ptr) {
    209 		((TemplateClass *)ptr)->IsActive = false;
    210 	}
    211 	Templates.Free((TemplateClass *)ptr);
    212 }
    213 
    214 
    215 /***********************************************************************************************
    216  * TemplateClass::TemplateClass -- Template object constructor.                                *
    217  *                                                                                             *
    218  *    This is the constructor for a template object.                                           *
    219  *                                                                                             *
    220  * INPUT:   type  -- The template object this is to become.                                    *
    221  *                                                                                             *
    222  *          pos   -- The position on the map to place the object.                              *
    223  *                                                                                             *
    224  * OUTPUT:  none                                                                               *
    225  *                                                                                             *
    226  * WARNINGS:   none                                                                            *
    227  *                                                                                             *
    228  * HISTORY:                                                                                    *
    229  *   05/17/1994 JLB : Created.                                                                 *
    230  *=============================================================================================*/
    231 TemplateClass::TemplateClass(TemplateType type, CELL pos) :
    232 	ObjectClass(RTTI_TEMPLATE, Templates.ID(this)),
    233 	Class(TemplateTypes.Ptr((int)type))
    234 {
    235 	if (pos != -1) {
    236 		Unlimbo(Cell_Coord(pos));
    237 	}
    238 }