summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/company_cmd.cpp18
-rw-r--r--src/order_cmd.cpp4
-rw-r--r--src/order_func.h2
-rw-r--r--src/saveload/vehicle_sl.cpp15
-rw-r--r--src/vehicle.cpp2
-rw-r--r--src/vehicle_base.h8
-rw-r--r--src/vehicle_cmd.cpp21
-rw-r--r--src/vehicle_func.h2
-rw-r--r--src/vehicle_gui.cpp22
-rw-r--r--src/widgets/vehicle_widget.h1
10 files changed, 85 insertions, 10 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp
index 0abd8c6fb..690058734 100644
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -1170,3 +1170,21 @@ CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, u
return CommandCost();
}
+
+/**
+ * Get the service interval for the given company and vehicle type.
+ * @param c The company, or NULL for client-default settings.
+ * @param type The vehicle type to get the interval for.
+ * @return The service interval.
+ */
+int CompanyServiceInterval(const Company *c, VehicleType type)
+{
+ const VehicleDefaultSettings *vds = (c == NULL) ? &_settings_client.company.vehicle : &c->settings.vehicle;
+ switch (type) {
+ default: NOT_REACHED();
+ case VEH_TRAIN: return vds->servint_trains;
+ case VEH_ROAD: return vds->servint_roadveh;
+ case VEH_AIRCRAFT: return vds->servint_aircraft;
+ case VEH_SHIP: return vds->servint_ships;
+ }
+}
diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp
index c33642cf8..e86e31900 100644
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -1819,9 +1819,9 @@ void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indic
* @param company_id the owner of the vehicle
* @return Clamped service interval
*/
-uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id)
+uint16 GetServiceIntervalClamped(uint interval, bool ispercent)
{
- return (Company::Get(company_id)->settings.vehicle.servint_ispercent) ? Clamp(interval, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(interval, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
+ return ispercent ? Clamp(interval, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(interval, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
}
/**
diff --git a/src/order_func.h b/src/order_func.h
index abdbf3f55..54977181a 100644
--- a/src/order_func.h
+++ b/src/order_func.h
@@ -33,6 +33,6 @@ void DrawOrderString(const Vehicle *v, const Order *order, int order_index, int
#define MIN_SERVINT_DAYS 30
#define MAX_SERVINT_DAYS 800
-uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id);
+uint16 GetServiceIntervalClamped(uint interval, bool ispercent);
#endif /* ORDER_FUNC_H */
diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp
index c1e07bf3c..aebde2bb0 100644
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -17,6 +17,8 @@
#include "../aircraft.h"
#include "../station_base.h"
#include "../effectvehicle_base.h"
+#include "../company_base.h"
+#include "../company_func.h"
#include "saveload.h"
@@ -349,6 +351,19 @@ void AfterLoadVehicles(bool part_of_load)
v->cargo_age_counter = _age_cargo_skip_counter;
}
}
+
+ if (IsSavegameVersionBefore(180)) {
+ /* Set service interval flags */
+ FOR_ALL_VEHICLES(v) {
+ if (!v->IsPrimaryVehicle()) continue;
+
+ const Company *c = Company::Get(v->owner);
+ int interval = CompanyServiceInterval(c, v->type);
+
+ v->SetServiceIntervalIsCustom(v->GetServiceInterval() != interval);
+ v->SetServiceIntervalIsPercent(c->settings.vehicle.servint_ispercent);
+ }
+ }
}
CheckValidVehicles();
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 6c542b91e..2f11421d6 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -111,7 +111,7 @@ bool Vehicle::NeedsServicing() const
/* Are we ready for the next service cycle? */
const Company *c = Company::Get(this->owner);
- if (c->settings.vehicle.servint_ispercent ?
+ if (this->ServiceIntervalIsPercent() ?
(this->reliability >= this->GetEngine()->reliability * (100 - this->GetServiceInterval()) / 100) :
(this->date_of_last_service + this->GetServiceInterval() >= _date)) {
return false;
diff --git a/src/vehicle_base.h b/src/vehicle_base.h
index 6dfea26b7..3343359d5 100644
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -648,6 +648,14 @@ public:
inline uint16 GetServiceInterval() const { return GB(this->service_interval, 0, 16); }
inline void SetServiceInterval(uint16 interval) { SB(this->service_interval, 0, 16, interval); }
+ inline bool ServiceIntervalIsCustom() const { return HasBit(this->service_interval, 31); }
+
+ inline bool ServiceIntervalIsPercent() const { return HasBit(this->service_interval, 30); }
+
+ inline void SetServiceIntervalIsCustom(bool on) { SB(this->service_interval, 31, 1, on); }
+
+ inline void SetServiceIntervalIsPercent(bool on) { SB(this->service_interval, 30, 1, on); }
+
private:
/**
* Advance cur_real_order_index to the next real order.
diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp
index be17c575e..98eb6a989 100644
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -30,6 +30,7 @@
#include "order_backup.h"
#include "ship.h"
#include "newgrf.h"
+#include "company_base.h"
#include "table/strings.h"
@@ -1030,7 +1031,10 @@ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
* @param tile unused
* @param flags type of operation
* @param p1 vehicle ID that is being service-interval-changed
- * @param p2 new service interval
+ * @param p2 bitmask
+ * - p2 = (bit 0-15) - new service interval
+ * - p2 = (bit 16) - service interval is custom flag
+ * - p2 = (bit 17) - service interval is percentage flag
* @param text unused
* @return the cost of this operation or an error
*/
@@ -1042,11 +1046,22 @@ CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1,
CommandCost ret = CheckOwnership(v->owner);
if (ret.Failed()) return ret;
- uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
- if (serv_int != p2) return CMD_ERROR;
+ const Company *company = Company::Get(v->owner);
+ bool iscustom = HasBit(p2, 16);
+ bool ispercent = iscustom ? HasBit(p2, 17) : company->settings.vehicle.servint_ispercent;
+
+ uint16 serv_int;
+ if (iscustom) {
+ serv_int = GB(p2, 0, 16);
+ if (serv_int != GetServiceIntervalClamped(serv_int, ispercent)) return CMD_ERROR;
+ } else {
+ serv_int = CompanyServiceInterval(company, v->type);
+ }
if (flags & DC_EXEC) {
v->SetServiceInterval(serv_int);
+ v->SetServiceIntervalIsCustom(iscustom);
+ v->SetServiceIntervalIsPercent(ispercent);
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
}
diff --git a/src/vehicle_func.h b/src/vehicle_func.h
index 758ad8305..3efed2d5a 100644
--- a/src/vehicle_func.h
+++ b/src/vehicle_func.h
@@ -176,4 +176,6 @@ void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
void CheckCargoCapacity(Vehicle *v);
+int CompanyServiceInterval(const Company *c, VehicleType type);
+
#endif /* VEHICLE_FUNC_H */
diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp
index ecb31d2e0..913220d67 100644
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -1687,6 +1687,8 @@ static const NWidgetPart _nested_nontrain_vehicle_details_widgets[] = {
SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_VD_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
SetDataTip(AWV_INCREASE, STR_VEHICLE_DETAILS_INCREASE_SERVICING_INTERVAL_TOOLTIP),
+ NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_VD_DEFAULT_SERVICING_INTERVAL), SetFill(0, 1),
+ SetDataTip(STR_BUTTON_DEFAULT, 0),
NWidget(WWT_PANEL, COLOUR_GREY, WID_VD_SERVICING_INTERVAL), SetFill(1, 1), SetResize(1, 0), EndContainer(),
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
EndContainer(),
@@ -1711,6 +1713,8 @@ static const NWidgetPart _nested_train_vehicle_details_widgets[] = {
SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_VD_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
SetDataTip(AWV_INCREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
+ NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_VD_DEFAULT_SERVICING_INTERVAL), SetFill(0, 1),
+ SetDataTip(STR_BUTTON_DEFAULT, 0),
NWidget(WWT_PANEL, COLOUR_GREY, WID_VD_SERVICING_INTERVAL), SetFill(1, 1), SetResize(1, 0), EndContainer(),
EndContainer(),
NWidget(NWID_HORIZONTAL),
@@ -1984,7 +1988,7 @@ struct VehicleDetailsWindow : Window {
SetDParam(0, v->GetServiceInterval());
SetDParam(1, v->date_of_last_service);
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + (r.bottom - r.top + 1 - FONT_HEIGHT_NORMAL) / 2,
- Company::Get(v->owner)->settings.vehicle.servint_ispercent ? STR_VEHICLE_DETAILS_SERVICING_INTERVAL_PERCENT : STR_VEHICLE_DETAILS_SERVICING_INTERVAL_DAYS);
+ v->ServiceIntervalIsPercent() ? STR_VEHICLE_DETAILS_SERVICING_INTERVAL_PERCENT : STR_VEHICLE_DETAILS_SERVICING_INTERVAL_DAYS);
break;
}
}
@@ -2007,6 +2011,8 @@ struct VehicleDetailsWindow : Window {
WID_VD_DECREASE_SERVICING_INTERVAL,
WIDGET_LIST_END);
+ this->SetWidgetLoweredState(WID_VD_DEFAULT_SERVICING_INTERVAL, !v->ServiceIntervalIsCustom());
+
this->DrawWidgets();
}
@@ -2027,10 +2033,20 @@ struct VehicleDetailsWindow : Window {
const Vehicle *v = Vehicle::Get(this->window_number);
mod = (widget == WID_VD_DECREASE_SERVICING_INTERVAL) ? -mod : mod;
- mod = GetServiceIntervalClamped(mod + v->GetServiceInterval(), v->owner);
+ mod = GetServiceIntervalClamped(mod + v->GetServiceInterval(), v->ServiceIntervalIsPercent());
if (mod == v->GetServiceInterval()) return;
- DoCommandP(v->tile, v->index, mod, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SERVICING));
+ DoCommandP(v->tile, v->index, mod | (1 << 16) | (v->ServiceIntervalIsPercent() << 17), CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SERVICING));
+ break;
+ }
+
+ case WID_VD_DEFAULT_SERVICING_INTERVAL: {
+ const Vehicle *v = Vehicle::Get(this->window_number);
+ if (_ctrl_pressed) {
+ DoCommandP(v->tile, v->index, v->service_interval | (1 << 16) | (!v->ServiceIntervalIsPercent() << 17), CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SERVICING));
+ } else {
+ DoCommandP(v->tile, v->index, v->service_interval | (!v->ServiceIntervalIsCustom() << 16) | (v->ServiceIntervalIsPercent() << 17), CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SERVICING));
+ }
break;
}
diff --git a/src/widgets/vehicle_widget.h b/src/widgets/vehicle_widget.h
index 3a68b00e5..ac9cf257b 100644
--- a/src/widgets/vehicle_widget.h
+++ b/src/widgets/vehicle_widget.h
@@ -49,6 +49,7 @@ enum VehicleDetailsWidgets {
WID_VD_TOP_DETAILS, ///< Panel with generic details.
WID_VD_INCREASE_SERVICING_INTERVAL, ///< Increase the servicing interval.
WID_VD_DECREASE_SERVICING_INTERVAL, ///< Decrease the servicing interval.
+ WID_VD_DEFAULT_SERVICING_INTERVAL, ///< Toggle default servicing interval.
WID_VD_SERVICING_INTERVAL, ///< Information about the servicing interval.
WID_VD_MIDDLE_DETAILS, ///< Details for non-trains.
WID_VD_MATRIX, ///< List of details for trains.