Rise
The Vieneo Province
Loading...
Searching...
No Matches
WeatherOverrides.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <charconv>
4#include <cmath>
5#include <cwctype>
6#include <string>
7#include <string_view>
8
10{
11 // Consume the entire suffix so a typo cannot silently set different weather.
12 inline bool ParseValue(const wchar_t* text, float minimum, float maximum, float& value)
13 {
14 std::wstring_view suffix(text);
15 while (!suffix.empty() && std::iswspace(suffix.front())) suffix.remove_prefix(1);
16 while (!suffix.empty() && std::iswspace(suffix.back())) suffix.remove_suffix(1);
17 if (suffix.empty()) return false;
18 std::string number;
19 for (const auto c : suffix)
20 {
21 if (c > 127) return false;
22 number.push_back(static_cast<char>(c));
23 }
24 const char* begin = number.data();
25 const char* end = begin + number.size();
26 if (*begin == '+')
27 {
28 ++begin;
29 if (begin != end && *begin == '-') return false;
30 }
31 float parsed;
32 // Chat command decimals always use '.', regardless of the UI language.
33 const auto result = std::from_chars(begin, end, parsed);
34 if (result.ec != std::errc{} || result.ptr != end || !std::isfinite(parsed)
35 || !(parsed == -1.f || (parsed >= minimum && parsed <= maximum))) return false;
36 value = parsed;
37 return true;
38 }
39 inline float Target(float overrideValue, float weatherTarget)
40 {
41 return overrideValue == -1.f ? weatherTarget : overrideValue;
42 }
43 inline void Apply(float value, float weatherTarget, float& overrideValue, float& smoothValue)
44 {
45 overrideValue = value;
46 smoothValue = Target(value, weatherTarget);
47 }
48}
bool ParseValue(const wchar_t *text, float minimum, float maximum, float &value)
float Target(float overrideValue, float weatherTarget)
void Apply(float value, float weatherTarget, float &overrideValue, float &smoothValue)