Changeset - r14750:777536f68ce0
[Not reviewed]
master
0 2 0
terkhen - 15 years ago 2010-03-06 13:03:17
terkhen@openttd.org
(svn r19348) -Add: Allow to sort road vehicles by power and by power vs running cost at the engine preview window.
2 files changed with 26 insertions and 7 deletions:
0 comments (0 inline, 0 general)
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -194,45 +194,46 @@ static int CDECL EnginePowerSorter(const
 

	
 
static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b)
 
{
 
	Money va = Engine::Get(*a)->GetRunningCost();
 
	Money vb = Engine::Get(*b)->GetRunningCost();
 
	int r = ClampToI32(va - vb);
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
 
	return _internal_sort_order ? -r : r;
 
}
 

	
 
/* Train sorting functions */
 
static int CDECL TrainEnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b)
 
static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b)
 
{
 
	const Engine *e_a = Engine::Get(*a);
 
	const Engine *e_b = Engine::Get(*b);
 

	
 
	/* Here we are using a few tricks to get the right sort.
 
	 * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
 
	 * we will actually calculate cunning cost/power (to make it more than 1).
 
	 * Because of this, the return value have to be reversed as well and we return b - a instead of a - b.
 
	 * Another thing is that both power and running costs should be doubled for multiheaded engines.
 
	 * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */
 
	Money va = (e_a->GetRunningCost()) / max(1U, (uint)e_a->GetPower());
 
	Money vb = (e_b->GetRunningCost()) / max(1U, (uint)e_b->GetPower());
 
	int r = ClampToI32(vb - va);
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
 
	return _internal_sort_order ? -r : r;
 
}
 

	
 
/* Train sorting functions */
 

	
 
static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b)
 
{
 
	const RailVehicleInfo *rvi_a = RailVehInfo(*a);
 
	const RailVehicleInfo *rvi_b = RailVehInfo(*b);
 

	
 
	int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
 
	int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
 
	int r = va - vb;
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
 
	return _internal_sort_order ? -r : r;
 
@@ -299,37 +300,39 @@ static int CDECL AircraftEngineCargoSort
 
	return _internal_sort_order ? -r : r;
 
}
 

	
 
