3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "lua_api/l_object.h"
22 #include "lua_api/l_internal.h"
23 #include "lua_api/l_inventory.h"
24 #include "lua_api/l_item.h"
25 #include "lua_api/l_playermeta.h"
26 #include "common/c_converter.h"
27 #include "common/c_content.h"
30 #include "serverobject.h"
31 #include "content_sao.h"
32 #include "remoteplayer.h"
35 #include "scripting_server.h"
42 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
44 luaL_checktype(L, narg, LUA_TUSERDATA);
45 void *ud = luaL_checkudata(L, narg, className);
46 if (!ud) luaL_typerror(L, narg, className);
47 return *(ObjectRef**)ud; // unbox pointer
50 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
52 ServerActiveObject *co = ref->m_object;
56 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
58 ServerActiveObject *obj = getobject(ref);
61 if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
63 return (LuaEntitySAO*)obj;
66 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
68 ServerActiveObject *obj = getobject(ref);
71 if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
73 return (PlayerSAO*)obj;
76 RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
78 PlayerSAO *playersao = getplayersao(ref);
79 if (playersao == NULL)
81 return playersao->getPlayer();
87 int ObjectRef::gc_object(lua_State *L) {
88 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
89 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
95 int ObjectRef::l_remove(lua_State *L)
99 ObjectRef *ref = checkobject(L, 1);
100 ServerActiveObject *co = getobject(ref);
103 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
106 co->clearChildAttachments();
107 co->clearParentAttachment();
109 verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
110 co->m_pending_removal = true;
115 // returns: {x=num, y=num, z=num}
116 int ObjectRef::l_get_pos(lua_State *L)
118 NO_MAP_LOCK_REQUIRED;
119 ObjectRef *ref = checkobject(L, 1);
120 ServerActiveObject *co = getobject(ref);
121 if (co == NULL) return 0;
122 v3f pos = co->getBasePosition() / BS;
124 lua_pushnumber(L, pos.X);
125 lua_setfield(L, -2, "x");
126 lua_pushnumber(L, pos.Y);
127 lua_setfield(L, -2, "y");
128 lua_pushnumber(L, pos.Z);
129 lua_setfield(L, -2, "z");
133 // set_pos(self, pos)
134 int ObjectRef::l_set_pos(lua_State *L)
136 NO_MAP_LOCK_REQUIRED;
137 ObjectRef *ref = checkobject(L, 1);
138 //LuaEntitySAO *co = getluaobject(ref);
139 ServerActiveObject *co = getobject(ref);
140 if (co == NULL) return 0;
142 v3f pos = checkFloatPos(L, 2);
148 // move_to(self, pos, continuous=false)
149 int ObjectRef::l_move_to(lua_State *L)
151 NO_MAP_LOCK_REQUIRED;
152 ObjectRef *ref = checkobject(L, 1);
153 //LuaEntitySAO *co = getluaobject(ref);
154 ServerActiveObject *co = getobject(ref);
155 if (co == NULL) return 0;
157 v3f pos = checkFloatPos(L, 2);
159 bool continuous = lua_toboolean(L, 3);
161 co->moveTo(pos, continuous);
165 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
166 int ObjectRef::l_punch(lua_State *L)
168 NO_MAP_LOCK_REQUIRED;
169 ObjectRef *ref = checkobject(L, 1);
170 ObjectRef *puncher_ref = checkobject(L, 2);
171 ServerActiveObject *co = getobject(ref);
172 ServerActiveObject *puncher = getobject(puncher_ref);
173 if (co == NULL) return 0;
174 if (puncher == NULL) return 0;
176 if (lua_type(L, 5) != LUA_TTABLE)
177 dir = co->getBasePosition() - puncher->getBasePosition();
179 dir = read_v3f(L, 5);
180 float time_from_last_punch = 1000000;
181 if (lua_isnumber(L, 3))
182 time_from_last_punch = lua_tonumber(L, 3);
183 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
186 s16 src_original_hp = co->getHP();
187 s16 dst_origin_hp = puncher->getHP();
190 co->punch(dir, &toolcap, puncher, time_from_last_punch);
192 // If the punched is a player, and its HP changed
193 if (src_original_hp != co->getHP() &&
194 co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
195 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
198 // If the puncher is a player, and its HP changed
199 if (dst_origin_hp != puncher->getHP() &&
200 puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
201 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
202 PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
207 // right_click(self, clicker); clicker = an another ObjectRef
208 int ObjectRef::l_right_click(lua_State *L)
210 NO_MAP_LOCK_REQUIRED;
211 ObjectRef *ref = checkobject(L, 1);
212 ObjectRef *ref2 = checkobject(L, 2);
213 ServerActiveObject *co = getobject(ref);
214 ServerActiveObject *co2 = getobject(ref2);
215 if (co == NULL) return 0;
216 if (co2 == NULL) return 0;
223 // hp = number of hitpoints (2 * number of hearts)
225 int ObjectRef::l_set_hp(lua_State *L)
227 NO_MAP_LOCK_REQUIRED;
230 ObjectRef *ref = checkobject(L, 1);
231 luaL_checknumber(L, 2);
232 ServerActiveObject *co = getobject(ref);
237 int hp = lua_tonumber(L, 2);
240 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
241 reason.from_mod = true;
242 if (lua_istable(L, 3)) {
245 lua_getfield(L, -1, "type");
246 if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) {
247 errorstream << "Bad type given!" << std::endl;
251 reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
255 co->setHP(hp, reason);
256 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
257 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
264 // returns: number of hitpoints (2 * number of hearts)
265 // 0 if not applicable to this type of object
266 int ObjectRef::l_get_hp(lua_State *L)
268 NO_MAP_LOCK_REQUIRED;
269 ObjectRef *ref = checkobject(L, 1);
270 ServerActiveObject *co = getobject(ref);
273 lua_pushnumber(L, 1);
276 int hp = co->getHP();
277 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
278 <<" hp="<<hp<<std::endl;*/
280 lua_pushnumber(L, hp);
284 // get_inventory(self)
285 int ObjectRef::l_get_inventory(lua_State *L)
287 NO_MAP_LOCK_REQUIRED;
288 ObjectRef *ref = checkobject(L, 1);
289 ServerActiveObject *co = getobject(ref);
290 if (co == NULL) return 0;
292 InventoryLocation loc = co->getInventoryLocation();
293 if (getServer(L)->getInventory(loc) != NULL)
294 InvRef::create(L, loc);
296 lua_pushnil(L); // An object may have no inventory (nil)
300 // get_wield_list(self)
301 int ObjectRef::l_get_wield_list(lua_State *L)
303 NO_MAP_LOCK_REQUIRED;
304 ObjectRef *ref = checkobject(L, 1);
305 ServerActiveObject *co = getobject(ref);
306 if (co == NULL) return 0;
308 lua_pushstring(L, co->getWieldList().c_str());
312 // get_wield_index(self)
313 int ObjectRef::l_get_wield_index(lua_State *L)
315 NO_MAP_LOCK_REQUIRED;
316 ObjectRef *ref = checkobject(L, 1);
317 ServerActiveObject *co = getobject(ref);
318 if (co == NULL) return 0;
320 lua_pushinteger(L, co->getWieldIndex() + 1);
324 // get_wielded_item(self)
325 int ObjectRef::l_get_wielded_item(lua_State *L)
327 NO_MAP_LOCK_REQUIRED;
328 ObjectRef *ref = checkobject(L, 1);
329 ServerActiveObject *co = getobject(ref);
332 LuaItemStack::create(L, ItemStack());
336 LuaItemStack::create(L, co->getWieldedItem());
340 // set_wielded_item(self, itemstack or itemstring or table or nil)
341 int ObjectRef::l_set_wielded_item(lua_State *L)
343 NO_MAP_LOCK_REQUIRED;
344 ObjectRef *ref = checkobject(L, 1);
345 ServerActiveObject *co = getobject(ref);
346 if (co == NULL) return 0;
348 ItemStack item = read_item(L, 2, getServer(L)->idef());
349 bool success = co->setWieldedItem(item);
350 if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
351 getServer(L)->SendInventory(((PlayerSAO*)co));
353 lua_pushboolean(L, success);
357 // set_armor_groups(self, groups)
358 int ObjectRef::l_set_armor_groups(lua_State *L)
360 NO_MAP_LOCK_REQUIRED;
361 ObjectRef *ref = checkobject(L, 1);
362 ServerActiveObject *co = getobject(ref);
363 if (co == NULL) return 0;
365 ItemGroupList groups;
366 read_groups(L, 2, groups);
367 co->setArmorGroups(groups);
371 // get_armor_groups(self)
372 int ObjectRef::l_get_armor_groups(lua_State *L)
374 NO_MAP_LOCK_REQUIRED;
375 ObjectRef *ref = checkobject(L, 1);
376 ServerActiveObject *co = getobject(ref);
380 push_groups(L, co->getArmorGroups());
384 // set_physics_override(self, physics_override_speed, physics_override_jump,
385 // physics_override_gravity, sneak, sneak_glitch, new_move)
386 int ObjectRef::l_set_physics_override(lua_State *L)
388 NO_MAP_LOCK_REQUIRED;
389 ObjectRef *ref = checkobject(L, 1);
390 PlayerSAO *co = (PlayerSAO *) getobject(ref);
391 if (co == NULL) return 0;
393 if (lua_istable(L, 2)) {
394 co->m_physics_override_speed = getfloatfield_default(
395 L, 2, "speed", co->m_physics_override_speed);
396 co->m_physics_override_jump = getfloatfield_default(
397 L, 2, "jump", co->m_physics_override_jump);
398 co->m_physics_override_gravity = getfloatfield_default(
399 L, 2, "gravity", co->m_physics_override_gravity);
400 co->m_physics_override_sneak = getboolfield_default(
401 L, 2, "sneak", co->m_physics_override_sneak);
402 co->m_physics_override_sneak_glitch = getboolfield_default(
403 L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
404 co->m_physics_override_new_move = getboolfield_default(
405 L, 2, "new_move", co->m_physics_override_new_move);
406 co->m_physics_override_sent = false;
408 // old, non-table format
409 if (!lua_isnil(L, 2)) {
410 co->m_physics_override_speed = lua_tonumber(L, 2);
411 co->m_physics_override_sent = false;
413 if (!lua_isnil(L, 3)) {
414 co->m_physics_override_jump = lua_tonumber(L, 3);
415 co->m_physics_override_sent = false;
417 if (!lua_isnil(L, 4)) {
418 co->m_physics_override_gravity = lua_tonumber(L, 4);
419 co->m_physics_override_sent = false;
425 // get_physics_override(self)
426 int ObjectRef::l_get_physics_override(lua_State *L)
428 NO_MAP_LOCK_REQUIRED;
429 ObjectRef *ref = checkobject(L, 1);
430 PlayerSAO *co = (PlayerSAO *)getobject(ref);
435 lua_pushnumber(L, co->m_physics_override_speed);
436 lua_setfield(L, -2, "speed");
437 lua_pushnumber(L, co->m_physics_override_jump);
438 lua_setfield(L, -2, "jump");
439 lua_pushnumber(L, co->m_physics_override_gravity);
440 lua_setfield(L, -2, "gravity");
441 lua_pushboolean(L, co->m_physics_override_sneak);
442 lua_setfield(L, -2, "sneak");
443 lua_pushboolean(L, co->m_physics_override_sneak_glitch);
444 lua_setfield(L, -2, "sneak_glitch");
445 lua_pushboolean(L, co->m_physics_override_new_move);
446 lua_setfield(L, -2, "new_move");
450 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
451 int ObjectRef::l_set_animation(lua_State *L)
453 NO_MAP_LOCK_REQUIRED;
454 ObjectRef *ref = checkobject(L, 1);
455 ServerActiveObject *co = getobject(ref);
456 if (co == NULL) return 0;
458 v2f frames = v2f(1, 1);
459 if (!lua_isnil(L, 2))
460 frames = read_v2f(L, 2);
461 float frame_speed = 15;
462 if (!lua_isnil(L, 3))
463 frame_speed = lua_tonumber(L, 3);
464 float frame_blend = 0;
465 if (!lua_isnil(L, 4))
466 frame_blend = lua_tonumber(L, 4);
467 bool frame_loop = true;
468 if (lua_isboolean(L, 5))
469 frame_loop = lua_toboolean(L, 5);
470 co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
474 // get_animation(self)
475 int ObjectRef::l_get_animation(lua_State *L)
477 NO_MAP_LOCK_REQUIRED;
478 ObjectRef *ref = checkobject(L, 1);
479 ServerActiveObject *co = getobject(ref);
483 v2f frames = v2f(1,1);
484 float frame_speed = 15;
485 float frame_blend = 0;
486 bool frame_loop = true;
487 co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
490 lua_pushnumber(L, frame_speed);
491 lua_pushnumber(L, frame_blend);
492 lua_pushboolean(L, frame_loop);
496 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
497 int ObjectRef::l_set_local_animation(lua_State *L)
499 NO_MAP_LOCK_REQUIRED;
500 ObjectRef *ref = checkobject(L, 1);
501 RemotePlayer *player = getplayer(ref);
506 for (int i=0;i<4;i++) {
507 if (!lua_isnil(L, 2+1))
508 frames[i] = read_v2s32(L, 2+i);
510 float frame_speed = 30;
511 if (!lua_isnil(L, 6))
512 frame_speed = lua_tonumber(L, 6);
514 getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
515 lua_pushboolean(L, true);
519 // get_local_animation(self)
520 int ObjectRef::l_get_local_animation(lua_State *L)
523 ObjectRef *ref = checkobject(L, 1);
524 RemotePlayer *player = getplayer(ref);
530 player->getLocalAnimations(frames, &frame_speed);
532 for (const v2s32 &frame : frames) {
533 push_v2s32(L, frame);
536 lua_pushnumber(L, frame_speed);
540 // set_eye_offset(self, v3f first pv, v3f third pv)
541 int ObjectRef::l_set_eye_offset(lua_State *L)
543 NO_MAP_LOCK_REQUIRED;
544 ObjectRef *ref = checkobject(L, 1);
545 RemotePlayer *player = getplayer(ref);
549 v3f offset_first = v3f(0, 0, 0);
550 v3f offset_third = v3f(0, 0, 0);
552 if (!lua_isnil(L, 2))
553 offset_first = read_v3f(L, 2);
554 if (!lua_isnil(L, 3))
555 offset_third = read_v3f(L, 3);
557 // Prevent abuse of offset values (keep player always visible)
558 offset_third.X = rangelim(offset_third.X,-10,10);
559 offset_third.Z = rangelim(offset_third.Z,-5,5);
560 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
561 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
563 getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
564 lua_pushboolean(L, true);
568 // get_eye_offset(self)
569 int ObjectRef::l_get_eye_offset(lua_State *L)
571 NO_MAP_LOCK_REQUIRED;
572 ObjectRef *ref = checkobject(L, 1);
573 RemotePlayer *player = getplayer(ref);
577 push_v3f(L, player->eye_offset_first);
578 push_v3f(L, player->eye_offset_third);
582 // set_animation_frame_speed(self, frame_speed)
583 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
585 NO_MAP_LOCK_REQUIRED;
586 ObjectRef *ref = checkobject(L, 1);
587 ServerActiveObject *co = getobject(ref);
592 if (!lua_isnil(L, 2)) {
593 float frame_speed = lua_tonumber(L, 2);
594 co->setAnimationSpeed(frame_speed);
595 lua_pushboolean(L, true);
597 lua_pushboolean(L, false);
602 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
603 int ObjectRef::l_set_bone_position(lua_State *L)
605 NO_MAP_LOCK_REQUIRED;
606 ObjectRef *ref = checkobject(L, 1);
607 ServerActiveObject *co = getobject(ref);
608 if (co == NULL) return 0;
611 if (!lua_isnil(L, 2))
612 bone = lua_tostring(L, 2);
613 v3f position = v3f(0, 0, 0);
614 if (!lua_isnil(L, 3))
615 position = check_v3f(L, 3);
616 v3f rotation = v3f(0, 0, 0);
617 if (!lua_isnil(L, 4))
618 rotation = check_v3f(L, 4);
619 co->setBonePosition(bone, position, rotation);
623 // get_bone_position(self, bone)
624 int ObjectRef::l_get_bone_position(lua_State *L)
626 NO_MAP_LOCK_REQUIRED;
627 ObjectRef *ref = checkobject(L, 1);
628 ServerActiveObject *co = getobject(ref);
633 if (!lua_isnil(L, 2))
634 bone = lua_tostring(L, 2);
636 v3f position = v3f(0, 0, 0);
637 v3f rotation = v3f(0, 0, 0);
638 co->getBonePosition(bone, &position, &rotation);
640 push_v3f(L, position);
641 push_v3f(L, rotation);
645 // set_attach(self, parent, bone, position, rotation)
646 int ObjectRef::l_set_attach(lua_State *L)
650 ObjectRef *ref = checkobject(L, 1);
651 ObjectRef *parent_ref = checkobject(L, 2);
652 ServerActiveObject *co = getobject(ref);
653 ServerActiveObject *parent = getobject(parent_ref);
661 v3f position = v3f(0, 0, 0);
662 v3f rotation = v3f(0, 0, 0);
663 co->getAttachment(&parent_id, &bone, &position, &rotation);
665 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
666 old_parent->removeAttachmentChild(co->getId());
670 if (!lua_isnil(L, 3))
671 bone = lua_tostring(L, 3);
672 position = v3f(0, 0, 0);
673 if (!lua_isnil(L, 4))
674 position = read_v3f(L, 4);
675 rotation = v3f(0, 0, 0);
676 if (!lua_isnil(L, 5))
677 rotation = read_v3f(L, 5);
678 co->setAttachment(parent->getId(), bone, position, rotation);
679 parent->addAttachmentChild(co->getId());
684 int ObjectRef::l_get_attach(lua_State *L)
688 ObjectRef *ref = checkobject(L, 1);
689 ServerActiveObject *co = getobject(ref);
696 v3f position = v3f(0, 0, 0);
697 v3f rotation = v3f(0, 0, 0);
698 co->getAttachment(&parent_id, &bone, &position, &rotation);
701 ServerActiveObject *parent = env->getActiveObject(parent_id);
703 getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
704 lua_pushlstring(L, bone.c_str(), bone.size());
705 push_v3f(L, position);
706 push_v3f(L, rotation);
711 int ObjectRef::l_set_detach(lua_State *L)
715 ObjectRef *ref = checkobject(L, 1);
716 ServerActiveObject *co = getobject(ref);
720 co->clearParentAttachment();
724 // set_properties(self, properties)
725 int ObjectRef::l_set_properties(lua_State *L)
727 NO_MAP_LOCK_REQUIRED;
728 ObjectRef *ref = checkobject(L, 1);
729 ServerActiveObject *co = getobject(ref);
730 if (co == NULL) return 0;
731 ObjectProperties *prop = co->accessObjectProperties();
734 read_object_properties(L, 2, prop, getServer(L)->idef());
735 if (prop->hp_max < co->getHP()) {
736 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
737 co->setHP(prop->hp_max, reason);
738 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
739 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
741 co->notifyObjectPropertiesModified();
745 // get_properties(self)
746 int ObjectRef::l_get_properties(lua_State *L)
748 NO_MAP_LOCK_REQUIRED;
749 ObjectRef *ref = checkobject(L, 1);
750 ServerActiveObject *co = getobject(ref);
753 ObjectProperties *prop = co->accessObjectProperties();
756 push_object_properties(L, prop);
761 int ObjectRef::l_is_player(lua_State *L)
763 NO_MAP_LOCK_REQUIRED;
764 ObjectRef *ref = checkobject(L, 1);
765 RemotePlayer *player = getplayer(ref);
766 lua_pushboolean(L, (player != NULL));
770 // set_nametag_attributes(self, attributes)
771 int ObjectRef::l_set_nametag_attributes(lua_State *L)
773 NO_MAP_LOCK_REQUIRED;
774 ObjectRef *ref = checkobject(L, 1);
775 ServerActiveObject *co = getobject(ref);
779 ObjectProperties *prop = co->accessObjectProperties();
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;
791 std::string nametag = getstringfield_default(L, 2, "text", "");
792 prop->nametag = nametag;
794 co->notifyObjectPropertiesModified();
795 lua_pushboolean(L, true);
799 // get_nametag_attributes(self)
800 int ObjectRef::l_get_nametag_attributes(lua_State *L)
802 NO_MAP_LOCK_REQUIRED;
803 ObjectRef *ref = checkobject(L, 1);
804 ServerActiveObject *co = getobject(ref);
808 ObjectProperties *prop = co->accessObjectProperties();
812 video::SColor color = prop->nametag_color;
815 push_ARGB8(L, color);
816 lua_setfield(L, -2, "color");
817 lua_pushstring(L, prop->nametag.c_str());
818 lua_setfield(L, -2, "text");
822 /* LuaEntitySAO-only */
824 // set_velocity(self, {x=num, y=num, z=num})
825 int ObjectRef::l_set_velocity(lua_State *L)
827 NO_MAP_LOCK_REQUIRED;
828 ObjectRef *ref = checkobject(L, 1);
829 LuaEntitySAO *co = getluaobject(ref);
830 if (co == NULL) return 0;
831 v3f pos = checkFloatPos(L, 2);
833 co->setVelocity(pos);
837 // add_velocity(self, {x=num, y=num, z=num})
838 int ObjectRef::l_add_velocity(lua_State *L)
840 NO_MAP_LOCK_REQUIRED;
841 ObjectRef *ref = checkobject(L, 1);
842 LuaEntitySAO *co = getluaobject(ref);
845 v3f pos = checkFloatPos(L, 2);
847 co->addVelocity(pos);
851 // get_velocity(self)
852 int ObjectRef::l_get_velocity(lua_State *L)
854 NO_MAP_LOCK_REQUIRED;
855 ObjectRef *ref = checkobject(L, 1);
856 LuaEntitySAO *co = getluaobject(ref);
857 if (co == NULL) return 0;
859 v3f v = co->getVelocity();
864 // set_acceleration(self, {x=num, y=num, z=num})
865 int ObjectRef::l_set_acceleration(lua_State *L)
867 NO_MAP_LOCK_REQUIRED;
868 ObjectRef *ref = checkobject(L, 1);
869 LuaEntitySAO *co = getluaobject(ref);
870 if (co == NULL) return 0;
872 v3f pos = checkFloatPos(L, 2);
874 co->setAcceleration(pos);
878 // get_acceleration(self)
879 int ObjectRef::l_get_acceleration(lua_State *L)
881 NO_MAP_LOCK_REQUIRED;
882 ObjectRef *ref = checkobject(L, 1);
883 LuaEntitySAO *co = getluaobject(ref);
884 if (co == NULL) return 0;
886 v3f v = co->getAcceleration();
891 // set_yaw(self, radians)
892 int ObjectRef::l_set_yaw(lua_State *L)
894 NO_MAP_LOCK_REQUIRED;
895 ObjectRef *ref = checkobject(L, 1);
896 LuaEntitySAO *co = getluaobject(ref);
897 if (co == NULL) return 0;
898 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
905 int ObjectRef::l_get_yaw(lua_State *L)
907 NO_MAP_LOCK_REQUIRED;
908 ObjectRef *ref = checkobject(L, 1);
909 LuaEntitySAO *co = getluaobject(ref);
910 if (co == NULL) return 0;
912 float yaw = co->getYaw() * core::DEGTORAD;
913 lua_pushnumber(L, yaw);
917 // set_texture_mod(self, mod)
918 int ObjectRef::l_set_texture_mod(lua_State *L)
920 NO_MAP_LOCK_REQUIRED;
921 ObjectRef *ref = checkobject(L, 1);
922 LuaEntitySAO *co = getluaobject(ref);
923 if (co == NULL) return 0;
925 std::string mod = luaL_checkstring(L, 2);
926 co->setTextureMod(mod);
930 // get_texture_mod(self)
931 int ObjectRef::l_get_texture_mod(lua_State *L)
933 NO_MAP_LOCK_REQUIRED;
934 ObjectRef *ref = checkobject(L, 1);
935 LuaEntitySAO *co = getluaobject(ref);
936 if (co == NULL) return 0;
938 std::string mod = co->getTextureMod();
939 lua_pushstring(L, mod.c_str());
943 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
944 // select_horiz_by_yawpitch=false)
945 int ObjectRef::l_set_sprite(lua_State *L)
947 NO_MAP_LOCK_REQUIRED;
948 ObjectRef *ref = checkobject(L, 1);
949 LuaEntitySAO *co = getluaobject(ref);
950 if (co == NULL) return 0;
953 if (!lua_isnil(L, 2))
954 p = read_v2s16(L, 2);
956 if (!lua_isnil(L, 3))
957 num_frames = lua_tonumber(L, 3);
958 float framelength = 0.2;
959 if (!lua_isnil(L, 4))
960 framelength = lua_tonumber(L, 4);
961 bool select_horiz_by_yawpitch = false;
962 if (!lua_isnil(L, 5))
963 select_horiz_by_yawpitch = lua_toboolean(L, 5);
964 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
969 // get_entity_name(self)
970 int ObjectRef::l_get_entity_name(lua_State *L)
972 NO_MAP_LOCK_REQUIRED;
973 ObjectRef *ref = checkobject(L, 1);
974 LuaEntitySAO *co = getluaobject(ref);
975 log_deprecated(L,"Deprecated call to \"get_entity_name");
976 if (co == NULL) return 0;
978 std::string name = co->getName();
979 lua_pushstring(L, name.c_str());
983 // get_luaentity(self)
984 int ObjectRef::l_get_luaentity(lua_State *L)
986 NO_MAP_LOCK_REQUIRED;
987 ObjectRef *ref = checkobject(L, 1);
988 LuaEntitySAO *co = getluaobject(ref);
989 if (co == NULL) return 0;
991 luaentity_get(L, co->getId());
997 // is_player_connected(self)
998 int ObjectRef::l_is_player_connected(lua_State *L)
1000 NO_MAP_LOCK_REQUIRED;
1001 ObjectRef *ref = checkobject(L, 1);
1002 RemotePlayer *player = getplayer(ref);
1003 lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
1007 // get_player_name(self)
1008 int ObjectRef::l_get_player_name(lua_State *L)
1010 NO_MAP_LOCK_REQUIRED;
1011 ObjectRef *ref = checkobject(L, 1);
1012 RemotePlayer *player = getplayer(ref);
1013 if (player == NULL) {
1014 lua_pushlstring(L, "", 0);
1018 lua_pushstring(L, player->getName());
1022 // get_player_velocity(self)
1023 int ObjectRef::l_get_player_velocity(lua_State *L)
1025 NO_MAP_LOCK_REQUIRED;
1026 ObjectRef *ref = checkobject(L, 1);
1027 RemotePlayer *player = getplayer(ref);
1028 if (player == NULL) {
1033 push_v3f(L, player->getSpeed() / BS);
1037 // get_look_dir(self)
1038 int ObjectRef::l_get_look_dir(lua_State *L)
1040 NO_MAP_LOCK_REQUIRED;
1041 ObjectRef *ref = checkobject(L, 1);
1042 PlayerSAO* co = getplayersao(ref);
1043 if (co == NULL) return 0;
1045 float pitch = co->getRadPitchDep();
1046 float yaw = co->getRadYawDep();
1047 v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
1054 // get_look_pitch(self)
1055 int ObjectRef::l_get_look_pitch(lua_State *L)
1057 NO_MAP_LOCK_REQUIRED;
1060 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1062 ObjectRef *ref = checkobject(L, 1);
1063 PlayerSAO* co = getplayersao(ref);
1064 if (co == NULL) return 0;
1066 lua_pushnumber(L, co->getRadPitchDep());
1071 // get_look_yaw(self)
1072 int ObjectRef::l_get_look_yaw(lua_State *L)
1074 NO_MAP_LOCK_REQUIRED;
1077 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1079 ObjectRef *ref = checkobject(L, 1);
1080 PlayerSAO* co = getplayersao(ref);
1081 if (co == NULL) return 0;
1083 lua_pushnumber(L, co->getRadYawDep());
1087 // get_look_pitch2(self)
1088 int ObjectRef::l_get_look_vertical(lua_State *L)
1090 NO_MAP_LOCK_REQUIRED;
1091 ObjectRef *ref = checkobject(L, 1);
1092 PlayerSAO* co = getplayersao(ref);
1093 if (co == NULL) return 0;
1095 lua_pushnumber(L, co->getRadPitch());
1099 // get_look_yaw2(self)
1100 int ObjectRef::l_get_look_horizontal(lua_State *L)
1102 NO_MAP_LOCK_REQUIRED;
1103 ObjectRef *ref = checkobject(L, 1);
1104 PlayerSAO* co = getplayersao(ref);
1105 if (co == NULL) return 0;
1107 lua_pushnumber(L, co->getRadYaw());
1111 // set_look_vertical(self, radians)
1112 int ObjectRef::l_set_look_vertical(lua_State *L)
1114 NO_MAP_LOCK_REQUIRED;
1115 ObjectRef *ref = checkobject(L, 1);
1116 PlayerSAO* co = getplayersao(ref);
1117 if (co == NULL) return 0;
1118 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1120 co->setPitchAndSend(pitch);
1124 // set_look_horizontal(self, radians)
1125 int ObjectRef::l_set_look_horizontal(lua_State *L)
1127 NO_MAP_LOCK_REQUIRED;
1128 ObjectRef *ref = checkobject(L, 1);
1129 PlayerSAO* co = getplayersao(ref);
1130 if (co == NULL) return 0;
1131 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1133 co->setYawAndSend(yaw);
1138 // set_look_pitch(self, radians)
1139 int ObjectRef::l_set_look_pitch(lua_State *L)
1141 NO_MAP_LOCK_REQUIRED;
1144 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1146 ObjectRef *ref = checkobject(L, 1);
1147 PlayerSAO* co = getplayersao(ref);
1148 if (co == NULL) return 0;
1149 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1151 co->setPitchAndSend(pitch);
1156 // set_look_yaw(self, radians)
1157 int ObjectRef::l_set_look_yaw(lua_State *L)
1159 NO_MAP_LOCK_REQUIRED;
1162 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1164 ObjectRef *ref = checkobject(L, 1);
1165 PlayerSAO* co = getplayersao(ref);
1166 if (co == NULL) return 0;
1167 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1169 co->setYawAndSend(yaw);
1173 // set_breath(self, breath)
1174 int ObjectRef::l_set_breath(lua_State *L)
1176 NO_MAP_LOCK_REQUIRED;
1177 ObjectRef *ref = checkobject(L, 1);
1178 PlayerSAO* co = getplayersao(ref);
1179 if (co == NULL) return 0;
1180 u16 breath = luaL_checknumber(L, 2);
1181 co->setBreath(breath);
1187 int ObjectRef::l_get_breath(lua_State *L)
1189 NO_MAP_LOCK_REQUIRED;
1190 ObjectRef *ref = checkobject(L, 1);
1191 PlayerSAO* co = getplayersao(ref);
1192 if (co == NULL) return 0;
1194 u16 breath = co->getBreath();
1195 lua_pushinteger (L, breath);
1199 // set_attribute(self, attribute, value)
1200 int ObjectRef::l_set_attribute(lua_State *L)
1202 ObjectRef *ref = checkobject(L, 1);
1203 PlayerSAO* co = getplayersao(ref);
1207 std::string attr = luaL_checkstring(L, 2);
1208 if (lua_isnil(L, 3)) {
1209 co->getMeta().removeString(attr);
1211 std::string value = luaL_checkstring(L, 3);
1212 co->getMeta().setString(attr, value);
1217 // get_attribute(self, attribute)
1218 int ObjectRef::l_get_attribute(lua_State *L)
1220 ObjectRef *ref = checkobject(L, 1);
1221 PlayerSAO* co = getplayersao(ref);
1225 std::string attr = luaL_checkstring(L, 2);
1228 if (co->getMeta().getStringToRef(attr, value)) {
1229 lua_pushstring(L, value.c_str());
1237 // get_meta(self, attribute)
1238 int ObjectRef::l_get_meta(lua_State *L)
1240 ObjectRef *ref = checkobject(L, 1);
1241 PlayerSAO *co = getplayersao(ref);
1245 PlayerMetaRef::create(L, &co->getMeta());
1250 // set_inventory_formspec(self, formspec)
1251 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1253 NO_MAP_LOCK_REQUIRED;
1254 ObjectRef *ref = checkobject(L, 1);
1255 RemotePlayer *player = getplayer(ref);
1256 if (player == NULL) return 0;
1257 std::string formspec = luaL_checkstring(L, 2);
1259 player->inventory_formspec = formspec;
1260 getServer(L)->reportInventoryFormspecModified(player->getName());
1261 lua_pushboolean(L, true);
1265 // get_inventory_formspec(self) -> formspec
1266 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1268 NO_MAP_LOCK_REQUIRED;
1269 ObjectRef *ref = checkobject(L, 1);
1270 RemotePlayer *player = getplayer(ref);
1271 if (player == NULL) return 0;
1273 std::string formspec = player->inventory_formspec;
1274 lua_pushlstring(L, formspec.c_str(), formspec.size());
1278 // set_formspec_prepend(self, formspec)
1279 int ObjectRef::l_set_formspec_prepend(lua_State *L)
1281 NO_MAP_LOCK_REQUIRED;
1282 ObjectRef *ref = checkobject(L, 1);
1283 RemotePlayer *player = getplayer(ref);
1287 std::string formspec = luaL_checkstring(L, 2);
1289 player->formspec_prepend = formspec;
1290 getServer(L)->reportFormspecPrependModified(player->getName());
1291 lua_pushboolean(L, true);
1295 // get_formspec_prepend(self) -> formspec
1296 int ObjectRef::l_get_formspec_prepend(lua_State *L)
1298 NO_MAP_LOCK_REQUIRED;
1299 ObjectRef *ref = checkobject(L, 1);
1300 RemotePlayer *player = getplayer(ref);
1304 std::string formspec = player->formspec_prepend;
1305 lua_pushlstring(L, formspec.c_str(), formspec.size());
1309 // get_player_control(self)
1310 int ObjectRef::l_get_player_control(lua_State *L)
1312 NO_MAP_LOCK_REQUIRED;
1313 ObjectRef *ref = checkobject(L, 1);
1314 RemotePlayer *player = getplayer(ref);
1315 if (player == NULL) {
1316 lua_pushlstring(L, "", 0);
1320 const PlayerControl &control = player->getPlayerControl();
1322 lua_pushboolean(L, control.up);
1323 lua_setfield(L, -2, "up");
1324 lua_pushboolean(L, control.down);
1325 lua_setfield(L, -2, "down");
1326 lua_pushboolean(L, control.left);
1327 lua_setfield(L, -2, "left");
1328 lua_pushboolean(L, control.right);
1329 lua_setfield(L, -2, "right");
1330 lua_pushboolean(L, control.jump);
1331 lua_setfield(L, -2, "jump");
1332 lua_pushboolean(L, control.aux1);
1333 lua_setfield(L, -2, "aux1");
1334 lua_pushboolean(L, control.sneak);
1335 lua_setfield(L, -2, "sneak");
1336 lua_pushboolean(L, control.LMB);
1337 lua_setfield(L, -2, "LMB");
1338 lua_pushboolean(L, control.RMB);
1339 lua_setfield(L, -2, "RMB");
1343 // get_player_control_bits(self)
1344 int ObjectRef::l_get_player_control_bits(lua_State *L)
1346 NO_MAP_LOCK_REQUIRED;
1347 ObjectRef *ref = checkobject(L, 1);
1348 RemotePlayer *player = getplayer(ref);
1349 if (player == NULL) {
1350 lua_pushlstring(L, "", 0);
1354 lua_pushnumber(L, player->keyPressed);
1358 // hud_add(self, form)
1359 int ObjectRef::l_hud_add(lua_State *L)
1361 NO_MAP_LOCK_REQUIRED;
1362 ObjectRef *ref = checkobject(L, 1);
1363 RemotePlayer *player = getplayer(ref);
1367 HudElement *elem = new HudElement;
1368 read_hud_element(L, elem);
1370 u32 id = getServer(L)->hudAdd(player, elem);
1371 if (id == U32_MAX) {
1376 lua_pushnumber(L, id);
1380 // hud_remove(self, id)
1381 int ObjectRef::l_hud_remove(lua_State *L)
1383 NO_MAP_LOCK_REQUIRED;
1384 ObjectRef *ref = checkobject(L, 1);
1385 RemotePlayer *player = getplayer(ref);
1390 if (!lua_isnil(L, 2))
1391 id = lua_tonumber(L, 2);
1393 if (!getServer(L)->hudRemove(player, id))
1396 lua_pushboolean(L, true);
1400 // hud_change(self, id, stat, data)
1401 int ObjectRef::l_hud_change(lua_State *L)
1403 NO_MAP_LOCK_REQUIRED;
1404 ObjectRef *ref = checkobject(L, 1);
1405 RemotePlayer *player = getplayer(ref);
1409 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1411 HudElement *e = player->getHud(id);
1416 HudElementStat stat = read_hud_change(L, e, &value);
1418 getServer(L)->hudChange(player, id, stat, value);
1420 lua_pushboolean(L, true);
1424 // hud_get(self, id)
1425 int ObjectRef::l_hud_get(lua_State *L)
1427 NO_MAP_LOCK_REQUIRED;
1428 ObjectRef *ref = checkobject(L, 1);
1429 RemotePlayer *player = getplayer(ref);
1433 u32 id = lua_tonumber(L, -1);
1435 HudElement *e = player->getHud(id);
1438 push_hud_element(L, e);
1442 // hud_set_flags(self, flags)
1443 int ObjectRef::l_hud_set_flags(lua_State *L)
1445 NO_MAP_LOCK_REQUIRED;
1446 ObjectRef *ref = checkobject(L, 1);
1447 RemotePlayer *player = getplayer(ref);
1455 const EnumString *esp = es_HudBuiltinElement;
1456 for (int i = 0; esp[i].str; i++) {
1457 if (getboolfield(L, 2, esp[i].str, flag)) {
1458 flags |= esp[i].num * flag;
1462 if (!getServer(L)->hudSetFlags(player, flags, mask))
1465 lua_pushboolean(L, true);
1469 int ObjectRef::l_hud_get_flags(lua_State *L)
1471 NO_MAP_LOCK_REQUIRED;
1472 ObjectRef *ref = checkobject(L, 1);
1473 RemotePlayer *player = getplayer(ref);
1478 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1479 lua_setfield(L, -2, "hotbar");
1480 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1481 lua_setfield(L, -2, "healthbar");
1482 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1483 lua_setfield(L, -2, "crosshair");
1484 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1485 lua_setfield(L, -2, "wielditem");
1486 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1487 lua_setfield(L, -2, "breathbar");
1488 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1489 lua_setfield(L, -2, "minimap");
1490 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
1491 lua_setfield(L, -2, "minimap_radar");
1496 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1497 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1499 NO_MAP_LOCK_REQUIRED;
1500 ObjectRef *ref = checkobject(L, 1);
1501 RemotePlayer *player = getplayer(ref);
1505 s32 hotbar_itemcount = lua_tonumber(L, 2);
1507 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1510 lua_pushboolean(L, true);
1514 // hud_get_hotbar_itemcount(self)
1515 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1517 NO_MAP_LOCK_REQUIRED;
1518 ObjectRef *ref = checkobject(L, 1);
1519 RemotePlayer *player = getplayer(ref);
1523 lua_pushnumber(L, player->getHotbarItemcount());
1527 // hud_set_hotbar_image(self, name)
1528 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1530 NO_MAP_LOCK_REQUIRED;
1531 ObjectRef *ref = checkobject(L, 1);
1532 RemotePlayer *player = getplayer(ref);
1536 std::string name = lua_tostring(L, 2);
1538 getServer(L)->hudSetHotbarImage(player, name);
1542 // hud_get_hotbar_image(self)
1543 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1545 NO_MAP_LOCK_REQUIRED;
1546 ObjectRef *ref = checkobject(L, 1);
1547 RemotePlayer *player = getplayer(ref);
1551 const std::string &name = player->getHotbarImage();
1552 lua_pushlstring(L, name.c_str(), name.size());
1556 // hud_set_hotbar_selected_image(self, name)
1557 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1559 NO_MAP_LOCK_REQUIRED;
1560 ObjectRef *ref = checkobject(L, 1);
1561 RemotePlayer *player = getplayer(ref);
1565 std::string name = lua_tostring(L, 2);
1567 getServer(L)->hudSetHotbarSelectedImage(player, name);
1571 // hud_get_hotbar_selected_image(self)
1572 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1574 NO_MAP_LOCK_REQUIRED;
1575 ObjectRef *ref = checkobject(L, 1);
1576 RemotePlayer *player = getplayer(ref);
1580 const std::string &name = player->getHotbarSelectedImage();
1581 lua_pushlstring(L, name.c_str(), name.size());
1585 // set_sky(self, bgcolor, type, list, clouds = true)
1586 int ObjectRef::l_set_sky(lua_State *L)
1588 NO_MAP_LOCK_REQUIRED;
1589 ObjectRef *ref = checkobject(L, 1);
1590 RemotePlayer *player = getplayer(ref);
1594 video::SColor bgcolor(255,255,255,255);
1595 read_color(L, 2, &bgcolor);
1597 std::string type = luaL_checkstring(L, 3);
1599 std::vector<std::string> params;
1600 if (lua_istable(L, 4)) {
1602 while (lua_next(L, 4) != 0) {
1603 // key at index -2 and value at index -1
1604 if (lua_isstring(L, -1))
1605 params.emplace_back(lua_tostring(L, -1));
1607 params.emplace_back("");
1608 // removes value, keeps key for next iteration
1613 if (type == "skybox" && params.size() != 6)
1614 throw LuaError("skybox expects 6 textures");
1617 if (lua_isboolean(L, 5))
1618 clouds = lua_toboolean(L, 5);
1620 getServer(L)->setSky(player, bgcolor, type, params, clouds);
1621 lua_pushboolean(L, true);
1626 int ObjectRef::l_get_sky(lua_State *L)
1628 NO_MAP_LOCK_REQUIRED;
1629 ObjectRef *ref = checkobject(L, 1);
1630 RemotePlayer *player = getplayer(ref);
1633 video::SColor bgcolor(255, 255, 255, 255);
1635 std::vector<std::string> params;
1638 player->getSky(&bgcolor, &type, ¶ms, &clouds);
1639 type = type.empty() ? "regular" : type;
1641 push_ARGB8(L, bgcolor);
1642 lua_pushlstring(L, type.c_str(), type.size());
1645 for (const std::string ¶m : params) {
1646 lua_pushlstring(L, param.c_str(), param.size());
1647 lua_rawseti(L, -2, i);
1650 lua_pushboolean(L, clouds);
1654 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1655 int ObjectRef::l_set_clouds(lua_State *L)
1657 NO_MAP_LOCK_REQUIRED;
1658 ObjectRef *ref = checkobject(L, 1);
1659 RemotePlayer *player = getplayer(ref);
1662 if (!lua_istable(L, 2))
1665 CloudParams cloud_params = player->getCloudParams();
1667 cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1669 lua_getfield(L, 2, "color");
1670 if (!lua_isnil(L, -1))
1671 read_color(L, -1, &cloud_params.color_bright);
1673 lua_getfield(L, 2, "ambient");
1674 if (!lua_isnil(L, -1))
1675 read_color(L, -1, &cloud_params.color_ambient);
1678 cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
1679 cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1681 lua_getfield(L, 2, "speed");
1682 if (lua_istable(L, -1)) {
1684 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1685 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1686 cloud_params.speed = new_speed;
1690 getServer(L)->setClouds(player, cloud_params);
1691 lua_pushboolean(L, true);
1695 int ObjectRef::l_get_clouds(lua_State *L)
1697 NO_MAP_LOCK_REQUIRED;
1698 ObjectRef *ref = checkobject(L, 1);
1699 RemotePlayer *player = getplayer(ref);
1702 const CloudParams &cloud_params = player->getCloudParams();
1705 lua_pushnumber(L, cloud_params.density);
1706 lua_setfield(L, -2, "density");
1707 push_ARGB8(L, cloud_params.color_bright);
1708 lua_setfield(L, -2, "color");
1709 push_ARGB8(L, cloud_params.color_ambient);
1710 lua_setfield(L, -2, "ambient");
1711 lua_pushnumber(L, cloud_params.height);
1712 lua_setfield(L, -2, "height");
1713 lua_pushnumber(L, cloud_params.thickness);
1714 lua_setfield(L, -2, "thickness");
1716 lua_pushnumber(L, cloud_params.speed.X);
1717 lua_setfield(L, -2, "x");
1718 lua_pushnumber(L, cloud_params.speed.Y);
1719 lua_setfield(L, -2, "y");
1720 lua_setfield(L, -2, "speed");
1726 // override_day_night_ratio(self, brightness=0...1)
1727 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1729 NO_MAP_LOCK_REQUIRED;
1730 ObjectRef *ref = checkobject(L, 1);
1731 RemotePlayer *player = getplayer(ref);
1735 bool do_override = false;
1737 if (!lua_isnil(L, 2)) {
1739 ratio = luaL_checknumber(L, 2);
1742 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1745 lua_pushboolean(L, true);
1749 // get_day_night_ratio(self)
1750 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1752 NO_MAP_LOCK_REQUIRED;
1753 ObjectRef *ref = checkobject(L, 1);
1754 RemotePlayer *player = getplayer(ref);
1760 player->getDayNightRatio(&do_override, &ratio);
1763 lua_pushnumber(L, ratio);
1770 ObjectRef::ObjectRef(ServerActiveObject *object):
1773 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1776 // Creates an ObjectRef and leaves it on top of stack
1777 // Not callable from Lua; all references are created on the C side.
1778 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1780 ObjectRef *o = new ObjectRef(object);
1781 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1782 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1783 luaL_getmetatable(L, className);
1784 lua_setmetatable(L, -2);
1787 void ObjectRef::set_null(lua_State *L)
1789 ObjectRef *o = checkobject(L, -1);
1793 void ObjectRef::Register(lua_State *L)
1796 int methodtable = lua_gettop(L);
1797 luaL_newmetatable(L, className);
1798 int metatable = lua_gettop(L);
1800 lua_pushliteral(L, "__metatable");
1801 lua_pushvalue(L, methodtable);
1802 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1804 lua_pushliteral(L, "__index");
1805 lua_pushvalue(L, methodtable);
1806 lua_settable(L, metatable);
1808 lua_pushliteral(L, "__gc");
1809 lua_pushcfunction(L, gc_object);
1810 lua_settable(L, metatable);
1812 lua_pop(L, 1); // drop metatable
1814 luaL_openlib(L, 0, methods, 0); // fill methodtable
1815 lua_pop(L, 1); // drop methodtable
1817 // Cannot be created from Lua
1818 //lua_register(L, className, create_object);
1821 const char ObjectRef::className[] = "ObjectRef";
1822 const luaL_Reg ObjectRef::methods[] = {
1823 // ServerActiveObject
1824 luamethod(ObjectRef, remove),
1825 luamethod_aliased(ObjectRef, get_pos, getpos),
1826 luamethod_aliased(ObjectRef, set_pos, setpos),
1827 luamethod_aliased(ObjectRef, move_to, moveto),
1828 luamethod(ObjectRef, punch),
1829 luamethod(ObjectRef, right_click),
1830 luamethod(ObjectRef, set_hp),
1831 luamethod(ObjectRef, get_hp),
1832 luamethod(ObjectRef, get_inventory),
1833 luamethod(ObjectRef, get_wield_list),
1834 luamethod(ObjectRef, get_wield_index),
1835 luamethod(ObjectRef, get_wielded_item),
1836 luamethod(ObjectRef, set_wielded_item),
1837 luamethod(ObjectRef, set_armor_groups),
1838 luamethod(ObjectRef, get_armor_groups),
1839 luamethod(ObjectRef, set_animation),
1840 luamethod(ObjectRef, get_animation),
1841 luamethod(ObjectRef, set_animation_frame_speed),
1842 luamethod(ObjectRef, set_bone_position),
1843 luamethod(ObjectRef, get_bone_position),
1844 luamethod(ObjectRef, set_attach),
1845 luamethod(ObjectRef, get_attach),
1846 luamethod(ObjectRef, set_detach),
1847 luamethod(ObjectRef, set_properties),
1848 luamethod(ObjectRef, get_properties),
1849 luamethod(ObjectRef, set_nametag_attributes),
1850 luamethod(ObjectRef, get_nametag_attributes),
1851 // LuaEntitySAO-only
1852 luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1853 luamethod(ObjectRef, add_velocity),
1854 luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1855 luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1856 luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1857 luamethod_aliased(ObjectRef, set_yaw, setyaw),
1858 luamethod_aliased(ObjectRef, get_yaw, getyaw),
1859 luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1860 luamethod_aliased(ObjectRef, set_sprite, setsprite),
1861 luamethod(ObjectRef, get_entity_name),
1862 luamethod(ObjectRef, get_luaentity),
1864 luamethod(ObjectRef, is_player),
1865 luamethod(ObjectRef, is_player_connected),
1866 luamethod(ObjectRef, get_player_name),
1867 luamethod(ObjectRef, get_player_velocity),
1868 luamethod(ObjectRef, get_look_dir),
1869 luamethod(ObjectRef, get_look_pitch),
1870 luamethod(ObjectRef, get_look_yaw),
1871 luamethod(ObjectRef, get_look_vertical),
1872 luamethod(ObjectRef, get_look_horizontal),
1873 luamethod(ObjectRef, set_look_horizontal),
1874 luamethod(ObjectRef, set_look_vertical),
1875 luamethod(ObjectRef, set_look_yaw),
1876 luamethod(ObjectRef, set_look_pitch),
1877 luamethod(ObjectRef, get_breath),
1878 luamethod(ObjectRef, set_breath),
1879 luamethod(ObjectRef, get_attribute),
1880 luamethod(ObjectRef, set_attribute),
1881 luamethod(ObjectRef, get_meta),
1882 luamethod(ObjectRef, set_inventory_formspec),
1883 luamethod(ObjectRef, get_inventory_formspec),
1884 luamethod(ObjectRef, set_formspec_prepend),
1885 luamethod(ObjectRef, get_formspec_prepend),
1886 luamethod(ObjectRef, get_player_control),
1887 luamethod(ObjectRef, get_player_control_bits),
1888 luamethod(ObjectRef, set_physics_override),
1889 luamethod(ObjectRef, get_physics_override),
1890 luamethod(ObjectRef, hud_add),
1891 luamethod(ObjectRef, hud_remove),
1892 luamethod(ObjectRef, hud_change),
1893 luamethod(ObjectRef, hud_get),
1894 luamethod(ObjectRef, hud_set_flags),
1895 luamethod(ObjectRef, hud_get_flags),
1896 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1897 luamethod(ObjectRef, hud_get_hotbar_itemcount),
1898 luamethod(ObjectRef, hud_set_hotbar_image),
1899 luamethod(ObjectRef, hud_get_hotbar_image),
1900 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1901 luamethod(ObjectRef, hud_get_hotbar_selected_image),
1902 luamethod(ObjectRef, set_sky),
1903 luamethod(ObjectRef, get_sky),
1904 luamethod(ObjectRef, set_clouds),
1905 luamethod(ObjectRef, get_clouds),
1906 luamethod(ObjectRef, override_day_night_ratio),
1907 luamethod(ObjectRef, get_day_night_ratio),
1908 luamethod(ObjectRef, set_local_animation),
1909 luamethod(ObjectRef, get_local_animation),
1910 luamethod(ObjectRef, set_eye_offset),
1911 luamethod(ObjectRef, get_eye_offset),