]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_object.cpp
Adding LuaError on attempt to assign vectors with values out of range
[dragonfireclient.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 "lua_api/l_object.h"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_inventory.h"
23 #include "lua_api/l_item.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
26 #include "log.h"
27 #include "tool.h"
28 #include "serverobject.h"
29 #include "content_sao.h"
30 #include "server.h"
31 #include "hud.h"
32 #include "scripting_game.h"
33
34 struct EnumString es_HudElementType[] =
35 {
36         {HUD_ELEM_IMAGE,     "image"},
37         {HUD_ELEM_TEXT,      "text"},
38         {HUD_ELEM_STATBAR,   "statbar"},
39         {HUD_ELEM_INVENTORY, "inventory"},
40         {HUD_ELEM_WAYPOINT,  "waypoint"},
41 {0, NULL},
42 };
43
44 struct EnumString es_HudElementStat[] =
45 {
46         {HUD_STAT_POS,    "position"},
47         {HUD_STAT_POS,    "pos"}, /* Deprecated, only for compatibility's sake */
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         {HUD_STAT_WORLD_POS, "world_pos"},
57         {0, NULL},
58 };
59
60 struct EnumString es_HudBuiltinElement[] =
61 {
62         {HUD_FLAG_HOTBAR_VISIBLE,    "hotbar"},
63         {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
64         {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
65         {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
66         {HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
67         {HUD_FLAG_MINIMAP_VISIBLE,   "minimap"},
68         {0, NULL},
69 };
70
71 /*
72         ObjectRef
73 */
74
75
76 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
77 {
78         luaL_checktype(L, narg, LUA_TUSERDATA);
79         void *ud = luaL_checkudata(L, narg, className);
80         if (!ud) luaL_typerror(L, narg, className);
81         return *(ObjectRef**)ud;  // unbox pointer
82 }
83
84 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
85 {
86         ServerActiveObject *co = ref->m_object;
87         return co;
88 }
89
90 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
91 {
92         ServerActiveObject *obj = getobject(ref);
93         if (obj == NULL)
94                 return NULL;
95         if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
96                 return NULL;
97         return (LuaEntitySAO*)obj;
98 }
99
100 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
101 {
102         ServerActiveObject *obj = getobject(ref);
103         if (obj == NULL)
104                 return NULL;
105         if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
106                 return NULL;
107         return (PlayerSAO*)obj;
108 }
109
110 RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
111 {
112         PlayerSAO *playersao = getplayersao(ref);
113         if (playersao == NULL)
114                 return NULL;
115         return playersao->getPlayer();
116 }
117
118 // Exported functions
119
120 // garbage collector
121 int ObjectRef::gc_object(lua_State *L) {
122         ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
123         //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
124         delete o;
125         return 0;
126 }
127
128 // remove(self)
129 int ObjectRef::l_remove(lua_State *L)
130 {
131         GET_ENV_PTR;
132
133         ObjectRef *ref = checkobject(L, 1);
134         ServerActiveObject *co = getobject(ref);
135         if (co == NULL)
136                 return 0;
137         if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
138                 return 0;
139
140         UNORDERED_SET<int> child_ids = co->getAttachmentChildIds();
141         UNORDERED_SET<int>::iterator it;
142         for (it = child_ids.begin(); it != child_ids.end(); ++it) {
143                 ServerActiveObject *child = env->getActiveObject(*it);
144                 child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
145         }
146
147         verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
148         co->m_removed = true;
149         return 0;
150 }
151
152 // getpos(self)
153 // returns: {x=num, y=num, z=num}
154 int ObjectRef::l_getpos(lua_State *L)
155 {
156         NO_MAP_LOCK_REQUIRED;
157         ObjectRef *ref = checkobject(L, 1);
158         ServerActiveObject *co = getobject(ref);
159         if (co == NULL) return 0;
160         v3f pos = co->getBasePosition() / BS;
161         lua_newtable(L);
162         lua_pushnumber(L, pos.X);
163         lua_setfield(L, -2, "x");
164         lua_pushnumber(L, pos.Y);
165         lua_setfield(L, -2, "y");
166         lua_pushnumber(L, pos.Z);
167         lua_setfield(L, -2, "z");
168         return 1;
169 }
170
171 // setpos(self, pos)
172 int ObjectRef::l_setpos(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         // Do it
182         co->setPos(pos);
183         return 0;
184 }
185
186 // moveto(self, pos, continuous=false)
187 int ObjectRef::l_moveto(lua_State *L)
188 {
189         NO_MAP_LOCK_REQUIRED;
190         ObjectRef *ref = checkobject(L, 1);
191         //LuaEntitySAO *co = getluaobject(ref);
192         ServerActiveObject *co = getobject(ref);
193         if (co == NULL) return 0;
194         // pos
195         v3f pos = checkFloatPos(L, 2);
196         // continuous
197         bool continuous = lua_toboolean(L, 3);
198         // Do it
199         co->moveTo(pos, continuous);
200         return 0;
201 }
202
203 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
204 int ObjectRef::l_punch(lua_State *L)
205 {
206         NO_MAP_LOCK_REQUIRED;
207         ObjectRef *ref = checkobject(L, 1);
208         ObjectRef *puncher_ref = checkobject(L, 2);
209         ServerActiveObject *co = getobject(ref);
210         ServerActiveObject *puncher = getobject(puncher_ref);
211         if (co == NULL) return 0;
212         if (puncher == NULL) return 0;
213         v3f dir;
214         if (lua_type(L, 5) != LUA_TTABLE)
215                 dir = co->getBasePosition() - puncher->getBasePosition();
216         else
217                 dir = read_v3f(L, 5);
218         float time_from_last_punch = 1000000;
219         if (lua_isnumber(L, 3))
220                 time_from_last_punch = lua_tonumber(L, 3);
221         ToolCapabilities toolcap = read_tool_capabilities(L, 4);
222         dir.normalize();
223
224         s16 src_original_hp = co->getHP();
225         s16 dst_origin_hp = puncher->getHP();
226
227         // Do it
228         co->punch(dir, &toolcap, puncher, time_from_last_punch);
229
230         // If the punched is a player, and its HP changed
231         if (src_original_hp != co->getHP() &&
232                         co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
233                 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
234         }
235
236         // If the puncher is a player, and its HP changed
237         if (dst_origin_hp != puncher->getHP() &&
238                         puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
239                 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher);
240         }
241         return 0;
242 }
243
244 // right_click(self, clicker); clicker = an another ObjectRef
245 int ObjectRef::l_right_click(lua_State *L)
246 {
247         NO_MAP_LOCK_REQUIRED;
248         ObjectRef *ref = checkobject(L, 1);
249         ObjectRef *ref2 = checkobject(L, 2);
250         ServerActiveObject *co = getobject(ref);
251         ServerActiveObject *co2 = getobject(ref2);
252         if (co == NULL) return 0;
253         if (co2 == NULL) return 0;
254         // Do it
255         co->rightClick(co2);
256         return 0;
257 }
258
259 // set_hp(self, hp)
260 // hp = number of hitpoints (2 * number of hearts)
261 // returns: nil
262 int ObjectRef::l_set_hp(lua_State *L)
263 {
264         NO_MAP_LOCK_REQUIRED;
265         ObjectRef *ref = checkobject(L, 1);
266         luaL_checknumber(L, 2);
267         ServerActiveObject *co = getobject(ref);
268         if (co == NULL) return 0;
269         int hp = lua_tonumber(L, 2);
270         /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
271                         <<" hp="<<hp<<std::endl;*/
272         // Do it
273         co->setHP(hp);
274         if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
275                 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
276
277         // Return
278         return 0;
279 }
280
281 // get_hp(self)
282 // returns: number of hitpoints (2 * number of hearts)
283 // 0 if not applicable to this type of object
284 int ObjectRef::l_get_hp(lua_State *L)
285 {
286         NO_MAP_LOCK_REQUIRED;
287         ObjectRef *ref = checkobject(L, 1);
288         ServerActiveObject *co = getobject(ref);
289         if (co == NULL) {
290                 // Default hp is 1
291                 lua_pushnumber(L, 1);
292                 return 1;
293         }
294         int hp = co->getHP();
295         /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
296                         <<" hp="<<hp<<std::endl;*/
297         // Return
298         lua_pushnumber(L, hp);
299         return 1;
300 }
301
302 // get_inventory(self)
303 int ObjectRef::l_get_inventory(lua_State *L)
304 {
305         NO_MAP_LOCK_REQUIRED;
306         ObjectRef *ref = checkobject(L, 1);
307         ServerActiveObject *co = getobject(ref);
308         if (co == NULL) return 0;
309         // Do it
310         InventoryLocation loc = co->getInventoryLocation();
311         if (getServer(L)->getInventory(loc) != NULL)
312                 InvRef::create(L, loc);
313         else
314                 lua_pushnil(L); // An object may have no inventory (nil)
315         return 1;
316 }
317
318 // get_wield_list(self)
319 int ObjectRef::l_get_wield_list(lua_State *L)
320 {
321         NO_MAP_LOCK_REQUIRED;
322         ObjectRef *ref = checkobject(L, 1);
323         ServerActiveObject *co = getobject(ref);
324         if (co == NULL) return 0;
325         // Do it
326         lua_pushstring(L, co->getWieldList().c_str());
327         return 1;
328 }
329
330 // get_wield_index(self)
331 int ObjectRef::l_get_wield_index(lua_State *L)
332 {
333         NO_MAP_LOCK_REQUIRED;
334         ObjectRef *ref = checkobject(L, 1);
335         ServerActiveObject *co = getobject(ref);
336         if (co == NULL) return 0;
337         // Do it
338         lua_pushinteger(L, co->getWieldIndex() + 1);
339         return 1;
340 }
341
342 // get_wielded_item(self)
343 int ObjectRef::l_get_wielded_item(lua_State *L)
344 {
345         NO_MAP_LOCK_REQUIRED;
346         ObjectRef *ref = checkobject(L, 1);
347         ServerActiveObject *co = getobject(ref);
348         if (co == NULL) {
349                 // Empty ItemStack
350                 LuaItemStack::create(L, ItemStack());
351                 return 1;
352         }
353         // Do it
354         LuaItemStack::create(L, co->getWieldedItem());
355         return 1;
356 }
357
358 // set_wielded_item(self, itemstack or itemstring or table or nil)
359 int ObjectRef::l_set_wielded_item(lua_State *L)
360 {
361         NO_MAP_LOCK_REQUIRED;
362         ObjectRef *ref = checkobject(L, 1);
363         ServerActiveObject *co = getobject(ref);
364         if (co == NULL) return 0;
365         // Do it
366         ItemStack item = read_item(L, 2, getServer(L));
367         bool success = co->setWieldedItem(item);
368         if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
369                 getServer(L)->SendInventory(((PlayerSAO*)co));
370         }
371         lua_pushboolean(L, success);
372         return 1;
373 }
374
375 // set_armor_groups(self, groups)
376 int ObjectRef::l_set_armor_groups(lua_State *L)
377 {
378         NO_MAP_LOCK_REQUIRED;
379         ObjectRef *ref = checkobject(L, 1);
380         ServerActiveObject *co = getobject(ref);
381         if (co == NULL) return 0;
382         // Do it
383         ItemGroupList groups;
384         read_groups(L, 2, groups);
385         co->setArmorGroups(groups);
386         return 0;
387 }
388
389 // get_armor_groups(self)
390 int ObjectRef::l_get_armor_groups(lua_State *L)
391 {
392         NO_MAP_LOCK_REQUIRED;
393         ObjectRef *ref = checkobject(L, 1);
394         ServerActiveObject *co = getobject(ref);
395         if (co == NULL)
396                 return 0;
397         // Do it
398         ItemGroupList groups = co->getArmorGroups();
399         push_groups(L, groups);
400         return 1;
401 }
402
403 // set_physics_override(self, physics_override_speed, physics_override_jump,
404 //                      physics_override_gravity, sneak, sneak_glitch)
405 int ObjectRef::l_set_physics_override(lua_State *L)
406 {
407         NO_MAP_LOCK_REQUIRED;
408         ObjectRef *ref = checkobject(L, 1);
409         PlayerSAO *co = (PlayerSAO *) getobject(ref);
410         if (co == NULL) return 0;
411         // Do it
412         if (lua_istable(L, 2)) {
413                 co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
414                 co->m_physics_override_jump = getfloatfield_default(L, 2, "jump", co->m_physics_override_jump);
415                 co->m_physics_override_gravity = getfloatfield_default(L, 2, "gravity", co->m_physics_override_gravity);
416                 co->m_physics_override_sneak = getboolfield_default(L, 2, "sneak", co->m_physics_override_sneak);
417                 co->m_physics_override_sneak_glitch = getboolfield_default(L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
418                 co->m_physics_override_sent = false;
419         } else {
420                 // old, non-table format
421                 if (!lua_isnil(L, 2)) {
422                         co->m_physics_override_speed = lua_tonumber(L, 2);
423                         co->m_physics_override_sent = false;
424                 }
425                 if (!lua_isnil(L, 3)) {
426                         co->m_physics_override_jump = lua_tonumber(L, 3);
427                         co->m_physics_override_sent = false;
428                 }
429                 if (!lua_isnil(L, 4)) {
430                         co->m_physics_override_gravity = lua_tonumber(L, 4);
431                         co->m_physics_override_sent = false;
432                 }
433         }
434         return 0;
435 }
436
437 // get_physics_override(self)
438 int ObjectRef::l_get_physics_override(lua_State *L)
439 {
440         NO_MAP_LOCK_REQUIRED;
441         ObjectRef *ref = checkobject(L, 1);
442         PlayerSAO *co = (PlayerSAO *)getobject(ref);
443         if (co == NULL)
444                 return 0;
445         // Do it
446         lua_newtable(L);
447         lua_pushnumber(L, co->m_physics_override_speed);
448         lua_setfield(L, -2, "speed");
449         lua_pushnumber(L, co->m_physics_override_jump);
450         lua_setfield(L, -2, "jump");
451         lua_pushnumber(L, co->m_physics_override_gravity);
452         lua_setfield(L, -2, "gravity");
453         lua_pushboolean(L, co->m_physics_override_sneak);
454         lua_setfield(L, -2, "sneak");
455         lua_pushboolean(L, co->m_physics_override_sneak_glitch);
456         lua_setfield(L, -2, "sneak_glitch");
457         return 1;
458 }
459
460 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
461 int ObjectRef::l_set_animation(lua_State *L)
462 {
463         NO_MAP_LOCK_REQUIRED;
464         ObjectRef *ref = checkobject(L, 1);
465         ServerActiveObject *co = getobject(ref);
466         if (co == NULL) return 0;
467         // Do it
468         v2f frames = v2f(1, 1);
469         if (!lua_isnil(L, 2))
470                 frames = read_v2f(L, 2);
471         float frame_speed = 15;
472         if (!lua_isnil(L, 3))
473                 frame_speed = lua_tonumber(L, 3);
474         float frame_blend = 0;
475         if (!lua_isnil(L, 4))
476                 frame_blend = lua_tonumber(L, 4);
477         bool frame_loop = true;
478         if (lua_isboolean(L, 5))
479                 frame_loop = lua_toboolean(L, 5);
480         co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
481         return 0;
482 }
483
484 // get_animation(self)
485 int ObjectRef::l_get_animation(lua_State *L)
486 {
487         NO_MAP_LOCK_REQUIRED;
488         ObjectRef *ref = checkobject(L, 1);
489         ServerActiveObject *co = getobject(ref);
490         if (co == NULL)
491                 return 0;
492         // Do it
493         v2f frames = v2f(1,1);
494         float frame_speed = 15;
495         float frame_blend = 0;
496         bool frame_loop = true;
497         co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
498
499         push_v2f(L, frames);
500         lua_pushnumber(L, frame_speed);
501         lua_pushnumber(L, frame_blend);
502         lua_pushboolean(L, frame_loop);
503         return 4;
504 }
505
506 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
507 int ObjectRef::l_set_local_animation(lua_State *L)
508 {
509         NO_MAP_LOCK_REQUIRED;
510         ObjectRef *ref = checkobject(L, 1);
511         RemotePlayer *player = getplayer(ref);
512         if (player == NULL)
513                 return 0;
514         // Do it
515         v2s32 frames[4];
516         for (int i=0;i<4;i++) {
517                 if (!lua_isnil(L, 2+1))
518                         frames[i] = read_v2s32(L, 2+i);
519         }
520         float frame_speed = 30;
521         if (!lua_isnil(L, 6))
522                 frame_speed = lua_tonumber(L, 6);
523
524         if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
525                 return 0;
526
527         lua_pushboolean(L, true);
528         return 0;
529 }
530
531 // get_local_animation(self)
532 int ObjectRef::l_get_local_animation(lua_State *L)
533 {
534         NO_MAP_LOCK_REQUIRED
535         ObjectRef *ref = checkobject(L, 1);
536         RemotePlayer *player = getplayer(ref);
537         if (player == NULL)
538                 return 0;
539
540         v2s32 frames[4];
541         float frame_speed;
542         player->getLocalAnimations(frames, &frame_speed);
543
544         for (int i = 0; i < 4; i++) {
545                 push_v2s32(L, frames[i]);
546         }
547
548         lua_pushnumber(L, frame_speed);
549         return 5;
550 }
551
552 // set_eye_offset(self, v3f first pv, v3f third pv)
553 int ObjectRef::l_set_eye_offset(lua_State *L)
554 {
555         NO_MAP_LOCK_REQUIRED;
556         ObjectRef *ref = checkobject(L, 1);
557         RemotePlayer *player = getplayer(ref);
558         if (player == NULL)
559                 return 0;
560         // Do it
561         v3f offset_first = v3f(0, 0, 0);
562         v3f offset_third = v3f(0, 0, 0);
563
564         if (!lua_isnil(L, 2))
565                 offset_first = read_v3f(L, 2);
566         if (!lua_isnil(L, 3))
567                 offset_third = read_v3f(L, 3);
568
569         // Prevent abuse of offset values (keep player always visible)
570         offset_third.X = rangelim(offset_third.X,-10,10);
571         offset_third.Z = rangelim(offset_third.Z,-5,5);
572         /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
573         offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
574
575         if (!getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third))
576                 return 0;
577
578         lua_pushboolean(L, true);
579         return 0;
580 }
581
582 // get_eye_offset(self)
583 int ObjectRef::l_get_eye_offset(lua_State *L)
584 {
585         NO_MAP_LOCK_REQUIRED;
586         ObjectRef *ref = checkobject(L, 1);
587         RemotePlayer *player = getplayer(ref);
588         if (player == NULL)
589                 return 0;
590         // Do it
591         push_v3f(L, player->eye_offset_first);
592         push_v3f(L, player->eye_offset_third);
593         return 2;
594 }
595
596 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
597 int ObjectRef::l_set_bone_position(lua_State *L)
598 {
599         NO_MAP_LOCK_REQUIRED;
600         ObjectRef *ref = checkobject(L, 1);
601         ServerActiveObject *co = getobject(ref);
602         if (co == NULL) return 0;
603         // Do it
604         std::string bone = "";
605         if (!lua_isnil(L, 2))
606                 bone = lua_tostring(L, 2);
607         v3f position = v3f(0, 0, 0);
608         if (!lua_isnil(L, 3))
609                 position = check_v3f(L, 3);
610         v3f rotation = v3f(0, 0, 0);
611         if (!lua_isnil(L, 4))
612                 rotation = check_v3f(L, 4);
613         co->setBonePosition(bone, position, rotation);
614         return 0;
615 }
616
617 // get_bone_position(self, bone)
618 int ObjectRef::l_get_bone_position(lua_State *L)
619 {
620         NO_MAP_LOCK_REQUIRED;
621         ObjectRef *ref = checkobject(L, 1);
622         ServerActiveObject *co = getobject(ref);
623         if (co == NULL)
624                 return 0;
625         // Do it
626         std::string bone = "";
627         if (!lua_isnil(L, 2))
628                 bone = lua_tostring(L, 2);
629
630         v3f position = v3f(0, 0, 0);
631         v3f rotation = v3f(0, 0, 0);
632         co->getBonePosition(bone, &position, &rotation);
633
634         push_v3f(L, position);
635         push_v3f(L, rotation);
636         return 2;
637 }
638
639 // set_attach(self, parent, bone, position, rotation)
640 int ObjectRef::l_set_attach(lua_State *L)
641 {
642         GET_ENV_PTR;
643
644         ObjectRef *ref = checkobject(L, 1);
645         ObjectRef *parent_ref = checkobject(L, 2);
646         ServerActiveObject *co = getobject(ref);
647         ServerActiveObject *parent = getobject(parent_ref);
648         if (co == NULL)
649                 return 0;
650         if (parent == NULL)
651                 return 0;
652         // Do it
653         int parent_id = 0;
654         std::string bone = "";
655         v3f position = v3f(0, 0, 0);
656         v3f rotation = v3f(0, 0, 0);
657         co->getAttachment(&parent_id, &bone, &position, &rotation);
658         if (parent_id) {
659                 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
660                 old_parent->removeAttachmentChild(co->getId());
661         }
662
663         bone = "";
664         if (!lua_isnil(L, 3))
665                 bone = lua_tostring(L, 3);
666         position = v3f(0, 0, 0);
667         if (!lua_isnil(L, 4))
668                 position = read_v3f(L, 4);
669         rotation = v3f(0, 0, 0);
670         if (!lua_isnil(L, 5))
671                 rotation = read_v3f(L, 5);
672         co->setAttachment(parent->getId(), bone, position, rotation);
673         parent->addAttachmentChild(co->getId());
674         return 0;
675 }
676
677 // get_attach(self)
678 int ObjectRef::l_get_attach(lua_State *L)
679 {
680         GET_ENV_PTR;
681
682         ObjectRef *ref = checkobject(L, 1);
683         ServerActiveObject *co = getobject(ref);
684         if (co == NULL)
685                 return 0;
686
687         // Do it
688         int parent_id = 0;
689         std::string bone = "";
690         v3f position = v3f(0, 0, 0);
691         v3f rotation = v3f(0, 0, 0);
692         co->getAttachment(&parent_id, &bone, &position, &rotation);
693         if (!parent_id)
694                 return 0;
695         ServerActiveObject *parent = env->getActiveObject(parent_id);
696
697         getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
698         lua_pushlstring(L, bone.c_str(), bone.size());
699         push_v3f(L, position);
700         push_v3f(L, rotation);
701         return 4;
702 }
703
704 // set_detach(self)
705 int ObjectRef::l_set_detach(lua_State *L)
706 {
707         GET_ENV_PTR;
708
709         ObjectRef *ref = checkobject(L, 1);
710         ServerActiveObject *co = getobject(ref);
711         if (co == NULL)
712                 return 0;
713
714         int parent_id = 0;
715         std::string bone = "";
716         v3f position;
717         v3f rotation;
718         co->getAttachment(&parent_id, &bone, &position, &rotation);
719         ServerActiveObject *parent = NULL;
720         if (parent_id)
721                 parent = env->getActiveObject(parent_id);
722
723         // Do it
724         co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
725         if (parent != NULL)
726                 parent->removeAttachmentChild(co->getId());
727         return 0;
728 }
729
730 // set_properties(self, properties)
731 int ObjectRef::l_set_properties(lua_State *L)
732 {
733         NO_MAP_LOCK_REQUIRED;
734         ObjectRef *ref = checkobject(L, 1);
735         ServerActiveObject *co = getobject(ref);
736         if (co == NULL) return 0;
737         ObjectProperties *prop = co->accessObjectProperties();
738         if (!prop)
739                 return 0;
740         read_object_properties(L, 2, prop);
741         co->notifyObjectPropertiesModified();
742         return 0;
743 }
744
745 // get_properties(self)
746 int ObjectRef::l_get_properties(lua_State *L)
747 {
748         NO_MAP_LOCK_REQUIRED;
749         ObjectRef *ref = checkobject(L, 1);
750         ServerActiveObject *co = getobject(ref);
751         if (co == NULL)
752                 return 0;
753         ObjectProperties *prop = co->accessObjectProperties();
754         if (!prop)
755                 return 0;
756         push_object_properties(L, prop);
757         return 1;
758 }
759
760 // is_player(self)
761 int ObjectRef::l_is_player(lua_State *L)
762 {
763         NO_MAP_LOCK_REQUIRED;
764         ObjectRef *ref = checkobject(L, 1);
765         RemotePlayer *player = getplayer(ref);
766         lua_pushboolean(L, (player != NULL));
767         return 1;
768 }
769
770 // set_nametag_attributes(self, attributes)
771 int ObjectRef::l_set_nametag_attributes(lua_State *L)
772 {
773         NO_MAP_LOCK_REQUIRED;
774         ObjectRef *ref = checkobject(L, 1);
775         ServerActiveObject *co = getobject(ref);
776
777         if (co == NULL)
778                 return 0;
779         ObjectProperties *prop = co->accessObjectProperties();
780         if (!prop)
781                 return 0;
782
783         lua_getfield(L, 2, "color");
784         if (!lua_isnil(L, -1)) {
785                 video::SColor color = prop->nametag_color;
786                 read_color(L, -1, &color);
787                 prop->nametag_color = color;
788         }
789         lua_pop(L, 1);
790
791         std::string nametag = getstringfield_default(L, 2, "text", "");
792         if (nametag != "")
793                 prop->nametag = nametag;
794
795         co->notifyObjectPropertiesModified();
796         lua_pushboolean(L, true);
797         return 1;
798 }
799
800 // get_nametag_attributes(self)
801 int ObjectRef::l_get_nametag_attributes(lua_State *L)
802 {
803         NO_MAP_LOCK_REQUIRED;
804         ObjectRef *ref = checkobject(L, 1);
805         ServerActiveObject *co = getobject(ref);
806
807         if (co == NULL)
808                 return 0;
809         ObjectProperties *prop = co->accessObjectProperties();
810         if (!prop)
811                 return 0;
812
813         video::SColor color = prop->nametag_color;
814
815         lua_newtable(L);
816         push_ARGB8(L, color);
817         lua_setfield(L, -2, "color");
818         lua_pushstring(L, prop->nametag.c_str());
819         lua_setfield(L, -2, "text");
820         return 1;
821 }
822
823 /* LuaEntitySAO-only */
824
825 // setvelocity(self, {x=num, y=num, z=num})
826 int ObjectRef::l_setvelocity(lua_State *L)
827 {
828         NO_MAP_LOCK_REQUIRED;
829         ObjectRef *ref = checkobject(L, 1);
830         LuaEntitySAO *co = getluaobject(ref);
831         if (co == NULL) return 0;
832         v3f pos = checkFloatPos(L, 2);
833         // Do it
834         co->setVelocity(pos);
835         return 0;
836 }
837
838 // getvelocity(self)
839 int ObjectRef::l_getvelocity(lua_State *L)
840 {
841         NO_MAP_LOCK_REQUIRED;
842         ObjectRef *ref = checkobject(L, 1);
843         LuaEntitySAO *co = getluaobject(ref);
844         if (co == NULL) return 0;
845         // Do it
846         v3f v = co->getVelocity();
847         pushFloatPos(L, v);
848         return 1;
849 }
850
851 // setacceleration(self, {x=num, y=num, z=num})
852 int ObjectRef::l_setacceleration(lua_State *L)
853 {
854         NO_MAP_LOCK_REQUIRED;
855         ObjectRef *ref = checkobject(L, 1);
856         LuaEntitySAO *co = getluaobject(ref);
857         if (co == NULL) return 0;
858         // pos
859         v3f pos = checkFloatPos(L, 2);
860         // Do it
861         co->setAcceleration(pos);
862         return 0;
863 }
864
865 // getacceleration(self)
866 int ObjectRef::l_getacceleration(lua_State *L)
867 {
868         NO_MAP_LOCK_REQUIRED;
869         ObjectRef *ref = checkobject(L, 1);
870         LuaEntitySAO *co = getluaobject(ref);
871         if (co == NULL) return 0;
872         // Do it
873         v3f v = co->getAcceleration();
874         pushFloatPos(L, v);
875         return 1;
876 }
877
878 // setyaw(self, radians)
879 int ObjectRef::l_setyaw(lua_State *L)
880 {
881         NO_MAP_LOCK_REQUIRED;
882         ObjectRef *ref = checkobject(L, 1);
883         LuaEntitySAO *co = getluaobject(ref);
884         if (co == NULL) return 0;
885         float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
886         // Do it
887         co->setYaw(yaw);
888         return 0;
889 }
890
891 // getyaw(self)
892 int ObjectRef::l_getyaw(lua_State *L)
893 {
894         NO_MAP_LOCK_REQUIRED;
895         ObjectRef *ref = checkobject(L, 1);
896         LuaEntitySAO *co = getluaobject(ref);
897         if (co == NULL) return 0;
898         // Do it
899         float yaw = co->getYaw() * core::DEGTORAD;
900         lua_pushnumber(L, yaw);
901         return 1;
902 }
903
904 // settexturemod(self, mod)
905 int ObjectRef::l_settexturemod(lua_State *L)
906 {
907         NO_MAP_LOCK_REQUIRED;
908         ObjectRef *ref = checkobject(L, 1);
909         LuaEntitySAO *co = getluaobject(ref);
910         if (co == NULL) return 0;
911         // Do it
912         std::string mod = luaL_checkstring(L, 2);
913         co->setTextureMod(mod);
914         return 0;
915 }
916
917 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
918 //           select_horiz_by_yawpitch=false)
919 int ObjectRef::l_setsprite(lua_State *L)
920 {
921         NO_MAP_LOCK_REQUIRED;
922         ObjectRef *ref = checkobject(L, 1);
923         LuaEntitySAO *co = getluaobject(ref);
924         if (co == NULL) return 0;
925         // Do it
926         v2s16 p(0,0);
927         if (!lua_isnil(L, 2))
928                 p = read_v2s16(L, 2);
929         int num_frames = 1;
930         if (!lua_isnil(L, 3))
931                 num_frames = lua_tonumber(L, 3);
932         float framelength = 0.2;
933         if (!lua_isnil(L, 4))
934                 framelength = lua_tonumber(L, 4);
935         bool select_horiz_by_yawpitch = false;
936         if (!lua_isnil(L, 5))
937                 select_horiz_by_yawpitch = lua_toboolean(L, 5);
938         co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
939         return 0;
940 }
941
942 // DEPRECATED
943 // get_entity_name(self)
944 int ObjectRef::l_get_entity_name(lua_State *L)
945 {
946         NO_MAP_LOCK_REQUIRED;
947         ObjectRef *ref = checkobject(L, 1);
948         LuaEntitySAO *co = getluaobject(ref);
949         log_deprecated(L,"Deprecated call to \"get_entity_name");
950         if (co == NULL) return 0;
951         // Do it
952         std::string name = co->getName();
953         lua_pushstring(L, name.c_str());
954         return 1;
955 }
956
957 // get_luaentity(self)
958 int ObjectRef::l_get_luaentity(lua_State *L)
959 {
960         NO_MAP_LOCK_REQUIRED;
961         ObjectRef *ref = checkobject(L, 1);
962         LuaEntitySAO *co = getluaobject(ref);
963         if (co == NULL) return 0;
964         // Do it
965         luaentity_get(L, co->getId());
966         return 1;
967 }
968
969 /* Player-only */
970
971 // is_player_connected(self)
972 int ObjectRef::l_is_player_connected(lua_State *L)
973 {
974         NO_MAP_LOCK_REQUIRED;
975         ObjectRef *ref = checkobject(L, 1);
976         RemotePlayer *player = getplayer(ref);
977         lua_pushboolean(L, (player != NULL && player->peer_id != 0));
978         return 1;
979 }
980
981 // get_player_name(self)
982 int ObjectRef::l_get_player_name(lua_State *L)
983 {
984         NO_MAP_LOCK_REQUIRED;
985         ObjectRef *ref = checkobject(L, 1);
986         RemotePlayer *player = getplayer(ref);
987         if (player == NULL) {
988                 lua_pushlstring(L, "", 0);
989                 return 1;
990         }
991         // Do it
992         lua_pushstring(L, player->getName());
993         return 1;
994 }
995
996 // get_player_velocity(self)
997 int ObjectRef::l_get_player_velocity(lua_State *L)
998 {
999         NO_MAP_LOCK_REQUIRED;
1000         ObjectRef *ref = checkobject(L, 1);
1001         RemotePlayer *player = getplayer(ref);
1002         if (player == NULL) {
1003                 lua_pushnil(L);
1004                 return 1;
1005         }
1006         // Do it
1007         push_v3f(L, player->getSpeed() / BS);
1008         return 1;
1009 }
1010
1011 // get_look_dir(self)
1012 int ObjectRef::l_get_look_dir(lua_State *L)
1013 {
1014         NO_MAP_LOCK_REQUIRED;
1015         ObjectRef *ref = checkobject(L, 1);
1016         RemotePlayer *player = getplayer(ref);
1017         if (player == NULL) return 0;
1018         // Do it
1019         float pitch = player->getRadPitchDep();
1020         float yaw = player->getRadYawDep();
1021         v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
1022         push_v3f(L, v);
1023         return 1;
1024 }
1025
1026 // DEPRECATED
1027 // get_look_pitch(self)
1028 int ObjectRef::l_get_look_pitch(lua_State *L)
1029 {
1030         NO_MAP_LOCK_REQUIRED;
1031
1032         log_deprecated(L,
1033                 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1034
1035         ObjectRef *ref = checkobject(L, 1);
1036         RemotePlayer *player = getplayer(ref);
1037         if (player == NULL) return 0;
1038         // Do it
1039         lua_pushnumber(L, player->getRadPitchDep());
1040         return 1;
1041 }
1042
1043 // DEPRECATED
1044 // get_look_yaw(self)
1045 int ObjectRef::l_get_look_yaw(lua_State *L)
1046 {
1047         NO_MAP_LOCK_REQUIRED;
1048
1049         log_deprecated(L,
1050                 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1051
1052         ObjectRef *ref = checkobject(L, 1);
1053         RemotePlayer *player = getplayer(ref);
1054         if (player == NULL) return 0;
1055         // Do it
1056         lua_pushnumber(L, player->getRadYawDep());
1057         return 1;
1058 }
1059
1060 // get_look_pitch2(self)
1061 int ObjectRef::l_get_look_vertical(lua_State *L)
1062 {
1063         NO_MAP_LOCK_REQUIRED;
1064         ObjectRef *ref = checkobject(L, 1);
1065         RemotePlayer *player = getplayer(ref);
1066         if (player == NULL) return 0;
1067         // Do it
1068         lua_pushnumber(L, player->getRadPitch());
1069         return 1;
1070 }
1071
1072 // get_look_yaw2(self)
1073 int ObjectRef::l_get_look_horizontal(lua_State *L)
1074 {
1075         NO_MAP_LOCK_REQUIRED;
1076         ObjectRef *ref = checkobject(L, 1);
1077         RemotePlayer *player = getplayer(ref);
1078         if (player == NULL) return 0;
1079         // Do it
1080         lua_pushnumber(L, player->getRadYaw());
1081         return 1;
1082 }
1083
1084 // set_look_vertical(self, radians)
1085 int ObjectRef::l_set_look_vertical(lua_State *L)
1086 {
1087         NO_MAP_LOCK_REQUIRED;
1088         ObjectRef *ref = checkobject(L, 1);
1089         PlayerSAO* co = getplayersao(ref);
1090         if (co == NULL) return 0;
1091         float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1092         // Do it
1093         co->setPitch(pitch);
1094         return 1;
1095 }
1096
1097 // set_look_horizontal(self, radians)
1098 int ObjectRef::l_set_look_horizontal(lua_State *L)
1099 {
1100         NO_MAP_LOCK_REQUIRED;
1101         ObjectRef *ref = checkobject(L, 1);
1102         PlayerSAO* co = getplayersao(ref);
1103         if (co == NULL) return 0;
1104         float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1105         // Do it
1106         co->setYaw(yaw);
1107         return 1;
1108 }
1109
1110 // DEPRECATED
1111 // set_look_pitch(self, radians)
1112 int ObjectRef::l_set_look_pitch(lua_State *L)
1113 {
1114         NO_MAP_LOCK_REQUIRED;
1115
1116         log_deprecated(L,
1117                 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1118
1119         ObjectRef *ref = checkobject(L, 1);
1120         PlayerSAO* co = getplayersao(ref);
1121         if (co == NULL) return 0;
1122         float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1123         // Do it
1124         co->setPitch(pitch);
1125         return 1;
1126 }
1127
1128 // DEPRECATED
1129 // set_look_yaw(self, radians)
1130 int ObjectRef::l_set_look_yaw(lua_State *L)
1131 {
1132         NO_MAP_LOCK_REQUIRED;
1133
1134         log_deprecated(L,
1135                 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1136
1137         ObjectRef *ref = checkobject(L, 1);
1138         PlayerSAO* co = getplayersao(ref);
1139         if (co == NULL) return 0;
1140         float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1141         // Do it
1142         co->setYaw(yaw);
1143         return 1;
1144 }
1145
1146 // set_breath(self, breath)
1147 int ObjectRef::l_set_breath(lua_State *L)
1148 {
1149         NO_MAP_LOCK_REQUIRED;
1150         ObjectRef *ref = checkobject(L, 1);
1151         PlayerSAO* co = getplayersao(ref);
1152         if (co == NULL) return 0;
1153         u16 breath = luaL_checknumber(L, 2);
1154         // Do it
1155         co->setBreath(breath);
1156
1157         // If the object is a player sent the breath to client
1158         if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
1159                         getServer(L)->SendPlayerBreath(((PlayerSAO*)co)->getPeerID());
1160
1161         return 0;
1162 }
1163
1164 // get_breath(self)
1165 int ObjectRef::l_get_breath(lua_State *L)
1166 {
1167         NO_MAP_LOCK_REQUIRED;
1168         ObjectRef *ref = checkobject(L, 1);
1169         PlayerSAO* co = getplayersao(ref);
1170         if (co == NULL) return 0;
1171         // Do it
1172         u16 breath = co->getBreath();
1173         lua_pushinteger (L, breath);
1174         return 1;
1175 }
1176
1177 // set_inventory_formspec(self, formspec)
1178 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1179 {
1180         NO_MAP_LOCK_REQUIRED;
1181         ObjectRef *ref = checkobject(L, 1);
1182         RemotePlayer *player = getplayer(ref);
1183         if (player == NULL) return 0;
1184         std::string formspec = luaL_checkstring(L, 2);
1185
1186         player->inventory_formspec = formspec;
1187         getServer(L)->reportInventoryFormspecModified(player->getName());
1188         lua_pushboolean(L, true);
1189         return 1;
1190 }
1191
1192 // get_inventory_formspec(self) -> formspec
1193 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1194 {
1195         NO_MAP_LOCK_REQUIRED;
1196         ObjectRef *ref = checkobject(L, 1);
1197         RemotePlayer *player = getplayer(ref);
1198         if (player == NULL) return 0;
1199
1200         std::string formspec = player->inventory_formspec;
1201         lua_pushlstring(L, formspec.c_str(), formspec.size());
1202         return 1;
1203 }
1204
1205 // get_player_control(self)
1206 int ObjectRef::l_get_player_control(lua_State *L)
1207 {
1208         NO_MAP_LOCK_REQUIRED;
1209         ObjectRef *ref = checkobject(L, 1);
1210         RemotePlayer *player = getplayer(ref);
1211         if (player == NULL) {
1212                 lua_pushlstring(L, "", 0);
1213                 return 1;
1214         }
1215
1216         const PlayerControl &control = player->getPlayerControl();
1217         lua_newtable(L);
1218         lua_pushboolean(L, control.up);
1219         lua_setfield(L, -2, "up");
1220         lua_pushboolean(L, control.down);
1221         lua_setfield(L, -2, "down");
1222         lua_pushboolean(L, control.left);
1223         lua_setfield(L, -2, "left");
1224         lua_pushboolean(L, control.right);
1225         lua_setfield(L, -2, "right");
1226         lua_pushboolean(L, control.jump);
1227         lua_setfield(L, -2, "jump");
1228         lua_pushboolean(L, control.aux1);
1229         lua_setfield(L, -2, "aux1");
1230         lua_pushboolean(L, control.sneak);
1231         lua_setfield(L, -2, "sneak");
1232         lua_pushboolean(L, control.LMB);
1233         lua_setfield(L, -2, "LMB");
1234         lua_pushboolean(L, control.RMB);
1235         lua_setfield(L, -2, "RMB");
1236         return 1;
1237 }
1238
1239 // get_player_control_bits(self)
1240 int ObjectRef::l_get_player_control_bits(lua_State *L)
1241 {
1242         NO_MAP_LOCK_REQUIRED;
1243         ObjectRef *ref = checkobject(L, 1);
1244         RemotePlayer *player = getplayer(ref);
1245         if (player == NULL) {
1246                 lua_pushlstring(L, "", 0);
1247                 return 1;
1248         }
1249         // Do it
1250         lua_pushnumber(L, player->keyPressed);
1251         return 1;
1252 }
1253
1254 // hud_add(self, form)
1255 int ObjectRef::l_hud_add(lua_State *L)
1256 {
1257         NO_MAP_LOCK_REQUIRED;
1258         ObjectRef *ref = checkobject(L, 1);
1259         RemotePlayer *player = getplayer(ref);
1260         if (player == NULL)
1261                 return 0;
1262
1263         HudElement *elem = new HudElement;
1264
1265         elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
1266                                                                 es_HudElementType, HUD_ELEM_TEXT);
1267
1268         lua_getfield(L, 2, "position");
1269         elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1270         lua_pop(L, 1);
1271
1272         lua_getfield(L, 2, "scale");
1273         elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1274         lua_pop(L, 1);
1275
1276         lua_getfield(L, 2, "size");
1277         elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
1278         lua_pop(L, 1);
1279
1280         elem->name   = getstringfield_default(L, 2, "name", "");
1281         elem->text   = getstringfield_default(L, 2, "text", "");
1282         elem->number = getintfield_default(L, 2, "number", 0);
1283         elem->item   = getintfield_default(L, 2, "item", 0);
1284         elem->dir    = getintfield_default(L, 2, "direction", 0);
1285
1286         // Deprecated, only for compatibility's sake
1287         if (elem->dir == 0)
1288                 elem->dir = getintfield_default(L, 2, "dir", 0);
1289
1290         lua_getfield(L, 2, "alignment");
1291         elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1292         lua_pop(L, 1);
1293
1294         lua_getfield(L, 2, "offset");
1295         elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1296         lua_pop(L, 1);
1297
1298         lua_getfield(L, 2, "world_pos");
1299         elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
1300         lua_pop(L, 1);
1301
1302         /* check for known deprecated element usage */
1303         if ((elem->type  == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
1304                 log_deprecated(L,"Deprecated usage of statbar without size!");
1305         }
1306
1307         u32 id = getServer(L)->hudAdd(player, elem);
1308         if (id == U32_MAX) {
1309                 delete elem;
1310                 return 0;
1311         }
1312
1313         lua_pushnumber(L, id);
1314         return 1;
1315 }
1316
1317 // hud_remove(self, id)
1318 int ObjectRef::l_hud_remove(lua_State *L)
1319 {
1320         NO_MAP_LOCK_REQUIRED;
1321         ObjectRef *ref = checkobject(L, 1);
1322         RemotePlayer *player = getplayer(ref);
1323         if (player == NULL)
1324                 return 0;
1325
1326         u32 id = -1;
1327         if (!lua_isnil(L, 2))
1328                 id = lua_tonumber(L, 2);
1329
1330         if (!getServer(L)->hudRemove(player, id))
1331                 return 0;
1332
1333         lua_pushboolean(L, true);
1334         return 1;
1335 }
1336
1337 // hud_change(self, id, stat, data)
1338 int ObjectRef::l_hud_change(lua_State *L)
1339 {
1340         NO_MAP_LOCK_REQUIRED;
1341         ObjectRef *ref = checkobject(L, 1);
1342         RemotePlayer *player = getplayer(ref);
1343         if (player == NULL)
1344                 return 0;
1345
1346         u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1347
1348         HudElement *e = player->getHud(id);
1349         if (!e)
1350                 return 0;
1351
1352         HudElementStat stat = HUD_STAT_NUMBER;
1353         if (lua_isstring(L, 3)) {
1354                 int statint;
1355                 std::string statstr = lua_tostring(L, 3);
1356                 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
1357                                 (HudElementStat)statint : HUD_STAT_NUMBER;
1358         }
1359
1360         void *value = NULL;
1361         switch (stat) {
1362                 case HUD_STAT_POS:
1363                         e->pos = read_v2f(L, 4);
1364                         value = &e->pos;
1365                         break;
1366                 case HUD_STAT_NAME:
1367                         e->name = luaL_checkstring(L, 4);
1368                         value = &e->name;
1369                         break;
1370                 case HUD_STAT_SCALE:
1371                         e->scale = read_v2f(L, 4);
1372                         value = &e->scale;
1373                         break;
1374                 case HUD_STAT_TEXT:
1375                         e->text = luaL_checkstring(L, 4);
1376                         value = &e->text;
1377                         break;
1378                 case HUD_STAT_NUMBER:
1379                         e->number = luaL_checknumber(L, 4);
1380                         value = &e->number;
1381                         break;
1382                 case HUD_STAT_ITEM:
1383                         e->item = luaL_checknumber(L, 4);
1384                         value = &e->item;
1385                         break;
1386                 case HUD_STAT_DIR:
1387                         e->dir = luaL_checknumber(L, 4);
1388                         value = &e->dir;
1389                         break;
1390                 case HUD_STAT_ALIGN:
1391                         e->align = read_v2f(L, 4);
1392                         value = &e->align;
1393                         break;
1394                 case HUD_STAT_OFFSET:
1395                         e->offset = read_v2f(L, 4);
1396                         value = &e->offset;
1397                         break;
1398                 case HUD_STAT_WORLD_POS:
1399                         e->world_pos = read_v3f(L, 4);
1400                         value = &e->world_pos;
1401                         break;
1402                 case HUD_STAT_SIZE:
1403                         e->size = read_v2s32(L, 4);
1404                         value = &e->size;
1405                         break;
1406         }
1407
1408         getServer(L)->hudChange(player, id, stat, value);
1409
1410         lua_pushboolean(L, true);
1411         return 1;
1412 }
1413
1414 // hud_get(self, id)
1415 int ObjectRef::l_hud_get(lua_State *L)
1416 {
1417         NO_MAP_LOCK_REQUIRED;
1418         ObjectRef *ref = checkobject(L, 1);
1419         RemotePlayer *player = getplayer(ref);
1420         if (player == NULL)
1421                 return 0;
1422
1423         u32 id = lua_tonumber(L, -1);
1424
1425         HudElement *e = player->getHud(id);
1426         if (!e)
1427                 return 0;
1428
1429         lua_newtable(L);
1430
1431         lua_pushstring(L, es_HudElementType[(u8)e->type].str);
1432         lua_setfield(L, -2, "type");
1433
1434         push_v2f(L, e->pos);
1435         lua_setfield(L, -2, "position");
1436
1437         lua_pushstring(L, e->name.c_str());
1438         lua_setfield(L, -2, "name");
1439
1440         push_v2f(L, e->scale);
1441         lua_setfield(L, -2, "scale");
1442
1443         lua_pushstring(L, e->text.c_str());
1444         lua_setfield(L, -2, "text");
1445
1446         lua_pushnumber(L, e->number);
1447         lua_setfield(L, -2, "number");
1448
1449         lua_pushnumber(L, e->item);
1450         lua_setfield(L, -2, "item");
1451
1452         lua_pushnumber(L, e->dir);
1453         lua_setfield(L, -2, "direction");
1454
1455         // Deprecated, only for compatibility's sake
1456         lua_pushnumber(L, e->dir);
1457         lua_setfield(L, -2, "dir");
1458
1459         push_v3f(L, e->world_pos);
1460         lua_setfield(L, -2, "world_pos");
1461
1462         return 1;
1463 }
1464
1465 // hud_set_flags(self, flags)
1466 int ObjectRef::l_hud_set_flags(lua_State *L)
1467 {
1468         NO_MAP_LOCK_REQUIRED;
1469         ObjectRef *ref = checkobject(L, 1);
1470         RemotePlayer *player = getplayer(ref);
1471         if (player == NULL)
1472                 return 0;
1473
1474         u32 flags = 0;
1475         u32 mask  = 0;
1476         bool flag;
1477
1478         const EnumString *esp = es_HudBuiltinElement;
1479         for (int i = 0; esp[i].str; i++) {
1480                 if (getboolfield(L, 2, esp[i].str, flag)) {
1481                         flags |= esp[i].num * flag;
1482                         mask  |= esp[i].num;
1483                 }
1484         }
1485         if (!getServer(L)->hudSetFlags(player, flags, mask))
1486                 return 0;
1487
1488         lua_pushboolean(L, true);
1489         return 1;
1490 }
1491
1492 int ObjectRef::l_hud_get_flags(lua_State *L)
1493 {
1494         NO_MAP_LOCK_REQUIRED;
1495         ObjectRef *ref = checkobject(L, 1);
1496         RemotePlayer *player = getplayer(ref);
1497         if (player == NULL)
1498                 return 0;
1499
1500         lua_newtable(L);
1501         lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1502         lua_setfield(L, -2, "hotbar");
1503         lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1504         lua_setfield(L, -2, "healthbar");
1505         lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1506         lua_setfield(L, -2, "crosshair");
1507         lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1508         lua_setfield(L, -2, "wielditem");
1509         lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1510         lua_setfield(L, -2, "breathbar");
1511         lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1512         lua_setfield(L, -2, "minimap");
1513
1514         return 1;
1515 }
1516
1517 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1518 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1519 {
1520         NO_MAP_LOCK_REQUIRED;
1521         ObjectRef *ref = checkobject(L, 1);
1522         RemotePlayer *player = getplayer(ref);
1523         if (player == NULL)
1524                 return 0;
1525
1526         s32 hotbar_itemcount = lua_tonumber(L, 2);
1527
1528         if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1529                 return 0;
1530
1531         lua_pushboolean(L, true);
1532         return 1;
1533 }
1534
1535 // hud_get_hotbar_itemcount(self)
1536 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1537 {
1538         NO_MAP_LOCK_REQUIRED;
1539         ObjectRef *ref = checkobject(L, 1);
1540         RemotePlayer *player = getplayer(ref);
1541         if (player == NULL)
1542                 return 0;
1543
1544         s32 hotbar_itemcount = getServer(L)->hudGetHotbarItemcount(player);
1545
1546         lua_pushnumber(L, hotbar_itemcount);
1547         return 1;
1548 }
1549
1550 // hud_set_hotbar_image(self, name)
1551 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1552 {
1553         NO_MAP_LOCK_REQUIRED;
1554         ObjectRef *ref = checkobject(L, 1);
1555         RemotePlayer *player = getplayer(ref);
1556         if (player == NULL)
1557                 return 0;
1558
1559         std::string name = lua_tostring(L, 2);
1560
1561         getServer(L)->hudSetHotbarImage(player, name);
1562         return 1;
1563 }
1564
1565 // hud_get_hotbar_image(self)
1566 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1567 {
1568         NO_MAP_LOCK_REQUIRED;
1569         ObjectRef *ref = checkobject(L, 1);
1570         RemotePlayer *player = getplayer(ref);
1571         if (player == NULL)
1572                 return 0;
1573
1574         std::string name = getServer(L)->hudGetHotbarImage(player);
1575         lua_pushlstring(L, name.c_str(), name.size());
1576         return 1;
1577 }
1578
1579 // hud_set_hotbar_selected_image(self, name)
1580 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1581 {
1582         NO_MAP_LOCK_REQUIRED;
1583         ObjectRef *ref = checkobject(L, 1);
1584         RemotePlayer *player = getplayer(ref);
1585         if (player == NULL)
1586                 return 0;
1587
1588         std::string name = lua_tostring(L, 2);
1589
1590         getServer(L)->hudSetHotbarSelectedImage(player, name);
1591         return 1;
1592 }
1593
1594 // hud_get_hotbar_selected_image(self)
1595 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1596 {
1597         NO_MAP_LOCK_REQUIRED;
1598         ObjectRef *ref = checkobject(L, 1);
1599         RemotePlayer *player = getplayer(ref);
1600         if (player == NULL)
1601                 return 0;
1602
1603         const std::string &name = getServer(L)->hudGetHotbarSelectedImage(player);
1604         lua_pushlstring(L, name.c_str(), name.size());
1605         return 1;
1606 }
1607
1608 // set_sky(self, bgcolor, type, list)
1609 int ObjectRef::l_set_sky(lua_State *L)
1610 {
1611         NO_MAP_LOCK_REQUIRED;
1612         ObjectRef *ref = checkobject(L, 1);
1613         RemotePlayer *player = getplayer(ref);
1614         if (player == NULL)
1615                 return 0;
1616
1617         video::SColor bgcolor(255,255,255,255);
1618         read_color(L, 2, &bgcolor);
1619
1620         std::string type = luaL_checkstring(L, 3);
1621
1622         std::vector<std::string> params;
1623         if (lua_istable(L, 4)) {
1624                 int table = lua_gettop(L);
1625                 lua_pushnil(L);
1626                 while (lua_next(L, table) != 0) {
1627                         // key at index -2 and value at index -1
1628                         if (lua_isstring(L, -1))
1629                                 params.push_back(lua_tostring(L, -1));
1630                         else
1631                                 params.push_back("");
1632                         // removes value, keeps key for next iteration
1633                         lua_pop(L, 1);
1634                 }
1635         }
1636
1637         if (type == "skybox" && params.size() != 6)
1638                 throw LuaError("skybox expects 6 textures");
1639
1640         if (!getServer(L)->setSky(player, bgcolor, type, params))
1641                 return 0;
1642
1643         lua_pushboolean(L, true);
1644         return 1;
1645 }
1646
1647 // get_sky(self)
1648 int ObjectRef::l_get_sky(lua_State *L)
1649 {
1650         NO_MAP_LOCK_REQUIRED;
1651         ObjectRef *ref = checkobject(L, 1);
1652         RemotePlayer *player = getplayer(ref);
1653         if (player == NULL)
1654                 return 0;
1655         video::SColor bgcolor(255, 255, 255, 255);
1656         std::string type;
1657         std::vector<std::string> params;
1658
1659         player->getSky(&bgcolor, &type, &params);
1660         type = type == "" ? "regular" : type;
1661
1662         push_ARGB8(L, bgcolor);
1663         lua_pushlstring(L, type.c_str(), type.size());
1664         lua_newtable(L);
1665         s16 i = 1;
1666         for (std::vector<std::string>::iterator it = params.begin();
1667                         it != params.end(); ++it) {
1668                 lua_pushlstring(L, it->c_str(), it->size());
1669                 lua_rawseti(L, -2, i);
1670                 i++;
1671         }
1672         return 3;
1673 }
1674
1675 // override_day_night_ratio(self, brightness=0...1)
1676 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1677 {
1678         NO_MAP_LOCK_REQUIRED;
1679         ObjectRef *ref = checkobject(L, 1);
1680         RemotePlayer *player = getplayer(ref);
1681         if (player == NULL)
1682                 return 0;
1683
1684         bool do_override = false;
1685         float ratio = 0.0f;
1686         if (!lua_isnil(L, 2)) {
1687                 do_override = true;
1688                 ratio = luaL_checknumber(L, 2);
1689         }
1690
1691         if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1692                 return 0;
1693
1694         lua_pushboolean(L, true);
1695         return 1;
1696 }
1697
1698 // get_day_night_ratio(self)
1699 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1700 {
1701         NO_MAP_LOCK_REQUIRED;
1702         ObjectRef *ref = checkobject(L, 1);
1703         RemotePlayer *player = getplayer(ref);
1704         if (player == NULL)
1705                 return 0;
1706
1707         bool do_override;
1708         float ratio;
1709         player->getDayNightRatio(&do_override, &ratio);
1710
1711         if (do_override)
1712                 lua_pushnumber(L, ratio);
1713         else
1714                 lua_pushnil(L);
1715
1716         return 1;
1717 }
1718
1719 ObjectRef::ObjectRef(ServerActiveObject *object):
1720         m_object(object)
1721 {
1722         //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1723 }
1724
1725 ObjectRef::~ObjectRef()
1726 {
1727         /*if (m_object)
1728                 infostream<<"ObjectRef destructing for id="
1729                                 <<m_object->getId()<<std::endl;
1730         else
1731                 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1732 }
1733
1734 // Creates an ObjectRef and leaves it on top of stack
1735 // Not callable from Lua; all references are created on the C side.
1736 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1737 {
1738         ObjectRef *o = new ObjectRef(object);
1739         //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1740         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1741         luaL_getmetatable(L, className);
1742         lua_setmetatable(L, -2);
1743 }
1744
1745 void ObjectRef::set_null(lua_State *L)
1746 {
1747         ObjectRef *o = checkobject(L, -1);
1748         o->m_object = NULL;
1749 }
1750
1751 void ObjectRef::Register(lua_State *L)
1752 {
1753         lua_newtable(L);
1754         int methodtable = lua_gettop(L);
1755         luaL_newmetatable(L, className);
1756         int metatable = lua_gettop(L);
1757
1758         lua_pushliteral(L, "__metatable");
1759         lua_pushvalue(L, methodtable);
1760         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1761
1762         lua_pushliteral(L, "__index");
1763         lua_pushvalue(L, methodtable);
1764         lua_settable(L, metatable);
1765
1766         lua_pushliteral(L, "__gc");
1767         lua_pushcfunction(L, gc_object);
1768         lua_settable(L, metatable);
1769
1770         lua_pop(L, 1);  // drop metatable
1771
1772         luaL_openlib(L, 0, methods, 0);  // fill methodtable
1773         lua_pop(L, 1);  // drop methodtable
1774
1775         // Cannot be created from Lua
1776         //lua_register(L, className, create_object);
1777 }
1778
1779 const char ObjectRef::className[] = "ObjectRef";
1780 const luaL_reg ObjectRef::methods[] = {
1781         // ServerActiveObject
1782         luamethod(ObjectRef, remove),
1783         luamethod(ObjectRef, getpos),
1784         luamethod(ObjectRef, setpos),
1785         luamethod(ObjectRef, moveto),
1786         luamethod(ObjectRef, punch),
1787         luamethod(ObjectRef, right_click),
1788         luamethod(ObjectRef, set_hp),
1789         luamethod(ObjectRef, get_hp),
1790         luamethod(ObjectRef, get_inventory),
1791         luamethod(ObjectRef, get_wield_list),
1792         luamethod(ObjectRef, get_wield_index),
1793         luamethod(ObjectRef, get_wielded_item),
1794         luamethod(ObjectRef, set_wielded_item),
1795         luamethod(ObjectRef, set_armor_groups),
1796         luamethod(ObjectRef, get_armor_groups),
1797         luamethod(ObjectRef, set_animation),
1798         luamethod(ObjectRef, get_animation),
1799         luamethod(ObjectRef, set_bone_position),
1800         luamethod(ObjectRef, get_bone_position),
1801         luamethod(ObjectRef, set_attach),
1802         luamethod(ObjectRef, get_attach),
1803         luamethod(ObjectRef, set_detach),
1804         luamethod(ObjectRef, set_properties),
1805         luamethod(ObjectRef, get_properties),
1806         luamethod(ObjectRef, set_nametag_attributes),
1807         luamethod(ObjectRef, get_nametag_attributes),
1808         // LuaEntitySAO-only
1809         luamethod(ObjectRef, setvelocity),
1810         luamethod(ObjectRef, getvelocity),
1811         luamethod(ObjectRef, setacceleration),
1812         luamethod(ObjectRef, getacceleration),
1813         luamethod(ObjectRef, setyaw),
1814         luamethod(ObjectRef, getyaw),
1815         luamethod(ObjectRef, settexturemod),
1816         luamethod(ObjectRef, setsprite),
1817         luamethod(ObjectRef, get_entity_name),
1818         luamethod(ObjectRef, get_luaentity),
1819         // Player-only
1820         luamethod(ObjectRef, is_player),
1821         luamethod(ObjectRef, is_player_connected),
1822         luamethod(ObjectRef, get_player_name),
1823         luamethod(ObjectRef, get_player_velocity),
1824         luamethod(ObjectRef, get_look_dir),
1825         luamethod(ObjectRef, get_look_pitch),
1826         luamethod(ObjectRef, get_look_yaw),
1827         luamethod(ObjectRef, get_look_vertical),
1828         luamethod(ObjectRef, get_look_horizontal),
1829         luamethod(ObjectRef, set_look_horizontal),
1830         luamethod(ObjectRef, set_look_vertical),
1831         luamethod(ObjectRef, set_look_yaw),
1832         luamethod(ObjectRef, set_look_pitch),
1833         luamethod(ObjectRef, get_breath),
1834         luamethod(ObjectRef, set_breath),
1835         luamethod(ObjectRef, set_inventory_formspec),
1836         luamethod(ObjectRef, get_inventory_formspec),
1837         luamethod(ObjectRef, get_player_control),
1838         luamethod(ObjectRef, get_player_control_bits),
1839         luamethod(ObjectRef, set_physics_override),
1840         luamethod(ObjectRef, get_physics_override),
1841         luamethod(ObjectRef, hud_add),
1842         luamethod(ObjectRef, hud_remove),
1843         luamethod(ObjectRef, hud_change),
1844         luamethod(ObjectRef, hud_get),
1845         luamethod(ObjectRef, hud_set_flags),
1846         luamethod(ObjectRef, hud_get_flags),
1847         luamethod(ObjectRef, hud_set_hotbar_itemcount),
1848         luamethod(ObjectRef, hud_get_hotbar_itemcount),
1849         luamethod(ObjectRef, hud_set_hotbar_image),
1850         luamethod(ObjectRef, hud_get_hotbar_image),
1851         luamethod(ObjectRef, hud_set_hotbar_selected_image),
1852         luamethod(ObjectRef, hud_get_hotbar_selected_image),
1853         luamethod(ObjectRef, set_sky),
1854         luamethod(ObjectRef, get_sky),
1855         luamethod(ObjectRef, override_day_night_ratio),
1856         luamethod(ObjectRef, get_day_night_ratio),
1857         luamethod(ObjectRef, set_local_animation),
1858         luamethod(ObjectRef, get_local_animation),
1859         luamethod(ObjectRef, set_eye_offset),
1860         luamethod(ObjectRef, get_eye_offset),
1861         {0,0}
1862 };