]> git.lizzy.rs Git - dragonfireclient.git/blob - src/utility.cpp
hopefully fixed a bit more
[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 "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_6dirs[6] =
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 };
74
75 const v3s16 g_26dirs[26] =
76 {
77         // +right, +top, +back
78         v3s16( 0, 0, 1), // back
79         v3s16( 0, 1, 0), // top
80         v3s16( 1, 0, 0), // right
81         v3s16( 0, 0,-1), // front
82         v3s16( 0,-1, 0), // bottom
83         v3s16(-1, 0, 0), // left
84         // 6
85         v3s16(-1, 1, 0), // top left
86         v3s16( 1, 1, 0), // top right
87         v3s16( 0, 1, 1), // top back
88         v3s16( 0, 1,-1), // top front
89         v3s16(-1, 0, 1), // back left
90         v3s16( 1, 0, 1), // back right
91         v3s16(-1, 0,-1), // front left
92         v3s16( 1, 0,-1), // front right
93         v3s16(-1,-1, 0), // bottom left
94         v3s16( 1,-1, 0), // bottom right
95         v3s16( 0,-1, 1), // bottom back
96         v3s16( 0,-1,-1), // bottom front
97         // 18
98         v3s16(-1, 1, 1), // top back-left
99         v3s16( 1, 1, 1), // top back-right
100         v3s16(-1, 1,-1), // top front-left
101         v3s16( 1, 1,-1), // top front-right
102         v3s16(-1,-1, 1), // bottom back-left
103         v3s16( 1,-1, 1), // bottom back-right
104         v3s16(-1,-1,-1), // bottom front-left
105         v3s16( 1,-1,-1), // bottom front-right
106         // 26
107 };
108
109 const v3s16 g_27dirs[27] =
110 {
111         // +right, +top, +back
112         v3s16( 0, 0, 1), // back
113         v3s16( 0, 1, 0), // top
114         v3s16( 1, 0, 0), // right
115         v3s16( 0, 0,-1), // front
116         v3s16( 0,-1, 0), // bottom
117         v3s16(-1, 0, 0), // left
118         // 6
119         v3s16(-1, 1, 0), // top left
120         v3s16( 1, 1, 0), // top right
121         v3s16( 0, 1, 1), // top back
122         v3s16( 0, 1,-1), // top front
123         v3s16(-1, 0, 1), // back left
124         v3s16( 1, 0, 1), // back right
125         v3s16(-1, 0,-1), // front left
126         v3s16( 1, 0,-1), // front right
127         v3s16(-1,-1, 0), // bottom left
128         v3s16( 1,-1, 0), // bottom right
129         v3s16( 0,-1, 1), // bottom back
130         v3s16( 0,-1,-1), // bottom front
131         // 18
132         v3s16(-1, 1, 1), // top back-left
133         v3s16( 1, 1, 1), // top back-right
134         v3s16(-1, 1,-1), // top front-left
135         v3s16( 1, 1,-1), // top front-right
136         v3s16(-1,-1, 1), // bottom back-left
137         v3s16( 1,-1, 1), // bottom back-right
138         v3s16(-1,-1,-1), // bottom front-left
139         v3s16( 1,-1,-1), // bottom front-right
140         // 26
141         v3s16(0,0,0),
142 };
143
144 static unsigned long next = 1;
145
146 /* RAND_MAX assumed to be 32767 */
147 int myrand(void)
148 {
149    next = next * 1103515245 + 12345;
150    return((unsigned)(next/65536) % 32768);
151 }
152
153 void mysrand(unsigned seed)
154 {
155    next = seed;
156 }
157
158 /*
159         blockpos: position of block in block coordinates
160         camera_pos: position of camera in nodes
161         camera_dir: an unit vector pointing to camera direction
162         range: viewing range
163 */
164 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
165                 f32 *distance_ptr)
166 {
167         v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
168         
169         // Block center position
170         v3f blockpos(
171                         ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
172                         ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
173                         ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
174         );
175
176         // Block position relative to camera
177         v3f blockpos_relative = blockpos - camera_pos;
178
179         // Distance in camera direction (+=front, -=back)
180         f32 dforward = blockpos_relative.dotProduct(camera_dir);
181
182         // Total distance
183         f32 d = blockpos_relative.getLength();
184
185         if(distance_ptr)
186                 *distance_ptr = d;
187         
188         // If block is far away, it's not in sight
189         if(d > range * BS)
190                 return false;
191
192         // Maximum radius of a block
193         f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
194         
195         // If block is (nearly) touching the camera, don't
196         // bother validating further (that is, render it anyway)
197         if(d > block_max_radius * 1.5)
198         {
199                 // Cosine of the angle between the camera direction
200                 // and the block direction (camera_dir is an unit vector)
201                 f32 cosangle = dforward / d;
202                 
203                 // Compensate for the size of the block
204                 // (as the block has to be shown even if it's a bit off FOV)
205                 // This is an estimate.
206                 cosangle += block_max_radius / dforward;
207
208                 // If block is not in the field of view, skip it
209                 //if(cosangle < cos(FOV_ANGLE/2))
210                 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))
211                         return false;
212         }
213
214         return true;
215 }
216