3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
29 TimeTaker::TimeTaker(const char *name, u32 *result)
34 m_time1 = getTimeMs();
37 u32 TimeTaker::stop(bool quiet)
41 u32 time2 = getTimeMs();
42 u32 dtime = time2 - m_time1;
50 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
58 u32 TimeTaker::getTime()
60 u32 time2 = getTimeMs();
61 u32 dtime = time2 - m_time1;
65 const v3s16 g_6dirs[6] =
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
76 const v3s16 g_26dirs[26] =
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
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
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
110 const v3s16 g_27dirs[27] =
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
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
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
145 static unsigned long next = 1;
147 /* RAND_MAX assumed to be 32767 */
150 next = next * 1103515245 + 12345;
151 return((unsigned)(next/65536) % 32768);
154 void mysrand(unsigned seed)
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
165 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
166 f32 camera_fov, f32 range, f32 *distance_ptr)
168 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
170 // Block center position
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
177 // Block position relative to camera
178 v3f blockpos_relative = blockpos - camera_pos;
180 // Distance in camera direction (+=front, -=back)
181 f32 dforward = blockpos_relative.dotProduct(camera_dir);
184 f32 d = blockpos_relative.getLength();
189 // If block is very close, it is always in sight
190 if(d < 1.44*1.44*MAP_BLOCKSIZE*BS/2)
193 // If block is far away, it's not in sight
197 // Maximum radius of a block
198 f32 block_max_radius = 0.5*1.44*1.44*MAP_BLOCKSIZE*BS;
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)
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;
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;
213 // If block is not in the field of view, skip it
214 if(cosangle < cos(camera_fov / 2))
221 // Get an sha-1 hash of the player's name combined with
222 // the password entered. That's what the server uses as
223 // their password. (Exception : if the password field is
224 // blank, we send a blank password - this is for backwards
225 // compatibility with password-less players).
226 std::string translatePassword(std::string playername, std::wstring password)
228 if(password.length() == 0)
231 std::string slt = playername + wide_to_narrow(password);
233 sha1.addBytes(slt.c_str(), slt.length());
234 unsigned char *digest = sha1.getDigest();
235 std::string pwd = base64_encode(digest, 20);