src/ai/default/default.cpp
branchcpp_gui
changeset 6308 646711c5feaa
parent 6307 f40e88cff863
equal deleted inserted replaced
6307:f40e88cff863 6308:646711c5feaa
   514 
   514 
   515 static void AiFindSubsidyPassengerRoute(FoundRoute *fr)
   515 static void AiFindSubsidyPassengerRoute(FoundRoute *fr)
   516 {
   516 {
   517 	uint i;
   517 	uint i;
   518 	const Subsidy* s;
   518 	const Subsidy* s;
   519 	Town *from,*to;
   519 	Town *from, *to;
   520 
   520 
   521 	// initially error
   521 	// initially error
   522 	fr->distance = -1;
   522 	fr->distance = -1;
   523 
   523 
   524 	// Randomize subsidy index..
   524 	// Randomize subsidy index..
  1559 			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
  1559 			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
  1560 			if (!(values[cargo] & ~7))
  1560 			if (!(values[cargo] & ~7))
  1561 				return false;
  1561 				return false;
  1562 			if (cargo != CT_MAIL)
  1562 			if (cargo != CT_MAIL)
  1563 				return true;
  1563 				return true;
  1564 			return !!((values[cargo]>>1) & ~7);
  1564 			return !!((values[cargo] >> 1) & ~7);
  1565 		}
  1565 		}
  1566 	}
  1566 	}
  1567 
  1567 
  1568 	return true;
  1568 	return true;
  1569 }
  1569 }
  1572 {
  1572 {
  1573 	int32 ret;
  1573 	int32 ret;
  1574 	int32 total_cost = 0;
  1574 	int32 total_cost = 0;
  1575 	Town *t = NULL;
  1575 	Town *t = NULL;
  1576 	int rating = 0;
  1576 	int rating = 0;
  1577 	int i,j,k;
  1577 	int i, j, k;
  1578 
  1578 
  1579 	for (;;) {
  1579 	for (;;) {
  1580 		// This will seldomly overflow for valid reasons. Mask it to be on the safe side.
  1580 		// This will seldomly overflow for valid reasons. Mask it to be on the safe side.
  1581 		uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
  1581 		uint c = TILE_MASK(tile + ToTileIndexDiff(p->tileoffs));
  1582 
  1582 
  1586 			if (p->mode == 0) {
  1586 			if (p->mode == 0) {
  1587 				// Depot
  1587 				// Depot
  1588 				ret = DoCommand(c, railtype, p->attr, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_TRAIN_DEPOT);
  1588 				ret = DoCommand(c, railtype, p->attr, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_TRAIN_DEPOT);
  1589 			} else {
  1589 			} else {
  1590 				// Station
  1590 				// Station
  1591 				ret = DoCommand(c, (p->attr&1) | (p->attr>>4)<<8 | (p->attr>>1&7)<<16, railtype, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_RAILROAD_STATION);
  1591 				ret = DoCommand(c, (p->attr & 1) | (p->attr >> 4) << 8 | (p->attr >> 1 & 7) << 16, railtype, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_RAILROAD_STATION);
  1592 			}
  1592 			}
  1593 
  1593 
  1594 			if (CmdFailed(ret)) return CMD_ERROR;
  1594 			if (CmdFailed(ret)) return CMD_ERROR;
  1595 			total_cost += ret;
  1595 			total_cost += ret;
  1596 
  1596 
  1608 			j = p->attr;
  1608 			j = p->attr;
  1609 			k = 0;
  1609 			k = 0;
  1610 
  1610 
  1611 			// Build the rail
  1611 			// Build the rail
  1612 			for (i = 0; i != 6; i++, j >>= 1) {
  1612 			for (i = 0; i != 6; i++, j >>= 1) {
  1613 				if (j&1) {
  1613 				if (j & 1) {
  1614 					k = i;
  1614 					k = i;
  1615 					ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
  1615 					ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
  1616 					if (CmdFailed(ret)) return CMD_ERROR;
  1616 					if (CmdFailed(ret)) return CMD_ERROR;
  1617 					total_cost += ret;
  1617 					total_cost += ret;
  1618 				}
  1618 				}
  1640 			ret = DoCommand(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
  1640 			ret = DoCommand(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
  1641 			if (CmdFailed(ret)) return CMD_ERROR;
  1641 			if (CmdFailed(ret)) return CMD_ERROR;
  1642 			total_cost += ret + _price.build_rail;
  1642 			total_cost += ret + _price.build_rail;
  1643 
  1643 
  1644 			if (flag & DC_EXEC) {
  1644 			if (flag & DC_EXEC) {
  1645 				DoCommand(c, railtype, p->attr&1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL);
  1645 				DoCommand(c, railtype, p->attr & 1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL);
  1646 			}
  1646 			}
  1647 
  1647 
  1648 			goto clear_town_stuff;
  1648 			goto clear_town_stuff;
  1649 		} else {
  1649 		} else {
  1650 			// Unk
  1650 			// Unk
  1782 			);
  1782 			);
  1783 
  1783 
  1784 			if (rule == -1) {
  1784 			if (rule == -1) {
  1785 				// cannot build, terraform after a while
  1785 				// cannot build, terraform after a while
  1786 				if (p->ai.state_counter >= 600) {
  1786 				if (p->ai.state_counter >= 600) {
  1787 					AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode);
  1787 					AiDoTerraformLand(aib->use_tile, Random() & 3, 3, (int8)p->ai.state_mode);
  1788 				}
  1788 				}
  1789 				// also try the other terraform direction
  1789 				// also try the other terraform direction
  1790 				if (++p->ai.state_counter >= 1000) {
  1790 				if (++p->ai.state_counter >= 1000) {
  1791 					p->ai.state_counter = 0;
  1791 					p->ai.state_counter = 0;
  1792 					p->ai.state_mode = -p->ai.state_mode;
  1792 					p->ai.state_mode = -p->ai.state_mode;
  1802 					p->ai.railtype_to_use,
  1802 					p->ai.railtype_to_use,
  1803 					DC_EXEC | DC_NO_TOWN_RATING
  1803 					DC_EXEC | DC_NO_TOWN_RATING
  1804 				);
  1804 				);
  1805 				assert(!CmdFailed(r));
  1805 				assert(!CmdFailed(r));
  1806 			}
  1806 			}
  1807 		} while (++aib,--j);
  1807 		} while (++aib, --j);
  1808 	}
  1808 	}
  1809 
  1809 
  1810 	// check if we're done with all of them
  1810 	// check if we're done with all of them
  1811 	aib = &p->ai.src;
  1811 	aib = &p->ai.src;
  1812 	j = p->ai.num_build_rec;
  1812 	j = p->ai.num_build_rec;
  1813 	do {
  1813 	do {
  1814 		if (aib->cur_building_rule == 255) return;
  1814 		if (aib->cur_building_rule == 255) return;
  1815 	} while (++aib,--j);
  1815 	} while (++aib, --j);
  1816 
  1816 
  1817 	// yep, all are done. switch state to the rail building state.
  1817 	// yep, all are done. switch state to the rail building state.
  1818 	p->ai.state = AIS_BUILD_RAIL;
  1818 	p->ai.state = AIS_BUILD_RAIL;
  1819 	p->ai.state_mode = 255;
  1819 	p->ai.state_mode = 255;
  1820 }
  1820 }
  1855 
  1855 
  1856 	arpfd.tile = p->ai.start_tile_a;
  1856 	arpfd.tile = p->ai.start_tile_a;
  1857 	arpfd.tile2 = p->ai.cur_tile_a;
  1857 	arpfd.tile2 = p->ai.cur_tile_a;
  1858 	arpfd.flag = false;
  1858 	arpfd.flag = false;
  1859 	arpfd.count = 0;
  1859 	arpfd.count = 0;
  1860 	FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a^2),
  1860 	FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a ^ 2),
  1861 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
  1861 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
  1862 	return arpfd.count > 8;
  1862 	return arpfd.count > 8;
  1863 }
  1863 }
  1864 
  1864 
  1865 struct AiRailFinder {
  1865 struct AiRailFinder {
  1999 	uint z;
  1999 	uint z;
  2000 
  2000 
  2001 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2001 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2002 		int32 cost = DoCommand(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2002 		int32 cost = DoCommand(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2003 
  2003 
  2004 		if (!CmdFailed(cost) && cost <= (arf->player->player_money>>4)) {
  2004 		if (!CmdFailed(cost) && cost <= (arf->player->player_money >> 4)) {
  2005 			AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0]&3);
  2005 			AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0] & 3);
  2006 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2006 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2007 		}
  2007 		}
  2008 	}
  2008 	}
  2009 }
  2009 }
  2010 
  2010 
  2015 
  2015 
  2016 	tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
  2016 	tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
  2017 
  2017 
  2018 	// Reached destination?
  2018 	// Reached destination?
  2019 	if (tile == arf->final_tile) {
  2019 	if (tile == arf->final_tile) {
  2020 		if (arf->final_dir != (dir^2)) {
  2020 		if (arf->final_dir != (dir ^ 2)) {
  2021 			if (arf->recursive_mode != 2) arf->recursive_mode = 1;
  2021 			if (arf->recursive_mode != 2) arf->recursive_mode = 1;
  2022 		} else if (arf->recursive_mode != 2) {
  2022 		} else if (arf->recursive_mode != 2) {
  2023 			arf->recursive_mode = 2;
  2023 			arf->recursive_mode = 2;
  2024 			arf->cur_best_depth = arf->depth;
  2024 			arf->cur_best_depth = arf->depth;
  2025 		} else {
  2025 		} else {
  2061 
  2061 
  2062 			// At the bottom depth?
  2062 			// At the bottom depth?
  2063 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2063 			if (arf->depth == 1) AiCheckRailPathBetter(arf, p);
  2064 
  2064 
  2065 			p += 2;
  2065 			p += 2;
  2066 		} while (!(p[0]&0x80));
  2066 		} while (!(p[0] & 0x80));
  2067 	}
  2067 	}
  2068 
  2068 
  2069 	AiCheckBuildRailBridgeHere(arf, tile, p);
  2069 	AiCheckBuildRailBridgeHere(arf, tile, p);
  2070 	AiCheckBuildRailTunnelHere(arf, tile, p+1);
  2070 	AiCheckBuildRailTunnelHere(arf, tile, p + 1);
  2071 
  2071 
  2072 	arf->depth--;
  2072 	arf->depth--;
  2073 }
  2073 }
  2074 
  2074 
  2075 
  2075 
  2076 static const byte _dir_table_3[]= {0x25, 0x2A, 0x19, 0x16};
  2076 static const byte _dir_table_3[] = {0x25, 0x2A, 0x19, 0x16};
  2077 
  2077 
  2078 static void AiBuildRailConstruct(Player *p)
  2078 static void AiBuildRailConstruct(Player *p)
  2079 {
  2079 {
  2080 	AiRailFinder arf;
  2080 	AiRailFinder arf;
  2081 	int i;
  2081 	int i;
  2322 			return;
  2322 			return;
  2323 		}
  2323 		}
  2324 	}
  2324 	}
  2325 
  2325 
  2326 	// Find first edge to build from.
  2326 	// Find first edge to build from.
  2327 	tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, cmd&3, &dir);
  2327 	tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, cmd & 3, &dir);
  2328 	p->ai.start_tile_a = tile;
  2328 	p->ai.start_tile_a = tile;
  2329 	p->ai.cur_tile_a = tile;
  2329 	p->ai.cur_tile_a = tile;
  2330 	p->ai.start_dir_a = dir;
  2330 	p->ai.start_dir_a = dir;
  2331 	p->ai.cur_dir_a = dir;
  2331 	p->ai.cur_dir_a = dir;
  2332 	DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir&1)?1:0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
  2332 	DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir & 1) ? 1 : 0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
  2333 
  2333 
  2334 	assert(TILE_MASK(tile) != 0xFF00);
  2334 	assert(TILE_MASK(tile) != 0xFF00);
  2335 
  2335 
  2336 	// Find second edge to build to
  2336 	// Find second edge to build to
  2337 	aib = (&p->ai.src) + ((cmd >> 4)&0xF);
  2337 	aib = (&p->ai.src) + ((cmd >> 4) & 0xF);
  2338 	tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, (cmd>>2)&3, &dir);
  2338 	tile = AiGetEdgeOfDefaultRailBlock(aib->cur_building_rule, aib->use_tile, (cmd >> 2) & 3, &dir);
  2339 	p->ai.start_tile_b = tile;
  2339 	p->ai.start_tile_b = tile;
  2340 	p->ai.cur_tile_b = tile;
  2340 	p->ai.cur_tile_b = tile;
  2341 	p->ai.start_dir_b = dir;
  2341 	p->ai.start_dir_b = dir;
  2342 	p->ai.cur_dir_b = dir;
  2342 	p->ai.cur_dir_b = dir;
  2343 	DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir&1)?1:0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
  2343 	DoCommand(TILE_MASK(tile + TileOffsByDiagDir(dir)), 0, (dir & 1) ? 1 : 0, DC_EXEC, CMD_REMOVE_SINGLE_RAIL);
  2344 
  2344 
  2345 	assert(TILE_MASK(tile) != 0xFF00);
  2345 	assert(TILE_MASK(tile) != 0xFF00);
  2346 
  2346 
  2347 	// And setup state.
  2347 	// And setup state.
  2348 	p->ai.state_mode = 2;
  2348 	p->ai.state_mode = 2;
  2501 
  2501 
  2502 		if (aib->cur_building_rule == 255) continue;
  2502 		if (aib->cur_building_rule == 255) continue;
  2503 		for (b = _default_rail_track_data[aib->cur_building_rule]->data; b->mode != 4; b++) {
  2503 		for (b = _default_rail_track_data[aib->cur_building_rule]->data; b->mode != 4; b++) {
  2504 			DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
  2504 			DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
  2505 		}
  2505 		}
  2506 	} while (++aib,--num);
  2506 	} while (++aib, --num);
  2507 
  2507 
  2508 	p->ai.state = AIS_0;
  2508 	p->ai.state = AIS_0;
  2509 }
  2509 }
  2510 
  2510 
  2511 static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
  2511 static bool AiCheckRoadResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
  2680 			);
  2680 			);
  2681 
  2681 
  2682 			if (rule == -1) {
  2682 			if (rule == -1) {
  2683 				// cannot build, terraform after a while
  2683 				// cannot build, terraform after a while
  2684 				if (p->ai.state_counter >= 600) {
  2684 				if (p->ai.state_counter >= 600) {
  2685 					AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode);
  2685 					AiDoTerraformLand(aib->use_tile, Random() & 3, 3, (int8)p->ai.state_mode);
  2686 				}
  2686 				}
  2687 				// also try the other terraform direction
  2687 				// also try the other terraform direction
  2688 				if (++p->ai.state_counter >= 1000) {
  2688 				if (++p->ai.state_counter >= 1000) {
  2689 					p->ai.state_counter = 0;
  2689 					p->ai.state_counter = 0;
  2690 					p->ai.state_mode = -p->ai.state_mode;
  2690 					p->ai.state_mode = -p->ai.state_mode;
  2691 				}
  2691 				}
  2692 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p,aib->use_tile)) {
  2692 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
  2693 				int32 r;
  2693 				int32 r;
  2694 
  2694 
  2695 				// player has money, build it.
  2695 				// player has money, build it.
  2696 				aib->cur_building_rule = rule;
  2696 				aib->cur_building_rule = rule;
  2697 
  2697 
  2700 					_road_default_block_data[rule]->data,
  2700 					_road_default_block_data[rule]->data,
  2701 					DC_EXEC | DC_NO_TOWN_RATING
  2701 					DC_EXEC | DC_NO_TOWN_RATING
  2702 				);
  2702 				);
  2703 				assert(!CmdFailed(r));
  2703 				assert(!CmdFailed(r));
  2704 			}
  2704 			}
  2705 		} while (++aib,--j);
  2705 		} while (++aib, --j);
  2706 	}
  2706 	}
  2707 
  2707 
  2708 	// check if we're done with all of them
  2708 	// check if we're done with all of them
  2709 	aib = &p->ai.src;
  2709 	aib = &p->ai.src;
  2710 	j = p->ai.num_build_rec;
  2710 	j = p->ai.num_build_rec;
  2711 	do {
  2711 	do {
  2712 		if (aib->cur_building_rule == 255) return;
  2712 		if (aib->cur_building_rule == 255) return;
  2713 	} while (++aib,--j);
  2713 	} while (++aib, --j);
  2714 
  2714 
  2715 	// yep, all are done. switch state to the rail building state.
  2715 	// yep, all are done. switch state to the rail building state.
  2716 	p->ai.state = AIS_BUILD_ROAD;
  2716 	p->ai.state = AIS_BUILD_ROAD;
  2717 	p->ai.state_mode = 255;
  2717 	p->ai.state_mode = 255;
  2718 }
  2718 }
  2899 	uint z;
  2899 	uint z;
  2900 
  2900 
  2901 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2901 	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
  2902 		int32 cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2902 		int32 cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
  2903 
  2903 
  2904 		if (!CmdFailed(cost) && cost <= (arf->player->player_money>>4)) {
  2904 		if (!CmdFailed(cost) && cost <= (arf->player->player_money >> 4)) {
  2905 			AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0]&3);
  2905 			AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0] & 3);
  2906 			if (arf->depth == 1)  AiCheckRoadPathBetter(arf, p);
  2906 			if (arf->depth == 1)  AiCheckRoadPathBetter(arf, p);
  2907 		}
  2907 		}
  2908 	}
  2908 	}
  2909 }
  2909 }
  2910 
  2910 
  2916 
  2916 
  2917 	tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
  2917 	tile = TILE_MASK(tile + TileOffsByDiagDir(dir));
  2918 
  2918 
  2919 	// Reached destination?
  2919 	// Reached destination?
  2920 	if (tile == arf->final_tile) {
  2920 	if (tile == arf->final_tile) {
  2921 		if ((arf->final_dir^2) == dir) {
  2921 		if ((arf->final_dir ^ 2) == dir) {
  2922 			arf->recursive_mode = 2;
  2922 			arf->recursive_mode = 2;
  2923 			arf->cur_best_depth = arf->depth;
  2923 			arf->cur_best_depth = arf->depth;
  2924 		}
  2924 		}
  2925 		return;
  2925 		return;
  2926 	}
  2926 	}
  2960 			p += 2;
  2960 			p += 2;
  2961 		} while (!(p[0] & 0x80));
  2961 		} while (!(p[0] & 0x80));
  2962 	}
  2962 	}
  2963 
  2963 
  2964 	AiCheckBuildRoadBridgeHere(arf, tile, p);
  2964 	AiCheckBuildRoadBridgeHere(arf, tile, p);
  2965 	AiCheckBuildRoadTunnelHere(arf, tile, p+1);
  2965 	AiCheckBuildRoadTunnelHere(arf, tile, p + 1);
  2966 
  2966 
  2967 	arf->depth--;
  2967 	arf->depth--;
  2968 }
  2968 }
  2969 
  2969 
  2970 
  2970 
  3018 		return;
  3018 		return;
  3019 	}
  3019 	}
  3020 
  3020 
  3021 	tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a));
  3021 	tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a));
  3022 
  3022 
  3023 	if (arf.best_ptr[0]&0x80) {
  3023 	if (arf.best_ptr[0] & 0x80) {
  3024 		int i;
  3024 		int i;
  3025 		int32 bridge_len;
  3025 		int32 bridge_len;
  3026 		p->ai.cur_tile_a = arf.bridge_end_tile;
  3026 		p->ai.cur_tile_a = arf.bridge_end_tile;
  3027 		bridge_len = GetBridgeLength(tile, p->ai.cur_tile_a); // tile
  3027 		bridge_len = GetBridgeLength(tile, p->ai.cur_tile_a); // tile
  3028 
  3028 
  3040 
  3040 
  3041 		// Build it
  3041 		// Build it
  3042 		DoCommand(tile, p->ai.cur_tile_a, i + (0x80 << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  3042 		DoCommand(tile, p->ai.cur_tile_a, i + (0x80 << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  3043 
  3043 
  3044 		p->ai.state_counter = 0;
  3044 		p->ai.state_counter = 0;
  3045 	} else if (arf.best_ptr[0]&0x40) {
  3045 	} else if (arf.best_ptr[0] & 0x40) {
  3046 		// tunnel
  3046 		// tunnel
  3047 		DoCommand(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
  3047 		DoCommand(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
  3048 		p->ai.cur_tile_a = _build_tunnel_endtile;
  3048 		p->ai.cur_tile_a = _build_tunnel_endtile;
  3049 		p->ai.state_counter = 0;
  3049 		p->ai.state_counter = 0;
  3050 	} else {
  3050 	} else {
  3146 	p->ai.cur_tile_a = tile;
  3146 	p->ai.cur_tile_a = tile;
  3147 	p->ai.start_dir_a = dir;
  3147 	p->ai.start_dir_a = dir;
  3148 	p->ai.cur_dir_a = dir;
  3148 	p->ai.cur_dir_a = dir;
  3149 
  3149 
  3150 	// Find second edge to build to
  3150 	// Find second edge to build to
  3151 	aib = (&p->ai.src) + (cmd&0xF);
  3151 	aib = (&p->ai.src) + (cmd & 0xF);
  3152 	tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir);
  3152 	tile = AiGetRoadBlockEdge(aib->cur_building_rule, aib->use_tile, &dir);
  3153 	p->ai.start_tile_b = tile;
  3153 	p->ai.start_tile_b = tile;
  3154 	p->ai.cur_tile_b = tile;
  3154 	p->ai.cur_tile_b = tile;
  3155 	p->ai.start_dir_b = dir;
  3155 	p->ai.start_dir_b = dir;
  3156 	p->ai.cur_dir_b = dir;
  3156 	p->ai.cur_dir_b = dir;
  3237 		if (aib->cur_building_rule == 255) continue;
  3237 		if (aib->cur_building_rule == 255) continue;
  3238 		for (b = _road_default_block_data[aib->cur_building_rule]->data; b->mode != 4; b++) {
  3238 		for (b = _road_default_block_data[aib->cur_building_rule]->data; b->mode != 4; b++) {
  3239 			if (b->mode > 1) continue;
  3239 			if (b->mode > 1) continue;
  3240 			DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
  3240 			DoCommand(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
  3241 		}
  3241 		}
  3242 	} while (++aib,--num);
  3242 	} while (++aib, --num);
  3243 
  3243 
  3244 	p->ai.state = AIS_0;
  3244 	p->ai.state = AIS_0;
  3245 }
  3245 }
  3246 
  3246 
  3247 
  3247 
  3324 	uint32 avail_airports = GetValidAirports();
  3324 	uint32 avail_airports = GetValidAirports();
  3325 	int32 total_cost = 0, ret;
  3325 	int32 total_cost = 0, ret;
  3326 
  3326 
  3327 	for (; p->mode == 0; p++) {
  3327 	for (; p->mode == 0; p++) {
  3328 		if (!HASBIT(avail_airports, p->attr)) return CMD_ERROR;
  3328 		if (!HASBIT(avail_airports, p->attr)) return CMD_ERROR;
  3329 		ret = DoCommand(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr,0,flag | DC_AUTO | DC_NO_WATER,CMD_BUILD_AIRPORT);
  3329 		ret = DoCommand(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_AIRPORT);
  3330 		if (CmdFailed(ret)) return CMD_ERROR;
  3330 		if (CmdFailed(ret)) return CMD_ERROR;
  3331 		total_cost += ret;
  3331 		total_cost += ret;
  3332 	}
  3332 	}
  3333 
  3333 
  3334 	return total_cost;
  3334 	return total_cost;
  3406 //			SetRedErrorSquare(aib->use_tile);
  3406 //			SetRedErrorSquare(aib->use_tile);
  3407 
  3407 
  3408 			if (rule == -1) {
  3408 			if (rule == -1) {
  3409 				// cannot build, terraform after a while
  3409 				// cannot build, terraform after a while
  3410 				if (p->ai.state_counter >= 600) {
  3410 				if (p->ai.state_counter >= 600) {
  3411 					AiDoTerraformLand(aib->use_tile, Random()&3, 3, (int8)p->ai.state_mode);
  3411 					AiDoTerraformLand(aib->use_tile, Random() & 3, 3, (int8)p->ai.state_mode);
  3412 				}
  3412 				}
  3413 				// also try the other terraform direction
  3413 				// also try the other terraform direction
  3414 				if (++p->ai.state_counter >= 1000) {
  3414 				if (++p->ai.state_counter >= 1000) {
  3415 					p->ai.state_counter = 0;
  3415 					p->ai.state_counter = 0;
  3416 					p->ai.state_mode = -p->ai.state_mode;
  3416 					p->ai.state_mode = -p->ai.state_mode;
  3417 				}
  3417 				}
  3418 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p,aib->use_tile)) {
  3418 			} else if (CheckPlayerHasMoney(cost) && AiCheckBlockDistances(p, aib->use_tile)) {
  3419 				// player has money, build it.
  3419 				// player has money, build it.
  3420 				int32 r;
  3420 				int32 r;
  3421 
  3421 
  3422 				aib->cur_building_rule = rule;
  3422 				aib->cur_building_rule = rule;
  3423 
  3423 
  3426 					_airport_default_block_data[rule],
  3426 					_airport_default_block_data[rule],
  3427 					DC_EXEC | DC_NO_TOWN_RATING
  3427 					DC_EXEC | DC_NO_TOWN_RATING
  3428 				);
  3428 				);
  3429 				assert(!CmdFailed(r));
  3429 				assert(!CmdFailed(r));
  3430 			}
  3430 			}
  3431 		} while (++aib,--j);
  3431 		} while (++aib, --j);
  3432 	} while (--i);
  3432 	} while (--i);
  3433 
  3433 
  3434 	// check if we're done with all of them
  3434 	// check if we're done with all of them
  3435 	aib = &p->ai.src;
  3435 	aib = &p->ai.src;
  3436 	j = p->ai.num_build_rec;
  3436 	j = p->ai.num_build_rec;
  3437 	do {
  3437 	do {
  3438 		if (aib->cur_building_rule == 255) return;
  3438 		if (aib->cur_building_rule == 255) return;
  3439 	} while (++aib,--j);
  3439 	} while (++aib, --j);
  3440 
  3440 
  3441 	// yep, all are done. switch state.
  3441 	// yep, all are done. switch state.
  3442 	p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES;
  3442 	p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES;
  3443 }
  3443 }
  3444 
  3444 
  3648 					p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE;
  3648 					p->ai.state = AIS_REMOVE_SINGLE_RAIL_TILE;
  3649 					return;
  3649 					return;
  3650 				}
  3650 				}
  3651 			}
  3651 			}
  3652 		} else {
  3652 		} else {
  3653 			static const byte _depot_bits[] = {0x19,0x16,0x25,0x2A};
  3653 			static const byte _depot_bits[] = {0x19, 0x16, 0x25, 0x2A};
  3654 
  3654 
  3655 			DiagDirection dir = GetRailDepotDirection(tile);
  3655 			DiagDirection dir = GetRailDepotDirection(tile);
  3656 
  3656 
  3657 			if (GetRailTrackStatus(tile + TileOffsByDiagDir(dir)) & _depot_bits[dir])
  3657 			if (GetRailTrackStatus(tile + TileOffsByDiagDir(dir)) & _depot_bits[dir])
  3658 				return;
  3658 				return;
  3793 		int32 best_val = -1;
  3793 		int32 best_val = -1;
  3794 
  3794 
  3795 		// Ask the guy with the highest performance hist.
  3795 		// Ask the guy with the highest performance hist.
  3796 		FOR_ALL_PLAYERS(pp) {
  3796 		FOR_ALL_PLAYERS(pp) {
  3797 			if (pp->is_active &&
  3797 			if (pp->is_active &&
  3798 					!(asked&1) &&
  3798 					!(asked & 1) &&
  3799 					pp->bankrupt_asked == 0 &&
  3799 					pp->bankrupt_asked == 0 &&
  3800 					best_val < pp->old_economy[1].performance_history) {
  3800 					best_val < pp->old_economy[1].performance_history) {
  3801 				best_val = pp->old_economy[1].performance_history;
  3801 				best_val = pp->old_economy[1].performance_history;
  3802 				best_pl = pp;
  3802 				best_pl = pp;
  3803 			}
  3803 			}
  3804 			asked>>=1;
  3804 			asked >>= 1;
  3805 		}
  3805 		}
  3806 
  3806 
  3807 		// Asked all players?
  3807 		// Asked all players?
  3808 		if (best_val == -1) {
  3808 		if (best_val == -1) {
  3809 			p->bankrupt_asked = 255;
  3809 			p->bankrupt_asked = 255;
  3841 		}
  3841 		}
  3842 	} else if (p->player_money < base * 500) {
  3842 	} else if (p->player_money < base * 500) {
  3843 		// Increase loan
  3843 		// Increase loan
  3844 		if (p->current_loan < _economy.max_loan &&
  3844 		if (p->current_loan < _economy.max_loan &&
  3845 				p->num_valid_stat_ent >= 2 &&
  3845 				p->num_valid_stat_ent >= 2 &&
  3846 				-(p->old_economy[0].expenses+p->old_economy[1].expenses) < base * 60) {
  3846 				-(p->old_economy[0].expenses + p->old_economy[1].expenses) < base * 60) {
  3847 			DoCommand(0, 0, 0, DC_EXEC, CMD_INCREASE_LOAN);
  3847 			DoCommand(0, 0, 0, DC_EXEC, CMD_INCREASE_LOAN);
  3848 		}
  3848 		}
  3849 	}
  3849 	}
  3850 }
  3850 }
  3851 
  3851 
  3870 
  3870 
  3871 	// Ugly hack to make sure the service interval of the AI is good, not looking
  3871 	// Ugly hack to make sure the service interval of the AI is good, not looking
  3872 	//  to the patch-setting
  3872 	//  to the patch-setting
  3873 	// Also, it takes into account the setting if the service-interval is in days
  3873 	// Also, it takes into account the setting if the service-interval is in days
  3874 	//  or in %
  3874 	//  or in %
  3875 	_ai_service_interval = _patches.servint_ispercent?80:180;
  3875 	_ai_service_interval = _patches.servint_ispercent ? 80 : 180;
  3876 
  3876 
  3877 	if (IsHumanPlayer(_current_player)) return;
  3877 	if (IsHumanPlayer(_current_player)) return;
  3878 
  3878 
  3879 	AiAdjustLoan(p);
  3879 	AiAdjustLoan(p);
  3880 	AiBuildCompanyHQ(p);
  3880 	AiBuildCompanyHQ(p);
  3881 
  3881 
  3882 #if 0
  3882 #if 0
  3883 	{
  3883 	{
  3884 		static byte old_state = 99;
  3884 		static byte old_state = 99;
  3885 		static bool hasdots = false;
  3885 		static bool hasdots = false;
  3886 		char *_ai_state_names[]={
  3886 		char *_ai_state_names[] = {
  3887 			"AiCase0",
  3887 			"AiCase0",
  3888 			"AiCase1",
  3888 			"AiCase1",
  3889 			"AiStateVehLoop",
  3889 			"AiStateVehLoop",
  3890 			"AiStateCheckReplaceVehicle",
  3890 			"AiStateCheckReplaceVehicle",
  3891 			"AiStateDoReplaceVehicle",
  3891 			"AiStateDoReplaceVehicle",