(svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
authorsmatz
Wed, 13 Feb 2008 19:24:40 +0000
changeset 9052 392f5995d323
parent 9051 0becfc1d0c09
child 9053 13c47e8836eb
(svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
-Fix [FS#1739]: vehicle profit is now counted with 8bit fract, so it is now shown properly in the vehicle details window
src/ai/default/default.cpp
src/ai/trolly/trolly.cpp
src/aircraft_cmd.cpp
src/economy.cpp
src/group_gui.cpp
src/newgrf_engine.cpp
src/openttd.cpp
src/roadveh_cmd.cpp
src/saveload.cpp
src/ship_cmd.cpp
src/train_cmd.cpp
src/vehicle.cpp
src/vehicle_base.h
src/vehicle_gui.cpp
--- a/src/ai/default/default.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/ai/default/default.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -112,8 +112,8 @@
 
 			/* not profitable? */
 			if (v->age >= 730 &&
-					v->profit_last_year < _price.station_value * 5 &&
-					v->profit_this_year < _price.station_value * 5) {
+					v->profit_last_year >> 8 < _price.station_value * 5 &&
+					v->profit_this_year >> 8 < _price.station_value * 5) {
 				_players_ai[p->index].state_counter = 0;
 				_players_ai[p->index].state = AIS_SELL_VEHICLE;
 				_players_ai[p->index].cur_veh = v;
--- a/src/ai/trolly/trolly.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/ai/trolly/trolly.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -1252,7 +1252,7 @@
 	if (v->age > 360) {
 		// If both years together are not more than AI_MINIMUM_ROUTE_PROFIT,
 		//  it is not worth the line I guess...
-		if (v->profit_last_year + v->profit_this_year < AI_MINIMUM_ROUTE_PROFIT ||
+		if ((v->profit_last_year + v->profit_this_year) >> 8 < AI_MINIMUM_ROUTE_PROFIT ||
 				(v->reliability * 100 >> 16) < 40) {
 			// There is a possibility that the route is fucked up...
 			if (v->cargo.DaysInTransit() > AI_VEHICLE_LOST_DAYS) {
--- a/src/aircraft_cmd.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/aircraft_cmd.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -321,6 +321,8 @@
 		u->z_pos = GetSlopeZ(x, y);
 		v->z_pos = u->z_pos + 1;
 
+		v->running_ticks = 0;
+
 //		u->delta_x = u->delta_y = 0;
 
 		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
@@ -735,11 +737,12 @@
 	AgeVehicle(this);
 	CheckIfAircraftNeedsService(this);
 
-	if (this->vehstatus & VS_STOPPED) return;
+	if (this->running_ticks == 0) return;
 
-	CommandCost cost = CommandCost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running / 364);
+	CommandCost cost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running * this->running_ticks / (364 * DAY_TICKS));
 
-	this->profit_this_year -= cost.GetCost() >> 8;
+	this->profit_this_year -= cost.GetCost();
+	this->running_ticks = 0;
 
 	SubtractMoneyFromPlayerFract(this->owner, cost);
 
@@ -2146,6 +2149,8 @@
 {
 	if (!IsNormalAircraft(this)) return;
 
+	if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
+
 	if (this->subtype == AIR_HELICOPTER) HelicopterTickHandler(this);
 
 	AgeAircraftCargo(this);
--- a/src/economy.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/economy.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -163,11 +163,9 @@
 				num++;
 				if (v->age > 730) {
 					/* Find the vehicle with the lowest amount of profit */
-					if (min_profit_first == true) {
-						min_profit = v->profit_last_year;
+					if (min_profit_first || min_profit > v->profit_last_year >> 8) {
+						min_profit = v->profit_last_year >> 8;
 						min_profit_first = false;
-					} else if (min_profit > v->profit_last_year) {
-						min_profit = v->profit_last_year;
 					}
 				}
 			}
@@ -1505,7 +1503,7 @@
 						cp->days_in_transit,
 						v->cargo_type);
 
-					front_v->profit_this_year += profit;
+					front_v->profit_this_year += profit << 8;
 					virtual_profit   += profit; // accumulate transfer profits for whole vehicle
 					cp->feeder_share += profit; // account for the (virtual) profit already made for the cargo packet
 					cp->paid_for      = true;   // record that the cargo has been paid for to eliminate double counting
@@ -1523,7 +1521,7 @@
 	}
 
 	if (route_profit != 0) {
-		front_v->profit_this_year += vehicle_profit;
+		front_v->profit_this_year += vehicle_profit << 8;
 		SubtractMoneyFromPlayer(CommandCost(front_v->GetExpenseType(true), -route_profit));
 
 		if (IsLocalPlayer() && !PlayVehicleSound(front_v, VSE_LOAD_UNLOAD)) {
--- a/src/group_gui.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/group_gui.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -483,8 +483,8 @@
 
 				if (w->resize.step_height == PLY_WND_PRC__SIZE_OF_ROW_BIG2) DrawSmallOrderList(v, x + 138, y2);
 
-				SetDParam(0, v->profit_this_year);
-				SetDParam(1, v->profit_last_year);
+				SetDParam(0, v->profit_this_year >> 8);
+				SetDParam(1, v->profit_last_year >> 8);
 				DrawString(x + 19, y2 + w->resize.step_height - 8, STR_0198_PROFIT_THIS_YEAR_LAST_YEAR, TC_FROMSTRING);
 
 				if (IsValidGroupID(v->group_id)) {
--- a/src/newgrf_engine.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/newgrf_engine.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -694,14 +694,14 @@
 		case 0x4F: return GB(v->reliability, 8, 8);
 		case 0x50: return v->reliability_spd_dec;
 		case 0x51: return GB(v->reliability_spd_dec, 8, 8);
-		case 0x52: return ClampToI32(v->profit_this_year);
-		case 0x53: return GB(ClampToI32(v->profit_this_year),  8, 24);
-		case 0x54: return GB(ClampToI32(v->profit_this_year), 16, 16);
-		case 0x55: return GB(ClampToI32(v->profit_this_year), 24,  8);
-		case 0x56: return ClampToI32(v->profit_last_year);
-		case 0x57: return GB(ClampToI32(v->profit_last_year),  8, 24);
-		case 0x58: return GB(ClampToI32(v->profit_last_year), 16, 16);
-		case 0x59: return GB(ClampToI32(v->profit_last_year), 24,  8);
+		case 0x52: return ClampToI32(v->profit_this_year >> 8);
+		case 0x53: return GB(ClampToI32(v->profit_this_year >> 8),  8, 24);
+		case 0x54: return GB(ClampToI32(v->profit_this_year >> 8), 16, 16);
+		case 0x55: return GB(ClampToI32(v->profit_this_year >> 8), 24,  8);
+		case 0x56: return ClampToI32(v->profit_last_year >> 8);
+		case 0x57: return GB(ClampToI32(v->profit_last_year >> 8),  8, 24);
+		case 0x58: return GB(ClampToI32(v->profit_last_year >> 8), 16, 16);
+		case 0x59: return GB(ClampToI32(v->profit_last_year >> 8), 24,  8);
 		case 0x5A: return v->Next() == NULL ? INVALID_VEHICLE : v->Next()->index;
 		case 0x5C: return ClampToI32(v->value);
 		case 0x5D: return GB(ClampToI32(v->value),  8, 24);
--- a/src/openttd.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/openttd.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -2419,6 +2419,16 @@
 		}
 	}
 
+	if (CheckSavegameVersion(88)) {
+		/* Profits are now with 8 bit fract */
+		Vehicle *v;
+		FOR_ALL_VEHICLES(v) {
+			v->profit_this_year <<= 8;
+			v->profit_last_year <<= 8;
+			v->running_ticks = 0;
+		}
+	}
+
 	return InitializeWindowsAndCaches();
 }
 
--- a/src/roadveh_cmd.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/roadveh_cmd.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -219,6 +219,8 @@
 		v->y_pos = y;
 		v->z_pos = GetSlopeZ(x, y);
 
+		v->running_ticks = 0;
+
 		v->u.road.state = RVSB_IN_DEPOT;
 		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 
@@ -1966,7 +1968,10 @@
 {
 	AgeRoadVehCargo(this);
 
-	if (IsRoadVehFront(this)) RoadVehController(this);
+	if (IsRoadVehFront(this)) {
+		if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
+		RoadVehController(this);
+	}
 }
 
 static void CheckIfRoadVehNeedsService(Vehicle *v)
@@ -2008,8 +2013,6 @@
 
 void RoadVehicle::OnNewDay()
 {
-	CommandCost cost(EXPENSES_ROADVEH_RUN);
-
 	if (!IsRoadVehFront(this)) return;
 
 	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
@@ -2027,10 +2030,8 @@
 		ClearSlot(this);
 	}
 
-	if (this->vehstatus & VS_STOPPED) return;
-
 	/* update destination */
-	if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
+	if (!(this->vehstatus & VS_STOPPED) && this->current_order.type == OT_GOTO_STATION && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
 		Station *st = GetStation(this->current_order.dest);
 		RoadStop *rs = st->GetPrimaryRoadStop(this);
 		RoadStop *best = NULL;
@@ -2088,9 +2089,12 @@
 		}
 	}
 
-	cost = CommandCost(EXPENSES_ROADVEH_RUN, RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running / 364);
+	if (this->running_ticks == 0) return;
 
-	this->profit_this_year -= cost.GetCost() >> 8;
+	CommandCost cost(EXPENSES_ROADVEH_RUN, RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running * this->running_ticks / (364 * DAY_TICKS));
+
+	this->profit_this_year -= cost.GetCost();
+	this->running_ticks = 0;
 
 	SubtractMoneyFromPlayerFract(this->owner, cost);
 
--- a/src/saveload.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/saveload.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -34,7 +34,7 @@
 
 #include "table/strings.h"
 
-extern const uint16 SAVEGAME_VERSION = 87;
+extern const uint16 SAVEGAME_VERSION = 88;
 uint16 _sl_version;       ///< the major savegame version identifier
 byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!
 
--- a/src/ship_cmd.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/ship_cmd.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -170,8 +170,6 @@
 
 void Ship::OnNewDay()
 {
-	CommandCost cost(EXPENSES_SHIP_RUN);
-
 	if ((++this->day_counter & 7) == 0)
 		DecreaseVehicleValue(this);
 
@@ -181,10 +179,12 @@
 
 	CheckOrders(this);
 
-	if (this->vehstatus & VS_STOPPED) return;
+	if (this->running_ticks == 0) return;
 
-	cost.AddCost(GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running / 364);
-	this->profit_this_year -= cost.GetCost() >> 8;
+	CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (364 * DAY_TICKS));
+
+	this->profit_this_year -= cost.GetCost();
+	this->running_ticks = 0;
 
 	SubtractMoneyFromPlayerFract(this->owner, cost);
 
@@ -781,6 +781,8 @@
 
 void Ship::Tick()
 {
+	if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
+
 	AgeShipCargo(this);
 	ShipController(this);
 }
@@ -843,6 +845,8 @@
 		v->y_pos = y;
 		v->z_pos = GetSlopeZ(x, y);
 
+		v->running_ticks = 0;
+
 		v->UpdateDeltaXY(v->direction);
 		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 
--- a/src/train_cmd.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/train_cmd.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -735,6 +735,7 @@
 			v->x_pos = x;
 			v->y_pos = y;
 			v->z_pos = GetSlopeZ(x, y);
+			v->running_ticks = 0;
 			v->u.rail.track = TRACK_BIT_DEPOT;
 			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 			v->spritenum = rvi->image_index;
@@ -3595,6 +3596,7 @@
 	this->tick_counter++;
 
 	if (IsFrontEngine(this)) {
+		if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
 		this->current_order_time++;
 
 		TrainLocoHandler(this, false);
@@ -3665,11 +3667,12 @@
 			if (tile != 0) this->dest_tile = tile;
 		}
 
-		if ((this->vehstatus & VS_STOPPED) == 0) {
+		if (this->running_ticks != 0) {
 			/* running costs */
-			CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() / 364);
-
-			this->profit_this_year -= cost.GetCost() >> 8;
+			CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (364 * DAY_TICKS));
+
+			this->profit_this_year -= cost.GetCost();
+			this->running_ticks = 0;
 
 			SubtractMoneyFromPlayerFract(this->owner, cost);
 
--- a/src/vehicle.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/vehicle.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -2797,6 +2797,7 @@
 
 	    SLE_VAR(Vehicle, day_counter,          SLE_UINT8),
 	    SLE_VAR(Vehicle, tick_counter,         SLE_UINT8),
+	SLE_CONDVAR(Vehicle, running_ticks,        SLE_UINT8,                   88, SL_MAX_VERSION),
 
 	    SLE_VAR(Vehicle, cur_order_index,      SLE_UINT8),
 	    SLE_VAR(Vehicle, num_orders,           SLE_UINT8),
--- a/src/vehicle_base.h	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/vehicle_base.h	Wed Feb 13 19:24:40 2008 +0000
@@ -249,8 +249,9 @@
 	CargoList cargo;         ///< The cargo this vehicle is carrying
 
 
-	byte day_counter;        // increased by one for each day
-	byte tick_counter;       // increased by one for each tick
+	byte day_counter;        ///< Increased by one for each day
+	byte tick_counter;       ///< Increased by one for each tick
+	byte running_ticks;      ///< Number of ticks this vehicle was not stopped this day
 
 	/* Begin Order-stuff */
 	Order current_order;     ///< The current order (+ status, like: loading)
@@ -291,8 +292,8 @@
 	uint16 load_unload_time_rem;
 	byte vehicle_flags;         // Used for gradual loading and other miscellaneous things (@see VehicleFlags enum)
 
-	Money profit_this_year;
-	Money profit_last_year;
+	Money profit_this_year;        ///< Profit this year << 8, low 8 bits are fract
+	Money profit_last_year;        ///< Profit last year << 8, low 8 bits are fract
 	Money value;
 
 	GroupID group_id;              ///< Index of group Pool array
--- a/src/vehicle_gui.cpp	Wed Feb 13 18:11:19 2008 +0000
+++ b/src/vehicle_gui.cpp	Wed Feb 13 19:24:40 2008 +0000
@@ -173,7 +173,7 @@
 		pal = PALETTE_TO_GREY;
 	} else if (v->profit_last_year < 0) {
 		pal = PALETTE_TO_RED;
-	} else if (v->profit_last_year < 10000) {
+	} else if (v->profit_last_year >> 8 < 10000) {
 		pal = PALETTE_TO_YELLOW;
 	} else {
 		pal = PALETTE_TO_GREEN;
@@ -598,7 +598,7 @@
 {
 	const Vehicle* va = *(const Vehicle**)a;
 	const Vehicle* vb = *(const Vehicle**)b;
-	int r = ClampToI32(va->profit_last_year - vb->profit_last_year);
+	int r = ClampToI32((va->profit_last_year - vb->profit_last_year) >> 8);
 
 	VEHICLEUNITNUMBERSORTER(r, va, vb);
 
@@ -983,8 +983,8 @@
 		const Vehicle *v = vl->sort_list[i];
 		StringID str;
 
-		SetDParam(0, v->profit_this_year);
-		SetDParam(1, v->profit_last_year);
+		SetDParam(0, v->profit_this_year >> 8);
+		SetDParam(1, v->profit_last_year >> 8);
 
 		DrawVehicleImage(v, x + 19, y + 6, INVALID_VEHICLE, w->widget[VLW_WIDGET_LIST].right - w->widget[VLW_WIDGET_LIST].left - 20, 0);
 		DrawString(x + 19, y + w->resize.step_height - 8, STR_0198_PROFIT_THIS_YEAR_LAST_YEAR, TC_FROMSTRING);
@@ -1500,8 +1500,8 @@
 	}
 
 	/* Draw profit */
-	SetDParam(0, v->profit_this_year);
-	SetDParam(1, v->profit_last_year);
+	SetDParam(0, v->profit_this_year >> 8);
+	SetDParam(1, v->profit_last_year >> 8);
 	DrawString(2, 35, _vehicle_translation_table[VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR][v->type], TC_FROMSTRING);
 
 	/* Draw breakdown & reliability */