src/ai/default/default.cpp
changeset 5587 167d9a91ef02
parent 5584 1111b4d36e35
child 5602 bccd4b89e536
equal deleted inserted replaced
5586:2d4126d81ebb 5587:167d9a91ef02
    24 #include "../../airport.h"
    24 #include "../../airport.h"
    25 #include "../../depot.h"
    25 #include "../../depot.h"
    26 #include "../../variables.h"
    26 #include "../../variables.h"
    27 #include "../../bridge.h"
    27 #include "../../bridge.h"
    28 #include "../../date.h"
    28 #include "../../date.h"
       
    29 #include "../../helpers.hpp"
    29 #include "default.h"
    30 #include "default.h"
    30 
    31 
    31 // remove some day perhaps?
    32 // remove some day perhaps?
    32 static uint _ai_service_interval;
    33 static uint _ai_service_interval;
    33 
    34 
    61 };
    62 };
    62 
    63 
    63 
    64 
    64 #include "../../table/ai_rail.h"
    65 #include "../../table/ai_rail.h"
    65 
    66 
    66 static byte GetRailTrackStatus(TileIndex tile)
    67 static TrackBits GetRailTrackStatus(TileIndex tile)
    67 {
    68 {
    68 	uint32 r = GetTileTrackStatus(tile, TRANSPORT_RAIL);
    69 	uint32 r = GetTileTrackStatus(tile, TRANSPORT_RAIL);
    69 	return (byte) (r | r >> 8);
    70 	return (TrackBits)(byte) (r | r >> 8);
    70 }
    71 }
    71 
    72 
    72 
    73 
    73 static void AiCase0(Player *p)
    74 static void AiCase0(Player *p)
    74 {
    75 {
   642 			!CHANCE16(1, 5)) {
   643 			!CHANCE16(1, 5)) {
   643 		return false;
   644 		return false;
   644 	}
   645 	}
   645 
   646 
   646 	if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) {
   647 	if (fr->cargo == CT_PASSENGERS || fr->cargo == CT_MAIL) {
   647 		const Town* from = fr->from;
   648 		const Town* from = (const Town*)fr->from;
   648 		const Town* to   = fr->to;
   649 		const Town* to   = (const Town*)fr->to;
   649 
   650 
   650 		if (from->pct_pass_transported > 0x99 ||
   651 		if (from->pct_pass_transported > 0x99 ||
   651 				to->pct_pass_transported > 0x99) {
   652 				to->pct_pass_transported > 0x99) {
   652 			return false;
   653 			return false;
   653 		}
   654 		}
   781 	p->ai.order_list_blocks[0] = 0;
   782 	p->ai.order_list_blocks[0] = 0;
   782 	p->ai.order_list_blocks[1] = 1;
   783 	p->ai.order_list_blocks[1] = 1;
   783 	p->ai.order_list_blocks[2] = 255;
   784 	p->ai.order_list_blocks[2] = 255;
   784 
   785 
   785 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
   786 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
   786 	p->ai.state_mode = -1;
   787 	p->ai.state_mode = UCHAR_MAX;
   787 	p->ai.state_counter = 0;
   788 	p->ai.state_counter = 0;
   788 	p->ai.timeout_counter = 0;
   789 	p->ai.timeout_counter = 0;
   789 }
   790 }
   790 
   791 
   791 static void AiWantMediumIndustryRoute(Player *p)
   792 static void AiWantMediumIndustryRoute(Player *p)
   849 	p->ai.wagon_list[0] = INVALID_VEHICLE;
   850 	p->ai.wagon_list[0] = INVALID_VEHICLE;
   850 	p->ai.order_list_blocks[0] = 0;
   851 	p->ai.order_list_blocks[0] = 0;
   851 	p->ai.order_list_blocks[1] = 1;
   852 	p->ai.order_list_blocks[1] = 1;
   852 	p->ai.order_list_blocks[2] = 255;
   853 	p->ai.order_list_blocks[2] = 255;
   853 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
   854 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
   854 	p->ai.state_mode = -1;
   855 	p->ai.state_mode = UCHAR_MAX;
   855 	p->ai.state_counter = 0;
   856 	p->ai.state_counter = 0;
   856 	p->ai.timeout_counter = 0;
   857 	p->ai.timeout_counter = 0;
   857 }
   858 }
   858 
   859 
   859 static void AiWantShortIndustryRoute(Player *p)
   860 static void AiWantShortIndustryRoute(Player *p)
   917 	p->ai.wagon_list[0] = INVALID_VEHICLE;
   918 	p->ai.wagon_list[0] = INVALID_VEHICLE;
   918 	p->ai.order_list_blocks[0] = 0;
   919 	p->ai.order_list_blocks[0] = 0;
   919 	p->ai.order_list_blocks[1] = 1;
   920 	p->ai.order_list_blocks[1] = 1;
   920 	p->ai.order_list_blocks[2] = 255;
   921 	p->ai.order_list_blocks[2] = 255;
   921 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
   922 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
   922 	p->ai.state_mode = -1;
   923 	p->ai.state_mode = UCHAR_MAX;
   923 	p->ai.state_counter = 0;
   924 	p->ai.state_counter = 0;
   924 	p->ai.timeout_counter = 0;
   925 	p->ai.timeout_counter = 0;
   925 }
   926 }
   926 
   927 
   927 static void AiWantMailRoute(Player *p)
   928 static void AiWantMailRoute(Player *p)
  1018 	p->ai.wagon_list[0] = INVALID_VEHICLE;
  1019 	p->ai.wagon_list[0] = INVALID_VEHICLE;
  1019 	p->ai.order_list_blocks[0] = 0;
  1020 	p->ai.order_list_blocks[0] = 0;
  1020 	p->ai.order_list_blocks[1] = 1;
  1021 	p->ai.order_list_blocks[1] = 1;
  1021 	p->ai.order_list_blocks[2] = 255;
  1022 	p->ai.order_list_blocks[2] = 255;
  1022 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
  1023 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
  1023 	p->ai.state_mode = -1;
  1024 	p->ai.state_mode = UCHAR_MAX;
  1024 	p->ai.state_counter = 0;
  1025 	p->ai.state_counter = 0;
  1025 	p->ai.timeout_counter = 0;
  1026 	p->ai.timeout_counter = 0;
  1026 }
  1027 }
  1027 
  1028 
  1028 static void AiWantPassengerRoute(Player *p)
  1029 static void AiWantPassengerRoute(Player *p)
  1087 	p->ai.wagon_list[0] = INVALID_VEHICLE;
  1088 	p->ai.wagon_list[0] = INVALID_VEHICLE;
  1088 	p->ai.order_list_blocks[0] = 0;
  1089 	p->ai.order_list_blocks[0] = 0;
  1089 	p->ai.order_list_blocks[1] = 1;
  1090 	p->ai.order_list_blocks[1] = 1;
  1090 	p->ai.order_list_blocks[2] = 255;
  1091 	p->ai.order_list_blocks[2] = 255;
  1091 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
  1092 	p->ai.state = AIS_BUILD_DEFAULT_RAIL_BLOCKS;
  1092 	p->ai.state_mode = -1;
  1093 	p->ai.state_mode = UCHAR_MAX;
  1093 	p->ai.state_counter = 0;
  1094 	p->ai.state_counter = 0;
  1094 	p->ai.timeout_counter = 0;
  1095 	p->ai.timeout_counter = 0;
  1095 }
  1096 }
  1096 
  1097 
  1097 static void AiWantTrainRoute(Player *p)
  1098 static void AiWantTrainRoute(Player *p)
  1162 	p->ai.order_list_blocks[0] = 0;
  1163 	p->ai.order_list_blocks[0] = 0;
  1163 	p->ai.order_list_blocks[1] = 1;
  1164 	p->ai.order_list_blocks[1] = 1;
  1164 	p->ai.order_list_blocks[2] = 255;
  1165 	p->ai.order_list_blocks[2] = 255;
  1165 
  1166 
  1166 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1167 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1167 	p->ai.state_mode = -1;
  1168 	p->ai.state_mode = UCHAR_MAX;
  1168 	p->ai.state_counter = 0;
  1169 	p->ai.state_counter = 0;
  1169 	p->ai.timeout_counter = 0;
  1170 	p->ai.timeout_counter = 0;
  1170 }
  1171 }
  1171 
  1172 
  1172 static void AiWantMediumRoadIndustryRoute(Player *p)
  1173 static void AiWantMediumRoadIndustryRoute(Player *p)
  1218 	p->ai.order_list_blocks[0] = 0;
  1219 	p->ai.order_list_blocks[0] = 0;
  1219 	p->ai.order_list_blocks[1] = 1;
  1220 	p->ai.order_list_blocks[1] = 1;
  1220 	p->ai.order_list_blocks[2] = 255;
  1221 	p->ai.order_list_blocks[2] = 255;
  1221 
  1222 
  1222 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1223 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1223 	p->ai.state_mode = -1;
  1224 	p->ai.state_mode = UCHAR_MAX;
  1224 	p->ai.state_counter = 0;
  1225 	p->ai.state_counter = 0;
  1225 	p->ai.timeout_counter = 0;
  1226 	p->ai.timeout_counter = 0;
  1226 }
  1227 }
  1227 
  1228 
  1228 static void AiWantLongRoadPassengerRoute(Player *p)
  1229 static void AiWantLongRoadPassengerRoute(Player *p)
  1276 	p->ai.order_list_blocks[0] = 0;
  1277 	p->ai.order_list_blocks[0] = 0;
  1277 	p->ai.order_list_blocks[1] = 1;
  1278 	p->ai.order_list_blocks[1] = 1;
  1278 	p->ai.order_list_blocks[2] = 255;
  1279 	p->ai.order_list_blocks[2] = 255;
  1279 
  1280 
  1280 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1281 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1281 	p->ai.state_mode = -1;
  1282 	p->ai.state_mode = UCHAR_MAX;
  1282 	p->ai.state_counter = 0;
  1283 	p->ai.state_counter = 0;
  1283 	p->ai.timeout_counter = 0;
  1284 	p->ai.timeout_counter = 0;
  1284 }
  1285 }
  1285 
  1286 
  1286 static void AiWantPassengerRouteInsideTown(Player *p)
  1287 static void AiWantPassengerRouteInsideTown(Player *p)
  1332 	p->ai.order_list_blocks[0] = 0;
  1333 	p->ai.order_list_blocks[0] = 0;
  1333 	p->ai.order_list_blocks[1] = 1;
  1334 	p->ai.order_list_blocks[1] = 1;
  1334 	p->ai.order_list_blocks[2] = 255;
  1335 	p->ai.order_list_blocks[2] = 255;
  1335 
  1336 
  1336 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1337 	p->ai.state = AIS_BUILD_DEFAULT_ROAD_BLOCKS;
  1337 	p->ai.state_mode = -1;
  1338 	p->ai.state_mode = UCHAR_MAX;
  1338 	p->ai.state_counter = 0;
  1339 	p->ai.state_counter = 0;
  1339 	p->ai.timeout_counter = 0;
  1340 	p->ai.timeout_counter = 0;
  1340 }
  1341 }
  1341 
  1342 
  1342 static void AiWantRoadRoute(Player *p)
  1343 static void AiWantRoadRoute(Player *p)
  1850 
  1851 
  1851 	arpfd.tile = p->ai.start_tile_a;
  1852 	arpfd.tile = p->ai.start_tile_a;
  1852 	arpfd.tile2 = p->ai.cur_tile_a;
  1853 	arpfd.tile2 = p->ai.cur_tile_a;
  1853 	arpfd.flag = false;
  1854 	arpfd.flag = false;
  1854 	arpfd.count = 0;
  1855 	arpfd.count = 0;
  1855 	FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, p->ai.cur_dir_a^2,
  1856 	FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a^2),
  1856 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
  1857 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
  1857 	return arpfd.count > 8;
  1858 	return arpfd.count > 8;
  1858 }
  1859 }
  1859 
  1860 
  1860 typedef struct AiRailFinder {
  1861 typedef struct AiRailFinder {
  1939 			arf->best_tile = 0;
  1940 			arf->best_tile = 0;
  1940 			better = true;
  1941 			better = true;
  1941 		}
  1942 		}
  1942 	}
  1943 	}
  1943 	arf->recursive_mode = 0;
  1944 	arf->recursive_mode = 0;
  1944 	arf->cur_best_dist = (uint)-1;
  1945 	arf->cur_best_dist = UINT_MAX;
  1945 	arf->cur_best_depth = 0xff;
  1946 	arf->cur_best_depth = 0xff;
  1946 
  1947 
  1947 	return better;
  1948 	return better;
  1948 }
  1949 }
  1949 
  1950 
  2183 			p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDiagDir(p->ai.cur_dir_a));
  2184 			p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDiagDir(p->ai.cur_dir_a));
  2184 			return true;
  2185 			return true;
  2185 		} else {
  2186 		} else {
  2186 			// Check if the bridge points in the right direction.
  2187 			// Check if the bridge points in the right direction.
  2187 			// This is not really needed the first place AiRemoveTileAndGoForward is called.
  2188 			// This is not really needed the first place AiRemoveTileAndGoForward is called.
  2188 			if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1U)) return false;
  2189 			if (DiagDirToAxis(GetBridgeRampDirection(tile)) != (p->ai.cur_dir_a & 1)) return false;
  2189 
  2190 
  2190 			tile = GetOtherBridgeEnd(tile);
  2191 			tile = GetOtherBridgeEnd(tile);
  2191 
  2192 
  2192 			tilenew = TILE_MASK(tile - TileOffsByDiagDir(p->ai.cur_dir_a));
  2193 			tilenew = TILE_MASK(tile - TileOffsByDiagDir(p->ai.cur_dir_a));
  2193 			// And clear the bridge.
  2194 			// And clear the bridge.
  2818 	if (bits == 0) return false;
  2819 	if (bits == 0) return false;
  2819 
  2820 
  2820 	are.best_dist = (uint)-1;
  2821 	are.best_dist = (uint)-1;
  2821 
  2822 
  2822 	for_each_bit(i, bits) {
  2823 	for_each_bit(i, bits) {
  2823 		FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
  2824 		FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, (DiagDirection)_dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
  2824 	}
  2825 	}
  2825 
  2826 
  2826 	if (DistanceManhattan(tile, are.dest) <= are.best_dist) return false;
  2827 	if (DistanceManhattan(tile, are.dest) <= are.best_dist) return false;
  2827 
  2828 
  2828 	if (are.best_dist == 0) return true;
  2829 	if (are.best_dist == 0) return true;
  3581 
  3582 
  3582 	// Go to this state when we're done.
  3583 	// Go to this state when we're done.
  3583 	p->ai.state = AIS_1;
  3584 	p->ai.state = AIS_1;
  3584 
  3585 
  3585 	// Get a list of all stations that are in use by a vehicle
  3586 	// Get a list of all stations that are in use by a vehicle
  3586 	in_use = malloc(GetMaxStationIndex() + 1);
  3587 	MallocT(&in_use, GetMaxStationIndex() + 1);
  3587 	memset(in_use, 0, GetMaxStationIndex() + 1);
  3588 	memset(in_use, 0, GetMaxStationIndex() + 1);
  3588 	FOR_ALL_ORDERS(ord) {
  3589 	FOR_ALL_ORDERS(ord) {
  3589 		if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
  3590 		if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
  3590 	}
  3591 	}
  3591 
  3592 
  3703 				!IsBridge(tile) ||
  3704 				!IsBridge(tile) ||
  3704 				GetBridgeTransportType(tile) != TRANSPORT_RAIL) {
  3705 				GetBridgeTransportType(tile) != TRANSPORT_RAIL) {
  3705 			return;
  3706 			return;
  3706 		}
  3707 		}
  3707 
  3708 
  3708 		rails = 0;
  3709 		rails = TRACK_BIT_NONE;
  3709 
  3710 
  3710 		switch (GetBridgeRampDirection(tile)) {
  3711 		switch (GetBridgeRampDirection(tile)) {
  3711 			default:
  3712 			default:
  3712 			case DIAGDIR_NE: goto pos_2;
  3713 			case DIAGDIR_NE: goto pos_2;
  3713 			case DIAGDIR_SE: goto pos_3;
  3714 			case DIAGDIR_SE: goto pos_3;
  3796 
  3797 
  3797 	{
  3798 	{
  3798 		uint asked = p->bankrupt_asked;
  3799 		uint asked = p->bankrupt_asked;
  3799 		Player *pp, *best_pl = NULL;
  3800 		Player *pp, *best_pl = NULL;
  3800 		int32 best_val = -1;
  3801 		int32 best_val = -1;
  3801 		uint old_p;
       
  3802 
  3802 
  3803 		// Ask the guy with the highest performance hist.
  3803 		// Ask the guy with the highest performance hist.
  3804 		FOR_ALL_PLAYERS(pp) {
  3804 		FOR_ALL_PLAYERS(pp) {
  3805 			if (pp->is_active &&
  3805 			if (pp->is_active &&
  3806 					!(asked&1) &&
  3806 					!(asked&1) &&
  3828 		if (IsHumanPlayer(best_pl->index)) return;
  3828 		if (IsHumanPlayer(best_pl->index)) return;
  3829 
  3829 
  3830 		// Too little money for computer to buy it?
  3830 		// Too little money for computer to buy it?
  3831 		if (best_pl->player_money >> 1 >= p->bankrupt_value) {
  3831 		if (best_pl->player_money >> 1 >= p->bankrupt_value) {
  3832 			// Computer wants to buy it.
  3832 			// Computer wants to buy it.
  3833 			old_p = _current_player;
  3833 			PlayerID old_p = _current_player;
  3834 			_current_player = p->index;
  3834 			_current_player = p->index;
  3835 			DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY);
  3835 			DoCommand(0, old_p, 0, DC_EXEC, CMD_BUY_COMPANY);
  3836 			_current_player = old_p;
  3836 			_current_player = old_p;
  3837 		}
  3837 		}
  3838 	}
  3838 	}