Add train weight, power, and max TE tracerestrict conditionals.

Minor refactorings.
This commit is contained in:
Jonathan G Rennison
2016-06-29 22:08:05 +01:00
parent a091780843
commit b5531975da
5 changed files with 215 additions and 13 deletions

View File

@@ -2396,10 +2396,16 @@ STR_TRACE_RESTRICT_VARIABLE_ENTRY_DIRECTION :entry direction
STR_TRACE_RESTRICT_VARIABLE_PBS_ENTRY_SIGNAL :PBS entry signal STR_TRACE_RESTRICT_VARIABLE_PBS_ENTRY_SIGNAL :PBS entry signal
STR_TRACE_RESTRICT_VARIABLE_PBS_ENTRY_SIGNAL_LONG :entered signal of PBS block STR_TRACE_RESTRICT_VARIABLE_PBS_ENTRY_SIGNAL_LONG :entered signal of PBS block
STR_TRACE_RESTRICT_VARIABLE_TRAIN_GROUP :train group STR_TRACE_RESTRICT_VARIABLE_TRAIN_GROUP :train group
STR_TRACE_RESTRICT_VARIABLE_TRAIN_WEIGHT :weight
STR_TRACE_RESTRICT_VARIABLE_TRAIN_POWER :power
STR_TRACE_RESTRICT_VARIABLE_TRAIN_MAX_TE :max T.E.
STR_TRACE_RESTRICT_VARIABLE_UNDEFINED :undefined STR_TRACE_RESTRICT_VARIABLE_UNDEFINED :undefined
STR_TRACE_RESTRICT_VARIABLE_UNDEFINED_RED :{RED}undefined STR_TRACE_RESTRICT_VARIABLE_UNDEFINED_RED :{RED}undefined
STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_INTEGER :{STRING} {STRING} {STRING} {COMMA} then STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_INTEGER :{STRING} {STRING} {STRING} {COMMA} then
STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_SPEED :{STRING} {STRING} {STRING} {VELOCITY} then STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_SPEED :{STRING} {STRING} {STRING} {VELOCITY} then
STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_WEIGHT :{STRING} {STRING} {STRING} {WEIGHT_SHORT} then
STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_POWER :{STRING} {STRING} {STRING} {POWER} then
STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_FORCE :{STRING} {STRING} {STRING} {FORCE} then
STR_TRACE_RESTRICT_CONDITIONAL_ORDER_STATION :{STRING} {STRING} {STRING} {STATION} then STR_TRACE_RESTRICT_CONDITIONAL_ORDER_STATION :{STRING} {STRING} {STRING} {STATION} then
STR_TRACE_RESTRICT_CONDITIONAL_ORDER_WAYPOINT :{STRING} {STRING} {STRING} {WAYPOINT} then STR_TRACE_RESTRICT_CONDITIONAL_ORDER_WAYPOINT :{STRING} {STRING} {STRING} {WAYPOINT} then
STR_TRACE_RESTRICT_CONDITIONAL_ORDER_DEPOT :{STRING} {STRING} {STRING} {DEPOT} then STR_TRACE_RESTRICT_CONDITIONAL_ORDER_DEPOT :{STRING} {STRING} {STRING} {DEPOT} then

View File

