Changeset - r20038:7e99f805aa24
[Not reviewed]
master
0 10 0
rubidium - 12 years ago 2013-02-14 17:06:49
rubidium@openttd.org
(svn r24995) -Codechange: Add flags to vehicle service interval for custom & ispercent (peter1138)
10 files changed with 85 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -1170,3 +1170,21 @@ CommandCost CmdRenamePresident(TileIndex
 

	
 
	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;
 
	}
 
}
src/order_cmd.cpp
Show inline comments
 
@@ -1819,9 +1819,9 @@ void DeleteVehicleOrders(Vehicle *v, boo
 
 * @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);
 
}
 

	
 
/**
src/order_func.h
Show inline comments
 
@@ -33,6 +33,6 @@ void DrawOrderString(const Vehicle *v, c
 
#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 */
src/saveload/vehicle_sl.cpp
Show inline comments
 
@@ -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();
src/vehicle.cpp
Show inline comments
 
@@ -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;
src/vehicle_base.h
Show inline comments
 
@@ -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.
src/vehicle_cmd.cpp
Show inline comments
 
@@ -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 t
 
 * @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(TileInde
 
	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);
 
	}
 

	
src/vehicle_func.h
Show inline comments
 
@@ -176,4 +176,6 @@ void GetVehicleSet(VehicleSet &set, Vehi
 

	
 
void CheckCargoCapacity(Vehicle *v);
 

	
 
int CompanyServiceInterval(const Company *c, VehicleType type);
 

	
 
#endif /* VEHICLE_FUNC_H */
src/vehicle_gui.cpp
Show inline comments
 
@@ -1687,6 +1687,8 @@ static const NWidgetPart _nested_nontrai
 
				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_v
 
				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;
 
			}
 

	
src/widgets/vehicle_widget.h
Show inline comments
 
@@ -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.
0 comments (0 inline, 0 general)