]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/numeric.cpp
Made unknown nodes stop falling nodes properly and shorten lines
[dragonfireclient.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 <iostream>
26
27 // Calculate the borders of a "d-radius" cube
28 void getFacePositions(std::list<v3s16> &list, u16 d)
29 {
30         if(d == 0)
31         {
32                 list.push_back(v3s16(0,0,0));
33                 return;
34         }
35         if(d == 1)
36         {
37                 /*
38                         This is an optimized sequence of coordinates.
39                 */
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
46                 // 6
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
59                 // 18
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
68                 // 26
69                 return;
70         }
71
72         // Take blocks in all sides, starting from y=0 and going +-y
73         for(s16 y=0; y<=d-1; y++)
74         {
75                 // Left and right side, including borders
76                 for(s16 z=-d; z<=d; z++)
77                 {
78                         list.push_back(v3s16(d,y,z));
79                         list.push_back(v3s16(-d,y,z));
80                         if(y != 0)
81                         {
82                                 list.push_back(v3s16(d,-y,z));
83                                 list.push_back(v3s16(-d,-y,z));
84                         }
85                 }
86                 // Back and front side, excluding borders
87                 for(s16 x=-d+1; x<=d-1; x++)
88                 {
89                         list.push_back(v3s16(x,y,d));
90                         list.push_back(v3s16(x,y,-d));
91                         if(y != 0)
92                         {
93                                 list.push_back(v3s16(x,-y,d));
94                                 list.push_back(v3s16(x,-y,-d));
95                         }
96                 }
97         }
98
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++)
103         {
104                 list.push_back(v3s16(x,-d,z));
105                 list.push_back(v3s16(x,d,z));
106         }
107 }
108
109 /*
110     myrand
111 */
112
113 static unsigned long next = 1;
114
115 /* RAND_MAX assumed to be 32767 */
116 int myrand(void)
117 {
118    next = next * 1103515245 + 12345;
119    return((unsigned)(next/65536) % 32768);
120 }
121
122 void mysrand(unsigned seed)
123 {
124    next = seed;
125 }
126
127 int myrand_range(int min, int max)
128 {
129         if(max-min > MYRAND_MAX)
130         {
131                 errorstream<<"WARNING: myrand_range: max-min > MYRAND_MAX"<<std::endl;
132         max = min + MYRAND_MAX;
133         }
134         if(min > max)
135         {
136                 errorstream<<"WARNING: myrand_range: min > max"<<std::endl;
137                 return max;
138         }
139         return (myrand()%(max-min+1))+min;
140 }
141
142 /*
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
146         range: viewing range
147 */
148 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
149                 f32 camera_fov, f32 range, f32 *distance_ptr)
150 {
151         v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
152         
153         // Block center position
154         v3f blockpos(
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
158         );
159
160         // Block position relative to camera
161         v3f blockpos_relative = blockpos - camera_pos;
162
163         // Total distance
164         f32 d = blockpos_relative.getLength();
165
166         if(distance_ptr)
167                 *distance_ptr = d;
168         
169         // If block is far away, it's not in sight
170         if(d > range)
171                 return false;
172
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;
176         
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)
180                 return true;
181
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
185         // adjusted postion
186         f32 adjdist = block_max_radius / cos((M_PI - camera_fov) / 2);
187
188         // Block position relative to adjusted camera
189         v3f blockpos_adj = blockpos - (camera_pos - camera_dir * adjdist);
190
191         // Distance in camera direction (+=front, -=back)
192         f32 dforward = blockpos_adj.dotProduct(camera_dir);
193
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();
197         
198         // If block is not in the field of view, skip it
199         if(cosangle < cos(camera_fov / 2))
200                 return false;
201
202         return true;
203 }
204