Expose getPointedThing to Lua
[oweals/minetest.git] / src / util / pointedthing.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 UTIL_POINTEDTHING_HEADER
21 #define UTIL_POINTEDTHING_HEADER
22
23 #include "../irrlichttypes.h"
24 #include "../irr_v3d.h"
25 #include <iostream>
26 #include <string>
27
28 enum PointedThingType
29 {
30         POINTEDTHING_NOTHING,
31         POINTEDTHING_NODE,
32         POINTEDTHING_OBJECT
33 };
34
35 //! An active object or node which is selected by a ray on the map.
36 struct PointedThing
37 {
38         //! The type of the pointed object.
39         PointedThingType type = POINTEDTHING_NOTHING;
40         /*!
41          * Only valid if type is POINTEDTHING_NODE.
42          * The coordinates of the node which owns the
43          * nodebox that the ray hits first.
44          * This may differ from node_real_undersurface if
45          * a nodebox exceeds the limits of its node.
46          */
47         v3s16 node_undersurface;
48         /*!
49          * Only valid if type is POINTEDTHING_NODE.
50          * The coordinates of the last node the ray intersects
51          * before node_undersurface. Same as node_undersurface
52          * if the ray starts in a nodebox.
53          */
54         v3s16 node_abovesurface;
55         /*!
56          * Only valid if type is POINTEDTHING_NODE.
57          * The coordinates of the node which contains the
58          * point of the collision and the nodebox of the node.
59          */
60         v3s16 node_real_undersurface;
61         /*!
62          * Only valid if type is POINTEDTHING_OBJECT.
63          * The ID of the object the ray hit.
64          */
65         s16 object_id = -1;
66         /*!
67          * Only valid if type isn't POINTEDTHING_NONE.
68          * First intersection point of the ray and the nodebox.
69          */
70         v3f intersection_point;
71         /*!
72          * Only valid if type isn't POINTEDTHING_NONE.
73          * Normal vector of the intersection.
74          * This is perpendicular to the face the ray hits,
75          * points outside of the box and it's length is 1.
76          */
77         v3s16 intersection_normal;
78         /*!
79          * Square of the distance between the pointing
80          * ray's start point and the intersection point.
81          */
82         f32 distanceSq = 0;
83
84         //! Constructor for POINTEDTHING_NOTHING
85         PointedThing() {};
86         //! Constructor for POINTEDTHING_NODE
87         PointedThing(const v3s16 &under, const v3s16 &above,
88                 const v3s16 &real_under, const v3f &point, const v3s16 &normal,
89                 f32 distSq);
90         //! Constructor for POINTEDTHING_OBJECT
91         PointedThing(s16 id, const v3f &point, const v3s16 &normal, f32 distSq);
92         std::string dump() const;
93         void serialize(std::ostream &os) const;
94         void deSerialize(std::istream &is);
95         /*!
96          * This function ignores the intersection point and normal.
97          */
98         bool operator==(const PointedThing &pt2) const;
99         bool operator!=(const PointedThing &pt2) const;
100 };
101
102 #endif
103