(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
authorDarkvater
Wed, 11 May 2005 16:17:03 +0000
changeset 1790 4afb4b4e4278
parent 1789 a0708f7418ae
child 1791 0143d485d947
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
aircraft_cmd.c
aircraft_gui.c
depot.h
roadveh_cmd.c
roadveh_gui.c
ship_cmd.c
ship_gui.c
train_cmd.c
train_gui.c
vehicle.h
--- a/aircraft_cmd.c	Wed May 11 15:31:10 2005 +0000
+++ b/aircraft_cmd.c	Wed May 11 16:17:03 2005 +0000
@@ -5,6 +5,7 @@
 #include "map.h"
 #include "tile.h"
 #include "vehicle.h"
+#include "depot.h"
 #include "engine.h"
 #include "command.h"
 #include "station.h"
@@ -86,7 +87,7 @@
 }
 #endif
 
-int GetAircraftImage(Vehicle *v, byte direction)
+int GetAircraftImage(const Vehicle *v, byte direction)
 {
 	int spritenum = v->spritenum;
 
@@ -475,21 +476,24 @@
 	return 0;
 }
 
-// p1 = vehicle
-// p2 = new service int
+/** Change the service interval for aircraft.
+ * @param x,y unused
+ * @param p1 vehicle ID that is being service-interval-changed
+ * @param p2 new service interval
+ */
 int32 CmdChangeAircraftServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
+	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
 
-	if (!IsVehicleIndex(p1)) return CMD_ERROR;
+	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
 
 	v = GetVehicle(p1);
 
-	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner))
-		return CMD_ERROR;
+	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		v->service_interval = (uint16)p2;
+		v->service_interval = serv_int;
 		InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7);
 	}
 
--- a/aircraft_gui.c	Wed May 11 15:31:10 2005 +0000
+++ b/aircraft_gui.c	Wed May 11 16:17:03 2005 +0000
@@ -36,7 +36,7 @@
 
 }
 
-static void DrawAircraftImage(Vehicle *v, int x, int y, VehicleID selection)
+static void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
 	int image = GetAircraftImage(v, 6);
 	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
