CnC_Remastered_Collection

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

AIRCRAFT.CPP (186628B)


      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/AIRCRAFT.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 : AIRCRAFT.CPP                                                 *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : July 22, 1994                                                *
     28  *                                                                                             *
     29  *                  Last Update : November 2, 1996 [JLB]                                       *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   AircraftClass::AI -- Processes the normal non-graphic AI for the aircraft.                *
     34  *   AircraftClass::Active_Click_With -- Handles clicking over specified cell.                 *
     35  *   AircraftClass::Active_Click_With -- Handles clicking over specified object.               *
     36  *   AircraftClass::AircraftClass -- The constructor for aircraft objects.                     *
     37  *   AircraftClass::Can_Enter_Cell -- Determines if the aircraft can land at this location.    *
     38  *   AircraftClass::Can_Fire -- Checks to see if the aircraft can fire.                        *
     39  *   AircraftClass::Cell_Seems_Ok -- Checks to see if a cell is good to enter.                 *
     40  *   AircraftClass::Desired_Load_Dir -- Determines where passengers should line up.            *
     41  *   AircraftClass::Draw_It -- Renders an aircraft object at the location specified.           *
     42  *   AircraftClass::Draw_Rotors -- Draw rotor blades on the aircraft.                          *
     43  *   AircraftClass::Edge_Of_World_AI -- Detect if aircraft has exited the map.                 *
     44  *   AircraftClass::Enter_Idle_Mode -- Gives the aircraft an appropriate mission.              *
     45  *   AircraftClass::Exit_Object -- Unloads passenger from aircraft.                            *
     46  *   AircraftClass::Fire_At -- Handles firing a projectile from an aircraft.                   *
     47  *   AircraftClass::Fire_Direction -- Determines the direction of fire.                        *
     48  *   AircraftClass::Good_Fire_Location -- Searches for and finds a good spot to fire from.     *
     49  *   AircraftClass::Good_LZ -- Locates a good spot ot land.                                    *
     50  *   AircraftClass::In_Which_Layer -- Calculates the display layer of the aircraft.            *
     51  *   AircraftClass::Init -- Initialize the aircraft system to an empty state.                  *
     52  *   AircraftClass::Is_LZ_Clear -- Determines if landing zone is free for landing.             *
     53  *   AircraftClass::Landing_Takeoff_AI -- Handle aircraft take off and landing processing.     *
     54  *   AircraftClass::Look -- Aircraft will look if they are on the ground always.               *
     55  *   AircraftClass::Mission_Attack -- Handles the attack mission for aircraft.                 *
     56  *   AircraftClass::Mission_Enter -- Control aircraft to fly to the helipad or repair center.  *
     57  *   AircraftClass::Mission_Guard -- Handles aircraft in guard mode.                           *
     58  *   AircraftClass::Mission_Guard_Area -- Handles the aircraft guard area logic.               *
     59  *   AircraftClass::Mission_Hunt -- Maintains hunt AI for the aircraft.                        *
     60  *   AircraftClass::Mission_Move -- Handles movement mission.                                  *
     61  *   AircraftClass::Mission_Retreat -- Handles the aircraft logic for leaving the battlefield. *
     62  *   AircraftClass::Mission_Unload -- Handles unloading cargo.                                 *
     63  *   AircraftClass::Movement_AI -- Handles aircraft physical movement logic.                   *
     64  *   AircraftClass::New_LZ -- Find a good landing zone.                                        *
     65  *   AircraftClass::Overlap_List -- Returns with list of cells the aircraft overlaps.          *
     66  *   AircraftClass::Paradrop_Cargo -- Drop a passenger by parachute.                           *
     67  *   AircraftClass::Per_Cell_Process -- Handle the aircraft per cell process.                  *
     68  *   AircraftClass::Pip_Count -- Returns the number of "objects" in aircraft.                  *
     69  *   AircraftClass::Player_Assign_Mission -- Handles player input to assign a mission.         *
     70  *   AircraftClass::Pose_Dir -- Fetches the natural landing facing.                            *
     71  *   AircraftClass::Process_Fly_To -- Handles state machine for flying to destination.         *
     72  *   AircraftClass::Process_Landing -- Landing process state machine handler.                  *
     73  *   AircraftClass::Process_Take_Off -- State machine support for taking off.                  *
     74  *   AircraftClass::Read_INI -- Reads aircraft object data from an INI file.                   *
     75  *   AircraftClass::Receive_Message -- Handles receipt of radio messages.                      *
     76  *   AircraftClass::Response_Attack -- Gives audio response to attack order.                   *
     77  *   AircraftClass::Response_Move -- Gives audio response to move request.                     *
     78  *   AircraftClass::Response_Select -- Gives audio response when selected.                     *
     79  *   AircraftClass::Rotation_AI -- Handle aircraft body and flight rotation.                   *
     80  *   AircraftClass::Scatter -- Causes the aircraft to move away a bit.                         *
     81  *   AircraftClass::Set_Speed -- Sets the speed for the aircraft.                              *
     82  *   AircraftClass::Shape_Number -- Fetch the shape number to use for the aircraft.            *
     83  *   AircraftClass::Sort_Y -- Figures the sorting coordinate.                                  *
     84  *   AircraftClass::Take_Damage -- Applies damage to the aircraft.                             *
     85  *   AircraftClass::Unlimbo -- Removes an aircraft from the limbo state.                       *
     86  *   AircraftClass::What_Action -- Determines what action to perform.                          *
     87  *   AircraftClass::What_Action -- Determines what action to perform.                          *
     88  *   AircraftClass::operator delete -- Deletes the aircraft object.                            *
     89  *   AircraftClass::operator new -- Allocates a new aircraft object from the pool              *
     90  *   AircraftClass::~AircraftClass -- Destructor for aircraft object.                          *
     91  *   _Counts_As_Civ_Evac -- Is the specified object a candidate for civilian evac logic?       *
     92  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     93 
     94 #include	"function.h"
     95 
     96 
     97 /***********************************************************************************************
     98  * _Counts_As_Civ_Evac -- Is the specified object a candidate for civilian evac logic?         *
     99  *                                                                                             *
    100  *    Examines the specified object to see if it qualifies to be a civilian evacuation. This   *
    101  *    can only occur if it is a civilian (or Tanya) and the special evacuation flag has been   *
    102  *    set in the scenario control structure.                                                   *
    103  *                                                                                             *
    104  * INPUT:   candidate   -- Candidate object to examine for civilian evacuation legality.       *
    105  *                                                                                             *
    106  * OUTPUT:  bool; Is the specified object considered a civilian that must be auto-evacuated?   *
    107  *                                                                                             *
    108  * WARNINGS:   none                                                                            *
    109  *                                                                                             *
    110  * HISTORY:                                                                                    *
    111  *   06/24/1996 JLB : Created.                                                                 *
    112  *=============================================================================================*/
    113 static bool _Counts_As_Civ_Evac(ObjectClass const * candidate)
    114 {
    115 	/*
    116 	**	If the candidate pointer is missing, then return with failure code.
    117 	*/
    118 	if (candidate == NULL) return(false);
    119 
    120 	/*
    121 	**	Only infantry objects can be considered for civilian evacuation action.
    122 	*/
    123 	if (candidate->What_Am_I() != RTTI_INFANTRY) return(false);
    124 
    125 	/*
    126 	**	Working infantry object pointer.
    127 	*/
    128 	InfantryClass const * inf = (InfantryClass const *)candidate;
    129 
    130 	/*
    131 	**	Certain infantry types will always be considered a civilian evacuation candidate. These
    132 	**	include the special one-time infantry that appear in some missions.
    133 	*/
    134 	if (*inf == INFANTRY_EINSTEIN || *inf == INFANTRY_GENERAL || *inf == INFANTRY_DELPHI || *inf == INFANTRY_CHAN) return(true);
    135 
    136 	/*
    137 	**	Consider Tanya to be part of the civilian evacuation logic if the scenario is
    138 	**	specially flagged for this.
    139 	*/
    140 	if (Scen.IsTanyaEvac && *inf == INFANTRY_TANYA) return(true);
    141 
    142 	/*
    143 	**	If the infantry is not a civilian, then it isn't allowed to be a civilian evacuation.
    144 	*/
    145 	if (!inf->Class->IsCivilian) return(false);
    146 
    147 	/*
    148 	**	Technicians look like civilians, but are not considered a legal evacuation candidate.
    149 	*/
    150 	if (inf->IsTechnician) return(false);
    151 
    152 	/*
    153 	**	All tests pass, so return the success of the infantry as a civilian evacuation candidate.
    154 	*/
    155 	return(true);
    156 }
    157 
    158 
    159 /***********************************************************************************************
    160  * AircraftClass::operator new -- Allocates a new aircraft object from the pool                *
    161  *                                                                                             *
    162  *    This routine will allocate an aircraft object from the free aircraft object pool. If     *
    163  *    there are no free object available, then this routine will fail (return NULL).           *
    164  *                                                                                             *
    165  * INPUT:   none                                                                               *
    166  *                                                                                             *
    167  * OUTPUT:  Returns with a pointer to the allocate aircraft object or NULL if none were        *
    168  *          available.                                                                         *
    169  *                                                                                             *
    170  * WARNINGS:   none                                                                            *
    171  *                                                                                             *
    172  * HISTORY:                                                                                    *
    173  *   07/26/1994 JLB : Created.                                                                 *
    174  *=============================================================================================*/
    175 void * AircraftClass::operator new(size_t)
    176 {
    177 	void * ptr = Aircraft.Allocate();
    178 	if (ptr) {
    179 		((AircraftClass *)ptr)->Set_Active();
    180 	}
    181 	return(ptr);
    182 }
    183 
    184 
    185 /***********************************************************************************************
    186  * AircraftClass::operator delete -- Deletes the aircraft object.                              *
    187  *                                                                                             *
    188  *    This routine will return the aircraft object back to the free aircraft object pool.      *
    189  *                                                                                             *
    190  * INPUT:   ptr   -- Pointer to the aircraft object to delete.                                 *
    191  *                                                                                             *
    192  * OUTPUT:  none                                                                               *
    193  *                                                                                             *
    194  * WARNINGS:   none                                                                            *
    195  *                                                                                             *
    196  * HISTORY:                                                                                    *
    197  *   07/26/1994 JLB : Created.                                                                 *
    198  *=============================================================================================*/
    199 void AircraftClass::operator delete(void * ptr)
    200 {
    201 	if (ptr) {
    202 		((AircraftClass *)ptr)->IsActive = false;
    203 	}
    204 	Aircraft.Free((AircraftClass *)ptr);
    205 }
    206 
    207 
    208 /***********************************************************************************************
    209  * AircraftClass::AircraftClass -- The constructor for aircraft objects.                       *
    210  *                                                                                             *
    211  *    This routine is the constructor for aircraft objects. An aircraft object can be          *
    212  *    created and possibly placed into the game system by this routine.                        *
    213  *                                                                                             *
    214  * INPUT:   classid  -- The type of aircraft to create.                                        *
    215  *                                                                                             *
    216  *          house    -- The owner of this aircraft.                                            *
    217  *                                                                                             *
    218  * OUTPUT:  none                                                                               *
    219  *                                                                                             *
    220  * WARNINGS:   none                                                                            *
    221  *                                                                                             *
    222  * HISTORY:                                                                                    *
    223  *   07/26/1994 JLB : Created.                                                                 *
    224  *=============================================================================================*/
    225 AircraftClass::AircraftClass(AircraftType classid, HousesType house) :
    226 	FootClass(RTTI_AIRCRAFT, Aircraft.ID(this), house),
    227 	Class(AircraftTypes.Ptr((int)classid)),
    228 	SecondaryFacing(PrimaryFacing),
    229 	Passenger(false),
    230 	IsLanding(false),
    231 	IsTakingOff(false),
    232 	IsHovering(false),
    233 	Jitter(0),
    234 	SightTimer(0),
    235 	AttacksRemaining(1)
    236 {
    237 	/*
    238 	**	For two shooters, clear out the second shot flag -- it will be set the first time
    239 	**	the object fires. For non two shooters, set the flag since it will never be cleared
    240 	**	and the second shot flag tells the system that normal rearm times apply -- this is
    241 	**	what is desired for non two shooters.
    242 	*/
    243 	IsSecondShot = !Class->Is_Two_Shooter();
    244 	House->Tracking_Add(this);
    245 	Ammo = Class->MaxAmmo;
    246 	Height = FLIGHT_LEVEL;
    247 	Strength = Class->MaxStrength;
    248 	NavCom = TARGET_NONE;
    249 
    250 	/*
    251 	** Keep count of the number of units created. Dont track cargo planes as they are created
    252 	** automatically, not bought.
    253 	*/
    254 //	if (/*classid != AIRCRAFT_CARGO && */ Session.Type == GAME_INTERNET) {
    255 //		House->AircraftTotals->Increment_Unit_Total((int)classid);
    256 //	}
    257 
    258 }
    259 
    260 
    261 /***********************************************************************************************
    262  * AircraftClass::Unlimbo -- Removes an aircraft from the limbo state.                         *
    263  *                                                                                             *
    264  *    This routine is used to transition the aircraft from the limbo to the non limbo state.   *
    265  *    It occurs when the aircraft is placed on the map for whatever reason. When it is         *
    266  *    unlimboed, only then will normal game processing recognize it.                           *
    267  *                                                                                             *
    268  * INPUT:   coord -- The coordinate that the aircraft should appear at.                        *
    269  *                                                                                             *
    270  *          dir   -- The direction it should start facing.                                     *
    271  *                                                                                             *
    272  *				strength (optional) -- sets initial strength													  *
    273  *                                                                                             *
    274  *				mission (optional) -- sets initial mission													  *
    275  *                                                                                             *
    276  * OUTPUT:  bool; Was the aircraft unlimboed successfully?                                     *
    277  *                                                                                             *
    278  * WARNINGS:   none                                                                            *
    279  *                                                                                             *
    280  * HISTORY:                                                                                    *
    281  *   07/26/1994 JLB : Created.                                                                 *
    282  *=============================================================================================*/
    283 bool AircraftClass::Unlimbo(COORDINATE coord, DirType dir)
    284 {
    285 	assert(Aircraft.ID(this) == ID);
    286 	assert(IsActive);
    287 
    288 	if (FootClass::Unlimbo(coord, dir)) {
    289 
    290 		if (*this == AIRCRAFT_BADGER || (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->IsCamera)) {
    291 			IsALoaner = true;
    292 		}
    293 
    294 		/*
    295 		**	Ensure that the owning house knows about the
    296 		**	new object.
    297 		*/
    298 		House->AScan |= (1L << Class->Type);
    299 		House->ActiveAScan |= (1L << Class->Type);
    300 
    301 		/*
    302 		**	Hack it so that aircraft that are both passenger and cargo carrying
    303 		**	will carry passengers at the expense of ammo.
    304 		*/
    305 		if (Is_Something_Attached()) {
    306 			Ammo = 0;
    307 			Passenger = true;
    308 		}
    309 
    310 		/*
    311 		**	Forces the body of the helicopter to face the correct direction.
    312 		*/
    313 		SecondaryFacing = dir;
    314 
    315 		/*
    316 		**	Start rotor animation.
    317 		*/
    318 		if (!Class->IsFixedWing) {
    319 			Set_Rate(1);
    320 			Set_Stage(0);
    321 		}
    322 
    323 		/*
    324 		**	When starting at flight level, then give it speed. When landed
    325 		**	then it must be stationary.
    326 		*/
    327 		if (Height == FLIGHT_LEVEL) {
    328 			Set_Speed(0xFF);
    329 		} else {
    330 			Set_Speed(0);
    331 		}
    332 		return(true);
    333 	}
    334 	return(false);
    335 }
    336 
    337 
    338 /***********************************************************************************************
    339  * AircraftClass::Shape_Number -- Fetch the shape number to use for the aircraft.              *
    340  *                                                                                             *
    341  *    This will determine what shape number to use for the aircraft in its current state.      *
    342  *    The shape number can be used for drawing or determine shape rectangle size.              *
    343  *                                                                                             *
    344  * INPUT:   none                                                                               *
    345  *                                                                                             *
    346  * OUTPUT:  Returns with the shape number to use for the aircraft body.                        *
    347  *                                                                                             *
    348  * WARNINGS:   Some aircraft, particularly helicopters, require other shapes attached to it.   *
    349  *                                                                                             *
    350  * HISTORY:                                                                                    *
    351  *   07/26/1996 JLB : Created.                                                                 *
    352  *=============================================================================================*/
    353 int AircraftClass::Shape_Number(void) const
    354 {
    355 	int shapenum = 0;
    356 
    357 	switch (Class->Rotation) {
    358 		case 32:
    359 			shapenum = UnitClass::BodyShape[Dir_To_32(SecondaryFacing)];
    360 			break;
    361 
    362 		case 16:
    363 			shapenum = UnitClass::BodyShape[Dir_To_16(SecondaryFacing)*2]/2;
    364 			break;
    365 
    366 		case 8:
    367 			shapenum = UnitClass::BodyShape[Dir_To_8(SecondaryFacing)*4]/4;
    368 			break;
    369 
    370 		default:
    371 			break;
    372 	}
    373 
    374 	/*
    375 	**	If there is a door on this aircraft (Chinook), then adjust the
    376 	**	shape number to match the door open state.
    377 	*/
    378 	if (!Is_Door_Closed()) {
    379 		shapenum = Class->Rotation + Door_Stage();
    380 	}
    381 
    382 	return(shapenum);
    383 }
    384 
    385 
    386 /***********************************************************************************************
    387  * AircraftClass::Draw_It -- Renders an aircraft object at the location specified.             *
    388  *                                                                                             *
    389  *    This routine is used to display the aircraft object at the coordinates specified.        *
    390  *    The tactical map display uses this routine for all aircraft rendering.                   *
    391  *                                                                                             *
    392  * INPUT:   x,y      -- The coordinates to render the aircraft at.                             *
    393  *                                                                                             *
    394  *          window   -- The window that the coordinates are based upon.                        *
    395  *                                                                                             *
    396  * OUTPUT:  none                                                                               *
    397  *                                                                                             *
    398  * WARNINGS:   none                                                                            *
    399  *                                                                                             *
    400  * HISTORY:                                                                                    *
    401  *   07/26/1994 JLB : Created.                                                                 *
    402  *=============================================================================================*/
    403 void AircraftClass::Draw_It(int x, int y, WindowNumberType window) const
    404 {
    405 	assert(Aircraft.ID(this) == ID);
    406 	assert(IsActive);
    407 
    408 	/*
    409 	**	Verify the legality of the unit class.
    410 	*/
    411 	void const * shapefile = Get_Image_Data();
    412 	if (!shapefile) return;
    413 
    414 	int shapenum = Shape_Number();
    415 
    416 	/*
    417 	**	Certain aircraft use algorithmic rotation for some stages. Set the
    418 	**	rotation value accordingly. A rotation of DIR_N means no rotation at all.
    419 	*/
    420 	DirType rotation = DIR_N;
    421 	if (Class->Rotation == 16) {
    422 		rotation = DirType(Rotation16[SecondaryFacing]);
    423 	}
    424 
    425 #ifdef TOFIX
    426 	/*
    427 	**	The orca attack helicopter uses a special shape set when it is travelling
    428 	**	forward above a certain speed.
    429 	*/
    430 	if (*this == AIRCRAFT_HIND && Get_Speed() >= MPH_MEDIUM_FAST) {
    431 		shapenum += Class->Rotation;
    432 	}
    433 #endif
    434 
    435 	/*
    436 	**	Helicopters that are flying have a "bobbing" effect.
    437 	*/
    438 	int jitter = 0;
    439 	if (Height == FLIGHT_LEVEL && Get_Speed() < 3) {
    440 		static int _jitter[] = {0,0,0,0,1,1,1,0,0,0,0,0,-1,-1,-1,0};
    441 		jitter = _jitter[::Frame % 16];
    442 	}
    443 
    444 	// Virtual window needs to draw the body first so it's considered the primary object and the shadow is a sub-object
    445 	if (window == WINDOW_VIRTUAL) {
    446 		/*
    447 		**	Draw the root body of the unit.
    448 		*/
    449 		Techno_Draw_Object(shapefile, shapenum, x, y+jitter, window, rotation);
    450 
    451 		/*
    452 		**	Special manual shadow draw code.
    453 		*/
    454 		if (Visual_Character() <= VISUAL_DARKEN) {
    455 			CC_Draw_Shape(this, shapefile, shapenum, x+1, y+2, window, SHAPE_PREDATOR|SHAPE_CENTER|SHAPE_WIN_REL|SHAPE_FADING, DisplayClass::FadingShade, NULL);
    456 		}
    457 	} else {
    458 		/*
    459 		**	Special manual shadow draw code.
    460 		*/
    461 		if (Visual_Character() <= VISUAL_DARKEN) {
    462 			CC_Draw_Shape(this, shapefile, shapenum, x+1, y+2, window, SHAPE_PREDATOR|SHAPE_CENTER|SHAPE_WIN_REL|SHAPE_FADING, DisplayClass::FadingShade, NULL);
    463 		}
    464 
    465 		/*
    466 		**	Draw the root body of the unit.
    467 		*/
    468 		Techno_Draw_Object(shapefile, shapenum, x, y+jitter, window, rotation);
    469 	}
    470 
    471 	/*
    472 	**	If this aircraft is equipped with rotor blades, then draw them at this time.
    473 	*/
    474 	if (Class->IsRotorEquipped) {
    475 		Draw_Rotors(x, y+jitter, window);
    476 	}
    477 
    478 	/*
    479 	**	This draws any overlay graphics on the aircraft.
    480 	*/
    481 	FootClass::Draw_It(x, y-Lepton_To_Pixel(Height), window);
    482 }
    483 
    484 
    485 /***********************************************************************************************
    486  * AircraftClass::Draw_Rotors -- Draw rotor blades on the aircraft.                            *
    487  *                                                                                             *
    488  *    This routine will draw rotor blades on the aircraft. It is presumed that the aircraft    *
    489  *    has already been drawn at the X and Y pixel coordinates specified.                       *
    490  *                                                                                             *
    491  * INPUT:   x,y   -- The X and Y pixel coordinates to draw the rotor blades.                   *
    492  *                                                                                             *
    493  * OUTPUT:  none                                                                               *
    494  *                                                                                             *
    495  * WARNINGS:   none                                                                            *
    496  *                                                                                             *
    497  * HISTORY:                                                                                    *
    498  *   07/26/1996 JLB : Created.                                                                 *
    499  *=============================================================================================*/
    500 void AircraftClass::Draw_Rotors(int x, int y, WindowNumberType window) const
    501 {
    502 	ShapeFlags_Type flags = SHAPE_CENTER|SHAPE_WIN_REL;
    503 	int shapenum;
    504 
    505 	/*
    506 	**	The rotor shape number depends on whether the helicopter is idling
    507 	**	or not. A landed helicopter uses slow moving "idling" blades.
    508 	*/
    509 	if (Height == 0) {
    510 		shapenum = (Fetch_Stage()%8)+4;
    511 		flags = flags | SHAPE_GHOST;
    512 	} else {
    513 		shapenum = Fetch_Stage()%4;
    514 		flags = flags | SHAPE_FADING|SHAPE_PREDATOR;
    515 	}
    516 
    517 	if (*this == AIRCRAFT_TRANSPORT) {
    518 		int _stretch[FACING_COUNT] = {8, 9, 10, 9, 8, 9, 10, 9};
    519 
    520 		/*
    521 		**	Dual rotors offset along flight axis.
    522 		*/
    523 		short xx = x;
    524 		short yy = y-Lepton_To_Pixel(Height);
    525 		FacingType face = Dir_Facing(SecondaryFacing);
    526 		Move_Point(xx, yy, SecondaryFacing.Current(), _stretch[face]);
    527 		// Add 'this' parameter to call new shape draw intercept. ST - 5/22/2019
    528 		CC_Draw_Shape(this, "RROTOR", AircraftTypeClass::RRotorData, shapenum, xx, yy-2, window, flags, NULL, DisplayClass::UnitShadow);
    529 
    530 		Move_Point(xx, yy, SecondaryFacing.Current()+DIR_S, _stretch[face]*2);
    531 		// Add 'this' parameter to call new shape draw intercept. ST - 5/22/2019
    532 		CC_Draw_Shape(this, "LROTOR", AircraftTypeClass::LRotorData, shapenum, xx, yy-2, window, flags, NULL, DisplayClass::UnitShadow);
    533 
    534 	} else {
    535 
    536 		/*
    537 		**	Single rotor centered about shape.
    538 		*/
    539 		// Add 'this' parameter to call new shape draw intercept. ST - 5/22/2019
    540 		CC_Draw_Shape(this, "RROTOR", AircraftTypeClass::RRotorData, shapenum, x, ((y-Lepton_To_Pixel(Height))-2), window, flags, NULL, DisplayClass::UnitShadow);
    541 	}
    542 }
    543 
    544 
    545 /***********************************************************************************************
    546  * AircraftClass::Read_INI -- Reads aircraft object data from an INI file.                     *
    547  *                                                                                             *
    548  *    This routine is used to read the aircraft object data from the INI file buffer           *
    549  *    specified. This is used by the scenario loader code to interpret the INI file and        *
    550  *    create the specified objects therein.                                                    *
    551  *                                                                                             *
    552  * INPUT:   buffer   -- Pointer to the INI buffer.                                             *
    553  *                                                                                             *
    554  * OUTPUT:  none                                                                               *
    555  *                                                                                             *
    556  * WARNINGS:   none                                                                            *
    557  *                                                                                             *
    558  * HISTORY:                                                                                    *
    559  *   07/26/1994 JLB : Created.                                                                 *
    560  *=============================================================================================*/
    561 void AircraftClass::Read_INI(CCINIClass & ini)
    562 {
    563 	AircraftClass	* air;			// Working unit pointer.
    564 	HousesType		inhouse;			// Unit house.
    565 	AircraftType	classid;			// Unit class.
    566 	char				buf[128];
    567 
    568 	int counter = ini.Entry_Count(INI_Name());
    569 	for (int index = 0; index < counter; index++) {
    570 		char const * entry = ini.Get_Entry(INI_Name(), index);
    571 
    572 		ini.Get_String(INI_Name(), entry, NULL, buf, sizeof(buf)-1);
    573 		inhouse = HouseTypeClass::From_Name(strtok(buf, ","));
    574 		if (inhouse != HOUSE_NONE) {
    575 			classid = AircraftTypeClass::From_Name(strtok(NULL, ","));
    576 
    577 			if (classid != AIRCRAFT_NONE) {
    578 
    579 				if (HouseClass::As_Pointer(inhouse) != NULL) {
    580 					air = new AircraftClass(classid, inhouse);
    581 					if (air) {
    582 						COORDINATE	coord;
    583 						int	strength;
    584 						DirType dir;
    585 
    586 						/*
    587 						**	Read the raw data.
    588 						*/
    589 						char * token = strtok(NULL, ",");
    590 						if (token) {
    591 							strength = atoi(token);
    592 						} else {
    593 							strength = 0;
    594 						}
    595 
    596 						token = strtok(NULL, ",");
    597 						if (token) {
    598 							coord = Cell_Coord((CELL)atoi(token));
    599 						} else {
    600 							coord = 0xFFFFFFFFL;
    601 						}
    602 
    603 						token = strtok(NULL, ",");
    604 						if (token) {
    605 							dir = (DirType)atoi(token);
    606 						} else {
    607 							dir = DIR_N;
    608 						}
    609 
    610 						if (!Map.In_Radar(Coord_Cell(coord))) {
    611 							delete air;
    612 						} else {
    613 
    614 							air->Strength = (int)air->Class->MaxStrength * fixed(strength, 256);		// Cast this to (int). ST - 5/8/19
    615 							if (air->Unlimbo(coord, dir)) {
    616 								air->Assign_Mission(AircraftClass::Mission_From_Name(strtok(NULL, ",\n\r")));
    617 							} else {
    618 								delete air;
    619 							}
    620 						}
    621 					}
    622 				}
    623 			}
    624 		}
    625 	}
    626 }
    627 
    628 
    629 /***********************************************************************************************
    630  * AircraftClass::Mission_Hunt -- Maintains hunt AI for the aircraft.                          *
    631  *                                                                                             *
    632  *    Hunt AI consists of finding a target and attacking it. If there is no target assigned    *
    633  *    and this unit doesn't automatically hunt for more targets, then it will change           *
    634  *    mission to a more passive (land and await further orders) type.                          *
    635  *                                                                                             *
    636  * INPUT:   none                                                                               *
    637  *                                                                                             *
    638  * OUTPUT:  Returns with the number of ticks before calling this routine again.                *
    639  *                                                                                             *
    640  * WARNINGS:   none                                                                            *
    641  *                                                                                             *
    642  * HISTORY:                                                                                    *
    643  *   07/26/1994 JLB : Created.                                                                 *
    644  *=============================================================================================*/
    645 int AircraftClass::Mission_Hunt(void)
    646 {
    647 	assert(Aircraft.ID(this) == ID);
    648 	assert(IsActive);
    649 
    650 	if (Class->IsFixedWing) {
    651 
    652 		if (TarCom != NavCom) {
    653 			Assign_Destination(TarCom);
    654 		}
    655 
    656 		enum {
    657 			LOOK_FOR_TARGET,
    658 			TAKE_OFF,
    659 			FLY_TO_TARGET,
    660 			DROP_BOMBS,
    661 			REGROUP
    662 		};
    663 		switch (Status) {
    664 
    665 			/*
    666 			**	Acquiring target stage.
    667 			*/
    668 			case LOOK_FOR_TARGET:
    669 				if (Target_Legal(TarCom)) {
    670 					Status = TAKE_OFF;
    671 					return(1);
    672 				} else {
    673 					if (!Team.Is_Valid()) {
    674 						if (Session.Type != GAME_NORMAL) {
    675 							Assign_Target(Greatest_Threat(THREAT_TIBERIUM));
    676 						}
    677 						if (!Target_Legal(TarCom)) {
    678 							Assign_Target(Greatest_Threat(THREAT_NORMAL));
    679 						}
    680 
    681 						/*
    682 						**	If there is no target, then this aircraft should just do its normal thing.
    683 						*/
    684 						if (!Target_Legal(TarCom) && !Team.Is_Valid()) {
    685 							Enter_Idle_Mode();
    686 						}
    687 					}
    688 				}
    689 				break;
    690 
    691 			/*
    692 			**	Make the aircraft take off from the airstrip.
    693 			*/
    694 			case TAKE_OFF:
    695 				/*
    696 				**	If the aircraft is high enough to begin its mission, then do so.
    697 				*/
    698 				if (Process_Take_Off())  {
    699 					IsTakingOff = false;
    700 					Set_Speed(0xFF);
    701 
    702 					/*
    703 					**	After takeoff is complete, break radio contact.
    704 					*/
    705 					if (In_Radio_Contact()/*KO && Map[Coord].Cell_Building() == Contact_With_Whom()*/) {
    706 						Transmit_Message(RADIO_OVER_OUT);
    707 					}
    708 
    709 					Status = FLY_TO_TARGET;
    710 				}
    711 				return(1);
    712 
    713 			/*
    714 			**	Homing in on target stage.
    715 			*/
    716 			case FLY_TO_TARGET:
    717 				switch (Can_Fire(TarCom, 0)) {
    718 					case FIRE_FACING:
    719 						/*
    720 						**	Catch the case where it is tightly circling the target. In that
    721 						**	case, increase the delay so that it has a chance to fly away and
    722 						**	break the circle cycle.
    723 						*/
    724 						if (In_Range(TarCom, 0) || Passenger) {
    725 							return(TICKS_PER_SECOND * 2);
    726 						}
    727 						if (!PrimaryFacing.Is_Rotating() && Target_Legal(TarCom)) {
    728 							PrimaryFacing.Set_Desired(Direction(TarCom));
    729 						}
    730 						break;
    731 
    732 					case FIRE_AMMO:
    733 						Status = REGROUP;
    734 						break;
    735 
    736 					case FIRE_CANT:
    737 					case FIRE_ILLEGAL:
    738 						if (Mission == MISSION_ATTACK) {
    739 							Status = REGROUP;
    740 						} else {
    741 							Status = LOOK_FOR_TARGET;
    742 						}
    743 						break;
    744 
    745 					case FIRE_OK:
    746 						Status = DROP_BOMBS;
    747 						return(1);
    748 
    749 					default:
    750 						if (!PrimaryFacing.Is_Rotating() && Target_Legal(TarCom)) {
    751 							PrimaryFacing.Set_Desired(Direction(TarCom));
    752 						}
    753 						break;
    754 				}
    755 				return(TICKS_PER_SECOND/2);
    756 
    757 			/*
    758 			**	Dropping a stream of bombs phase.
    759 			*/
    760 			case DROP_BOMBS:
    761 				TARGET targ;
    762 				switch (Can_Fire(TarCom, 0)) {
    763 					case FIRE_OK:
    764 						targ = ::As_Target(Coord_Move(Center_Coord(), SecondaryFacing, Weapon_Range(0)-0x0200));
    765 						if (Class->PrimaryWeapon != NULL) {
    766 							if (Class->PrimaryWeapon->IsCamera) {
    767 								Status = REGROUP;
    768 							} else {
    769 								Map[::As_Cell(TarCom)].Incoming(Coord, true);
    770 							}
    771 
    772 							/*
    773 							**	Force the target to be the actual target if this aircraft is
    774 							**	equipped with homing projectile.
    775 							*/
    776 							if (Class->PrimaryWeapon->Bullet != NULL && Class->PrimaryWeapon->Bullet->ROT > 0) {
    777 								targ = TarCom;
    778 							}
    779 						}
    780 						Fire_At(targ, 0);
    781 						if (Class->Is_Two_Shooter()) {
    782 							Fire_At(targ, 0);
    783 						}
    784 						return(Arm);
    785 
    786 					case FIRE_RANGE:
    787 					case FIRE_FACING:
    788 						Status = FLY_TO_TARGET;
    789 						return(TICKS_PER_SECOND*4);
    790 
    791 					case FIRE_ILLEGAL:
    792 						if (Mission == MISSION_ATTACK) {
    793 							Status = REGROUP;
    794 						} else {
    795 							Status = LOOK_FOR_TARGET;
    796 						}
    797 						break;
    798 
    799 					case FIRE_CANT:
    800 						Status = REGROUP;
    801 						break;
    802 
    803 					case FIRE_AMMO:
    804 						AttacksRemaining--;
    805 						Status = REGROUP;
    806 						break;
    807 
    808 					default:
    809 						break;
    810 				}
    811 				return(1);
    812 
    813 			/*
    814 			**	Pull away to regroup for possibly another attack or a retreat.
    815 			*/
    816 			case REGROUP:
    817 				if (Ammo == 0) {
    818 					AttacksRemaining = 0;
    819 					if (Team.Is_Valid()) Team->Remove(this);
    820 					Enter_Idle_Mode();
    821 				}
    822 
    823 				if (Mission == MISSION_ATTACK || (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->IsCamera) || (!AttacksRemaining && !Is_Something_Attached())) {
    824 
    825 					if (IsALoaner)  {
    826 						if (Team) Team->Remove(this);
    827 						Assign_Mission(MISSION_RETREAT);
    828 						Commence();
    829 					} else {
    830 						if (!Team.Is_Valid()) Enter_Idle_Mode();
    831 					}
    832 					Commence();
    833 				} else {
    834 					Status = LOOK_FOR_TARGET;
    835 				}
    836 				break;
    837 
    838 			default:
    839 				break;
    840 		}
    841 	} else {
    842 		if (!Ammo) {
    843 			if (Team) Team->Remove(this);
    844 			Enter_Idle_Mode();
    845 		} else {
    846 			if (!Target_Legal(TarCom)) {
    847 				if (Session.Type != GAME_NORMAL) {
    848 					Assign_Target(Greatest_Threat(THREAT_TIBERIUM));
    849 				}
    850 				if (!Target_Legal(TarCom)) {
    851 					Assign_Target(Greatest_Threat(THREAT_NORMAL));
    852 				}
    853 				if (!Target_Legal(TarCom)) {
    854 					Enter_Idle_Mode();
    855 					return(1);
    856 				}
    857 			}
    858 
    859 			Assign_Mission(MISSION_ATTACK);
    860 			return(1);
    861 		}
    862 	}
    863 	return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
    864 }
    865 
    866 
    867 /***********************************************************************************************
    868  * AircraftClass::AI -- Processes the normal non-graphic AI for the aircraft.                  *
    869  *                                                                                             *
    870  *    This handles the non-graphic AI processing for the aircraft. This usually entails        *
    871  *    maintenance and other AI functions.                                                      *
    872  *                                                                                             *
    873  * INPUT:   none                                                                               *
    874  *                                                                                             *
    875  * OUTPUT:  none                                                                               *
    876  *                                                                                             *
    877  * WARNINGS:   none                                                                            *
    878  *                                                                                             *
    879  * HISTORY:                                                                                    *
    880  *   07/26/1994 JLB : Created.                                                                 *
    881  *=============================================================================================*/
    882 void AircraftClass::AI(void)
    883 {
    884 	assert(Aircraft.ID(this) == ID);
    885 	assert(IsActive);
    886 
    887 	/*
    888 	**	A Mission change can always occur if the aircraft is landed or flying.
    889 	*/
    890 	if (!IsLanding && !IsTakingOff) {
    891 		Commence();
    892 	}
    893 
    894 	/*
    895 	**	Perform any base class AI processing. If during this process, the aircraft was
    896 	**	destroyed, then detect this and bail from this AI routine early.
    897 	*/
    898 	FootClass::AI();
    899 	if (!IsActive) {
    900 		return;
    901 	}
    902 
    903 	/*
    904 	**	A Mission change can always occur if the aircraft is landed or flying.
    905 	*/
    906 	if (!IsLanding && !IsTakingOff) {
    907 		Commence();
    908 	}
    909 
    910 	/*
    911 	**	Handle any body rotation at this time. Body rotation can occur even if the
    912 	**	flying object is not actually moving.
    913 	*/
    914 	Rotation_AI();
    915 
    916 	/*
    917 	**	Handle any aircraft movement at this time.
    918 	*/
    919 	Movement_AI();
    920 
    921 	/*
    922 	**	Any aircraft that is not in the ground layer must be redrawn. This is a
    923 	**	performance hit, but there is no other choice. The cells under an aircraft
    924 	**	do not know if there is an aircraft above it. Thus, it cannot flag the
    925 	**	aircraft to redraw. As a consequence, all aircraft must redraw.
    926 	*/
    927 	if (In_Which_Layer() != LAYER_GROUND) {
    928 		Mark();
    929 	}
    930 
    931 	/*
    932 	**	Perform sighting every so often as controlled by the sight timer.
    933 	*/
    934 	//if (IsOwnedByPlayer && Class->SightRange && SightTimer == 0) {  // Changed to facilitate client/server multiplayer. ST - 8/2/2019 2:29PM	
    935 	if (House->IsHuman && Class->SightRange && SightTimer == 0) {
    936 		Look();
    937 		SightTimer = TICKS_PER_SECOND;
    938 	}
    939 
    940 	/*
    941 	**	Handle landing and taking off logic. Helicopters are prime users of this technique. The
    942 	**	aircraft will either gain or lose altitude as appropriate. As the aircraft transitions
    943 	**	between flying level and ground level, it will be moved into the appropriate render
    944 	**	layer.
    945 	*/
    946 	if (Landing_Takeoff_AI()) {
    947 		return;
    948 	}
    949 
    950 	/*
    951 	**	Always flag the map draw process to occur if there is an aircraft in the view.
    952 	**	This ensures that it will be rendered even if there is nothing else that flagged
    953 	**	the map to be redrawn.
    954 	*/
    955 	if (Map.In_View(Coord_Cell(Coord))) {
    956 		Map.Flag_To_Redraw(false);
    957 		Map.DisplayClass::IsToRedraw = true;
    958 	}
    959 
    960 	/*
    961 	**	When aircraft leave the edge of the map, they might get destroyed. This occurs if the
    962 	**	aircraft is a non-player produced unit and it has completed its mission. A transport
    963 	**	helicopter that has already delivered reinforcements is a good example of this.
    964 	*/
    965 	if (Edge_Of_World_AI()) {
    966 		return;
    967 	}
    968 }
    969 
    970 
    971 /***********************************************************************************************
    972  * AircraftClass::Overlap_List -- Returns with list of cells the aircraft overlaps.            *
    973  *                                                                                             *
    974  *    When aircraft are flying, they can overlap quite a number of cells. These cells can      *
    975  *    be determined from the coordinate where the aircraft is centered and the size of the     *
    976  *    aircraft's shape. Landed aircraft are a special case and are usually much smaller        *
    977  *    than when flying.                                                                        *
    978  *                                                                                             *
    979  * INPUT:   none                                                                               *
    980  *                                                                                             *
    981  * OUTPUT:  Returns with a pointer to a cell offset list that specifies all cells that         *
    982  *          the aircraft overlaps given the aircraft's current state.                          *
    983  *                                                                                             *
    984  * WARNINGS:   none                                                                            *
    985  *                                                                                             *
    986  * HISTORY:                                                                                    *
    987  *   07/26/1994 JLB : Created.                                                                 *
    988  *=============================================================================================*/
    989 short const * AircraftClass::Overlap_List(bool redraw) const
    990 {
    991 	assert(Aircraft.ID(this) == ID);
    992 	assert(IsActive);
    993 
    994 	static short const _list[] = {
    995 		-(MAP_CELL_W-1), -MAP_CELL_W, -(MAP_CELL_W+1),
    996 		-1, 0, 1,
    997 		(MAP_CELL_W-1), MAP_CELL_W, (MAP_CELL_W+1),
    998 		-((MAP_CELL_W*2)-1), -(MAP_CELL_W*2), -((MAP_CELL_W*2)+1),
    999 		-((MAP_CELL_W*3)-1), -(MAP_CELL_W*3), -((MAP_CELL_W*3)+1),
   1000 		REFRESH_EOL
   1001 	};
   1002 
   1003 	static short const _listbadger[] = {
   1004 		-(MAP_CELL_W-2), -(MAP_CELL_W-1), -MAP_CELL_W, -(MAP_CELL_W+1), -(MAP_CELL_W+2),
   1005 		-2, -1, 0, 1, 2,
   1006 		(MAP_CELL_W-2), (MAP_CELL_W-1), MAP_CELL_W, (MAP_CELL_W+1), (MAP_CELL_W+2),
   1007 		-((MAP_CELL_W*2)-2), -((MAP_CELL_W*2)-1), -(MAP_CELL_W*2), -((MAP_CELL_W*2)+1), -((MAP_CELL_W*2)+2),
   1008 		-((MAP_CELL_W*3)-2), -((MAP_CELL_W*3)-1), -(MAP_CELL_W*3), -((MAP_CELL_W*3)+1), -((MAP_CELL_W*3)+2),
   1009 		REFRESH_EOL
   1010 	};
   1011 
   1012 	if (redraw || Height != 0) {
   1013 #ifdef PARTIAL
   1014 		Rect rect;
   1015 		if (!IsSelected && Class->DimensionData != NULL && Class->IsFixedWing) {
   1016 			int shapenum = min(Shape_Number(), Get_Build_Frame_Count(Class->Get_Image_Data())-1);
   1017 			if (!Class->DimensionData[shapenum].Is_Valid()) {
   1018 				Class->DimensionData[shapenum] = Shape_Dimensions(Class->Get_Image_Data(), shapenum);
   1019 			}
   1020 			rect = Class->DimensionData[shapenum];
   1021 
   1022 			/*
   1023 			**	Increase the rectangle for the aircraft since the aircraft could
   1024 			**	have its shape algorithmically rotated.
   1025 			*/
   1026 			rect.X -= 5;
   1027 			rect.Y -= 5;
   1028 			rect.Width += 10;
   1029 			rect.Height += 10;
   1030 
   1031 			Rect hrect = rect;
   1032 
   1033 			hrect.Y -= Lepton_To_Pixel(Height);
   1034 
   1035 			return(Coord_Spillage_List(Coord, Union(rect, hrect), true));
   1036 		}
   1037 #endif
   1038 
   1039 		if (*this == AIRCRAFT_BADGER) {
   1040 			return(_listbadger);
   1041 		} else {
   1042 			return(_list);
   1043 		}
   1044 	}
   1045 	return(Class->Overlap_List());
   1046 }
   1047 
   1048 
   1049 /***********************************************************************************************
   1050  * AircraftClass::Init -- Initialize the aircraft system to an empty state.                    *
   1051  *                                                                                             *
   1052  *    This routine is used to clear out the aircraft allocation system. It is called in        *
   1053  *    preparation for a scenario load or save game load.                                       *
   1054  *                                                                                             *
   1055  * INPUT:   none                                                                               *
   1056  *                                                                                             *
   1057  * OUTPUT:  none                                                                               *
   1058  *                                                                                             *
   1059  * WARNINGS:   none                                                                            *
   1060  *                                                                                             *
   1061  * HISTORY:                                                                                    *
   1062  *   09/24/1994 JLB : Created.                                                                 *
   1063  *=============================================================================================*/
   1064 void AircraftClass::Init(void)
   1065 {
   1066 	Aircraft.Free_All();
   1067 }
   1068 
   1069 
   1070 /***********************************************************************************************
   1071  * AircraftClass::Mission_Unload -- Handles unloading cargo.                                   *
   1072  *                                                                                             *
   1073  *    This function is used to handle finding, heading toward, landing, and unloading the      *
   1074  *    cargo from the aircraft. Once unloading of cargo has occurred, then the aircraft follows *
   1075  *    a different mission.                                                                     *
   1076  *                                                                                             *
   1077  * INPUT:   none                                                                               *
   1078  *                                                                                             *
   1079  * OUTPUT:  Returns the number of game ticks to delay before calling this function again.      *
   1080  *                                                                                             *
   1081  * WARNINGS:   none                                                                            *
   1082  *                                                                                             *
   1083  * HISTORY:                                                                                    *
   1084  *   10/31/94   JLB : Created.                                                                 *
   1085  *=============================================================================================*/
   1086 int AircraftClass::Mission_Unload(void)
   1087 {
   1088 	assert(Aircraft.ID(this) == ID);
   1089 	assert(IsActive);
   1090 
   1091 	if (Class->IsFixedWing) {
   1092 
   1093 		Assign_Target(NavCom);
   1094 		return(Mission_Hunt());
   1095 
   1096 	} else {
   1097 		enum {
   1098 			SEARCH_FOR_LZ,
   1099 			FLY_TO_LZ,
   1100 			LAND_ON_LZ,
   1101 			UNLOAD_PASSENGERS,
   1102 			TAKE_OFF
   1103 		};
   1104 
   1105 		switch (Status) {
   1106 
   1107 			/*
   1108 			**	Search for an appropriate destination spot if one isn't already assigned.
   1109 			*/
   1110 			case SEARCH_FOR_LZ:
   1111 				if (Height == 0 && (Target_Legal(NavCom) || Coord == As_Coord(NavCom))) {
   1112 					Status = UNLOAD_PASSENGERS;
   1113 				} else {
   1114 					if (!Is_LZ_Clear(NavCom)) {
   1115 
   1116 						FootClass * foot = Attached_Object();
   1117 						if (foot != NULL && foot->Team && foot->Team->Class->Origin != -1) {
   1118 							Assign_Destination(::As_Target(Scen.Waypoint[foot->Team->Class->Origin]));
   1119 						} else {
   1120 							Assign_Destination(New_LZ(::As_Target(Scen.Waypoint[WAYPT_REINF])));
   1121 							if (Team.Is_Valid()) {
   1122 								Team->Assign_Mission_Target(NavCom);
   1123 							}
   1124 						}
   1125 					} else {
   1126 						if (Height == FLIGHT_LEVEL) {
   1127 							Status = FLY_TO_LZ;
   1128 						} else {
   1129 							Status = TAKE_OFF;
   1130 						}
   1131 					}
   1132 				}
   1133 				break;
   1134 
   1135 			/*
   1136 			**	Fly to destination.
   1137 			*/
   1138 			case FLY_TO_LZ:
   1139 				if (Is_LZ_Clear(NavCom)) {
   1140 					int distance = Process_Fly_To(true, NavCom);
   1141 
   1142 					if (distance < 0x0100) {
   1143 						SecondaryFacing.Set_Desired(Pose_Dir());
   1144 
   1145 						if (distance < 0x0010) {
   1146 							Status = LAND_ON_LZ;
   1147 						}
   1148 						return(1);
   1149 					} else {
   1150 						SecondaryFacing.Set_Desired(PrimaryFacing.Desired());
   1151 						return(5);
   1152 					}
   1153 				} else {
   1154 					Status = SEARCH_FOR_LZ;
   1155 				}
   1156 				break;
   1157 
   1158 			/*
   1159 			**	Landing phase. Just delay until landing is complete. At that time,
   1160 			**	transition to the unloading phase.
   1161 			*/
   1162 			case LAND_ON_LZ:
   1163 				if (IsTakingOff) {
   1164 					Status = TAKE_OFF;
   1165 				} else {
   1166 					if (Process_Landing()) {
   1167 						Status = UNLOAD_PASSENGERS;
   1168 					}
   1169 				}
   1170 				return(1);
   1171 
   1172 			/*
   1173 			**	Hold while unloading passengers. When passengers are unloaded the order for this
   1174 			**	transport gets changed to MISSION_RETREAT.
   1175 			*/
   1176 			case UNLOAD_PASSENGERS:
   1177 				if (!IsTethered) {
   1178 					if (Is_Something_Attached()) {
   1179 						FootClass * unit = (FootClass *)Detach_Object();
   1180 
   1181 						/*
   1182 						**	First thing is to lift the transport off of the map so that the unlimbo
   1183 						**	process for the passengers is more likely to succeed.
   1184 						*/
   1185 						Map.Pick_Up(Coord_Cell(Coord), this);
   1186 
   1187 						if (!Exit_Object(unit)) {
   1188 							delete unit;
   1189 						}
   1190 
   1191 						/*
   1192 						**	Restore the transport back down on the map.
   1193 						*/
   1194 						Map.Place_Down(Coord_Cell(Coord), this);
   1195 
   1196 						if (!Is_Something_Attached()) {
   1197 							Enter_Idle_Mode();
   1198 						}
   1199 
   1200 					} else  {
   1201 
   1202 						Enter_Idle_Mode();
   1203 					}
   1204 				}
   1205 				break;
   1206 
   1207 			/*
   1208 			**	Aircraft is now taking off. Once the aircraft reaches flying altitude then it
   1209 			**	will either take off or look for another landing spot to try again.
   1210 			*/
   1211 			case TAKE_OFF:  {
   1212 				if (Process_Take_Off()) {
   1213 					if (Is_Something_Attached()) {
   1214 						Status = SEARCH_FOR_LZ;
   1215 
   1216 						/*
   1217 						**	Break off radio contact with the helipad it is taking off from.
   1218 						*/
   1219 						if (In_Radio_Contact() && Map[Coord].Cell_Building() == Contact_With_Whom()) {
   1220 							Transmit_Message(RADIO_OVER_OUT);
   1221 						}
   1222 					} else  {
   1223 						Enter_Idle_Mode();
   1224 					}
   1225 				}
   1226 				return(1);
   1227 			}
   1228 
   1229 			default:
   1230 				break;
   1231 		}
   1232 	}
   1233 	return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
   1234 }
   1235 
   1236 
   1237 /***********************************************************************************************
   1238  * AircraftClass::Is_LZ_Clear -- Determines if landing zone is free for landing.               *
   1239  *                                                                                             *
   1240  *    This routine examines the landing zone (as specified by the target parameter) in order   *
   1241  *    to determine if it is free to be landed upon. Call this routine when it is necessary     *
   1242  *    to double check this. Typically this occurs right before a helicopter lands and also     *
   1243  *    when determining the landing zone in the first place.                                    *
   1244  *                                                                                             *
   1245  * INPUT:   target   -- The target that is the "landing zone".                                 *
   1246  *                                                                                             *
   1247  * OUTPUT:  bool; Is the landing zone clear for landing?                                       *
   1248  *                                                                                             *
   1249  * WARNINGS:   none                                                                            *
   1250  *                                                                                             *
   1251  * HISTORY:                                                                                    *
   1252  *   10/31/94   JLB : Created.                                                                 *
   1253  *=============================================================================================*/
   1254 bool AircraftClass::Is_LZ_Clear(TARGET target) const
   1255 {
   1256 	assert(Aircraft.ID(this) == ID);
   1257 	assert(IsActive);
   1258 
   1259 	if (!Target_Legal(target)) return(false);
   1260 	CELL cell = ::As_Cell(target);
   1261 	if (!Map.In_Radar(cell)) return(false);
   1262 
   1263 	/*
   1264 	**	If the requested landing location is occupied, then only consider that location
   1265 	**	legal if the occupying object is in radio contact with the aircraft. This presumes that
   1266 	**	the two objects know what they are doing.
   1267 	*/
   1268 	ObjectClass * object = Map[cell].Cell_Object();
   1269 	if (object) {
   1270 		if (object == this) return(true);
   1271 
   1272 		if (In_Radio_Contact() && Contact_With_Whom() == object) {
   1273 			return(true);
   1274 		}
   1275 		return(false);
   1276 	}
   1277 
   1278 	if (!Map[cell].Is_Clear_To_Move(SPEED_TRACK, false, false)) return(false);
   1279 
   1280 	return(true);
   1281 }
   1282 
   1283 
   1284 /***********************************************************************************************
   1285  * AircraftClass::Sort_Y -- Figures the sorting coordinate.                                    *
   1286  *                                                                                             *
   1287  *    This routine is used to determine the coordinate to use for sorting the aircraft. This   *
   1288  *    sorting value is used when the aircraft is on the ground. At that time the aircraft      *
   1289  *    must be rendered in proper relationship to the other ground objects.                     *
   1290  *                                                                                             *
   1291  * INPUT:   none                                                                               *
   1292  *                                                                                             *
   1293  * OUTPUT:  Returns with the coordinate to use when sorting the aircraft with other ground     *
   1294  *          objects.                                                                           *
   1295  *                                                                                             *
   1296  * WARNINGS:   none                                                                            *
   1297  *                                                                                             *
   1298  * HISTORY:                                                                                    *
   1299  *   11/02/1994 JLB : Created.                                                                 *
   1300  *=============================================================================================*/
   1301 COORDINATE AircraftClass::Sort_Y(void) const
   1302 {
   1303 	assert(Aircraft.ID(this) == ID);
   1304 	assert(IsActive);
   1305 
   1306 	return(Coord_Add(Coord, 0x00800000L));
   1307 }
   1308 
   1309 
   1310 /***********************************************************************************************
   1311  * AircraftClass::Mission_Retreat -- Handles the aircraft logic for leaving the battlefield.   *
   1312  *                                                                                             *
   1313  *    This mission will be followed when the aircraft decides that it is time to leave the     *
   1314  *    battle. Typically, this occurs when a loaner transport has dropped off its load or when  *
   1315  *    an attack air vehicle has expended its ordinance.                                        *
   1316  *                                                                                             *
   1317  * INPUT:   none                                                                               *
   1318  *                                                                                             *
   1319  * OUTPUT:  Returns with the number of game ticks to delay before calling this routine again.  *
   1320  *                                                                                             *
   1321  * WARNINGS:   none                                                                            *
   1322  *                                                                                             *
   1323  * HISTORY:                                                                                    *
   1324  *   03/19/1995 JLB : Created.                                                                 *
   1325  *   08/13/1995 JLB : Handles aircraft altitude gain after takeoff logic.                      *
   1326  *=============================================================================================*/
   1327 int AircraftClass::Mission_Retreat(void)
   1328 {
   1329 	assert(Aircraft.ID(this) == ID);
   1330 	assert(IsActive);
   1331 
   1332 	if (Class->IsFixedWing) {
   1333 		if (Class->IsFixedWing && Height < FLIGHT_LEVEL) {
   1334 			Height += 1;
   1335 			return(3);
   1336 		}
   1337 		return(TICKS_PER_SECOND*10);
   1338 	}
   1339 
   1340 	enum {
   1341 		TAKE_OFF,
   1342 		FACE_MAP_EDGE,
   1343 		KEEP_FLYING
   1344 	};
   1345 	switch (Status) {
   1346 
   1347 		/*
   1348 		**	Take off if landed.
   1349 		*/
   1350 		case TAKE_OFF:
   1351 			if (Process_Take_Off()) {
   1352 				Status = FACE_MAP_EDGE;
   1353 			}
   1354 			return(1);
   1355 
   1356 		/*
   1357 		**	Set facing and speed toward the friendly map edge.
   1358 		*/
   1359 		case FACE_MAP_EDGE:
   1360 			Set_Speed(0xFF);
   1361 
   1362 			/*
   1363 			**	Take advantage of the fact that the source map edge enumerations happen to
   1364 			**	occur in a clockwise order and are the first four enumerations of the map
   1365 			**	edge default for the house. If this value is masked and then shifted, a
   1366 			**	normalized direction value results. Use this value to head the aircraft
   1367 			**	toward the "friendly" map edge.
   1368 			*/
   1369 			PrimaryFacing.Set_Desired((DirType)((House->Control.Edge & 0x03) << 6));
   1370 			SecondaryFacing.Set_Desired(PrimaryFacing.Desired());
   1371 			Status = KEEP_FLYING;
   1372 			break;
   1373 
   1374 		/*
   1375 		**	Just do nothing since we are headed toward the map edge. When the edge is
   1376 		**	reached, the aircraft should be automatically eliminated.
   1377 		*/
   1378 		case KEEP_FLYING:
   1379 			break;
   1380 
   1381 		default:
   1382 			break;
   1383 	}
   1384 	return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
   1385 }
   1386 
   1387 
   1388 /***********************************************************************************************
   1389  * AircraftClass::Exit_Object -- Unloads passenger from aircraft.                              *
   1390  *                                                                                             *
   1391  *    This routine is called when the aircraft is to unload a passenger. The passenger must    *
   1392  *    be able to move under its own power. Typical situation is when a transport helicopter    *
   1393  *    is to unload an infantry unit.                                                           *
   1394  *                                                                                             *
   1395  * INPUT:   unit  -- Pointer to the unit that is to be unloaded from this aircraft.            *
   1396  *                                                                                             *
   1397  * OUTPUT:  bool; Was the unit unloaded successfully?                                          *
   1398  *                                                                                             *
   1399  * WARNINGS:   The unload process is merely started by this routine. Radio contact is          *
   1400  *             established with the unloading unit and when the unit is clear of the aircraft  *
   1401  *             the radio contact will be broken and then the aircraft is free to pursue        *
   1402  *             other.                                                                          *
   1403  *                                                                                             *
   1404  * HISTORY:                                                                                    *
   1405  *   01/10/1995 JLB : Created.                                                                 *
   1406  *=============================================================================================*/
   1407 int AircraftClass::Exit_Object(TechnoClass * unit)
   1408 {
   1409 	assert(Aircraft.ID(this) == ID);
   1410 	assert(IsActive);
   1411 
   1412 	static FacingType _toface[FACING_COUNT] = {FACING_S, FACING_SW, FACING_SE, FACING_NW, FACING_NE, FACING_N, FACING_W, FACING_E};
   1413 	CELL	cell;
   1414 
   1415 	/*
   1416 	**	Find a free cell to drop the unit off at.
   1417 	*/
   1418 	FacingType face;
   1419 	for (face = FACING_N; face < FACING_COUNT; face++) {
   1420 		cell = Adjacent_Cell(Coord_Cell(Coord), _toface[face]);
   1421 		if (unit->Can_Enter_Cell(cell) == MOVE_OK) break;
   1422 	}
   1423 
   1424 	// Should perform a check here to see if no cell could be found.
   1425 
   1426 	/*
   1427 	**	If the passenger can be placed on the map, then start it moving toward the
   1428 	**	destination cell and establish radio contact with the transport. This is used
   1429 	**	to make sure that the transport waits until the passenger is clear before
   1430 	**	unloading the next passenger or taking off.
   1431 	*/
   1432 	if (unit->Unlimbo(Coord, Facing_Dir(_toface[face]))) {
   1433 		unit->Assign_Mission(MISSION_MOVE);
   1434 		unit->Assign_Destination(::As_Target(cell));
   1435 		if (Transmit_Message(RADIO_HELLO, unit) == RADIO_ROGER) {
   1436 			Transmit_Message(RADIO_UNLOAD);
   1437 		}
   1438 		unit->Look(false);
   1439 		return(true);
   1440 	}
   1441 	return(false);
   1442 }
   1443 
   1444 
   1445 /***********************************************************************************************
   1446  * AircraftClass::Paradrop_Cargo -- Drop a passenger by parachute.                             *
   1447  *                                                                                             *
   1448  *    Call this routine when a passenger needs to be dropped off by parachute. One passenger   *
   1449  *    is offloaded by a call to this routine.                                                  *
   1450  *                                                                                             *
   1451  * INPUT:   none                                                                               *
   1452  *                                                                                             *
   1453  * OUTPUT:  Returns with the delay time that it is safe to wait before processing any further  *
   1454  *          paradrop actions.                                                                  *
   1455  *                                                                                             *
   1456  * WARNINGS:   none                                                                            *
   1457  *                                                                                             *
   1458  * HISTORY:                                                                                    *
   1459  *   07/26/1996 JLB : Created.                                                                 *
   1460  *=============================================================================================*/
   1461 int AircraftClass::Paradrop_Cargo(void)
   1462 {
   1463 	FootClass * passenger = Detach_Object();
   1464 	if (passenger) {
   1465 		if (!passenger->Paradrop(Center_Coord())) {
   1466 			Attach(passenger);
   1467 		} else {
   1468 
   1469 			/*
   1470 			**	Play a sound effect of the parachute opening.
   1471 			*/
   1472 			Sound_Effect(VOC_CHUTE1, Coord);
   1473 
   1474 			if (Team.Is_Valid()) {
   1475 				Team->Remove(passenger);
   1476 				if (passenger->House->IsHuman) {
   1477 					Assign_Mission(MISSION_GUARD);
   1478 				} else {
   1479 					Assign_Mission(MISSION_HUNT);
   1480 				}
   1481 			}
   1482 //			Arm = Rearm_Delay(IsSecondShot);
   1483 			Arm = 0;
   1484 		}
   1485 	}
   1486 	return(Arm);
   1487 }
   1488 
   1489 
   1490 /***********************************************************************************************
   1491  * AircraftClass::Fire_At -- Handles firing a projectile from an aircraft.                     *
   1492  *                                                                                             *
   1493  *    Sometimes, aircraft firing needs special handling. Example: for napalm bombs, the        *
   1494  *    bomb travels forward at nearly the speed of the delivery aircraft, not necessarily the   *
   1495  *    default speed defined in the BulletTypeClass structure.                                  *
   1496  *                                                                                             *
   1497  * INPUT:   target   -- The target that the projectile is heading for.                         *
   1498  *                                                                                             *
   1499  *          which    -- Which weapon to use in the attack. 0=primary, 1=secondary.             *
   1500  *                                                                                             *
   1501  * OUTPUT:  Returns with a pointer to the bullet that was created as a result of this attack.  *
   1502  *                                                                                             *
   1503  * WARNINGS:   none                                                                            *
   1504  *                                                                                             *
   1505  * HISTORY:                                                                                    *
   1506  *   03/19/1995 JLB : Created.                                                                 *
   1507  *=============================================================================================*/
   1508 BulletClass * AircraftClass::Fire_At(TARGET target, int which)
   1509 {
   1510 	assert(Aircraft.ID(this) == ID);
   1511 	assert(IsActive);
   1512 
   1513 	/*
   1514 	**	Passenger aircraft will actually paradrop their cargo instead of
   1515 	**	firing their weapon.
   1516 	*/
   1517 	if (Is_Something_Attached()) {
   1518 		Paradrop_Cargo();
   1519 		return(0);
   1520 	}
   1521 
   1522 	/*
   1523 	**	If the weapon is actually a camera, then perform the "snapshot" of the
   1524 	**	ground instead of normal weapon fire.
   1525 	*/
   1526 	if (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->IsCamera) {
   1527 		
   1528 		if (Session.Type != GAME_GLYPHX_MULTIPLAYER) {
   1529 
   1530 			if (House->Is_Ally(PlayerPtr)) {
   1531 				Map.Sight_From(Coord_Cell(Center_Coord()), 9, House, false);
   1532 			}
   1533 			
   1534 		} else {
   1535 		
   1536 			for (int i = 0; i < Session.Players.Count(); i++) {
   1537 				HousesType house_type = Session.Players[i]->Player.ID;
   1538 				HouseClass *house = HouseClass::As_Pointer(house_type);
   1539 
   1540 				if (house == House || House->Is_Ally(house)) {
   1541 					Map.Sight_From(Coord_Cell(Center_Coord()), 9, house, false);
   1542 				}
   1543 			}
   1544 		}
   1545 		
   1546 		Ammo = 0;
   1547 		Arm = Rearm_Delay(IsSecondShot);
   1548 		return(0);
   1549 	}
   1550 
   1551 
   1552 	BulletClass * bullet = FootClass::Fire_At(target, which);
   1553 
   1554 	if (bullet) {
   1555 
   1556 		/*
   1557 		**	Aircraft reveal when firing
   1558 		*/
   1559 		HouseClass *player = HouseClass::As_Pointer(Owner());
   1560 		if (player != nullptr && player->IsHuman) {
   1561 			Map.Sight_From(Coord_Cell(Center_Coord()), 1, player, false);
   1562 		}
   1563 
   1564 		/*
   1565 		**	Falling bullets move at a speed proportionate to the delivery craft.
   1566 		*/
   1567 		if (bullet->Class->IsDropping) {
   1568 			bullet->Fly_Speed(40, MPH_MEDIUM_SLOW);		// TCTC To fix.
   1569 		}
   1570 	}
   1571 	return(bullet);
   1572 }
   1573 
   1574 
   1575 /***********************************************************************************************
   1576  * AircraftClass::Take_Damage -- Applies damage to the aircraft.                               *
   1577  *                                                                                             *
   1578  *    This routine is used to apply damage to the specified aircraft. This is where any        *
   1579  *    special crash animation will be initiated.                                               *
   1580  *                                                                                             *
   1581  * INPUT:   damage   -- Reference to the damage that will be applied to the aircraft.          *
   1582  *                      This value will be filled in with the actual damage that was           *
   1583  *                      applied.                                                               *
   1584  *                                                                                             *
   1585  *          distance -- Distance from the source of the explosion to this aircraft.            *
   1586  *                                                                                             *
   1587  *          warhead  -- The warhead type that the damage occurs from.                          *
   1588  *                                                                                             *
   1589  *          source   -- Pointer to the originator of the damage. This can be used so that      *
   1590  *                      proper "thank you" can be delivered.                                   *
   1591  *                                                                                             *
   1592  * OUTPUT:  Returns with the result of the damage as it affects this aircraft.                 *
   1593  *                                                                                             *
   1594  * WARNINGS:   none                                                                            *
   1595  *                                                                                             *
   1596  * HISTORY:                                                                                    *
   1597  *   05/26/1995 JLB : Created.                                                                 *
   1598  *=============================================================================================*/
   1599 ResultType AircraftClass::Take_Damage(int & damage, int distance, WarheadType warhead, TechnoClass * source, bool forced)
   1600 {
   1601 	assert(Aircraft.ID(this) == ID);
   1602 	assert(IsActive);
   1603 
   1604 	ResultType res = RESULT_NONE;
   1605 
   1606 	/*
   1607 	**	Flying aircraft take half damage.
   1608 	*/
   1609 	if (Height) {
   1610 		damage /= 2;
   1611 	}
   1612 
   1613 	/*
   1614 	**	Apply the damage to the aircraft.
   1615 	*/
   1616 	res = FootClass::Take_Damage(damage, distance, warhead, source, forced);
   1617 
   1618 	/*
   1619 	**	Special action is performed if the aircraft is killed -- the cargo is destroyed
   1620 	**	as well.
   1621 	*/
   1622 	switch (res) {
   1623 		case RESULT_DESTROYED:
   1624 			{
   1625 				Kill_Cargo(source);
   1626 				Death_Announcement();
   1627 				COORDINATE coord = Target_Coord();
   1628 				if (!(coord & HIGH_COORD_MASK)) {
   1629 					new AnimClass(ANIM_FBALL1, coord);
   1630 				}
   1631 
   1632 				/*
   1633 				**	Parachute a survivor if possible.
   1634 				*/
   1635 				if (Class->IsCrew && Percent_Chance(90) && Map[Center_Coord()].Is_Clear_To_Move(SPEED_FOOT, true, false)) {
   1636 					InfantryClass * infantry = new InfantryClass(INFANTRY_E1, House->Class->House);
   1637 					if (infantry != NULL) {
   1638 						if (!infantry->Paradrop(Center_Coord())) {
   1639 							delete infantry;
   1640 						}
   1641 					}
   1642 				}
   1643 
   1644 				delete this;
   1645 			}
   1646 			break;
   1647 
   1648 		default:
   1649 		case RESULT_HALF:
   1650 			break;
   1651 	}
   1652 
   1653 	return(res);
   1654 }
   1655 
   1656 
   1657 /***********************************************************************************************
   1658  * AircraftClass::Mission_Move -- Handles movement mission.                                    *
   1659  *                                                                                             *
   1660  *    This state machine routine is used when an aircraft (usually helicopter) is to move      *
   1661  *    from one location to another. It will handle any necessary take off and landing this     *
   1662  *    may require.                                                                             *
   1663  *                                                                                             *
   1664  * INPUT:   none                                                                               *
   1665  *                                                                                             *
   1666  * OUTPUT:  Returns with the number of game frames that should elapse before this routine      *
   1667  *          is called again.                                                                   *
   1668  *                                                                                             *
   1669  * WARNINGS:   none                                                                            *
   1670  *                                                                                             *
   1671  * HISTORY:                                                                                    *
   1672  *   06/19/1995 JLB : Created.                                                                 *
   1673  *=============================================================================================*/
   1674 int AircraftClass::Mission_Move(void)
   1675 {
   1676 	assert(Aircraft.ID(this) == ID);
   1677 	assert(IsActive);
   1678 
   1679 	if (Class->IsFixedWing) {
   1680 
   1681 		enum  {
   1682 			TAKE_OFF,
   1683 			FLY_TOWARD_TARGET
   1684 		};
   1685 
   1686 		switch (Status) {
   1687 			int distance;
   1688 
   1689 			case TAKE_OFF:
   1690 
   1691 				/*
   1692 				**	If the aircraft is high enough to begin its mission, then do so.
   1693 				*/
   1694 				if (Process_Take_Off())  {
   1695 					IsTakingOff = false;
   1696 					Set_Speed(0xFF);
   1697 
   1698 					/*
   1699 					**	After takeoff is complete, break radio contact.
   1700 					*/
   1701 					if (In_Radio_Contact() && Map[Coord].Cell_Building() == Contact_With_Whom()) {
   1702 						Transmit_Message(RADIO_OVER_OUT);
   1703 					}
   1704 
   1705 					Status = FLY_TOWARD_TARGET;
   1706 				}
   1707 				return(1);
   1708 
   1709 			case FLY_TOWARD_TARGET:
   1710 				PrimaryFacing.Set_Desired(Direction(NavCom));
   1711 				distance = Distance(NavCom);
   1712 
   1713 				if (distance < 0x00C0) {
   1714 					MissionType mission = MISSION_GUARD;
   1715 
   1716 					if (!IsALoaner) {
   1717 						/*
   1718 						**	Normal aircraft try to find a good landing spot to rest.
   1719 						*/
   1720 						BuildingClass * building = Find_Docking_Bay(Class->Building, false);
   1721 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   1722 	if (!Class->IsFixedWing) {
   1723 		int dist = 0x7FFFFFFF;
   1724 		if (building) dist=Distance(building);
   1725 		for (int index = 0; index < Vessels.Count(); index++) {
   1726 			VesselClass *ship = Vessels.Ptr(index);
   1727 			if (ship != NULL && *ship == VESSEL_CARRIER && !ship->IsInLimbo && ship->IsActive && ship->House == House && ship->How_Many() < ship->Class->Max_Passengers() ) {
   1728 				if (Distance(ship) < dist || !building) {
   1729 					building = (BuildingClass *)ship;
   1730 					dist = Distance(ship);
   1731 				}
   1732 //				break;
   1733 			}
   1734 		}
   1735 	}
   1736 #endif
   1737 						Assign_Destination(TARGET_NONE);
   1738 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   1739 						if (building && (Transmit_Message(RADIO_HELLO, building) == RADIO_ROGER || building->What_Am_I() == RTTI_VESSEL) ) {
   1740 #else
   1741 						if (building && Transmit_Message(RADIO_HELLO, building) == RADIO_ROGER) {
   1742 #endif
   1743 							mission = MISSION_ENTER;
   1744 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   1745 	if (building->What_Am_I() == RTTI_VESSEL) {
   1746 		Assign_Destination(building->As_Target());
   1747 	}
   1748 #endif
   1749 						} else {
   1750 							Assign_Destination(Good_LZ());
   1751 
   1752 							/*
   1753 							** If this aircraft has nowhere else to go, meaning that
   1754 							** there is no airfield available, then it has to crash.
   1755 							*/
   1756 							if (Is_Target_Cell(NavCom)) {
   1757 
   1758 								if (Process_Landing()) {
   1759 									Strength = 1;
   1760 									int damage = Strength;
   1761 									Take_Damage(damage, 0, WARHEAD_AP, 0, true);
   1762 									return(1);
   1763 								}
   1764 								return(500);
   1765 							}
   1766 							mission = MISSION_MOVE;
   1767 						}
   1768 						Assign_Mission(mission);
   1769 						Commence();
   1770 					} else {
   1771 						if (!Team.Is_Valid()) {
   1772 							Enter_Idle_Mode();
   1773 						}
   1774 					}
   1775 					return(1);
   1776 				}
   1777 				break;
   1778 
   1779 			default:
   1780 				break;
   1781 		}
   1782 
   1783 		return(5);
   1784 	}
   1785 
   1786 	enum {
   1787 		VALIDATE_LZ,
   1788 		TAKE_OFF,
   1789 		FLY_TO_LZ,
   1790 		LAND
   1791 	};
   1792 	switch (Status) {
   1793 
   1794 		/*
   1795 		**	Double check and change LZ if necessary.
   1796 		*/
   1797 		case VALIDATE_LZ:
   1798 			if (!Target_Legal(NavCom)) {
   1799 				Enter_Idle_Mode();
   1800 			} else {
   1801 				if (!Is_LZ_Clear(NavCom) || !Cell_Seems_Ok(As_Cell(NavCom))) {
   1802 					Assign_Destination(New_LZ(NavCom));
   1803 					if (Team.Is_Valid()) {
   1804 						Team->Assign_Mission_Target(NavCom);
   1805 					}
   1806 				} else {
   1807 					Status = TAKE_OFF;
   1808 				}
   1809 			}
   1810 			break;
   1811 
   1812 		/*
   1813 		**	Take off if necessary.
   1814 		*/
   1815 		case TAKE_OFF:
   1816 			if (!Target_Legal(NavCom)) {
   1817 				Status = VALIDATE_LZ;
   1818 			} else {
   1819 				if (Process_Take_Off()) {
   1820 
   1821 					/*
   1822 					**	After takeoff is complete, break radio contact with any helipad that this
   1823 					**	helicopter is taking off from.
   1824 					*/
   1825 					if (In_Radio_Contact() && Map[Coord].Cell_Building() == Contact_With_Whom()) {
   1826 						Transmit_Message(RADIO_OVER_OUT);
   1827 					}
   1828 
   1829 					Status = FLY_TO_LZ;
   1830 				}
   1831 				return(1);
   1832 			}
   1833 			break;
   1834 
   1835 		/*
   1836 		**	Fly toward target.
   1837 		*/
   1838 		case FLY_TO_LZ:
   1839 			if (Is_LZ_Clear(NavCom)) {
   1840 				int distance = Process_Fly_To(true, NavCom);
   1841 
   1842 				if (distance < 0x0080) {
   1843 					if (Target_Legal(TarCom)) {
   1844 						SecondaryFacing.Set_Desired(Direction(TarCom));
   1845 					} else {
   1846 						SecondaryFacing.Set_Desired(Pose_Dir());
   1847 					}
   1848 
   1849 					if (distance < 0x0010) {
   1850 						Status = LAND;
   1851 					}
   1852 					return(1);
   1853 				}
   1854 
   1855 //				SecondaryFacing.Set_Desired(::Direction(Fire_Coord(0), As_Coord(NavCom)));
   1856 				SecondaryFacing.Set_Desired(Direction(NavCom));
   1857 
   1858 			} else {
   1859 				Assign_Destination(New_LZ(NavCom));
   1860 				if (Team.Is_Valid()) {
   1861 					Team->Assign_Mission_Target(NavCom);
   1862 				}
   1863 				if (!Target_Legal(NavCom)) {
   1864 					Status = LAND;
   1865 				}
   1866 			}
   1867 			return(1);
   1868 
   1869 		/*
   1870 		**	Land on target.
   1871 		*/
   1872 		case LAND:
   1873 			if (IsTakingOff) {
   1874 				Assign_Destination(New_LZ(NavCom));
   1875 				if (Team.Is_Valid()) {
   1876 					Team->Assign_Mission_Target(NavCom);
   1877 				}
   1878 				Status = TAKE_OFF;
   1879 			}
   1880 			if (Process_Landing()) {
   1881 				if (MissionQueue == MISSION_NONE) {
   1882 					Enter_Idle_Mode();
   1883 				}
   1884 			}
   1885 			return(1);
   1886 
   1887 		default:
   1888 			break;
   1889 	}
   1890 
   1891 	return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
   1892 }
   1893 
   1894 
   1895 /***********************************************************************************************
   1896  * AircraftClass::Enter_Idle_Mode -- Gives the aircraft an appropriate mission.                *
   1897  *                                                                                             *
   1898  *    Use this routine when the mission for the aircraft is in doubt. This routine will find   *
   1899  *    an appropriate mission for the aircraft and dispatch it.                                 *
   1900  *                                                                                             *
   1901  * INPUT:   initial  -- Is this called when the unit just leaves a factory or is initially     *
   1902  *                      or is initially placed on the map?                                     *
   1903  *                                                                                             *
   1904  * OUTPUT:  none                                                                               *
   1905  *                                                                                             *
   1906  * WARNINGS:   none                                                                            *
   1907  *                                                                                             *
   1908  * HISTORY:                                                                                    *
   1909  *   06/05/1995 JLB : Created.                                                                 *
   1910  *=============================================================================================*/
   1911 void AircraftClass::Enter_Idle_Mode(bool )
   1912 {
   1913 	assert(Aircraft.ID(this) == ID);
   1914 	assert(IsActive);
   1915 
   1916 	MissionType mission = (Class->IsFixedWing && IsALoaner && Class->PrimaryWeapon != NULL) ? MISSION_HUNT : MISSION_GUARD;
   1917 	if (Class->IsFixedWing) {
   1918 		if (In_Which_Layer() == LAYER_GROUND) {
   1919 			if (IsALoaner) {
   1920 				mission = MISSION_RETREAT;
   1921 			} else {
   1922 				Assign_Destination(TARGET_NONE);
   1923 				Assign_Target(TARGET_NONE);
   1924 				mission = MISSION_GUARD;
   1925 			}
   1926 		} else {
   1927 
   1928 			/*
   1929 			**	If this transport is a loaner and part of a team, then remove it from
   1930 			**	the team it is attached to.
   1931 			*/
   1932 			if ((IsALoaner && House->IsHuman) || (!House->IsHuman && !Ammo)) {
   1933 				if (Team.Is_Valid() && Team->Has_Entered_Map()) {
   1934 					Team->Remove(this);
   1935 				}
   1936 			}
   1937 			if (Team.Is_Valid()) return;
   1938 
   1939 			/*
   1940 			**	Weapon equipped helicopters that run out of ammo and were
   1941 			**	brought in as reinforcements will leave the map.
   1942 			*/
   1943 			if (Mission != MISSION_ATTACK && IsALoaner && Ammo == 0 && Class->PrimaryWeapon != NULL) {
   1944 				mission = MISSION_HUNT;
   1945 			} else {
   1946 
   1947 				if (!IsALoaner) {
   1948 					/*
   1949 					**	Normal aircraft try to find a good landing spot to rest.
   1950 					*/
   1951 					BuildingClass * building = NULL;
   1952 					if (In_Radio_Contact() && Contact_With_Whom()->What_Am_I() == RTTI_BUILDING) {
   1953 						building = (BuildingClass *)Contact_With_Whom();
   1954 					} else {
   1955 						building = Find_Docking_Bay(Class->Building, false);
   1956 						if (Transmit_Message(RADIO_HELLO, building) != RADIO_ROGER) {
   1957 							building = NULL;
   1958 						}
   1959 					}
   1960 					Assign_Destination(TARGET_NONE);
   1961 					if (building != NULL) {
   1962 						if (Class->IsFixedWing) {
   1963 							Status = 0;	//BG - reset the mission status to avoid landing on the ground next to the airstrip
   1964 							if (IsLanding) {
   1965 								Process_Take_Off();
   1966 							}
   1967 						}
   1968 						mission = MISSION_ENTER;
   1969 					} else {
   1970 						mission = MISSION_RETREAT;
   1971 					}
   1972 				}
   1973 			}
   1974 		}
   1975 
   1976 	} else {
   1977 
   1978 		if (In_Which_Layer() == LAYER_GROUND) {
   1979 			if (IsALoaner) {
   1980 				if (Is_Something_Attached()) {
   1981 
   1982 					/*
   1983 					**	In the case of a computer controlled helicopter that hold passengers,
   1984 					**	don't unload when landing. Wait for specific instructions from the
   1985 					**	controlling team.
   1986 					*/
   1987 					if (Team.Is_Valid()) {
   1988 //					if (Team.Is_Valid() && !House->IsHuman) {
   1989 						mission = MISSION_GUARD;
   1990 					} else {
   1991 						mission = MISSION_UNLOAD;
   1992 					}
   1993 				} else {
   1994 					mission = MISSION_RETREAT;
   1995 				}
   1996 			} else {
   1997 				Assign_Destination(TARGET_NONE);
   1998 				Assign_Target(TARGET_NONE);
   1999 				mission = MISSION_GUARD;
   2000 			}
   2001 		} else {
   2002 			if (Is_Something_Attached()) {
   2003 				if (IsALoaner) {
   2004 					if (Team) {
   2005 						mission = MISSION_GUARD;
   2006 					} else {
   2007 						mission = MISSION_UNLOAD;
   2008 						Assign_Destination(Good_LZ());
   2009 					}
   2010 				} else {
   2011 					Assign_Destination(Good_LZ());
   2012 					mission = MISSION_MOVE;
   2013 				}
   2014 			} else {
   2015 
   2016 				/*
   2017 				**	If this transport is a loaner and part of a team, then remove it from
   2018 				**	the team it is attached to.
   2019 				*/
   2020 				if ((IsALoaner && House->IsHuman) || (!House->IsHuman && !Ammo)) {
   2021 					if (Team.Is_Valid() && Team->Has_Entered_Map()) {
   2022 						Team->Remove(this);
   2023 					}
   2024 				}
   2025 
   2026 				if (Class->PrimaryWeapon != NULL) {
   2027 
   2028 					/*
   2029 					**	Weapon equipped helicopters that run out of ammo and were
   2030 					**	brought in as reinforcements will leave the map.
   2031 					*/
   2032 					if (IsALoaner) {
   2033 
   2034 						/*
   2035 						**	If it has no ammo, then break off of the team and leave the map.
   2036 						**	If it can fight, then give it fighting orders.
   2037 						*/
   2038 						if (Ammo == 0) {
   2039 							if (Team.Is_Valid()) Team->Remove(this);
   2040 							mission = MISSION_RETREAT;
   2041 						} else {
   2042 							if (!Team.Is_Valid()) {
   2043 								mission = MISSION_HUNT;
   2044 							}
   2045 						}
   2046 
   2047 					} else {
   2048 
   2049 						/*
   2050 						**	Normal aircraft try to find a good landing spot to rest.
   2051 						*/
   2052 						BuildingClass * building = Find_Docking_Bay(Class->Building, false);
   2053 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   2054 	if (!Class->IsFixedWing) {
   2055 		int dist = 0x7FFFFFFF;
   2056 		if (building) dist=Distance(building);
   2057 		for (int index = 0; index < Vessels.Count(); index++) {
   2058 			VesselClass *ship = Vessels.Ptr(index);
   2059 			if (ship != NULL && *ship == VESSEL_CARRIER && !ship->IsInLimbo && ship->IsActive && ship->House == House && ship->How_Many() < ship->Class->Max_Passengers()/* && !ship->In_Radio_Contact()*/) {
   2060 				if (Distance(ship) < dist || !building) {
   2061 					building = (BuildingClass *)ship;
   2062 					dist = Distance(ship);
   2063 				}
   2064 //				break;
   2065 			}
   2066 		}
   2067 	}
   2068 #endif
   2069 						Assign_Destination(TARGET_NONE);
   2070 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   2071 						if (building && (Transmit_Message(RADIO_HELLO, building) == RADIO_ROGER || building->What_Am_I() == RTTI_VESSEL) ) {
   2072 #else
   2073 						if (building && Transmit_Message(RADIO_HELLO, building) == RADIO_ROGER) {
   2074 #endif
   2075 							mission = MISSION_ENTER;
   2076 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   2077 							if (building->What_Am_I() == RTTI_VESSEL) {
   2078 								Assign_Destination(building->As_Target());
   2079 							}
   2080 #endif
   2081 						} else {
   2082 							Assign_Destination(Good_LZ());
   2083 							mission = MISSION_MOVE;
   2084 						}
   2085 					}
   2086 				} else {
   2087 					if (Team) return;
   2088 
   2089 					Assign_Destination(Good_LZ());
   2090 					mission = MISSION_MOVE;
   2091 				}
   2092 			}
   2093 		}
   2094 	}
   2095 	Assign_Mission(mission);
   2096 	Commence();
   2097 }
   2098 
   2099 
   2100 /***********************************************************************************************
   2101  * AircraftClass::Process_Fly_To -- Handles state machine for flying to destination.           *
   2102  *                                                                                             *
   2103  *    This support routine is used when the helicopter is to fly to the destination. It can    *
   2104  *    optionally slow the helicopter down as it approaches the destination.                    *
   2105  *                                                                                             *
   2106  * INPUT:   slowdown -- Should the aircraft be slowed down when it approaches the dest?        *
   2107  *                                                                                             *
   2108  * OUTPUT:  Returns with the distance remaining between the aircraft and the destination.      *
   2109  *                                                                                             *
   2110  * WARNINGS:   Because the aircraft can move at a fast speed, the distance to target value     *
   2111  *             will probably never be zero. The likely case will be that the aircraft          *
   2112  *             overshoots the target.                                                          *
   2113  *                                                                                             *
   2114  * HISTORY:                                                                                    *
   2115  *   06/14/1995 JLB : Created.                                                                 *
   2116  *   03/05/1996 JLB : Specifies destination target value.                                      *
   2117  *=============================================================================================*/
   2118 int AircraftClass::Process_Fly_To(bool slowdown, TARGET dest)
   2119 {
   2120 	assert(Aircraft.ID(this) == ID);
   2121 	assert(IsActive);
   2122 
   2123 	if (Class->IsFixedWing) slowdown = false;
   2124 
   2125 	COORDINATE coord;
   2126 	if (Is_Target_Building(dest)) {
   2127 		coord = As_Building(dest)->Docking_Coord();
   2128 	} else {
   2129 		coord = As_Coord(dest);
   2130 	}
   2131 	int distance = Distance(coord);
   2132 
   2133 	PrimaryFacing.Set_Desired(Direction(coord));
   2134 
   2135 	if (slowdown) {
   2136 		int speed = min(distance, 0x0300);
   2137 		speed = Bound(speed/3, 0x0020, 0x00FF);
   2138 		if (Speed != speed) {
   2139 			Set_Speed(speed);
   2140 		}
   2141 	}
   2142 
   2143 	if (distance < 0x0010) {
   2144 		if (slowdown) {
   2145 			Set_Speed(0);
   2146 		}
   2147 		distance = 0;
   2148 	}
   2149 	return(distance);
   2150 }
   2151 
   2152 
   2153 #ifdef CHEAT_KEYS
   2154 /***********************************************************************************************
   2155  * AircraftClass::Debug_Dump -- Displays the status of the aircraft to the mono monitor.       *
   2156  *                                                                                             *
   2157  *    This displays the current status of the aircraft class to the mono monitor. By this      *
   2158  *    display bugs may be tracked down or prevented.                                           *
   2159  *                                                                                             *
   2160  * INPUT:   none                                                                               *
   2161  *                                                                                             *
   2162  * OUTPUT:  none                                                                               *
   2163  *                                                                                             *
   2164  * WARNINGS:   none                                                                            *
   2165  *                                                                                             *
   2166  * HISTORY:                                                                                    *
   2167  *   06/02/1994 JLB : Created.                                                                 *
   2168  *=============================================================================================*/
   2169 void AircraftClass::Debug_Dump(MonoClass * mono) const
   2170 {
   2171 	assert(Aircraft.ID(this) == ID);
   2172 	assert(IsActive);
   2173 
   2174 	mono->Set_Cursor(0, 0);
   2175 	mono->Print(Text_String(TXT_DEBUG_AIRCRAFT));
   2176 	mono->Set_Cursor(1, 11);mono->Printf("%3d", AttacksRemaining);
   2177 
   2178 	FootClass::Debug_Dump(mono);
   2179 }
   2180 #endif
   2181 
   2182 
   2183 /***********************************************************************************************
   2184  * AircraftClass::Active_Click_With -- Handles clicking over specified object.                 *
   2185  *                                                                                             *
   2186  *    This routine is used when the player clicks over the speicifed object. It will assign    *
   2187  *    the appropriate mission to the aircraft.                                                 *
   2188  *                                                                                             *
   2189  * INPUT:   action   -- The action that was nominally determined by the What_Action function.  *
   2190  *                                                                                             *
   2191  *          object   -- The object over which the mouse was clicked.                           *
   2192  *                                                                                             *
   2193  * OUTPUT:  none                                                                               *
   2194  *                                                                                             *
   2195  * WARNINGS:   This routine will alter the game sequence and causes an event packet to be      *
   2196  *             propagated to all connected machines.                                           *
   2197  *                                                                                             *
   2198  * HISTORY:                                                                                    *
   2199  *   06/19/1995 JLB : Created.                                                                 *
   2200  *=============================================================================================*/
   2201 void AircraftClass::Active_Click_With(ActionType action, ObjectClass * object)
   2202 {
   2203 	assert(Aircraft.ID(this) == ID);
   2204 	assert(IsActive);
   2205 
   2206 	action = What_Action(object);
   2207 
   2208 	switch (action) {
   2209 		case ACTION_NOMOVE:
   2210 			return;
   2211 
   2212 		case ACTION_ENTER:
   2213 			Player_Assign_Mission(MISSION_ENTER, TARGET_NONE, object->As_Target());
   2214 			break;
   2215 
   2216 		case ACTION_SELF:
   2217 			Player_Assign_Mission(MISSION_UNLOAD, TARGET_NONE, TARGET_NONE);
   2218 			break;
   2219 
   2220 		default:
   2221 			break;
   2222 	}
   2223 	FootClass::Active_Click_With(action, object);
   2224 }
   2225 
   2226 
   2227 /***********************************************************************************************
   2228  * AircraftClass::Active_Click_With -- Handles clicking over specified cell.                   *
   2229  *                                                                                             *
   2230  *    This routine is used when the player clicks the mouse of the specified cell. It will     *
   2231  *    assign the appropriate mission to the aircraft.                                          *
   2232  *                                                                                             *
   2233  * INPUT:   action   -- The action nominally determined by What_Action().                      *
   2234  *                                                                                             *
   2235  *          cell     -- The cell over which the mouse was clicked.                             *
   2236  *                                                                                             *
   2237  * OUTPUT:  none                                                                               *
   2238  *                                                                                             *
   2239  * WARNINGS:   This routine will affect the game sequence and causes an event object to be     *
   2240  *             propagated to all connected machines.                                           *
   2241  *                                                                                             *
   2242  * HISTORY:                                                                                    *
   2243  *   06/19/1995 JLB : Created.                                                                 *
   2244  *=============================================================================================*/
   2245 void AircraftClass::Active_Click_With(ActionType action, CELL cell)
   2246 {
   2247 	assert(Aircraft.ID(this) == ID);
   2248 	assert(IsActive);
   2249 
   2250 	FootClass::Active_Click_With(action, cell);
   2251 }
   2252 
   2253 
   2254 /***********************************************************************************************
   2255  * AircraftClass::Player_Assign_Mission -- Handles player input to assign a mission.           *
   2256  *                                                                                             *
   2257  *    This routine is called as a result of player input with the intent to change the         *
   2258  *    mission of the aircraft.                                                                 *
   2259  *                                                                                             *
   2260  * INPUT:   mission  -- The mission requested of the aircraft.                                 *
   2261  *                                                                                             *
   2262  *          target   -- The value to assign to the aircraft's targeting computer.              *
   2263  *                                                                                             *
   2264  *          dest.    -- The value to assign to the aircraft's navigation computer.             *
   2265  *                                                                                             *
   2266  * OUTPUT:  none                                                                               *
   2267  *                                                                                             *
   2268  * WARNINGS:   The mission specified will be executed at an indeterminate future game frame.   *
   2269  *             This is controlled by net/modem propagation delay.                              *
   2270  *                                                                                             *
   2271  * HISTORY:                                                                                    *
   2272  *   06/19/1995 JLB : Created.                                                                 *
   2273  *=============================================================================================*/
   2274 void AircraftClass::Player_Assign_Mission(MissionType mission, TARGET target, TARGET destination)
   2275 {
   2276 	assert(Aircraft.ID(this) == ID);
   2277 	assert(IsActive);
   2278 
   2279 	if (AllowVoice) {
   2280 		if (mission == MISSION_ATTACK) {
   2281 			Response_Attack();
   2282 		} else {
   2283 			Response_Move();
   2284 		}
   2285 	}
   2286 	Queue_Mission(TargetClass(this), mission, target, destination);
   2287 }
   2288 
   2289 
   2290 /***********************************************************************************************
   2291  * AircraftClass::What_Action -- Determines what action to perform.                            *
   2292  *                                                                                             *
   2293  *    This routine is used to determine what action will likely be performed if the mouse      *
   2294  *    were clicked over the object specified. The display system calls this routine to         *
   2295  *    control the mouse shape.                                                                 *
   2296  *                                                                                             *
   2297  * INPUT:   target   -- Pointer to the object that the mouse is currently over.                *
   2298  *                                                                                             *
   2299  * OUTPUT:  Returns with the action that will occur if the mouse were clicked over the         *
   2300  *          object specified.                                                                  *
   2301  *                                                                                             *
   2302  * WARNINGS:   none                                                                            *
   2303  *                                                                                             *
   2304  * HISTORY:                                                                                    *
   2305  *   06/19/1995 JLB : Created.                                                                 *
   2306  *=============================================================================================*/
   2307 ActionType AircraftClass::What_Action(ObjectClass const * target) const
   2308 {
   2309 	assert(Aircraft.ID(this) == ID);
   2310 	assert(IsActive);
   2311 
   2312 	ActionType action = FootClass::What_Action(target);
   2313 
   2314 	if (action == ACTION_SELF && (!How_Many() || (Height > 0) || IsTethered)) {
   2315 		action = ACTION_NONE;
   2316 	}
   2317 
   2318 	if (action == ACTION_ATTACK && Class->PrimaryWeapon == NULL) {
   2319 		action = ACTION_NONE;
   2320 	}
   2321 
   2322 	if (House->IsPlayerControl && House->Is_Ally(target) && target->What_Am_I() == RTTI_BUILDING && ((AircraftClass *)this)->Transmit_Message(RADIO_CAN_LOAD, (TechnoClass*)target) == RADIO_ROGER) {
   2323 		action = ACTION_ENTER;
   2324 	}
   2325 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   2326 	if (!Class->IsFixedWing && House->IsPlayerControl && House->Is_Ally(target) && target->What_Am_I() == RTTI_VESSEL && *(VesselClass *)target == VESSEL_CARRIER && ((AircraftClass *)this)->Transmit_Message(RADIO_CAN_LOAD, (TechnoClass*)target) == RADIO_ROGER) {
   2327 		action = ACTION_ENTER;
   2328 	}
   2329 #endif
   2330 
   2331 	if (Class->IsFixedWing && action == ACTION_MOVE) {
   2332 		action = ACTION_NOMOVE;
   2333 	}
   2334 
   2335 	if (action == ACTION_NONE) {
   2336 		action = What_Action(Coord_Cell(target->Center_Coord()));
   2337 	}
   2338 
   2339 	/*
   2340 	**	Special return to friendly repair factory action.
   2341 	*/
   2342 	if (House->IsPlayerControl && action == ACTION_SELECT && target->What_Am_I() == RTTI_BUILDING) {
   2343 		BuildingClass * building = (BuildingClass *)target;
   2344 		if (building->Class->Type == STRUCT_REPAIR && !building->In_Radio_Contact() && !building->Is_Something_Attached()) {
   2345 			action = ACTION_ENTER;
   2346 		}
   2347 	}
   2348 
   2349 	return(action);
   2350 }
   2351 
   2352 
   2353 /***********************************************************************************************
   2354  * AircraftClass::What_Action -- Determines what action to perform.                            *
   2355  *                                                                                             *
   2356  *    This routine will determine what action would occur if the mouse were clicked over the   *
   2357  *    cell specified. The display system calls this routine to determine what mouse shape      *
   2358  *    to use.                                                                                  *
   2359  *                                                                                             *
   2360  * INPUT:   cell  -- The cell over which the mouse is currently positioned.                    *
   2361  *                                                                                             *
   2362  * OUTPUT:  Returns with the action that will be performed if the mouse were clicked at the    *
   2363  *          specified cell location.                                                           *
   2364  *                                                                                             *
   2365  * WARNINGS:   none                                                                            *
   2366  *                                                                                             *
   2367  * HISTORY:                                                                                    *
   2368  *   06/19/1995 JLB : Created.                                                                 *
   2369  *=============================================================================================*/
   2370 ActionType AircraftClass::What_Action(CELL cell) const
   2371 {
   2372 	assert(Aircraft.ID(this) == ID);
   2373 	assert(IsActive);
   2374 
   2375 	ActionType action = FootClass::What_Action(cell);
   2376 
   2377 	//using function for IsVisible so we have different results for different players - JAS 2019/09/30
   2378 	if ((action == ACTION_MOVE || action == ACTION_ATTACK) && !Map[cell].Is_Visible(PlayerPtr)) {
   2379 		action = ACTION_NOMOVE;
   2380 	}
   2381 
   2382 	if (action == ACTION_ATTACK && Class->PrimaryWeapon == NULL) {
   2383 		action = ACTION_NONE;
   2384 	}
   2385 
   2386 	if (Class->IsFixedWing && action == ACTION_MOVE) {
   2387 		action = ACTION_NOMOVE;
   2388 	}
   2389 
   2390 	return(action);
   2391 }
   2392 
   2393 
   2394 /***********************************************************************************************
   2395  * AircraftClass::Pose_Dir -- Fetches the natural landing facing.                              *
   2396  *                                                                                             *
   2397  *    Use this routine to get the desired facing the aircraft should assume when landing.      *
   2398  *                                                                                             *
   2399  * INPUT:   none                                                                               *
   2400  *                                                                                             *
   2401  * OUTPUT:  Returns with the normal default facing the aircraft should have when landed.       *
   2402  *                                                                                             *
   2403  * WARNINGS:   none                                                                            *
   2404  *                                                                                             *
   2405  * HISTORY:                                                                                    *
   2406  *   06/19/1995 JLB : Created.                                                                 *
   2407  *   03/04/1996 JLB : Fixed wing aircraft always face down the runway.                         *
   2408  *=============================================================================================*/
   2409 DirType AircraftClass::Pose_Dir(void) const
   2410 {
   2411 	assert(Aircraft.ID(this) == ID);
   2412 	assert(IsActive);
   2413 
   2414 	if (*this == AIRCRAFT_TRANSPORT) {
   2415 		return(DIR_N);
   2416 	}
   2417 	if (Class->IsFixedWing)  {
   2418 		return(DIR_E);
   2419 	}
   2420 	return(DIR_NE);
   2421 }
   2422 
   2423 
   2424 /***********************************************************************************************
   2425  * AircraftClass::Mission_Attack -- Handles the attack mission for aircraft.                   *
   2426  *                                                                                             *
   2427  *    This routine is the state machine that handles the attack mission for aircraft. It will  *
   2428  *    handling homing in on and firing on the target in the aircraft's targeting computer.     *
   2429  *                                                                                             *
   2430  * INPUT:   none                                                                               *
   2431  *                                                                                             *
   2432  * OUTPUT:  Returns with the number of game ticks to pass before this routine must be called   *
   2433  *          again.                                                                             *
   2434  *                                                                                             *
   2435  * WARNINGS:   none                                                                            *
   2436  *                                                                                             *
   2437  * HISTORY:                                                                                    *
   2438  *   06/19/1995 JLB : Created.                                                                 *
   2439  *   09/22/1995 JLB : Fixes brain dead helicopter for Nod scen #7.                             *
   2440  *=============================================================================================*/
   2441 int AircraftClass::Mission_Attack(void)
   2442 {
   2443 	assert(Aircraft.ID(this) == ID);
   2444 	assert(IsActive);
   2445 
   2446 	if (Class->IsFixedWing) {
   2447 		return(Mission_Hunt());
   2448 	}
   2449 
   2450 	enum {
   2451 		VALIDATE_AZ,
   2452 		PICK_ATTACK_LOCATION,
   2453 		TAKE_OFF,
   2454 		FLY_TO_POSITION,
   2455 		FIRE_AT_TARGET,
   2456 		FIRE_AT_TARGET2,
   2457 		RETURN_TO_BASE
   2458 	};
   2459 	switch (Status) {
   2460 
   2461 		/*
   2462 		**	Double check target and validate the attack zone.
   2463 		*/
   2464 		case VALIDATE_AZ:
   2465 			if (!Target_Legal(TarCom)) {
   2466 				Status = RETURN_TO_BASE;
   2467 			} else {
   2468 				Status = PICK_ATTACK_LOCATION;
   2469 			}
   2470 			break;
   2471 
   2472 		/*
   2473 		**	Pick a good location to attack from.
   2474 		*/
   2475 		case PICK_ATTACK_LOCATION:
   2476 			if (!Target_Legal(TarCom)) {
   2477 				Status = RETURN_TO_BASE;
   2478 			} else {
   2479 				Assign_Destination(Good_Fire_Location(TarCom));
   2480 				if (Target_Legal(NavCom)) {
   2481 					Status = TAKE_OFF;
   2482 				} else {
   2483 					Status = RETURN_TO_BASE;
   2484 				}
   2485 			}
   2486 			break;
   2487 
   2488 		/*
   2489 		**	Take off (if necessary).
   2490 		*/
   2491 		case TAKE_OFF:
   2492 			if (!Target_Legal(TarCom)) {
   2493 				Status = RETURN_TO_BASE;
   2494 			} else {
   2495 				if (Process_Take_Off()) {
   2496 					Status = FLY_TO_POSITION;
   2497 
   2498 					/*
   2499 					**	Break off radio contact with the helipad it is taking off from.
   2500 					*/
   2501 					if (In_Radio_Contact() && Map[Coord].Cell_Building() == Contact_With_Whom()) {
   2502 						Transmit_Message(RADIO_OVER_OUT);
   2503 					}
   2504 
   2505 					/*
   2506 					**	Start flying toward the destination by skewing at first.
   2507 					**	As the flight progresses, the body will rotate to face
   2508 					**	the direction of travel.
   2509 					*/
   2510 					int diff = SecondaryFacing.Difference(Direction(NavCom));
   2511 					diff = Bound(diff, -128, 128);
   2512 					PrimaryFacing = DirType((int)SecondaryFacing.Current()+diff);
   2513 				}
   2514 				return(1);
   2515 			}
   2516 			break;
   2517 
   2518 		/*
   2519 		**	Fly to attack location.
   2520 		*/
   2521 		case FLY_TO_POSITION:
   2522 			if (Target_Legal(TarCom)) {
   2523 
   2524 				/*
   2525 				**	If the navcom was cleared mysteriously, then try to pick
   2526 				**	a new attack location. This is a likely event if the player
   2527 				**	clicks on a new target while in flight to an existing target.
   2528 				*/
   2529 				if (!Target_Legal(NavCom)) {
   2530 					Status = PICK_ATTACK_LOCATION;
   2531 					return(1);
   2532 				}
   2533 
   2534 				int distance = Process_Fly_To(true, NavCom);
   2535 
   2536 				if (distance < 0x0200) {
   2537 					SecondaryFacing.Set_Desired(Direction(TarCom));
   2538 
   2539 					if (distance < 0x0010) {
   2540 						Status = FIRE_AT_TARGET;
   2541 						Assign_Destination(TARGET_NONE);
   2542 					}
   2543 				} else {
   2544 					SecondaryFacing.Set_Desired(::Direction(Fire_Coord(0), As_Coord(NavCom)));
   2545 					return(1);
   2546 				}
   2547 			} else {
   2548 				Status = RETURN_TO_BASE;
   2549 			}
   2550 			return(1);
   2551 
   2552 		/*
   2553 		**	Fire at the target.
   2554 		*/
   2555 		case FIRE_AT_TARGET:
   2556 			if (!Target_Legal(TarCom)) {
   2557 				Status = RETURN_TO_BASE;
   2558 				return(1);
   2559 			}
   2560 
   2561 			PrimaryFacing.Set_Desired(Direction(TarCom));
   2562 			SecondaryFacing.Set_Desired(Direction(TarCom));
   2563 			switch (Can_Fire(TarCom, 0)) {
   2564 				case FIRE_CLOAKED:
   2565 					Do_Uncloak();
   2566 					break;
   2567 
   2568 				case FIRE_OK:
   2569 					Fire_At(TarCom, 0);
   2570 					Map[::As_Cell(TarCom)].Incoming(Coord, true);
   2571 					Status = FIRE_AT_TARGET2;
   2572 					break;
   2573 
   2574 				case FIRE_REARM:
   2575 				case FIRE_FACING:
   2576 					break;
   2577 
   2578 				default:
   2579 					if (!Ammo) {
   2580 						Status = RETURN_TO_BASE;
   2581 					} else {
   2582 						Status = FIRE_AT_TARGET2;
   2583 					}
   2584 					break;
   2585 			}
   2586 			return(1);
   2587 
   2588 		/*
   2589 		**	Fire at the target.
   2590 		*/
   2591 		case FIRE_AT_TARGET2:
   2592 			if (!Target_Legal(TarCom)) {
   2593 				Status = RETURN_TO_BASE;
   2594 				return(1);
   2595 			}
   2596 
   2597 			PrimaryFacing.Set_Desired(Direction(TarCom));
   2598 			SecondaryFacing.Set_Desired(Direction(TarCom));
   2599 			switch (Can_Fire(TarCom, 0)) {
   2600 				case FIRE_CLOAKED:
   2601 					Do_Uncloak();
   2602 					break;
   2603 
   2604 				case FIRE_REARM:
   2605 					break;
   2606 
   2607 				case FIRE_OK:
   2608 					Fire_At(TarCom, 0);
   2609 					Map[::As_Cell(TarCom)].Incoming(Coord, true);
   2610 
   2611 					if (Ammo) {
   2612 						Status = Rule.IsCurleyShuffle ? PICK_ATTACK_LOCATION : FIRE_AT_TARGET;
   2613 					} else {
   2614 						Status = RETURN_TO_BASE;
   2615 					}
   2616 					break;
   2617 
   2618 				default:
   2619 					if (!Ammo) {
   2620 						Status = RETURN_TO_BASE;
   2621 					} else {
   2622 						if (!In_Range(TarCom)) {
   2623 							Status = PICK_ATTACK_LOCATION;
   2624 						} else {
   2625 							Status = Rule.IsCurleyShuffle ? PICK_ATTACK_LOCATION : FIRE_AT_TARGET;
   2626 						}
   2627 					}
   2628 					break;
   2629 			}
   2630 			break;
   2631 
   2632 		/*
   2633 		**	Fly back to landing spot.
   2634 		*/
   2635 		case RETURN_TO_BASE:
   2636 			/*
   2637 			**	Break off of firing at the target if there is no more
   2638 			**	point in attacking it this mission. The player will
   2639 			**	reassign a target for the next mission.
   2640 			*/
   2641 			if (!Ammo && (IsALoaner || House->IsHuman)) {
   2642 				Assign_Target(TARGET_NONE);
   2643 			}
   2644 			Assign_Destination(TARGET_NONE);
   2645 			Enter_Idle_Mode();
   2646 			break;
   2647 
   2648 		default:
   2649 			break;
   2650 	}
   2651 
   2652 	return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
   2653 }
   2654 
   2655 
   2656 /***********************************************************************************************
   2657  * AircraftClass::New_LZ -- Find a good landing zone.                                          *
   2658  *                                                                                             *
   2659  *    Use this routine to locate a good landing zone that is nearby the location specified.    *
   2660  *    By using this routine it is possible to assign the same landing zone to several          *
   2661  *    aircraft and they will land nearby without conflict.                                     *
   2662  *                                                                                             *
   2663  * INPUT:   oldlz -- Target value of desired landing zone (usually a cell target value).       *
   2664  *                                                                                             *
   2665  * OUTPUT:  Returns with the new good landing zone. It might be the same value passed in.      *
   2666  *                                                                                             *
   2667  * WARNINGS:   The landing zone might be a goodly distance away from the ideal if there is     *
   2668  *             extensive blocking terrain in the vicinity.                                     *
   2669  *                                                                                             *
   2670  * HISTORY:                                                                                    *
   2671  *   06/19/1995 JLB : Created.                                                                 *
   2672  *=============================================================================================*/
   2673 TARGET AircraftClass::New_LZ(TARGET oldlz) const
   2674 {
   2675 	assert(Aircraft.ID(this) == ID);
   2676 	assert(IsActive);
   2677 
   2678 	if (Target_Legal(oldlz) && (!Is_LZ_Clear(oldlz) || !Cell_Seems_Ok(As_Cell(oldlz)))) {
   2679 		COORDINATE coord = As_Coord(oldlz);
   2680 
   2681 		/*
   2682 		**	Scan outward in a series of concentric rings up to certain distance
   2683 		**	in cells.
   2684 		*/
   2685 		for (int radius = 0; radius < Rule.LZScanRadius / CELL_LEPTON_W; radius++) {
   2686 			FacingType modifier = Random_Pick(FACING_N, FACING_NW);
   2687 			CELL lastcell = -1;
   2688 
   2689 			/*
   2690 			**	Perform a radius scan out from the original center location. Try to
   2691 			**	find a cell that is allowed to be a legal LZ.
   2692 			*/
   2693 			for (FacingType facing = FACING_N; facing < FACING_COUNT; facing++) {
   2694 				CELL newcell = Coord_Cell(Coord_Move(coord, Facing_Dir(facing+modifier), radius * ICON_LEPTON_W));
   2695 				if (Map.In_Radar(newcell)) {
   2696 					TARGET newtarget = ::As_Target(newcell);
   2697 
   2698 					if (newcell != lastcell && Is_LZ_Clear(newtarget) && Cell_Seems_Ok(newcell)) {
   2699 						return(newtarget);
   2700 					}
   2701 					lastcell = newcell;
   2702 				}
   2703 			}
   2704 		}
   2705 	}
   2706 	return(oldlz);
   2707 }
   2708 
   2709 
   2710 /***********************************************************************************************
   2711  * AircraftClass::Receive_Message -- Handles receipt of radio messages.                        *
   2712  *                                                                                             *
   2713  *    This routine receives all radio messages directed at this aircraft. It is used to handle *
   2714  *    all inter-object coordination. Typically, this would be for transport helicopters and    *
   2715  *    other complex landing operations required of helicopters.                                *
   2716  *                                                                                             *
   2717  * INPUT:   from     -- The source of this radio message.                                      *
   2718  *                                                                                             *
   2719  *          message  -- The message itself.                                                    *
   2720  *                                                                                             *
   2721  *          param    -- An optional parameter that may be used to transfer additional          *
   2722  *                      data.                                                                  *
   2723  *                                                                                             *
   2724  * OUTPUT:  Returns with the radio response from the aircraft.                                 *
   2725  *                                                                                             *
   2726  * WARNINGS:   Some radio messages are handled by the base classes.                            *
   2727  *                                                                                             *
   2728  * HISTORY:                                                                                    *
   2729  *   06/19/1995 JLB : Created.                                                                 *
   2730  *=============================================================================================*/
   2731 RadioMessageType AircraftClass::Receive_Message(RadioClass * from, RadioMessageType message, long & param)
   2732 {
   2733 	assert(Aircraft.ID(this) == ID);
   2734 	assert(IsActive);
   2735 
   2736 	switch (message) {
   2737 
   2738 		case RADIO_PREPARED:
   2739 			if (Target_Legal(TarCom)) return(RADIO_NEGATIVE);
   2740 			if ((Height == 0 && Ammo == Class->MaxAmmo) || (Height > 0 && Ammo > 0)) return(RADIO_ROGER);
   2741 			return(RADIO_NEGATIVE);
   2742 
   2743 		/*
   2744 		**	Something disastrous has happened to the object in contact with. Fall back
   2745 		**	and regroup. This means that any landing process is immediately aborted.
   2746 		*/
   2747 		case RADIO_RUN_AWAY:
   2748 			if (IsLanding) {
   2749 				IsLanding = false;
   2750 				IsTakingOff = true;
   2751 			}
   2752 			if (Class->IsFixedWing) {
   2753 				Assign_Destination(Good_LZ());
   2754 				if (!Target_Legal(NavCom)) {
   2755 					Assign_Mission(MISSION_RETREAT);
   2756 				} else {
   2757 					Assign_Mission(MISSION_MOVE);
   2758 				}
   2759 			} else {
   2760 				Scatter(0, true);
   2761 			}
   2762 			break;
   2763 
   2764 		/*
   2765 		**	The ground control requests that this specified landing spot be used.
   2766 		*/
   2767 		case RADIO_MOVE_HERE:
   2768 			FootClass::Receive_Message(from, message, param);
   2769 			if (Is_Target_Building(param)) {
   2770 				if (Transmit_Message(RADIO_CAN_LOAD, As_Techno(param)) != RADIO_ROGER) {
   2771 					return(RADIO_NEGATIVE);
   2772 				}
   2773 				Assign_Mission(MISSION_ENTER);
   2774 				Assign_Destination((TARGET)param);
   2775 			} else {
   2776 				Assign_Mission(MISSION_MOVE);
   2777 				Assign_Destination((TARGET)param);
   2778 			}
   2779 			Commence();
   2780 			return(RADIO_ROGER);
   2781 
   2782 		/*
   2783 		**	Ground control is requesting if the aircraft requires navigation direction.
   2784 		*/
   2785 		case RADIO_NEED_TO_MOVE:
   2786 			FootClass::Receive_Message(from, message, param);
   2787 			if (!Target_Legal(NavCom) && !IsTakingOff && !IsLanding) {
   2788 				return(RADIO_ROGER);
   2789 			}
   2790 			return(RADIO_NEGATIVE);
   2791 
   2792 		/*
   2793 		**	This message is sent by the passenger when it determines that it has
   2794 		**	entered the transport.
   2795 		*/
   2796 		case RADIO_IM_IN:
   2797 			if (How_Many() == Class->Max_Passengers()) {
   2798 				Close_Door(5, 4);
   2799 			}
   2800 
   2801 			/*
   2802 			**	If a civilian has entered the transport, then the transport will immediately
   2803 			**	fly off the map.
   2804 			*/
   2805 			if (_Counts_As_Civ_Evac(from)) {
   2806 				Assign_Mission(MISSION_RETREAT);
   2807 			}
   2808 			return(RADIO_ATTACH);
   2809 
   2810 		/*
   2811 		**	Docking maintenance message received. Check to see if new orders should be given
   2812 		**	to the impatient unit.
   2813 		*/
   2814 		case RADIO_DOCKING:
   2815 			if (Class->Max_Passengers() > 0 && How_Many() < Class->Max_Passengers()) {
   2816 				FootClass::Receive_Message(from, message, param);
   2817 
   2818 				if (!IsTethered && !IsLanding && !IsTakingOff && Height == 0) {
   2819 
   2820 					Open_Door(5, 4);
   2821 
   2822 					/*
   2823 					**	If the potential passenger needs someplace to go, then figure out a good
   2824 					**	spot and tell it to go.
   2825 					*/
   2826 					if (Transmit_Message(RADIO_NEED_TO_MOVE, from) == RADIO_ROGER) {
   2827 						CELL cell;
   2828 						/*DirType dir =*/ Desired_Load_Dir(from, cell);
   2829 
   2830 						/*
   2831 						**	If no adjacent free cells are detected, then passenger loading
   2832 						**	cannot occur. Break radio contact.
   2833 						*/
   2834 						if (cell == 0) {
   2835 							Transmit_Message(RADIO_OVER_OUT, from);
   2836 						} else {
   2837 							param = (long)::As_Target(cell);
   2838 
   2839 							/*
   2840 							**	Tell the potential passenger where it should go. If the passenger is
   2841 							**	already at the staging location, then tell it to move onto the transport
   2842 							**	directly.
   2843 							*/
   2844 							if (Transmit_Message(RADIO_MOVE_HERE, param, from) == RADIO_YEA_NOW_WHAT) {
   2845 								param = (long)As_Target();
   2846 								Transmit_Message(RADIO_TETHER);
   2847 								if (Transmit_Message(RADIO_MOVE_HERE, param, from) != RADIO_ROGER) {
   2848 									Transmit_Message(RADIO_OVER_OUT, from);
   2849 								} else {
   2850 									Contact_With_Whom()->Unselect();
   2851 								}
   2852 							}
   2853 						}
   2854 					}
   2855 				}
   2856 				return(RADIO_ROGER);
   2857 			}
   2858 			break;
   2859 
   2860 		/*
   2861 		**	Asks if the passenger can load on this transport.
   2862 		*/
   2863 		case RADIO_CAN_LOAD:
   2864 			if (Class->Max_Passengers() == 0 || from == NULL || !House->Is_Ally(from->Owner()) || Height > 0) return(RADIO_STATIC);
   2865 			if (/*!In_Radio_Contact() &&*/ How_Many() < Class->Max_Passengers()) {
   2866 				return(RADIO_ROGER);
   2867 			}
   2868 			return(RADIO_NEGATIVE);
   2869 
   2870 		default:
   2871 			break;
   2872 	}
   2873 
   2874 	/*
   2875 	**	Let the base class take over processing this message.
   2876 	*/
   2877 	return(FootClass::Receive_Message(from, message, param));
   2878 }
   2879 
   2880 
   2881 /***********************************************************************************************
   2882  * AircraftClass::Desired_Load_Dir -- Determines where passengers should line up.              *
   2883  *                                                                                             *
   2884  *    This routine is used by the transport helicopter to determine the location where the     *
   2885  *    infantry passengers should line up before loading.                                       *
   2886  *                                                                                             *
   2887  * INPUT:   object   -- The object that is trying to load up on this transport.                *
   2888  *                                                                                             *
   2889  *                   -- Reference to the cell that the passengers should move to before the    *
   2890  *                      actual load process may begin.                                         *
   2891  *                                                                                             *
   2892  * OUTPUT:  Returns with the direction that the helicopter should face for the load operation. *
   2893  *                                                                                             *
   2894  * WARNINGS:   none                                                                            *
   2895  *                                                                                             *
   2896  * HISTORY:                                                                                    *
   2897  *   06/12/1995 JLB : Created.                                                                 *
   2898  *   07/30/1995 JLB : Revamped to scan all adjacent cells.                                     *
   2899  *=============================================================================================*/
   2900 DirType AircraftClass::Desired_Load_Dir(ObjectClass * object, CELL & moveto) const
   2901 {
   2902 	assert(Aircraft.ID(this) == ID);
   2903 	assert(IsActive);
   2904 
   2905 	CELL center = Coord_Cell(Center_Coord());
   2906 	for (int sweep = FACING_N; sweep < FACING_S; sweep++) {
   2907 		moveto = Adjacent_Cell(center, FacingType(FACING_S+sweep));
   2908 		if (Map.In_Radar(moveto) && (Coord_Cell(object->Center_Coord()) == moveto || Map[moveto].Is_Clear_To_Move(SPEED_FOOT, false, false))) return(DIR_N);
   2909 
   2910 		moveto = Adjacent_Cell(center, FacingType(FACING_S-sweep));
   2911 		if (Map.In_Radar(moveto) && (Coord_Cell(object->Center_Coord()) == moveto || Map[moveto].Is_Clear_To_Move(SPEED_FOOT, false, false))) return(DIR_N);
   2912 	}
   2913 	return(DIR_N);
   2914 }
   2915 
   2916 
   2917 /***********************************************************************************************
   2918  * AircraftClass::Process_Take_Off -- State machine support for taking off.                    *
   2919  *                                                                                             *
   2920  *    This routine is used by the main game state machine processor. This utility routine      *
   2921  *    handles a helicopter as it transitions from landed to flying state.                      *
   2922  *                                                                                             *
   2923  * INPUT:   none                                                                               *
   2924  *                                                                                             *
   2925  * OUTPUT:  Has the helicopter reached flight level now?                                       *
   2926  *                                                                                             *
   2927  * WARNINGS:   none                                                                            *
   2928  *                                                                                             *
   2929  * HISTORY:                                                                                    *
   2930  *   06/12/1995 JLB : Created.                                                                 *
   2931  *=============================================================================================*/
   2932 bool AircraftClass::Process_Take_Off(void)
   2933 {
   2934 	assert(Aircraft.ID(this) == ID);
   2935 	assert(IsActive);
   2936 
   2937 	IsLanding = false;
   2938 	IsTakingOff = true;
   2939 
   2940 	if (Class->IsFixedWing)  {
   2941 		Set_Speed(0xFF);
   2942 		if (Height == FLIGHT_LEVEL)  {
   2943 			return(true);
   2944 		}
   2945 
   2946 	} else {
   2947 
   2948 		switch (Height) {
   2949 			case 0:
   2950 				Close_Door(5, 4);
   2951 				PrimaryFacing = SecondaryFacing;
   2952 				break;
   2953 
   2954 			case FLIGHT_LEVEL/2:
   2955 				PrimaryFacing.Set_Desired(Direction(NavCom));
   2956 				break;
   2957 
   2958 			case FLIGHT_LEVEL-(FLIGHT_LEVEL/3):
   2959 				SecondaryFacing.Set_Desired(PrimaryFacing.Desired());
   2960 				Set_Speed(0x20);
   2961 				break;
   2962 
   2963 			case FLIGHT_LEVEL-(FLIGHT_LEVEL/5):
   2964 				Set_Speed(0x40);
   2965 				break;
   2966 
   2967 			case FLIGHT_LEVEL:
   2968 				Set_Speed(0xFF);
   2969 				IsTakingOff = false;
   2970 				return(true);
   2971 
   2972 			default:
   2973 				break;
   2974 		}
   2975 	}
   2976 	return(false);
   2977 }
   2978 
   2979 
   2980 /***********************************************************************************************
   2981  * AircraftClass::Process_Landing -- Landing process state machine handler.                    *
   2982  *                                                                                             *
   2983  *    This is a support routine that is called by the main state machine routines. This        *
   2984  *    routine is responsible for handling the helicopter as it transitions from flight to      *
   2985  *    landing.                                                                                 *
   2986  *                                                                                             *
   2987  * INPUT:   none                                                                               *
   2988  *                                                                                             *
   2989  * OUTPUT:  Has the helicopter completely landed now?                                          *
   2990  *                                                                                             *
   2991  * WARNINGS:   none                                                                            *
   2992  *                                                                                             *
   2993  * HISTORY:                                                                                    *
   2994  *   06/12/1995 JLB : Created.                                                                 *
   2995  *   03/04/1996 JLB : Handles fixed wing aircraft.                                             *
   2996  *=============================================================================================*/
   2997 bool AircraftClass::Process_Landing(void)
   2998 {
   2999 	assert(Aircraft.ID(this) == ID);
   3000 	assert(IsActive);
   3001 
   3002 	IsTakingOff = false;
   3003 	IsLanding = true;
   3004 
   3005 	if (Class->IsFixedWing)  {
   3006 		int distance = Distance(NavCom);
   3007 
   3008 		if (distance > 0x0100)  {
   3009 			SecondaryFacing.Set_Desired(::Direction(Fire_Coord(0), As_Coord(NavCom)));
   3010 		}
   3011 
   3012 		switch (Height) {
   3013 			case 0:
   3014 				Set_Speed(0);
   3015 				IsLanding = false;
   3016 				return(true);
   3017 
   3018 			default:
   3019 //				if (distance*2 > Class->LandingSpeed) {
   3020 //					Set_Speed(Class->LandingSpeed);
   3021 //				} else {
   3022 //					Set_Speed(distance/2);
   3023 //				}
   3024 
   3025 				Set_Speed(Class->LandingSpeed / House->AirspeedBias);
   3026 				break;
   3027 		}
   3028 
   3029 	} else {
   3030 		switch (Height) {
   3031 			case 0:
   3032 				IsLanding = false;
   3033 				return(true);
   3034 
   3035 			case FLIGHT_LEVEL/2:
   3036 				Set_Speed(0);
   3037 				break;
   3038 
   3039 			case FLIGHT_LEVEL:
   3040 				break;
   3041 
   3042 			default:
   3043 				break;
   3044 		}
   3045 	}
   3046 	return(false);
   3047 }
   3048 
   3049 
   3050 /***********************************************************************************************
   3051  * AircraftClass::Can_Enter_Cell -- Determines if the aircraft can land at this location.      *
   3052  *                                                                                             *
   3053  *    This routine is used when the passability of a cell needs to be determined. This is      *
   3054  *    necessary when scanning for a location that the aircraft can land.                       *
   3055  *                                                                                             *
   3056  * INPUT:   cell  -- The cell location to check for landing.                                   *
   3057  *                                                                                             *
   3058  * OUTPUT:  Returns a value indicating if the cell is a legal landing spot or not.             *
   3059  *                                                                                             *
   3060  * WARNINGS:   none                                                                            *
   3061  *                                                                                             *
   3062  * HISTORY:                                                                                    *
   3063  *   06/12/1995 JLB : Created.                                                                 *
   3064  *=============================================================================================*/
   3065 MoveType AircraftClass::Can_Enter_Cell(CELL cell, FacingType ) const
   3066 {
   3067 	assert(Aircraft.ID(this) == ID);
   3068 	assert(IsActive);
   3069 
   3070 	if (!Map.In_Radar(cell)) return(MOVE_NO);
   3071 
   3072 	CellClass * cellptr = &Map[cell];
   3073 
   3074 	ObjectClass const * occupier = cellptr->Cell_Occupier();
   3075 
   3076 	if (occupier == NULL ||
   3077 		!occupier->Is_Techno() ||
   3078 		((TechnoClass *)occupier)->House->Is_Ally(House) ||
   3079 		(!((TechnoClass *)occupier)->Is_Cloaked(this) &&
   3080 			(ScenarioInit == 0 && (occupier->What_Am_I() != RTTI_BUILDING || !((BuildingClass*)occupier)->Class->IsInvisible)) )
   3081 		) {
   3082 
   3083 		if (!cellptr->Is_Clear_To_Move(SPEED_TRACK, false, false)) return(MOVE_NO);
   3084 	}
   3085 
   3086 	if (Session.Type == GAME_NORMAL && IsOwnedByPlayer && !cellptr->IsMapped) {
   3087 		return(MOVE_NO);
   3088 	}
   3089 
   3090 	return(MOVE_OK);
   3091 }
   3092 
   3093 
   3094 /***********************************************************************************************
   3095  * AircraftClass::Good_Fire_Location -- Searches for and finds a good spot to fire from.       *
   3096  *                                                                                             *
   3097  *    Given the specified target, this routine will locate a good spot for the aircraft to     *
   3098  *    fire at the target.                                                                      *
   3099  *                                                                                             *
   3100  * INPUT:   target   -- The target that is desired to be attacked.                             *
   3101  *                                                                                             *
   3102  * OUTPUT:  Returns with the target location of the place that firing should be made from.     *
   3103  *                                                                                             *
   3104  * WARNINGS:   none                                                                            *
   3105  *                                                                                             *
   3106  * HISTORY:                                                                                    *
   3107  *   06/12/1995 JLB : Created.                                                                 *
   3108  *   06/14/1995 JLB : Finer resolution on ring scan.                                           *
   3109  *   11/02/1996 JLB : Bias fire position to get closer to moving objects.                      *
   3110  *=============================================================================================*/
   3111 TARGET AircraftClass::Good_Fire_Location(TARGET target) const
   3112 {
   3113 	assert(Aircraft.ID(this) == ID);
   3114 	assert(IsActive);
   3115 
   3116 	if (Target_Legal(target)) {
   3117 		int range = Weapon_Range(0);
   3118 		COORDINATE tcoord = As_Coord(target);
   3119 		CELL bestcell = 0;
   3120 		CELL best2cell = 0;
   3121 		int bestval = -1;
   3122 		int best2val = -1;
   3123 
   3124 		/*
   3125 		**	Try to get closer to a target that is moving.
   3126 		*/
   3127 		COORDINATE altcoord = 0;
   3128 		if (Is_Target_Object(target) && As_Object(target)->Is_Foot()) {
   3129 			TARGET alttarg = ((FootClass *)As_Object(target))->NavCom;
   3130 			if (Target_Legal(alttarg)) {
   3131 				altcoord = As_Coord(alttarg);
   3132 			}
   3133 		}
   3134 
   3135 		for (int r = range-0x0100; r > 0x0100; r -= 0x0100) {
   3136 			for (int face = 0; face < 255; face += 16) {
   3137 				COORDINATE newcoord = Coord_Move(tcoord, (DirType)face, r);
   3138 				CELL newcell = Coord_Cell(newcoord);
   3139 				CELL actualcell = Coord_Cell(Coord_Sub(newcoord, XY_Coord(0, FLIGHT_LEVEL)));
   3140 
   3141 				//using function for IsVisible so we have different results for different players - JAS 2019/09/30
   3142 				if (Map.In_Radar(actualcell) && (Session.Type != GAME_NORMAL || Map[newcell].Is_Visible(PlayerPtr)) && Cell_Seems_Ok(newcell, true)) {
   3143 					int dist;
   3144 					if (altcoord != 0) {
   3145 						dist = ::Distance(newcoord, altcoord);
   3146 					} else {
   3147 						dist = Distance(newcoord);
   3148 					}
   3149 					if (bestval == -1 || dist < bestval) {
   3150 						best2val = bestval;
   3151 						best2cell = bestcell;
   3152 						bestval = dist;
   3153 						bestcell = newcell;
   3154 					}
   3155 				}
   3156 			}
   3157 			if (bestval != -1) break;
   3158 		}
   3159 
   3160 		if (best2val == -1) {
   3161 			best2cell = bestcell;
   3162 		}
   3163 
   3164 		/*
   3165 		**	If it found a good firing location, then return this location as
   3166 		**	a target value.
   3167 		*/
   3168 		if (bestval != -1) {
   3169 			if (Percent_Chance(50)) {
   3170 				return(::As_Target(bestcell));
   3171 			} else {
   3172 				return(::As_Target(best2cell));
   3173 			}
   3174 		}
   3175 	}
   3176 	return(TARGET_NONE);
   3177 }
   3178 
   3179 
   3180 /***********************************************************************************************
   3181  * AircraftClass::Cell_Seems_Ok -- Checks to see if a cell is good to enter.                   *
   3182  *                                                                                             *
   3183  *    This routine examines the navigation computers of other aircraft in order to see if the  *
   3184  *    specified cell is safe to fly to. The intent of this routine is to avoid unnecessary     *
   3185  *    mid-air collisions.                                                                      *
   3186  *                                                                                             *
   3187  * INPUT:   cell     -- The cell to examine for clear airspace.                                *
   3188  *                                                                                             *
   3189  *          strict   -- Should the scan consider the aircraft, that is making this check, a    *
   3190  *                      blocking aircraft. Typically, the aircraft itself is not considered    *
   3191  *                      a blockage -- an aircraft can always exist where it is currently       *
   3192  *                      located. A strict check is useful for helicopters that need to move    *
   3193  *                      around at the slightest provocation.                                   *
   3194  *                                                                                             *
   3195  * OUTPUT:  Is the specified cell free from airspace conflicts?                                *
   3196  *                                                                                             *
   3197  * WARNINGS:   none                                                                            *
   3198  *                                                                                             *
   3199  * HISTORY:                                                                                    *
   3200  *   06/19/1995 JLB : Created.                                                                 *
   3201  *=============================================================================================*/
   3202 bool AircraftClass::Cell_Seems_Ok(CELL cell, bool strict) const
   3203 {
   3204 	assert(Aircraft.ID(this) == ID);
   3205 	assert(IsActive);
   3206 
   3207 	/*
   3208 	**	Make sure that no other aircraft are heading to the selected location. If they
   3209 	**	are, then don't consider the location as valid.
   3210 	*/
   3211 	TARGET astarget = ::As_Target(cell);
   3212 	for (int index = 0; index < Aircraft.Count(); index++) {
   3213 		AircraftClass * air = Aircraft.Ptr(index);
   3214 		if (air && (strict || air != this) && !air->IsInLimbo) {
   3215 			if (Coord_Cell(air->Coord) == cell || air->NavCom == astarget) {
   3216 				return(false);
   3217 			}
   3218 		}
   3219 	}
   3220 	return(true);
   3221 }
   3222 
   3223 
   3224 /***********************************************************************************************
   3225  * AircraftClass::Pip_Count -- Returns the number of "objects" in aircraft.                    *
   3226  *                                                                                             *
   3227  *    This routine is used by the render logic to draw the little container "pips". This       *
   3228  *    corresponds to the number of passengers for a transport helicopter or the number of      *
   3229  *    shots remaining for an attack helicopter.                                                *
   3230  *                                                                                             *
   3231  * INPUT:   none                                                                               *
   3232  *                                                                                             *
   3233  * OUTPUT:  Returns with the number of "pips" to render on the aircraft.                       *
   3234  *                                                                                             *
   3235  * WARNINGS:   none                                                                            *
   3236  *                                                                                             *
   3237  * HISTORY:                                                                                    *
   3238  *   06/11/1995 JLB : Created.                                                                 *
   3239  *=============================================================================================*/
   3240 int AircraftClass::Pip_Count(void) const
   3241 {
   3242 	assert(Aircraft.ID(this) == ID);
   3243 	assert(IsActive);
   3244 
   3245 	int retval = 0;
   3246 
   3247 	bool carrying_passengers = (Class->Max_Passengers() > 0) && ((*this != AIRCRAFT_BADGER) || (Mission != MISSION_HUNT));
   3248 	if (carrying_passengers) {
   3249 		retval = How_Many();
   3250 	} else {
   3251 		if (Ammo) {
   3252 			retval = Class->Max_Pips() * fixed(Ammo, Class->MaxAmmo);
   3253 			if (!retval) retval = 1;
   3254 		}
   3255 	}
   3256 	return(retval);
   3257 }
   3258 
   3259 
   3260 /***********************************************************************************************
   3261  * AircraftClass::Mission_Enter -- Control aircraft to fly to the helipad or repair center.    *
   3262  *                                                                                             *
   3263  *    This routine is used when the aircraft needs to fly for either rearming or repairing.    *
   3264  *    It tries to establish contact with the support building. Once contact is established     *
   3265  *    the ground controller takes care of commanding the aircraft.                             *
   3266  *                                                                                             *
   3267  * INPUT:   none                                                                               *
   3268  *                                                                                             *
   3269  * OUTPUT:  Returns with the delay before this routine should be called again.                 *
   3270  *                                                                                             *
   3271  * WARNINGS:   none                                                                            *
   3272  *                                                                                             *
   3273  * HISTORY:                                                                                    *
   3274  *   06/12/1995 JLB : Created.                                                                 *
   3275  *   07/04/1995 JLB : Ground controller gives orders.                                          *
   3276  *=============================================================================================*/
   3277 int AircraftClass::Mission_Enter(void)
   3278 {
   3279 	assert(Aircraft.ID(this) == ID);
   3280 	assert(IsActive);
   3281 
   3282 	enum {
   3283 		INITIAL,
   3284 		TAKEOFF,
   3285 		ALTITUDE,
   3286 		STACK,
   3287 		DOWNWIND,
   3288 		CROSSWIND,
   3289 		TRAVEL,
   3290 		LANDING
   3291 	};
   3292 
   3293 	/*
   3294 	**	Verify that it has a valid NavCom. If it doesn't then request one from the
   3295 	**	building this building is trying to land upon. If that fails, then enter
   3296 	**	idle mode.
   3297 	*/
   3298 	if (!Target_Legal(NavCom) && In_Which_Layer() != LAYER_GROUND) {
   3299 		if (Transmit_Message(RADIO_DOCKING) != RADIO_ROGER) {
   3300 			Enter_Idle_Mode();
   3301 			return(1);
   3302 		}
   3303 	}
   3304 
   3305 	switch (Status) {
   3306 		case INITIAL:
   3307 			if (Height < FLIGHT_LEVEL || IsLanding) {
   3308 				Status = TAKEOFF;
   3309 			} else {
   3310 				Status = ALTITUDE;
   3311 			}
   3312 			break;
   3313 
   3314 		case TAKEOFF:
   3315 			if (Process_Take_Off()) {
   3316 				/*
   3317 				**	After takeoff is complete, break radio contact with any helipad that this
   3318 				**	helicopter is taking off from.
   3319 				*/
   3320 				if (In_Radio_Contact() && Map[Coord].Cell_Building() == Contact_With_Whom()) {
   3321 					Transmit_Message(RADIO_OVER_OUT);
   3322 				}
   3323 				Status = ALTITUDE;
   3324 			}
   3325 			break;
   3326 
   3327 		case ALTITUDE:
   3328 			/*
   3329 			**	Establish radio contact with the building this helicopter is trying
   3330 			**	to land at.
   3331 			*/
   3332 			if (In_Radio_Contact()) {
   3333 				if (!Target_Legal(NavCom)) {
   3334 					Transmit_Message(RADIO_DOCKING);
   3335 					if (!Target_Legal(NavCom)) {
   3336 						Enter_Idle_Mode();
   3337 						return(1);
   3338 					}
   3339 				}
   3340 				Status = STACK;
   3341 			} else {
   3342 				TechnoClass * tech = As_Techno(NavCom);
   3343 				if (tech && Transmit_Message(RADIO_CAN_LOAD, tech) == RADIO_ROGER) {
   3344 					Transmit_Message(RADIO_HELLO, tech);
   3345 					Transmit_Message(RADIO_DOCKING);
   3346 					Status = STACK;
   3347 				} else {
   3348 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3349 					if (tech->What_Am_I() != RTTI_VESSEL) {
   3350 						Assign_Destination(TARGET_NONE);
   3351 						Enter_Idle_Mode();
   3352 					}
   3353 #else
   3354 					Assign_Destination(TARGET_NONE);
   3355 					Enter_Idle_Mode();
   3356 #endif
   3357 				}
   3358 			}
   3359 			break;
   3360 
   3361 		case STACK:
   3362 			if (Class->IsFixedWing)  {
   3363 				int distance;
   3364 				TARGET togo;
   3365 
   3366 				BuildingClass const * building = As_Building(NavCom);
   3367 				if (building)  {
   3368 					togo = ::As_Target(building->Check_Point(CHECK_STACK));
   3369 				} else {
   3370 					togo = NavCom;
   3371 				}
   3372 
   3373 				distance = Process_Fly_To(true, togo);
   3374 				if (distance < 0x0080)  {
   3375 					Status = DOWNWIND;
   3376 				}
   3377 			} else {
   3378 				Status = DOWNWIND;
   3379 			}
   3380 			break;
   3381 
   3382 		case DOWNWIND:
   3383 			if (Class->IsFixedWing)  {
   3384 				int distance;
   3385 				TARGET togo;
   3386 
   3387 				Set_Speed(200);
   3388 				BuildingClass const * building = As_Building(NavCom);
   3389 				if (building)  {
   3390 					togo = ::As_Target(building->Check_Point(CHECK_DOWNWIND));
   3391 				} else {
   3392 					togo = NavCom;
   3393 				}
   3394 
   3395 				distance = Process_Fly_To(true, togo);
   3396 				if (distance < 0x0080)  {
   3397 					Status = CROSSWIND;
   3398 				}
   3399 			} else {
   3400 				Status = CROSSWIND;
   3401 			}
   3402 			break;
   3403 
   3404 		case CROSSWIND:
   3405 			if (Class->IsFixedWing)  {
   3406 				int distance;
   3407 				TARGET togo;
   3408 
   3409 				Set_Speed(140);
   3410 				BuildingClass const * building = As_Building(NavCom);
   3411 				if (building)  {
   3412 					togo = ::As_Target(building->Check_Point(CHECK_CROSSWIND));
   3413 				} else {
   3414 					togo = NavCom;
   3415 				}
   3416 
   3417 				distance = Process_Fly_To(true, togo);
   3418 				if (distance < 0x0080)  {
   3419 					Status = TRAVEL;
   3420 				}
   3421 			} else {
   3422 				Status = TRAVEL;
   3423 			}
   3424 			break;
   3425 
   3426 		case TRAVEL:
   3427 			Transmit_Message(RADIO_DOCKING);
   3428 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3429 			if (!In_Radio_Contact() && !Is_Target_Vessel(NavCom)) {
   3430 #else
   3431 			if (!In_Radio_Contact()) {
   3432 #endif
   3433 				Assign_Destination(TARGET_NONE);
   3434 				Enter_Idle_Mode();
   3435 			} else {
   3436 				int distance = Process_Fly_To(true, NavCom);
   3437 
   3438 				if (Class->IsFixedWing)  {
   3439 
   3440 					if (distance < 0x0400)  {
   3441 						Status = LANDING;
   3442 					}
   3443 					return(1);
   3444 
   3445 				} else {
   3446 					if (distance < 0x0080) {
   3447 						if (Target_Legal(TarCom)) {
   3448 							SecondaryFacing.Set_Desired(Direction(TarCom));
   3449 						} else {
   3450 							SecondaryFacing.Set_Desired(Pose_Dir());
   3451 						}
   3452 
   3453 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3454  if (Is_Target_Vessel(NavCom) && !In_Radio_Contact()) {
   3455  	Enter_Idle_Mode();
   3456  	break;
   3457  }
   3458 #endif
   3459 						if (distance < 0x0010) {
   3460 							Status = LANDING;
   3461 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3462 	if(Is_Target_Vessel(NavCom) && As_Vessel(NavCom)->NavCom) {
   3463 		Status = TRAVEL;
   3464 	}
   3465 #endif
   3466 
   3467 						}
   3468 						break;
   3469 					} else {
   3470 						SecondaryFacing.Set_Desired(Direction(NavCom));
   3471 //						SecondaryFacing.Set_Desired(::Direction(Fire_Coord(0), As_Coord(NavCom)));
   3472 					}
   3473 				}
   3474 				return(3);
   3475 			}
   3476 			break;
   3477 
   3478 		case LANDING:
   3479 			if (IsTakingOff && !Class->IsFixedWing) {
   3480 				Assign_Destination(TARGET_NONE);
   3481 				Enter_Idle_Mode();
   3482 			}
   3483 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3484 // If we were trying to land on a carrier and it moved, take off again
   3485 	if ( As_Vessel(NavCom) && !In_Radio_Contact()) {
   3486 		Status = INITIAL;
   3487 		break;
   3488 	}
   3489 #endif
   3490 			if (Process_Landing()) {
   3491 				switch (Transmit_Message(RADIO_IM_IN)) {
   3492 					case RADIO_ROGER:
   3493 						Assign_Mission(MISSION_GUARD);
   3494 						break;
   3495 
   3496 					case RADIO_ATTACH:
   3497 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3498 						if(Contact_With_Whom()->What_Am_I() != RTTI_VESSEL) Limbo();
   3499 #else
   3500 						Limbo();
   3501 #endif
   3502 						Contact_With_Whom()->Attach(this);
   3503 						break;
   3504 
   3505 					default:
   3506 						Enter_Idle_Mode();
   3507 				}
   3508 			}
   3509 			break;
   3510 
   3511 		default:
   3512 			break;
   3513 	}
   3514 	return(1);
   3515 }
   3516 
   3517 
   3518 /***********************************************************************************************
   3519  * AircraftClass::Good_LZ -- Locates a good spot to land.                                      *
   3520  *                                                                                             *
   3521  *    This routine is used when helicopters need a place to land, but there are no obvious     *
   3522  *    spots (i.e., helipad) available. It will try to land near a friendly helipad or friendly *
   3523  *    building if there are no helipads anywhere. In the event that there are no friendly      *
   3524  *    buildings anywhere on the map, then just land right where it is flying.                  *
   3525  *                                                                                             *
   3526  * INPUT:   none                                                                               *
   3527  *                                                                                             *
   3528  * OUTPUT:  Returns with the target location where this aircraft should land. This value may   *
   3529  *          not be a clear cell, but the normal landing logic will resolve that problem.       *
   3530  *                                                                                             *
   3531  * WARNINGS:   none                                                                            *
   3532  *                                                                                             *
   3533  * HISTORY:                                                                                    *
   3534  *   06/12/1995 JLB : Created.                                                                 *
   3535  *=============================================================================================*/
   3536 TARGET AircraftClass::Good_LZ(void) const
   3537 {
   3538 	assert(Aircraft.ID(this) == ID);
   3539 	assert(IsActive);
   3540 
   3541 	/*
   3542 	**	Scan through all of the buildings and try to land near
   3543 	**	the helipad (if there is one) or the nearest friendly building.
   3544 	*/
   3545 	CELL bestcell = 0;
   3546 	int bestdist = -1;
   3547 	for (int index = 0; index < Buildings.Count(); index++) {
   3548 		BuildingClass * building = Buildings.Ptr(index);
   3549 
   3550 		if (building && !building->IsInLimbo && building->House == House) {
   3551 			int dist = Distance(building);
   3552 			if (*building == Class->Building) {
   3553 				dist /= 4;
   3554 			}
   3555 			if (bestdist == -1 || dist < bestdist) {
   3556 				bestdist = dist;
   3557 				bestcell = Coord_Cell(building->Center_Coord());
   3558 			}
   3559 		}
   3560 	}
   3561 
   3562 	/*
   3563 	**	Return with the suitable location if one was found.
   3564 	*/
   3565 	if (bestdist != -1) {
   3566 		return(::As_Target(bestcell));
   3567 	}
   3568 
   3569 	/*
   3570 	**	No good location was found. Just try to land here.
   3571 	*/
   3572 	return(::As_Target(Coord_Cell(Coord)));
   3573 }
   3574 
   3575 
   3576 /***********************************************************************************************
   3577  * AircraftClass::Set_Speed -- Sets the speed for the aircraft.                                *
   3578  *                                                                                             *
   3579  *    This routine will set the speed for the aircraft. The speed is specified as a fraction   *
   3580  *    of full speed.                                                                           *
   3581  *                                                                                             *
   3582  * INPUT:   speed -- The fixed point fractional speed setting. 0x00 is stopped, 0xFF is full   *
   3583  *                   speed.                                                                    *
   3584  *                                                                                             *
   3585  * OUTPUT:  none                                                                               *
   3586  *                                                                                             *
   3587  * WARNINGS:   none                                                                            *
   3588  *                                                                                             *
   3589  * HISTORY:                                                                                    *
   3590  *   06/19/1995 JLB : Created.                                                                 *
   3591  *=============================================================================================*/
   3592 void AircraftClass::Set_Speed(int speed)
   3593 {
   3594 	assert(Aircraft.ID(this) == ID);
   3595 	assert(IsActive);
   3596 
   3597 	FootClass::Set_Speed(speed);
   3598 
   3599 	MPHType sp = MPHType(min(Class->MaxSpeed * SpeedBias * House->AirspeedBias, MPH_LIGHT_SPEED));
   3600 	Fly_Speed(speed, sp);
   3601 }
   3602 
   3603 
   3604 /***********************************************************************************************
   3605  * AircraftClass::Fire_Direction -- Determines the direction of fire.                          *
   3606  *                                                                                             *
   3607  *    This routine will determine what direction a projectile would take if it were fired      *
   3608  *    from the aircraft. This is the direction that the aircraft's body is facing.             *
   3609  *                                                                                             *
   3610  * INPUT:   none                                                                               *
   3611  *                                                                                             *
   3612  * OUTPUT:  Returns with the direction of projectile fire.                                     *
   3613  *                                                                                             *
   3614  * WARNINGS:   none                                                                            *
   3615  *                                                                                             *
   3616  * HISTORY:                                                                                    *
   3617  *   06/19/1995 JLB : Created.                                                                 *
   3618  *=============================================================================================*/
   3619 DirType AircraftClass::Fire_Direction(void) const
   3620 {
   3621 	assert(Aircraft.ID(this) == ID);
   3622 	assert(IsActive);
   3623 
   3624 	return(SecondaryFacing.Current());
   3625 }
   3626 
   3627 
   3628 /***********************************************************************************************
   3629  * AircraftClass::~AircraftClass -- Destructor for aircraft object.                            *
   3630  *                                                                                             *
   3631  *    This is the destructor for aircraft. It will limbo the aircraft if it isn't already      *
   3632  *    and also removes the aircraft from any team it may be attached to.                       *
   3633  *                                                                                             *
   3634  * INPUT:   none                                                                               *
   3635  *                                                                                             *
   3636  * OUTPUT:  none                                                                               *
   3637  *                                                                                             *
   3638  * WARNINGS:   none                                                                            *
   3639  *                                                                                             *
   3640  * HISTORY:                                                                                    *
   3641  *   06/24/1995 JLB : Created.                                                                 *
   3642  *=============================================================================================*/
   3643 AircraftClass::~AircraftClass(void)
   3644 {
   3645 	if (GameActive && Class) {
   3646 
   3647 		/*
   3648 		**	Remove this member from any team it may be associated with. This must occur at the
   3649 		**	top most level of the inheritance hierarchy because it may call virtual functions.
   3650 		*/
   3651 		if (Team) {
   3652 			Team->Remove(this);
   3653 			Team = NULL;
   3654 		}
   3655 
   3656 		House->Tracking_Remove(this);
   3657 
   3658 		/*
   3659 		**	If there are any cargo members, delete them.
   3660 		*/
   3661 		while (Is_Something_Attached()) {
   3662 			delete Detach_Object();
   3663 		}
   3664 
   3665 		AircraftClass::Limbo();
   3666 		Class = 0;
   3667 	}
   3668 	ID = -1;
   3669 }
   3670 
   3671 
   3672 /***********************************************************************************************
   3673  * AircraftClass::Scatter -- Causes the aircraft to move away a bit.                           *
   3674  *                                                                                             *
   3675  *    This routine will cause the aircraft to move away from its current location and then     *
   3676  *    enter some idle mode. Typically this is called when the aircraft is attacked while on    *
   3677  *    the ground.                                                                              *
   3678  *                                                                                             *
   3679  * INPUT:   none                                                                               *
   3680  *                                                                                             *
   3681  * OUTPUT:  none                                                                               *
   3682  *                                                                                             *
   3683  * WARNINGS:   none                                                                            *
   3684  *                                                                                             *
   3685  * HISTORY:                                                                                    *
   3686  *   07/08/1995 JLB : Created.                                                                 *
   3687  *=============================================================================================*/
   3688 void AircraftClass::Scatter(COORDINATE , bool, bool )
   3689 {
   3690 	assert(Aircraft.ID(this) == ID);
   3691 	assert(IsActive);
   3692 
   3693 	/*
   3694 	**	Certain missions prevent scattering regardless of whether it would be
   3695 	**	a good idea or not.
   3696 	*/
   3697 	if (!MissionControl[Mission].IsScatter) return;
   3698 
   3699 	/*
   3700 	**	Fixed wing aircraft never scatter.
   3701 	*/
   3702 	if (Class->IsFixedWing) return;
   3703 
   3704 	if (IsLanding || Height == 0) {
   3705 		IsLanding = false;
   3706 		IsTakingOff = true;
   3707 	}
   3708 	Enter_Idle_Mode();
   3709 }
   3710 
   3711 
   3712 /***********************************************************************************************
   3713  * AircraftClass::Mission_Guard -- Handles aircraft in guard mode.                             *
   3714  *                                                                                             *
   3715  *    Aircraft don't like to be in guard mode if in flight. If this situation is detected,     *
   3716  *    then figure out what the aircraft should be doing and go do it.                          *
   3717  *                                                                                             *
   3718  * INPUT:   none                                                                               *
   3719  *                                                                                             *
   3720  * OUTPUT:  Returns with the number of game frames to delay before calling this routine again. *
   3721  *                                                                                             *
   3722  * WARNINGS:   This routine typically calls the normal guard logic for ground units.           *
   3723  *                                                                                             *
   3724  * HISTORY:                                                                                    *
   3725  *   07/18/1995 JLB : Created.                                                                 *
   3726  *   10/10/1995 JLB : Hunts for harvesters that are unescorted.                                *
   3727  *=============================================================================================*/
   3728 int AircraftClass::Mission_Guard(void)
   3729 {
   3730 	assert(Aircraft.ID(this) == ID);
   3731 	assert(IsActive);
   3732 
   3733 	if (Height == FLIGHT_LEVEL) {
   3734 
   3735 		/*
   3736 		**	If part of a team, then do nothing, since the team
   3737 		**	handler will take care of giving this aircraft a
   3738 		**	mission.
   3739 		*/
   3740 		if (Team) {
   3741 			if (Target_Legal(NavCom)) {
   3742 				Assign_Mission(MISSION_MOVE);
   3743 			}
   3744 			return(MissionControl[Mission].Normal_Delay());
   3745 		}
   3746 
   3747 		if (Class->PrimaryWeapon == NULL) {
   3748 			Assign_Destination(::As_Target(Coord_Cell(Coord)));
   3749 			Assign_Mission(MISSION_MOVE);
   3750 		} else {
   3751 			if (!Team.Is_Valid()) Enter_Idle_Mode();
   3752 		}
   3753 		return(1);
   3754 	}
   3755 	if (House->IsHuman) return(MissionControl[Mission].Normal_Delay());
   3756 
   3757 	/*
   3758 	**	If the aircraft is very badly damaged, then it will search for a
   3759 	**	repair bay first.
   3760 	*/
   3761 	if (House->Available_Money() >= 100 && Health_Ratio() <= Rule.ConditionYellow) {
   3762 		if (!In_Radio_Contact() ||
   3763 			(Height == 0 &&
   3764 				(Contact_With_Whom()->What_Am_I() != RTTI_BUILDING || *((BuildingClass *)Contact_With_Whom()) != STRUCT_REPAIR))) {
   3765 
   3766 
   3767 			BuildingClass * building = Find_Docking_Bay(STRUCT_REPAIR, true);
   3768 			if (building != NULL) {
   3769 				Assign_Destination(building->As_Target());
   3770 				Assign_Target(TARGET_NONE);
   3771 				Assign_Mission(MISSION_ENTER);
   3772 				return(1);
   3773 			}
   3774 		}
   3775 	}
   3776 
   3777 	/*
   3778 	**	If the aircraft cannot attack anything because of lack of ammo,
   3779 	**	abort any normal guard logic in order to look for a helipad
   3780 	**	to rearm.
   3781 	*/
   3782 	if (Ammo == 0 && Is_Weapon_Equipped()) {
   3783 		if (!In_Radio_Contact()) {
   3784 			BuildingClass * building = Find_Docking_Bay(STRUCT_HELIPAD, false);
   3785 #ifdef FIXIT_CARRIER	//	checked - ajw 9/28/98
   3786 	if (!Class->IsFixedWing) {
   3787 		int dist = 0x7FFFFFFF;
   3788 		if (building) dist=Distance(building);
   3789 		for (int index = 0; index < Vessels.Count(); index++) {
   3790 			VesselClass *ship = Vessels.Ptr(index);
   3791 			if (ship != NULL && *ship == VESSEL_CARRIER && !ship->IsInLimbo && ship->IsActive && ship->House == House && ship->How_Many() < ship->Class->Max_Passengers()) {
   3792 				if (Distance(ship) < dist || !building) {
   3793 					building = (BuildingClass *)ship;
   3794 					dist = Distance(ship);
   3795 				}
   3796 //				break;
   3797 			}
   3798 		}
   3799 	}
   3800 #endif
   3801 			if (building != NULL) {
   3802 				Assign_Destination(building->As_Target());
   3803 				Assign_Target(TARGET_NONE);
   3804 				Assign_Mission(MISSION_ENTER);
   3805 				return(1);
   3806 			}
   3807 		}
   3808 	}
   3809 
   3810 	/*
   3811 	**	If the aircraft already has a target, then attack it if possible.
   3812 	*/
   3813 	if (Target_Legal(TarCom)) {
   3814 		Assign_Mission(MISSION_ATTACK);
   3815 		return(1);
   3816 	}
   3817 
   3818 	/*
   3819 	**	Transport helicopters don't really do anything but just sit there.
   3820 	*/
   3821 	if (!Is_Weapon_Equipped()) {
   3822 		return(TICKS_PER_SECOND*3);
   3823 	}
   3824 
   3825 	/*
   3826 	**	Computer controlled helicopters will defend themselves by bouncing around
   3827 	**	and looking for a free helipad.
   3828 	*/
   3829 	if (Height == 0 && !In_Radio_Contact()) {
   3830 		Scatter(0, true);
   3831 		return(TICKS_PER_SECOND*3);
   3832 	}
   3833 
   3834 	/*
   3835 	**	Perform a special check to hunt for harvesters that are outside of the protective
   3836 	**	shield of their base.
   3837 	*/
   3838 	if (House->State != STATE_ATTACKED) {
   3839 		TARGET target = House->Find_Juicy_Target(Coord);
   3840 
   3841 		if (Target_Legal(target)) {
   3842 			Assign_Target(target);
   3843 			Assign_Mission(MISSION_ATTACK);
   3844 		}
   3845 	}
   3846 
   3847 	return(FootClass::Mission_Guard());
   3848 }
   3849 
   3850 
   3851 /***********************************************************************************************
   3852  * AircraftClass::Mission_Guard_Area -- Handles the aircraft guard area logic.                 *
   3853  *                                                                                             *
   3854  *    This routine handles area guard logic for aircraft. Aircraft require special handling    *
   3855  *    for this mode since they are to guard area only if they are in a position to do so.      *
   3856  *    Otherwise they just defend themselves.                                                   *
   3857  *                                                                                             *
   3858  * INPUT:   none                                                                               *
   3859  *                                                                                             *
   3860  * OUTPUT:  Returns with the number of game frames to delay before calling this routine        *
   3861  *          again.                                                                             *
   3862  *                                                                                             *
   3863  * WARNINGS:   none                                                                            *
   3864  *                                                                                             *
   3865  * HISTORY:                                                                                    *
   3866  *   08/10/1995 JLB : Created.                                                                 *
   3867  *=============================================================================================*/
   3868 int AircraftClass::Mission_Guard_Area(void)
   3869 {
   3870 	assert(Aircraft.ID(this) == ID);
   3871 	assert(IsActive);
   3872 
   3873 	if (Height == FLIGHT_LEVEL) {
   3874 		if (!Team.Is_Valid()) Enter_Idle_Mode();
   3875 		return(1);
   3876 	}
   3877 	if (House->IsHuman) return(TICKS_PER_SECOND);
   3878 
   3879 	if (Height == 0 && !In_Radio_Contact()) {
   3880 		Scatter(0, true);
   3881 		return(TICKS_PER_SECOND*3);
   3882 	}
   3883 
   3884 	if (Target_Legal(TarCom)) {
   3885 		Assign_Mission(MISSION_ATTACK);
   3886 		return(1);
   3887 	}
   3888 	return(FootClass::Mission_Guard_Area());
   3889 }
   3890 
   3891 
   3892 /***********************************************************************************************
   3893  * AircraftClass::Response_Attack -- Gives audio response to attack order.                     *
   3894  *                                                                                             *
   3895  *    This routine is used to give an audio response to an attack order.                       *
   3896  *                                                                                             *
   3897  * INPUT:   none                                                                               *
   3898  *                                                                                             *
   3899  * OUTPUT:  none                                                                               *
   3900  *                                                                                             *
   3901  * WARNINGS:   none                                                                            *
   3902  *                                                                                             *
   3903  * HISTORY:                                                                                    *
   3904  *   08/10/1995 JLB : Created.                                                                 *
   3905  *=============================================================================================*/
   3906 void AircraftClass::Response_Attack(void)
   3907 {
   3908 	assert(Aircraft.ID(this) == ID);
   3909 	assert(IsActive);
   3910 
   3911 	static VocType _response[] = {
   3912 		VOC_AFFIRM,
   3913 		VOC_ACKNOWL
   3914 	};
   3915 	VocType response = _response[Sim_Random_Pick(0, ARRAY_SIZE(_response)-1)];
   3916 	if (AllowVoice) {
   3917 		Sound_Effect(response, fixed(1), -(ID+1));
   3918 	}
   3919 }
   3920 
   3921 
   3922 /***********************************************************************************************
   3923  * AircraftClass::Response_Move -- Gives audio response to move request.                       *
   3924  *                                                                                             *
   3925  *    This routine is used to give an audio response to movement orders.                       *
   3926  *                                                                                             *
   3927  * INPUT:   none                                                                               *
   3928  *                                                                                             *
   3929  * OUTPUT:  none                                                                               *
   3930  *                                                                                             *
   3931  * WARNINGS:   none                                                                            *
   3932  *                                                                                             *
   3933  * HISTORY:                                                                                    *
   3934  *   08/10/1995 JLB : Created.                                                                 *
   3935  *=============================================================================================*/
   3936 void AircraftClass::Response_Move(void)
   3937 {
   3938 	assert(Aircraft.ID(this) == ID);
   3939 	assert(IsActive);
   3940 
   3941 	static VocType _response[] = {
   3942 		VOC_ACKNOWL,
   3943 		VOC_AFFIRM
   3944 	};
   3945 	VocType response = _response[Sim_Random_Pick(0, ARRAY_SIZE(_response)-1)];
   3946 	if (AllowVoice) {
   3947 		Sound_Effect(response, fixed(1), -(ID+1));
   3948 	}
   3949 }
   3950 
   3951 
   3952 /***********************************************************************************************
   3953  * AircraftClass::Response_Select -- Gives audio response when selected.                       *
   3954  *                                                                                             *
   3955  *    This routine is called when an audio response for selection is desired.                  *
   3956  *                                                                                             *
   3957  * INPUT:   none                                                                               *
   3958  *                                                                                             *
   3959  * OUTPUT:  none                                                                               *
   3960  *                                                                                             *
   3961  * WARNINGS:   none                                                                            *
   3962  *                                                                                             *
   3963  * HISTORY:                                                                                    *
   3964  *   08/10/1995 JLB : Created.                                                                 *
   3965  *=============================================================================================*/
   3966 void AircraftClass::Response_Select(void)
   3967 {
   3968 	assert(Aircraft.ID(this) == ID);
   3969 	assert(IsActive);
   3970 
   3971 	static VocType _response[] = {
   3972 		VOC_VEHIC,
   3973 		VOC_REPORT,
   3974 		VOC_YESSIR,
   3975 		VOC_YESSIR,
   3976 		VOC_YESSIR,
   3977 		VOC_AWAIT
   3978 	};
   3979 	VocType response = _response[Sim_Random_Pick(0, ARRAY_SIZE(_response)-1)];
   3980 	if (AllowVoice) {
   3981 		Sound_Effect(response, fixed(1), -(ID+1));
   3982 	}
   3983 }
   3984 
   3985 
   3986 /***********************************************************************************************
   3987  * AircraftClass::Can_Fire -- Checks to see if the aircraft can fire.                          *
   3988  *                                                                                             *
   3989  *    This routine is used to determine if the aircraft can fire its weapon at the target      *
   3990  *    specified. If it cannot, then the reason why is returned.                                *
   3991  *                                                                                             *
   3992  * INPUT:   target   -- The target that the aircraft might fire upon.                          *
   3993  *                                                                                             *
   3994  *          which    -- The weapon that will be used to fire.                                  *
   3995  *                                                                                             *
   3996  * OUTPUT:  Returns with the reason why it can't fire or with FIRE_OK.                         *
   3997  *                                                                                             *
   3998  * WARNINGS:   none                                                                            *
   3999  *                                                                                             *
   4000  * HISTORY:                                                                                    *
   4001  *   03/05/1996 JLB : Created.                                                                 *
   4002  *   07/11/1996 JLB : Fixed for camera carrying aircraft.                                      *
   4003  *=============================================================================================*/
   4004 FireErrorType AircraftClass::Can_Fire(TARGET target, int which) const
   4005 {
   4006 	assert(Aircraft.ID(this) == ID);
   4007 	assert(IsActive);
   4008 
   4009 	if (Passenger && !Is_Something_Attached()) {
   4010 		return(FIRE_AMMO);
   4011 	}
   4012 
   4013 	bool camera = (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->IsCamera);
   4014 	bool fudge = (Passenger || (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->Bullet != NULL && Class->PrimaryWeapon->Bullet->IsParachuted));
   4015 
   4016 	if (fudge && !camera && !Ammo && !Passenger) {
   4017 		return(FIRE_AMMO);
   4018 	}
   4019 
   4020 	/*
   4021 	**	Passenger aircraft that wish to 'fire' actually are requesting to
   4022 	**	paradrop or 'throw out' the cargo. This is always allowed if the terrain under the
   4023 	**	aircraft is generally clear.
   4024 	*/
   4025 	if (camera || (fudge && Passenger && Is_Something_Attached()))  {
   4026 		if (Arm != 0) return(FIRE_REARM);
   4027 
   4028 		if (Distance(target) < (camera ? 0x0380 : 0x0200) && Map.In_Radar(Coord_Cell(Center_Coord()))) {
   4029 //		if (Distance(target) < (camera ? 0x0380 : 0x0280) && Map.In_Radar(Coord_Cell(Center_Coord()))) {
   4030 			return(FIRE_OK);
   4031 		}
   4032 		return(FIRE_RANGE);
   4033 	}
   4034 
   4035 	FireErrorType canfire = FootClass::Can_Fire(target, which);
   4036 
   4037 	if (canfire == FIRE_OK) {
   4038 
   4039 		/*
   4040 		**	Double check to make sure that the facing is roughly toward
   4041 		**	the target. If the difference is too great, then firing is
   4042 		**	temporarily postponed.
   4043 		*/
   4044 		if (Class->IsFixedWing) {
   4045 
   4046 			int diff = PrimaryFacing.Difference(Direction(TarCom));
   4047 			if (ABS(diff) > (fudge ? 16 : 8)) {
   4048 				return(FIRE_FACING);
   4049 			}
   4050 		}
   4051 	}
   4052 	return(canfire);
   4053 }
   4054 
   4055 
   4056 /***********************************************************************************************
   4057  * AircraftClass::Landing_Takeoff_AI -- Handle aircraft take off and landing processing.       *
   4058  *                                                                                             *
   4059  *    This routine handles the tricky maneuver of taking off and landing. The process of       *
   4060  *    landing is not entirely safe and thus the aircraft may be destroyed as a consequence.    *
   4061  *                                                                                             *
   4062  * INPUT:   none                                                                               *
   4063  *                                                                                             *
   4064  * OUTPUT:  bool; Was the aircraft destroyed by this process?                                  *
   4065  *                                                                                             *
   4066  * WARNINGS:   Only call this routine once per aircraft per game logic loop. Be sure to        *
   4067  *             examine the return value and if true, abort all further processing of this      *
   4068  *             aircraft since it is now dead.                                                  *
   4069  *                                                                                             *
   4070  * HISTORY:                                                                                    *
   4071  *   07/29/1996 JLB : Created.                                                                 *
   4072  *=============================================================================================*/
   4073 bool AircraftClass::Landing_Takeoff_AI(void)
   4074 {
   4075 	/*
   4076 	**	Handle landing and taking off logic. Helicopters are prime users of this technique. The
   4077 	**	aircraft will either gain or lose altitude as appropriate. As the aircraft transitions
   4078 	**	between flying level and ground level, it will be moved into the appropriate render
   4079 	**	layer.
   4080 	*/
   4081 	if (Is_Door_Closed() && (IsLanding || IsTakingOff)) {
   4082 		LayerType layer = In_Which_Layer();
   4083 
   4084 		if (IsLanding) {
   4085 			Mark(MARK_UP);
   4086 			if (Height) Height -= Pixel_To_Lepton(1);
   4087 			if (Height <= 0) {
   4088 				Height = 0;
   4089 				IsLanding = false;
   4090 				Set_Speed(0);
   4091 
   4092 				/*
   4093 				**	If the NavCom now equals the destination, then clear out the NavCom.
   4094 				*/
   4095 				if (Coord_Cell(Center_Coord()) == As_Cell(NavCom)) {
   4096 					Assign_Destination(TARGET_NONE);
   4097 				}
   4098 
   4099 				/*
   4100 				** If a fixed-wing aircraft just landed on the ground, blow him up
   4101 				*/
   4102 				if (Class->IsFixedWing && Mission != MISSION_ENTER) {
   4103 					Strength = 1;
   4104 
   4105 					int damage = Strength;
   4106 					Map.Remove(this, layer);
   4107 					Take_Damage(damage, 0, WARHEAD_AP, 0, true);
   4108 					return(true);
   4109 				}
   4110 
   4111 				if (Target_Legal(NavCom) && As_Techno(NavCom) == Contact_With_Whom()) {
   4112 					if (In_Radio_Contact() && Transmit_Message(RADIO_IM_IN) != RADIO_ROGER) {
   4113 						Scatter(0, true);
   4114 					}
   4115 				}
   4116 			}
   4117 			Mark(MARK_DOWN);
   4118 		}
   4119 		if (IsTakingOff) {
   4120 			Mark(MARK_UP);
   4121 //			Map.Remove(this, layer);
   4122 			Height += Pixel_To_Lepton(1);
   4123 			if (Height >= FLIGHT_LEVEL) {
   4124 				Height = FLIGHT_LEVEL;
   4125 				IsTakingOff = false;
   4126 			}
   4127 //			Map.Submit(this, In_Which_Layer());
   4128 			Mark(MARK_DOWN);
   4129 		}
   4130 
   4131 		/*
   4132 		**	Make adjustments for altitude by moving from one layer to another as
   4133 		**	necessary.
   4134 		*/
   4135 		if (layer != In_Which_Layer()) {
   4136 
   4137 			/*
   4138 			**	When the aircraft is about to enter the ground layer, perform on last
   4139 			**	check to see if it is legal to enter that location. If not, then
   4140 			**	start the take off process. Let the normal logic handle this
   4141 			**	change of plans.
   4142 			*/
   4143 			bool ok = true;
   4144 			if (In_Which_Layer() == LAYER_GROUND && !IsTakingOff && !Class->IsFixedWing) {
   4145 				if (!Is_LZ_Clear(::As_Target(Coord_Cell(Coord)))) {
   4146 					IsTakingOff = true;
   4147 					Mark(MARK_UP);
   4148 					Height += Pixel_To_Lepton(1);
   4149 					Mark(MARK_DOWN);
   4150 					ok = false;
   4151 				}
   4152 			}
   4153 
   4154 			if (ok) {
   4155 
   4156 				Map.Remove(this, layer);
   4157 				Map.Submit(this, In_Which_Layer());
   4158 
   4159 				/*
   4160 				**	When the aircraft is close to the ground, it should exist as a ground object.
   4161 				**	This aspect is controlled by the Place_Down and Pick_Up functions.
   4162 				*/
   4163 				if (In_Which_Layer() == LAYER_GROUND) {
   4164 					Assign_Destination(TARGET_NONE);		// Clear the navcom.
   4165 					Transmit_Message(RADIO_TETHER);
   4166 					Look();
   4167 //					Map.Sight_From(Coord_Cell(Coord), 1, House, false);
   4168 				} else  {
   4169 					Transmit_Message(RADIO_UNTETHER);
   4170 
   4171 					/*
   4172 					**	If the navigation computer is not attached to the object this
   4173 					**	aircraft is in radio contact with, then assume that radio
   4174 					**	contact is now superfluous. Break radio contact.
   4175 					*/
   4176 					if (In_Radio_Contact() && Target_Legal(NavCom) && NavCom != Contact_With_Whom()->As_Target()) {
   4177 						Transmit_Message(RADIO_OVER_OUT);
   4178 					}
   4179 				}
   4180 			}
   4181 		}
   4182 	}
   4183 	return(false);
   4184 }
   4185 
   4186 
   4187 /***********************************************************************************************
   4188  * AircraftClass::Edge_Of_World_AI -- Detect if aircraft has exited the map.                   *
   4189  *                                                                                             *
   4190  *    Certain aircraft will be eliminated when they leave the edge of the world presumably     *
   4191  *    after completing their mission. An exception is for aircraft that have been newly        *
   4192  *    created as reinforcements and have not yet completed their mission.                      *
   4193  *                                                                                             *
   4194  * INPUT:   none                                                                               *
   4195  *                                                                                             *
   4196  * OUTPUT:  bool; Was the aircraft deleted by this routine?                                    *
   4197  *                                                                                             *
   4198  * WARNINGS:   Be sure to call this routine only once per aircraft per game logic loop. If     *
   4199  *             the return value is true, then abort any further processing of this aircraft    *
   4200  *             since it has been eliminated.                                                   *
   4201  *                                                                                             *
   4202  * HISTORY:                                                                                    *
   4203  *   07/29/1996 JLB : Created.                                                                 *
   4204  *=============================================================================================*/
   4205 bool AircraftClass::Edge_Of_World_AI(void)
   4206 {
   4207 	if (!Map.In_Radar(Coord_Cell(Coord))) {
   4208 		if (Mission == MISSION_RETREAT /*|| (*this == AIRCRAFT_CARGO && !Is_Something_Attached())*/) {
   4209 
   4210 			/*
   4211 			**	Check to see if there are any civilians aboard. If so, then flag the house
   4212 			**	that the civilian evacuation trigger event has been fulfilled.
   4213 			*/
   4214 			while (Is_Something_Attached()) {
   4215 				FootClass * obj = Detach_Object();
   4216 
   4217 				/*
   4218 				**	Flag the owning house that civ evacuation has occurred.
   4219 				*/
   4220 				if (_Counts_As_Civ_Evac(obj)) {
   4221 					obj->House->IsCivEvacuated = true;
   4222 				}
   4223 
   4224 				if (obj->Team.Is_Valid()) obj->Team->IsLeaveMap = true;
   4225 
   4226 #ifdef OLD
   4227 				/*
   4228 				**	Transport planes that leave can only be because they carry purchased
   4229 				**	equipment and must be have their cost refunded.
   4230 				*/
   4231 				if (*this == AIRCRAFT_CARGO) {
   4232 					House->Refund_Money(obj->Class_Of().Cost_Of());
   4233 				}
   4234 #endif
   4235 				delete obj;
   4236 			}
   4237 			if (Team.Is_Valid()) {
   4238 				Team->IsLeaveMap = true;
   4239 			}
   4240 			Stun();
   4241 			delete this;
   4242 			return(true);
   4243 		}
   4244 	} else {
   4245 		IsLocked = true;
   4246 	}
   4247 	return(false);
   4248 }
   4249 
   4250 
   4251 /***********************************************************************************************
   4252  * AircraftClass::Movement_AI -- Handles aircraft physical movement logic.                     *
   4253  *                                                                                             *
   4254  *    This routine manages the aircraft movement across the map. If any movement occurred, the *
   4255  *    aircraft will be flagged to be redrawn.                                                  *
   4256  *                                                                                             *
   4257  * INPUT:   none                                                                               *
   4258  *                                                                                             *
   4259  * OUTPUT:  none                                                                               *
   4260  *                                                                                             *
   4261  * WARNINGS:   Only call this routine once per aircraft per game logic loop.                   *
   4262  *                                                                                             *
   4263  * HISTORY:                                                                                    *
   4264  *   07/29/1996 JLB : Created.                                                                 *
   4265  *=============================================================================================*/
   4266 void AircraftClass::Movement_AI(void)
   4267 {
   4268 	/*
   4269 	**	If for some strange reason, there is a valid NavCom, but this aircraft is not
   4270 	**	in a movement order, then give it a movement order.
   4271 	*/
   4272 	if (Target_Legal(NavCom) && Mission == MISSION_GUARD && MissionQueue == MISSION_NONE) {
   4273 		Assign_Mission(MISSION_MOVE);
   4274 	}
   4275 
   4276 	if (Speed != 0) {
   4277 		if (In_Which_Layer() == LAYER_GROUND)  {
   4278 			Mark(MARK_UP);
   4279 			Physics(Coord, PrimaryFacing);
   4280 			Mark(MARK_DOWN);
   4281 		} else {
   4282 			Mark(MARK_CHANGE_REDRAW);
   4283 			if (Physics(Coord, PrimaryFacing) != RESULT_NONE) {
   4284 				Mark(MARK_CHANGE_REDRAW);
   4285 			}
   4286 		}
   4287 	}
   4288 }
   4289 
   4290 
   4291 /***********************************************************************************************
   4292  * AircraftClass::Rotation_AI -- Handle aircraft body and flight rotation.                     *
   4293  *                                                                                             *
   4294  *    This will process the aircraft visible body and flight model rotation operations. If     *
   4295  *    any rotation occurred, the aircraft will be flagged to be redrawn.                       *
   4296  *                                                                                             *
   4297  * INPUT:   none                                                                               *
   4298  *                                                                                             *
   4299  * OUTPUT:  none                                                                               *
   4300  *                                                                                             *
   4301  * WARNINGS:   Only call this routine once per aircraft per game logic loop.                   *
   4302  *                                                                                             *
   4303  * HISTORY:                                                                                    *
   4304  *   07/29/1996 JLB : Created.                                                                 *
   4305  *=============================================================================================*/
   4306 void AircraftClass::Rotation_AI(void)
   4307 {
   4308 	if (PrimaryFacing.Is_Rotating()) {
   4309 		Mark(MARK_CHANGE_REDRAW);
   4310 		if (PrimaryFacing.Rotation_Adjust(Class->ROT)) {
   4311 			Mark(MARK_CHANGE_REDRAW);
   4312 		}
   4313 	}
   4314 	if (Class->IsFixedWing) {
   4315 		SecondaryFacing = PrimaryFacing;
   4316 	}
   4317 	if (SecondaryFacing.Is_Rotating()) {
   4318 		Mark(MARK_CHANGE_REDRAW);
   4319 		if (SecondaryFacing.Rotation_Adjust(Class->ROT)) {
   4320 			Mark(MARK_CHANGE_REDRAW);
   4321 		}
   4322 	}
   4323 }
   4324 
   4325 
   4326 /***********************************************************************************************
   4327  * AircraftClass::Per_Cell_Process -- Handle the aircraft per cell process.                    *
   4328  *                                                                                             *
   4329  *    This is a seldom used function since its only purpose is to be called when an aircraft   *
   4330  *    lands on the ground.                                                                     *
   4331  *                                                                                             *
   4332  * INPUT:   why   -- Why was this per cell process function called.                            *
   4333  *                                                                                             *
   4334  * OUTPUT:  none                                                                               *
   4335  *                                                                                             *
   4336  * WARNINGS:   none                                                                            *
   4337  *                                                                                             *
   4338  * HISTORY:                                                                                    *
   4339  *   09/15/1996 JLB : Created.                                                                 *
   4340  *=============================================================================================*/
   4341 void AircraftClass::Per_Cell_Process(PCPType why)
   4342 {
   4343 	BStart(BENCH_PCP);
   4344 	FootClass::Per_Cell_Process(why);
   4345 	BEnd(BENCH_PCP);
   4346 }
   4347 
   4348 
   4349 /***********************************************************************************************
   4350  * AircraftClass::Assign_Destination -- Assigns movement destination to the object.            *
   4351  *                                                                                             *
   4352  *    This routine is called when the object needs to have a new movement destination          *
   4353  *    assigned.  Aircraft have their own version of this routine because a fixed-wing plane	  *
   4354  *    trying to land will behave poorly if given a new destinatio while it's landing.			  *                                                                                         *
   4355  *                                                                                             *
   4356  * INPUT:   destination -- The destination to assign to this object.                           *
   4357  *                                                                                             *
   4358  * OUTPUT:  none                                                                               *
   4359  *                                                                                             *
   4360  * WARNINGS:   none                                                                            *
   4361  *                                                                                             *
   4362  * HISTORY:                                                                                    *
   4363  *   07/24/1995 JLB : Created.                                                                 *
   4364  *=============================================================================================*/
   4365 void AircraftClass::Assign_Destination(TARGET dest)
   4366 {
   4367 	assert(IsActive);
   4368 	if (dest == NavCom) return;
   4369 
   4370 	if (Target_Legal(dest) && Class->IsFixedWing && (IsLanding || (Target_Legal(NavCom) && dest != NavCom))) {
   4371 //	if (Target_Legal(dest) /*&& Class->IsFixedWing*/ && (IsLanding || (Target_Legal(NavCom) && dest != NavCom))) {
   4372 
   4373 //		if (Class->IsFixedWing || As_Cell(dest) != Coord_Cell(Center_Coord())) {
   4374 			Process_Take_Off();
   4375 			Status = 0;
   4376 //		}
   4377 	}
   4378 	FootClass::Assign_Destination(dest);
   4379 }
   4380 
   4381 
   4382 /***********************************************************************************************
   4383  * AircraftClass::In_Which_Layer -- Calculates the display layer of the aircraft.              *
   4384  *                                                                                             *
   4385  *    This examines the aircraft to determine what display layer it should be located          *
   4386  *    in. Fixed wing aircraft must always be in the top layer if they are flying even though   *
   4387  *    they may be low to the ground.                                                           *
   4388  *                                                                                             *
   4389  * INPUT:   none                                                                               *
   4390  *                                                                                             *
   4391  * OUTPUT:  Returns with the layer that this aircraft resides in.                              *
   4392  *                                                                                             *
   4393  * WARNINGS:   none                                                                            *
   4394  *                                                                                             *
   4395  * HISTORY:                                                                                    *
   4396  *   10/20/1996 JLB : Created.                                                                 *
   4397  *=============================================================================================*/
   4398 LayerType AircraftClass::In_Which_Layer(void) const
   4399 {
   4400 	if (Class->IsFixedWing && Height > 0) {
   4401 		return(LAYER_TOP);
   4402 	}
   4403 	return(FootClass::In_Which_Layer());
   4404 }
   4405 
   4406 
   4407 /***********************************************************************************************
   4408  * AircraftClass::Look -- Aircraft will look if they are on the ground always.                 *
   4409  *                                                                                             *
   4410  *    Aircraft perform a look operation according to their sight range. If the aircraft is     *
   4411  *    on the ground, then it will look a distance of one cell regardless of what its           *
   4412  *    specified sight range is.                                                                *
   4413  *                                                                                             *
   4414  * INPUT:   incremental -- Is this an incremental look?                                        *
   4415  *                                                                                             *
   4416  * OUTPUT:  none                                                                               *
   4417  *                                                                                             *
   4418  * WARNINGS:   none                                                                            *
   4419  *                                                                                             *
   4420  * HISTORY:                                                                                    *
   4421  *   10/23/1996 JLB : Created.                                                                 *
   4422  *=============================================================================================*/
   4423 void AircraftClass::Look(bool incremental)
   4424 {
   4425 	assert(IsActive);
   4426 	assert(!IsInLimbo);
   4427 
   4428 	int sight_range = Techno_Type_Class()->SightRange;
   4429 	if (Height == 0) {
   4430 		sight_range = 1;
   4431 	}
   4432 
   4433 	if (sight_range) {
   4434 		Map.Sight_From(Coord_Cell(Coord), sight_range, House, incremental);
   4435 	}
   4436 }
   4437 
   4438 unsigned AircraftClass::Spied_By() const
   4439 {
   4440 	unsigned spiedby = FootClass::Spied_By();
   4441 
   4442 	/*
   4443 	** Aircraft inherit the spied by flags of the helipad they've landed on
   4444 	*/
   4445 	BuildingClass* building = Map[Coord].Cell_Building();
   4446 	if ((Height == 0) && In_Radio_Contact() && (building == Contact_With_Whom())) {
   4447 		spiedby |= building->Spied_By();
   4448 	}
   4449 
   4450 	return spiedby;
   4451 }