C++ modernize: Pragma once (#6264)
[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)
26                 t += ((int)(-t)/24000)*24000;
27         if(t >= 24000)
28                 t -= ((int)(t)/24000)*24000;
29         if(t > 12000)
30                 t = 24000 - t;
31         float values[][2] = {
32                 {4250+125, 150},
33                 {4500+125, 150},
34                 {4750+125, 250},
35                 {5000+125, 350},
36                 {5250+125, 500},
37                 {5500+125, 675},
38                 {5750+125, 875},
39                 {6000+125, 1000},
40                 {6250+125, 1000},
41         };
42         if(!smooth){
43                 float lastt = values[0][0];
44                 for(u32 i=1; i<sizeof(values)/sizeof(*values); i++){
45                         float t0 = values[i][0];
46                         float switch_t = (t0 + lastt) / 2;
47                         lastt = t0;
48                         if(switch_t <= t)
49                                 continue;
50                         return values[i][1];
51                 }
52                 return 1000;
53         } else {
54                 for(u32 i=0; i<sizeof(values)/sizeof(*values); i++){
55                         if(values[i][0] <= t)
56                                 continue;
57                         if(i == 0)
58                                 return values[i][1];
59                         float td0 = values[i][0] - values[i-1][0];
60                         float f = (t - values[i-1][0]) / td0;
61                         return f * values[i][1] + (1.0 - f) * values[i-1][1];
62                 }
63                 return 1000;
64         }
65 }