tuned lava/universal damage code
[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 "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 /*
160         blockpos: position of block in block coordinates
161         camera_pos: position of camera in nodes
162         camera_dir: an unit vector pointing to camera direction
163         range: viewing range
164 */
165 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
166                 f32 *distance_ptr)
167 {
168         v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
169         
170         // Block center position
171         v3f blockpos(
172                         ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
173                         ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
174                         ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
175         );
176
177         // Block position relative to camera
178         v3f blockpos_relative = blockpos - camera_pos;
179
180         // Distance in camera direction (+=front, -=back)
181         f32 dforward = blockpos_relative.dotProduct(camera_dir);
182
183         // Total distance
184         f32 d = blockpos_relative.getLength();
185
186         if(distance_ptr)
187                 *distance_ptr = d;
188         
189         // If block is very close, it is always in sight
190         if(d < 1.44*1.44*MAP_BLOCKSIZE*BS/2)
191                 return true;
192
193         // If block is far away, it's not in sight
194         if(d > range * BS)
195                 return false;
196
197         // Maximum radius of a block
198         f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
199         
200         // If block is (nearly) touching the camera, don't
201         // bother validating further (that is, render it anyway)
202         if(d > block_max_radius * 1.5)
203         {
204                 // Cosine of the angle between the camera direction
205                 // and the block direction (camera_dir is an unit vector)
206                 f32 cosangle = dforward / d;
207                 
208                 // Compensate for the size of the block
209                 // (as the block has to be shown even if it's a bit off FOV)
210                 // This is an estimate.
211                 cosangle += block_max_radius / dforward;
212
213                 // If block is not in the field of view, skip it
214                 //if(cosangle < cos(FOV_ANGLE/2))
215                 if(cosangle < cos(FOV_ANGLE/2. * 4./3.))
216                         return false;
217         }
218
219         return true;
220 }
221
222 // Get an sha-1 hash of the player's name combined with
223 // the password entered. That's what the server uses as
224 // their password. (Exception : if the password field is
225 // blank, we send a blank password - this is for backwards
226 // compatibility with password-less players).
227 std::string translatePassword(std::string playername, std::wstring password)
228 {
229         if(password.length() == 0)
230                 return "";
231
232         std::string slt = playername + wide_to_narrow(password);
233         SHA1 sha1;
234         sha1.addBytes(slt.c_str(), slt.length());
235         unsigned char *digest = sha1.getDigest();
236         std::string pwd = base64_encode(digest, 20);
237         free(digest);
238         return pwd;
239 }
240
241
242