LocalPlayer::accelerateHorizontal: cleanups
[oweals/minetest.git] / src / voxelalgorithms.h
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 #ifndef VOXELALGORITHMS_HEADER
21 #define VOXELALGORITHMS_HEADER
22
23 #include "voxel.h"
24 #include "mapnode.h"
25 #include "util/container.h"
26
27 class Map;
28 class ServerMap;
29 class MapBlock;
30 class MMVManip;
31
32 namespace voxalgo
33 {
34
35 // TODO: Move unspreadLight and spreadLight from VoxelManipulator to here
36
37 void setLight(VoxelManipulator &v, VoxelArea a, u8 light,
38                 INodeDefManager *ndef);
39
40 void clearLightAndCollectSources(VoxelManipulator &v, VoxelArea a,
41                 enum LightBank bank, INodeDefManager *ndef,
42                 std::set<v3s16> & light_sources,
43                 std::map<v3s16, u8> & unlight_from);
44
45 struct SunlightPropagateResult
46 {
47         bool bottom_sunlight_valid;
48
49         SunlightPropagateResult(bool bottom_sunlight_valid_):
50                 bottom_sunlight_valid(bottom_sunlight_valid_)
51         {}
52 };
53
54 SunlightPropagateResult propagateSunlight(VoxelManipulator &v, VoxelArea a,
55                 bool inexistent_top_provides_sunlight,
56                 std::set<v3s16> & light_sources,
57                 INodeDefManager *ndef);
58
59 /*!
60  * Updates the lighting on the map.
61  * The result will be correct only if
62  * no nodes were changed except the given ones.
63  * Before calling this procedure make sure that all new nodes on
64  * the map have zero light level!
65  *
66  * \param oldnodes contains the MapNodes that were replaced by the new
67  * MapNodes and their positions
68  * \param modified_blocks output, contains all map blocks that
69  * the function modified
70  */
71 void update_lighting_nodes(
72         Map *map,
73         std::vector<std::pair<v3s16, MapNode> > &oldnodes,
74         std::map<v3s16, MapBlock*> &modified_blocks);
75
76 /*!
77  * Updates borders of the given mapblock.
78  * Only updates if the block was marked with incomplete
79  * lighting and the neighbor is also loaded.
80  *
81  * \param block the block to update
82  * \param modified_blocks output, contains all map blocks that
83  * the function modified
84  */
85 void update_block_border_lighting(Map *map, MapBlock *block,
86         std::map<v3s16, MapBlock*> &modified_blocks);
87
88 /*!
89  * Copies back nodes from a voxel manipulator
90  * to the map and updates lighting.
91  * For server use only.
92  *
93  * \param modified_blocks output, contains all map blocks that
94  * the function modified
95  */
96 void blit_back_with_light(ServerMap *map, MMVManip *vm,
97         std::map<v3s16, MapBlock*> *modified_blocks);
98
99 /*!
100  * Corrects the light in a map block.
101  * For server use only.
102  *
103  * \param block the block to update
104  */
105 void repair_block_light(ServerMap *map, MapBlock *block,
106         std::map<v3s16, MapBlock*> *modified_blocks);
107
108 /*!
109  * This class iterates trough voxels that intersect with
110  * a line. The collision detection does not see nodeboxes,
111  * every voxel is a cube and is returned.
112  * This iterator steps to all nodes exactly once.
113  */
114 struct VoxelLineIterator
115 {
116 public:
117         //! Starting position of the line in world coordinates.
118         v3f m_start_position;
119         //! Direction and length of the line in world coordinates.
120         v3f m_line_vector;
121         /*!
122          * Each component stores the next smallest positive number, by
123          * which multiplying the line's vector gives a vector that ends
124          * on the intersection of two nodes.
125          */
126         v3f m_next_intersection_multi { 10000.0f, 10000.0f, 10000.0f };
127         /*!
128          * Each component stores the smallest positive number, by which
129          * m_next_intersection_multi's components can be increased.
130          */
131         v3f m_intersection_multi_inc { 10000.0f, 10000.0f, 10000.0f };
132         /*!
133          * Direction of the line. Each component can be -1 or 1 (if a
134          * component of the line's vector is 0, then there will be 1).
135          */
136         v3s16 m_step_directions { 1, 1, 1 };
137         //! Position of the current node.
138         v3s16 m_current_node_pos;
139         //! Index of the current node
140         s16 m_current_index = 0;
141         //! Position of the start node.
142         v3s16 m_start_node_pos;
143         //! Index of the last node
144         s16 m_last_index;
145
146         /*!
147          * Creates a voxel line iterator with the given line.
148          * @param start_position starting point of the line
149          * in voxel coordinates
150          * @param line_vector    length and direction of the
151          * line in voxel coordinates. start_position+line_vector
152          * is the end of the line
153          */
154         VoxelLineIterator(const v3f &start_position,const v3f &line_vector);
155
156         /*!
157          * Steps to the next voxel.
158          * Updates m_current_node_pos and
159          * m_previous_node_pos.
160          * Note that it works even if hasNext() is false,
161          * continuing the line as a ray.
162          */
163         void next();
164
165         /*!
166          * Returns true if the next voxel intersects the given line.
167          */
168         inline bool hasNext() const
169         {
170                 return m_current_index < m_last_index;
171         }
172
173         /*!
174          * Returns how many times next() must be called until
175          * voxel==m_current_node_pos.
176          * If voxel does not intersect with the line,
177          * the result is undefined.
178          */
179         s16 getIndex(v3s16 voxel);
180 };
181
182 } // namespace voxalgo
183
184
185
186 #endif
187