Merge remote-tracking branch 'origin/master'
[oweals/minetest.git] / src / script / lua_api / l_object.cpp
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 #include "cpp_api/scriptapi.h"
21 #include "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "lua_api/l_object.h"
24 #include "common/c_internal.h"
25 #include "lua_api/l_inventory.h"
26 #include "lua_api/l_item.h"
27 #include "log.h"
28 #include "tool.h"
29 #include "serverobject.h"
30 #include "content_object.h"
31 #include "content_sao.h"
32 #include "server.h"
33 #include "hud.h"
34
35
36 struct EnumString es_HudElementType[] =
37 {
38         {HUD_ELEM_IMAGE,     "image"},
39         {HUD_ELEM_TEXT,      "text"},
40         {HUD_ELEM_STATBAR,   "statbar"},
41         {HUD_ELEM_INVENTORY, "inventory"},
42 {0, NULL},
43 };
44
45 struct EnumString es_HudElementStat[] =
46 {
47         {HUD_STAT_POS,    "pos"},
48         {HUD_STAT_NAME,   "name"},
49         {HUD_STAT_SCALE,  "scale"},
50         {HUD_STAT_TEXT,   "text"},
51         {HUD_STAT_NUMBER, "number"},
52         {HUD_STAT_ITEM,   "item"},
53         {HUD_STAT_DIR,    "direction"},
54         {HUD_STAT_ALIGN,  "alignment"},
55         {HUD_STAT_OFFSET, "offset"},
56         {0, NULL},
57 };
58
59 struct EnumString es_HudBuiltinElement[] =
60 {
61         {HUD_FLAG_HOTBAR_VISIBLE,    "hotbar"},
62         {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
63         {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
64         {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
65         {0, NULL},
66 };
67
68 /*
69         ObjectRef
70 */
71
72
73 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
74 {
75         luaL_checktype(L, narg, LUA_TUSERDATA);
76         void *ud = luaL_checkudata(L, narg, className);
77         if(!ud) luaL_typerror(L, narg, className);
78         return *(ObjectRef**)ud;  // unbox pointer
79 }
80
81 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
82 {
83         ServerActiveObject *co = ref->m_object;
84         return co;
85 }
86
87 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
88 {
89         ServerActiveObject *obj = getobject(ref);
90         if(obj == NULL)
91                 return NULL;
92         if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
93                 return NULL;
94         return (LuaEntitySAO*)obj;
95 }
96
97 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
98 {
99         ServerActiveObject *obj = getobject(ref);
100         if(obj == NULL)
101                 return NULL;
102         if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
103                 return NULL;
104         return (PlayerSAO*)obj;
105 }
106
107 Player* ObjectRef::getplayer(ObjectRef *ref)
108 {
109         PlayerSAO *playersao = getplayersao(ref);
110         if(playersao == NULL)
111                 return NULL;
112         return playersao->getPlayer();
113 }
114
115 // Exported functions
116
117 // garbage collector
118 int ObjectRef::gc_object(lua_State *L) {
119         ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
120         //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
121         delete o;
122         return 0;
123 }
124
125 // remove(self)
126 int ObjectRef::l_remove(lua_State *L)
127 {
128         NO_MAP_LOCK_REQUIRED;
129         ObjectRef *ref = checkobject(L, 1);
130         ServerActiveObject *co = getobject(ref);
131         if(co == NULL) return 0;
132         verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
133         co->m_removed = true;
134         return 0;
135 }
136
137 // getpos(self)
138 // returns: {x=num, y=num, z=num}
139 int ObjectRef::l_getpos(lua_State *L)
140 {
141         NO_MAP_LOCK_REQUIRED;
142         ObjectRef *ref = checkobject(L, 1);
143         ServerActiveObject *co = getobject(ref);
144         if(co == NULL) return 0;
145         v3f pos = co->getBasePosition() / BS;
146         lua_newtable(L);
147         lua_pushnumber(L, pos.X);
148         lua_setfield(L, -2, "x");
149         lua_pushnumber(L, pos.Y);
150         lua_setfield(L, -2, "y");
151         lua_pushnumber(L, pos.Z);
152         lua_setfield(L, -2, "z");
153         return 1;
154 }
155
156 // setpos(self, pos)
157 int ObjectRef::l_setpos(lua_State *L)
158 {
159         NO_MAP_LOCK_REQUIRED;
160         ObjectRef *ref = checkobject(L, 1);
161         //LuaEntitySAO *co = getluaobject(ref);
162         ServerActiveObject *co = getobject(ref);
163         if(co == NULL) return 0;
164         // pos
165         v3f pos = checkFloatPos(L, 2);
166         // Do it
167         co->setPos(pos);
168         return 0;
169 }
170
171 // moveto(self, pos, continuous=false)
172 int ObjectRef::l_moveto(lua_State *L)
173 {
174         NO_MAP_LOCK_REQUIRED;
175         ObjectRef *ref = checkobject(L, 1);
176         //LuaEntitySAO *co = getluaobject(ref);
177         ServerActiveObject *co = getobject(ref);
178         if(co == NULL) return 0;
179         // pos
180         v3f pos = checkFloatPos(L, 2);
181         // continuous
182         bool continuous = lua_toboolean(L, 3);
183         // Do it
184         co->moveTo(pos, continuous);
185         return 0;
186 }
187
188 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
189 int ObjectRef::l_punch(lua_State *L)
190 {
191         NO_MAP_LOCK_REQUIRED;
192         ObjectRef *ref = checkobject(L, 1);
193         ObjectRef *puncher_ref = checkobject(L, 2);
194         ServerActiveObject *co = getobject(ref);
195         ServerActiveObject *puncher = getobject(puncher_ref);
196         if(co == NULL) return 0;
197         if(puncher == NULL) return 0;
198         v3f dir;
199         if(lua_type(L, 5) != LUA_TTABLE)
200                 dir = co->getBasePosition() - puncher->getBasePosition();
201         else
202                 dir = read_v3f(L, 5);
203         float time_from_last_punch = 1000000;
204         if(lua_isnumber(L, 3))
205                 time_from_last_punch = lua_tonumber(L, 3);
206         ToolCapabilities toolcap = read_tool_capabilities(L, 4);
207         dir.normalize();
208         // Do it
209         co->punch(dir, &toolcap, puncher, time_from_last_punch);
210         return 0;
211 }
212
213 // right_click(self, clicker); clicker = an another ObjectRef
214 int ObjectRef::l_right_click(lua_State *L)
215 {
216         NO_MAP_LOCK_REQUIRED;
217         ObjectRef *ref = checkobject(L, 1);
218         ObjectRef *ref2 = checkobject(L, 2);
219         ServerActiveObject *co = getobject(ref);
220         ServerActiveObject *co2 = getobject(ref2);
221         if(co == NULL) return 0;
222         if(co2 == NULL) return 0;
223         // Do it
224         co->rightClick(co2);
225         return 0;
226 }
227
228 // set_hp(self, hp)
229 // hp = number of hitpoints (2 * number of hearts)
230 // returns: nil
231 int ObjectRef::l_set_hp(lua_State *L)
232 {
233         NO_MAP_LOCK_REQUIRED;
234         ObjectRef *ref = checkobject(L, 1);
235         luaL_checknumber(L, 2);
236         ServerActiveObject *co = getobject(ref);
237         if(co == NULL) return 0;
238         int hp = lua_tonumber(L, 2);
239         /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
240                         <<" hp="<<hp<<std::endl;*/
241         // Do it
242         co->setHP(hp);
243         // Return
244         return 0;
245 }
246
247 // get_hp(self)
248 // returns: number of hitpoints (2 * number of hearts)
249 // 0 if not applicable to this type of object
250 int ObjectRef::l_get_hp(lua_State *L)
251 {
252         NO_MAP_LOCK_REQUIRED;
253         ObjectRef *ref = checkobject(L, 1);
254         ServerActiveObject *co = getobject(ref);
255         if(co == NULL){
256                 // Default hp is 1
257                 lua_pushnumber(L, 1);
258                 return 1;
259         }
260         int hp = co->getHP();
261         /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
262                         <<" hp="<<hp<<std::endl;*/
263         // Return
264         lua_pushnumber(L, hp);
265         return 1;
266 }
267
268 // get_inventory(self)
269 int ObjectRef::l_get_inventory(lua_State *L)
270 {
271         NO_MAP_LOCK_REQUIRED;
272         ObjectRef *ref = checkobject(L, 1);
273         ServerActiveObject *co = getobject(ref);
274         if(co == NULL) return 0;
275         // Do it
276         InventoryLocation loc = co->getInventoryLocation();
277         if(STACK_TO_SERVER(L)->getInventory(loc) != NULL)
278                 InvRef::create(L, loc);
279         else
280                 lua_pushnil(L); // An object may have no inventory (nil)
281         return 1;
282 }
283
284 // get_wield_list(self)
285 int ObjectRef::l_get_wield_list(lua_State *L)
286 {
287         NO_MAP_LOCK_REQUIRED;
288         ObjectRef *ref = checkobject(L, 1);
289         ServerActiveObject *co = getobject(ref);
290         if(co == NULL) return 0;
291         // Do it
292         lua_pushstring(L, co->getWieldList().c_str());
293         return 1;
294 }
295
296 // get_wield_index(self)
297 int ObjectRef::l_get_wield_index(lua_State *L)
298 {
299         NO_MAP_LOCK_REQUIRED;
300         ObjectRef *ref = checkobject(L, 1);
301         ServerActiveObject *co = getobject(ref);
302         if(co == NULL) return 0;
303         // Do it
304         lua_pushinteger(L, co->getWieldIndex() + 1);
305         return 1;
306 }
307
308 // get_wielded_item(self)
309 int ObjectRef::l_get_wielded_item(lua_State *L)
310 {
311         NO_MAP_LOCK_REQUIRED;
312         ObjectRef *ref = checkobject(L, 1);
313         ServerActiveObject *co = getobject(ref);
314         if(co == NULL){
315                 // Empty ItemStack
316                 LuaItemStack::create(L, ItemStack());
317                 return 1;
318         }
319         // Do it
320         LuaItemStack::create(L, co->getWieldedItem());
321         return 1;
322 }
323
324 // set_wielded_item(self, itemstack or itemstring or table or nil)
325 int ObjectRef::l_set_wielded_item(lua_State *L)
326 {
327         NO_MAP_LOCK_REQUIRED;
328         ObjectRef *ref = checkobject(L, 1);
329         ServerActiveObject *co = getobject(ref);
330         if(co == NULL) return 0;
331         // Do it
332         ItemStack item = read_item(L, 2,STACK_TO_SERVER(L));
333         bool success = co->setWieldedItem(item);
334         lua_pushboolean(L, success);
335         return 1;
336 }
337
338 // set_armor_groups(self, groups)
339 int ObjectRef::l_set_armor_groups(lua_State *L)
340 {
341         NO_MAP_LOCK_REQUIRED;
342         ObjectRef *ref = checkobject(L, 1);
343         ServerActiveObject *co = getobject(ref);
344         if(co == NULL) return 0;
345         // Do it
346         ItemGroupList groups;
347         read_groups(L, 2, groups);
348         co->setArmorGroups(groups);
349         return 0;
350 }
351
352 // set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity)
353 int ObjectRef::l_set_physics_override(lua_State *L)
354 {
355         ObjectRef *ref = checkobject(L, 1);
356         PlayerSAO *co = (PlayerSAO *) getobject(ref);
357         if(co == NULL) return 0;
358         // Do it
359         if(!lua_isnil(L, 2)){
360                 co->m_physics_override_speed = lua_tonumber(L, 2);
361                 co->m_physics_override_sent = false;
362         }
363         if(!lua_isnil(L, 3)){
364                 co->m_physics_override_jump = lua_tonumber(L, 3);
365                 co->m_physics_override_sent = false;
366         }
367         if(!lua_isnil(L, 4)){
368                 co->m_physics_override_gravity = lua_tonumber(L, 4);
369                 co->m_physics_override_sent = false;
370         }
371         return 0;
372 }
373
374 // set_animation(self, frame_range, frame_speed, frame_blend)
375 int ObjectRef::l_set_animation(lua_State *L)
376 {
377         NO_MAP_LOCK_REQUIRED;
378         ObjectRef *ref = checkobject(L, 1);
379         ServerActiveObject *co = getobject(ref);
380         if(co == NULL) return 0;
381         // Do it
382         v2f frames = v2f(1, 1);
383         if(!lua_isnil(L, 2))
384                 frames = read_v2f(L, 2);
385         float frame_speed = 15;
386         if(!lua_isnil(L, 3))
387                 frame_speed = lua_tonumber(L, 3);
388         float frame_blend = 0;
389         if(!lua_isnil(L, 4))
390                 frame_blend = lua_tonumber(L, 4);
391         co->setAnimation(frames, frame_speed, frame_blend);
392         return 0;
393 }
394
395 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
396 int ObjectRef::l_set_bone_position(lua_State *L)
397 {
398         NO_MAP_LOCK_REQUIRED;
399         ObjectRef *ref = checkobject(L, 1);
400         ServerActiveObject *co = getobject(ref);
401         if(co == NULL) return 0;
402         // Do it
403         std::string bone = "";
404         if(!lua_isnil(L, 2))
405                 bone = lua_tostring(L, 2);
406         v3f position = v3f(0, 0, 0);
407         if(!lua_isnil(L, 3))
408                 position = read_v3f(L, 3);
409         v3f rotation = v3f(0, 0, 0);
410         if(!lua_isnil(L, 4))
411                 rotation = read_v3f(L, 4);
412         co->setBonePosition(bone, position, rotation);
413         return 0;
414 }
415
416 // set_attach(self, parent, bone, position, rotation)
417 int ObjectRef::l_set_attach(lua_State *L)
418 {
419         NO_MAP_LOCK_REQUIRED;
420         ObjectRef *ref = checkobject(L, 1);
421         ObjectRef *parent_ref = checkobject(L, 2);
422         ServerActiveObject *co = getobject(ref);
423         ServerActiveObject *parent = getobject(parent_ref);
424         if(co == NULL) return 0;
425         if(parent == NULL) return 0;
426         // Do it
427         std::string bone = "";
428         if(!lua_isnil(L, 3))
429                 bone = lua_tostring(L, 3);
430         v3f position = v3f(0, 0, 0);
431         if(!lua_isnil(L, 4))
432                 position = read_v3f(L, 4);
433         v3f rotation = v3f(0, 0, 0);
434         if(!lua_isnil(L, 5))
435                 rotation = read_v3f(L, 5);
436         co->setAttachment(parent->getId(), bone, position, rotation);
437         return 0;
438 }
439
440 // set_detach(self)
441 int ObjectRef::l_set_detach(lua_State *L)
442 {
443         NO_MAP_LOCK_REQUIRED;
444         ObjectRef *ref = checkobject(L, 1);
445         ServerActiveObject *co = getobject(ref);
446         if(co == NULL) return 0;
447         // Do it
448         co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
449         return 0;
450 }
451
452 // set_properties(self, properties)
453 int ObjectRef::l_set_properties(lua_State *L)
454 {
455         NO_MAP_LOCK_REQUIRED;
456         ObjectRef *ref = checkobject(L, 1);
457         ServerActiveObject *co = getobject(ref);
458         if(co == NULL) return 0;
459         ObjectProperties *prop = co->accessObjectProperties();
460         if(!prop)
461                 return 0;
462         read_object_properties(L, 2, prop);
463         co->notifyObjectPropertiesModified();
464         return 0;
465 }
466
467 /* LuaEntitySAO-only */
468
469 // setvelocity(self, {x=num, y=num, z=num})
470 int ObjectRef::l_setvelocity(lua_State *L)
471 {
472         NO_MAP_LOCK_REQUIRED;
473         ObjectRef *ref = checkobject(L, 1);
474         LuaEntitySAO *co = getluaobject(ref);
475         if(co == NULL) return 0;
476         v3f pos = checkFloatPos(L, 2);
477         // Do it
478         co->setVelocity(pos);
479         return 0;
480 }
481
482 // getvelocity(self)
483 int ObjectRef::l_getvelocity(lua_State *L)
484 {
485         NO_MAP_LOCK_REQUIRED;
486         ObjectRef *ref = checkobject(L, 1);
487         LuaEntitySAO *co = getluaobject(ref);
488         if(co == NULL) return 0;
489         // Do it
490         v3f v = co->getVelocity();
491         pushFloatPos(L, v);
492         return 1;
493 }
494
495 // setacceleration(self, {x=num, y=num, z=num})
496 int ObjectRef::l_setacceleration(lua_State *L)
497 {
498         NO_MAP_LOCK_REQUIRED;
499         ObjectRef *ref = checkobject(L, 1);
500         LuaEntitySAO *co = getluaobject(ref);
501         if(co == NULL) return 0;
502         // pos
503         v3f pos = checkFloatPos(L, 2);
504         // Do it
505         co->setAcceleration(pos);
506         return 0;
507 }
508
509 // getacceleration(self)
510 int ObjectRef::l_getacceleration(lua_State *L)
511 {
512         NO_MAP_LOCK_REQUIRED;
513         ObjectRef *ref = checkobject(L, 1);
514         LuaEntitySAO *co = getluaobject(ref);
515         if(co == NULL) return 0;
516         // Do it
517         v3f v = co->getAcceleration();
518         pushFloatPos(L, v);
519         return 1;
520 }
521
522 // setyaw(self, radians)
523 int ObjectRef::l_setyaw(lua_State *L)
524 {
525         NO_MAP_LOCK_REQUIRED;
526         ObjectRef *ref = checkobject(L, 1);
527         LuaEntitySAO *co = getluaobject(ref);
528         if(co == NULL) return 0;
529         float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
530         // Do it
531         co->setYaw(yaw);
532         return 0;
533 }
534
535 // getyaw(self)
536 int ObjectRef::l_getyaw(lua_State *L)
537 {
538         NO_MAP_LOCK_REQUIRED;
539         ObjectRef *ref = checkobject(L, 1);
540         LuaEntitySAO *co = getluaobject(ref);
541         if(co == NULL) return 0;
542         // Do it
543         float yaw = co->getYaw() * core::DEGTORAD;
544         lua_pushnumber(L, yaw);
545         return 1;
546 }
547
548 // settexturemod(self, mod)
549 int ObjectRef::l_settexturemod(lua_State *L)
550 {
551         NO_MAP_LOCK_REQUIRED;
552         ObjectRef *ref = checkobject(L, 1);
553         LuaEntitySAO *co = getluaobject(ref);
554         if(co == NULL) return 0;
555         // Do it
556         std::string mod = luaL_checkstring(L, 2);
557         co->setTextureMod(mod);
558         return 0;
559 }
560
561 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
562 //           select_horiz_by_yawpitch=false)
563 int ObjectRef::l_setsprite(lua_State *L)
564 {
565         NO_MAP_LOCK_REQUIRED;
566         ObjectRef *ref = checkobject(L, 1);
567         LuaEntitySAO *co = getluaobject(ref);
568         if(co == NULL) return 0;
569         // Do it
570         v2s16 p(0,0);
571         if(!lua_isnil(L, 2))
572                 p = read_v2s16(L, 2);
573         int num_frames = 1;
574         if(!lua_isnil(L, 3))
575                 num_frames = lua_tonumber(L, 3);
576         float framelength = 0.2;
577         if(!lua_isnil(L, 4))
578                 framelength = lua_tonumber(L, 4);
579         bool select_horiz_by_yawpitch = false;
580         if(!lua_isnil(L, 5))
581                 select_horiz_by_yawpitch = lua_toboolean(L, 5);
582         co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
583         return 0;
584 }
585
586 // DEPRECATED
587 // get_entity_name(self)
588 int ObjectRef::l_get_entity_name(lua_State *L)
589 {
590         NO_MAP_LOCK_REQUIRED;
591         ObjectRef *ref = checkobject(L, 1);
592         LuaEntitySAO *co = getluaobject(ref);
593         if(co == NULL) return 0;
594         // Do it
595         std::string name = co->getName();
596         lua_pushstring(L, name.c_str());
597         return 1;
598 }
599
600 // get_luaentity(self)
601 int ObjectRef::l_get_luaentity(lua_State *L)
602 {
603         NO_MAP_LOCK_REQUIRED;
604         ObjectRef *ref = checkobject(L, 1);
605         LuaEntitySAO *co = getluaobject(ref);
606         if(co == NULL) return 0;
607         // Do it
608         luaentity_get(L, co->getId());
609         return 1;
610 }
611
612 /* Player-only */
613
614 // is_player(self)
615 int ObjectRef::l_is_player(lua_State *L)
616 {
617         NO_MAP_LOCK_REQUIRED;
618         ObjectRef *ref = checkobject(L, 1);
619         Player *player = getplayer(ref);
620         lua_pushboolean(L, (player != NULL));
621         return 1;
622 }
623
624 // get_player_name(self)
625 int ObjectRef::l_get_player_name(lua_State *L)
626 {
627         NO_MAP_LOCK_REQUIRED;
628         ObjectRef *ref = checkobject(L, 1);
629         Player *player = getplayer(ref);
630         if(player == NULL){
631                 lua_pushlstring(L, "", 0);
632                 return 1;
633         }
634         // Do it
635         lua_pushstring(L, player->getName());
636         return 1;
637 }
638
639 // get_look_dir(self)
640 int ObjectRef::l_get_look_dir(lua_State *L)
641 {
642         NO_MAP_LOCK_REQUIRED;
643         ObjectRef *ref = checkobject(L, 1);
644         Player *player = getplayer(ref);
645         if(player == NULL) return 0;
646         // Do it
647         float pitch = player->getRadPitch();
648         float yaw = player->getRadYaw();
649         v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
650         push_v3f(L, v);
651         return 1;
652 }
653
654 // get_look_pitch(self)
655 int ObjectRef::l_get_look_pitch(lua_State *L)
656 {
657         NO_MAP_LOCK_REQUIRED;
658         ObjectRef *ref = checkobject(L, 1);
659         Player *player = getplayer(ref);
660         if(player == NULL) return 0;
661         // Do it
662         lua_pushnumber(L, player->getRadPitch());
663         return 1;
664 }
665
666 // get_look_yaw(self)
667 int ObjectRef::l_get_look_yaw(lua_State *L)
668 {
669         NO_MAP_LOCK_REQUIRED;
670         ObjectRef *ref = checkobject(L, 1);
671         Player *player = getplayer(ref);
672         if(player == NULL) return 0;
673         // Do it
674         lua_pushnumber(L, player->getRadYaw());
675         return 1;
676 }
677
678 // set_look_pitch(self, radians)
679 int ObjectRef::l_set_look_pitch(lua_State *L)
680 {
681         NO_MAP_LOCK_REQUIRED;
682         ObjectRef *ref = checkobject(L, 1);
683         PlayerSAO* co = getplayersao(ref);
684         if(co == NULL) return 0;
685         float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
686         // Do it
687         co->setPitch(pitch);
688         return 1;
689 }
690
691 // set_look_yaw(self, radians)
692 int ObjectRef::l_set_look_yaw(lua_State *L)
693 {
694         NO_MAP_LOCK_REQUIRED;
695         ObjectRef *ref = checkobject(L, 1);
696         PlayerSAO* co = getplayersao(ref);
697         if(co == NULL) return 0;
698         float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
699         // Do it
700         co->setYaw(yaw);
701         return 1;
702 }
703
704 // set_inventory_formspec(self, formspec)
705 int ObjectRef::l_set_inventory_formspec(lua_State *L)
706 {
707         NO_MAP_LOCK_REQUIRED;
708         ObjectRef *ref = checkobject(L, 1);
709         Player *player = getplayer(ref);
710         if(player == NULL) return 0;
711         std::string formspec = luaL_checkstring(L, 2);
712
713         player->inventory_formspec = formspec;
714         STACK_TO_SERVER(L)->reportInventoryFormspecModified(player->getName());
715         lua_pushboolean(L, true);
716         return 1;
717 }
718
719 // get_inventory_formspec(self) -> formspec
720 int ObjectRef::l_get_inventory_formspec(lua_State *L)
721 {
722         NO_MAP_LOCK_REQUIRED;
723         ObjectRef *ref = checkobject(L, 1);
724         Player *player = getplayer(ref);
725         if(player == NULL) return 0;
726
727         std::string formspec = player->inventory_formspec;
728         lua_pushlstring(L, formspec.c_str(), formspec.size());
729         return 1;
730 }
731
732 // get_player_control(self)
733 int ObjectRef::l_get_player_control(lua_State *L)
734 {
735         NO_MAP_LOCK_REQUIRED;
736         ObjectRef *ref = checkobject(L, 1);
737         Player *player = getplayer(ref);
738         if(player == NULL){
739                 lua_pushlstring(L, "", 0);
740                 return 1;
741         }
742         // Do it
743         PlayerControl control = player->getPlayerControl();
744         lua_newtable(L);
745         lua_pushboolean(L, control.up);
746         lua_setfield(L, -2, "up");
747         lua_pushboolean(L, control.down);
748         lua_setfield(L, -2, "down");
749         lua_pushboolean(L, control.left);
750         lua_setfield(L, -2, "left");
751         lua_pushboolean(L, control.right);
752         lua_setfield(L, -2, "right");
753         lua_pushboolean(L, control.jump);
754         lua_setfield(L, -2, "jump");
755         lua_pushboolean(L, control.aux1);
756         lua_setfield(L, -2, "aux1");
757         lua_pushboolean(L, control.sneak);
758         lua_setfield(L, -2, "sneak");
759         lua_pushboolean(L, control.LMB);
760         lua_setfield(L, -2, "LMB");
761         lua_pushboolean(L, control.RMB);
762         lua_setfield(L, -2, "RMB");
763         return 1;
764 }
765
766 // get_player_control_bits(self)
767 int ObjectRef::l_get_player_control_bits(lua_State *L)
768 {
769         NO_MAP_LOCK_REQUIRED;
770         ObjectRef *ref = checkobject(L, 1);
771         Player *player = getplayer(ref);
772         if(player == NULL){
773                 lua_pushlstring(L, "", 0);
774                 return 1;
775         }
776         // Do it
777         lua_pushnumber(L, player->keyPressed);
778         return 1;
779 }
780
781 // hud_add(self, form)
782 int ObjectRef::l_hud_add(lua_State *L)
783 {
784         ObjectRef *ref = checkobject(L, 1);
785         Player *player = getplayer(ref);
786         if (player == NULL)
787                 return 0;
788
789         HudElement *elem = new HudElement;
790
791         elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
792                                                                 es_HudElementType, HUD_ELEM_TEXT);
793
794         lua_getfield(L, 2, "position");
795         elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
796         lua_pop(L, 1);
797
798         lua_getfield(L, 2, "scale");
799         elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
800         lua_pop(L, 1);
801
802         elem->name   = getstringfield_default(L, 2, "name", "");
803         elem->text   = getstringfield_default(L, 2, "text", "");
804         elem->number = getintfield_default(L, 2, "number", 0);
805         elem->item   = getintfield_default(L, 2, "item", 0);
806         elem->dir    = getintfield_default(L, 2, "dir", 0);
807
808         lua_getfield(L, 2, "alignment");
809         elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
810         lua_pop(L, 1);
811
812         lua_getfield(L, 2, "offset");
813         elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
814         lua_pop(L, 1);
815
816         u32 id = STACK_TO_SERVER(L)->hudAdd(player, elem);
817         if (id == (u32)-1) {
818                 delete elem;
819                 return 0;
820         }
821
822         lua_pushnumber(L, id);
823         return 1;
824 }
825
826 // hud_remove(self, id)
827 int ObjectRef::l_hud_remove(lua_State *L)
828 {
829         ObjectRef *ref = checkobject(L, 1);
830         Player *player = getplayer(ref);
831         if (player == NULL)
832                 return 0;
833
834         u32 id = -1;
835         if (!lua_isnil(L, 2))
836                 id = lua_tonumber(L, 2);
837
838         if (!STACK_TO_SERVER(L)->hudRemove(player, id))
839                 return 0;
840
841         lua_pushboolean(L, true);
842         return 1;
843 }
844
845 // hud_change(self, id, stat, data)
846 int ObjectRef::l_hud_change(lua_State *L)
847 {
848         ObjectRef *ref = checkobject(L, 1);
849         Player *player = getplayer(ref);
850         if (player == NULL)
851                 return 0;
852
853         u32 id = -1;
854         if (!lua_isnil(L, 2))
855                 id = lua_tonumber(L, 2);
856
857         HudElementStat stat = (HudElementStat)getenumfield(L, 3, "stat",
858                                                                 es_HudElementStat, HUD_STAT_NUMBER);
859
860         if (id >= player->hud.size())
861                 return 0;
862
863         void *value = NULL;
864         HudElement *e = player->hud[id];
865         if (!e)
866                 return 0;
867
868         switch (stat) {
869                 case HUD_STAT_POS:
870                         e->pos = read_v2f(L, 4);
871                         value = &e->pos;
872                         break;
873                 case HUD_STAT_NAME:
874                         e->name = lua_tostring(L, 4);
875                         value = &e->name;
876                         break;
877                 case HUD_STAT_SCALE:
878                         e->scale = read_v2f(L, 4);
879                         value = &e->scale;
880                         break;
881                 case HUD_STAT_TEXT:
882                         e->text = lua_tostring(L, 4);
883                         value = &e->text;
884                         break;
885                 case HUD_STAT_NUMBER:
886                         e->number = lua_tonumber(L, 4);
887                         value = &e->number;
888                         break;
889                 case HUD_STAT_ITEM:
890                         e->item = lua_tonumber(L, 4);
891                         value = &e->item;
892                         break;
893                 case HUD_STAT_DIR:
894                         e->dir = lua_tonumber(L, 4);
895                         value = &e->dir;
896                 case HUD_STAT_ALIGN:
897                         e->align = read_v2f(L, 4);
898                         value = &e->align;
899                 case HUD_STAT_OFFSET:
900                         e->offset = read_v2f(L, 4);
901                         value = &e->offset;
902         }
903
904         STACK_TO_SERVER(L)->hudChange(player, id, stat, value);
905
906         lua_pushboolean(L, true);
907         return 1;
908 }
909
910 // hud_get(self, id)
911 int ObjectRef::l_hud_get(lua_State *L)
912 {
913         ObjectRef *ref = checkobject(L, 1);
914         Player *player = getplayer(ref);
915         if (player == NULL)
916                 return 0;
917
918         u32 id = lua_tonumber(L, -1);
919         if (id >= player->hud.size())
920                 return 0;
921
922         HudElement *e = player->hud[id];
923         if (!e)
924                 return 0;
925
926         lua_newtable(L);
927
928         lua_pushstring(L, es_HudElementType[(u8)e->type].str);
929         lua_setfield(L, -2, "type");
930
931         push_v2f(L, e->pos);
932         lua_setfield(L, -2, "position");
933
934         lua_pushstring(L, e->name.c_str());
935         lua_setfield(L, -2, "name");
936
937         push_v2f(L, e->scale);
938         lua_setfield(L, -2, "scale");
939
940         lua_pushstring(L, e->text.c_str());
941         lua_setfield(L, -2, "text");
942
943         lua_pushnumber(L, e->number);
944         lua_setfield(L, -2, "number");
945
946         lua_pushnumber(L, e->item);
947         lua_setfield(L, -2, "item");
948
949         lua_pushnumber(L, e->dir);
950         lua_setfield(L, -2, "dir");
951
952         return 1;
953 }
954
955 // hud_set_flags(self, flags)
956 int ObjectRef::l_hud_set_flags(lua_State *L)
957 {
958         ObjectRef *ref = checkobject(L, 1);
959         Player *player = getplayer(ref);
960         if (player == NULL)
961                 return 0;
962
963         u32 flags = 0;
964         u32 mask  = 0;
965         bool flag;
966         
967         const EnumString *esp = es_HudBuiltinElement;
968         for (int i = 0; esp[i].str; i++) {
969                 if (getboolfield(L, 2, esp[i].str, flag)) {
970                         flags |= esp[i].num * flag;
971                         mask  |= esp[i].num;
972                 }
973         }
974         if (!STACK_TO_SERVER(L)->hudSetFlags(player, flags, mask))
975                 return 0;
976
977         lua_pushboolean(L, true);
978         return 1;
979 }
980
981 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
982 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
983 {
984         ObjectRef *ref = checkobject(L, 1);
985         Player *player = getplayer(ref);
986         if (player == NULL)
987                 return 0;
988
989         s32 hotbar_itemcount = lua_tonumber(L, 2);
990
991         if (!STACK_TO_SERVER(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
992                 return 0;
993
994         lua_pushboolean(L, true);
995         return 1;
996 }
997
998 ObjectRef::ObjectRef(ServerActiveObject *object):
999         m_object(object)
1000 {
1001         //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1002 }
1003
1004 ObjectRef::~ObjectRef()
1005 {
1006         /*if(m_object)
1007                 infostream<<"ObjectRef destructing for id="
1008                                 <<m_object->getId()<<std::endl;
1009         else
1010                 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1011 }
1012
1013 // Creates an ObjectRef and leaves it on top of stack
1014 // Not callable from Lua; all references are created on the C side.
1015 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1016 {
1017         ObjectRef *o = new ObjectRef(object);
1018         //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1019         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1020         luaL_getmetatable(L, className);
1021         lua_setmetatable(L, -2);
1022 }
1023
1024 void ObjectRef::set_null(lua_State *L)
1025 {
1026         ObjectRef *o = checkobject(L, -1);
1027         o->m_object = NULL;
1028 }
1029
1030 void ObjectRef::Register(lua_State *L)
1031 {
1032         lua_newtable(L);
1033         int methodtable = lua_gettop(L);
1034         luaL_newmetatable(L, className);
1035         int metatable = lua_gettop(L);
1036
1037         lua_pushliteral(L, "__metatable");
1038         lua_pushvalue(L, methodtable);
1039         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1040
1041         lua_pushliteral(L, "__index");
1042         lua_pushvalue(L, methodtable);
1043         lua_settable(L, metatable);
1044
1045         lua_pushliteral(L, "__gc");
1046         lua_pushcfunction(L, gc_object);
1047         lua_settable(L, metatable);
1048
1049         lua_pop(L, 1);  // drop metatable
1050
1051         luaL_openlib(L, 0, methods, 0);  // fill methodtable
1052         lua_pop(L, 1);  // drop methodtable
1053
1054         // Cannot be created from Lua
1055         //lua_register(L, className, create_object);
1056 }
1057
1058 const char ObjectRef::className[] = "ObjectRef";
1059 const luaL_reg ObjectRef::methods[] = {
1060         // ServerActiveObject
1061         luamethod(ObjectRef, remove),
1062         luamethod(ObjectRef, getpos),
1063         luamethod(ObjectRef, setpos),
1064         luamethod(ObjectRef, moveto),
1065         luamethod(ObjectRef, punch),
1066         luamethod(ObjectRef, right_click),
1067         luamethod(ObjectRef, set_hp),
1068         luamethod(ObjectRef, get_hp),
1069         luamethod(ObjectRef, get_inventory),
1070         luamethod(ObjectRef, get_wield_list),
1071         luamethod(ObjectRef, get_wield_index),
1072         luamethod(ObjectRef, get_wielded_item),
1073         luamethod(ObjectRef, set_wielded_item),
1074         luamethod(ObjectRef, set_armor_groups),
1075         luamethod(ObjectRef, set_physics_override),
1076         luamethod(ObjectRef, set_animation),
1077         luamethod(ObjectRef, set_bone_position),
1078         luamethod(ObjectRef, set_attach),
1079         luamethod(ObjectRef, set_detach),
1080         luamethod(ObjectRef, set_properties),
1081         // LuaEntitySAO-only
1082         luamethod(ObjectRef, setvelocity),
1083         luamethod(ObjectRef, getvelocity),
1084         luamethod(ObjectRef, setacceleration),
1085         luamethod(ObjectRef, getacceleration),
1086         luamethod(ObjectRef, setyaw),
1087         luamethod(ObjectRef, getyaw),
1088         luamethod(ObjectRef, settexturemod),
1089         luamethod(ObjectRef, setsprite),
1090         luamethod(ObjectRef, get_entity_name),
1091         luamethod(ObjectRef, get_luaentity),
1092         // Player-only
1093         luamethod(ObjectRef, is_player),
1094         luamethod(ObjectRef, get_player_name),
1095         luamethod(ObjectRef, get_look_dir),
1096         luamethod(ObjectRef, get_look_pitch),
1097         luamethod(ObjectRef, get_look_yaw),
1098         luamethod(ObjectRef, set_look_yaw),
1099         luamethod(ObjectRef, set_look_pitch),
1100         luamethod(ObjectRef, set_inventory_formspec),
1101         luamethod(ObjectRef, get_inventory_formspec),
1102         luamethod(ObjectRef, get_player_control),
1103         luamethod(ObjectRef, get_player_control_bits),
1104         luamethod(ObjectRef, hud_add),
1105         luamethod(ObjectRef, hud_remove),
1106         luamethod(ObjectRef, hud_change),
1107         luamethod(ObjectRef, hud_get),
1108         luamethod(ObjectRef, hud_set_flags),
1109         luamethod(ObjectRef, hud_set_hotbar_itemcount),
1110         {0,0}
1111 };
1112
1113 REGISTER_LUA_REF(ObjectRef)