Changeset - r6087:a13ab75a089f
[Not reviewed]
master
0 5 0
tron - 18 years ago 2007-02-20 06:39:09
tron@openttd.org
(svn r8822) -Fix

Variable scope, const
5 files changed with 119 insertions and 203 deletions:
0 comments (0 inline, 0 general)
src/aircraft.h
Show inline comments
 
@@ -37,14 +37,16 @@ static inline bool IsAircraftInHangar(co
 

	
 
static inline bool IsAircraftInHangarStopped(const Vehicle* v)
 
{
 
	return IsAircraftInHangar(v) && v->vehstatus & VS_STOPPED;
 
}
 

	
 
uint16 AircraftDefaultCargoCapacity(CargoID cid, EngineID engine_type);
 
uint16 AircraftDefaultCargoCapacity(CargoID cid, const AircraftVehicleInfo*);
 

	
 
void CcBuildAircraft(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
void CcCloneAircraft(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
void HandleAircraftEnterHangar(Vehicle *v);
 
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height);
 

	
 
void UpdateAirplanesOnNewStation(const Station *st);
 

	
 
#endif /* AIRCRAFT_H */
src/aircraft_cmd.cpp
Show inline comments
 
@@ -31,13 +31,13 @@
 
// this maps the terminal to its corresponding state and block flag
 
// currently set for 10 terms, 4 helipads
 
static const byte _airport_terminal_state[] = {2, 3, 4, 5, 6, 7, 19, 20, 0, 0, 8, 9, 21, 22};
 
static const byte _airport_terminal_flag[] =  {0, 1, 2, 3, 4, 5, 22, 23, 0, 0, 6, 7, 24, 25};
 

	
 
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc);
 
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc);
 
static bool AirportSetBlocks(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
 
static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
 
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc);
 
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc);
 
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *apc);
 
static void CrashAirplane(Vehicle *v);
 

	
 
@@ -127,17 +127,15 @@ int GetAircraftImage(const Vehicle* v, D
 
	}
 
	return direction + _aircraft_sprite[spritenum];
 
}
 

	
 
SpriteID GetRotorImage(const Vehicle *v)
 
