CnC_Remastered_Collection

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

TARGET.CPP (32545B)


      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\target.cpv   2.17   16 Oct 1995 16:51:06   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 : TARGET.CPP                                                   *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : September 10, 1993                                           *
     28  *                                                                                             *
     29  *                  Last Update : August 27, 1995 [JLB]                                        *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   As_Aircraft -- Converts the target value into an aircraft pointer.                        *
     34  *   As_Animation -- Converts target value into animation pointer.                             *
     35  *   As_Building -- Converts a target value into a building object pointer.                    *
     36  *   As_Bullet -- Converts the target into a bullet pointer.                                   *
     37  *   As_Cell -- Converts a target value into a cell number.                                    *
     38  *   As_Coord -- Converts a target value into a coordinate value.                              *
     39  *   As_Infantry -- If the target is infantry, return a pointer to it.                         *
     40  *   As_Movement_Coord -- Fetches coordinate if trying to move to this target.                 *
     41  *   As_Object -- Converts a target value into an object pointer.                              *
     42  *   As_Team -- Converts a target number into a team pointer.                                  *
     43  *   As_TeamType -- Converts a target into a team type pointer.                                *
     44  *   As_Techno -- Converts a target value into a TechnoClass pointer.                          *
     45  *   As_Trigger -- Converts specified target into a trigger pointer.                           *
     46  *   As_Unit -- Converts a target value into a unit pointer.                                   *
     47  *   Target_Legal -- Determines if the specified target is legal.                              *
     48  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     49 
     50 #include	"function.h"
     51 #include	"target.h"
     52 
     53 
     54 /***********************************************************************************************
     55  * As_Trigger -- Converts specified target into a trigger pointer.                             *
     56  *                                                                                             *
     57  *    This routine will convert the specified target number into a trigger pointer.            *
     58  *                                                                                             *
     59  * INPUT:   target   -- The target number to convert.                                          *
     60  *                                                                                             *
     61  * OUTPUT:  Returns with the trigger pointer that the specified target number represents. If   *
     62  *          it doesn't represent a legal trigger object, then NULL is returned.                *
     63  *                                                                                             *
     64  * WARNINGS:   none                                                                            *
     65  *                                                                                             *
     66  * HISTORY:                                                                                    *
     67  *   07/08/1995 JLB : Created.                                                                 *
     68  *=============================================================================================*/
     69 TriggerClass * As_Trigger(TARGET target, bool check_active)
     70 {
     71 	TriggerClass* trigger = Is_Target_Trigger(target) ? Triggers.Raw_Ptr(Target_Value(target)) : NULL;
     72 	if (check_active && trigger != NULL && !trigger->IsActive) {
     73 		trigger = NULL;
     74 	}
     75 	return(trigger);
     76 }
     77 
     78 
     79 /***********************************************************************************************
     80  * As_Team -- Converts a target number into a team pointer.                                    *
     81  *                                                                                             *
     82  *    This routine will convert the specified target number into a team pointer.               *
     83  *                                                                                             *
     84  * INPUT:   target   -- The target number to convert.                                          *
     85  *                                                                                             *
     86  * OUTPUT:  Returns with the team object that the specified target number represents. If it    *
     87  *          doesn't represent a legal team then NULL is returned.                              *
     88  *                                                                                             *
     89  * WARNINGS:   none                                                                            *
     90  *                                                                                             *
     91  * HISTORY:                                                                                    *
     92  *   07/08/1995 JLB : Created.                                                                 *
     93  *=============================================================================================*/
     94 TeamClass * As_Team(TARGET target, bool check_active)
     95 {
     96 	TeamClass* team = Is_Target_Team(target) ? Teams.Raw_Ptr(Target_Value(target)) : NULL;
     97 	if (check_active && team != NULL && !team->IsActive) {
     98 		team = NULL;
     99 	}
    100 	return(team);
    101 }
    102 
    103 
    104 /***********************************************************************************************
    105  * As_TeamType -- Converts a target into a team type pointer.                                  *
    106  *                                                                                             *
    107  *    This routine will convert the specified target number into a team type pointer.          *
    108  *                                                                                             *
    109  * INPUT:   target   -- The target number to convert.                                          *
    110  *                                                                                             *
    111  * OUTPUT:  Returns with a pointer to the team type represented by the target number. If the   *
    112  *          target number doesn't represent a legal team type, then NULL is returned.          *
    113  *                                                                                             *
    114  * WARNINGS:   none                                                                            *
    115  *                                                                                             *
    116  * HISTORY:                                                                                    *
    117  *   07/08/1995 JLB : Created.                                                                 *
    118  *=============================================================================================*/
    119 TeamTypeClass * As_TeamType(TARGET target)
    120 {
    121 	return(Is_Target_TeamType(target) ? TeamTypes.Raw_Ptr(Target_Value(target)) : NULL);
    122 }
    123 
    124 
    125 /***********************************************************************************************
    126  * As_Animation -- Converts target value into animation pointer.                               *
    127  *                                                                                             *
    128  *    This routine will convert the specified target number into an animation pointer.         *
    129  *                                                                                             *
    130  * INPUT:   target   -- The target number to convert into an animation pointer.                *
    131  *                                                                                             *
    132  * OUTPUT:  Returns with a pointer to the legal animation that this target represents. If it   *
    133  *          doesn't represent a legal animation, then NULL is returned.                        *
    134  *                                                                                             *
    135  * WARNINGS:   none                                                                            *
    136  *                                                                                             *
    137  * HISTORY:                                                                                    *
    138  *   07/08/1995 JLB : Created.                                                                 *
    139  *=============================================================================================*/
    140 AnimClass * As_Animation(TARGET target, bool check_active)
    141 {
    142 	AnimClass* anim = Is_Target_Animation(target) ? Anims.Raw_Ptr(Target_Value(target)) : NULL;
    143 	if (check_active && anim != NULL && !anim->IsActive) {
    144 		anim = NULL;
    145 	}
    146 	return(anim);
    147 }
    148 
    149 
    150 /***********************************************************************************************
    151  * As_Bullet -- Converts the target into a bullet pointer.                                     *
    152  *                                                                                             *
    153  *    This routine will convert the specified target number into a bullet pointer.             *
    154  *                                                                                             *
    155  * INPUT:   target   -- The target number to convert.                                          *
    156  *                                                                                             *
    157  * OUTPUT:  Returns with a pointer to the bullet it specifies. If the target doesn't refer to  *
    158  *          a legal bullet, then NULL is returned.                                             *
    159  *                                                                                             *
    160  * WARNINGS:   none                                                                            *
    161  *                                                                                             *
    162  * HISTORY:                                                                                    *
    163  *   07/08/1995 JLB : Created.                                                                 *
    164  *=============================================================================================*/
    165 BulletClass * As_Bullet(TARGET target, bool check_active)
    166 {
    167 	BulletClass* bullet = Is_Target_Bullet(target) ? Bullets.Raw_Ptr(Target_Value(target)) : NULL;
    168 	if (check_active && bullet != NULL && !bullet->IsActive) {
    169 		bullet = NULL;
    170 	}
    171 	return(bullet);
    172 }
    173 
    174 
    175 /***********************************************************************************************
    176  * As_Aircraft -- Converts the target value into an aircraft pointer.                          *
    177  *                                                                                             *
    178  *    This routine will convert the specified target value into an aircraft object pointer.    *
    179  *                                                                                             *
    180  * INPUT:   target   -- The target value to convert.                                           *
    181  *                                                                                             *
    182  * OUTPUT:  Returns with a pointer to the aircraft that this target value represents. If the   *
    183  *          specified target value doesn't represent an aircraft, then NULL is returned.       *
    184  *                                                                                             *
    185  * WARNINGS:   none                                                                            *
    186  *                                                                                             *
    187  * HISTORY:                                                                                    *
    188  *   08/27/1995 JLB : Created.                                                                 *
    189  *=============================================================================================*/
    190 AircraftClass * As_Aircraft(TARGET target, bool check_active)
    191 {
    192 	AircraftClass* aircraft = Is_Target_Aircraft(target) ? Aircraft.Raw_Ptr(Target_Value(target)) : NULL;
    193 	if (check_active && aircraft != NULL && !aircraft->IsActive) {
    194 		aircraft = NULL;
    195 	}
    196 	return(aircraft);
    197 }
    198 
    199 
    200 /***********************************************************************************************
    201  * As_Techno -- Converts a target value into a TechnoClass pointer.                            *
    202  *                                                                                             *
    203  *    This routine will take the target value specified and convert it into a TechnoClass      *
    204  *    pointer if the target represents an object that has a TechnoClass.                       *
    205  *                                                                                             *
    206  * INPUT:   target   -- The target value to convert into a TechnoClass pointer.                *
    207  *                                                                                             *
    208  * OUTPUT:  Returns with a pointer to the associated object's TechnoClass. If the target       *
    209  *          cannot be converted into a TechnoClass pointer, then NULL is returned.             *
    210  *                                                                                             *
    211  * WARNINGS:   none                                                                            *
    212  *                                                                                             *
    213  * HISTORY:                                                                                    *
    214  *   06/02/1994 JLB : Created.                                                                 *
    215  *=============================================================================================*/
    216 TechnoClass * As_Techno(TARGET target, bool check_active)
    217 {
    218 	ObjectClass * obj = As_Object(target, check_active);
    219 
    220 	if (obj && obj->Is_Techno()) {
    221 		return(TechnoClass *)obj;
    222 	}
    223 	return(NULL);
    224 }
    225 
    226 
    227 /***********************************************************************************************
    228  * As_Object -- Converts a target value into an object pointer.                                *
    229  *                                                                                             *
    230  *    This routine is used to convert the target value specified into an object pointer. If    *
    231  *    the target doesn't represent an object or the target value is illegal, then NULL is      *
    232  *    returned.                                                                                *
    233  *                                                                                             *
    234  * INPUT:   target   -- The target value to convert from.                                      *
    235  *          check_active -- Check if the target is active, return NULL if not.                 *
    236  *                                                                                             *
    237  * OUTPUT:  Returns with a pointer to the object it represent, or NULL if not an object.       *
    238  *                                                                                             *
    239  * WARNINGS:   none                                                                            *
    240  *                                                                                             *
    241  * HISTORY:                                                                                    *
    242  *   05/27/1994 JLB : Created.                                                                 *
    243  *=============================================================================================*/
    244 ObjectClass * As_Object(TARGET target, bool check_active)
    245 {
    246 	int val = Target_Value(target);
    247 	ObjectClass * object = NULL;
    248 
    249 	switch (Target_Kind(target)) {
    250 		case KIND_INFANTRY:
    251 			object = Infantry.Raw_Ptr(val);
    252 			break;
    253 
    254 		case KIND_UNIT:
    255 			object = Units.Raw_Ptr(val);
    256 			break;
    257 
    258 		case KIND_BUILDING:
    259 			object = Buildings.Raw_Ptr(val);
    260 			break;
    261 
    262 		case KIND_AIRCRAFT:
    263 			object = Aircraft.Raw_Ptr(val);
    264 			break;
    265 
    266 		case KIND_TERRAIN:
    267 			object = Terrains.Raw_Ptr(val);
    268 			break;
    269 
    270 		case KIND_BULLET:
    271 			object = Bullets.Raw_Ptr(val);
    272 			break;
    273 
    274 		case KIND_ANIMATION:
    275 			object = Anims.Raw_Ptr(val);
    276 			break;
    277 
    278 		default:
    279 			break;
    280 	}
    281 
    282 	/*
    283 	**	Special check to ensure that a target value that references an
    284 	**	invalid object will not be converted back into an object pointer.
    285 	**	This condition is rare, but could occur in a network game if the
    286 	**	object it refers to is destroyed between the time an event message
    287 	**	is sent and when it is received.
    288 	*/
    289 	if (check_active && object != NULL && !object->IsActive) {
    290 		object = NULL;
    291 	}
    292 
    293 	return(object);
    294 }
    295 
    296 
    297 /***********************************************************************************************
    298  * As_Unit -- Converts a target value into a unit pointer.                                     *
    299  *                                                                                             *
    300  *    This routine is used to convert the target value specified into a pointer to a unit      *
    301  *    object.                                                                                  *
    302  *                                                                                             *
    303  * INPUT:   target   -- The target value to convert into a unit pointer.                       *
    304  *                                                                                             *
    305  * OUTPUT:  Returns with a pointer to the unit the target value represents or NULL if not      *
    306  *          a unit.                                                                            *
    307  *                                                                                             *
    308  * WARNINGS:   none                                                                            *
    309  *                                                                                             *
    310  * HISTORY:                                                                                    *
    311  *   05/27/1994 JLB : Created.                                                                 *
    312  *=============================================================================================*/
    313 UnitClass * As_Unit(TARGET target, bool check_active)
    314 {
    315 	UnitClass* unit = Is_Target_Unit(target) ? Units.Raw_Ptr(Target_Value(target)) : NULL;
    316 	if (check_active && unit != NULL && !unit->IsActive) {
    317 		unit = NULL;
    318 	}
    319 	return(unit);
    320 }
    321 
    322 
    323 /***********************************************************************************************
    324  * As_Infantry -- If the target is infantry, return a pointer to it.                           *
    325  *                                                                                             *
    326  *    This routine will translate the specified target value into an infantry pointer if the   *
    327  *    target actually represents an infantry object.                                           *
    328  *                                                                                             *
    329  * INPUT:   target   -- The target to convert to a pointer.                                    *
    330  *                                                                                             *
    331  * OUTPUT:  Returns a pointer to the infantry object that this target value represents. If     *
    332  *          the target doesn't represent an infantry object, then return NULL.                 *
    333  *                                                                                             *
    334  * WARNINGS:   none                                                                            *
    335  *                                                                                             *
    336  * HISTORY:                                                                                    *
    337  *   10/17/1994 JLB : Created.                                                                 *
    338  *=============================================================================================*/
    339 InfantryClass * As_Infantry(TARGET target, bool check_active)
    340 {
    341 	InfantryClass* infantry = Is_Target_Infantry(target) ? Infantry.Raw_Ptr(Target_Value(target)) : NULL;
    342 	if (check_active && infantry != NULL && !infantry->IsActive) {
    343 		infantry = NULL;
    344 	}
    345 	return(infantry);
    346 }
    347 
    348 
    349 #ifdef NEVER
    350 TerrainClass * As_Terrain(TARGET target)
    351 {
    352 	return(Is_Target_Terrain(target) ? &Terrains[Target_Value(target)] : NULL);
    353 }
    354 #endif
    355 
    356 
    357 /***********************************************************************************************
    358  * As_Building -- Converts a target value into a building object pointer.                      *
    359  *                                                                                             *
    360  *    This routine is used to convert the target value specified into a building pointer.      *
    361  *                                                                                             *
    362  * INPUT:   target   -- The target value to convert from.                                      *
    363  *                                                                                             *
    364  * OUTPUT:  Returns with a pointer to the building object that the target value represents.    *
    365  *          If it doesn't represent a building, then return NULL.                              *
    366  *                                                                                             *
    367  * WARNINGS:   none                                                                            *
    368  *                                                                                             *
    369  * HISTORY:                                                                                    *
    370  *   05/27/1994 JLB : Created.                                                                 *
    371  *=============================================================================================*/
    372 BuildingClass * As_Building(TARGET target, bool check_active)
    373 {
    374 	BuildingClass* building = Is_Target_Building(target) ? Buildings.Raw_Ptr(Target_Value(target)) : NULL;
    375 	if (check_active && building != NULL && !building->IsActive) {
    376 		building = NULL;
    377 	}
    378 	return(building);
    379 }
    380 
    381 
    382 /***********************************************************************************************
    383  * Target_Legal -- Determines if the specified target is legal.                                *
    384  *                                                                                             *
    385  *    This routine is used to check for the legality of the target value specified. It is      *
    386  *    necessary to call this routine if there is doubt about the the legality of the target.   *
    387  *    It is possible for the unit that a target value represents to be eliminated and thus     *
    388  *    rendering the target value invalid.                                                      *
    389  *                                                                                             *
    390  * INPUT:   target   -- The target value to check.                                             *
    391  *                                                                                             *
    392  * OUTPUT:  bool; Is the target value legal?                                                   *
    393  *                                                                                             *
    394  * WARNINGS:   none                                                                            *
    395  *                                                                                             *
    396  * HISTORY:                                                                                    *
    397  *   05/27/1994 JLB : Created.                                                                 *
    398  *=============================================================================================*/
    399 bool Target_Legal(TARGET target)
    400 {
    401 	if (target == TARGET_NONE) return(false);
    402 
    403 	ObjectClass * obj = As_Object(target, false);
    404 	if (obj) {
    405 		return(obj->IsActive);
    406 	}
    407 	return(true);
    408 }
    409 
    410 
    411 /***********************************************************************************************
    412  * As_Cell -- Converts a target value into a cell number.                                      *
    413  *                                                                                             *
    414  *    This routine is used to convert the target value specified, into a cell value. This is   *
    415  *    necessary for find path and other procedures that need a cell value.                     *
    416  *                                                                                             *
    417  * INPUT:   target   -- The target value to convert to a cell value.                           *
    418  *                                                                                             *
    419  * OUTPUT:  Returns with the target value expressed as a cell location.                        *
    420  *                                                                                             *
    421  * WARNINGS:   none                                                                            *
    422  *                                                                                             *
    423  * HISTORY:                                                                                    *
    424  *   05/27/1994 JLB : Created.                                                                 *
    425  *=============================================================================================*/
    426 CELL As_Cell(TARGET target)
    427 {
    428 	return(Coord_Cell(As_Coord(target)));
    429 }
    430 
    431 
    432 /***********************************************************************************************
    433  * As_Coord -- Converts a target value into a coordinate value.                                *
    434  *                                                                                             *
    435  *    This routine is used to convert the target value specified into a coordinate value. It   *
    436  *    is necessary for those procedures that require a coordinate value.                       *
    437  *                                                                                             *
    438  * INPUT:   target   -- The target value to convert.                                           *
    439  *                                                                                             *
    440  * OUTPUT:  Returns with the target expressed as a COORD value.                                *
    441  *                                                                                             *
    442  * WARNINGS:   none                                                                            *
    443  *                                                                                             *
    444  * HISTORY:                                                                                    *
    445  *   05/27/1994 JLB : Created.                                                                 *
    446  *   11/16/1994 JLB : Simplified.                                                              *
    447  *=============================================================================================*/
    448 COORDINATE As_Coord(TARGET target)
    449 {
    450 	if (Target_Legal(target)) {
    451 		/*
    452 		**	Cell target values are handled as a special case. The value of the target number is
    453 		**	actually the cell index number.
    454 		*/
    455 		if (Is_Target_Cell(target)) {
    456 			return(Cell_Coord((CELL)Target_Value(target)));
    457 		}
    458 
    459 		/*
    460 		**	Normal targets correspond to game objects. Fetch the object pointer and then ask it
    461 		**	for the center coordinate. Return the center coordinate as the target's coordinate.
    462 		*/
    463 		ObjectClass * obj = As_Object(target);
    464 		if (obj) {
    465 
    466 			/*
    467 			** If this is invalid memory or the object is dead then return 0
    468 			** This is a kludge to fix the problem of team target objects being assigned after
    469 			** the object is already destroyed - 1/15/97 3:13PM
    470 			*/
    471 			if (IsBadReadPtr ((void*)obj, sizeof (ObjectClass) ) || !obj->IsActive){
    472 //OutputDebugString ("C&C95 - As_Coord called for invalid target object\m");
    473 				return(0x00000000L);
    474 			}
    475 
    476 			return(obj->Target_Coord());
    477 		}
    478 	}
    479 
    480 	/*
    481 	**	An unrecognized target value results in a null coordinate value.
    482 	*/
    483 	return(0x00000000L);
    484 }
    485 
    486 
    487 /***********************************************************************************************
    488  * As_Movement_Coord -- Fetches coordinate if trying to move to this target.                   *
    489  *                                                                                             *
    490  *    This routine will convert the specified target into a coordinate location. This location *
    491  *    is used when moving to the target specified. For cells, this is the center of the cell.  *
    492  *    For special buildings that allow docking, it is the center location of the docking       *
    493  *    bay.                                                                                     *
    494  *                                                                                             *
    495  * INPUT:   target   -- The target to convert into a coordinate value.                         *
    496  *                                                                                             *
    497  * OUTPUT:  Returns with the docking coordinate of the target value specified.                 *
    498  *                                                                                             *
    499  * WARNINGS:   none                                                                            *
    500  *                                                                                             *
    501  * HISTORY:                                                                                    *
    502  *   08/27/1995 JLB : Created.                                                                 *
    503  *=============================================================================================*/
    504 COORDINATE As_Movement_Coord(TARGET target)
    505 {
    506 	if (Target_Legal(target)) {
    507 		/*
    508 		**	Cell target values are handled as a special case. The value of the target number is
    509 		**	actually the cell index number.
    510 		*/
    511 		if (Is_Target_Cell(target)) {
    512 			return(Cell_Coord((CELL)Target_Value(target)));
    513 		}
    514 
    515 		/*
    516 		**	Normal targets correspond to game objects. Fetch the object pointer and then ask it
    517 		**	for the center coordinate. Return the center coordinate as the target's coordinate.
    518 		*/
    519 		ObjectClass * obj = As_Object(target);
    520 		if (obj) {
    521 			return(obj->Docking_Coord());
    522 		}
    523 	}
    524 
    525 	/*
    526 	**	An unrecognized target value results in a null coordinate value.
    527 	*/
    528 	return(0x00000000L);
    529 }