changeset 5888:11683122ef51 draft

(svn r8501) -Fix (r7377) [FS#539]: Keep track of how much cargo has been paid for, so that cargo cannot be paid for more than once.
author maedhros <maedhros@openttd.org>
date Wed, 31 Jan 2007 22:33:24 +0000
parents 2f489a2c465a
children 30d820c68271
files src/economy.cpp src/openttd.cpp src/saveload.cpp src/vehicle.cpp src/vehicle.h
diffstat 5 files changed, 32 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1376,25 +1376,26 @@
 				st->time_since_unload = 0;
 
 				unloading_time += v->cargo_count; /* TTDBUG: bug in original TTD */
-				if (just_arrived && !HASBIT(v->load_status, LS_CARGO_PAID_FOR)) {
-					profit += DeliverGoods(v->cargo_count, v->cargo_type, v->cargo_source, last_visited, v->cargo_source_xy, v->cargo_days);
-					SETBIT(v->load_status, LS_CARGO_PAID_FOR);
+				if (just_arrived && v->cargo_paid_for < v->cargo_count) {
+					profit += DeliverGoods(v->cargo_count - v->cargo_paid_for, v->cargo_type, v->cargo_source, last_visited, v->cargo_source_xy, v->cargo_days);
+					v->cargo_paid_for = v->cargo_count;
 				}
 				result |= 1;
 				v->cargo_count -= amount_unloaded;
+				v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for);
 				if (_patches.gradual_loading) continue;
 			} else if (u->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
 				/* unload goods and let it wait at the station */
 				st->time_since_unload = 0;
-				if (just_arrived && (u->current_order.flags & OF_TRANSFER) && !HASBIT(v->load_status, LS_CARGO_PAID_FOR)) {
+				if (just_arrived && (u->current_order.flags & OF_TRANSFER) && v->cargo_paid_for < v->cargo_count) {
 					v_profit = GetTransportedGoodsIncome(
-						v->cargo_count,
+						v->cargo_count - v->cargo_paid_for,
 						DistanceManhattan(v->cargo_source_xy, GetStation(last_visited)->xy),
 						v->cargo_days,
 						v->cargo_type) * 3 / 2;
 
 					v_profit_total += v_profit;
-					SETBIT(v->load_status, LS_CARGO_PAID_FOR);
+					v->cargo_paid_for = v->cargo_count;
 				}
 
 				unloading_time += v->cargo_count;
@@ -1422,6 +1423,7 @@
 				}
 				result |= 2;
 				v->cargo_count -= amount_unloaded;
+				v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for);
 				if (_patches.gradual_loading) continue;
 			}
 
@@ -1431,7 +1433,9 @@
 		/* The vehicle must have been unloaded because it is either empty, or
 		 * the UNLOADING bit is already clear in v->load_status. */
 		CLRBIT(v->load_status, LS_CARGO_UNLOADING);
-		CLRBIT(v->load_status, LS_CARGO_PAID_FOR);
+
+		/* We cannot have paid for more cargo than there is on board. */
+		assert(v->cargo_paid_for <= v->cargo_count);
 
 		/* don't pick up goods that we unloaded */
 		if (u->current_order.flags & OF_UNLOAD) continue;
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -1748,6 +1748,23 @@
 		}
 	}
 
+	if (CheckSavegameVersion(45)) {
+		Vehicle *v;
+		/* Originally just the fact that some cargo had been paid for was
+		 * stored to stop people cheating and cashing in several times. This
+		 * wasn't enough though as it was cleared when the vehicle started
+		 * loading again, even if it didn't actually load anything, so now the
+		 * amount of cargo that has been paid for is stored. */
+		FOR_ALL_VEHICLES(v) {
+			if (HASBIT(v->load_status, 2)) {
+				v->cargo_paid_for = v->cargo_count;
+				CLRBIT(v->load_status, 2);
+			} else {
+				v->cargo_paid_for = 0;
+			}
+		}
+	}
+
 	return true;
 }
 
--- a/src/saveload.cpp
+++ b/src/saveload.cpp
@@ -30,7 +30,7 @@
 #include "variables.h"
 #include <setjmp.h>
 
-extern const uint16 SAVEGAME_VERSION = 44;
+extern const uint16 SAVEGAME_VERSION = 45;
 uint16 _sl_version;       /// the major savegame version identifier
 byte   _sl_minor_version; /// the minor savegame version, DO NOT USE!
 
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -3029,6 +3029,7 @@
 	SLE_CONDVAR(Vehicle, build_year,           SLE_INT32,                 31, SL_MAX_VERSION),
 
 	    SLE_VAR(Vehicle, load_unload_time_rem, SLE_UINT16),
+	SLE_CONDVAR(Vehicle, cargo_paid_for,       SLE_UINT16,                45, SL_MAX_VERSION),
 	SLE_CONDVAR(Vehicle, load_status,          SLE_UINT8,                 40, SL_MAX_VERSION),
 
 	    SLE_VAR(Vehicle, profit_this_year,     SLE_INT32),
--- a/src/vehicle.h
+++ b/src/vehicle.h
@@ -31,7 +31,7 @@
 enum LoadStatus {
 	LS_LOADING_FINISHED,
 	LS_CARGO_UNLOADING,
-	LS_CARGO_PAID_FOR,
+	/* LS_CARGO_PAID_FOR was here until savegame version 45. */
 };
 
 /* Effect vehicle types */
@@ -238,6 +238,7 @@
 	bool leave_depot_instantly; // NOSAVE: stores if the vehicle needs to leave the depot it just entered. Used by autoreplace
 
 	uint16 load_unload_time_rem;
+	uint16 cargo_paid_for;      // How much of the cargo currently on board has been paid for.
 	byte load_status;
 
 	int32 profit_this_year;