3 Copyright (C) 2010-2013 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 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.
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.
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.
21 #include "mathconstants.h"
24 #include "../constants.h" // BS, MAP_BLOCKSIZE
27 // Calculate the borders of a "d-radius" cube
28 void getFacePositions(std::list<v3s16> &list, u16 d)
32 list.push_back(v3s16(0,0,0));
38 This is an optimized sequence of coordinates.
40 list.push_back(v3s16( 0, 1, 0)); // top
41 list.push_back(v3s16( 0, 0, 1)); // back
42 list.push_back(v3s16(-1, 0, 0)); // left
43 list.push_back(v3s16( 1, 0, 0)); // right
44 list.push_back(v3s16( 0, 0,-1)); // front
45 list.push_back(v3s16( 0,-1, 0)); // bottom
47 list.push_back(v3s16(-1, 0, 1)); // back left
48 list.push_back(v3s16( 1, 0, 1)); // back right
49 list.push_back(v3s16(-1, 0,-1)); // front left
50 list.push_back(v3s16( 1, 0,-1)); // front right
51 list.push_back(v3s16(-1,-1, 0)); // bottom left
52 list.push_back(v3s16( 1,-1, 0)); // bottom right
53 list.push_back(v3s16( 0,-1, 1)); // bottom back
54 list.push_back(v3s16( 0,-1,-1)); // bottom front
55 list.push_back(v3s16(-1, 1, 0)); // top left
56 list.push_back(v3s16( 1, 1, 0)); // top right
57 list.push_back(v3s16( 0, 1, 1)); // top back
58 list.push_back(v3s16( 0, 1,-1)); // top front
60 list.push_back(v3s16(-1, 1, 1)); // top back-left
61 list.push_back(v3s16( 1, 1, 1)); // top back-right
62 list.push_back(v3s16(-1, 1,-1)); // top front-left
63 list.push_back(v3s16( 1, 1,-1)); // top front-right
64 list.push_back(v3s16(-1,-1, 1)); // bottom back-left
65 list.push_back(v3s16( 1,-1, 1)); // bottom back-right
66 list.push_back(v3s16(-1,-1,-1)); // bottom front-left
67 list.push_back(v3s16( 1,-1,-1)); // bottom front-right
72 // Take blocks in all sides, starting from y=0 and going +-y
73 for(s16 y=0; y<=d-1; y++)
75 // Left and right side, including borders
76 for(s16 z=-d; z<=d; z++)
78 list.push_back(v3s16(d,y,z));
79 list.push_back(v3s16(-d,y,z));
82 list.push_back(v3s16(d,-y,z));
83 list.push_back(v3s16(-d,-y,z));
86 // Back and front side, excluding borders
87 for(s16 x=-d+1; x<=d-1; x++)
89 list.push_back(v3s16(x,y,d));
90 list.push_back(v3s16(x,y,-d));
93 list.push_back(v3s16(x,-y,d));
94 list.push_back(v3s16(x,-y,-d));
99 // Take the bottom and top face with borders
100 // -d<x<d, y=+-d, -d<z<d
101 for(s16 x=-d; x<=d; x++)
102 for(s16 z=-d; z<=d; z++)
104 list.push_back(v3s16(x,-d,z));
105 list.push_back(v3s16(x,d,z));
113 static unsigned long next = 1;
115 /* RAND_MAX assumed to be 32767 */
118 next = next * 1103515245 + 12345;
119 return((unsigned)(next/65536) % 32768);
122 void mysrand(unsigned seed)
127 int myrand_range(int min, int max)
129 if(max-min > MYRAND_MAX)
131 errorstream<<"WARNING: myrand_range: max-min > MYRAND_MAX"<<std::endl;
132 max = min + MYRAND_MAX;
136 errorstream<<"WARNING: myrand_range: min > max"<<std::endl;
139 return (myrand()%(max-min+1))+min;
143 blockpos: position of block in block coordinates
144 camera_pos: position of camera in nodes
145 camera_dir: an unit vector pointing to camera direction
148 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
149 f32 camera_fov, f32 range, f32 *distance_ptr)
151 v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
153 // Block center position
155 ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS,
156 ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS,
157 ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS
160 // Block position relative to camera
161 v3f blockpos_relative = blockpos - camera_pos;
164 f32 d = blockpos_relative.getLength();
169 // If block is far away, it's not in sight
173 // Maximum radius of a block. The magic number is
174 // sqrt(3.0) / 2.0 in literal form.
175 f32 block_max_radius = 0.866025403784 * MAP_BLOCKSIZE * BS;
177 // If block is (nearly) touching the camera, don't
178 // bother validating further (that is, render it anyway)
179 if(d < block_max_radius)
182 // Adjust camera position, for purposes of computing the angle,
183 // such that a block that has any portion visible with the
184 // current camera position will have the center visible at the
186 f32 adjdist = block_max_radius / cos((M_PI - camera_fov) / 2);
188 // Block position relative to adjusted camera
189 v3f blockpos_adj = blockpos - (camera_pos - camera_dir * adjdist);
191 // Distance in camera direction (+=front, -=back)
192 f32 dforward = blockpos_adj.dotProduct(camera_dir);
194 // Cosine of the angle between the camera direction
195 // and the block direction (camera_dir is an unit vector)
196 f32 cosangle = dforward / blockpos_adj.getLength();
198 // If block is not in the field of view, skip it
199 if(cosangle < cos(camera_fov / 2))