File diff r1976:fcef181ed14a → r1977:1f8b99c96041
ai.c
Show inline comments
 
@@ -260,114 +260,117 @@ static void AiHandleGotoDepot(Player *p,
 
		return;
 
	}
 

	
 
	if (p->ai.cur_veh->current_order.type == OT_GOTO_DEPOT) {
 
		p->ai.cur_veh->current_order.type = OT_DUMMY;
 
		p->ai.cur_veh->current_order.flags = 0;
 
		InvalidateWindow(WC_VEHICLE_VIEW, p->ai.cur_veh->index);
 
	}
 
}
 

	
 
static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
 
{
 
	int i;
 

	
 
	for (i = 0; bak->order[i].type != OT_NOTHING; i++)
 
		if (CmdFailed(DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)))
 
			break;
 
}
 

	
 
static void AiHandleReplaceTrain(Player *p)
 
{
 
	Vehicle *v = p->ai.cur_veh;
 
	BackuppedOrders orderbak[1];
 
	int veh;
 
	uint tile;
 

	
 
	// wait until the vehicle reaches the depot.
 
	if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
 
		AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT);
 
		return;
 
	}
 

	
 
	veh = AiChooseTrainToReplaceWith(p, v);
 
	if (veh != -1) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		tile = v->tile;
 

	
 
		if (!CmdFailed(DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
 
			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE)) ) {
 
			veh = _new_train_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
 

	
 
			DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
 
		}
 
	}
 
}
 

	
 
static void AiHandleReplaceRoadVeh(Player *p)
 
{
 
	Vehicle *v = p->ai.cur_veh;
 
	BackuppedOrders orderbak[1];
 
	int veh;
 
	uint tile;
 

	
 
	if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
 
		AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
 
		return;
 
	}
 

	
 
	veh = AiChooseRoadVehToReplaceWith(p, v);
 
	if (veh != -1) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		tile = v->tile;
 

	
 
		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
 
			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) {
 
			veh = _new_roadveh_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
 

	
 
			DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
 
		}
 
	}
 
}
 

	
 
static void AiHandleReplaceAircraft(Player *p)
 
{
 
	Vehicle *v = p->ai.cur_veh;
 
	int veh;
 
	BackuppedOrders orderbak[1];
 
	uint tile;
 

	
 
	if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
 
		AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
 
		return;
 
	}
 

	
 
	veh = AiChooseAircraftToReplaceWith(p, v);
 
	if (veh != -1) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		tile = v->tile;
 

	
 
		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
 
			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT)) ) {
 
			veh = _new_aircraft_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
 

	
 
			DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
 
		}
 
	}
 
}
 

	
 
static void AiHandleReplaceShip(Player *p)
 
{
 
	error("!AiHandleReplaceShip");
 
}
 

	
 
typedef int CheckReplaceProc(Player *p, Vehicle *v);
 

	
 
static CheckReplaceProc * const _veh_check_replace_proc[] = {
 
	AiChooseTrainToReplaceWith,
 
	AiChooseRoadVehToReplaceWith,
 
@@ -1511,53 +1514,52 @@ static void AiStateWantNewRoute(Player *
 
		} else if (r < 0xD89B) {
 
			if (_patches.ai_disable_veh_aircraft) continue;
 
			AiWantAircraftRoute(p);
 
		} else {
 
			if (_patches.ai_disable_veh_ship) continue;
 
			AiWantShipRoute(p);
 
		}
 

	
 
		// got a route?
 
		if (p->ai.state != AIS_WANT_NEW_ROUTE)
 
			break;
 

	
 
		// time out?
 
		if (--i == 0) {
 
			if (++p->ai.state_counter == 556) p->ai.state = AIS_0;
 
			break;
 
		}
 
	}
 
}
 

	
 
static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
 
{
 
	uint values[NUM_CARGO];
 
	int w,h;
 
	uint tile2;
 
	int rad;
 

	
 
	for(;p->mode != 4;p++) if (p->mode == 1) {
 
		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 
		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 

	
 
		w = ((p->attr>>1) & 7);
 
		h = ((p->attr>>4) & 7);
 
		if (p->attr&1) intswap(w, h);
 

	
 

	
 
		if (_patches.modified_catchment) {
 
			rad = CA_TRAIN;
 
		} else {
 
			rad = 4;
 
		}
 

	
 
		if (cargo & 0x80) {
 
			GetProductionAroundTiles(values, tile2, w, h, rad);
 
			return values[cargo & 0x7F] != 0;
 
		} else {
 
			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
 
			if (!(values[cargo] & ~7))
 
				return false;
 
			if (cargo != CT_MAIL)
 
				return true;
 
			return !!((values[cargo]>>1) & ~7);
 
		}
 
	}
 
@@ -1812,49 +1814,49 @@ static void AiStateBuildDefaultRailBlock
 
			return;
 
	} while (++aib,--j);
 

	
 
	// yep, all are done. switch state to the rail building state.
 
	p->ai.state = AIS_BUILD_RAIL;
 
	p->ai.state_mode = 255;
 
}
 

	
 
