diff --git a/src/airport_gui.cpp b/src/airport_gui.cpp index de467e0a5c..e4d7d7fddd 100644 --- a/src/airport_gui.cpp +++ b/src/airport_gui.cpp @@ -73,7 +73,7 @@ static void PlaceAirport(TileIndex tile) auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), tile, airport_type, layout, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(tile, airport_type, layout, StationID::Invalid(), adjacent).Succeeded(); } else { return Command::Post(STR_ERROR_CAN_T_BUILD_AIRPORT_HERE, CcBuildAirport, tile, airport_type, layout, to_join, adjacent); } diff --git a/src/command_func.h b/src/command_func.h index cb3ba95610..8771635b53 100644 --- a/src/command_func.h +++ b/src/command_func.h @@ -164,6 +164,17 @@ public: return res; } + /** + * Shortcut to query a command with its flags to test if it will succeed. + * @param args Parameters for the command. + * @return cost of the command. + */ + static CommandCost Query(Targs... args) + { + Tret res = Do(CommandFlagsToDCFlags(GetCommandFlags()), args...); + return ExtractCommandCost(res); + } + /** * Shortcut for the long Post when not using a callback. * @param err_message Message prefix to show on error diff --git a/src/dock_gui.cpp b/src/dock_gui.cpp index 68f3959e90..d4f3555245 100644 --- a/src/dock_gui.cpp +++ b/src/dock_gui.cpp @@ -219,7 +219,7 @@ struct BuildDocksToolbarWindow : Window { bool adjacent = _ctrl_pressed; auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), tile, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(tile, StationID::Invalid(), adjacent).Succeeded(); } else { return Command::Post(STR_ERROR_CAN_T_BUILD_DOCK_HERE, CcBuildDocks, tile, to_join, adjacent); } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 291039204a..f9433225be 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -219,7 +219,7 @@ static void PlaceRail_Station(TileIndex tile) auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), tile, rt, params.axis, numtracks, platlength, params.sel_class, params.sel_type, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(tile, rt, params.axis, numtracks, platlength, params.sel_class, params.sel_type, StationID::Invalid(), adjacent).Succeeded(); } else { return Command::Post(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION, CcStation, tile, rt, params.axis, numtracks, platlength, params.sel_class, params.sel_type, to_join, adjacent); } @@ -777,7 +777,7 @@ struct BuildRailToolbarWindow : Window { auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), ta.tile, axis, ta.w, ta.h, _waypoint_gui.sel_class, _waypoint_gui.sel_type, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(ta.tile, axis, ta.w, ta.h, _waypoint_gui.sel_class, _waypoint_gui.sel_type, StationID::Invalid(), adjacent).Succeeded(); } else { return Command::Post(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT, CcPlaySound_CONSTRUCTION_RAIL, ta.tile, axis, ta.w, ta.h, _waypoint_gui.sel_class, _waypoint_gui.sel_type, to_join, adjacent); } @@ -944,7 +944,7 @@ static void HandleStationPlacement(TileIndex start, TileIndex end) auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), ta.tile, rt, params.axis, numtracks, platlength, params.sel_class, params.sel_type, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(ta.tile, rt, params.axis, numtracks, platlength, params.sel_class, params.sel_type, StationID::Invalid(), adjacent).Succeeded(); } else { return Command::Post(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION, CcStation, ta.tile, rt, params.axis, numtracks, platlength, params.sel_class, params.sel_type, to_join, adjacent); } diff --git a/src/road_gui.cpp b/src/road_gui.cpp index d53b21668d..e141cdd957 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -237,8 +237,8 @@ static void PlaceRoadStop(TileIndex start_tile, TileIndex end_tile, RoadStopType auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), ta.tile, ta.w, ta.h, stop_type, drive_through, - ddir, rt, spec_class, spec_index, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(ta.tile, ta.w, ta.h, stop_type, drive_through, + ddir, rt, spec_class, spec_index, StationID::Invalid(), adjacent); } else { return Command::Post(err_msg, CcRoadStop, ta.tile, ta.w, ta.h, stop_type, drive_through, ddir, rt, spec_class, spec_index, to_join, adjacent); @@ -765,7 +765,7 @@ struct BuildRoadToolbarWindow : Window { auto proc = [=](bool test, StationID to_join) -> bool { if (test) { - return Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), ta.tile, axis, ta.w, ta.h, _waypoint_gui.sel_class, _waypoint_gui.sel_type, StationID::Invalid(), adjacent).Succeeded(); + return Command::Query(ta.tile, axis, ta.w, ta.h, _waypoint_gui.sel_class, _waypoint_gui.sel_type, StationID::Invalid(), adjacent).Succeeded(); } else { return Command::Post(STR_ERROR_CAN_T_BUILD_ROAD_WAYPOINT, CcPlaySound_CONSTRUCTION_OTHER, ta.tile, axis, ta.w, ta.h, _waypoint_gui.sel_class, _waypoint_gui.sel_type, to_join, adjacent); } diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index e9e72f1aec..3f5f18ae2a 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -1378,7 +1378,7 @@ static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDi /* Can we actually build the bridge? */ RoadType rt = GetTownRoadType(); - if (Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), tile, bridge_tile, TRANSPORT_ROAD, bridge_type, rt).Succeeded()) { + if (Command::Query(tile, bridge_tile, TRANSPORT_ROAD, bridge_type, rt).Succeeded()) { Command::Do(CommandFlagsToDCFlags(GetCommandFlags()).Set(DoCommandFlag::Execute), tile, bridge_tile, TRANSPORT_ROAD, bridge_type, rt); return true; } @@ -1448,7 +1448,7 @@ static bool GrowTownWithTunnel(const Town *t, const TileIndex tile, const DiagDi /* Attempt to build the tunnel. Return false if it fails to let the town build a road instead. */ RoadType rt = GetTownRoadType(); - if (Command::Do(CommandFlagsToDCFlags(GetCommandFlags()), tile, TRANSPORT_ROAD, rt).Succeeded()) { + if (Command::Query(tile, TRANSPORT_ROAD, rt).Succeeded()) { Command::Do(CommandFlagsToDCFlags(GetCommandFlags()).Set(DoCommandFlag::Execute), tile, TRANSPORT_ROAD, rt); return true; }