summaryrefslogtreecommitdiff
path: root/src/roadveh_cmd.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2009-10-28 21:09:37 +0000
committerfrosch <frosch@openttd.org>2009-10-28 21:09:37 +0000
commit00aca63b48e18e4bf3644964457c7d81c1a10573 (patch)
tree0588c61cbfcbb82e0a194ec742f629c2af9aef0b /src/roadveh_cmd.cpp
parentd01f5e9e7e78ceb22e36d5378f74b6ef4a842756 (diff)
downloadopenttd-00aca63b48e18e4bf3644964457c7d81c1a10573.tar.xz
(svn r17899) -Codechange: Deduplicate code for refitting vehicles.
Diffstat (limited to 'src/roadveh_cmd.cpp')
-rw-r--r--src/roadveh_cmd.cpp49
1 files changed, 6 insertions, 43 deletions
diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp
index bbdbb874b..f482c0ebb 100644
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -2003,11 +2003,9 @@ Trackdir RoadVehicle::GetVehicleTrackdir() const
*/
CommandCost CmdRefitRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
- CommandCost cost(EXPENSES_ROADVEH_RUN);
CargoID new_cid = GB(p2, 0, 8);
byte new_subtype = GB(p2, 8, 8);
bool only_this = HasBit(p2, 16);
- uint total_capacity = 0;
RoadVehicle *v = RoadVehicle::GetIfValid(p1);
@@ -2017,52 +2015,17 @@ CommandCost CmdRefitRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
if (new_cid >= NUM_CARGO) return CMD_ERROR;
- v->InvalidateNewGRFCacheOfChain();
- for (; v != NULL; v = (only_this ? NULL : v->Next())) {
- /* XXX: We refit all the attached wagons en-masse if they can be
- * refitted. This is how TTDPatch does it. TODO: Have some nice
- * [Refit] button near each wagon. */
- if (!CanRefitTo(v->engine_type, new_cid)) continue;
-
- const Engine *e = Engine::Get(v->engine_type);
- if (!e->CanCarryCargo()) continue;
-
- /* Back up the cargo type */
- CargoID temp_cid = v->cargo_type;
- byte temp_subtype = v->cargo_subtype;
- v->cargo_type = new_cid;
- v->cargo_subtype = new_subtype;
-
- uint capacity = GetVehicleCapacity(v);
-
- /* Restore the original cargo type */
- v->cargo_type = temp_cid;
- v->cargo_subtype = temp_subtype;
-
- total_capacity += capacity;
-
- if (new_cid != v->cargo_type) {
- cost.AddCost(GetRefitCost(v->engine_type));
- }
-
- if (flags & DC_EXEC) {
- v->cargo_cap = capacity;
- v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
- v->cargo_type = new_cid;
- v->cargo_subtype = new_subtype;
- SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
- SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
- InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
- }
- }
+ CommandCost cost = RefitVehicle(v, only_this, new_cid, new_subtype, flags);
if (flags & DC_EXEC) {
- RoadVehUpdateCache(RoadVehicle::Get(p1)->First());
+ RoadVehicle *front = v->First();
+ RoadVehUpdateCache(front);
+ SetWindowDirty(WC_VEHICLE_DETAILS, front->index);
+ SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
+ InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
} else {
v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
}
- _returned_refit_capacity = total_capacity;
-
return cost;
}