Use std::queue for HTTPFetchRequest and std::vector for log_output instead of std...
[oweals/minetest.git] / src / util / numeric.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "numeric.h"
21 #include "mathconstants.h"
22
23 #include "log.h"
24 #include "../constants.h" // BS, MAP_BLOCKSIZE
25 #include <string.h>
26 #include <iostream>
27
28 std::map<u16, std::vector<v3s16> > FacePositionCache::m_cache;
29 // Calculate the borders of a "d-radius" cube
30 std::vector<v3s16> FacePositionCache::getFacePositions(u16 d)
31 {
32         if (m_cache.find(d) != m_cache.end())
33                 return m_cache[d];
34
35         generateFacePosition(d);
36         return m_cache[d];
37
38 }
39
40 void FacePositionCache::generateFacePosition(u16 d)
41 {
42         m_cache[d] = std::vector<v3s16>();
43         if(d == 0) {
44                 m_cache[d].push_back(v3s16(0,0,0));
45                 return;
46         }
47         if(d == 1) {
48                 /*
49                         This is an optimized sequence of coordinates.
50                 */
51                 m_cache[d].push_back(v3s16( 0, 1, 0)); // top
52                 m_cache[d].push_back(v3s16( 0, 0, 1)); // back
53                 m_cache[d].push_back(v3s16(-1, 0, 0)); // left
54                 m_cache[d].push_back(v3s16( 1, 0, 0)); // right
55                 m_cache[d].push_back(v3s16( 0, 0,-1)); // front
56                 m_cache[d].push_back(v3s16( 0,-1, 0)); // bottom
57                 // 6
58                 m_cache[d].push_back(v3s16(-1, 0, 1)); // back left
59                 m_cache[d].push_back(v3s16( 1, 0, 1)); // back right
60                 m_cache[d].push_back(v3s16(-1, 0,-1)); // front left
61                 m_cache[d].push_back(v3s16( 1, 0,-1)); // front right
62                 m_cache[d].push_back(v3s16(-1,-1, 0)); // bottom left
63                 m_cache[d].push_back(v3s16( 1,-1, 0)); // bottom right
64                 m_cache[d].push_back(v3s16( 0,-1, 1)); // bottom back
65                 m_cache[d].push_back(v3s16( 0,-1,-1)); // bottom front
66                 m_cache[d].push_back(v3s16(-1, 1, 0)); // top left
67                 m_cache[d].push_back(v3s16( 1, 1, 0)); // top right
68                 m_cache[d].push_back(v3s16( 0, 1, 1)); // top back
69                 m_cache[d].push_back(v3s16( 0, 1,-1)); // top front
70                 // 18
71                 m_cache[d].push_back(v3s16(-1, 1, 1)); // top back-left
72                 m_cache[d].push_back(v3s16( 1, 1, 1)); // top back-right
73                 m_cache[d].push_back(v3s16(-1, 1,-1)); // top front-left
74                 m_cache[d].push_back(v3s16( 1, 1,-1)); // top front-right
75                 m_cache[d].push_back(v3s16(-1,-1, 1)); // bottom back-left
76                 m_cache[d].push_back(v3s16( 1,-1, 1)); // bottom back-right
77                 m_cache[d].push_back(v3s16(-1,-1,-1)); // bottom front-left
78                 m_cache[d].push_back(v3s16( 1,-1,-1)); // bottom front-right
79                 // 26
80                 return;
81         }
82
83         // Take blocks in all sides, starting from y=0 and going +-y
84         for(s16 y=0; y<=d-1; y++) {
85                 // Left and right side, including borders
86                 for(s16 z=-d; z<=d; z++) {
87                         m_cache[d].push_back(v3s16(d,y,z));
88                         m_cache[d].push_back(v3s16(-d,y,z));
89                         if(y != 0) {
90                                 m_cache[d].push_back(v3s16(d,-y,z));
91                                 m_cache[d].push_back(v3s16(-d,-y,z));
92                         }
93                 }
94                 // Back and front side, excluding borders
95                 for(s16 x=-d+1; x<=d-1; x++) {
96                         m_cache[d].push_back(v3s16(x,y,d));
97                         m_cache[d].push_back(v3s16(x,y,-d));
98                         if(y != 0) {
99                                 m_cache[d].push_back(v3s16(x,-y,d));
100                                 m_cache[d].push_back(v3s16(x,-y,-d));
101                         }
102                 }
103         }
104
105         // Take the bottom and top face with borders
106         // -d<x<d, y=+-d, -d<z<d
107         for(s16 x=-d; x<=d; x++)
108         for(s16 z=-d; z<=d; z++) {
109                 m_cache[d].push_back(v3s16(x,-d,z));
110                 m_cache[d].push_back(v3s16(x,d,z));
111         }
112 }
113
114 /*
115     myrand
116 */
117
118 static unsigned long next = 1;
119
120 /* RAND_MAX assumed to be 32767 */
121 int myrand(void)
122 {
123    next = next * 1103515245 + 12345;
124    return((unsigned)(next/65536) % 32768);
125 }
126
127 void mysrand(unsigned seed)
128 {
129    next = seed;
130 }
131
132 int myrand_range(int min, int max)
133 {
134         if(max-min > MYRAND_MAX)
135         {
136                 errorstream<<"WARNING: myrand_range: max-min > MYRAND_MAX"<<std::endl;
137         max = min + MYRAND_MAX;
138         }
139         if(min > max)
140         {
141                 errorstream<<"WARNING: myrand_range: min > max"<<std::endl;
142                 return max;
143         }
144         return (myrand()%(max-min+1))+min;
145 }
146
147 // 64-bit unaligned version of MurmurHash
148 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed)
149 {
150         const u64 m = 0xc6a4a7935bd1e995ULL;
151         const int r = 47;
152         u64 h = seed ^ (len * m);
153
154         const u64 *data = (const u64 *)key;
155         const u64 *end = data + (len / 8);
156
157         while (data != end) {
158                 u64 k;
159                 memcpy(&k, data, sizeof(u64));
160                 data++;
161
162                 k *= m; 
163                 k ^= k >> r; 
164                 k *= m; 
165                 
166                 h ^= k;
167                 h *= m; 
168         }
169
170         const unsigned char *data2 = (const unsigned char *)data;
171         switch (len & 7) {
172                 case 7: h ^= (u64)data2[6] << 48;
173                 case 6: h ^= (u64)data2[5] << 40;
174                 case 5: h ^= (u64)data2[4] << 32;
175                 case 4: h ^= (u64)data2[3] << 24;
176                 case 3: h ^= (u64)data2[2] << 16;
177                 case 2: h ^= (u64)data2[1] << 8;
178                 case 1: h ^= (u64)data2[0];
179                                 h *= m;
180         }
181  
182         h ^= h >> r;
183         h *= m;
184         h ^= h >> r;
185         
186         return h;
187
188
189
190 /*
191         blockpos: position of block in block coordinates
192         camera_pos: position of camera in nodes
193         camera_dir: an unit vector pointing to camera direction
194         range: viewing range
195 */
196 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
197                 f32 camera_fov, f32 range, f32 *distance_ptr)
198 {
199         v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
200         
201         // Block center position
202         v3f blockpos(
203                         ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
204                         ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
205                         ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
206         );
207
208         // Block position relative to camera
209         v3f blockpos_relative = blockpos - camera_pos;
210
211         // Total distance
212         f32 d = blockpos_relative.getLength();
213
214         if(distance_ptr)
215                 *distance_ptr = d;
216         
217         // If block is far away, it's not in sight
218         if(d > range)
219                 return false;
220
221         // Maximum radius of a block.  The magic number is
222         // sqrt(3.0) / 2.0 in literal form.
223         f32 block_max_radius = 0.866025403784 * MAP_BLOCKSIZE * BS;
224         
225         // If block is (nearly) touching the camera, don't
226         // bother validating further (that is, render it anyway)
227         if(d < block_max_radius)
228                 return true;
229
230         // Adjust camera position, for purposes of computing the angle,
231         // such that a block that has any portion visible with the
232         // current camera position will have the center visible at the
233         // adjusted postion
234         f32 adjdist = block_max_radius / cos((M_PI - camera_fov) / 2);
235
236         // Block position relative to adjusted camera
237         v3f blockpos_adj = blockpos - (camera_pos - camera_dir * adjdist);
238
239         // Distance in camera direction (+=front, -=back)
240         f32 dforward = blockpos_adj.dotProduct(camera_dir);
241
242         // Cosine of the angle between the camera direction
243         // and the block direction (camera_dir is an unit vector)
244         f32 cosangle = dforward / blockpos_adj.getLength();
245         
246         // If block is not in the field of view, skip it
247         if(cosangle < cos(camera_fov / 2))
248                 return false;
249
250         return true;
251 }
252