src/ai/default/default.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
   134 
   134 
   135 static EngineID AiChooseTrainToBuild(RailType railtype, int32 money, byte flag, TileIndex tile)
   135 static EngineID AiChooseTrainToBuild(RailType railtype, int32 money, byte flag, TileIndex tile)
   136 {
   136 {
   137 	EngineID best_veh_index = INVALID_ENGINE;
   137 	EngineID best_veh_index = INVALID_ENGINE;
   138 	byte best_veh_score = 0;
   138 	byte best_veh_score = 0;
   139 	int32 ret;
   139 	CommandCost ret;
   140 	EngineID i;
   140 	EngineID i;
   141 
   141 
   142 	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
   142 	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
   143 		const RailVehicleInfo *rvi = RailVehInfo(i);
   143 		const RailVehicleInfo *rvi = RailVehInfo(i);
   144 		const Engine* e = GetEngine(i);
   144 		const Engine* e = GetEngine(i);
   150 				e->reliability < 0x8A3D) {
   150 				e->reliability < 0x8A3D) {
   151 			continue;
   151 			continue;
   152 		}
   152 		}
   153 
   153 
   154 		ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE);
   154 		ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE);
   155 		if (!CmdFailed(ret) && ret <= money && rvi->ai_rank >= best_veh_score) {
   155 		if (CmdSucceeded(ret) && ret <= money && rvi->ai_rank >= best_veh_score) {
   156 			best_veh_score = rvi->ai_rank;
   156 			best_veh_score = rvi->ai_rank;
   157 			best_veh_index = i;
   157 			best_veh_index = i;
   158 		}
   158 		}
   159 	}
   159 	}
   160 
   160 
   170 
   170 
   171 	for (; i != end; i++) {
   171 	for (; i != end; i++) {
   172 		const RoadVehicleInfo *rvi = RoadVehInfo(i);
   172 		const RoadVehicleInfo *rvi = RoadVehInfo(i);
   173 		const Engine* e = GetEngine(i);
   173 		const Engine* e = GetEngine(i);
   174 		int32 rating;
   174 		int32 rating;
   175 		int32 ret;
   175 		CommandCost ret;
   176 
   176 
   177 		if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
   177 		if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
   178 			continue;
   178 			continue;
   179 		}
   179 		}
   180 
   180 
   205 	int32 best_veh_cost = 0;
   205 	int32 best_veh_cost = 0;
   206 	EngineID i;
   206 	EngineID i;
   207 
   207 
   208 	for (i = AIRCRAFT_ENGINES_INDEX; i != AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; i++) {
   208 	for (i = AIRCRAFT_ENGINES_INDEX; i != AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; i++) {
   209 		const Engine* e = GetEngine(i);
   209 		const Engine* e = GetEngine(i);
   210 		int32 ret;
   210 		CommandCost ret;
   211 
   211 
   212 		if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
   212 		if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
   213 			continue;
   213 			continue;
   214 		}
   214 		}
   215 
   215 
   216 		if ((AircraftVehInfo(i)->subtype & AIR_CTOL) != flag) continue;
   216 		if ((AircraftVehInfo(i)->subtype & AIR_CTOL) != flag) continue;
   217 
   217 
   218 		ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT);
   218 		ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT);
   219 		if (!CmdFailed(ret) && ret <= money && ret >= best_veh_cost) {
   219 		if (CmdSucceeded(ret) && ret <= money && ret >= best_veh_cost) {
   220 			best_veh_cost = ret;
   220 			best_veh_cost = ret;
   221 			best_veh_index = i;
   221 			best_veh_index = i;
   222 		}
   222 		}
   223 	}
   223 	}
   224 
   224 
   329 		TileIndex tile;
   329 		TileIndex tile;
   330 
   330 
   331 		BackupVehicleOrders(v, orderbak);
   331 		BackupVehicleOrders(v, orderbak);
   332 		tile = v->tile;
   332 		tile = v->tile;
   333 
   333 
   334 		if (!CmdFailed(DoCommand(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
   334 		if (CmdSucceeded(DoCommand(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
   335 				!CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE))) {
   335 				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE))) {
   336 			VehicleID veh = _new_vehicle_id;
   336 			VehicleID veh = _new_vehicle_id;
   337 			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
   337 			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
   338 			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
   338 			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
   339 
   339 
   340 			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
   340 			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
   358 		TileIndex tile;
   358 		TileIndex tile;
   359 
   359 
   360 		BackupVehicleOrders(v, orderbak);
   360 		BackupVehicleOrders(v, orderbak);
   361 		tile = v->tile;
   361 		tile = v->tile;
   362 
   362 
   363 		if (!CmdFailed(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
   363 		if (CmdSucceeded(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
   364 				!CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) {
   364 				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) {
   365 			VehicleID veh = _new_vehicle_id;
   365 			VehicleID veh = _new_vehicle_id;
   366 
   366 
   367 			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
   367 			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
   368 			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
   368 			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
   369 			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
   369 			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
   387 		TileIndex tile;
   387 		TileIndex tile;
   388 
   388 
   389 		BackupVehicleOrders(v, orderbak);
   389 		BackupVehicleOrders(v, orderbak);
   390 		tile = v->tile;
   390 		tile = v->tile;
   391 
   391 
   392 		if (!CmdFailed(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
   392 		if (CmdSucceeded(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
   393 				!CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) {
   393 				CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) {
   394 			VehicleID veh = _new_vehicle_id;
   394 			VehicleID veh = _new_vehicle_id;
   395 			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
   395 			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
   396 			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
   396 			DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
   397 
   397 
   398 			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
   398 			DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT);
   652 	 * AI will have chance of randomly rejecting routes further than 37 tiles from their network,
   652 	 * AI will have chance of randomly rejecting routes further than 37 tiles from their network,
   653 	 * so there will be some attempt to cluster the network together */
   653 	 * so there will be some attempt to cluster the network together */
   654 
   654 
   655 	/* Random value between 37 and 292. Low values are exponentially more likely
   655 	/* Random value between 37 and 292. Low values are exponentially more likely
   656 	 * With 50% chance the value will be under 52 tiles */
   656 	 * With 50% chance the value will be under 52 tiles */
   657 	int min_distance = 36 + 1 << (Random() % 9); // 0..8
   657 	int min_distance = 36 + (1 << (Random() % 9)); // 0..8
   658 
   658 
   659 	/* Make sure distance to closest station is < min_distance tiles. */
   659 	/* Make sure distance to closest station is < min_distance tiles. */
   660 	if (dist != 0xFFFF && dist > min_distance) return false;
   660 	if (dist != 0xFFFF && dist > min_distance) return false;
   661 
   661 
   662 	if (p->ai.route_type_mask != 0 &&
   662 	if (p->ai.route_type_mask != 0 &&
  1636 	}
  1636 	}
  1637 
  1637 
  1638 	return true;
  1638 	return true;
  1639 }
  1639 }
  1640 
  1640 
  1641 static int32 AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData* p, RailType railtype, byte flag)
  1641 static CommandCost AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData* p, RailType railtype, byte flag)
  1642 {
  1642 {
  1643 	int32 ret;
  1643 	CommandCost ret;
  1644 	int32 total_cost = 0;
  1644 	CommandCost total_cost = 0;
  1645 	Town *t = NULL;
  1645 	Town *t = NULL;
  1646 	int rating = 0;
  1646 	int rating = 0;
  1647 	int i, j, k;
  1647 	int i, j, k;
  1648 
  1648 
  1649 	for (;;) {
  1649 	for (;;) {
  1732 
  1732 
  1733 	return total_cost;
  1733 	return total_cost;
  1734 }
  1734 }
  1735 
  1735 
  1736 // Returns rule and cost
  1736 // Returns rule and cost
  1737 static int AiBuildDefaultRailTrack(TileIndex tile, byte p0, byte p1, byte p2, byte p3, byte dir, byte cargo, RailType railtype, int32* cost)
  1737 static int AiBuildDefaultRailTrack(TileIndex tile, byte p0, byte p1, byte p2, byte p3, byte dir, byte cargo, RailType railtype, CommandCost* cost)
  1738 {
  1738 {
  1739 	int i;
  1739 	int i;
  1740 	const AiDefaultRailBlock *p;
  1740 	const AiDefaultRailBlock *p;
  1741 
  1741 
  1742 	for (i = 0; (p = _default_rail_track_data[i]) != NULL; i++) {
  1742 	for (i = 0; (p = _default_rail_track_data[i]) != NULL; i++) {
  1743 		if (p->p0 == p0 && p->p1 == p1 && p->p2 == p2 && p->p3 == p3 &&
  1743 		if (p->p0 == p0 && p->p1 == p1 && p->p2 == p2 && p->p3 == p3 &&
  1744 				(p->dir == 0xFF || p->dir == dir || ((p->dir - 1) & 3) == dir)) {
  1744 				(p->dir == 0xFF || p->dir == dir || ((p->dir - 1) & 3) == dir)) {
  1745 			*cost = AiDoBuildDefaultRailTrack(tile, p->data, railtype, DC_NO_TOWN_RATING);
  1745 			*cost = AiDoBuildDefaultRailTrack(tile, p->data, railtype, DC_NO_TOWN_RATING);
  1746 			if (!CmdFailed(*cost) && AiCheckTrackResources(tile, p->data, cargo))
  1746 			if (CmdSucceeded(*cost) && AiCheckTrackResources(tile, p->data, cargo))
  1747 				return i;
  1747 				return i;
  1748 		}
  1748 		}
  1749 	}
  1749 	}
  1750 
  1750 
  1751 	return -1;
  1751 	return -1;
  1819 {
  1819 {
  1820 	uint i;
  1820 	uint i;
  1821 	int j;
  1821 	int j;
  1822 	AiBuildRec *aib;
  1822 	AiBuildRec *aib;
  1823 	int rule;
  1823 	int rule;
  1824 	int32 cost;
  1824 	CommandCost cost;
  1825 
  1825 
  1826 	// time out?
  1826 	// time out?
  1827 	if (++p->ai.timeout_counter == 1388) {
  1827 	if (++p->ai.timeout_counter == 1388) {
  1828 		p->ai.state = AIS_DELETE_RAIL_BLOCKS;
  1828 		p->ai.state = AIS_DELETE_RAIL_BLOCKS;
  1829 		return;
  1829 		return;
  2065 static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p)
  2065 static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, const byte *p)
  2066 {
  2066 {
  2067 	uint z;
  2067 	uint z;
  2068 
  2068 
  2069 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2069 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2070 		int32 cost = DoCommand(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2070 		CommandCost cost = DoCommand(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2071 
  2071 
  2072 		if (!CmdFailed(cost) && cost <= (arf->player->player_money >> 4)) {
  2072 		if (CmdSucceeded(cost) && cost <= (arf->player->player_money >> 4)) {
  2073 			AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0] & 3);
  2073 			AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0] & 3);
  2074 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2074 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2075 		}
  2075 		}
  2076 	}
  2076 	}
  2077 }
  2077 }
  2121 		p += 6;
  2121 		p += 6;
  2122 	} else {
  2122 	} else {
  2123 		do {
  2123 		do {
  2124 			// Make sure the tile is not in the list of banned tiles and that a rail can be built here.
  2124 			// Make sure the tile is not in the list of banned tiles and that a rail can be built here.
  2125 			if (!AiIsTileBanned(arf->player, tile, p[0]) &&
  2125 			if (!AiIsTileBanned(arf->player, tile, p[0]) &&
  2126 					!CmdFailed(DoCommand(tile, arf->player->ai.railtype_to_use, p[0], DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL))) {
  2126 					CmdSucceeded(DoCommand(tile, arf->player->ai.railtype_to_use, p[0], DC_AUTO | DC_NO_WATER | DC_NO_RAIL_OVERLAP, CMD_BUILD_SINGLE_RAIL))) {
  2127 				AiBuildRailRecursive(arf, tile, p[1]);
  2127 				AiBuildRailRecursive(arf, tile, p[1]);
  2128 			}
  2128 			}
  2129 
  2129 
  2130 			// At the bottom depth?
  2130 			// At the bottom depth?
  2131 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2131 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2209 		 * solve a thing
  2209 		 * solve a thing
  2210 		 */
  2210 		 */
  2211 		for (i = MAX_BRIDGES - 1; i != 0; i--) {
  2211 		for (i = MAX_BRIDGES - 1; i != 0; i--) {
  2212 			if (CheckBridge_Stuff(i, bridge_len)) {
  2212 			if (CheckBridge_Stuff(i, bridge_len)) {
  2213 				int32 cost = DoCommand(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE);
  2213 				int32 cost = DoCommand(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE);
  2214 				if (!CmdFailed(cost) && cost < (p->player_money >> 5)) break;
  2214 				if (CmdSucceeded(cost) && cost < (p->player_money >> 5)) break;
  2215 			}
  2215 			}
  2216 		}
  2216 		}
  2217 
  2217 
  2218 		// Build it
  2218 		// Build it
  2219 		DoCommand(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  2219 		DoCommand(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  2463 	const AiDefaultBlockData *ptr;
  2463 	const AiDefaultBlockData *ptr;
  2464 	TileIndex tile;
  2464 	TileIndex tile;
  2465 	EngineID veh;
  2465 	EngineID veh;
  2466 	int i;
  2466 	int i;
  2467 	CargoID cargo;
  2467 	CargoID cargo;
  2468 	int32 cost;
  2468 	CommandCost cost;
  2469 	Vehicle *v;
  2469 	Vehicle *v;
  2470 	VehicleID loco_id;
  2470 	VehicleID loco_id;
  2471 
  2471 
  2472 	ptr = _default_rail_track_data[p->ai.src.cur_building_rule]->data;
  2472 	ptr = _default_rail_track_data[p->ai.src.cur_building_rule]->data;
  2473 	while (ptr->mode != 0) ptr++;
  2473 	while (ptr->mode != 0) ptr++;
  2498 handle_nocash:
  2498 handle_nocash:
  2499 		// after a while, if AI still doesn't have cash, get out of this block by selling the wagons.
  2499 		// after a while, if AI still doesn't have cash, get out of this block by selling the wagons.
  2500 		if (++p->ai.state_counter == 1000) {
  2500 		if (++p->ai.state_counter == 1000) {
  2501 			for (i = 0; p->ai.wagon_list[i] != INVALID_VEHICLE; i++) {
  2501 			for (i = 0; p->ai.wagon_list[i] != INVALID_VEHICLE; i++) {
  2502 				cost = DoCommand(tile, p->ai.wagon_list[i], 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
  2502 				cost = DoCommand(tile, p->ai.wagon_list[i], 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
  2503 				assert(!CmdFailed(cost));
  2503 				assert(CmdSucceeded(cost));
  2504 			}
  2504 			}
  2505 			p->ai.state = AIS_0;
  2505 			p->ai.state = AIS_0;
  2506 		}
  2506 		}
  2507 		return;
  2507 		return;
  2508 	}
  2508 	}
  2509 
  2509 
  2510 	// Try to build the locomotive
  2510 	// Try to build the locomotive
  2511 	cost = DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE);
  2511 	cost = DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE);
  2512 	assert(!CmdFailed(cost));
  2512 	assert(CmdSucceeded(cost));
  2513 	loco_id = _new_vehicle_id;
  2513 	loco_id = _new_vehicle_id;
  2514 
  2514 
  2515 	// Sell a vehicle if the train is double headed.
  2515 	// Sell a vehicle if the train is double headed.
  2516 	v = GetVehicle(loco_id);
  2516 	v = GetVehicle(loco_id);
  2517 	if (v->next != NULL) {
  2517 	if (v->next != NULL) {
  2603 		}
  2603 		}
  2604 	}
  2604 	}
  2605 }
  2605 }
  2606 
  2606 
  2607 static bool _want_road_truck_station;
  2607 static bool _want_road_truck_station;
  2608 static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag);
  2608 static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag);
  2609 
  2609 
  2610 // Returns rule and cost
  2610 // Returns rule and cost
  2611 static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, int32 *cost)
  2611 static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo, CommandCost *cost)
  2612 {
  2612 {
  2613 	int i;
  2613 	int i;
  2614 	const AiDefaultRoadBlock *p;
  2614 	const AiDefaultRoadBlock *p;
  2615 
  2615 
  2616 	_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS;
  2616 	_want_road_truck_station = (cargo & 0x7F) != CT_PASSENGERS;
  2617 
  2617 
  2618 	for (i = 0; (p = _road_default_block_data[i]) != NULL; i++) {
  2618 	for (i = 0; (p = _road_default_block_data[i]) != NULL; i++) {
  2619 		if (p->dir == direction) {
  2619 		if (p->dir == direction) {
  2620 			*cost = AiDoBuildDefaultRoadBlock(tile, p->data, 0);
  2620 			*cost = AiDoBuildDefaultRoadBlock(tile, p->data, 0);
  2621 			if (!CmdFailed(*cost) && AiCheckRoadResources(tile, p->data, cargo))
  2621 			if (CmdSucceeded(*cost) && AiCheckRoadResources(tile, p->data, cargo))
  2622 				return i;
  2622 				return i;
  2623 		}
  2623 		}
  2624 	}
  2624 	}
  2625 
  2625 
  2626 	return -1;
  2626 	return -1;
  2627 }
  2627 }
  2628 
  2628 
  2629 static int32 AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
  2629 static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
  2630 {
  2630 {
  2631 	int32 ret;
  2631 	CommandCost ret;
  2632 	int32 total_cost = 0;
  2632 	CommandCost total_cost = 0;
  2633 	Town *t = NULL;
  2633 	Town *t = NULL;
  2634 	int rating = 0;
  2634 	int rating = 0;
  2635 	int roadflag = 0;
  2635 	int roadflag = 0;
  2636 
  2636 
  2637 	for (;p->mode != 4;p++) {
  2637 	for (;p->mode != 4;p++) {
  2719 {
  2719 {
  2720 	uint i;
  2720 	uint i;
  2721 	int j;
  2721 	int j;
  2722 	AiBuildRec *aib;
  2722 	AiBuildRec *aib;
  2723 	int rule;
  2723 	int rule;
  2724 	int32 cost;
  2724 	CommandCost cost;
  2725 
  2725 
  2726 	// time out?
  2726 	// time out?
  2727 	if (++p->ai.timeout_counter == 1388) {
  2727 	if (++p->ai.timeout_counter == 1388) {
  2728 		p->ai.state = AIS_DELETE_RAIL_BLOCKS;
  2728 		p->ai.state = AIS_DELETE_RAIL_BLOCKS;
  2729 		return;
  2729 		return;
  2756 				if (++p->ai.state_counter >= 1000) {
  2756 				if (++p->ai.state_counter >= 1000) {
  2757 					p->ai.state_counter = 0;
  2757 					p->ai.state_counter = 0;
  2758 					p->ai.state_mode = -p->ai.state_mode;
  2758 					p->ai.state_mode = -p->ai.state_mode;
  2759 				}
  2759 				}
  2760 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
  2760 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
  2761 				int32 r;
  2761 				CommandCost r;
  2762 
  2762 
  2763 				// player has money, build it.
  2763 				// player has money, build it.
  2764 				aib->cur_building_rule = rule;
  2764 				aib->cur_building_rule = rule;
  2765 
  2765 
  2766 				r = AiDoBuildDefaultRoadBlock(
  2766 				r = AiDoBuildDefaultRoadBlock(
  2767 					aib->use_tile,
  2767 					aib->use_tile,
  2768 					_road_default_block_data[rule]->data,
  2768 					_road_default_block_data[rule]->data,
  2769 					DC_EXEC | DC_NO_TOWN_RATING
  2769 					DC_EXEC | DC_NO_TOWN_RATING
  2770 				);
  2770 				);
  2771 				assert(!CmdFailed(r));
  2771 				assert(CmdSucceeded(r));
  2772 			}
  2772 			}
  2773 		} while (++aib, --j);
  2773 		} while (++aib, --j);
  2774 	}
  2774 	}
  2775 
  2775 
  2776 	// check if we're done with all of them
  2776 	// check if we're done with all of them
  2914 		ROAD_NW | ROAD_NE,
  2914 		ROAD_NW | ROAD_NE,
  2915 		ROAD_SW | ROAD_SE,
  2915 		ROAD_SW | ROAD_SE,
  2916 		ROAD_NW | ROAD_SW,
  2916 		ROAD_NW | ROAD_SW,
  2917 		ROAD_SE | ROAD_NE
  2917 		ROAD_SE | ROAD_NE
  2918 	};
  2918 	};
  2919 	return !CmdFailed(DoCommand(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
  2919 	return CmdSucceeded(DoCommand(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
  2920 }
  2920 }
  2921 
  2921 
  2922 static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
  2922 static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
  2923 {
  2923 {
  2924 	Slope tileh;
  2924 	Slope tileh;
  2965 static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
  2965 static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, const byte *p)
  2966 {
  2966 {
  2967 	uint z;
  2967 	uint z;
  2968 
  2968 
  2969 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2969 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2970 		int32 cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2970 		CommandCost cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2971 
  2971 
  2972 		if (!CmdFailed(cost) && cost <= (arf->player->player_money >> 4)) {
  2972 		if (CmdSucceeded(cost) && cost <= (arf->player->player_money >> 4)) {
  2973 			AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0] & 3);
  2973 			AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0] & 3);
  2974 			if (arf->depth == 1)  AiCheckRoadPathBetter(arf, p);
  2974 			if (arf->depth == 1)  AiCheckRoadPathBetter(arf, p);
  2975 		}
  2975 		}
  2976 	}
  2976 	}
  2977 }
  2977 }
  3099 		 * unnecessary to check for worse bridge (i=0), since AI will always build that.
  3099 		 * unnecessary to check for worse bridge (i=0), since AI will always build that.
  3100 		 *AI is so fucked up that fixing this small thing will probably not solve a thing
  3100 		 *AI is so fucked up that fixing this small thing will probably not solve a thing
  3101 		 */
  3101 		 */
  3102 		for (i = 10; i != 0; i--) {
  3102 		for (i = 10; i != 0; i--) {
  3103 			if (CheckBridge_Stuff(i, bridge_len)) {
  3103 			if (CheckBridge_Stuff(i, bridge_len)) {
  3104 				int32 cost = DoCommand(tile, p->ai.cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE);
  3104 				CommandCost cost = DoCommand(tile, p->ai.cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE);
  3105 				if (!CmdFailed(cost) && cost < (p->player_money >> 5)) break;
  3105 				if (CmdSucceeded(cost) && cost < (p->player_money >> 5)) break;
  3106 			}
  3106 			}
  3107 		}
  3107 		}
  3108 
  3108 
  3109 		// Build it
  3109 		// Build it
  3110 		DoCommand(tile, p->ai.cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  3110 		DoCommand(tile, p->ai.cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  3385 	p->ai.state = AIS_BUILD_DEFAULT_AIRPORT_BLOCKS;
  3385 	p->ai.state = AIS_BUILD_DEFAULT_AIRPORT_BLOCKS;
  3386 	p->ai.state_mode = 255;
  3386 	p->ai.state_mode = 255;
  3387 	p->ai.state_counter = 0;
  3387 	p->ai.state_counter = 0;
  3388 }
  3388 }
  3389 
  3389 
  3390 static int32 AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
  3390 static CommandCost AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
  3391 {
  3391 {
  3392 	uint32 avail_airports = GetValidAirports();
  3392 	uint32 avail_airports = GetValidAirports();
  3393 	int32 total_cost = 0, ret;
  3393 	CommandCost total_cost = 0, ret;
  3394 
  3394 
  3395 	for (; p->mode == 0; p++) {
  3395 	for (; p->mode == 0; p++) {
  3396 		if (!HASBIT(avail_airports, p->attr)) return CMD_ERROR;
  3396 		if (!HASBIT(avail_airports, p->attr)) return CMD_ERROR;
  3397 		ret = DoCommand(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_AIRPORT);
  3397 		ret = DoCommand(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_AIRPORT);
  3398 		if (CmdFailed(ret)) return CMD_ERROR;
  3398 		if (CmdFailed(ret)) return CMD_ERROR;
  3422 		}
  3422 		}
  3423 	}
  3423 	}
  3424 	return true;
  3424 	return true;
  3425 }
  3425 }
  3426 
  3426 
  3427 static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, int32 *cost)
  3427 static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, CommandCost *cost)
  3428 {
  3428 {
  3429 	const AiDefaultBlockData *p;
  3429 	const AiDefaultBlockData *p;
  3430 	uint i;
  3430 	uint i;
  3431 
  3431 
  3432 	for (i = 0; (p = _airport_default_block_data[i]) != NULL; i++) {
  3432 	for (i = 0; (p = _airport_default_block_data[i]) != NULL; i++) {
  3433 		// If we are doing a helicopter service, avoid building
  3433 		// If we are doing a helicopter service, avoid building
  3434 		// airports where they can't land.
  3434 		// airports where they can't land.
  3435 		if (heli && !(GetAirport(p->attr)->flags & AirportFTAClass::HELICOPTERS)) continue;
  3435 		if (heli && !(GetAirport(p->attr)->flags & AirportFTAClass::HELICOPTERS)) continue;
  3436 
  3436 
  3437 		*cost = AiDoBuildDefaultAirportBlock(tile, p, 0);
  3437 		*cost = AiDoBuildDefaultAirportBlock(tile, p, 0);
  3438 		if (!CmdFailed(*cost) && AiCheckAirportResources(tile, p, cargo))
  3438 		if (CmdSucceeded(*cost) && AiCheckAirportResources(tile, p, cargo))
  3439 			return i;
  3439 			return i;
  3440 	}
  3440 	}
  3441 	return -1;
  3441 	return -1;
  3442 }
  3442 }
  3443 
  3443 
  3444 static void AiStateBuildDefaultAirportBlocks(Player *p)
  3444 static void AiStateBuildDefaultAirportBlocks(Player *p)
  3445 {
  3445 {
  3446 	int i, j;
  3446 	int i, j;
  3447 	AiBuildRec *aib;
  3447 	AiBuildRec *aib;
  3448 	int rule;
  3448 	int rule;
  3449 	int32 cost;
  3449 	CommandCost cost;
  3450 
  3450 
  3451 	// time out?
  3451 	// time out?
  3452 	if (++p->ai.timeout_counter == 1388) {
  3452 	if (++p->ai.timeout_counter == 1388) {
  3453 		p->ai.state = AIS_0;
  3453 		p->ai.state = AIS_0;
  3454 		return;
  3454 		return;
  3483 					p->ai.state_counter = 0;
  3483 					p->ai.state_counter = 0;
  3484 					p->ai.state_mode = -p->ai.state_mode;
  3484 					p->ai.state_mode = -p->ai.state_mode;
  3485 				}
  3485 				}
  3486 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
  3486 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
  3487 				// player has money, build it.
  3487 				// player has money, build it.
  3488 				int32 r;
  3488 				CommandCost r;
  3489 
  3489 
  3490 				aib->cur_building_rule = rule;
  3490 				aib->cur_building_rule = rule;
  3491 
  3491 
  3492 				r = AiDoBuildDefaultAirportBlock(
  3492 				r = AiDoBuildDefaultAirportBlock(
  3493 					aib->use_tile,
  3493 					aib->use_tile,
  3494 					_airport_default_block_data[rule],
  3494 					_airport_default_block_data[rule],
  3495 					DC_EXEC | DC_NO_TOWN_RATING
  3495 					DC_EXEC | DC_NO_TOWN_RATING
  3496 				);
  3496 				);
  3497 				assert(!CmdFailed(r));
  3497 				assert(CmdSucceeded(r));
  3498 			}
  3498 			}
  3499 		} while (++aib, --j);
  3499 		} while (++aib, --j);
  3500 	} while (--i);
  3500 	} while (--i);
  3501 
  3501 
  3502 	// check if we're done with all of them
  3502 	// check if we're done with all of them