@@ -378,11 +378,10 @@
 
 static void AircraftDetailsWndProc(Window *w, WindowEvent *e)
 {
-	int mod;
-	Vehicle *v = GetVehicle(w->window_number), *u;
+	switch (e->event) {
+	case WE_PAINT: {
+		const Vehicle *v = GetVehicle(w->window_number);
 
-	switch(e->event) {
-	case WE_PAINT:
 		w->disabled_state = v->owner == _local_player ? 0 : (1 << 2);
 		if (!_patches.servint_aircraft) // disable service-scroller when interval is set to disabled
 			w->disabled_state |= (1 << 5) | (1 << 6);
@@ -440,6 +439,7 @@
 		DrawAircraftImage(v, 3, 57, INVALID_VEHICLE);
 
 		{
+			const Vehicle *u;
 			int y = 57;
 
 			do {
@@ -471,34 +471,32 @@
 				}
 			} while ( (v=v->next) != NULL);
 		}
-		break;
+	} break;
 
-	case WE_CLICK:
-		switch(e->click.widget) {
+	case WE_CLICK: {
+		int mod;
+		const Vehicle *v;
+		switch (e->click.widget) {
 		case 2: /* rename */
+			v = GetVehicle(w->window_number);
 			SetDParam(0, v->unitnumber);
 			ShowQueryString(v->string_id, STR_A030_NAME_AIRCRAFT, 31, 150, w->window_class, w->window_number);
 			break;
-
 		case 5: /* increase int */
 			mod = _ctrl_pressed? 5 : 10;
-			goto change_int;
+			goto do_change_service_int;
 		case 6: /* decrease int */
 			mod = _ctrl_pressed?- 5 : -10;
-change_int:
-			mod += v->service_interval;
+do_change_service_int:
+			v = GetVehicle(w->window_number);
 
-			/*	%-based service interval max 5%-90%
-					day-based service interval max 30-800 days */
-			mod = _patches.servint_ispercent ? clamp(mod, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(mod, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS+1);
-			if (mod == v->service_interval)
-				return;
+			mod = GetServiceIntervalClamped(mod + v->service_interval);
+			if (mod == v->service_interval) return;
 
-			DoCommandP(v->tile, v->index, mod, NULL,
-				CMD_CHANGE_AIRCRAFT_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
+			DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_AIRCRAFT_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
 			break;
 		}
-		break;
+	} break;
 
 	case WE_4:
 		if (FindWindowById(WC_VEHICLE_VIEW, w->window_number) == NULL)
--- a/depot.h	Wed May 11 15:31:10 2005 +0000
+++ b/depot.h	Wed May 11 16:17:03 2005 +0000
@@ -41,6 +41,16 @@
 #define MIN_SERVINT_DAYS    30
 #define MAX_SERVINT_DAYS   800
 
+/** Get the service interval domain.
+ * Get the new proposed service interval for the vehicle is indeed, clamped
+ * within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
+ * @param index proposed service interval
+ */
+static inline uint16 GetServiceIntervalClamped(uint index)
+{
+	return (_patches.servint_ispercent) ? clamp(index, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(index, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
+}
+
 VARDEF TileIndex _last_built_train_depot_tile;
 VARDEF TileIndex _last_built_road_depot_tile;
 VARDEF TileIndex _last_built_aircraft_depot_tile;
--- a/roadveh_cmd.c	Wed May 11 15:31:10 2005 +0000
+++ b/roadveh_cmd.c	Wed May 11 16:17:03 2005 +0000
@@ -58,7 +58,7 @@
 	0x910, 0x1600, 0x2005, 0x2A
 };
 
-int GetRoadVehImage(Vehicle *v, byte direction)
+int GetRoadVehImage(const Vehicle *v, byte direction)
 {
 	int img = v->spritenum;
 	int image;
@@ -405,19 +405,24 @@
 	return 0;
 }
 
+/** Change the service interval for road vehicles.
+ * @param x,y unused
+ * @param p1 vehicle ID that is being service-interval-changed
+ * @param p2 new service interval
+ */
 int32 CmdChangeRoadVehServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
+	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
 
-	if (!IsVehicleIndex(p1)) return CMD_ERROR;
+	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
 
 	v = GetVehicle(p1);
 
-	if (v->type != VEH_Road || !CheckOwnership(v->owner))
-		return CMD_ERROR;
+	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		v->service_interval = (uint16)p2;
+		v->service_interval = serv_int;
 		InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7);
 	}
 
--- a/roadveh_gui.c	Wed May 11 15:31:10 2005 +0000
+++ b/roadveh_gui.c	Wed May 11 16:17:03 2005 +0000
@@ -33,7 +33,7 @@
 	SetDParam(5, ymd.year + 1920);
 }
 
-static void DrawRoadVehImage(Vehicle *v, int x, int y, VehicleID selection)
+static void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
 	int image = GetRoadVehImage(v, 6);
 	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
@@ -47,12 +47,11 @@
 
 static void RoadVehDetailsWndProc(Window *w, WindowEvent *e)
 {
-	Vehicle *v = GetVehicle(w->window_number);
-	StringID str;
-	int mod;
+	switch (e->event) {
+	case WE_PAINT: {
+		const Vehicle *v = GetVehicle(w->window_number);
+		StringID str;
 
-	switch(e->event) {
-	case WE_PAINT:
 		w->disabled_state = v->owner == _local_player ? 0 : (1 << 2);
 		if (!_patches.servint_roadveh) // disable service-scroller when interval is set to disabled
 			w->disabled_state |= (1 << 5) | (1 << 6);
@@ -126,33 +125,33 @@
 			str = STR_8813_FROM;
 		}
 		DrawString(34, 78, str, 0);
-		break;
+	} break;
 
-	case WE_CLICK:
-		switch(e->click.widget) {
+	case WE_CLICK: {
+		int mod;
+		const Vehicle *v;
+		switch (e->click.widget) {
 		case 2: /* rename */
+			v = GetVehicle(w->window_number);
 			SetDParam(0, v->unitnumber);
 			ShowQueryString(v->string_id, STR_902C_NAME_ROAD_VEHICLE, 31, 150, w->window_class, w->window_number);
 			break;
 
 		case 5: /* increase int */
 			mod = _ctrl_pressed? 5 : 10;
-			goto change_int;
+			goto do_change_service_int;
 		case 6: /* decrease int */
 			mod = _ctrl_pressed? -5 : -10;
-change_int:
-			mod += v->service_interval;
+do_change_service_int:
+			v = GetVehicle(w->window_number);
 
-			/*	%-based service interval max 5%-90%
-					day-based service interval max 30-800 days */
-			mod = _patches.servint_ispercent ? clamp(mod, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(mod, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS+1);
-			if (mod == v->service_interval)
-				return;
+			mod = GetServiceIntervalClamped(mod + v->service_interval);
+			if (mod == v->service_interval) return;
 
 			DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_ROADVEH_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
 			break;
 		}
-		break;
+	} break;
 
 	case WE_4:
 		if (FindWindowById(WC_VEHICLE_VIEW, w->window_number) == NULL)
--- a/ship_cmd.c	Wed May 11 15:31:10 2005 +0000
+++ b/ship_cmd.c	Wed May 11 16:17:03 2005 +0000
@@ -51,7 +51,7 @@
 	DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw);
 }
 
-int GetShipImage(Vehicle *v, byte direction)
+int GetShipImage(const Vehicle *v, byte direction)
 {
 	int spritenum = v->spritenum;
 
@@ -1032,19 +1032,24 @@
 	return 0;
 }
 
+/** Change the service interval for ships.
+ * @param x,y unused
+ * @param p1 vehicle ID that is being service-interval-changed
+ * @param p2 new service interval
+ */
 int32 CmdChangeShipServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
+	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
 
-	if (!IsVehicleIndex(p1)) return CMD_ERROR;
+	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
 
 	v = GetVehicle(p1);
 
-	if (v->type != VEH_Ship || !CheckOwnership(v->owner))
-		return CMD_ERROR;
+	if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		v->service_interval = (uint16)p2;
+		v->service_interval = serv_int;
 		InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7);
 	}
 
--- a/ship_gui.c	Wed May 11 15:31:10 2005 +0000
+++ b/ship_gui.c	Wed May 11 16:17:03 2005 +0000
@@ -34,8 +34,16 @@
 	SetDParam(6, ymd.year + 1920);
 }
 
-static void DrawShipImage(Vehicle *v, int x, int y, VehicleID selection);
+static void DrawShipImage(const Vehicle *v, int x, int y, VehicleID selection)
+{
+	int image = GetShipImage(v, 6);
+	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
+	DrawSprite(image | ormod, x+32, y+10);
 
+	if (v->index == selection) {
+		DrawFrameRect(x-5, y-1, x+67, y+21, 15, 0x10);
+	}
+}
 
 const byte _ship_refit_types[4][16] = {
 	{CT_MAIL, CT_COAL, CT_LIVESTOCK, CT_GOODS, CT_GRAIN, CT_WOOD, CT_IRON_ORE, CT_STEEL, CT_VALUABLES, 255},
@@ -168,12 +176,11 @@
 
 static void ShipDetailsWndProc(Window *w, WindowEvent *e)
 {
-	Vehicle *v = GetVehicle(w->window_number);
-	StringID str;
-	int mod;
+	switch (e->event) {
+	case WE_PAINT: {
+		const Vehicle *v = GetVehicle(w->window_number);
+		StringID str;
 
-	switch(e->event) {
-	case WE_PAINT:
 		w->disabled_state = v->owner == _local_player ? 0 : (1 << 2);
 		if (!_patches.servint_ships) // disable service-scroller when interval is set to disabled
 			w->disabled_state |= (1 << 5) | (1 << 6);
@@ -247,32 +254,32 @@
 			str = STR_8813_FROM;
 		}
 		DrawString(74, 78, str, 0);
-		break;
+	} break;
 
-	case WE_CLICK:
-		switch(e->click.widget) {
+	case WE_CLICK: {
+		int mod;
+		const Vehicle *v;
+		switch (e->click.widget) {
 		case 2: /* rename */
+			v = GetVehicle(w->window_number);
 			SetDParam(0, v->unitnumber);
 			ShowQueryString(v->string_id, STR_9831_NAME_SHIP, 31, 150, w->window_class, w->window_number);
 			break;
 		case 5: /* increase int */
 			mod = _ctrl_pressed? 5 : 10;
-			goto change_int;
+			goto do_change_service_int;
 		case 6: /* decrease int */
 			mod = _ctrl_pressed?- 5 : -10;
-change_int:
-			mod += v->service_interval;
+do_change_service_int:
+			v = GetVehicle(w->window_number);
 
-			/*	%-based service interval max 5%-90%
-					day-based service interval max 30-800 days */
-			mod = _patches.servint_ispercent ? clamp(mod, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(mod, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS+1);
-			if (mod == v->service_interval)
-				return;
+			mod = GetServiceIntervalClamped(mod + v->service_interval);
+			if (mod == v->service_interval) return;
 
 			DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_SHIP_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
 			break;
 		}
-		break;
+	} break;
 
 	case WE_4:
 		if (FindWindowById(WC_VEHICLE_VIEW, w->window_number) == NULL)
@@ -622,18 +629,6 @@
 	}
 }
 
-
-static void DrawShipImage(Vehicle *v, int x, int y, VehicleID selection)
-{
-	int image = GetShipImage(v, 6);
-	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
-	DrawSprite(image | ormod, x+32, y+10);
-
-	if (v->index == selection) {
-		DrawFrameRect(x-5, y-1, x+67, y+21, 15, 0x10);
-	}
-}
-
 static void DrawShipDepotWindow(Window *w)
 {
 	uint tile;
--- a/train_cmd.c	Wed May 11 15:31:10 2005 +0000
+++ b/train_cmd.c	Wed May 11 16:17:03 2005 +0000
@@ -1481,20 +1481,21 @@
 /** Change the service interval for trains.
  * @param x,y unused
  * @param p1 vehicle ID that is being service-interval-changed
- * @param p2 new service interval (0 - 65536)
+ * @param p2 new service interval
  */
 int32 CmdChangeTrainServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Vehicle *v;
-
-	if (!IsVehicleIndex(p1)) return CMD_ERROR;
+	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
+
+	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
 
 	v = GetVehicle(p1);
 
 	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		v->service_interval = (uint16)p2;
+		v->service_interval = serv_int;
 		InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 8);
 	}
 
@@ -3134,7 +3135,7 @@
 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
 }
 
-int32 GetTrainRunningCost(Vehicle *v)
+int32 GetTrainRunningCost(const Vehicle *v)
 {
 	int32 cost = 0;
 
--- a/train_gui.c	Wed May 11 15:31:10 2005 +0000
+++ b/train_gui.c	Wed May 11 16:17:03 2005 +0000
@@ -280,7 +280,7 @@
 	}
 }
 
-static void DrawTrainImage(Vehicle *v, int x, int y, int count, int skip, VehicleID selection)
+static void DrawTrainImage(const Vehicle *v, int x, int y, int count, int skip, VehicleID selection)
 {
 	do {
 		if (--skip < 0) {
@@ -980,7 +980,7 @@
 	}
 }
 
-static void TrainDetailsCargoTab(Vehicle *v, int x, int y)
+static void TrainDetailsCargoTab(const Vehicle *v, int x, int y)
 {
 	int num;
 	StringID str;
@@ -998,7 +998,7 @@
 	}
 }
 
-static void TrainDetailsInfoTab(Vehicle *v, int x, int y)
+static void TrainDetailsInfoTab(const Vehicle *v, int x, int y)
 {
 	const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
 
@@ -1014,7 +1014,7 @@
 	}
 }
 
-static void TrainDetailsCapacityTab(Vehicle *v, int x, int y)
+static void TrainDetailsCapacityTab(const Vehicle *v, int x, int y)
 {
 	if (v->cargo_cap != 0) {
 		SetDParam(1, v->cargo_cap);
@@ -1023,7 +1023,7 @@
 	}
 }
 
-typedef void TrainDetailsDrawerProc(Vehicle *v, int x, int y);
+typedef void TrainDetailsDrawerProc(const Vehicle *v, int x, int y);
 
 static TrainDetailsDrawerProc * const _train_details_drawer_proc[3] = {
 	TrainDetailsCargoTab,
@@ -1033,7 +1033,7 @@
 
 static void DrawTrainDetailsWindow(Window *w)
 {
-	Vehicle *v, *u;
+	const Vehicle *v, *u;
 	uint16 tot_cargo[NUM_CARGO][2];	// count total cargo ([0]-actual cargo, [1]-total cargo)
 	int max_speed = 0xFFFF;
 	int i,num,x,y,sel;
@@ -1151,14 +1151,14 @@
 
 static void TrainDetailsWndProc(Window *w, WindowEvent *e)
 {
-	switch(e->event) {
+	switch (e->event) {
 	case WE_PAINT:
 		DrawTrainDetailsWindow(w);
 		break;
 	case WE_CLICK: {
 		int mod;
-		Vehicle *v;
-		switch(e->click.widget) {
+		const Vehicle *v;
+		switch (e->click.widget) {
 		case 2: /* name train */
 			v = GetVehicle(w->window_number);
 			SetDParam(0, v->unitnumber);
@@ -1172,13 +1172,9 @@
 			mod = _ctrl_pressed? -5 : -10;
 do_change_service_int:
 			v = GetVehicle(w->window_number);
-			mod += v->service_interval;
 
-				/*	%-based service interval max 5%-90%
-						day-based service interval max 30-800 days */
-				mod = _patches.servint_ispercent ? clamp(mod, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(mod, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS+1);
-				if (mod == v->service_interval)
-					return;
+			mod = GetServiceIntervalClamped(mod + v->service_interval);
+			if (mod == v->service_interval) return;
 
 			DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_TRAIN_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
 			break;
--- a/vehicle.h	Wed May 11 15:31:10 2005 +0000
+++ b/vehicle.h	Wed May 11 16:17:03 2005 +0000
@@ -271,9 +271,9 @@
 
 /* train_cmd.h */
 int GetTrainImage(const Vehicle *v, byte direction);
-int GetAircraftImage(Vehicle *v, byte direction);
-int GetRoadVehImage(Vehicle *v, byte direction);
-int GetShipImage(Vehicle *v, byte direction);
+int GetAircraftImage(const Vehicle *v, byte direction);
+int GetRoadVehImage(const Vehicle *v, byte direction);
+int GetShipImage(const Vehicle *v, byte direction);
 
 Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type);
 Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type);
@@ -307,7 +307,7 @@
 int LoadUnloadVehicle(Vehicle *v);
 
 void UpdateTrainAcceleration(Vehicle *v);
-int32 GetTrainRunningCost(Vehicle *v);
+int32 GetTrainRunningCost(const Vehicle *v);
 
 int CheckTrainStoppedInDepot(const Vehicle *v);