1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::fmt;

use serde::{Deserialize, Serialize};
use vek::{Lerp, Vec2, Vec3};

use crate::{grid::Grid, terrain::TerrainChunkSize, vol::RectVolSize};

/// Weather::default is Clear, 0 degrees C and no wind
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct Weather {
    /// Clouds currently in the area between 0 and 1
    pub cloud: f32,
    /// Rain per time, between 0 and 1
    pub rain: f32,
    /// Wind velocity in block / second
    pub wind: Vec2<f32>,
}

impl Weather {
    pub fn new(cloud: f32, rain: f32, wind: Vec2<f32>) -> Self { Self { cloud, rain, wind } }

    pub fn get_kind(&self) -> WeatherKind {
        // Over 24.5 m/s wind is a storm
        if self.wind.magnitude_squared() >= 24.5f32.powi(2) {
            WeatherKind::Storm
        } else if (0.1..=1.0).contains(&self.rain) {
            WeatherKind::Rain
        } else if (0.2..=1.0).contains(&self.cloud) {
            WeatherKind::Cloudy
        } else {
            WeatherKind::Clear
        }
    }

    pub fn lerp_unclamped(&self, to: &Self, t: f32) -> Self {
        Self {
            cloud: f32::lerp_unclamped(self.cloud, to.cloud, t),
            rain: f32::lerp_unclamped(self.rain, to.rain, t),
            wind: Vec2::<f32>::lerp_unclamped(self.wind, to.wind, t),
        }
    }

    // Get the rain velocity for this weather
    pub fn rain_vel(&self) -> Vec3<f32> {
        const FALL_RATE: f32 = 30.0;
        self.wind.with_z(-FALL_RATE)
    }

    // Get the wind velocity for this weather
    pub fn wind_vel(&self) -> Vec2<f32> { self.wind }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum WeatherKind {
    Clear,
    Cloudy,
    Rain,
    Storm,
}

impl fmt::Display for WeatherKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WeatherKind::Clear => write!(f, "Clear"),
            WeatherKind::Cloudy => write!(f, "Cloudy"),
            WeatherKind::Rain => write!(f, "Rain"),
            WeatherKind::Storm => write!(f, "Storm"),
        }
    }
}

// How many chunks wide a weather cell is.
// So one weather cell has (CHUNKS_PER_CELL * CHUNKS_PER_CELL) chunks.
pub const CHUNKS_PER_CELL: u32 = 16;

pub const CELL_SIZE: u32 = CHUNKS_PER_CELL * TerrainChunkSize::RECT_SIZE.x;

#[derive(Debug, Clone)]
pub struct WeatherGrid {
    weather: Grid<Weather>,
}

/// Weather that's compressed in order to send it to the client.
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
pub struct CompressedWeather {
    cloud: u8,
    rain: u8,
}

impl CompressedWeather {
    pub fn lerp_unclamped(&self, to: &CompressedWeather, t: f32) -> Weather {
        Weather {
            cloud: f32::lerp_unclamped(self.cloud as f32, to.cloud as f32, t) / 255.0,
            rain: f32::lerp_unclamped(self.rain as f32, to.rain as f32, t) / 255.0,
            wind: Vec2::zero(),
        }
    }
}

impl From<Weather> for CompressedWeather {
    fn from(weather: Weather) -> Self {
        Self {
            cloud: (weather.cloud * 255.0).round() as u8,
            rain: (weather.rain * 255.0).round() as u8,
        }
    }
}

