Fix itemdef drop on NULL texture
[oweals/minetest.git] / src / scriptapi_object.h
1 /*
2 Minetest
3 Copyright (C) 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 LUA_OBJECT_H_
21 #define LUA_OBJECT_H_
22
23 extern "C" {
24 #include <lua.h>
25 #include <lauxlib.h>
26 }
27
28 #include "serverobject.h"
29 #include "content_sao.h"
30 #include "player.h"
31
32 /*
33         ObjectRef
34 */
35
36 class ObjectRef
37 {
38 private:
39         ServerActiveObject *m_object;
40
41         static const char className[];
42         static const luaL_reg methods[];
43 public:
44         static ObjectRef *checkobject(lua_State *L, int narg);
45
46         static ServerActiveObject* getobject(ObjectRef *ref);
47 private:
48         static LuaEntitySAO* getluaobject(ObjectRef *ref);
49
50         static PlayerSAO* getplayersao(ObjectRef *ref);
51
52         static Player* getplayer(ObjectRef *ref);
53
54         // Exported functions
55
56         // garbage collector
57         static int gc_object(lua_State *L);
58
59         // remove(self)
60         static int l_remove(lua_State *L);
61
62         // getpos(self)
63         // returns: {x=num, y=num, z=num}
64         static int l_getpos(lua_State *L);
65
66         // setpos(self, pos)
67         static int l_setpos(lua_State *L);
68
69         // moveto(self, pos, continuous=false)
70         static int l_moveto(lua_State *L);
71
72         // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
73         static int l_punch(lua_State *L);
74
75         // right_click(self, clicker); clicker = an another ObjectRef
76         static int l_right_click(lua_State *L);
77
78         // set_hp(self, hp)
79         // hp = number of hitpoints (2 * number of hearts)
80         // returns: nil
81         static int l_set_hp(lua_State *L);
82
83         // get_hp(self)
84         // returns: number of hitpoints (2 * number of hearts)
85         // 0 if not applicable to this type of object
86         static int l_get_hp(lua_State *L);
87
88         // get_inventory(self)
89         static int l_get_inventory(lua_State *L);
90
91         // get_wield_list(self)
92         static int l_get_wield_list(lua_State *L);
93
94         // get_wield_index(self)
95         static int l_get_wield_index(lua_State *L);
96
97         // get_wielded_item(self)
98         static int l_get_wielded_item(lua_State *L);
99
100         // set_wielded_item(self, itemstack or itemstring or table or nil)
101         static int l_set_wielded_item(lua_State *L);
102
103         // set_armor_groups(self, groups)
104         static int l_set_armor_groups(lua_State *L);
105
106         // set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity)
107         static int l_set_physics_override(lua_State *L);
108
109         // set_animation(self, frame_range, frame_speed, frame_blend)
110         static int l_set_animation(lua_State *L);
111
112         // set_bone_position(self, std::string bone, v3f position, v3f rotation)
113         static int l_set_bone_position(lua_State *L);
114
115         // set_attach(self, parent, bone, position, rotation)
116         static int l_set_attach(lua_State *L);
117
118         // set_detach(self)
119         static int l_set_detach(lua_State *L);
120
121         // set_properties(self, properties)
122         static int l_set_properties(lua_State *L);
123
124         /* LuaEntitySAO-only */
125
126         // setvelocity(self, {x=num, y=num, z=num})
127         static int l_setvelocity(lua_State *L);
128
129         // getvelocity(self)
130         static int l_getvelocity(lua_State *L);
131
132         // setacceleration(self, {x=num, y=num, z=num})
133         static int l_setacceleration(lua_State *L);
134
135         // getacceleration(self)
136         static int l_getacceleration(lua_State *L);
137
138         // setyaw(self, radians)
139         static int l_setyaw(lua_State *L);
140
141         // getyaw(self)
142         static int l_getyaw(lua_State *L);
143
144         // settexturemod(self, mod)
145         static int l_settexturemod(lua_State *L);
146
147         // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
148         //           select_horiz_by_yawpitch=false)
149         static int l_setsprite(lua_State *L);
150
151         // DEPRECATED
152         // get_entity_name(self)
153         static int l_get_entity_name(lua_State *L);
154
155         // get_luaentity(self)
156         static int l_get_luaentity(lua_State *L);
157
158         /* Player-only */
159
160         // is_player(self)
161         static int l_is_player(lua_State *L);
162
163         // get_player_name(self)
164         static int l_get_player_name(lua_State *L);
165
166         // get_look_dir(self)
167         static int l_get_look_dir(lua_State *L);
168
169         // get_look_pitch(self)
170         static int l_get_look_pitch(lua_State *L);
171
172         // get_look_yaw(self)
173         static int l_get_look_yaw(lua_State *L);
174
175         // set_look_pitch(self, radians)
176         static int l_set_look_pitch(lua_State *L);
177
178         // set_look_yaw(self, radians)
179         static int l_set_look_yaw(lua_State *L);
180
181         // set_inventory_formspec(self, formspec)
182         static int l_set_inventory_formspec(lua_State *L);
183
184         // get_inventory_formspec(self) -> formspec
185         static int l_get_inventory_formspec(lua_State *L);
186
187         // get_player_control(self)
188         static int l_get_player_control(lua_State *L);
189
190         // get_player_control_bits(self)
191         static int l_get_player_control_bits(lua_State *L);
192
193         // hud_add(self, id, form)
194         static int l_hud_add(lua_State *L);
195
196         // hud_rm(self, id)
197         static int l_hud_remove(lua_State *L);
198
199         // hud_change(self, id, stat, data)
200         static int l_hud_change(lua_State *L);
201
202         // hud_get(self, id)
203         static int l_hud_get(lua_State *L);
204
205         // hud_set_flags(self, flags)
206         static int l_hud_set_flags(lua_State *L);
207
208 public:
209         ObjectRef(ServerActiveObject *object);
210
211         ~ObjectRef();
212
213         // Creates an ObjectRef and leaves it on top of stack
214         // Not callable from Lua; all references are created on the C side.
215         static void create(lua_State *L, ServerActiveObject *object);
216
217         static void set_null(lua_State *L);
218
219         static void Register(lua_State *L);
220 };
221
222 /*****************************************************************************/
223 /* scriptapi internal                                                        */
224 /*****************************************************************************/
225 // Creates a new anonymous reference if cobj=NULL or id=0
226 void objectref_get_or_create(lua_State *L,
227                 ServerActiveObject *cobj);
228 void objectref_get(lua_State *L, u16 id);
229 void read_object_properties(lua_State *L, int index,
230                 ObjectProperties *prop);
231
232 /*****************************************************************************/
233 /* Minetest interface                                                        */
234 /*****************************************************************************/
235 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
236 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj);
237
238 #endif /* LUA_OBJECT_H_ */