Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
daynightratio.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
8{
9 float t = time_of_day;
10 if (t < 0.0f)
11 t += ((int)(-t) / 24000) * 24000.0f;
12 if (t >= 24000.0f)
13 t -= ((int)(t) / 24000) * 24000.0f;
14 if (t > 12000.0f)
15 t = 24000.0f - t;
16
17 const float values[9][2] = {
18 {4250.0f + 125.0f, 175.0f},
19 {4500.0f + 125.0f, 175.0f},
20 {4750.0f + 125.0f, 250.0f},
21 {5000.0f + 125.0f, 350.0f},
22 {5250.0f + 125.0f, 500.0f},
23 {5500.0f + 125.0f, 675.0f},
24 {5750.0f + 125.0f, 875.0f},
25 {6000.0f + 125.0f, 1000.0f},
26 {6250.0f + 125.0f, 1000.0f},
27 };
28
29 if (!smooth) {
30 float lastt = values[0][0];
31 for (u32 i = 1; i < 9; i++) {
32 float t0 = values[i][0];
33 float switch_t = (t0 + lastt) / 2.0f;
34 lastt = t0;
35 if (switch_t <= t)
36 continue;
37
38 return values[i][1];
39 }
40 return 1000;
41 }
42
43 if (t <= 4625.0f) // 4500 + 125
44 return values[0][1];
45 else if (t >= 6125.0f) // 6000 + 125
46 return 1000;
47
48 for (u32 i = 0; i < 9; i++) {
49 if (values[i][0] <= t)
50 continue;
51
52 float td0 = values[i][0] - values[i - 1][0];
53 float f = (t - values[i - 1][0]) / td0;
54 return f * values[i][1] + (1.0f - f) * values[i - 1][1];
55 }
56 return 1000;
57}
u32 time_to_daynight_ratio(float time_of_day, bool smooth)
Definition daynightratio.h:7