changeset 16729:235fa32934bf draft

(svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
author rubidium <rubidium@openttd.org>
date Sat, 11 Dec 2010 11:00:42 +0000
parents d2b7f8ee8e02
children 32fd4a8fa466
files src/economy.cpp
diffstat 1 files changed, 24 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1110,10 +1110,8 @@
 {
 	assert(v->current_order.IsType(OT_LOADING));
 
-	assert(v->load_unload_ticks != 0);
-
 	/* We have not waited enough time till the next round of loading/unloading */
-	if (--v->load_unload_ticks != 0) {
+	if (v->load_unload_ticks != 0) {
 		if (_settings_game.order.improved_load && (v->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
 			/* 'Reserve' this cargo for this vehicle, because we were first. */
 			for (; v != NULL; v = v->Next()) {
@@ -1423,14 +1421,36 @@
 	/* No vehicle is here... */
 	if (st->loading_vehicles.empty()) return;
 
+	Vehicle *last_loading = NULL;
+	std::list<Vehicle *>::iterator iter;
+
+	/* Check if anything will be loaded at all. Otherwise we don't need to reserve either. */
+	for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
+		Vehicle *v = *iter;
+
+		if ((v->vehstatus & (VS_STOPPED | VS_CRASHED))) continue;
+
+		assert(v->load_unload_ticks != 0);
+		if (--v->load_unload_ticks == 0) last_loading = v;
+	}
+
+	/* We only need to reserve and load/unload up to the last loading vehicle.
+	 * Anything else will be forgotten anyway after returning from this function.
+	 *
+	 * Especially this means we do _not_ need to reserve cargo for a single
+	 * consist in a station which is not allowed to load yet because its
+	 * load_unload_ticks is still not 0.
+	 */
+	if (last_loading == NULL) return;
+
 	int cargo_left[NUM_CARGO];
 
 	for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = st->goods[i].cargo.Count();
 
-	std::list<Vehicle *>::iterator iter;
 	for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
 		Vehicle *v = *iter;
 		if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
+		if (v == last_loading) break;
 	}
 
 	/* Call the production machinery of industries */