1 /*
2 Minetest
3 Copyright (C) 2016 juhdanad, Daniel Juhasz <juhdanad@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 #pragma once
21 
22 #include "voxelalgorithms.h"
23 #include "util/pointedthing.h"
24 
25 //! Sorts PointedThings based on their distance.
26 struct RaycastSort
27 {
28 	bool operator() (const PointedThing &pt1, const PointedThing &pt2) const;
29 };
30 
31 //! Describes the state of a raycast.
32 class RaycastState
33 {
34 public:
35 	/*!
36 	 * Creates a raycast.
37 	 * @param objects_pointable if false, only nodes will be found
38 	 * @param liquids pointable if false, liquid nodes won't be found
39 	 */
40 	RaycastState(const core::line3d<f32> &shootline, bool objects_pointable,
41 		bool liquids_pointable);
42 
43 	//! Shootline of the raycast.
44 	core::line3d<f32> m_shootline;
45 	//! Iterator to store the progress of the raycast.
46 	voxalgo::VoxelLineIterator m_iterator;
47 	//! Previous tested node during the raycast.
48 	v3s16 m_previous_node;
49 
50 	/*!
51 	 * This priority queue stores the found pointed things
52 	 * waiting to be returned.
53 	 */
54 	std::priority_queue<PointedThing, std::vector<PointedThing>, RaycastSort> m_found;
55 
56 	bool m_objects_pointable;
57 	bool m_liquids_pointable;
58 
59 	//! The code needs to search these nodes around the center node.
60 	core::aabbox3d<s16> m_search_range { 0, 0, 0, 0, 0, 0 };
61 
62 	//! If true, the Environment will initialize this state.
63 	bool m_initialization_needed = true;
64 };
65 
66 /*!
67  * Checks if a line and a box intersects.
68  * @param[in]  box              box to test collision
69  * @param[in]  start            starting point of the line
70  * @param[in]  dir              direction and length of the line
71  * @param[out] collision_point  first point of the collision
72  * @param[out] collision_normal normal vector at the collision, points
73  * outwards of the surface. If start is in the box, zero vector.
74  * @returns true if a collision point was found
75  */
76 bool boxLineCollision(const aabb3f &box, const v3f &start, const v3f &dir,
77 	v3f *collision_point, v3s16 *collision_normal);
78