changeset 18043:c9919ff3aae4 draft

(svn r22858) -Feature: Conditional order depending on remaining lifetime of a vehicle. (monoid)
author frosch <frosch@openttd.org>
date Tue, 30 Aug 2011 20:21:01 +0000
parents af7c4aeb37dd
children 33c23534eccc
files src/lang/english.txt src/order_cmd.cpp src/order_gui.cpp src/order_type.h
diffstat 4 files changed, 39 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -3198,12 +3198,15 @@
 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
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -1799,12 +1799,13 @@
 	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();
 	}
 
--- 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 @@
 	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 @@
 
 			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 @@
 					}
 					OrderConditionVariable ocv = order->GetConditionVariable();
 					/* Set the strings for the dropdown boxes. */
-					this->GetWidget<NWidgetCore>(ORDER_WIDGET_COND_VARIABLE)->widget_data   = _order_conditional_variable[order == NULL ? 0 : ocv];
+					this->GetWidget<NWidgetCore>(ORDER_WIDGET_COND_VARIABLE)->widget_data   = STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + (order == NULL ? 0 : ocv);
 					this->GetWidget<NWidgetCore>(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 @@
 				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 @@
 				case OCV_RELIABILITY:
 				case OCV_LOAD_PERCENTAGE:
 					value = Clamp(value, 0, 100);
+					break;
 
 				default:
 					break;
--- a/src/order_type.h
+++ b/src/order_type.h
@@ -110,12 +110,13 @@
  * 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
 };