mirror of https://github.com/OpenTTD/OpenTTD
(svn r24995) -Codechange: Add flags to vehicle service interval for custom & ispercent (peter1138)
parent
c77cd1f409
commit
369a6f9d1b
|
@ -1170,3 +1170,21 @@ CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, u
|
||||||
|
|
||||||
return CommandCost();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1819,9 +1819,9 @@ void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indic
|
||||||
* @param company_id the owner of the vehicle
|
* @param company_id the owner of the vehicle
|
||||||
* @return Clamped service interval
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -33,6 +33,6 @@ void DrawOrderString(const Vehicle *v, const Order *order, int order_index, int
|
||||||
#define MIN_SERVINT_DAYS 30
|
#define MIN_SERVINT_DAYS 30
|
||||||
#define MAX_SERVINT_DAYS 800
|
#define MAX_SERVINT_DAYS 800
|
||||||
|
|
||||||
uint16 GetServiceIntervalClamped(uint interval, CompanyID company_id);
|
uint16 GetServiceIntervalClamped(uint interval, bool ispercent);
|
||||||
|
|
||||||
#endif /* ORDER_FUNC_H */
|
#endif /* ORDER_FUNC_H */
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
#include "../aircraft.h"
|
#include "../aircraft.h"
|
||||||
#include "../station_base.h"
|
#include "../station_base.h"
|
||||||
#include "../effectvehicle_base.h"
|
#include "../effectvehicle_base.h"
|
||||||
|
#include "../company_base.h"
|
||||||
|
#include "../company_func.h"
|
||||||
|
|
||||||
#include "saveload.h"
|
#include "saveload.h"
|
||||||
|
|
||||||
|
@ -349,6 +351,19 @@ void AfterLoadVehicles(bool part_of_load)
|
||||||
v->cargo_age_counter = _age_cargo_skip_counter;
|
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();
|
CheckValidVehicles();
|
||||||
|
|
|
@ -111,7 +111,7 @@ bool Vehicle::NeedsServicing() const
|
||||||
|
|
||||||
/* Are we ready for the next service cycle? */
|
/* Are we ready for the next service cycle? */
|
||||||
const Company *c = Company::Get(this->owner);
|
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->reliability >= this->GetEngine()->reliability * (100 - this->GetServiceInterval()) / 100) :
|
||||||
(this->date_of_last_service + this->GetServiceInterval() >= _date)) {
|
(this->date_of_last_service + this->GetServiceInterval() >= _date)) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -648,6 +648,14 @@ public:
|
||||||
inline uint16 GetServiceInterval() const { return GB(this->service_interval, 0, 16); }
|
inline uint16 GetServiceInterval() const { return GB(this->service_interval, 0, 16); }
|
||||||
inline void SetServiceInterval(uint16 interval) { SB(this->service_interval, 0, 16, interval); }
|
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:
|
private:
|
||||||
/**
|
/**
|
||||||
* Advance cur_real_order_index to the next real order.
|
* Advance cur_real_order_index to the next real order.
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include "order_backup.h"
|
#include "order_backup.h"
|
||||||
#include "ship.h"
|
#include "ship.h"
|
||||||
#include "newgrf.h"
|
#include "newgrf.h"
|
||||||
|
#include "company_base.h"
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
|
||||||
|
@ -1030,7 +1031,10 @@ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
|
||||||
* @param tile unused
|
* @param tile unused
|
||||||
* @param flags type of operation
|
* @param flags type of operation
|
||||||
* @param p1 vehicle ID that is being service-interval-changed
|
* @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
|
* @param text unused
|
||||||
* @return the cost of this operation or an error
|
* @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);
|
CommandCost ret = CheckOwnership(v->owner);
|
||||||
if (ret.Failed()) return ret;
|
if (ret.Failed()) return ret;
|
||||||
|
|
||||||
uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input
|
const Company *company = Company::Get(v->owner);
|
||||||
if (serv_int != p2) return CMD_ERROR;
|
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) {
|
if (flags & DC_EXEC) {
|
||||||
v->SetServiceInterval(serv_int);
|
v->SetServiceInterval(serv_int);
|
||||||
|
v->SetServiceIntervalIsCustom(iscustom);
|
||||||
|
v->SetServiceIntervalIsPercent(ispercent);
|
||||||
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,4 +176,6 @@ void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
|
||||||
|
|
||||||
void CheckCargoCapacity(Vehicle *v);
|
void CheckCargoCapacity(Vehicle *v);
|
||||||
|
|
||||||
|
int CompanyServiceInterval(const Company *c, VehicleType type);
|
||||||
|
|
||||||
#endif /* VEHICLE_FUNC_H */
|
#endif /* VEHICLE_FUNC_H */
|
||||||
|
|
|
@ -1687,6 +1687,8 @@ static const NWidgetPart _nested_nontrain_vehicle_details_widgets[] = {
|
||||||
SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
|
SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
|
||||||
NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_VD_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
|
NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_VD_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
|
||||||
SetDataTip(AWV_INCREASE, STR_VEHICLE_DETAILS_INCREASE_SERVICING_INTERVAL_TOOLTIP),
|
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_PANEL, COLOUR_GREY, WID_VD_SERVICING_INTERVAL), SetFill(1, 1), SetResize(1, 0), EndContainer(),
|
||||||
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
|
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
|
||||||
EndContainer(),
|
EndContainer(),
|
||||||
|
@ -1711,6 +1713,8 @@ static const NWidgetPart _nested_train_vehicle_details_widgets[] = {
|
||||||
SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
|
SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
|
||||||
NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_VD_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
|
NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_VD_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
|
||||||
SetDataTip(AWV_INCREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
|
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(),
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_VD_SERVICING_INTERVAL), SetFill(1, 1), SetResize(1, 0), EndContainer(),
|
||||||
EndContainer(),
|
EndContainer(),
|
||||||
NWidget(NWID_HORIZONTAL),
|
NWidget(NWID_HORIZONTAL),
|
||||||
|
@ -1984,7 +1988,7 @@ struct VehicleDetailsWindow : Window {
|
||||||
SetDParam(0, v->GetServiceInterval());
|
SetDParam(0, v->GetServiceInterval());
|
||||||
SetDParam(1, v->date_of_last_service);
|
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,
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2007,6 +2011,8 @@ struct VehicleDetailsWindow : Window {
|
||||||
WID_VD_DECREASE_SERVICING_INTERVAL,
|
WID_VD_DECREASE_SERVICING_INTERVAL,
|
||||||
WIDGET_LIST_END);
|
WIDGET_LIST_END);
|
||||||
|
|
||||||
|
this->SetWidgetLoweredState(WID_VD_DEFAULT_SERVICING_INTERVAL, !v->ServiceIntervalIsCustom());
|
||||||
|
|
||||||
this->DrawWidgets();
|
this->DrawWidgets();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2027,10 +2033,20 @@ struct VehicleDetailsWindow : Window {
|
||||||
const Vehicle *v = Vehicle::Get(this->window_number);
|
const Vehicle *v = Vehicle::Get(this->window_number);
|
||||||
|
|
||||||
mod = (widget == WID_VD_DECREASE_SERVICING_INTERVAL) ? -mod : mod;
|
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;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,7 @@ enum VehicleDetailsWidgets {
|
||||||
WID_VD_TOP_DETAILS, ///< Panel with generic details.
|
WID_VD_TOP_DETAILS, ///< Panel with generic details.
|
||||||
WID_VD_INCREASE_SERVICING_INTERVAL, ///< Increase the servicing interval.
|
WID_VD_INCREASE_SERVICING_INTERVAL, ///< Increase the servicing interval.
|
||||||
WID_VD_DECREASE_SERVICING_INTERVAL, ///< Decrease 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_SERVICING_INTERVAL, ///< Information about the servicing interval.
|
||||||
WID_VD_MIDDLE_DETAILS, ///< Details for non-trains.
|
WID_VD_MIDDLE_DETAILS, ///< Details for non-trains.
|
||||||
WID_VD_MATRIX, ///< List of details for trains.
|
WID_VD_MATRIX, ///< List of details for trains.
|
||||||
|
|
Loading…
Reference in New Issue