(svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
authorrubidium
Wed, 02 May 2007 18:29:11 +0000
changeset 7061 81d3a68972b5
parent 7060 aedcbd510a9a
child 7062 383e83c8d519
(svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
src/aircraft_cmd.cpp
src/economy.cpp
src/economy.h
src/roadveh_cmd.cpp
src/ship_cmd.cpp
src/train_cmd.cpp
src/vehicle.cpp
src/vehicle.h
--- a/src/aircraft_cmd.cpp	Wed May 02 15:20:48 2007 +0000
+++ b/src/aircraft_cmd.cpp	Wed May 02 18:29:11 2007 +0000
@@ -1421,15 +1421,8 @@
 			if (mode != 0) return;
 			if (--v->load_unload_time_rem != 0) return;
 
-			if (CanFillVehicle(v) && (
-						v->current_order.flags & OF_FULL_LOAD ||
-						(_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED))
-					)) {
-				SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
-				if (LoadUnloadVehicle(v, false)) {
-					InvalidateWindow(WC_AIRCRAFT_LIST, v->owner);
-					v->MarkDirty();
-				}
+			if (CanFillVehicle(v)) {
+				LoadUnloadVehicle(v);
 				return;
 			}
 
--- a/src/economy.cpp	Wed May 02 15:20:48 2007 +0000
+++ b/src/economy.cpp	Wed May 02 18:29:11 2007 +0000
@@ -1381,11 +1381,8 @@
 /**
  * Performs the vehicle payment _and_ marks the vehicle to be unloaded.
  * @param front_v the vehicle to be unloaded
- * @return what windows need to be updated;
- *         bit 0 set: only vehicle details,
- *         bit 1 set: vehicle details and station details
  */
-static int VehiclePayment(Vehicle *front_v)
+void VehiclePayment(Vehicle *front_v)
 {
 	int result = 0;
 
@@ -1401,10 +1398,22 @@
 	StationID last_visited = front_v->last_station_visited;
 	Station *st = GetStation(last_visited);
 
+	/* The owner of the train wants to be paid */
+	PlayerID old_player = _current_player;
+	_current_player = front_v->owner;
+
+	/* At this moment loading cannot be finished */
+	CLRBIT(front_v->vehicle_flags, VF_LOADING_FINISHED);
+
+	/* Start unloading in at the first possible moment */
+	front_v->load_unload_time_rem = 1;
+
 	for (Vehicle *v = front_v; v != NULL; v = v->next) {
+		/* No cargo to unload */
 		if (v->cargo_cap == 0) continue;
 
 		SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
+		/* All cargo has already been paid for, no need to pay again */
 		if (v->cargo_count == v->cargo_paid_for) continue;
 
 		GoodsEntry *ge = &st->goods[v->cargo_type];
@@ -1465,17 +1474,17 @@
 		ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -total_veh_profit);
 	}
 
-	return result;
+	_current_player = old_player;
 }
 
-int LoadUnloadVehicle(Vehicle *v, bool just_arrived)
+int LoadUnloadVehicle(Vehicle *v)
 {
 	int unloading_time = 20;
 	Vehicle *u = v;
 	int result = 0;
 	int t;
 	uint count, cap;
-	PlayerID old_player;
+
 	bool completely_empty = true;
 	byte load_amount;
 	bool anything_loaded = false;
@@ -1491,14 +1500,9 @@
 	 * enabling though. */
 	SETBIT(v->vehicle_flags, VF_LOADING_FINISHED);
 
-	old_player = _current_player;
-	_current_player = v->owner;
-
 	StationID last_visited = v->last_station_visited;
 	Station *st = GetStation(last_visited);
 
-	if (just_arrived) result |= VehiclePayment(v);
-
 	for (; v != NULL; v = v->next) {
 		GoodsEntry* ge;
 		load_amount = EngInfo(v->engine_type)->load_amount;
@@ -1596,7 +1600,10 @@
 
 			/* Skip loading this vehicle if another train/vehicle is already handling
 			 * the same cargo type at this station */
-			if (_patches.improved_load && (u->current_order.flags & OF_FULL_LOAD) && LoadWait(v,u)) continue;
+			if (_patches.improved_load && (u->current_order.flags & OF_FULL_LOAD) && LoadWait(v,u)) {
+				CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED);
+				continue;
+			}
 
 			/* TODO: Regarding this, when we do gradual loading, we
 			 * should first unload all vehicles and then start
@@ -1673,13 +1680,15 @@
 	}
 
 	if (result != 0) {
+		InvalidateWindow(v->GetVehicleListWindowClass(), v->owner);
 		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
+
 		st->MarkTilesDirty();
+		v->MarkDirty();
 
 		if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited);
 	}
 
-	_current_player = old_player;
 	return result;
 }
 
--- a/src/economy.h	Wed May 02 15:20:48 2007 +0000
+++ b/src/economy.h	Wed May 02 18:29:11 2007 +0000
@@ -68,4 +68,6 @@
 int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type);
 uint MoveGoodsToStation(TileIndex tile, int w, int h, CargoID type, uint amount);
 
+void VehiclePayment(Vehicle *front_v);
+
 #endif /* ECONOMY_H */
--- a/src/roadveh_cmd.cpp	Wed May 02 15:20:48 2007 +0000
+++ b/src/roadveh_cmd.cpp	Wed May 02 18:29:11 2007 +0000
@@ -761,13 +761,8 @@
 
 			if (--v->load_unload_time_rem != 0) return;
 
-			if (CanFillVehicle(v) && (v->current_order.flags & OF_FULL_LOAD ||
-					(_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)))) {
-				SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC);
-				if (LoadUnloadVehicle(v, false)) {
-					InvalidateWindow(WC_ROADVEH_LIST, v->owner);
-					v->MarkDirty();
-				}
+			if (CanFillVehicle(v)) {
+				LoadUnloadVehicle(v);
 				return;
 			}
 
--- a/src/ship_cmd.cpp	Wed May 02 15:20:48 2007 +0000
+++ b/src/ship_cmd.cpp	Wed May 02 18:29:11 2007 +0000
@@ -306,15 +306,8 @@
 		case OT_LOADING: {
 			if (--v->load_unload_time_rem != 0) return;
 
-			if (CanFillVehicle(v) && (
-						v->current_order.flags & OF_FULL_LOAD ||
-						(_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED))
-					)) {
-				SET_EXPENSES_TYPE(EXPENSES_SHIP_INC);
-				if (LoadUnloadVehicle(v, false)) {
-					InvalidateWindow(WC_SHIPS_LIST, v->owner);
-					v->MarkDirty();
-				}
+			if (CanFillVehicle(v)) {
+				LoadUnloadVehicle(v);
 				return;
 			}
 			PlayShipSound(v);
--- a/src/train_cmd.cpp	Wed May 02 15:20:48 2007 +0000
+++ b/src/train_cmd.cpp	Wed May 02 18:29:11 2007 +0000
@@ -2544,16 +2544,8 @@
 
 			if (--v->load_unload_time_rem) return;
 
-			if (CanFillVehicle(v) && (
-						v->current_order.flags & OF_FULL_LOAD ||
-						(_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED))
-					)) {
-				v->u.rail.days_since_order_progr = 0; // Prevent a train lost message for full loading trains
-				SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
-				if (LoadUnloadVehicle(v, false)) {
-					InvalidateWindow(WC_TRAINS_LIST, v->owner);
-					v->MarkDirty();
-				}
+			if (CanFillVehicle(v)) {
+				LoadUnloadVehicle(v);
 				return;
 			}
 
--- a/src/vehicle.cpp	Wed May 02 15:20:48 2007 +0000
+++ b/src/vehicle.cpp	Wed May 02 18:29:11 2007 +0000
@@ -40,6 +40,7 @@
 #include "newgrf_engine.h"
 #include "newgrf_sound.h"
 #include "helpers.hpp"
+#include "economy.h"
 
 #define INVALID_COORD (-0x8000)
 #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
@@ -741,6 +742,7 @@
 	return keep_loading || (not_full && (full & ~not_full) == 0);
 }
 
+
 bool CanFillVehicle(Vehicle *v)
 {
 	TileIndex tile = v->tile;
@@ -2971,11 +2973,15 @@
 	GetStation(this->last_station_visited)->loading_vehicles.push_back(this);
 
 	SET_EXPENSES_TYPE(this->GetExpenseType(true));
-	if (LoadUnloadVehicle(this, true) != 0) {
-		InvalidateWindow(this->GetVehicleListWindowClass(), this->owner);
-		this->MarkDirty();
-	}
+	VehiclePayment(this);
+
+	InvalidateWindow(this->GetVehicleListWindowClass(), this->owner);
 	InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, STATUS_BAR);
+	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
+	InvalidateWindow(WC_STATION_VIEW, this->last_station_visited);
+
+	GetStation(this->last_station_visited)->MarkTilesDirty();
+	this->MarkDirty();
 }
 
 void Vehicle::LeaveStation()
--- a/src/vehicle.h	Wed May 02 15:20:48 2007 +0000
+++ b/src/vehicle.h	Wed May 02 18:29:11 2007 +0000
@@ -508,7 +508,7 @@
 
 UnitID GetFreeUnitNumber(byte type);
 
-int LoadUnloadVehicle(Vehicle *v, bool just_arrived);
+int LoadUnloadVehicle(Vehicle *v);
 
 void TrainConsistChanged(Vehicle *v);
 void TrainPowerChanged(Vehicle *v);