impl From<CompressedWeather> for Weather {
    fn from(weather: CompressedWeather) -> Self {
        Self {
            cloud: weather.cloud as f32 / 255.0,
            rain: weather.rain as f32 / 255.0,
            wind: Vec2::zero(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SharedWeatherGrid {
    weather: Grid<CompressedWeather>,
}

impl From<&WeatherGrid> for SharedWeatherGrid {
    fn from(value: &WeatherGrid) -> Self {
        Self {
            weather: Grid::from_raw(
                value.weather.size(),
                value
                    .weather
                    .raw()
                    .iter()
                    .copied()
                    .map(CompressedWeather::from)
                    .collect::<Vec<_>>(),
            ),
        }
    }
}

impl From<&SharedWeatherGrid> for WeatherGrid {
    fn from(value: &SharedWeatherGrid) -> Self {
        Self {
            weather: Grid::from_raw(
                value.weather.size(),
                value
                    .weather
                    .raw()
                    .iter()
                    .copied()
                    .map(Weather::from)
                    .collect::<Vec<_>>(),
            ),
        }
    }
}

impl SharedWeatherGrid {
    pub fn new(size: Vec2<u32>) -> Self {
        size.map(|e| debug_assert!(i32::try_from(e).is_ok()));
        Self {
            weather: Grid::new(size.as_(), CompressedWeather::default()),
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = (Vec2<i32>, &CompressedWeather)> {
        self.weather.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (Vec2<i32>, &mut CompressedWeather)> {
        self.weather.iter_mut()
    }

    pub fn size(&self) -> Vec2<u32> { self.weather.size().as_() }
}

/// Transforms a world position to cell coordinates. Where (0.0, 0.0) in cell
/// coordinates is the center of the weather cell located at (0, 0) in the grid.
fn to_cell_pos(wpos: Vec2<f32>) -> Vec2<f32> { wpos / CELL_SIZE as f32 - 0.5 }

// TODO: Move consts from world to common to avoid duplication
const LOCALITY: [Vec2<i32>; 9] = [
    Vec2::new(0, 0),
    Vec2::new(0, 1),
    Vec2::new(1, 0),
    Vec2::new(0, -1),
    Vec2::new(-1, 0),
    Vec2::new(1, 1),
    Vec2::new(1, -1),
    Vec2::new(-1, 1),
    Vec2::new(-1, -1),
];

impl WeatherGrid {
    pub fn new(size: Vec2<u32>) -> Self {
        size.map(|e| debug_assert!(i32::try_from(e).is_ok()));
        Self {
            weather: Grid::new(size.as_(), Weather::default()),
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = (Vec2<i32>, &Weather)> { self.weather.iter() }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (Vec2<i32>, &mut Weather)> {
        self.weather.iter_mut()
    }

    pub fn size(&self) -> Vec2<u32> { self.weather.size().as_() }

    pub fn get(&self, cell_pos: Vec2<u32>) -> Weather {
        self.weather
            .get(cell_pos.as_())
            .copied()
            .unwrap_or_default()
    }

    /// Get the weather at a given world position by doing bilinear
    /// interpolation between four cells.
    pub fn get_interpolated(&self, wpos: Vec2<f32>) -> Weather {
        let cell_pos = to_cell_pos(wpos);
        let rpos = cell_pos.map(|e| e.fract() + (1.0 - e.signum()) / 2.0);
        let cell_pos = cell_pos.map(|e| e.floor());

        let cpos = cell_pos.as_::<i32>();
        Weather::lerp_unclamped(
            &Weather::lerp_unclamped(
                self.weather.get(cpos).unwrap_or(&Weather::default()),
                self.weather
                    .get(cpos + Vec2::unit_x())
                    .unwrap_or(&Weather::default()),
                rpos.x,
            ),
            &Weather::lerp_unclamped(
                self.weather
                    .get(cpos + Vec2::unit_y())
                    .unwrap_or(&Weather::default()),
                self.weather
                    .get(cpos + Vec2::one())
                    .unwrap_or(&Weather::default()),
                rpos.x,
            ),
            rpos.y,
        )
    }

    /// Get the max weather near a position
    pub fn get_max_near(&self, wpos: Vec2<f32>) -> Weather {
        let cell_pos: Vec2<i32> = to_cell_pos(wpos).as_();
        LOCALITY
            .iter()
            .map(|l| {
                self.weather
                    .get(cell_pos + l)
                    .cloned()
                    .unwrap_or_default()
            })
            .reduce(|a, b| Weather {
                cloud: a.cloud.max(b.cloud),
                rain: a.rain.max(b.rain),
                wind: a.wind.map2(b.wind, |a, b| a.max(b)),
            })
            // There will always be 9 elements in locality
            .unwrap()
    }
}