src/ai/trolly/trolly.cpp
changeset 7317 e61af9d8a2b3
parent 7010 6f0d9f03180d
child 7370 41adc721b1fa
equal deleted inserted replaced
7316:123a3a88b9c2 7317:e61af9d8a2b3
   571 	TileIndex tile;
   571 	TileIndex tile;
   572 	Station *st;
   572 	Station *st;
   573 	int count = 0;
   573 	int count = 0;
   574 	EngineID i;
   574 	EngineID i;
   575 	TileIndex new_tile = 0;
   575 	TileIndex new_tile = 0;
   576 	byte direction = 0;
   576 	DiagDirection direction = DIAGDIR_NE;
   577 	Town *town = NULL;
   577 	Town *town = NULL;
   578 	assert(p->ainew.state == AI_STATE_FIND_STATION);
   578 	assert(p->ainew.state == AI_STATE_FIND_STATION);
   579 
   579 
   580 	if (p->ainew.from_tile == 0) {
   580 	if (p->ainew.from_tile == 0) {
   581 		// First we scan for a station in the from-city
   581 		// First we scan for a station in the from-city
   690 
   690 
   691 		// See how much it is going to cost us...
   691 		// See how much it is going to cost us...
   692 		r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
   692 		r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
   693 		p->ainew.new_cost += r.GetCost();
   693 		p->ainew.new_cost += r.GetCost();
   694 
   694 
   695 		direction = AI_PATHFINDER_NO_DIRECTION;
   695 		direction = (DiagDirection)AI_PATHFINDER_NO_DIRECTION;
   696 	} else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) {
   696 	} else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) {
   697 		// Truck station locater works differently.. a station can be on any place
   697 		// Truck station locater works differently.. a station can be on any place
   698 		//  as long as it is in range. So we give back code AI_STATION_RANGE
   698 		//  as long as it is in range. So we give back code AI_STATION_RANGE
   699 		//  so the pathfinder routine can work it out!
   699 		//  so the pathfinder routine can work it out!
   700 		new_tile = AI_STATION_RANGE;
   700 		new_tile = AI_STATION_RANGE;
   701 		direction = AI_PATHFINDER_NO_DIRECTION;
   701 		direction = (DiagDirection)AI_PATHFINDER_NO_DIRECTION;
   702 	}
   702 	}
   703 
   703 
   704 	if (p->ainew.from_tile == 0) {
   704 	if (p->ainew.from_tile == 0) {
   705 		p->ainew.from_tile = new_tile;
   705 		p->ainew.from_tile = new_tile;
   706 		p->ainew.from_direction = direction;
   706 		p->ainew.from_direction = direction;
  1033 		// This means we are done building!
  1033 		// This means we are done building!
  1034 
  1034 
  1035 		if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
  1035 		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...
  1036 			// 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
  1037 			// We don't want that, so try building some road left or right of the station
  1038 			int dir1, dir2, dir3;
  1038 			DiagDirection dir1, dir2, dir3;
  1039 			TileIndex tile;
  1039 			TileIndex tile;
  1040 			CommandCost ret;
  1040 			CommandCost ret;
  1041 			for (int i = 0; i < 2; i++) {
  1041 			for (int i = 0; i < 2; i++) {
  1042 				if (i == 0) {
  1042 				if (i == 0) {
  1043 					tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction);
  1043 					tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction);
  1044 					dir1 = p->ainew.from_direction - 1;
  1044 					dir1 = ChangeDiagDir(p->ainew.from_direction, DIAGDIRDIFF_90LEFT);
  1045 					if (dir1 < 0) dir1 = 3;
  1045 					dir2 = ChangeDiagDir(p->ainew.from_direction, DIAGDIRDIFF_90RIGHT);
  1046 					dir2 = p->ainew.from_direction + 1;
       
  1047 					if (dir2 > 3) dir2 = 0;
       
  1048 					dir3 = p->ainew.from_direction;
  1046 					dir3 = p->ainew.from_direction;
  1049 				} else {
  1047 				} else {
  1050 					tile = p->ainew.to_tile + TileOffsByDiagDir(p->ainew.to_direction);
  1048 					tile = p->ainew.to_tile + TileOffsByDiagDir(p->ainew.to_direction);
  1051 					dir1 = p->ainew.to_direction - 1;
  1049 					dir1 = ChangeDiagDir(p->ainew.to_direction, DIAGDIRDIFF_90LEFT);
  1052 					if (dir1 < 0) dir1 = 3;
  1050 					dir2 = ChangeDiagDir(p->ainew.to_direction, DIAGDIRDIFF_90RIGHT);
  1053 					dir2 = p->ainew.to_direction + 1;
       
  1054 					if (dir2 > 3) dir2 = 0;
       
  1055 					dir3 = p->ainew.to_direction;
  1051 					dir3 = p->ainew.to_direction;
  1056 				}
  1052 				}
  1057 
  1053 
  1058 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1054 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1059 				if (CmdSucceeded(ret)) {
  1055 				if (CmdSucceeded(ret)) {
  1060 					dir1 = TileOffsByDiagDir(dir1);
  1056 					TileIndex offset = TileOffsByDiagDir(dir1);
  1061 					if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
  1057 					if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, 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);
  1058 						ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1063 						if (CmdSucceeded(ret)) {
  1059 						if (CmdSucceeded(ret)) {
  1064 							if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES))
  1060 							if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, 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);
  1061 								AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1066 						}
  1062 						}
  1067 					}
  1063 					}
  1068 				}
  1064 				}
  1069 
  1065 
  1070 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir((DiagDirection)dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1066 				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1071 				if (CmdSucceeded(ret)) {
  1067 				if (CmdSucceeded(ret)) {
  1072 					dir2 = TileOffsByDiagDir(dir2);
  1068 					TileIndex offset = TileOffsByDiagDir(dir2);
  1073 					if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
  1069 					if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, 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);
  1070 						ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1075 						if (CmdSucceeded(ret)) {
  1071 						if (CmdSucceeded(ret)) {
  1076 							if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES))
  1072 							if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, 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);
  1073 								AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1078 						}
  1074 						}
  1079 					}
  1075 					}
  1080 				}
  1076 				}
  1081 
  1077 
  1082 				ret = AI_DoCommand(tile, DiagDirToRoadBits((DiagDirection)dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1078 				ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1083 				if (CmdSucceeded(ret)) {
  1079 				if (CmdSucceeded(ret)) {
  1084 					dir3 = TileOffsByDiagDir(dir3);
  1080 					TileIndex offset = TileOffsByDiagDir(dir3);
  1085 					if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
  1081 					if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, 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);
  1082 						ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1087 						if (CmdSucceeded(ret)) {
  1083 						if (CmdSucceeded(ret)) {
  1088 							if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES))
  1084 							if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, 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);
  1085 								AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1090 						}
  1086 						}
  1091 					}
  1087 					}
  1092 				}
  1088 				}
  1093 			}
  1089 			}
  1094 		}
  1090 		}