]> git.lizzy.rs Git - dragonfireclient.git/blob - src/utility.cpp
Header file tweaking; mainly for speed
[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 #include "sha1.h"
27 #include "base64.h"
28
29 TimeTaker::TimeTaker(const char *name, u32 *result)
30 {
31         m_name = name;
32         m_result = result;
33         m_running = true;
34         m_time1 = getTimeMs();
35 }
36
37 u32 TimeTaker::stop(bool quiet)
38 {
39         if(m_running)
40         {
41                 u32 time2 = getTimeMs();
42                 u32 dtime = time2 - m_time1;
43                 if(m_result != NULL)
44                 {
45                         (*m_result) += dtime;
46                 }
47                 else
48                 {
49                         if(quiet == false)
50                                 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
51                 }
52                 m_running = false;
53                 return dtime;
54         }
55         return 0;
56 }
57
58 u32 TimeTaker::getTime()
59 {
60         u32 time2 = getTimeMs();
61         u32 dtime = time2 - m_time1;
62         return dtime;
63 }
64
65 const v3s16 g_6dirs[6] =
66 {
67         // +right, +top, +back
68         v3s16( 0, 0, 1), // back
69         v3s16( 0, 1, 0), // top
70         v3s16( 1, 0, 0), // right
71         v3s16( 0, 0,-1), // front
72         v3s16( 0,-1, 0), // bottom
73         v3s16(-1, 0, 0) // left
74 };
75
76 const v3s16 g_26dirs[26] =
77 {
78         // +right, +top, +back
79         v3s16( 0, 0, 1), // back
80         v3s16( 0, 1, 0), // top
81         v3s16( 1, 0, 0), // right
82         v3s16( 0, 0,-1), // front
83         v3s16( 0,-1, 0), // bottom
84         v3s16(-1, 0, 0), // left
85         // 6
86         v3s16(-1, 1, 0), // top left
87         v3s16( 1, 1, 0), // top right
88         v3s16( 0, 1, 1), // top back
89         v3s16( 0, 1,-1), // top front
90         v3s16(-1, 0, 1), // back left
91         v3s16( 1, 0, 1), // back right
92         v3s16(-1, 0,-1), // front left
93         v3s16( 1, 0,-1), // front right
94         v3s16(-1,-1, 0), // bottom left
95         v3s16( 1,-1, 0), // bottom right
96         v3s16( 0,-1, 1), // bottom back
97         v3s16( 0,-1,-1), // bottom front
98         // 18
99         v3s16(-1, 1, 1), // top back-left
100         v3s16( 1, 1, 1), // top back-right
101         v3s16(-1, 1,-1), // top front-left
102         v3s16( 1, 1,-1), // top front-right
103         v3s16(-1,-1, 1), // bottom back-left
104         v3s16( 1,-1, 1), // bottom back-right
105         v3s16(-1,-1,-1), // bottom front-left
106         v3s16( 1,-1,-1), // bottom front-right
107         // 26
108 };
109
110 const v3s16 g_27dirs[27] =
111 {
112         // +right, +top, +back
113         v3s16( 0, 0, 1), // back
114         v3s16( 0, 1, 0), // top
115         v3s16( 1, 0, 0), // right
116         v3s16( 0, 0,-1), // front
117         v3s16( 0,-1, 0), // bottom
118         v3s16(-1, 0, 0), // left
119         // 6
120         v3s16(-1, 1, 0), // top left
121         v3s16( 1, 1, 0), // top right
122         v3s16( 0, 1, 1), // top back
123         v3s16( 0, 1,-1), // top front
124         v3s16(-1, 0, 1), // back left
125         v3s16( 1, 0, 1), // back right
126         v3s16(-1, 0,-1), // front left
127         v3s16( 1, 0,-1), // front right
128         v3s16(-1,-1, 0), // bottom left
129         v3s16( 1,-1, 0), // bottom right
130         v3s16( 0,-1, 1), // bottom back
131         v3s16( 0,-1,-1), // bottom front
132         // 18
133         v3s16(-1, 1, 1), // top back-left
134         v3s16( 1, 1, 1), // top back-right
135         v3s16(-1, 1,-1), // top front-left
136         v3s16( 1, 1,-1), // top front-right
137         v3s16(-1,-1, 1), // bottom back-left
138         v3s16( 1,-1, 1), // bottom back-right
139         v3s16(-1,-1,-1), // bottom front-left
140         v3s16( 1,-1,-1), // bottom front-right
141         // 26
142         v3s16(0,0,0),
143 };
144
145 static unsigned long next = 1;
146
147 /* RAND_MAX assumed to be 32767 */
148 int myrand(void)
149 {
150    next = next * 1103515245 + 12345;
151    return((unsigned)(next/65536) % 32768);
152 }
153
154 void mysrand(unsigned seed)
155 {
156    next = seed;
157 }
158
159 #ifndef SERVER
160 // Sets the color of all vertices in the mesh
161 void setMeshVerticesColor(scene::IMesh* mesh, video::SColor& color)
162 {
163         if(mesh == NULL)
164                 return;
165         
166         u16 mc = mesh->getMeshBufferCount();
167         for(u16 j=0; j<mc; j++)
168         {
169                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
170                 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
171                 u16 vc = buf->getVertexCount();
172                 for(u16 i=0; i<vc; i++)
173                 {
174                         vertices[i].Color = color;
175                 }
176         }
177 }
178 #endif
179
180 /*
181         blockpos: position of block in block coordinates
182         camera_pos: position of camera in nodes
183         camera_dir: an unit vector pointing to camera direction
184         range: viewing range
185 */
186 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
187                 f32 camera_fov, f32 range, f32 *distance_ptr)
188 {
189         v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
190         
191         // Block center position
192         v3f blockpos(
193                         ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
194                         ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
195                         ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
196         );
197
198         // Block position relative to camera
199         v3f blockpos_relative = blockpos - camera_pos;
200
201         // Distance in camera direction (+=front, -=back)
202         f32 dforward = blockpos_relative.dotProduct(camera_dir);
203
204         // Total distance
205         f32 d = blockpos_relative.getLength();
206
207         if(distance_ptr)
208                 *distance_ptr = d;
209         
210         // If block is very close, it is always in sight
211         if(d < 1.44*1.44*MAP_BLOCKSIZE*BS/2)
212                 return true;
213
214         // If block is far away, it's not in sight
215         if(d > range * BS)
216                 return false;
217
218         // Maximum radius of a block
219         f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
220         
221         // If block is (nearly) touching the camera, don't
222         // bother validating further (that is, render it anyway)
223         if(d > block_max_radius * 1.5)
224         {
225                 // Cosine of the angle between the camera direction
226                 // and the block direction (camera_dir is an unit vector)
227                 f32 cosangle = dforward / d;
228                 
229                 // Compensate for the size of the block
230                 // (as the block has to be shown even if it's a bit off FOV)
231                 // This is an estimate.
232                 cosangle += block_max_radius / dforward;
233
234                 // If block is not in the field of view, skip it
235                 if(cosangle < cos(camera_fov / 2))
236                         return false;
237         }
238
239         return true;
240 }
241
242 // Get an sha-1 hash of the player's name combined with
243 // the password entered. That's what the server uses as
244 // their password. (Exception : if the password field is
245 // blank, we send a blank password - this is for backwards
246 // compatibility with password-less players).
247 std::string translatePassword(std::string playername, std::wstring password)
248 {
249         if(password.length() == 0)
250                 return "";
251
252         std::string slt = playername + wide_to_narrow(password);
253         SHA1 sha1;
254         sha1.addBytes(slt.c_str(), slt.length());
255         unsigned char *digest = sha1.getDigest();
256         std::string pwd = base64_encode(digest, 20);
257         free(digest);
258         return pwd;
259 }
260
261
262