static TileIndex AiGetEdgeOfDefaultRailBlock(byte rule, TileIndex tile, byte cmd, int *dir)
 
{
 
	const AiDefaultBlockData *p = _default_rail_track_data[rule]->data;
 

	
 
	while (p->mode != 3 || !((--cmd) & 0x80)) p++;
 

	
 
	return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDir(*dir = p->attr);
 
}
 

	
 
typedef struct AiRailPathFindData {
 
	TileIndex tile;
 
	TileIndex tile2;
 
	int count;
 
	bool flag;
 
} AiRailPathFindData;
 

	
 
static bool AiEnumFollowTrack(uint tile, AiRailPathFindData *a, int track, uint length, byte *state)
 
static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
 
{
 
	if (a->flag)
 
		return true;
 

	
 
	if (length > 20 || tile == a->tile) {
 
		a->flag = true;
 
		return true;
 
	}
 

	
 
	if (DistanceMax(tile, a->tile2) < 4)
 
		a->count++;
 

	
 
	return false;
 
}
 

	
 
static bool AiDoFollowTrack(Player *p)
 
{
 
	AiRailPathFindData arpfd;
 
	arpfd.tile = p->ai.start_tile_a;
 
	arpfd.tile2 = p->ai.cur_tile_a;
 
	arpfd.flag = false;
 
	arpfd.count = 0;
 
	FollowTrack(p->ai.cur_tile_a + TileOffsByDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2,
 
		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
 
@@ -2154,49 +2156,49 @@ static void AiBuildRailConstruct(Player 
 
	} else if (arf.best_ptr[0]&0x40) {
 
		// tunnel
 
		DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
 
		p->ai.cur_tile_a = _build_tunnel_endtile;
 
		p->ai.state_counter = 0;
 
	} else {
 
		// rail
 
		p->ai.cur_dir_a = arf.best_ptr[1];
 
		DoCommandByTile(p->ai.cur_tile_a, p->ai.railtype_to_use, arf.best_ptr[0],
 
			DC_EXEC | DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL);
 
		p->ai.state_counter = 0;
 
	}
 

	
 
	if (arf.best_tile != 0) {
 
		for(i=0; i!=2; i++)
 
			AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0);
 
	}
 
}
 

	
 
static bool AiRemoveTileAndGoForward(Player *p)
 
{
 
	byte b;
 
	int bit;
 
	const byte *ptr;
 
	uint tile = p->ai.cur_tile_a;
 
	TileIndex tile = p->ai.cur_tile_a;
 
	int offs;
 
	TileIndex tilenew;
 

	
 
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 
		if (!(_map5[tile] & 0x80)) {
 
			// Clear the tunnel and continue at the other side of it.
 
			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
 
				return false;
 
			p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a));
 
			return true;
 
		}
 

	
 
		if (!(_map5[tile] & 0x40)) {
 

	
 
			// Check if the bridge points in the right direction.
 
			// This is not really needed the first place AiRemoveTileAndGoForward is called.
 
			if ((_map5[tile]&1) != (p->ai.cur_dir_a&1))
 
				return false;
 

	
 
			// Find other side of bridge.
 
			offs = TileOffsByDir(p->ai.cur_dir_a);
 
			do {
 
				tile = TILE_MASK(tile - offs);
 
			} while (_map5[tile] & 0x40);
 
@@ -2473,49 +2475,50 @@ static void AiStateDeleteRailBlocks(Play
 
				DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 
				b++;
 
			}
 
		}
 
	} while (++aib,--num);
 

	
 
	p->ai.state = AIS_0;
 
}
 

	
 
static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
 
