ed0c179e8fc634d485380fe8983d466a6f22dfbd
[oweals/minetest.git] / src / utility.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "utility.h"
25 #include "irrlichtwrapper.h"
26 #include "gettime.h"
27
28 TimeTaker::TimeTaker(const char *name, u32 *result)
29 {
30         m_name = name;
31         m_result = result;
32         m_running = true;
33         m_time1 = getTimeMs();
34 }
35
36 u32 TimeTaker::stop(bool quiet)
37 {
38         if(m_running)
39         {
40                 u32 time2 = getTimeMs();
41                 u32 dtime = time2 - m_time1;
42                 if(m_result != NULL)
43                 {
44                         (*m_result) += dtime;
45                 }
46                 else
47                 {
48                         if(quiet == false)
49                                 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
50                 }
51                 m_running = false;
52                 return dtime;
53         }
54         return 0;
55 }
56
57 u32 TimeTaker::getTime()
58 {
59         u32 time2 = getTimeMs();
60         u32 dtime = time2 - m_time1;
61         return dtime;
62 }
63
64 const v3s16 g_26dirs[26] =
65 {
66         // +right, +top, +back
67         v3s16( 0, 0, 1), // back
68         v3s16( 0, 1, 0), // top
69         v3s16( 1, 0, 0), // right
70         v3s16( 0, 0,-1), // front
71         v3s16( 0,-1, 0), // bottom
72         v3s16(-1, 0, 0), // left
73         // 6
74         v3s16(-1, 1, 0), // top left
75         v3s16( 1, 1, 0), // top right
76         v3s16( 0, 1, 1), // top back
77         v3s16( 0, 1,-1), // top front
78         v3s16(-1, 0, 1), // back left
79         v3s16( 1, 0, 1), // back right
80         v3s16(-1, 0,-1), // front left
81         v3s16( 1, 0,-1), // front right
82         v3s16(-1,-1, 0), // bottom left
83         v3s16( 1,-1, 0), // bottom right
84         v3s16( 0,-1, 1), // bottom back
85         v3s16( 0,-1,-1), // bottom front
86         // 18
87         v3s16(-1, 1, 1), // top back-left
88         v3s16( 1, 1, 1), // top back-right
89         v3s16(-1, 1,-1), // top front-left
90         v3s16( 1, 1,-1), // top front-right
91         v3s16(-1,-1, 1), // bottom back-left
92         v3s16( 1,-1, 1), // bottom back-right
93         v3s16(-1,-1,-1), // bottom front-left
94         v3s16( 1,-1,-1), // bottom front-right
95         // 26
96 };
97
98 const v3s16 g_27dirs[27] =
99 {
100         // +right, +top, +back
101         v3s16( 0, 0, 1), // back
102         v3s16( 0, 1, 0), // top
103         v3s16( 1, 0, 0), // right
104         v3s16( 0, 0,-1), // front
105         v3s16( 0,-1, 0), // bottom
106         v3s16(-1, 0, 0), // left
107         // 6
108         v3s16(-1, 1, 0), // top left
109         v3s16( 1, 1, 0), // top right
110         v3s16( 0, 1, 1), // top back
111         v3s16( 0, 1,-1), // top front
112         v3s16(-1, 0, 1), // back left
113         v3s16( 1, 0, 1), // back right
114         v3s16(-1, 0,-1), // front left
115         v3s16( 1, 0,-1), // front right
116         v3s16(-1,-1, 0), // bottom left
117         v3s16( 1,-1, 0), // bottom right
118         v3s16( 0,-1, 1), // bottom back
119         v3s16( 0,-1,-1), // bottom front
120         // 18
121         v3s16(-1, 1, 1), // top back-left
122         v3s16( 1, 1, 1), // top back-right
123         v3s16(-1, 1,-1), // top front-left
124         v3s16( 1, 1,-1), // top front-right
125         v3s16(-1,-1, 1), // bottom back-left
126         v3s16( 1,-1, 1), // bottom back-right
127         v3s16(-1,-1,-1), // bottom front-left
128         v3s16( 1,-1,-1), // bottom front-right
129         // 26
130         v3s16(0,0,0),
131 };
132
133 static unsigned long next = 1;
134
135 /* RAND_MAX assumed to be 32767 */
136 int myrand(void)
137 {
138    next = next * 1103515245 + 12345;
139    return((unsigned)(next/65536) % 32768);
140 }
141
142 void mysrand(unsigned seed)
143 {
144    next = seed;
145 }
146
147 /*
148         blockpos: position of block in block coordinates
149         camera_pos: position of camera in nodes
150         camera_dir: an unit vector pointing to camera direction
151         range: viewing range
152 */
153 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
154                 f32 *distance_ptr)
155 {
156         v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
157         
158         // Block center position
159         v3f blockpos(
160                         ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
161                         ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
162                         ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
163         );
164
165         // Block position relative to camera
166         v3f blockpos_relative = blockpos - camera_pos;
167
168         // Distance in camera direction (+=front, -=back)
169         f32 dforward = blockpos_relative.dotProduct(camera_dir);
170
171         // Total distance
172         f32 d = blockpos_relative.getLength();
173
174         if(distance_ptr)
175                 *distance_ptr = d;
176         
177         // If block is far away, it's not in sight
178         if(d > range * BS)
179                 return false;
180
181         // Maximum radius of a block
182         f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
183         
184         // If block is (nearly) touching the camera, don't
185         // bother validating further (that is, render it anyway)
186         if(d > block_max_radius * 1.5)
187         {
188                 // Cosine of the angle between the camera direction
189                 // and the block direction (camera_dir is an unit vector)
190                 f32 cosangle = dforward / d;
191                 
192                 // Compensate for the size of the block
193                 // (as the block has to be shown even if it's a bit off FOV)
194                 // This is an estimate.
195                 cosangle += block_max_radius / dforward;
196
197                 // If block is not in the field of view, skip it
198                 //if(cosangle < cos(FOV_ANGLE/2))
199                 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))
200                         return false;
201         }
202
203         return true;
204 }
205