changeset 20074:37a1f3a45816 draft

(svn r25008) -Codechange: Make CargoList::Truncate behave similarly to CargoList::MoveTo, i.e. pass the amount to truncate (fonsinchen)
author rubidium <rubidium@openttd.org>
date Sun, 17 Feb 2013 14:10:15 +0000
parents 3c8cf1b2e76d
children 52b689a6e5f0
files src/aircraft_cmd.cpp src/autoreplace_cmd.cpp src/cargopacket.cpp src/cargopacket.h src/station.cpp src/station_cmd.cpp src/vehicle.cpp src/vehicle_cmd.cpp
diffstat 8 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -1160,8 +1160,8 @@
 	uint pass = v->Crash();
 	SetDParam(0, pass);
 
-	v->cargo.Truncate(0);
-	v->Next()->cargo.Truncate(0);
+	v->cargo.Truncate();
+	v->Next()->cargo.Truncate();
 	const Station *st = GetTargetAirportIfValid(v);
 	StringID newsitem;
 	if (st == NULL) {
@@ -1205,7 +1205,7 @@
 	/* Crash the airplane. Remove all goods stored at the station. */
 	for (CargoID i = 0; i < NUM_CARGO; i++) {
 		st->goods[i].rating = 1;
-		st->goods[i].cargo.Truncate(0);
+		st->goods[i].cargo.Truncate();
 	}
 
 	CrashAirplane(v);
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -111,7 +111,7 @@
 		}
 
 		/* Any left-overs will be thrown away, but not their feeder share. */
-		src->cargo.Truncate(src->cargo_cap);
+		if (src->cargo_cap < src->cargo.Count()) src->cargo.Truncate(src->cargo.Count() - src->cargo_cap);
 	}
 }
 
--- a/src/cargopacket.cpp
+++ b/src/cargopacket.cpp
@@ -209,11 +209,14 @@
 /**
  * Truncates the cargo in this list to the given amount. It leaves the
  * first count cargo entities and removes the rest.
- * @param max_remaining Maximum amount of entities to be in the list after the command.
+ * @param max_move Maximum amount of entities to be removed from the list.
+ * @return Amount of entities actually moved.
  */
 template <class Tinst>
-void CargoList<Tinst>::Truncate(uint max_remaining)
+uint CargoList<Tinst>::Truncate(uint max_move)
 {
+	max_move = min(this->count, max_move);
+	uint max_remaining = this->count - max_move;
 	for (Iterator it(packets.begin()); it != packets.end(); /* done during loop*/) {
 		CargoPacket *cp = *it;
 		if (max_remaining == 0) {
@@ -236,6 +239,7 @@
 		}
 		++it;
 	}
+	return max_move;
 }
 
 /**
--- a/src/cargopacket.h
+++ b/src/cargopacket.h
@@ -246,7 +246,7 @@
 
 
 	void Append(CargoPacket *cp);
-	void Truncate(uint max_remaining);
+	uint Truncate(uint max_move = UINT_MAX);
 
 	template <class Tother_inst>
 	bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0);
--- a/src/station.cpp
+++ b/src/station.cpp
@@ -114,7 +114,7 @@
 	DeleteStationNews(this->index);
 
 	for (CargoID c = 0; c < NUM_CARGO; c++) {
-		this->goods[c].cargo.Truncate(0);
+		this->goods[c].cargo.Truncate();
 	}
 
 	CargoPacket::InvalidateAllFrom(this->index);
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -3307,7 +3307,7 @@
 					waiting_changed = true;
 				}
 
-				if (waiting_changed) ge->cargo.Truncate(waiting);
+				if (waiting_changed) ge->cargo.Truncate(ge->cargo.Count() - waiting);
 			}
 		}
 	}
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -769,7 +769,7 @@
 	}
 	InvalidateWindowClassesData(GetWindowClassForVehicleType(this->type), 0);
 
-	this->cargo.Truncate(0);
+	this->cargo.Truncate();
 	DeleteVehicleOrders(this);
 	DeleteDepotHighlightOfVehicle(this);
 
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -386,14 +386,20 @@
 		/* Store the result */
 		for (RefitResult *result = refit_result.Begin(); result != refit_result.End(); result++) {
 			Vehicle *u = result->v;
-			u->cargo.Truncate((u->cargo_type == new_cid) ? result->capacity : 0);
+			if (u->cargo_type != new_cid) {
+				u->cargo.Truncate(u->cargo_cap);
+			} else if (u->cargo_cap > result->capacity) {
+				u->cargo.Truncate(u->cargo_cap - result->capacity);
+			}
 			u->cargo_type = new_cid;
 			u->cargo_cap = result->capacity;
 			u->cargo_subtype = new_subtype;
 			if (u->type == VEH_AIRCRAFT) {
 				Vehicle *w = u->Next();
+				if (w->cargo_cap > result->mail_capacity) {
+					w->cargo.Truncate(w->cargo_cap - result->mail_capacity);
+				}
 				w->cargo_cap = result->mail_capacity;
-				w->cargo.Truncate(result->mail_capacity);
 			}
 		}
 	}