MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / MakeValueValid

Method MakeValueValid

src/settings.cpp:502–549  ·  view source on GitHub ↗

* Make the value valid given the limitations of this setting. * * In the case of int settings this is ensuring the value is between the minimum and * maximum value, with a special case for 0 if SettingFlag::GuiZeroIsSpecial is set. * This is generally done by clamping the value so it is within the allowed value range. * However, for SettingFlag::GuiDropdown the default is used when the value

Source from the content-addressed store, hash-verified

500 * @param val The value to make valid.
501 */
502void IntSettingDesc::MakeValueValid(int32_t &val) const
503{
504 auto [min_val, max_val] = this->GetRange();
505 /* We need to take special care of the uint32_t type as we receive from the function
506 * a signed integer. While here also bail out on 64-bit settings as those are not
507 * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
508 * 32-bit variable
509 * TODO: Support 64-bit settings/variables; requires 64 bit over command protocol! */
510 switch (GetVarMemType(this->save.conv)) {
511 case SLE_VAR_NULL: return;
512 case SLE_VAR_BL:
513 case SLE_VAR_I8:
514 case SLE_VAR_U8:
515 case SLE_VAR_I16:
516 case SLE_VAR_U16:
517 case SLE_VAR_I32: {
518 /* Override the minimum value. No value below this->min, except special value 0 */
519 if (!this->flags.Test(SettingFlag::GuiZeroIsSpecial) || val != 0) {
520 if (!this->flags.Test(SettingFlag::GuiDropdown)) {
521 /* Clamp value-type setting to its valid range */
522 val = Clamp(val, min_val, max_val);
523 } else if (val < min_val || val > static_cast<int32_t>(max_val)) {
524 /* Reset invalid discrete setting (where different values change gameplay) to its default value */
525 val = this->GetDefaultValue();
526 }
527 }
528 break;
529 }
530 case SLE_VAR_U32: {
531 /* Override the minimum value. No value below this->min, except special value 0 */
532 uint32_t uval = static_cast<uint32_t>(val);
533 if (!this->flags.Test(SettingFlag::GuiZeroIsSpecial) || uval != 0) {
534 if (!this->flags.Test(SettingFlag::GuiDropdown)) {
535 /* Clamp value-type setting to its valid range */
536 uval = ClampU(uval, min_val, max_val);
537 } else if (uval < static_cast<uint32_t>(min_val) || uval > max_val) {
538 /* Reset invalid discrete setting to its default value */
539 uval = static_cast<uint32_t>(this->GetDefaultValue());
540 }
541 }
542 val = static_cast<int32_t>(uval);
543 return;
544 }
545 case SLE_VAR_I64:
546 case SLE_VAR_U64:
547 default: NOT_REACHED();
548 }
549}
550
551/**
552 * Set the value of a setting.

Callers 3

ChangeValueMethod · 0.95
ParseValueMethod · 0.80

Calls 10

GetRangeMethod · 0.95
GetDefaultValueMethod · 0.95
GetVarMemTypeFunction · 0.85
ClampFunction · 0.85
ClampUFunction · 0.85
NOT_REACHEDFunction · 0.85
StrMakeValidInPlaceFunction · 0.85
TestMethod · 0.80
sizeMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected