Line data Source code
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 1460846 : inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
24 : {
25 1460846 : float t = time_of_day;
26 1460846 : if(t < 0)
27 0 : t += ((int)(-t)/24000)*24000;
28 1460846 : if(t >= 24000)
29 0 : t -= ((int)(t)/24000)*24000;
30 1460846 : if(t > 12000)
31 0 : 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 1460846 : };
43 1460846 : if(!smooth){
44 0 : float lastt = values[0][0];
45 0 : for(u32 i=1; i<sizeof(values)/sizeof(*values); i++){
46 0 : float t0 = values[i][0];
47 0 : float switch_t = (t0 + lastt) / 2;
48 0 : lastt = t0;
49 0 : if(switch_t <= t)
50 0 : continue;
51 0 : return values[i][1];
52 : }
53 0 : return 1000;
54 : } else {
55 14608460 : for(u32 i=0; i<sizeof(values)/sizeof(*values); i++){
56 13147614 : if(values[i][0] <= t)
57 13147614 : continue;
58 0 : if(i == 0)
59 0 : return values[i][1];
60 0 : float td0 = values[i][0] - values[i-1][0];
61 0 : float f = (t - values[i-1][0]) / td0;
62 0 : return f * values[i][1] + (1.0 - f) * values[i-1][1];
63 : }
64 1460846 : return 1000;
65 : }
66 : }
67 :
68 : #endif
69 :
|