From 729066e407731ca323a9b368b108c100c37044e3 Mon Sep 17 00:00:00 2001 From: Darkvater Date: Wed, 11 May 2005 16:17:03 +0000 Subject: [PATCH] (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 | 18 ++++++++++------- aircraft_gui.c | 38 +++++++++++++++++----------------- depot.h | 10 +++++++++ roadveh_cmd.c | 15 +++++++++----- roadveh_gui.c | 35 ++++++++++++++++---------------- ship_cmd.c | 15 +++++++++----- ship_gui.c | 55 +++++++++++++++++++++++--------------------------- train_cmd.c | 9 +++++---- train_gui.c | 26 ++++++++++-------------- vehicle.h | 8 ++++---- 10 files changed, 121 insertions(+), 108 deletions(-) diff --git a/aircraft_cmd.c b/aircraft_cmd.c index 9448abc19b..423e97c451 100644 --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -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 @@ static bool HaveHangarInOrderList(Vehicle *v) } #endif -int GetAircraftImage(Vehicle *v, byte direction) +int GetAircraftImage(const Vehicle *v, byte direction) { int spritenum = v->spritenum; @@ -475,21 +476,24 @@ int32 CmdSendAircraftToHangar(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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); } diff --git a/aircraft_gui.c b/aircraft_gui.c index de3ae1b77b..60502ad33e 100644 --- a/aircraft_gui.c +++ b/aircraft_gui.c @@ -36,7 +36,7 @@ void Set_DPARAM_Aircraft_Build_Window(uint16 engine_number) } -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 ShowAircraftRefitWindow(Vehicle *v) 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 @@ static void AircraftDetailsWndProc(Window *w, WindowEvent *e) DrawAircraftImage(v, 3, 57, INVALID_VEHICLE); { + const Vehicle *u; int y = 57; do { @@ -471,34 +471,32 @@ static void AircraftDetailsWndProc(Window *w, WindowEvent *e) } } 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) diff --git a/depot.h b/depot.h index ddd88bf748..05a6df08d8 100644 --- a/depot.h +++ b/depot.h @@ -41,6 +41,16 @@ static inline bool IsDepotIndex(uint index) #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; diff --git a/roadveh_cmd.c b/roadveh_cmd.c index 7cc09504e5..1630d832d9 100644 --- a/roadveh_cmd.c +++ b/roadveh_cmd.c @@ -58,7 +58,7 @@ static const uint16 _road_pf_table_3[4] = { 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 @@ int32 CmdTurnRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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); } diff --git a/roadveh_gui.c b/roadveh_gui.c index 7c52a24ec9..dcff9c803f 100644 --- a/roadveh_gui.c +++ b/roadveh_gui.c @@ -33,7 +33,7 @@ void Set_DPARAM_Road_Veh_Build_Window(uint16 engine_number) 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 DrawRoadVehImage(Vehicle *v, int x, int y, VehicleID selection) 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 @@ static void RoadVehDetailsWndProc(Window *w, WindowEvent *e) 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) diff --git a/ship_cmd.c b/ship_cmd.c index 90810ad968..675b1ed052 100644 --- a/ship_cmd.c +++ b/ship_cmd.c @@ -51,7 +51,7 @@ void DrawShipEngineInfo(int engine, int x, int y, int maxw) 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 @@ int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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); } diff --git a/ship_gui.c b/ship_gui.c index 1d3f275e83..c0cf59e103 100644 --- a/ship_gui.c +++ b/ship_gui.c @@ -34,8 +34,16 @@ void Set_DPARAM_Ship_Build_Window(uint16 engine_number) 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 ShowShipRefitWindow(Vehicle *v) 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 @@ static void ShipDetailsWndProc(Window *w, WindowEvent *e) 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 @@ void ShowShipViewWindow(Vehicle *v) } } - -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; diff --git a/train_cmd.c b/train_cmd.c index 6d571bb25c..75552704f8 100644 --- a/train_cmd.c +++ b/train_cmd.c @@ -1481,20 +1481,21 @@ int32 CmdTrainGotoDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) /** 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; + 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_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 @@ static void CheckIfTrainNeedsService(Vehicle *v) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); } -int32 GetTrainRunningCost(Vehicle *v) +int32 GetTrainRunningCost(const Vehicle *v) { int32 cost = 0; diff --git a/train_gui.c b/train_gui.c index e95b7ca87e..c6bcf66cdd 100644 --- a/train_gui.c +++ b/train_gui.c @@ -280,7 +280,7 @@ static void ShowBuildTrainWindow(uint tile) } } -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 @@ void ShowTrainViewWindow(Vehicle *v) } } -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 TrainDetailsCargoTab(Vehicle *v, int x, int y) } } -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 TrainDetailsInfoTab(Vehicle *v, int x, int y) } } -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 @@ static void TrainDetailsCapacityTab(Vehicle *v, int x, int y) } } -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 TrainDetailsDrawerProc * const _train_details_drawer_proc[3] = { 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 DrawTrainDetailsWindow(Window *w) 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 @@ static void TrainDetailsWndProc(Window *w, WindowEvent *e) 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; diff --git a/vehicle.h b/vehicle.h index 8083ea53ae..6e82372962 100644 --- a/vehicle.h +++ b/vehicle.h @@ -271,9 +271,9 @@ void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building) ; /* 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 @@ UnitID GetFreeUnitNumber(byte type); int LoadUnloadVehicle(Vehicle *v); void UpdateTrainAcceleration(Vehicle *v); -int32 GetTrainRunningCost(Vehicle *v); +int32 GetTrainRunningCost(const Vehicle *v); int CheckTrainStoppedInDepot(const Vehicle *v);