{
 
	const Vehicle *w;
 

	
 
	assert(v->subtype == AIR_HELICOPTER);
 

	
 
	w = v->next->next;
 
	const Vehicle *w = v->next->next;
 
	if (is_custom_sprite(v->spritenum)) {
 
		SpriteID spritenum = GetCustomRotorSprite(v, false);
 
		if (spritenum != 0) return spritenum;
 
	}
 

	
 
	/* Return standard rotor sprites if there are no custom sprites for this helicopter */
 
@@ -202,16 +200,14 @@ static int32 EstimateAircraftCost(const 
 
 * Calculates cargo capacity based on an aircraft's passenger
 
 * and mail capacities.
 
 * @param cid Which cargo type to calculate a capacity for.
 
 * @param engine Which engine to find a cargo capacity for.
 
 * @return New cargo capacity value.
 
 */
 
uint16 AircraftDefaultCargoCapacity(CargoID cid, EngineID engine_type)
 
uint16 AircraftDefaultCargoCapacity(CargoID cid, const AircraftVehicleInfo *avi)
 
{
 
	const AircraftVehicleInfo *avi = AircraftVehInfo(engine_type);
 

	
 
	assert(cid != CT_INVALID);
 

	
 
	/* An aircraft can carry twice as much goods as normal cargo,
 
	 * and four times as many passengers. */
 
	switch (cid) {
 
		case CT_PASSENGERS:
 
@@ -230,15 +226,12 @@ uint16 AircraftDefaultCargoCapacity(Carg
 
 * @param tile tile of depot where aircraft is built
 
 * @param p1 aircraft type being built (engine)
 
 * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
 
 */
 
int32 CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *vl[3];
 
	UnitID unit_num;
 

	
 
	if (!IsEngineBuildable(p1, VEH_Aircraft, _current_player)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
 

	
 
	const AircraftVehicleInfo *avi = AircraftVehInfo(p1);
 
	int32 value = EstimateAircraftCost(avi);
 

	
 
	// to just query the cost, it is not neccessary to have a valid tile (automation/AI)
 
@@ -254,39 +247,36 @@ int32 CmdBuildAircraft(TileIndex tile, u
 
	if (!(apc->flags & (avi->subtype & AIR_CTOL ? AirportFTAClass::AIRPLANES : AirportFTAClass::HELICOPTERS))) {
 
		return CMD_ERROR;
 
	}
 

	
 
	/* Allocate 2 or 3 vehicle structs, depending on type
 
	 * vl[0] = aircraft, vl[1] = shadow, [vl[2] = rotor] */
 
	Vehicle *vl[3];
 
	if (!AllocateVehicles(vl, avi->subtype & AIR_CTOL ? 2 : 3)) {
 
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_Aircraft);
 
	UnitID unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_Aircraft);
 
	if (unit_num > _patches.max_aircraft)
 
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 

	
 
	if (flags & DC_EXEC) {
 
		CargoID cargo;
 
		uint x;
 
		uint y;
 

	
 
		Vehicle *v = vl[0]; // aircraft
 
		Vehicle *u = vl[1]; // shadow
 

	
 
		v->unitnumber = unit_num;
 
		v->type = u->type = VEH_Aircraft;
 
		v->direction = DIR_SE;
 

	
 
		v->owner = u->owner = _current_player;
 

	
 
		v->tile = tile;
 
//		u->tile = 0;
 

	
 
		x = TileX(tile) * TILE_SIZE + 5;
 
		y = TileY(tile) * TILE_SIZE + 3;
 
		uint x = TileX(tile) * TILE_SIZE + 5;
 
		uint y = TileY(tile) * TILE_SIZE + 3;
 

	
 
		v->x_pos = u->x_pos = x;
 
		v->y_pos = u->y_pos = y;
 

	
 
		u->z_pos = GetSlopeZ(x, y);
 
		v->z_pos = u->z_pos + 1;
 
@@ -333,25 +323,25 @@ int32 CmdBuildAircraft(TileIndex tile, u
 

	
 
		/* Danger, Will Robinson!
 
		 * If the aircraft is refittable, but cannot be refitted to
 
		 * passengers, we select the cargo type from the refit mask.
 
		 * This is a fairly nasty hack to get around the fact that TTD
 
		 * has no default cargo type specifier for planes... */
 
		cargo = FindFirstRefittableCargo(p1);
 
		CargoID cargo = FindFirstRefittableCargo(p1);
 
		if (cargo != CT_INVALID && cargo != CT_PASSENGERS) {
 
			uint16 callback = CALLBACK_FAILED;
 

	
 
			v->cargo_type = cargo;
 

	
 
			if (HASBIT(EngInfo(p1)->callbackmask, CBM_REFIT_CAPACITY)) {
 
				callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
 
			}
 

	
 
			if (callback == CALLBACK_FAILED) {
 
				/* Callback failed, or not executed; use the default cargo capacity */
 
				v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, v->engine_type);
 
				v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, avi);
 
			} else {
 
				v->cargo_cap = callback;
 
			}
 

	
 
			/* Set the 'second compartent' capacity to none */
 
			u->cargo_cap = 0;
 
@@ -447,17 +437,15 @@ static void DoDeleteAircraft(Vehicle *v)
 
 * @param tile unused
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
int32 CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 
	Vehicle *v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 

	
 
@@ -474,28 +462,25 @@ int32 CmdSellAircraft(TileIndex tile, ui
 
 * @param tile unused
 
 * @param p1 aircraft ID to start/stop
 
 * @param p2 unused
 
 */
 
int32 CmdStartStopAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	uint16 callback;
 

	
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 
	Vehicle *v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	// cannot stop airplane when in flight, or when taking off / landing
 
	if (v->u.air.state >= STARTTAKEOFF && v->u.air.state < TERM7)
 
		return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
 

	
 
	/* Check if this aircraft can be started/stopped. The callback will fail or
 
	 * return 0xFF if it can. */
 
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
 
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
 
		return_cmd_error(error);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
@@ -518,23 +503,21 @@ int32 CmdStartStopAircraft(TileIndex til
 
 * @param p2 various bitmasked elements
 
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
 
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
 
 */
 
int32 CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (p2 & DEPOT_MASS_SEND) {
 
		/* Mass goto depot requested */
 
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
 
		return SendAllVehiclesToDepot(VEH_Aircraft, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
 
	}
 

	
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 
	Vehicle *v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner) || IsAircraftInHangar(v)) return CMD_ERROR;
 

	
 
	if (v->current_order.type == OT_GOTO_DEPOT && !(p2 & DEPOT_LOCATE_HANGAR)) {
 
		if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
 
			/* We called with a different DEPOT_SERVICE setting.
 
@@ -557,22 +540,19 @@ int32 CmdSendAircraftToHangar(TileIndex 
 
	} else {
 
		bool next_airport_has_hangar = true;
 
		StationID next_airport_index = v->u.air.targetairport;
 
		const Station *st = GetStation(next_airport_index);
 
		/* If the station is not a valid airport or if it has no hangars */
 
		if (!st->IsValid() || st->airport_tile == 0 || st->Airport()->nof_depots == 0) {
 
			StationID station;
 

	
 
			// the aircraft has to search for a hangar on its own
 
			station = FindNearestHangar(v);
 
			StationID station = FindNearestHangar(v);
 

	
 
			next_airport_has_hangar = false;
 
			if (station == INVALID_STATION) return CMD_ERROR;
 
			st = GetStation(station);
 
			next_airport_index = station;
 

	
 
		}
 

	
 
		if (flags & DC_EXEC) {
 
			v->current_order.type = OT_GOTO_DEPOT;
 
			v->current_order.flags = OF_NON_STOP;
 
			if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
 
@@ -596,35 +576,29 @@ int32 CmdSendAircraftToHangar(TileIndex 
 
 * @param p2 various bitstuffed elements
 
 * - p2 = (bit 0-7) - the new cargo type to refit to
 
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
 
 */
 
int32 CmdRefitAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	int pass, mail;
 
	int32 cost;
 
	CargoID new_cid = GB(p2, 0, 8);
 
	byte new_subtype = GB(p2, 8, 8);
 
	const AircraftVehicleInfo *avi;
 
	uint16 callback = CALLBACK_FAILED;
 

	
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 
	Vehicle *v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
 

	
 
	avi = AircraftVehInfo(v->engine_type);
 

	
 
	/* Check cargo */
 
	CargoID new_cid = GB(p2, 0, 8);
 
	if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
 

	
 
	/* Check the refit capacity callback */
 
	uint16 callback = CALLBACK_FAILED;
 
	if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
 
		/* Back up the existing cargo type */
 
		CargoID temp_cid = v->cargo_type;
 
		byte temp_subtype = v->cargo_subtype;
 
		v->cargo_type = new_cid;
 
		v->cargo_subtype = new_subtype;
 
@@ -633,32 +607,34 @@ int32 CmdRefitAircraft(TileIndex tile, u
 

	
 
		/* Restore the cargo type */
 
		v->cargo_type = temp_cid;
 
		v->cargo_subtype = temp_subtype;
 
	}
 

	
 
	const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
 

	
 
	uint pass;
 
	if (callback == CALLBACK_FAILED) {
 
		/* If the callback failed, or wasn't executed, use the aircraft's
 
		 * default cargo capacity */
 
		pass = AircraftDefaultCargoCapacity(new_cid, v->engine_type);
 
		pass = AircraftDefaultCargoCapacity(new_cid, avi);
 
	} else {
 
		pass = callback;
 
	}
 
	_returned_refit_capacity = pass;
 

	
 
	cost = 0;
 
	int32 cost = 0;
 
	if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
 
		cost = GetRefitCost(v->engine_type);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Vehicle *u;
 
		v->cargo_cap = pass;
 

	
 
		u = v->next;
 
		mail = (new_cid != CT_PASSENGERS) ? 0 : avi->mail_capacity;
 
		Vehicle *u = v->next;
 
		uint mail = new_cid != CT_PASSENGERS ? 0 : avi->mail_capacity;
 
		u->cargo_cap = mail;
 
		if (v->cargo_type == new_cid) {
 
			v->cargo_count = min(pass, v->cargo_count);
 
			u->cargo_count = min(mail, u->cargo_count);
 
		} else {
 
			v->cargo_count = 0;
 
@@ -674,14 +650,12 @@ int32 CmdRefitAircraft(TileIndex tile, u
 
	return cost;
 
}
 

	
 

	
 
static void CheckIfAircraftNeedsService(Vehicle *v)
 
{
 
	const Station* st;
 

	
 
	if (_patches.servint_aircraft == 0) return;
 
	if (!VehicleNeedsService(v)) return;
 
	if (v->vehstatus & VS_STOPPED) return;
 

	
 
	if (v->current_order.type == OT_GOTO_DEPOT &&
 
			v->current_order.flags & OF_HALT_IN_DEPOT)
 
@@ -691,13 +665,13 @@ static void CheckIfAircraftNeedsService(
 

	
 
	if (IsAircraftInHangar(v)) {
 
		VehicleServiceInDepot(v);
 
		return;
 
	}
 

	
 
	st = GetStation(v->current_order.dest);
 
	const Station *st = GetStation(v->current_order.dest);
 
	// only goto depot if the target airport has terminals (eg. it is airport)
 
	if (st->IsValid() && st->airport_tile != 0 && st->Airport()->terminals != NULL) {
 
//		printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index);
 
//		v->u.air.targetairport = st->index;
 
		v->current_order.type = OT_GOTO_DEPOT;
 
		v->current_order.flags = OF_NON_STOP;
 
@@ -708,27 +682,25 @@ static void CheckIfAircraftNeedsService(
 
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
 
	}
 
}
 

	
 
void OnNewDay_Aircraft(Vehicle *v)
 
{
 
	int32 cost;
 

	
 
	if (!IsNormalAircraft(v)) return;
 

	
 
	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
 

	
 
	CheckOrders(v);
 

	
 
	CheckVehicleBreakdown(v);
 
	AgeVehicle(v);
 
	CheckIfAircraftNeedsService(v);
 

	
 
	if (v->vehstatus & VS_STOPPED) return;
 

	
 
	cost = AircraftVehInfo(v->engine_type)->running_cost * _price.aircraft_running / 364;
 
	int32 cost = AircraftVehInfo(v->engine_type)->running_cost * _price.aircraft_running / 364;
 

	
 
	v->profit_this_year -= cost >> 8;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
 
	SubtractMoneyFromPlayerFract(v->owner, cost);
 

	
 
@@ -758,17 +730,13 @@ static void AgeAircraftCargo(Vehicle *v)
 
		v = v->next;
 
	} while (v != NULL);
 
}
 

	
 
static void HelicopterTickHandler(Vehicle *v)
 
{
 
	Vehicle *u;
 
	int tick,spd;
 
	SpriteID img;
 

	
 
	u = v->next->next;
 
	Vehicle *u = v->next->next;
 

	
 
	if (u->vehstatus & VS_HIDDEN) return;
 

	
 
	// if true, helicopter rotors do not rotate. This should only be the case if a helicopter is
 
	// loading/unloading at a terminal or stopped
 
	if (v->current_order.type == OT_LOADING || (v->vehstatus & VS_STOPPED)) {
 
@@ -783,15 +751,16 @@ static void HelicopterTickHandler(Vehicl
 
			u->cur_speed = 0x70;
 

	
 
		if (u->cur_speed >= 0x50)
 
			u->cur_speed--;
 
	}
 

	
 
	tick = ++u->tick_counter;
 
	spd = u->cur_speed >> 4;
 
	int tick = ++u->tick_counter;
 
	int spd = u->cur_speed >> 4;
 

	
 
	SpriteID img;
 
	if (spd == 0) {
 
		u->u.air.state = HRS_ROTOR_STOPPED;
 
		img = GetRotorImage(v);
 
		if (u->cur_image == img) return;
 
	} else if (tick >= spd) {
 
		u->tick_counter = 0;
 
@@ -808,31 +777,27 @@ static void HelicopterTickHandler(Vehicl
 
	VehiclePositionChanged(u);
 
	EndVehicleMove(u);
 
}
 

	
 
static void SetAircraftPosition(Vehicle *v, int x, int y, int z)
 
{
 
	Vehicle *u;
 
	int safe_x;
 
	int safe_y;
 

	
 
	v->x_pos = x;
 
	v->y_pos = y;
 
	v->z_pos = z;
 

	
 
	v->cur_image = GetAircraftImage(v, v->direction);
 
	if (v->subtype == AIR_HELICOPTER) v->next->next->cur_image = GetRotorImage(v);
 

	
 
	BeginVehicleMove(v);
 
	VehiclePositionChanged(v);
 
	EndVehicleMove(v);
 

	
 
	u = v->next;
 
	Vehicle *u = v->next;
 

	
 
	safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
 
	safe_y = clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
 
	int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
 
	int safe_y = clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
 
	u->x_pos = x;
 
	u->y_pos = y - ((v->z_pos-GetSlopeZ(safe_x, safe_y)) >> 3);;
 

	
 
	safe_y = clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
 
	u->z_pos = GetSlopeZ(safe_x, safe_y);
 
	u->cur_image = v->cur_image;
 
@@ -855,18 +820,16 @@ static void SetAircraftPosition(Vehicle 
 

	
 
/** Handle Aircraft specific tasks when a an Aircraft enters a hangar
 
 * @param *v Vehicle that enters the hangar
 
 */
 
void HandleAircraftEnterHangar(Vehicle *v)
 
{
 
	Vehicle *u;
 

	
 
	v->subspeed = 0;
 
	v->progress = 0;
 

	
 
	u = v->next;
 
	Vehicle *u = v->next;
 
	u->vehstatus |= VS_HIDDEN;
 
	u = u->next;
 
	if (u != NULL) {
 
		u->vehstatus |= VS_HIDDEN;
 
		u->cur_speed = 0;
 
	}
 
@@ -934,51 +897,45 @@ static byte GetAircraftFlyingAltitude(co
 
	byte base_altitude = 150;
 

	
 
	/* Make sure eastbound and westbound planes do not "crash" into each
 
	 * other by providing them with vertical seperation
 
	 */
 
	switch (v->direction) {
 
		case DIR_N: case DIR_NE: case DIR_E: case DIR_SE: base_altitude += 15; break;
 
		case DIR_N:
 
		case DIR_NE:
 
		case DIR_E:
 
		case DIR_SE:
 
			base_altitude += 15;
 
			break;
 

	
 
		default: break;
 
	}
 

	
 
	/* Make faster planes fly higher so that they can overtake slower ones */
 
	base_altitude += min(30 * (v->max_speed / 37), 90);
 

	
 
	return base_altitude;
 
}
 

	
 
static bool AircraftController(Vehicle *v)
 
{
 
	Station *st;
 
	Vehicle *u;
 
	byte z, maxz, curz;
 
	Direction newdir;
 
	GetNewVehiclePosResult gp;
 
	uint dist;
 
	int x,y;
 

	
 
	st = GetStation(v->u.air.targetairport);
 
	const Station *st = GetStation(v->u.air.targetairport);
 

	
 
	// prevent going to 0,0 if airport is deleted.
 
	{
 
		TileIndex tile = st->airport_tile;
 

	
 
		if (tile == 0) tile = st->xy;
 
		// xy of destination
 
		x = TileX(tile) * TILE_SIZE;
 
		y = TileY(tile) * TILE_SIZE;
 
	}
 
	TileIndex tile = st->airport_tile;
 
	if (tile == 0) tile = st->xy;
 
	int x = TileX(tile) * TILE_SIZE;
 
	int y = TileY(tile) * TILE_SIZE;
 

	
 
	// get airport moving data
 
	const AirportFTAClass *afc = st->Airport();
 
	const AirportMovingData *amd = afc->MovingData(v->u.air.pos);
 

	
 
	// Helicopter raise
 
	if (amd->flag & AMED_HELI_RAISE) {
 
		u = v->next->next;
 
		Vehicle *u = v->next->next;
 

	
 
		// Make sure the rotors don't rotate too fast
 
		if (u->cur_speed > 32) {
 
			v->cur_speed = 0;
 
			if (--u->cur_speed == 32) SndPlayVehicleFx(SND_18_HELICOPTER, v);
 
		} else {
 
@@ -1010,16 +967,16 @@ static bool AircraftController(Vehicle *
 
			}
 

	
 
			// Vehicle is now at the airport.
 
			v->tile = st->airport_tile;
 

	
 
			// Find altitude of landing position.
 
			z = GetSlopeZ(x, y) + 1 + afc->delta_z;
 
			uint z = GetSlopeZ(x, y) + 1 + afc->delta_z;
 

	
 
			if (z == v->z_pos) {
 
				u = v->next->next;
 
				Vehicle *u = v->next->next;
 

	
 
				// Increase speed of rotors. When speed is 80, we've landed.
 
				if (u->cur_speed >= 80) return true;
 
				u->cur_speed += 4;
 
			} else if (v->z_pos > z) {
 
				SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos-1);
 
@@ -1028,24 +985,22 @@ static bool AircraftController(Vehicle *
 
			}
 
		}
 
		return false;
 
	}
 

	
 
	// Get distance from destination pos to current pos.
 
	dist = myabs(x + amd->x - v->x_pos) +  myabs(y + amd->y - v->y_pos);
 
	uint dist = myabs(x + amd->x - v->x_pos) +  myabs(y + amd->y - v->y_pos);
 

	
 
	// Need exact position?
 
	if (!(amd->flag & AMED_EXACTPOS) && dist <= (amd->flag & AMED_SLOWTURN ? 8U : 4U))
 
		return true;
 

	
 
	// At final pos?
 
	if (dist == 0) {
 
		DirDiff dirdiff;
 

	
 
		// Change direction smoothly to final direction.
 
		dirdiff = DirDifference(amd->direction, v->direction);
 
		DirDiff dirdiff = DirDifference(amd->direction, v->direction);
 
		// if distance is 0, and plane points in right direction, no point in calling
 
		// UpdateAircraftSpeed(). So do it only afterwards
 
		if (dirdiff == DIRDIFF_SAME) {
 
			v->cur_speed = 0;
 
			return true;
 
		}
 
@@ -1061,49 +1016,48 @@ static bool AircraftController(Vehicle *
 

	
 
	if (!UpdateAircraftSpeed(v, ((amd->flag & AMED_NOSPDCLAMP) == 0) ? SPEED_LIMIT_TAXI : SPEED_LIMIT_NONE)) return false;
 

	
 
	if (v->load_unload_time_rem != 0) v->load_unload_time_rem--;
 

	
 
	// Turn. Do it slowly if in the air.
 
	newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
 
	Direction newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
 
	if (newdir != v->direction) {
 
		if (amd->flag & AMED_SLOWTURN) {
 
			if (v->load_unload_time_rem == 0) v->load_unload_time_rem = 8;
 
			v->direction = newdir;
 
		} else {
 
			v->cur_speed >>= 1;
 
			v->direction = newdir;
 
		}
 
	}
 

	
 
	// Move vehicle.
 
	GetNewVehiclePosResult gp;
 
	GetNewVehiclePos(v, &gp);
 
	v->tile = gp.new_tile;
 

	
 
	// If vehicle is in the air, use tile coordinate 0.
 
	if (amd->flag & (AMED_TAKEOFF | AMED_SLOWTURN | AMED_LAND)) v->tile = 0;
 

	
 
	// Adjust Z for land or takeoff?
 
	z = v->z_pos;
 
	uint z = v->z_pos;
 

	
 
	if (amd->flag & AMED_TAKEOFF) {
 
		z += 2;
 
		maxz = GetAircraftFlyingAltitude(v);
 
		if (z > maxz) z = maxz;
 
		z = min(z + 2, GetAircraftFlyingAltitude(v));
 
	}
 

	
 
	if (amd->flag & AMED_LAND) {
 
		if (st->airport_tile == 0) {
 
			v->u.air.state = FLYING;
 
			AircraftNextAirportPos_and_Order(v);
 
			// get aircraft back on running altitude
 
			SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
 
			return false;
 
		}
 

	
 
		curz = GetSlopeZ(x, y) + 1;
 
		uint curz = GetSlopeZ(x, y) + 1;
 

	
 
		if (curz > z) {
 
			z++;
 
		} else {
 
			int t = max(1U, dist - 4);
 

	
 
@@ -1111,13 +1065,13 @@ static bool AircraftController(Vehicle *
 
			if (z < curz) z = curz;
 
		}
 
	}
 

	
 
	// We've landed. Decrase speed when we're reaching end of runway.
 
	if (amd->flag & AMED_BRAKE) {
 
		curz = GetSlopeZ(x, y) + 1;
 
		uint curz = GetSlopeZ(x, y) + 1;
 

	
 
		if (z > curz) {
 
			z--;
 
		} else if (z < curz) {
 
			z++;
 
		}
 
@@ -1129,31 +1083,28 @@ static bool AircraftController(Vehicle *
 
	return false;
 
}
 

	
 

	
 
static void HandleCrashedAircraft(Vehicle *v)
 
{
 
	uint32 r;
 
	Station *st;
 
	int z;
 

	
 
	v->u.air.crashed_counter++;
 

	
 
	st = GetStation(v->u.air.targetairport);
 
	Station *st = GetStation(v->u.air.targetairport);
 

	
 
	// make aircraft crash down to the ground
 
	if (v->u.air.crashed_counter < 500 && st->airport_tile==0 && ((v->u.air.crashed_counter % 3) == 0) ) {
 
		z = GetSlopeZ(v->x_pos, v->y_pos);
 
		uint z = GetSlopeZ(v->x_pos, v->y_pos);
 
		v->z_pos -= 1;
 
		if (v->z_pos == z) {
 
			v->u.air.crashed_counter = 500;
 
			v->z_pos++;
 
		}
 
	}
 

	
 
	if (v->u.air.crashed_counter < 650) {
 
		uint32 r;
 
		if (CHANCE16R(1,32,r)) {
 
			static const DirDiff delta[] = {
 
				DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
 
			};
 

	
 
			v->direction = ChangeDir(v->direction, delta[GB(r, 16, 2)]);
 
@@ -1229,14 +1180,12 @@ static void HandleAircraftSmoke(Vehicle 
 
		);
 
	}
 
}
 

	
 
static void ProcessAircraftOrder(Vehicle *v)
 
{
 
	const Order *order;
 

	
 
	switch (v->current_order.type) {
 
		case OT_GOTO_DEPOT:
 
			if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
 
			if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
 
					!VehicleNeedsService(v)) {
 
				v->cur_order_index++;
 
@@ -1247,13 +1196,13 @@ static void ProcessAircraftOrder(Vehicle
 

	
 
		default: break;
 
	}
 

	
 
	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
 

	
 
	order = GetVehicleOrder(v, v->cur_order_index);
 
	const Order *order = GetVehicleOrder(v, v->cur_order_index);
 

	
 
	if (order == NULL) {
 
		v->current_order.type = OT_NOTHING;
 
		v->current_order.flags = 0;
 
		return;
 
	}
 
@@ -1322,30 +1271,27 @@ static void HandleAircraftLoading(Vehicl
 
	v->cur_order_index++;
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
static void CrashAirplane(Vehicle *v)
 
{
 
	uint16 amt;
 
	Station *st;
 
	StringID newsitem;
 

	
 
	v->vehstatus |= VS_CRASHED;
 
	v->u.air.crashed_counter = 0;
 

	
 
	CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
 

	
 
	InvalidateWindow(WC_VEHICLE_VIEW, v->index);
 

	
 
	amt = 2;
 
	uint amt = 2;
 
	if (v->cargo_type == CT_PASSENGERS) amt += v->cargo_count;
 
	SetDParam(0, amt);
 

	
 
	v->cargo_count = 0;
 
	v->next->cargo_count = 0;
 
	st = GetStation(v->u.air.targetairport);
 
	const Station *st = GetStation(v->u.air.targetairport);
 
	StringID newsitem;
 
	if (st->airport_tile == 0) {
 
		newsitem = STR_PLANE_CRASH_OUT_OF_FUEL;
 
	} else {
 
		SetDParam(1, st->index);
 
		newsitem = STR_A034_PLANE_CRASH_DIE_IN_FIREBALL;
 
	}
 
@@ -1358,46 +1304,39 @@ static void CrashAirplane(Vehicle *v)
 

	
 
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
 
}
 

	
 
static void MaybeCrashAirplane(Vehicle *v)
 
{
 
	Station *st;
 
	uint16 prob;
 
	uint i;
 

	
 
	st = GetStation(v->u.air.targetairport);
 
	Station *st = GetStation(v->u.air.targetairport);
 

	
 
	//FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports
 
	prob = 0x10000 / 1500;
 
	uint16 prob = 0x10000 / 1500;
 
	if (st->Airport()->flags & AirportFTAClass::SHORT_STRIP &&
 
			AircraftVehInfo(v->engine_type)->subtype & AIR_FAST &&
 
			!_cheats.no_jetcrash.value) {
 
		prob = 0x10000 / 20;
 
	}
 

	
 
	if (GB(Random(), 0, 16) > prob) return;
 

	
 
	// Crash the airplane. Remove all goods stored at the station.
 
	for (i = 0; i != NUM_CARGO; i++) {
 
	for (uint i = 0; i != NUM_CARGO; i++) {
 
		st->goods[i].rating = 1;
 
		SB(st->goods[i].waiting_acceptance, 0, 12, 0);
 
	}
 

	
 
	CrashAirplane(v);
 
}
 

	
 
// we've landed and just arrived at a terminal
 
static void AircraftEntersTerminal(Vehicle *v)
 
{
 
	Station *st;
 
	Order old_order;
 

	
 
	if (v->current_order.type == OT_GOTO_DEPOT) return;
 

	
 
	st = GetStation(v->u.air.targetairport);
 
	Station *st = GetStation(v->u.air.targetairport);
 
	v->last_station_visited = v->u.air.targetairport;
 

	
 
	/* Check if station was ever visited before */
 
	if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) {
 
		uint32 flags;
 

	
 
@@ -1409,13 +1348,13 @@ static void AircraftEntersTerminal(Vehic
 
			STR_A033_CITIZENS_CELEBRATE_FIRST,
 
			flags,
 
			v->index,
 
			0);
 
	}
 

	
 
	old_order = v->current_order;
 
	Order old_order = v->current_order;
 
	v->BeginLoading();
 
	v->current_order.flags = 0;
 

	
 
	if (old_order.type == OT_GOTO_STATION &&
 
			v->current_order.dest == v->last_station_visited) {
 
		v->current_order.flags =
 
@@ -1622,13 +1561,14 @@ static void AircraftEventHandler_AtTermi
 

	
 
static void AircraftEventHandler_General(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	assert("OK, you shouldn't be here, check your Airport Scheme!" && 0);
 
}
 

	
 
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *apc) {
 
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	PlayAircraftSound(v); // play takeoffsound for airplanes
 
	v->u.air.state = STARTTAKEOFF;
 
}
 

	
 
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *apc)
 
{
 
@@ -1663,34 +1603,30 @@ static void AircraftEventHandler_HeliTak
 
		_current_player = OWNER_NONE;
 
	}
 
}
 

	
 
static void AircraftEventHandler_Flying(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	Station *st;
 
	byte landingtype;
 
	AirportFTA *current;
 
	uint16 tcur_speed, tsubspeed;
 
	Station *st = GetStation(v->u.air.targetairport);
 

	
 
	st = GetStation(v->u.air.targetairport);
 
	// runway busy or not allowed to use this airstation, circle
 
	if (apc->flags & (v->subtype == AIR_HELICOPTER ? AirportFTAClass::HELICOPTERS : AirportFTAClass::AIRPLANES) &&
 
			st->airport_tile != 0 &&
 
			(st->owner == OWNER_NONE || st->owner == v->owner)) {
 
		// {32,FLYING,NOTHING_block,37}, {32,LANDING,N,33}, {32,HELILANDING,N,41},
 
		// if it is an airplane, look for LANDING, for helicopter HELILANDING
 
		// it is possible to choose from multiple landing runways, so loop until a free one is found
 
		landingtype = (v->subtype == AIR_HELICOPTER) ? HELILANDING : LANDING;
 
		current = apc->layout[v->u.air.pos].next;
 
		byte landingtype = (v->subtype == AIR_HELICOPTER) ? HELILANDING : LANDING;
 
		const AirportFTA *current = apc->layout[v->u.air.pos].next;
 
		while (current != NULL) {
 
			if (current->heading == landingtype) {
 
				// save speed before, since if AirportHasBlock is false, it resets them to 0
 
				// we don't want that for plane in air
 
				// hack for speed thingie
 
				tcur_speed = v->cur_speed;
 
				tsubspeed = v->subspeed;
 
				uint16 tcur_speed = v->cur_speed;
 
				uint16 tsubspeed = v->subspeed;
 
				if (!AirportHasBlock(v, current, apc)) {
 
					v->u.air.state = landingtype; // LANDING / HELILANDING
 
					// it's a bit dirty, but I need to set position to next position, otherwise
 
					// if there are multiple runways, plane won't know which one it took (because
 
					// they all have heading LANDING). And also occupy that block!
 
					v->u.air.pos = current->next_position;
 
@@ -1706,18 +1642,18 @@ static void AircraftEventHandler_Flying(
 
	v->u.air.state = FLYING;
 
	v->u.air.pos = apc->layout[v->u.air.pos].next_position;
 
}
 

	
 
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	const Player* p = GetPlayer(v->owner);
 
	AircraftLandAirplane(v);  // maybe crash airplane
 
	v->u.air.state = ENDLANDING;
 
	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
 
	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
 
		// only the vehicle owner needs to calculate the rest (locally)
 
		const Player* p = GetPlayer(v->owner);
 
		if (EngineHasReplacementForPlayer(p, v->engine_type) ||
 
			(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
 
			// send the aircraft to the hangar at next airport
 
			_current_player = _local_player;
 
			DoCommandP(v->tile, v->index, DEPOT_SERVICE, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
 
			_current_player = OWNER_NONE;
 
@@ -1811,25 +1747,22 @@ static void AirportGoToNextPosition(Vehi
 
	AirportMove(v, apc); // move aircraft to next position
 
}
 

	
 
// gets pos from vehicle and next orders
 
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	AirportFTA *current;
 
	byte prev_pos;
 

	
 
	// error handling
 
	if (v->u.air.pos >= apc->nofelements) {
 
		DEBUG(misc, 0, "[Ap] position %d is not valid for current airport. Max position is %d", v->u.air.pos, apc->nofelements-1);
 
		assert(v->u.air.pos < apc->nofelements);
 
	}
 

	
 
	current = &apc->layout[v->u.air.pos];
 
	const AirportFTA *current = &apc->layout[v->u.air.pos];
 
	// we have arrived in an important state (eg terminal, hangar, etc.)
 
	if (current->heading == v->u.air.state) {
 
		prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand
 
		byte prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand
 
		_aircraft_state_handlers[v->u.air.state](v, apc);
 
		if (v->u.air.state != FLYING) v->u.air.previous_pos = prev_pos;
 
		return true;
 
	}
 

	
 
	v->u.air.previous_pos = v->u.air.pos; // save previous location
 
@@ -1882,24 +1815,23 @@ static bool AirportHasBlock(Vehicle *v, 
 
		}
 
	}
 
	return false;
 
}
 

	
 
// returns true on success. Eg, next block was free and we have occupied it
 
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc)
 
static bool AirportSetBlocks(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
 
{
 
	AirportFTA *next = &apc->layout[current_pos->next_position];
 
	AirportFTA *reference = &apc->layout[v->u.air.pos];
 
	const AirportFTA *next = &apc->layout[current_pos->next_position];
 
	const AirportFTA *reference = &apc->layout[v->u.air.pos];
 

	
 
	// if the next position is in another block, check it and wait until it is free
 
	if ((apc->layout[current_pos->position].block & next->block) != next->block) {
 
		uint64 airport_flags = next->block;
 
		Station* st = GetStation(v->u.air.targetairport);
 
		//search for all all elements in the list with the same state, and blocks != N
 
		// this means more blocks should be checked/set
 
			AirportFTA *current = current_pos;
 
		const AirportFTA *current = current_pos;
 
		if (current == reference) current = current->next;
 
		while (current != NULL) {
 
			if (current->heading == current_pos->heading && current->block != 0) {
 
				airport_flags |= current->block;
 
				break;
 
			}
 
@@ -1907,12 +1839,13 @@ static bool AirportSetBlocks(Vehicle *v,
 
		};
 

	
 
		// if the block to be checked is in the next position, then exclude that from
 
		// checking, because it has been set by the airplane before
 
		if (current_pos->block == next->block) airport_flags ^= next->block;
 

	
 
		Station* st = GetStation(v->u.air.targetairport);
 
		if (HASBITS(st->airport_flags, airport_flags)) {
 
			v->cur_speed = 0;
 
			v->subspeed = 0;
 
			return false;
 
		}
 

	
 
@@ -1937,15 +1870,14 @@ static bool FreeTerminal(Vehicle *v, byt
 
	return false;
 
}
 

	
 
static uint GetNumTerminals(const AirportFTAClass *apc)
 
{
 
	uint num = 0;
 
	uint i;
 

	
 
	for (i = apc->terminals[0]; i > 0; i--) num += apc->terminals[i];
 
	for (uint i = apc->terminals[0]; i > 0; i--) num += apc->terminals[i];
 

	
 
	return num;
 
}
 

	
 
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc)
 
{
 
@@ -1957,34 +1889,31 @@ static bool AirportFindFreeTerminal(Vehi
 
	 * First in line is checked first, group 0. If the block (TERM_GROUP1_block) is free, it
 
	 * looks at the corresponding terminals of that group. If no free ones are found, other
 
	 * possible groups are checked (in this case group 1, since that is after group 0). If that
 
	 * fails, then attempt fails and plane waits
 
	 */
 
	if (apc->terminals[0] > 1) {
 
		Station *st = GetStation(v->u.air.targetairport);
 
		AirportFTA *temp = apc->layout[v->u.air.pos].next;
 
		const Station *st = GetStation(v->u.air.targetairport);
 
		const AirportFTA *temp = apc->layout[v->u.air.pos].next;
 

	
 
		while (temp != NULL) {
 
			if (temp->heading == 255) {
 
				if (!HASBITS(st->airport_flags, temp->block)) {
 
					int target_group;
 
					int i;
 
					int group_start = 0;
 
					int group_end;
 

	
 
					//read which group do we want to go to?
 
					//(the first free group)
 
					target_group = temp->next_position + 1;
 
					uint target_group = temp->next_position + 1;
 

	
 
					//at what terminal does the group start?
 
					//that means, sum up all terminals of
 
					//groups with lower number
 
					for (i = 1; i < target_group; i++)
 
					uint group_start = 0;
 
					for (uint i = 1; i < target_group; i++) {
 
						group_start += apc->terminals[i];
 
					}
 

	
 
					group_end = group_start + apc->terminals[target_group];
 
					uint group_end = group_start + apc->terminals[target_group];
 
					if (FreeTerminal(v, group_start, group_end)) return true;
 
				}
 
			} else {
 
				/* once the heading isn't 255, we've exhausted the possible blocks.
 
				 * So we cannot move */
 
				return false;
 
@@ -1997,15 +1926,14 @@ static bool AirportFindFreeTerminal(Vehi
 
	return FreeTerminal(v, 0, GetNumTerminals(apc));
 
}
 

	
 
static uint GetNumHelipads(const AirportFTAClass *apc)
 
{
 
	uint num = 0;
 
	uint i;
 

	
 
	for (i = apc->helipads[0]; i > 0; i--) num += apc->helipads[i];
 
	for (uint i = apc->helipads[0]; i > 0; i--) num += apc->helipads[i];
 

	
 
	return num;
 
}
 

	
 

	
 
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc)
 
@@ -2018,28 +1946,26 @@ static bool AirportFindFreeHelipad(Vehic
 
		const Station* st = GetStation(v->u.air.targetairport);
 
		const AirportFTA* temp = apc->layout[v->u.air.pos].next;
 

	
 
		while (temp != NULL) {
 
			if (temp->heading == 255) {
 
				if (!HASBITS(st->airport_flags, temp->block)) {
 
					int target_group;
 
					int i;
 
					int group_start = 0;
 
					int group_end;
 

	
 
					//read which group do we want to go to?
 
					//(the first free group)
 
					target_group = temp->next_position + 1;
 
					uint target_group = temp->next_position + 1;
 

	
 
					//at what terminal does the group start?
 
					//that means, sum up all terminals of
 
					//groups with lower number
 
					for (i = 1; i < target_group; i++)
 
					uint group_start = 0;
 
					for (uint i = 1; i < target_group; i++) {
 
						group_start += apc->helipads[i];
 
					}
 

	
 
					group_end = group_start + apc->helipads[target_group];
 
					uint group_end = group_start + apc->helipads[target_group];
 
					if (FreeTerminal(v, group_start, group_end)) return true;
 
				}
 
			} else {
 
				/* once the heading isn't 255, we've exhausted the possible blocks.
 
				 * So we cannot move */
 
				return false;
 
@@ -2082,40 +2008,36 @@ static void AircraftEventHandler(Vehicle
 

	
 
	AirportGoToNextPosition(v, GetStation(v->u.air.targetairport)->Airport());
 
}
 

	
 
void Aircraft_Tick(Vehicle *v)
 
{
 
	int i;
 

	
 
	if (!IsNormalAircraft(v)) return;
 

	
 
	if (v->subtype == AIR_HELICOPTER) HelicopterTickHandler(v);
 

	
 
	AgeAircraftCargo(v);
 

	
 
	for (i = 0; i != 6; i++) {
 
	for (uint i = 0; i != 6; i++) {
 
		AircraftEventHandler(v, i);
 
		if (v->type != VEH_Aircraft) // In case it was deleted
 
			break;
 
	}
 
}
 

	
 

	
 
// need to be called to load aircraft from old version
 
void UpdateOldAircraft(void)
 
{
 
	// set airport_flags to 0 for all airports just to be sure
 
	Station *st;
 
	Vehicle *v_oldstyle;
 
	GetNewVehiclePosResult gp;
 

	
 
	// set airport_flags to 0 for all airports just to be sure
 
	FOR_ALL_STATIONS(st) {
 
		st->airport_flags = 0; // reset airport
 
	}
 

	
 
	Vehicle *v_oldstyle;
 
	FOR_ALL_VEHICLES(v_oldstyle) {
 
	// airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
 
	// skip those
 
		if (v_oldstyle->type == VEH_Aircraft && IsNormalAircraft(v_oldstyle)) {
 
			// airplane in terminal stopped doesn't hurt anyone, so goto next
 
			if (v_oldstyle->vehstatus & VS_STOPPED && v_oldstyle->u.air.state == 0) {
 
@@ -2124,52 +2046,52 @@ void UpdateOldAircraft(void)
 
			}
 

	
 
			AircraftLeaveHangar(v_oldstyle); // make airplane visible if it was in a depot for example
 
			v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving
 
			v_oldstyle->u.air.state = FLYING;
 
			AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport
 
			GetNewVehiclePosResult gp;
 
			GetNewVehiclePos(v_oldstyle, &gp); // get the position of the plane (to be used for setting)
 
			v_oldstyle->tile = 0; // aircraft in air is tile=0
 

	
 
			// correct speed of helicopter-rotors
 
			if (v_oldstyle->subtype == AIR_HELICOPTER) v_oldstyle->next->next->cur_speed = 32;
 

	
 
			// set new position x,y,z
 
			SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle));
 
		}
 
	}
 
}
 

	
 
void UpdateAirplanesOnNewStation(Station *st)
 
void UpdateAirplanesOnNewStation(const Station *st)
 
{
 
	GetNewVehiclePosResult gp;
 
	Vehicle *v;
 
	byte takeofftype;
 
	uint16 cnt;
 
	// only 1 station is updated per function call, so it is enough to get entry_point once
 
	const AirportFTAClass *ap = st->Airport();
 

	
 
	Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_Aircraft && IsNormalAircraft(v)) {
 
			if (v->u.air.targetairport == st->index) { // if heading to this airport
 
				/* update position of airplane. If plane is not flying, landing, or taking off
 
				 *you cannot delete airport, so it doesn't matter
 
				 */
 
				if (v->u.air.state >= FLYING) { // circle around
 
					v->u.air.pos = v->u.air.previous_pos = AircraftGetEntryPoint(v, ap);
 
					v->u.air.state = FLYING;
 
					// landing plane needs to be reset to flying height (only if in pause mode upgrade,
 
					// in normal mode, plane is reset in AircraftController. It doesn't hurt for FLYING
 
					GetNewVehiclePosResult gp;
 
					GetNewVehiclePos(v, &gp);
 
					// set new position x,y,z
 
					SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
 
				} else {
 
					assert(v->u.air.state == ENDTAKEOFF || v->u.air.state == HELITAKEOFF);
 
					takeofftype = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : ENDTAKEOFF;
 
					byte takeofftype = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : ENDTAKEOFF;
 
					// search in airportdata for that heading
 
					// easiest to do, since this doesn't happen a lot
 
					for (cnt = 0; cnt < ap->nofelements; cnt++) {
 
					for (uint cnt = 0; cnt < ap->nofelements; cnt++) {
 
						if (ap->layout[cnt].heading == takeofftype) {
 
							v->u.air.pos = ap->layout[cnt].position;
 
							break;
 
						}
 
					}
 
				}
src/airport.cpp
Show inline comments
 
@@ -285,17 +285,16 @@ AirportFTAClass::~AirportFTAClass()
 

	
 
/** Get the number of elements of a source Airport state automata
 
 * Since it is actually just a big array of AirportFTA types, we only
 
 * know one element from the other by differing 'position' identifiers */
 
static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA)
 
{
 
	int i;
 
	uint16 nofelements = 0;
 
	int temp = apFA[0].position;
 

	
 
	for (i = 0; i < MAX_ELEMENTS; i++) {
 
	for (uint i = 0; i < MAX_ELEMENTS; i++) {
 
		if (temp != apFA[i].position) {
 
			nofelements++;
 
			temp = apFA[i].position;
 
		}
 
		if (apFA[i].position == MAX_ELEMENTS) break;
 
	}
 
@@ -304,18 +303,17 @@ static uint16 AirportGetNofElements(cons
 

	
 
/* We calculate the terminal/helipod count based on the data passed to us
 
 * This data (terminals) contains an index as a first element as to how many
 
 * groups there are, and then the number of terminals for each group */
 
static byte AirportGetTerminalCount(const byte *terminals, byte *groups)
 
{
 
	byte i;
 
	byte nof_terminals = 0;
 
	*groups = 0;
 

	
 
	if (terminals != NULL) {
 
		i = terminals[0];
 
		uint i = terminals[0];
 
		*groups = i;
 
		while (i-- > 0) {
 
			terminals++;
 
			assert(*terminals != 0); // no empty groups please
 
			nof_terminals += *terminals;
 
		}
 
@@ -323,18 +321,17 @@ static byte AirportGetTerminalCount(cons
 
	return nof_terminals;
 
}
 

	
 

	
 
static AirportFTA* AirportBuildAutomata(uint nofelements, const AirportFTAbuildup *apFA)
 
{
 
	AirportFTA *current;
 
	AirportFTA *FAutomata = MallocT<AirportFTA>(nofelements);
 
	uint16 internalcounter = 0;
 

	
 
	for (uint i = 0; i < nofelements; i++) {
 
		current = &FAutomata[i];
 
		AirportFTA *current = &FAutomata[i];
 
		current->position      = apFA[internalcounter].position;
 
		current->heading       = apFA[internalcounter].heading;
 
		current->block         = apFA[internalcounter].block;
 
		current->next_position = apFA[internalcounter].next;
 

	
 
		// outgoing nodes from the same position, create linked list
 
@@ -346,13 +343,13 @@ static AirportFTA* AirportBuildAutomata(
 
			newNode->block         = apFA[internalcounter + 1].block;
 
			newNode->next_position = apFA[internalcounter + 1].next;
 
			// create link
 
			current->next = newNode;
 
			current = current->next;
 
			internalcounter++;
 
		} // while
 
		}
 
		current->next = NULL;
 
		internalcounter++;
 
	}
 
	return FAutomata;
 
}
 

	
 
@@ -362,15 +359,14 @@ static byte AirportTestFTA(uint nofeleme
 
	uint next_position = 0;
 

	
 
	for (uint i = 0; i < nofelements; i++) {
 
		uint position = layout[i].position;
 
		if (position != next_position) return i;
 
		const AirportFTA *first = &layout[i];
 
		const AirportFTA *current = first;
 

	
 
		for (; current != NULL; current = current->next) {
 
		for (const AirportFTA *current = first; current != NULL; current = current->next) {
 
			/* A heading must always be valid. The only exceptions are
 
			 * - multiple choices as start, identified by a special value of 255
 
			 * - terminal group which is identified by a special value of 255 */
 
			if (current->heading > MAX_HEADINGS) {
 
				if (current->heading != 255) return i;
 
				if (current == first && current->next == NULL) return i;
 
@@ -431,15 +427,13 @@ static uint AirportBlockToString(uint32 
 

	
 
static void AirportPrintOut(uint nofelements, const AirportFTA *layout, bool full_report)
 
{
 
	if (!full_report) printf("(P = Current Position; NP = Next Position)\n");
 

	
 
	for (uint i = 0; i < nofelements; i++) {
 
		const AirportFTA *current = &layout[i];
 

	
 
		for (; current != NULL; current = current->next) {
 
		for (const AirportFTA *current = &layout[i]; current != NULL; current = current->next) {
 
			if (full_report) {
 
				byte heading = (current->heading == 255) ? MAX_HEADINGS + 1 : current->heading;
 
				printf("\tPos:%2d NPos:%2d Heading:%15s Block:%2d\n", current->position,
 
					    current->next_position, _airport_heading_strings[heading],
 
							AirportBlockToString(current->block));
 
			} else {
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -502,13 +502,13 @@ static int DrawAircraftPurchaseInfo(int 
 
		SetDParam(1, avi->mail_capacity);
 
		DrawString(x, y, STR_PURCHASE_INFO_AIRCRAFT_CAPACITY, 0);
 
	} else {
 
		/* Note, if the default capacity is selected by the refit capacity
 
		* callback, then the capacity shown is likely to be incorrect. */
 
		SetDParam(0, cargo);
 
		SetDParam(1, AircraftDefaultCargoCapacity(cargo, engine_number));
 
		SetDParam(1, AircraftDefaultCargoCapacity(cargo, avi));
 
		SetDParam(2, STR_9842_REFITTABLE);
 
		DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, 0);
 
	}
 
	y += 10;
 

	
 
	/* Running cost */
src/station_cmd.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/** @file station_cmd.c */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "aircraft.h"
 
#include "bridge_map.h"
 
#include "debug.h"
 
#include "functions.h"
 
#include "station_map.h"
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
@@ -69,15 +70,12 @@ static void RoadStopPoolNewBlock(uint st
 
}
 

	
 
DEFINE_OLD_POOL(Station, Station, StationPoolNewBlock, StationPoolCleanBlock)
 
DEFINE_OLD_POOL(RoadStop, RoadStop, RoadStopPoolNewBlock, NULL)
 

	
 

	
 
extern void UpdateAirplanesOnNewStation(Station *st);
 

	
 

	
 
RoadStop* GetRoadStopByTile(TileIndex tile, RoadStop::Type type)
 
{
 
	const Station* st = GetStationByTile(tile);
 

	
 
	for (RoadStop *rs = st->GetPrimaryRoadStop(type);; rs = rs->next) {
 
		if (rs->xy == tile) return rs;
0 comments (0 inline, 0 general)