From 4e5d841d666cf7e1f06918d008c2b4b723196f41 Mon Sep 17 00:00:00 2001 From: frosch Date: Tue, 30 Aug 2011 20:21:01 +0000 Subject: [PATCH] (svn r22858) -Feature: Conditional order depending on remaining lifetime of a vehicle. (monoid) --- src/lang/english.txt | 5 ++++- src/order_cmd.cpp | 13 +++++++------ src/order_gui.cpp | 34 +++++++++++++++++++++------------- src/order_type.h | 13 +++++++------ 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/lang/english.txt b/src/lang/english.txt index 301a67db14..4d190d5e65 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -3198,12 +3198,15 @@ STR_ORDER_DROP_HALT_DEPOT :Stop STR_ORDER_SERVICE_TOOLTIP :{BLACK}Skip this order unless a service is needed STR_ORDER_CONDITIONAL_VARIABLE_TOOLTIP :{BLACK}Vehicle data to base jumping on + +# Conditional order variables, must follow order of OrderConditionVariable enum STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE :Load percentage STR_ORDER_CONDITIONAL_RELIABILITY :Reliability STR_ORDER_CONDITIONAL_MAX_SPEED :Maximum speed -STR_ORDER_CONDITIONAL_AGE :Vehicle age (years) +STR_ORDER_CONDITIONAL_AGE :Age (years) STR_ORDER_CONDITIONAL_REQUIRES_SERVICE :Requires service STR_ORDER_CONDITIONAL_UNCONDITIONALLY :Always +STR_ORDER_CONDITIONAL_REMAINING_LIFETIME :Remaining lifetime (years) STR_ORDER_CONDITIONAL_COMPARATOR_TOOLTIP :{BLACK}How to compare the vehicle data to the given value STR_ORDER_CONDITIONAL_COMPARATOR_EQUALS :is equal to diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 9a90460057..5f69fcd147 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1799,12 +1799,13 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v) uint16 value = order->GetConditionValue(); switch (order->GetConditionVariable()) { - case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break; - case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break; - case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break; - case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break; - case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break; - case OCV_UNCONDITIONALLY: skip_order = true; break; + case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break; + case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break; + case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break; + case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break; + case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break; + case OCV_UNCONDITIONALLY: skip_order = true; break; + case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, max(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1, 0) / DAYS_IN_LEAP_YEAR, value); break; default: NOT_REACHED(); } diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 1a16d0dedd..b62c440e3d 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -19,6 +19,7 @@ #include "strings_func.h" #include "window_func.h" #include "company_func.h" +#include "widgets/dropdown_type.h" #include "widgets/dropdown_func.h" #include "textbuf_gui.h" #include "string_func.h" @@ -134,14 +135,15 @@ static const StringID _order_goto_dropdown_aircraft[] = { INVALID_STRING_ID }; -static const StringID _order_conditional_variable[] = { - STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE, - STR_ORDER_CONDITIONAL_RELIABILITY, - STR_ORDER_CONDITIONAL_MAX_SPEED, - STR_ORDER_CONDITIONAL_AGE, - STR_ORDER_CONDITIONAL_REQUIRES_SERVICE, - STR_ORDER_CONDITIONAL_UNCONDITIONALLY, - INVALID_STRING_ID, +/** Variables for conditional orders; this defines the order of appearance in the dropdown box */ +static const OrderConditionVariable _order_conditional_variable[] = { + OCV_LOAD_PERCENTAGE, + OCV_RELIABILITY, + OCV_MAX_SPEED, + OCV_AGE, + OCV_REMAINING_LIFETIME, + OCV_REQUIRES_SERVICE, + OCV_UNCONDITIONALLY, }; static const StringID _order_conditional_condition[] = { @@ -783,8 +785,8 @@ public: case ORDER_WIDGET_COND_VARIABLE: { Dimension d = {0, 0}; - for (int i = 0; _order_conditional_variable[i] != INVALID_STRING_ID; i++) { - d = maxdim(d, GetStringBoundingBox(_order_conditional_variable[i])); + for (uint i = 0; i < lengthof(_order_conditional_variable); i++) { + d = maxdim(d, GetStringBoundingBox(STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + _order_conditional_variable[i])); } d.width += padding.width; d.height += padding.height; @@ -996,7 +998,7 @@ public: } OrderConditionVariable ocv = order->GetConditionVariable(); /* Set the strings for the dropdown boxes. */ - this->GetWidget(ORDER_WIDGET_COND_VARIABLE)->widget_data = _order_conditional_variable[order == NULL ? 0 : ocv]; + this->GetWidget(ORDER_WIDGET_COND_VARIABLE)->widget_data = STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + (order == NULL ? 0 : ocv); this->GetWidget(ORDER_WIDGET_COND_COMPARATOR)->widget_data = _order_conditional_condition[order == NULL ? 0 : order->GetConditionComparator()]; this->SetWidgetDisabledState(ORDER_WIDGET_COND_COMPARATOR, ocv == OCV_UNCONDITIONALLY); this->SetWidgetDisabledState(ORDER_WIDGET_COND_VALUE, ocv == OCV_REQUIRES_SERVICE || ocv == OCV_UNCONDITIONALLY); @@ -1225,9 +1227,14 @@ public: ShowTimetableWindow(this->vehicle); break; - case ORDER_WIDGET_COND_VARIABLE: - ShowDropDownMenu(this, _order_conditional_variable, this->vehicle->GetOrder(this->OrderGetSel())->GetConditionVariable(), ORDER_WIDGET_COND_VARIABLE, 0, 0); + case ORDER_WIDGET_COND_VARIABLE: { + DropDownList *list = new DropDownList(); + for (uint i = 0; i < lengthof(_order_conditional_variable); i++) { + list->push_back(new DropDownListStringItem(STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + _order_conditional_variable[i], _order_conditional_variable[i], false)); + } + ShowDropDownList(this, list, this->vehicle->GetOrder(this->OrderGetSel())->GetConditionVariable(), ORDER_WIDGET_COND_VARIABLE); break; + } case ORDER_WIDGET_COND_COMPARATOR: { const Order *o = this->vehicle->GetOrder(this->OrderGetSel()); @@ -1264,6 +1271,7 @@ public: case OCV_RELIABILITY: case OCV_LOAD_PERCENTAGE: value = Clamp(value, 0, 100); + break; default: break; diff --git a/src/order_type.h b/src/order_type.h index 8aafe5835d..0c6f86542f 100644 --- a/src/order_type.h +++ b/src/order_type.h @@ -110,12 +110,13 @@ DECLARE_ENUM_AS_BIT_SET(OrderDepotActionFlags) * Variables (of a vehicle) to 'cause' skipping on. */ enum OrderConditionVariable { - OCV_LOAD_PERCENTAGE, ///< Skip based on the amount of load - OCV_RELIABILITY, ///< Skip based on the reliability - OCV_MAX_SPEED, ///< Skip based on the maximum speed - OCV_AGE, ///< Skip based on the age - OCV_REQUIRES_SERVICE, ///< Skip when the vehicle requires service - OCV_UNCONDITIONALLY, ///< Always skip + OCV_LOAD_PERCENTAGE, ///< Skip based on the amount of load + OCV_RELIABILITY, ///< Skip based on the reliability + OCV_MAX_SPEED, ///< Skip based on the maximum speed + OCV_AGE, ///< Skip based on the age + OCV_REQUIRES_SERVICE, ///< Skip when the vehicle requires service + OCV_UNCONDITIONALLY, ///< Always skip + OCV_REMAINING_LIFETIME, ///< Skip based on the remaining lifetime OCV_END };