{
 
	uint values[NUM_CARGO];
 
	int rad;
 

	
 
	if (_patches.modified_catchment) {
 
		rad = CA_TRUCK;		//Same as CA_BUS at the moment?
 
	} else {			//change that at some point?
 
		rad = 4;
 
	}
 

	
 
	for(;;p++) {
 
		if (p->mode == 4) {
 
			return true;
 
		} else if (p->mode == 1) {
 
			uint tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 
			TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 

	
 
			if (cargo & 0x80) {
 
				GetProductionAroundTiles(values, tile2, 1, 1, rad);
 
				return values[cargo & 0x7F] != 0;
 
			} else {
 
				GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
 
				return (values[cargo]&~7) != 0;
 
			}
 
		}
 
	}
 
}
 

	
 
static bool _want_road_truck_station;
 
static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag);
 

	
 
// Returns rule and cost
 
static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, int32 *cost)
 
{
 
	int i;
 
	const AiDefaultRoadBlock *p;
 

	
 
	_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS;
 

	
 
	for(i=0; (p = _road_default_block_data[i]) != NULL; i++) {
 
		if (p->dir == direction) {
 
@@ -2744,108 +2747,108 @@ static bool AiCheckRoadPathBetter(AiRoad
 
			arf->best_dist = arf->cur_best_dist;
 
			arf->best_dir = arf->cur_best_dir;
 
			arf->best_ptr = p;
 
			arf->best_tile = arf->cur_best_tile;
 
			better = true;
 
		}
 
	} else if (arf->recursive_mode > 1) {
 
		// Mode is 2.
 
		if (arf->best_dist != 0 || arf->cur_best_depth < arf->best_depth) {
 
			arf->best_depth = arf->cur_best_depth;
 
			arf->best_dist = 0;
 
			arf->best_ptr = p;
 
			arf->best_tile = 0;
 
			better = true;
 
		}
 
	}
 
	arf->recursive_mode = 0;
 
	arf->cur_best_dist = (uint)-1;
 
	arf->cur_best_depth = 0xff;
 

	
 
	return better;
 
}
 

	
 

	
 
static bool AiEnumFollowRoad(uint tile, AiRoadEnum *a, int track, uint length, byte *state)
 
static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length, byte *state)
 
