nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
weather.cc
Go to the documentation of this file.
1#include "server/weather.h"
2
3#include <algorithm>
4#include <array>
5#include <chrono>
6#include <cmath>
7#include <cstddef>
8#include <cstdint>
9
10#include "terrain/lands.h"
11
12namespace nanommo {
13
14namespace {
15
16// How often each weather comes, out of the weights' sum, for each land.
17struct Odds {
18 protocol::Weather weather;
19 int weight;
20};
21
22constexpr std::array<Odds, 5> kMeadows = {{{protocol::WEATHER_CLEAR, 7},
23 {protocol::WEATHER_CLOUDY, 4},
24 {protocol::WEATHER_RAIN, 5},
25 {protocol::WEATHER_STORM, 2},
26 {protocol::WEATHER_MIST, 2}}};
27constexpr std::array<Odds, 3> kAshlands = {{{protocol::WEATHER_CLEAR, 7},
28 {protocol::WEATHER_ASH, 6},
29 {protocol::WEATHER_FIRE, 7}}};
30constexpr std::array<Odds, 2> kCrypt = {
31 {{protocol::WEATHER_CLEAR, 1}, {protocol::WEATHER_MIST, 1}}};
32
33// Out in the open, mist settles in the night and lifts in the morning: it
34// only comes in a slot that starts between these two times of day. The
35// Crypt's night never ends, so its mist comes whenever it likes.
36constexpr float kMistFrom = 0.1f;
37constexpr float kMistTo = 0.35f;
38
39// The same numbers from the same seed, wherever they're worked out.
40uint64_t Mixed(uint64_t z) {
41 z += 0x9E3779B97F4A7C15ull;
42 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
43 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
44 return z ^ (z >> 31);
45}
46
47// From 0 to 1, the nth number from the seed.
48float Fraction(uint64_t seed, int n) {
49 return static_cast<float>(Mixed(seed + static_cast<uint64_t>(n)) >> 40) /
50 static_cast<float>(1ull << 24);
51}
52
53template <std::size_t N>
54protocol::Weather Pick(const std::array<Odds, N>& odds, float roll) {
55 int total = 0;
56 for (const Odds& o : odds) {
57 total += o.weight;
58 }
59 float left = roll * static_cast<float>(total);
60 for (const Odds& o : odds) {
61 left -= static_cast<float>(o.weight);
62 if (left < 0.0f) {
63 return o.weather;
64 }
65 }
66 return odds.back().weather;
67}
68
69protocol::Weather PickFor(int land, float roll) {
70 switch (land) {
71 case 1:
72 return Pick(kAshlands, roll);
73 case 2:
74 return Pick(kCrypt, roll);
75 default:
76 return Pick(kMeadows, roll);
77 }
78}
79
80float DayAt(int64_t unix_ms) {
81 const int64_t into = ((unix_ms % kDayMs) + kDayMs) % kDayMs;
82 return static_cast<float>(into) / static_cast<float>(kDayMs);
83}
84
85} // namespace
86
87protocol::Sky SkyAt(int land, int64_t unix_ms) {
88 protocol::Sky sky;
89 sky.set_day(DayAt(unix_ms));
90 const int64_t slot =
91 unix_ms >= 0 ? unix_ms / kSlotMs : (unix_ms - kSlotMs + 1) / kSlotMs;
92 const int64_t start = slot * kSlotMs;
93 const uint64_t seed =
94 Mixed(static_cast<uint64_t>(slot) * 7919u +
95 kLands[static_cast<std::size_t>(land) % kLands.size()].seed);
96 protocol::Weather weather = PickFor(land, Fraction(seed, 0));
97 if (weather == protocol::WEATHER_MIST && !kLands[land].dungeon) {
98 const float day = DayAt(start);
99 if (day < kMistFrom || day > kMistTo) {
100 weather = protocol::WEATHER_CLOUDY;
101 }
102 }
103 const int64_t lasts =
105 static_cast<int64_t>(Fraction(seed, 1) *
106 static_cast<float>(kSlotMs - kShortestSpellMs));
107 const float peak = 0.55f + 0.45f * Fraction(seed, 2);
108 const int64_t into = unix_ms - start;
109 // Up from nothing as it comes, down to nothing as it goes.
110 const float ramp =
111 std::clamp(static_cast<float>(std::min(into, lasts - into)) /
112 static_cast<float>(kRampMs),
113 0.0f, 1.0f);
114 if (into < lasts && weather != protocol::WEATHER_CLEAR) {
115 sky.set_weather(weather);
116 sky.set_strength(peak * ramp);
117 }
118 // A wind of its own each slot, which a storm whips up.
119 const float turn = 6.2831853f * Fraction(seed, 3);
120 const float storm = sky.weather() == protocol::WEATHER_STORM ||
121 sky.weather() == protocol::WEATHER_FIRE
122 ? sky.strength()
123 : 0.0f;
124 const float speed = 1.0f + 2.0f * Fraction(seed, 4) + 4.0f * storm;
125 sky.set_wind_x(speed * std::cos(turn));
126 sky.set_wind_z(speed * std::sin(turn));
127 return sky;
128}
129
130int64_t UnixMs() {
131 return std::chrono::duration_cast<std::chrono::milliseconds>(
132 std::chrono::system_clock::now().time_since_epoch())
133 .count();
134}
135
136} // namespace nanommo
Definition chat.cc:9
constexpr int64_t kShortestSpellMs
Definition weather.h:17
int64_t UnixMs()
Now, by the wall clock, in milliseconds since 1970.
Definition weather.cc:130
constexpr int64_t kDayMs
From one midnight to the next, in milliseconds of the wall clock.
Definition weather.h:11
constexpr Look kAshlands
Definition atmosphere.h:106
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
protocol::Sky SkyAt(int land, int64_t unix_ms)
Definition weather.cc:87
constexpr Look kCrypt
Definition atmosphere.h:134
constexpr int64_t kSlotMs
Definition weather.h:16
constexpr int64_t kRampMs
How long a spell takes to come, and to go, in milliseconds.
Definition weather.h:20
constexpr Look kMeadows
Definition atmosphere.h:80