static EngList_SortTypeFunction * const _sorter[][10] = {{
 
	/* Trains */
 
	&EngineNumberSorter,
 
	&EngineCostSorter,
 
	&EngineSpeedSorter,
 
	&EnginePowerSorter,
 
	&EngineIntroDateSorter,
 
	&EngineNameSorter,
 
	&EngineRunningCostSorter,
 
	&TrainEnginePowerVsRunningCostSorter,
 
	&EnginePowerVsRunningCostSorter,
 
	&EngineReliabilitySorter,
 
	&TrainEngineCapacitySorter,
 
}, {
 
	/* Road vehicles */
 
	&EngineNumberSorter,
 
	&EngineCostSorter,
 
	&EngineSpeedSorter,
 
	&EngineIntroDateSorter,
 
	&EngineNameSorter,
 
	&EngineRunningCostSorter,
 
	&EngineReliabilitySorter,
 
	&RoadVehEngineCapacitySorter,
 
	&EnginePowerSorter,
 
	&EnginePowerVsRunningCostSorter,
 
}, {
 
	/* Ships */
 
	&EngineNumberSorter,
 
	&EngineCostSorter,
 
	&EngineSpeedSorter,
 
	&EngineIntroDateSorter,
 
	&EngineNameSorter,
 
	&EngineRunningCostSorter,
 
	&EngineReliabilitySorter,
 
	&ShipEngineCapacitySorter,
 
}, {
 
	/* Aircraft */
 
@@ -357,24 +360,26 @@ static const StringID _sort_listing[][11
 
	STR_SORT_BY_CARGO_CAPACITY,
 
	INVALID_STRING_ID
 
}, {
 
	/* Road vehicles */
 
	STR_SORT_BY_ENGINE_ID,
 
	STR_SORT_BY_COST,
 
	STR_SORT_BY_MAX_SPEED,
 
	STR_SORT_BY_INTRO_DATE,
 
	STR_SORT_BY_NAME,
 
	STR_SORT_BY_RUNNING_COST,
 
	STR_SORT_BY_RELIABILITY,
 
	STR_SORT_BY_CARGO_CAPACITY,
 
	STR_SORT_BY_POWER,
 
	STR_SORT_BY_POWER_VS_RUNNING_COST,
 
	INVALID_STRING_ID
 
}, {
 
	/* Ships */
 
	STR_SORT_BY_ENGINE_ID,
 
	STR_SORT_BY_COST,
 
	STR_SORT_BY_MAX_SPEED,
 
	STR_SORT_BY_INTRO_DATE,
 
	STR_SORT_BY_NAME,
 
	STR_SORT_BY_RUNNING_COST,
 
	STR_SORT_BY_RELIABILITY,
 
	STR_SORT_BY_CARGO_CAPACITY,
 
	INVALID_STRING_ID
 
@@ -1038,27 +1043,34 @@ struct BuildVehicleWindow : Window {
 
				this->SetDirty();
 
				break;
 

	
 
			case BUILD_VEHICLE_WIDGET_LIST: {
 
				uint i = (pt.y - this->GetWidget<NWidgetBase>(BUILD_VEHICLE_WIDGET_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
 
				size_t num_items = this->eng_list.Length();
 
				this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
 
				this->SetDirty();
 
				if (click_count > 1) this->OnClick(pt, BUILD_VEHICLE_WIDGET_BUILD, 1);
 
				break;
 
			}
 

	
 
			case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: // Select sorting criteria dropdown menu
 
				ShowDropDownMenu(this, _sort_listing[this->vehicle_type], this->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0);
 
				break;
 
			case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: { // Select sorting criteria dropdown menu
 
				uint32 hidden_mask = 0;
 
				/* Disable sorting by power when the original acceleration model for road vehicles is being used. */
 
				if (this->vehicle_type == VEH_ROAD &&
 
						_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) {
 
					SetBit(hidden_mask, 8);
 
					SetBit(hidden_mask, 9);
 
				}
 
				ShowDropDownMenu(this, _sort_listing[this->vehicle_type], this->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, hidden_mask);
 
			} break;
 

	
 
			case BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN: // Select cargo filtering criteria dropdown menu
 
				ShowDropDownMenu(this, this->cargo_filter_texts, this->cargo_filter_criteria, BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN, 0, 0);
 
				break;
 

	
 
			case BUILD_VEHICLE_WIDGET_BUILD: {
 
				EngineID sel_eng = this->sel_engine;
 
				if (sel_eng != INVALID_ENGINE) {
 
					CommandCallback *callback = (this->vehicle_type == VEH_TRAIN && RailVehInfo(sel_eng)->railveh_type == RAILVEH_WAGON) ? CcBuildWagon : CcBuildPrimaryVehicle;
 
					DoCommandP(this->window_number, sel_eng, 0, GetCmdBuildVeh(this->vehicle_type), callback);
 
				}
 
				break;
 
@@ -1069,24 +1081,31 @@ struct BuildVehicleWindow : Window {
 
				if (sel_eng != INVALID_ENGINE) {
 
					this->rename_engine = sel_eng;
 
					SetDParam(0, sel_eng);
 
					ShowQueryString(STR_ENGINE_NAME, STR_QUERY_RENAME_TRAIN_TYPE_CAPTION + this->vehicle_type, MAX_LENGTH_ENGINE_NAME_BYTES, MAX_LENGTH_ENGINE_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
 
				}
 
				break;
 
			}
 
		}
 
	}
 

	
 
	virtual void OnInvalidateData(int data)
 
	{
 
		/* When switching to original acceleration model for road vehicles, clear the selected sort criteria if it is not available now. */
 
		if (this->vehicle_type == VEH_ROAD &&
 
				_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL &&
 
				this->sort_criteria > 7) {
 
			this->sort_criteria = 0;
 
			_last_sort_criteria[VEH_ROAD] = 0;
 
		}
 
		this->eng_list.ForceRebuild();
 
	}
 

	
 
	virtual void SetStringParameters(int widget) const
 
	{
 
		switch (widget) {
 
			case BUILD_VEHICLE_WIDGET_CAPTION:
 
				if (this->vehicle_type == VEH_TRAIN && !this->listview_mode) {
 
					const RailtypeInfo *rti = GetRailTypeInfo(this->filter.railtype);
 
					SetDParam(0, rti->strings.build_caption);
 
				} else {
 
					SetDParam(0, (this->listview_mode ? STR_VEHICLE_LIST_AVAILABLE_TRAINS : STR_BUY_VEHICLE_TRAIN_ALL_CAPTION) + this->vehicle_type);
src/settings.cpp
Show inline comments
 
@@ -784,25 +784,25 @@ static bool RoadVehAccelerationModelChan
 
{
 
	if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
 
		RoadVehicle *rv;
 
		FOR_ALL_ROADVEHICLES(rv) {
 
			if (rv->IsRoadVehFront()) {
 
				rv->CargoChanged();
 
			}
 
		}
 
	}
 

	
 
	/* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
 
	SetWindowClassesDirty(WC_ENGINE_PREVIEW);
 
	SetWindowClassesDirty(WC_BUILD_VEHICLE);
 
	InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
 
	SetWindowClassesDirty(WC_VEHICLE_DETAILS);
 

	
 
	return true;
 
}
 

	
 
/**
 
 * This function updates the road vehicle acceleration cache after a steepness change.
 
 * @param p1 Callback parameter.
 
 * @return Always true.
 
 */
 
static bool RoadVehSlopeSteepnessChanged(int32 p1)
 
{
0 comments (0 inline, 0 general)