Fix my name.
[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 #ifndef DAYNIGHTRATIO_HEADER
21 #define DAYNIGHTRATIO_HEADER
22
23 inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
24 {
25         float t = time_of_day;
26         if(t < 0)
27                 t += ((int)(-t)/24000)*24000;
28         if(t >= 24000)
29                 t -= ((int)(t)/24000)*24000;
30         if(t > 12000)
31                 t = 24000 - t;
32         float values[][2] = {
33                 {4250+125, 150},
34                 {4500+125, 150},
35                 {4750+125, 250},
36                 {5000+125, 350},
37                 {5250+125, 500},
38                 {5500+125, 675},
39                 {5750+125, 875},
40                 {6000+125, 1000},
41                 {6250+125, 1000},
42         };
43         if(!smooth){
44                 float lastt = values[0][0];
45                 for(u32 i=1; i<sizeof(values)/sizeof(*values); i++){
46                         float t0 = values[i][0];
47                         float switch_t = (t0 + lastt) / 2;
48                         lastt = t0;
49                         if(switch_t <= t)
50                                 continue;
51                         return values[i][1];
52                 }
53                 return 1000;
54         } else {
55                 for(u32 i=0; i<sizeof(values)/sizeof(*values); i++){
56                         if(values[i][0] <= t)
57                                 continue;
58                         if(i == 0)
59                                 return values[i][1];
60                         float td0 = values[i][0] - values[i-1][0];
61                         float f = (t - values[i-1][0]) / td0;
62                         return f * values[i][1] + (1.0 - f) * values[i-1][1];
63                 }
64                 return 1000;
65         }
66 }
67
68 #endif
69