Mercurial > hg > openttd
changeset 20062:7456d9748fcb draft
(svn r24995) -Codechange: Add flags to vehicle service interval for custom & ispercent (peter1138)
author | rubidium <rubidium@openttd.org> |
---|---|
date | Thu, 14 Feb 2013 17:06:49 +0000 |
parents | aad15ac081e0 |
children | 9484060b5b83 |
files | src/company_cmd.cpp src/order_cmd.cpp src/order_func.h src/saveload/vehicle_sl.cpp src/vehicle.cpp src/vehicle_base.h src/vehicle_cmd.cpp src/vehicle_func.h src/vehicle_gui.cpp src/widgets/vehicle_widget.h |
diffstat | 10 files changed, 85 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -1170,3 +1170,21 @@ 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; + } +}
--- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1819,9 +1819,9 @@ * @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); } /**
--- a/src/order_func.h +++ b/src/order_func.h @@ -33,6 +33,6 @@ #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 */
--- 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 @@ 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();
--- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -111,7 +111,7 @@ /* 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;
--- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -648,6 +648,14 @@ 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.
--- 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 @@ * @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 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); }
--- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -176,4 +176,6 @@ void CheckCargoCapacity(Vehicle *v); +int CompanyServiceInterval(const Company *c, VehicleType type); + #endif /* VEHICLE_FUNC_H */
--- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1687,6 +1687,8 @@ 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 @@ 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 @@ 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 @@ WID_VD_DECREASE_SERVICING_INTERVAL, WIDGET_LIST_END); + this->SetWidgetLoweredState(WID_VD_DEFAULT_SERVICING_INTERVAL, !v->ServiceIntervalIsCustom()); + this->DrawWidgets(); } @@ -2027,10 +2033,20 @@ 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; }
--- a/src/widgets/vehicle_widget.h +++ b/src/widgets/vehicle_widget.h @@ -49,6 +49,7 @@ 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.