{
 
	uint dist = DistanceManhattan(tile, a->dest);
 
	uint tile2;
 

	
 
	if (dist <= a->best_dist) {
 
		tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
 
		TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
 

	
 
		if (IsTileType(tile2, MP_STREET) &&
 
				(_map5[tile2]&0xF0) == 0) {
 
			a->best_dist = dist;
 
			a->best_tile = tile;
 
			a->best_track = track;
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 
static const uint16 _ai_road_table_and[4] = {
 
	0x1009,
 
	0x16,
 
	0x520,
 
	0x2A00,
 
};
 

	
 
static bool AiCheckRoadFinished(Player *p)
 
{
 
	AiRoadEnum are;
 
	uint tile;
 
	TileIndex tile;
 
	int dir = p->ai.cur_dir_a;
 
	uint32 bits;
 
	int i;
 

	
 
	are.dest = p->ai.cur_tile_b;
 
	tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(dir));
 

	
 
	bits = GetTileTrackStatus(tile, TRANSPORT_ROAD) & _ai_road_table_and[dir];
 
	if (bits == 0) {
 
		return false;
 
	}
 

	
 
	are.best_dist = (uint)-1;
 

	
 
	for_each_bit(i, bits) {
 
		FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
 
	}
 

	
 
	if (DistanceManhattan(tile, are.dest) <= are.best_dist)
 
		return false;
 

	
 
	if (are.best_dist == 0)
 
		return true;
 

	
 
	p->ai.cur_tile_a = are.best_tile;
 
	p->ai.cur_dir_a = _dir_by_track[are.best_track];
 
	return false;
 
}
 

	
 

	
 
static bool AiBuildRoadHelper(uint tile, int flags, int type)
 
static bool AiBuildRoadHelper(TileIndex tile, int flags, int type)
 
{
 
	static const byte _road_bits[] = {
 
		8+2,
 
		1+4,
 
		1+8,
 
		4+2,
 
		1+2,
 
		8+4,
 
	};
 
	return !CmdFailed(DoCommandByTile(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
 
}
 

	
 
static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
 
{
 
	TileIndex tile_new;
 
	bool flag;
 

	
 
	int dir2 = p[0] & 3;
 

	
 
	FindLandscapeHeightByTile(&arf->ti, tile);
 
	if (arf->ti.tileh == _dir_table_1[dir2] || (arf->ti.tileh==0 && arf->ti.z!=0)) {
 
		tile_new = tile;
 
		// Allow bridges directly over bottom tiles
 
		flag = arf->ti.z == 0;
 
@@ -2941,49 +2944,49 @@ static void AiBuildRoadRecursive(AiRoadF
 
			}
 

	
 
			// At the bottom depth?
 
			if (arf->depth == 1) {
 
				AiCheckRoadPathBetter(arf, p);
 
			}
 

	
 
			p += 2;
 
		} while (!(p[0]&0x80));
 
	}
 

	
 
	AiCheckBuildRoadBridgeHere(arf, tile, p);
 
	AiCheckBuildRoadTunnelHere(arf, tile, p+1);
 

	
 
	arf->depth--;
 
}
 

	
 
int sw;
 

	
 

	
 
static void AiBuildRoadConstruct(Player *p)
 
{
 
	AiRoadFinder arf;
 
	int i;
 
	uint tile;
 
	TileIndex tile;
 

	
 
	// Reached destination?
 
	if (AiCheckRoadFinished(p)) {
 
		p->ai.state_mode = 255;
 
		return;
 
	}
 

	
 
	// Setup recursive finder and call it.
 
	arf.player = p;
 
	arf.final_tile = p->ai.cur_tile_b;
 
	arf.final_dir = p->ai.cur_dir_b;
 
	arf.depth = 0;
 
	arf.recursive_mode = 0;
 
	arf.best_ptr = NULL;
 
	arf.cur_best_dist = (uint)-1;
 
	arf.cur_best_depth = 0xff;
 
	arf.best_dist = (uint)-1;
 
	arf.best_depth =  0xff;
 
	arf.cur_best_tile = 0;
 
	arf.best_tile = 0;
 
	AiBuildRoadRecursive(&arf, p->ai.cur_tile_a, p->ai.cur_dir_a);
 

	
 
	// Reached destination?
 
	if (arf.recursive_mode == 2 && arf.cur_best_depth == 0) {
 
@@ -3143,49 +3146,50 @@ static void AiStateBuildRoad(Player *p)
 
	// Find second edge to build to
 
	aib = (&p->ai.src) + (cmd&0xF);
 
	tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir);
 
	p->ai.start_tile_b = tile;
 
	p->ai.cur_tile_b = tile;
 
	p->ai.start_dir_b = dir;
 
	p->ai.cur_dir_b = dir;
 

	
 
	// And setup state.
 
	p->ai.state_mode = 2;
 
	p->ai.state_counter = 0;
 
	p->ai.banned_tile_count = 0;
 
}
 

	
 
static int AiGetStationIdFromRoadBlock(TileIndex tile, int id)
 
{
 
	const AiDefaultBlockData *p = _road_default_block_data[id]->data;
 
	while (p->mode != 1) p++;
 
	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
 
}
 

	
 
static void AiStateBuildRoadVehicles(Player *p)
 
{
 
	const AiDefaultBlockData *ptr;
 
	uint tile,loco_id;
 
	TileIndex tile;
 
	uint loco_id;
 
	int veh, i;
 

	
 
	ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data;
 
	for(;ptr->mode != 0;ptr++) {}
 
	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
 

	
 
	veh = AiChooseRoadVehToBuild(p->ai.cargo_type, p->player_money, tile);
 
	if (veh == -1) {
 
		p->ai.state = AIS_0;
 
		return;
 
	}
 

	
 
	if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) return;
 

	
 
	loco_id = _new_roadveh_id;
 

	
 
	for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) {
 
		AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i];
 
		bool is_pass = (p->ai.cargo_type == CT_PASSENGERS ||
 
							p->ai.cargo_type == CT_MAIL ||
 
							(_opt.landscape==LT_NORMAL && p->ai.cargo_type == CT_VALUABLES));
 
		Order order;
 

	
 
		order.type = OT_GOTO_STATION;
 
@@ -3215,49 +3219,50 @@ static void AiStateDeleteRoadBlocks(Play
 
{
 
	int num;
 
	AiBuildRec *aib;
 
	const AiDefaultBlockData *b;
 

	
 
	num = p->ai.num_build_rec;
 
	aib = &p->ai.src;
 
	do {
 
		if (aib->cur_building_rule != 255) {
 
			b = _road_default_block_data[aib->cur_building_rule]->data;
 
			while (b->mode != 4) {
 
				if (b->mode <= 1) {
 
					DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 
				}
 
				b++;
 
			}
 
		}
 
	} while (++aib,--num);
 

	
 
	p->ai.state = AIS_0;
 
}
 

	
 
static bool AiCheckIfHangar(Station *st)
 
{
 
	uint tile = st->airport_tile;
 
	TileIndex tile = st->airport_tile;
 

	
 
	// HANGAR of airports
 
	// 0x20 - hangar large airport (32)
 
	// 0x41 - hangar small airport (65)
 
	return (_map5[tile] == 32 || _map5[tile] == 65);
 
}
 

	
 
static void AiStateAirportStuff(Player *p)
 
{
 
	Station *st;
 
	byte acc_planes;
 
	int i;
 
	AiBuildRec *aib;
 
	byte rule;
 

	
 
	// Here we look for an airport we could use instead of building a new
 
	// one. If we find such an aiport for any waypoint,
 
	// AiStateBuildDefaultAirportBlocks() will kindly skip that one when
 
	// building the waypoints.
 

	
 
	i = 0;
 
	do {
 
		// We do this all twice - once for the source (town in the case
 
		// of oilrig route) and then for the destination (oilrig in the
 
		// case of oilrig route).
 
@@ -3327,59 +3332,59 @@ static void AiStateAirportStuff(Player *
 
	p->ai.state = AIS_BUILD_DEFAULT_AIRPORT_BLOCKS;
 
	p->ai.state_mode = 255;
 
	p->ai.state_counter = 0;
 
}
 

	
 
static int32 AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
 
{
 
	int32 total_cost = 0, ret;
 

	
 
	for(;p->mode == 0;p++) {
 
		if (!HASBIT(_avail_aircraft, p->attr))
 
			return CMD_ERROR;
 
		ret = DoCommandByTile(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr,0,flag | DC_AUTO | DC_NO_WATER,CMD_BUILD_AIRPORT);
 
		if (CmdFailed(ret)) return CMD_ERROR;
 
		total_cost += ret;
 
	}
 

	
 
	return total_cost;
 
}
 

	
 
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
 
{
 
	uint values[NUM_CARGO];
 
	int w,h;
 
	uint tile2;
 
	int rad;
 

	
 
	if (_patches.modified_catchment) {
 
		rad = CA_AIR_LARGE;		//I Have NFI what airport the
 
	} else {				//AI is going to build here
 
		rad = 4;
 
	}
 

	
 
	for(;p->mode==0;p++) {
 
		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 
		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 

	
 
		w = _airport_size_x[p->attr];
 
		h = _airport_size_y[p->attr];
 
		if (cargo & 0x80) {
 
			GetProductionAroundTiles(values, tile2, w, h, rad);
 
			return values[cargo & 0x7F] != 0;
 
		} else {
 
			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
 
			return values[cargo] >= 8;
 
		}
 
	}
 
	return true;
 
}
 

	
 
static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, int32 *cost)
 
{
 
	int i;
 
	const AiDefaultBlockData *p;
 
	for(i=0; (p = _airport_default_block_data[i]) != NULL; i++) {
 
		// If we are doing a helicopter service, avoid building
 
		// airports where they can't land.
 
		if (heli && GetAirport(p->attr)->acc_planes == AIRCRAFT_ONLY)
 
			continue;
 

	
 
		*cost = AiDoBuildDefaultAirportBlock(tile, p, 0);
 
@@ -3460,49 +3465,49 @@ static void AiStateBuildDefaultAirportBl
 
	} while (--i);
 

	
 
	// check if we're done with all of them
 
	aib = &p->ai.src;
 
	j = p->ai.num_build_rec;
 
	do {
 
		if (aib->cur_building_rule == 255)
 
			return;
 
	} while (++aib,--j);
 

	
 
	// yep, all are done. switch state.
 
	p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES;
 
}
 

	
 
static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
 
{
 
	const AiDefaultBlockData *p = _airport_default_block_data[id];
 
	while (p->mode != 1) p++;
 
	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
 
}
 

	
 
static void AiStateBuildAircraftVehicles(Player *p)
 
{
 
	const AiDefaultBlockData *ptr;
 
	uint tile;
 
	TileIndex tile;
 
	int veh;
 
	int i;
 
	uint loco_id;
 

	
 
	ptr = _airport_default_block_data[p->ai.src.cur_building_rule];
 
	for(;ptr->mode!=0;ptr++) {}
 

	
 
	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
 

	
 
	veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind!=0 ? 1 : 0);
 
	if (veh == -1) {
 
		return;
 
	}
 

	
 
	if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) return;
 
	loco_id = _new_aircraft_id;
 

	
 
	for(i=0; p->ai.order_list_blocks[i] != 0xFF; i++) {
 
		AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i];
 
		bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || p->ai.cargo_type == CT_MAIL);
 
		Order order;
 

	
 
		order.type = OT_GOTO_STATION;
 
		order.flags = 0;