src/ai/trolly/trolly.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
   640 		return;
   640 		return;
   641 	}
   641 	}
   642 
   642 
   643 	if (new_tile == 0 && p->ainew.tbt == AI_BUS) {
   643 	if (new_tile == 0 && p->ainew.tbt == AI_BUS) {
   644 		uint x, y, i = 0;
   644 		uint x, y, i = 0;
   645 		int r;
   645 		CommandCost r;
   646 		uint best;
   646 		uint best;
   647 		uint accepts[NUM_CARGO];
   647 		uint accepts[NUM_CARGO];
   648 		TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4];
   648 		TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4];
   649 		uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4];
   649 		uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4];
   650 		// To find a good spot we scan a range from the center, a get the point
   650 		// To find a good spot we scan a range from the center, a get the point
   651 		//  where we get the most cargo and where it is buildable.
   651 		//  where we get the most cargo and where it is buildable.
   652 		// TODO: also check for station of myself and make sure we are not
   652 		// TODO: also check for station of myself and make sure we are not
   653 		//   taking eachothers passangers away (bad result when it does not)
   653 		//   taking eachothers passengers away (bad result when it does not)
   654 		for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) {
   654 		for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) {
   655 			for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) {
   655 			for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) {
   656 				new_tile = TileXY(x, y);
   656 				new_tile = TileXY(x, y);
   657 				if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) {
   657 				if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) {
   658 					// This tile we can build on!
   658 					// This tile we can build on!
   786 	// To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there..
   786 	// To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there..
   787 	// Simple, easy, works!
   787 	// Simple, easy, works!
   788 	// To make the depot stand in the middle of the route, we start from the center..
   788 	// To make the depot stand in the middle of the route, we start from the center..
   789 	// But first we walk through the route see if we can find a depot that is ours
   789 	// But first we walk through the route see if we can find a depot that is ours
   790 	//  this keeps things nice ;)
   790 	//  this keeps things nice ;)
   791 	int g, i, r;
   791 	int g, i;
       
   792 	CommandCost r;
   792 	DiagDirection j;
   793 	DiagDirection j;
   793 	TileIndex tile;
   794 	TileIndex tile;
   794 	assert(p->ainew.state == AI_STATE_FIND_DEPOT);
   795 	assert(p->ainew.state == AI_STATE_FIND_DEPOT);
   795 
   796 
   796 	p->ainew.depot_tile = 0;
   797 	p->ainew.depot_tile = 0;
   982 
   983 
   983 
   984 
   984 // Build the stations
   985 // Build the stations
   985 static void AiNew_State_BuildStation(Player *p)
   986 static void AiNew_State_BuildStation(Player *p)
   986 {
   987 {
   987 	int res = 0;
   988 	CommandCost res = 0;
   988 	assert(p->ainew.state == AI_STATE_BUILD_STATION);
   989 	assert(p->ainew.state == AI_STATE_BUILD_STATION);
   989 	if (p->ainew.temp == 0) {
   990 	if (p->ainew.temp == 0) {
   990 		if (!IsTileType(p->ainew.from_tile, MP_STATION))
   991 		if (!IsTileType(p->ainew.from_tile, MP_STATION))
   991 			res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC);
   992 			res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC);
   992 	} else {
   993 	} else {
  1035 		if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
  1036 		if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
  1036 			// If they not queue, they have to go up and down to try again at a station...
  1037 			// If they not queue, they have to go up and down to try again at a station...
  1037 			// We don't want that, so try building some road left or right of the station
  1038 			// We don't want that, so try building some road left or right of the station
  1038 			int dir1, dir2, dir3;
  1039 			int dir1, dir2, dir3;
  1039 			TileIndex tile;
  1040 			TileIndex tile;
  1040 			int i, ret;
  1041 			CommandCost ret;
  1041 			for (i=0;i<2;i++) {
  1042 			for (int i = 0; i < 2; i++) {
  1042 				if (i == 0) {
  1043 				if (i == 0) {
  1043 					tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction);
  1044 					tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction);
  1044 					dir1 = p->ainew.from_direction - 1;
  1045 					dir1 = p->ainew.from_direction - 1;
  1045 					if (dir1 < 0) dir1 = 3;
  1046 					if (dir1 < 0) dir1 = 3;
  1046 					dir2 = p->ainew.from_direction + 1;
  1047 					dir2 = p->ainew.from_direction + 1;
  1054 					if (dir2 > 3) dir2 = 0;
  1055 					if (dir2 > 3) dir2 = 0;
  1055 					dir3 = p->ainew.to_direction;
  1056 					dir3 = p->ainew.to_direction;
  1056 				}
  1057 				}
  1057 
  1058 
  1058 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1059 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1059 				if (!CmdFailed(ret)) {
  1060 				if (CmdSucceeded(ret)) {
  1060 					dir1 = TileOffsByDiagDir(dir1);
  1061 					dir1 = TileOffsByDiagDir(dir1);
  1061 					if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
  1062 					if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
  1062 						ret = AI_DoCommand(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1063 						ret = AI_DoCommand(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1063 						if (!CmdFailed(ret)) {
  1064 						if (CmdSucceeded(ret)) {
  1064 							if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES))
  1065 							if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES))
  1065 								AI_DoCommand(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1066 								AI_DoCommand(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1066 						}
  1067 						}
  1067 					}
  1068 					}
  1068 				}
  1069 				}
  1069 
  1070 
  1070 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1071 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1071 				if (!CmdFailed(ret)) {
  1072 				if (CmdSucceeded(ret)) {
  1072 					dir2 = TileOffsByDiagDir(dir2);
  1073 					dir2 = TileOffsByDiagDir(dir2);
  1073 					if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
  1074 					if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
  1074 						ret = AI_DoCommand(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1075 						ret = AI_DoCommand(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1075 						if (!CmdFailed(ret)) {
  1076 						if (CmdSucceeded(ret)) {
  1076 							if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES))
  1077 							if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES))
  1077 								AI_DoCommand(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1078 								AI_DoCommand(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1078 						}
  1079 						}
  1079 					}
  1080 					}
  1080 				}
  1081 				}
  1081 
  1082 
  1082 				ret = AI_DoCommand(tile, DiagDirToRoadBits((DiagDirection)dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1083 				ret = AI_DoCommand(tile, DiagDirToRoadBits((DiagDirection)dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1083 				if (!CmdFailed(ret)) {
  1084 				if (CmdSucceeded(ret)) {
  1084 					dir3 = TileOffsByDiagDir(dir3);
  1085 					dir3 = TileOffsByDiagDir(dir3);
  1085 					if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
  1086 					if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
  1086 						ret = AI_DoCommand(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1087 						ret = AI_DoCommand(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1087 						if (!CmdFailed(ret)) {
  1088 						if (CmdSucceeded(ret)) {
  1088 							if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES))
  1089 							if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES))
  1089 								AI_DoCommand(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1090 								AI_DoCommand(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1090 						}
  1091 						}
  1091 					}
  1092 					}
  1092 				}
  1093 				}
  1100 
  1101 
  1101 
  1102 
  1102 // Builds the depot
  1103 // Builds the depot
  1103 static void AiNew_State_BuildDepot(Player *p)
  1104 static void AiNew_State_BuildDepot(Player *p)
  1104 {
  1105 {
  1105 	int res = 0;
  1106 	CommandCost res = 0;
  1106 	assert(p->ainew.state == AI_STATE_BUILD_DEPOT);
  1107 	assert(p->ainew.state == AI_STATE_BUILD_DEPOT);
  1107 
  1108 
  1108 	if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) {
  1109 	if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) {
  1109 		if (IsTileOwner(p->ainew.depot_tile, _current_player)) {
  1110 		if (IsTileOwner(p->ainew.depot_tile, _current_player)) {
  1110 			// The depot is already built
  1111 			// The depot is already built
  1135 
  1136 
  1136 
  1137 
  1137 // Build vehicles
  1138 // Build vehicles
  1138 static void AiNew_State_BuildVehicle(Player *p)
  1139 static void AiNew_State_BuildVehicle(Player *p)
  1139 {
  1140 {
  1140 	int res;
  1141 	CommandCost res;
  1141 	assert(p->ainew.state == AI_STATE_BUILD_VEHICLE);
  1142 	assert(p->ainew.state == AI_STATE_BUILD_VEHICLE);
  1142 
  1143 
  1143 	// Check if we need to build a vehicle
  1144 	// Check if we need to build a vehicle
  1144 	if (p->ainew.amount_veh == 0) {
  1145 	if (p->ainew.amount_veh == 0) {
  1145 		// Nope, we are done!
  1146 		// Nope, we are done!
  1275 				return;
  1276 				return;
  1276 			}
  1277 			}
  1277 
  1278 
  1278 			if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return;
  1279 			if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return;
  1279 			{
  1280 			{
  1280 				int ret = 0;
  1281 				CommandCost ret = 0;
  1281 				if (v->type == VEH_ROAD)
  1282 				if (v->type == VEH_ROAD)
  1282 					ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
  1283 					ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
  1283 				// This means we can not find a depot :s
  1284 				// This means we can not find a depot :s
  1284 				//				if (CmdFailed(ret))
  1285 				//				if (CmdFailed(ret))
  1285 			}
  1286 			}