@@ -759,6 +759,67 @@ uint ConvertDisplaySpeedToKmhishSpeed(uint speed)
{ {
return _units_velocity[_settings_game.locale.units_velocity].c.FromDisplay(speed * 16, true, 10); return _units_velocity[_settings_game.locale.units_velocity].c.FromDisplay(speed * 16, true, 10);
} }
/**
* Convert the given internal weight to the display weight.
* @param weight the weight to convert
* @return the converted weight.
*/
uint ConvertWeightToDisplayWeight(uint weight)
{
return _units_weight[_settings_game.locale.units_weight].c.ToDisplay(weight);
}
/**
* Convert the given display weight to the (internal) weight.
* @param weight the weight to convert
* @return the converted weight.
*/
uint ConvertDisplayWeightToWeight(uint weight)
{
return _units_weight[_settings_game.locale.units_weight].c.FromDisplay(weight);
}
/**
* Convert the given internal power to the display power.
* @param power the power to convert
* @return the converted power.
*/
uint ConvertPowerToDisplayPower(uint power)
{
return _units_power[_settings_game.locale.units_power].c.ToDisplay(power);
}
/**
* Convert the given display power to the (internal) power.
* @param power the power to convert
* @return the converted power.
*/
uint ConvertDisplayPowerToPower(uint power)
{
return _units_power[_settings_game.locale.units_power].c.FromDisplay(power);
}
/**
* Convert the given internal force to the display force.
* @param force the force to convert
* @return the converted force.
*/
uint ConvertForceToDisplayForce(uint force)
{
return _units_force[_settings_game.locale.units_force].c.ToDisplay(force);
}
/**
* Convert the given display force to the (internal) force.
* @param force the force to convert
* @return the converted force.
*/
uint ConvertDisplayForceToForce(uint force)
{
return _units_force[_settings_game.locale.units_force].c.FromDisplay(force);
}
/** /**
* Parse most format codes within a string and write the result to a buffer. * Parse most format codes within a string and write the result to a buffer.
* @param buff The buffer to write the final string to. * @param buff The buffer to write the final string to.

View File

@@ -349,6 +349,27 @@ void TraceRestrictProgram::Execute(const Train* v, const TraceRestrictProgramInp
break; break;
} }
case TRIT_COND_PHYS_PROP: {
switch (static_cast<TraceRestrictPhysPropCondAuxField>(GetTraceRestrictAuxField(item))) {
case TRPPCAF_WEIGHT:
result = TestCondition(v->gcache.cached_weight, condop, condvalue);
break;
case TRPPCAF_POWER:
result = TestCondition(v->gcache.cached_power, condop, condvalue);
break;
case TRPPCAF_MAX_TE:
result = TestCondition(v->gcache.cached_max_te / 1000, condop, condvalue);
break;
default:
NOT_REACHED();
break;
}
break;
}
default: default:
NOT_REACHED(); NOT_REACHED();
} }
@@ -486,6 +507,7 @@ CommandCost TraceRestrictProgram::Validate(const std::vector<TraceRestrictItem>
case TRIT_COND_ENTRY_DIRECTION: case TRIT_COND_ENTRY_DIRECTION:
case TRIT_COND_PBS_ENTRY_SIGNAL: case TRIT_COND_PBS_ENTRY_SIGNAL:
case TRIT_COND_TRAIN_GROUP: case TRIT_COND_TRAIN_GROUP:
case TRIT_COND_PHYS_PROP:
break; break;
default: default:
@@ -559,6 +581,9 @@ void SetTraceRestrictValueDefault(TraceRestrictItem &item, TraceRestrictValueTyp
case TRVT_TILE_INDEX: case TRVT_TILE_INDEX:
case TRVT_RESERVE_THROUGH: case TRVT_RESERVE_THROUGH:
case TRVT_LONG_RESERVE: case TRVT_LONG_RESERVE:
case TRVT_WEIGHT:
case TRVT_POWER:
case TRVT_FORCE:
SetTraceRestrictValue(item, 0); SetTraceRestrictValue(item, 0);
SetTraceRestrictAuxField(item, 0); SetTraceRestrictAuxField(item, 0);
break; break;
@@ -598,7 +623,7 @@ void SetTraceRestrictValueDefault(TraceRestrictItem &item, TraceRestrictValueTyp
/** /**
* Set the type field of a TraceRestrictItem, and resets any other fields which are no longer valid/meaningful to sensible defaults * Set the type field of a TraceRestrictItem, and resets any other fields which are no longer valid/meaningful to sensible defaults
*/ */
void SetTraceRestrictTypeAndNormalise(TraceRestrictItem &item, TraceRestrictItemType type) void SetTraceRestrictTypeAndNormalise(TraceRestrictItem &item, TraceRestrictItemType type, uint8 aux_data)
{ {
if (item != 0) { if (item != 0) {
assert(GetTraceRestrictType(item) != TRIT_NULL); assert(GetTraceRestrictType(item) != TRIT_NULL);
@@ -608,12 +633,26 @@ void SetTraceRestrictTypeAndNormalise(TraceRestrictItem &item, TraceRestrictItem
TraceRestrictTypePropertySet old_properties = GetTraceRestrictTypeProperties(item); TraceRestrictTypePropertySet old_properties = GetTraceRestrictTypeProperties(item);
SetTraceRestrictType(item, type); SetTraceRestrictType(item, type);
bool set_aux_field = false;
if (type == TRIT_COND_PHYS_PROP) {
SetTraceRestrictAuxField(item, aux_data);
set_aux_field = true;
} else {
assert(aux_data == 0);
}
TraceRestrictTypePropertySet new_properties = GetTraceRestrictTypeProperties(item); TraceRestrictTypePropertySet new_properties = GetTraceRestrictTypeProperties(item);
if (old_properties.cond_type != new_properties.cond_type || if (old_properties.cond_type != new_properties.cond_type ||
old_properties.value_type != new_properties.value_type) { old_properties.value_type != new_properties.value_type) {
SetTraceRestrictCondOp(item, TRCO_IS); SetTraceRestrictCondOp(item, TRCO_IS);
SetTraceRestrictValueDefault(item, new_properties.value_type); SetTraceRestrictValueDefault(item, new_properties.value_type);
if (set_aux_field) {
SetTraceRestrictAuxField(item, aux_data);
}
}
if (GetTraceRestrictType(item) == TRIT_COND_LAST_STATION && GetTraceRestrictAuxField(item) != TROCAF_STATION) {
// if changing type from another order type to last visited station, reset value if not currently a station
SetTraceRestrictValueDefault(item, TRVT_ORDER);
} }
} }

View File

@@ -113,6 +113,7 @@ enum TraceRestrictItemType {
TRIT_COND_ENTRY_DIRECTION = 16, ///< Test which side of signal/signal tile is being entered from TRIT_COND_ENTRY_DIRECTION = 16, ///< Test which side of signal/signal tile is being entered from
TRIT_COND_PBS_ENTRY_SIGNAL = 17, ///< Test tile and PBS-state of previous signal TRIT_COND_PBS_ENTRY_SIGNAL = 17, ///< Test tile and PBS-state of previous signal
TRIT_COND_TRAIN_GROUP = 18, ///< Test train group membership TRIT_COND_TRAIN_GROUP = 18, ///< Test train group membership
TRIT_COND_PHYS_PROP = 19, ///< Test train physical property
//TRIT_COND_TRAIN_OWNER = 24, ///< Test train owner: reserved for future use //TRIT_COND_TRAIN_OWNER = 24, ///< Test train owner: reserved for future use
/* space up to 31 */ /* space up to 31 */
}; };
@@ -172,6 +173,16 @@ enum TraceRestrictOrderCondAuxField {
/* space up to 3 */ /* space up to 3 */
}; };
/**
* TraceRestrictItem auxiliary type field, for physical property type conditionals
*/
enum TraceRestrictPhysPropCondAuxField {
TRPPCAF_WEIGHT = 0, ///< value field is a weight
TRPPCAF_POWER = 1, ///< value field is a power
TRPPCAF_MAX_TE = 2, ///< value field is a tractive effort
/* space up to 3 */
};
/** /**
* TraceRestrictItem auxiliary type field, for order type conditionals * TraceRestrictItem auxiliary type field, for order type conditionals
*/ */
@@ -414,6 +425,9 @@ enum TraceRestrictValueType {
TRVT_RESERVE_THROUGH = 10,///< takes a value 0 = reserve through, 1 = cancel previous reserve through TRVT_RESERVE_THROUGH = 10,///< takes a value 0 = reserve through, 1 = cancel previous reserve through
TRVT_LONG_RESERVE = 11,///< takes a value 0 = long reserve, 1 = cancel previous long reserve TRVT_LONG_RESERVE = 11,///< takes a value 0 = long reserve, 1 = cancel previous long reserve
TRVT_GROUP_INDEX = 12,///< takes a GroupID TRVT_GROUP_INDEX = 12,///< takes a GroupID
TRVT_WEIGHT = 13,///< takes a weight
TRVT_POWER = 14,///< takes a power
TRVT_FORCE = 15,///< takes a force
}; };
/** /**
@@ -425,7 +439,7 @@ struct TraceRestrictTypePropertySet {
}; };
void SetTraceRestrictValueDefault(TraceRestrictItem &item, TraceRestrictValueType value_type); void SetTraceRestrictValueDefault(TraceRestrictItem &item, TraceRestrictValueType value_type);
void SetTraceRestrictTypeAndNormalise(TraceRestrictItem &item, TraceRestrictItemType type); void SetTraceRestrictTypeAndNormalise(TraceRestrictItem &item, TraceRestrictItemType type, uint8 aux_data = 0);
/** /**
* Get TraceRestrictTypePropertySet for a given instruction, only looks at value field * Get TraceRestrictTypePropertySet for a given instruction, only looks at value field
@@ -480,6 +494,26 @@ static inline TraceRestrictTypePropertySet GetTraceRestrictTypeProperties(TraceR
out.cond_type = TRCOT_BINARY; out.cond_type = TRCOT_BINARY;
break; break;
case TRIT_COND_PHYS_PROP:
switch (static_cast<TraceRestrictPhysPropCondAuxField>(GetTraceRestrictAuxField(item))) {
case TRPPCAF_WEIGHT:
out.value_type = TRVT_WEIGHT;
break;
case TRPPCAF_POWER:
out.value_type = TRVT_POWER;
break;
case TRPPCAF_MAX_TE:
out.value_type = TRVT_FORCE;
break;
default:
NOT_REACHED();
break;
}
break;
default: default:
NOT_REACHED(); NOT_REACHED();
break; break;

View File

@@ -40,6 +40,12 @@
extern uint ConvertSpeedToDisplaySpeed(uint speed); extern uint ConvertSpeedToDisplaySpeed(uint speed);
extern uint ConvertDisplaySpeedToSpeed(uint speed); extern uint ConvertDisplaySpeedToSpeed(uint speed);
extern uint ConvertWeightToDisplayWeight(uint weight);
extern uint ConvertDisplayWeightToWeight(uint weight);
extern uint ConvertPowerToDisplayPower(uint power);
extern uint ConvertDisplayPowerToPower(uint power);
extern uint ConvertForceToDisplayForce(uint force);
extern uint ConvertDisplayForceToForce(uint force);
/** Widget IDs */ /** Widget IDs */
enum TraceRestrictWindowWidgets { enum TraceRestrictWindowWidgets {
@@ -240,10 +246,26 @@ static StringID GetDropDownStringByValue(const TraceRestrictDropDownListSet *lis
return list_set->string_array[GetDropDownListIndexByValue(list_set, value, false)]; return list_set->string_array[GetDropDownListIndexByValue(list_set, value, false)];
} }
typedef uint TraceRestrictGuiItemType;
static TraceRestrictGuiItemType GetItemGuiType(TraceRestrictItem item)
{
TraceRestrictGuiItemType type = GetTraceRestrictType(item);
if (type == TRIT_COND_PHYS_PROP) {
type |= GetTraceRestrictAuxField(item) << 16;
}
return type;
}
static TraceRestrictItemType ItemTypeFromGuiType(TraceRestrictGuiItemType type)
{
return static_cast<TraceRestrictItemType>(type & 0xFFFF);
}
/** /**
* Return the appropriate type dropdown TraceRestrictDropDownListSet for the given item type @p type * Return the appropriate type dropdown TraceRestrictDropDownListSet for the given item type @p type
*/ */
static const TraceRestrictDropDownListSet *GetTypeDropDownListSet(TraceRestrictItemType type) static const TraceRestrictDropDownListSet *GetTypeDropDownListSet(TraceRestrictGuiItemType type)
{ {
static const StringID str_action[] = { static const StringID str_action[] = {
STR_TRACE_RESTRICT_PF_DENY, STR_TRACE_RESTRICT_PF_DENY,
@@ -272,6 +294,9 @@ static const TraceRestrictDropDownListSet *GetTypeDropDownListSet(TraceRestrictI
STR_TRACE_RESTRICT_VARIABLE_ENTRY_DIRECTION, STR_TRACE_RESTRICT_VARIABLE_ENTRY_DIRECTION,
STR_TRACE_RESTRICT_VARIABLE_PBS_ENTRY_SIGNAL, STR_TRACE_RESTRICT_VARIABLE_PBS_ENTRY_SIGNAL,
STR_TRACE_RESTRICT_VARIABLE_TRAIN_GROUP, STR_TRACE_RESTRICT_VARIABLE_TRAIN_GROUP,
STR_TRACE_RESTRICT_VARIABLE_TRAIN_WEIGHT,
STR_TRACE_RESTRICT_VARIABLE_TRAIN_POWER,
STR_TRACE_RESTRICT_VARIABLE_TRAIN_MAX_TE,
STR_TRACE_RESTRICT_VARIABLE_UNDEFINED, STR_TRACE_RESTRICT_VARIABLE_UNDEFINED,
INVALID_STRING_ID, INVALID_STRING_ID,
}; };
@@ -285,13 +310,16 @@ static const TraceRestrictDropDownListSet *GetTypeDropDownListSet(TraceRestrictI
TRIT_COND_ENTRY_DIRECTION, TRIT_COND_ENTRY_DIRECTION,
TRIT_COND_PBS_ENTRY_SIGNAL, TRIT_COND_PBS_ENTRY_SIGNAL,
TRIT_COND_TRAIN_GROUP, TRIT_COND_TRAIN_GROUP,
TRIT_COND_PHYS_PROP | (TRPPCAF_WEIGHT << 16),
TRIT_COND_PHYS_PROP | (TRPPCAF_POWER << 16),
TRIT_COND_PHYS_PROP | (TRPPCAF_MAX_TE << 16),
TRIT_COND_UNDEFINED, TRIT_COND_UNDEFINED,
}; };
static const TraceRestrictDropDownListSet set_cond = { static const TraceRestrictDropDownListSet set_cond = {
str_cond, val_cond, str_cond, val_cond,
}; };
return IsTraceRestrictTypeConditional(type) ? &set_cond : &set_action; return IsTraceRestrictTypeConditional(ItemTypeFromGuiType(type)) ? &set_cond : &set_action;
} }
/** /**
@@ -378,8 +406,9 @@ static StringID GetCargoStringByID(CargoID cargo)
/** /**
* Get the StringID for a given item type @p type * Get the StringID for a given item type @p type
*/ */
static StringID GetTypeString(TraceRestrictItemType type) static StringID GetTypeString(TraceRestrictItem item)
{ {
TraceRestrictGuiItemType type = GetItemGuiType(item);
return GetDropDownStringByValue(GetTypeDropDownListSet(type), type); return GetDropDownStringByValue(GetTypeDropDownListSet(type), type);
} }
@@ -446,6 +475,9 @@ static bool IsIntegerValueType(TraceRestrictValueType type)
switch (type) { switch (type) {
case TRVT_INT: case TRVT_INT:
case TRVT_SPEED: case TRVT_SPEED:
case TRVT_WEIGHT:
case TRVT_POWER:
case TRVT_FORCE:
return true; return true;
default: default:
@@ -467,6 +499,24 @@ static uint ConvertIntegerValue(TraceRestrictValueType type, uint in, bool to_di
? ConvertSpeedToDisplaySpeed(in) * 10 / 16 ? ConvertSpeedToDisplaySpeed(in) * 10 / 16
: ConvertDisplaySpeedToSpeed(in) * 16 / 10; : ConvertDisplaySpeedToSpeed(in) * 16 / 10;
case TRVT_WEIGHT:
return to_display
? ConvertWeightToDisplayWeight(in)
: ConvertDisplayWeightToWeight(in);
break;
case TRVT_POWER:
return to_display
? ConvertPowerToDisplayPower(in)
: ConvertDisplayPowerToPower(in);
break;
case TRVT_FORCE:
return to_display
? ConvertForceToDisplayForce(in)
: ConvertDisplayForceToForce(in);
break;
case TRVT_PF_PENALTY: case TRVT_PF_PENALTY:
return in; return in;
@@ -549,7 +599,7 @@ static void DrawInstructionStringConditionalCommon(TraceRestrictItem item, const
{ {
assert(GetTraceRestrictCondFlags(item) <= TRCF_OR); assert(GetTraceRestrictCondFlags(item) <= TRCF_OR);
SetDParam(0, _program_cond_type[GetTraceRestrictCondFlags(item)]); SetDParam(0, _program_cond_type[GetTraceRestrictCondFlags(item)]);
SetDParam(1, GetTypeString(GetTraceRestrictType(item))); SetDParam(1, GetTypeString(item));
SetDParam(2, GetDropDownStringByValue(GetCondOpDropDownListSet(properties), GetTraceRestrictCondOp(item))); SetDParam(2, GetDropDownStringByValue(GetCondOpDropDownListSet(properties), GetTraceRestrictCondOp(item)));
} }
@@ -694,6 +744,21 @@ static void DrawInstructionString(const TraceRestrictProgram *prog, TraceRestric
break; break;
} }
case TRVT_WEIGHT:
instruction_string = STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_WEIGHT;
DrawInstructionStringConditionalIntegerCommon(item, properties);
break;
case TRVT_POWER:
instruction_string = STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_POWER;
DrawInstructionStringConditionalIntegerCommon(item, properties);
break;
case TRVT_FORCE:
instruction_string = STR_TRACE_RESTRICT_CONDITIONAL_COMPARE_FORCE;
DrawInstructionStringConditionalIntegerCommon(item, properties);
break;
default: default:
NOT_REACHED(); NOT_REACHED();
break; break;
@@ -904,7 +969,7 @@ public:
case TR_WIDGET_TYPE_COND: case TR_WIDGET_TYPE_COND:
case TR_WIDGET_TYPE_NONCOND: { case TR_WIDGET_TYPE_NONCOND: {
TraceRestrictItem item = this->GetSelected(); TraceRestrictItem item = this->GetSelected();
TraceRestrictItemType type = GetTraceRestrictType(item); TraceRestrictGuiItemType type = GetItemGuiType(item);
if (type != TRIT_NULL) { if (type != TRIT_NULL) {
this->ShowDropDownListWithValue(GetTypeDropDownListSet(type), type, false, widget, 0, 0, 0); this->ShowDropDownListWithValue(GetTypeDropDownListSet(type), type, false, widget, 0, 0, 0);
@@ -1083,11 +1148,8 @@ public:
case TR_WIDGET_TYPE_COND: case TR_WIDGET_TYPE_COND:
case TR_WIDGET_TYPE_NONCOND: { case TR_WIDGET_TYPE_NONCOND: {
SetTraceRestrictTypeAndNormalise(item, static_cast<TraceRestrictItemType>(value)); SetTraceRestrictTypeAndNormalise(item, static_cast<TraceRestrictItemType>(value & 0xFFFF), value >> 16);
if (GetTraceRestrictType(item) == TRIT_COND_LAST_STATION && GetTraceRestrictAuxField(item) != TROCAF_STATION) {
// if changing type from another order type to last visited station, reset value if not currently a station
SetTraceRestrictValueDefault(item, TRVT_ORDER);
}
TraceRestrictDoCommandP(this->tile, this->track, TRDCT_MODIFY_ITEM, this->selected_instruction - 1, item, STR_TRACE_RESTRICT_ERROR_CAN_T_MODIFY_ITEM); TraceRestrictDoCommandP(this->tile, this->track, TRDCT_MODIFY_ITEM, this->selected_instruction - 1, item, STR_TRACE_RESTRICT_ERROR_CAN_T_MODIFY_ITEM);
break; break;
} }
@@ -1649,7 +1711,7 @@ private:
this->EnableWidget(type_widget); this->EnableWidget(type_widget);
this->GetWidget<NWidgetCore>(type_widget)->widget_data = this->GetWidget<NWidgetCore>(type_widget)->widget_data =
GetTypeString(GetTraceRestrictType(item)); GetTypeString(item);
if (properties.cond_type == TRCOT_BINARY || properties.cond_type == TRCOT_ALL) { if (properties.cond_type == TRCOT_BINARY || properties.cond_type == TRCOT_ALL) {
middle_sel->SetDisplayedPlane(DPM_COMPARATOR); middle_sel->SetDisplayedPlane(DPM_COMPARATOR);