Fix bone-attached entities (#10015)
[oweals/minetest.git] / src / daynightratio.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
23 {
24         float t = time_of_day;
25         if (t < 0.0f)
26                 t += ((int)(-t) / 24000) * 24000.0f;
27         if (t >= 24000.0f)
28                 t -= ((int)(t) / 24000) * 24000.0f;
29         if (t > 12000.0f)
30                 t = 24000.0f - t;
31
32         const float values[9][2] = {
33                 {4250.0f + 125.0f, 175.0f},
34                 {4500.0f + 125.0f, 175.0f},
35                 {4750.0f + 125.0f, 250.0f},
36                 {5000.0f + 125.0f, 350.0f},
37                 {5250.0f + 125.0f, 500.0f},
38                 {5500.0f + 125.0f, 675.0f},
39                 {5750.0f + 125.0f, 875.0f},
40                 {6000.0f + 125.0f, 1000.0f},
41                 {6250.0f + 125.0f, 1000.0f},
42         };
43
44         if (!smooth) {
45                 float lastt = values[0][0];
46                 for (u32 i = 1; i < 9; i++) {
47                         float t0 = values[i][0];
48                         float switch_t = (t0 + lastt) / 2.0f;
49                         lastt = t0;
50                         if (switch_t <= t)
51                                 continue;
52
53                         return values[i][1];
54                 }
55                 return 1000;
56         }
57
58         if (t <= 4625.0f) // 4500 + 125
59                 return values[0][1];
60         else if (t >= 6125.0f) // 6000 + 125
61                 return 1000;
62
63         for (u32 i = 0; i < 9; i++) {
64                 if (values[i][0] <= t)
65                         continue;
66
67                 float td0 = values[i][0] - values[i - 1][0];
68                 float f = (t - values[i - 1][0]) / td0;
69                 return f * values[i][1] + (1.0f - f) * values[i - 1][1];
70         }
71         return 1000;
72 }