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