CnC_Remastered_Collection

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

FACING.CPP (11196B)


      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/FACING.CPP 1     3/03/97 10:24a 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 : FACING.CPP                                                   *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 03/21/95                                                     *
     28  *                                                                                             *
     29  *                  Last Update : March 21, 1995 [JLB]                                         *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   FacingClass::FacingClass -- Default constructor for the facing class.                     *
     34  *   FacingClass::Rotation_Adjust -- Perform a rotation adjustment to current facing.          *
     35  *   FacingClass::Set_Current -- Sets the current rotation value.                              *
     36  *   FacingClass::Set_Desired -- Sets the desired facing  value.                               *
     37  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     38 
     39 #include	"function.h"
     40 #include	"facing.h"
     41 
     42 
     43 /***********************************************************************************************
     44  * FacingClass::FacingClass -- Default constructor for the facing class.                       *
     45  *                                                                                             *
     46  *    This default constructor merely sets the desired and current facing values to be the     *
     47  *    same (North).                                                                            *
     48  *                                                                                             *
     49  * INPUT:   none                                                                               *
     50  *                                                                                             *
     51  * OUTPUT:  none                                                                               *
     52  *                                                                                             *
     53  * WARNINGS:   none                                                                            *
     54  *                                                                                             *
     55  * HISTORY:                                                                                    *
     56  *   03/21/1995 JLB : Created.                                                                 *
     57  *=============================================================================================*/
     58 FacingClass::FacingClass(void)
     59 {
     60 	CurrentFacing = DIR_N;
     61 	DesiredFacing = DIR_N;
     62 }
     63 
     64 
     65 /***********************************************************************************************
     66  * FacingClass::Set_Desired -- Sets the desired facing  value.                                 *
     67  *                                                                                             *
     68  *    This routine is used to set the desired facing value without altering the current        *
     69  *    facing setting. Typical use of this routine is when a vehicle needs to face a           *
     70  *    direction, but currently isn't facing the correct direction. After this routine is       *
     71  *    called, it is presumed that subsequent calls to Rotation_Adjust() will result in the     *
     72  *    eventual alignment of the current facing.                                                *
     73  *                                                                                             *
     74  * INPUT:   facing   -- The new facing to assign to the desired value.                         *
     75  *                                                                                             *
     76  * OUTPUT:  bool; Did the desired facing value actually change by this routine call?           *
     77  *                                                                                             *
     78  * WARNINGS:   none                                                                            *
     79  *                                                                                             *
     80  * HISTORY:                                                                                    *
     81  *   03/21/1995 JLB : Created.                                                                 *
     82  *=============================================================================================*/
     83 int FacingClass::Set_Desired(DirType facing)
     84 {
     85 	if (DesiredFacing != facing) {
     86 		DesiredFacing = facing;
     87 		return(true);
     88 	}
     89 	return(false);
     90 }
     91 
     92 
     93 /***********************************************************************************************
     94  * FacingClass::Set_Current -- Sets the current rotation value.                                *
     95  *                                                                                             *
     96  *    This routine will set the current rotation value. It is used to override the facing      *
     97  *    value without adjusting the desired setting.                                             *
     98  *                                                                                             *
     99  * INPUT:   facing   -- The new facing to assign to the current facing value.                  *
    100  *                                                                                             *
    101  * OUTPUT:  bool; Was the current setting changed by this routine. Failure means that the      *
    102  *                current setting was already at the value specified.                          *
    103  *                                                                                             *
    104  * WARNINGS:   none                                                                            *
    105  *                                                                                             *
    106  * HISTORY:                                                                                    *
    107  *   03/21/1995 JLB : Created.                                                                 *
    108  *=============================================================================================*/
    109 int FacingClass::Set_Current(DirType facing)
    110 {
    111 	if (CurrentFacing != facing) {
    112 		CurrentFacing = facing;
    113 		return(true);
    114 	}
    115 	return(false);
    116 }
    117 
    118 
    119 /***********************************************************************************************
    120  * FacingClass::Rotation_Adjust -- Perform a rotation adjustment to current facing.            *
    121  *                                                                                             *
    122  *    This routine is used when the current and desired facings differ but the current         *
    123  *    facing should be adjusted toward the desired facing. The amount of rotation to adjust    *
    124  *    is provided as a rotation rate parameter. Typical use of this routine is for turrets     *
    125  *    and other vehicle related rotating.                                                      *
    126  *                                                                                             *
    127  * INPUT:   rate  -- The rotation rate to use when adjusting the current facing toward the     *
    128  *                   desired facing. A value of 127 means instantaneous rotation.              *
    129  *                                                                                             *
    130  * OUTPUT:  bool; Did the rotation result in the current facing transitioning from one         *
    131  *                1/32 zone to another? If true, then the owning object most likely will       *
    132  *                need to be redrawn to reflect the change.                                    *
    133  *                                                                                             *
    134  * WARNINGS:   none                                                                            *
    135  *                                                                                             *
    136  * HISTORY:                                                                                    *
    137  *   03/21/1995 JLB : Created.                                                                 *
    138  *=============================================================================================*/
    139 int FacingClass::Rotation_Adjust(int rate)
    140 {
    141 	/*
    142 	**	Only perform the rotation adjustment if the desired facing is not the
    143 	**	same as the current facing.
    144 	*/
    145 	if (Is_Rotating()) {
    146 		rate = min(rate, 127);
    147 
    148 		DirType oldfacing = CurrentFacing;
    149 		int diff = Difference();
    150 
    151 		/*
    152 		**	If the allowed facing change is greater than the difference between
    153 		**	the current facing and the desired facing, then just snap the
    154 		**	facing to the new value.
    155 		*/
    156 		if (ABS(diff) < rate) {
    157 			CurrentFacing = DesiredFacing;
    158 		} else {
    159 
    160 			/*
    161 			**	Adjust the current facing clockwise or counterclockwise depending
    162 			**	on the shortest distance to the desired facing from the current
    163 			**	facing.
    164 			*/
    165 			if (diff < 0) {
    166 				CurrentFacing = (DirType)(CurrentFacing - (DirType)rate);
    167 			} else {
    168 				CurrentFacing = (DirType)(CurrentFacing + (DirType)rate);
    169 			}
    170 		}
    171 
    172 		/*
    173 		**	If this facing adjustment caused the current facing to rotate into a
    174 		**	new 1/32 rotation zone (likely to cause a redraw), then return
    175 		**	this fact with a true value.
    176 		*/
    177 		return(Dir_To_32(CurrentFacing) != Dir_To_32(oldfacing));
    178 	}
    179 	return(false);
    180 }