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 const std::unordered_set<int> &child_ids = co->getAttachmentChildIds();
107 for (int child_id : child_ids) {
108 // Child can be NULL if it was deleted earlier
109 if (ServerActiveObject *child = env->getActiveObject(child_id))
110 child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
113 verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
114 co->m_pending_removal = true;
119 // returns: {x=num, y=num, z=num}
120 int ObjectRef::l_get_pos(lua_State *L)
122 NO_MAP_LOCK_REQUIRED;
123 ObjectRef *ref = checkobject(L, 1);
124 ServerActiveObject *co = getobject(ref);
125 if (co == NULL) return 0;
126 v3f pos = co->getBasePosition() / BS;
128 lua_pushnumber(L, pos.X);
129 lua_setfield(L, -2, "x");
130 lua_pushnumber(L, pos.Y);
131 lua_setfield(L, -2, "y");
132 lua_pushnumber(L, pos.Z);
133 lua_setfield(L, -2, "z");
137 // set_pos(self, pos)
138 int ObjectRef::l_set_pos(lua_State *L)
140 NO_MAP_LOCK_REQUIRED;
141 ObjectRef *ref = checkobject(L, 1);
142 //LuaEntitySAO *co = getluaobject(ref);
143 ServerActiveObject *co = getobject(ref);
144 if (co == NULL) return 0;
146 v3f pos = checkFloatPos(L, 2);
152 // move_to(self, pos, continuous=false)
153 int ObjectRef::l_move_to(lua_State *L)
155 NO_MAP_LOCK_REQUIRED;
156 ObjectRef *ref = checkobject(L, 1);
157 //LuaEntitySAO *co = getluaobject(ref);
158 ServerActiveObject *co = getobject(ref);
159 if (co == NULL) return 0;
161 v3f pos = checkFloatPos(L, 2);
163 bool continuous = lua_toboolean(L, 3);
165 co->moveTo(pos, continuous);
169 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
170 int ObjectRef::l_punch(lua_State *L)
172 NO_MAP_LOCK_REQUIRED;
173 ObjectRef *ref = checkobject(L, 1);
174 ObjectRef *puncher_ref = checkobject(L, 2);
175 ServerActiveObject *co = getobject(ref);
176 ServerActiveObject *puncher = getobject(puncher_ref);
177 if (co == NULL) return 0;
178 if (puncher == NULL) return 0;
180 if (lua_type(L, 5) != LUA_TTABLE)
181 dir = co->getBasePosition() - puncher->getBasePosition();
183 dir = read_v3f(L, 5);
184 float time_from_last_punch = 1000000;
185 if (lua_isnumber(L, 3))
186 time_from_last_punch = lua_tonumber(L, 3);
187 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
190 s16 src_original_hp = co->getHP();
191 s16 dst_origin_hp = puncher->getHP();
194 co->punch(dir, &toolcap, puncher, time_from_last_punch);
196 // If the punched is a player, and its HP changed
197 if (src_original_hp != co->getHP() &&
198 co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
199 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
202 // If the puncher is a player, and its HP changed
203 if (dst_origin_hp != puncher->getHP() &&
204 puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
205 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
206 PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
211 // right_click(self, clicker); clicker = an another ObjectRef
212 int ObjectRef::l_right_click(lua_State *L)
214 NO_MAP_LOCK_REQUIRED;
215 ObjectRef *ref = checkobject(L, 1);
216 ObjectRef *ref2 = checkobject(L, 2);
217 ServerActiveObject *co = getobject(ref);
218 ServerActiveObject *co2 = getobject(ref2);
219 if (co == NULL) return 0;
220 if (co2 == NULL) return 0;
227 // hp = number of hitpoints (2 * number of hearts)
229 int ObjectRef::l_set_hp(lua_State *L)
231 NO_MAP_LOCK_REQUIRED;
234 ObjectRef *ref = checkobject(L, 1);
235 luaL_checknumber(L, 2);
236 ServerActiveObject *co = getobject(ref);
241 int hp = lua_tonumber(L, 2);
244 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
245 reason.from_mod = true;
246 if (lua_istable(L, 3)) {
249 lua_getfield(L, -1, "type");
250 if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) {
251 errorstream << "Bad type given!" << std::endl;
255 reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
259 co->setHP(hp, reason);
260 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
261 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
268 // returns: number of hitpoints (2 * number of hearts)
269 // 0 if not applicable to this type of object
270 int ObjectRef::l_get_hp(lua_State *L)
272 NO_MAP_LOCK_REQUIRED;
273 ObjectRef *ref = checkobject(L, 1);
274 ServerActiveObject *co = getobject(ref);
277 lua_pushnumber(L, 1);
280 int hp = co->getHP();
281 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
282 <<" hp="<<hp<<std::endl;*/
284 lua_pushnumber(L, hp);
288 // get_inventory(self)
289 int ObjectRef::l_get_inventory(lua_State *L)
291 NO_MAP_LOCK_REQUIRED;
292 ObjectRef *ref = checkobject(L, 1);
293 ServerActiveObject *co = getobject(ref);
294 if (co == NULL) return 0;
296 InventoryLocation loc = co->getInventoryLocation();
297 if (getServer(L)->getInventory(loc) != NULL)
298 InvRef::create(L, loc);
300 lua_pushnil(L); // An object may have no inventory (nil)
304 // get_wield_list(self)
305 int ObjectRef::l_get_wield_list(lua_State *L)
307 NO_MAP_LOCK_REQUIRED;
308 ObjectRef *ref = checkobject(L, 1);
309 ServerActiveObject *co = getobject(ref);
310 if (co == NULL) return 0;
312 lua_pushstring(L, co->getWieldList().c_str());
316 // get_wield_index(self)
317 int ObjectRef::l_get_wield_index(lua_State *L)
319 NO_MAP_LOCK_REQUIRED;
320 ObjectRef *ref = checkobject(L, 1);
321 ServerActiveObject *co = getobject(ref);
322 if (co == NULL) return 0;
324 lua_pushinteger(L, co->getWieldIndex() + 1);
328 // get_wielded_item(self)
329 int ObjectRef::l_get_wielded_item(lua_State *L)
331 NO_MAP_LOCK_REQUIRED;
332 ObjectRef *ref = checkobject(L, 1);
333 ServerActiveObject *co = getobject(ref);
336 LuaItemStack::create(L, ItemStack());
340 LuaItemStack::create(L, co->getWieldedItem());
344 // set_wielded_item(self, itemstack or itemstring or table or nil)
345 int ObjectRef::l_set_wielded_item(lua_State *L)
347 NO_MAP_LOCK_REQUIRED;
348 ObjectRef *ref = checkobject(L, 1);
349 ServerActiveObject *co = getobject(ref);
350 if (co == NULL) return 0;
352 ItemStack item = read_item(L, 2, getServer(L)->idef());
353 bool success = co->setWieldedItem(item);
354 if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
355 getServer(L)->SendInventory(((PlayerSAO*)co));
357 lua_pushboolean(L, success);
361 // set_armor_groups(self, groups)
362 int ObjectRef::l_set_armor_groups(lua_State *L)
364 NO_MAP_LOCK_REQUIRED;
365 ObjectRef *ref = checkobject(L, 1);
366 ServerActiveObject *co = getobject(ref);
367 if (co == NULL) return 0;
369 ItemGroupList groups;
370 read_groups(L, 2, groups);
371 co->setArmorGroups(groups);
375 // get_armor_groups(self)
376 int ObjectRef::l_get_armor_groups(lua_State *L)
378 NO_MAP_LOCK_REQUIRED;
379 ObjectRef *ref = checkobject(L, 1);
380 ServerActiveObject *co = getobject(ref);
384 push_groups(L, co->getArmorGroups());
388 // set_physics_override(self, physics_override_speed, physics_override_jump,
389 // physics_override_gravity, sneak, sneak_glitch, new_move)
390 int ObjectRef::l_set_physics_override(lua_State *L)
392 NO_MAP_LOCK_REQUIRED;
393 ObjectRef *ref = checkobject(L, 1);
394 PlayerSAO *co = (PlayerSAO *) getobject(ref);
395 if (co == NULL) return 0;
397 if (lua_istable(L, 2)) {
398 co->m_physics_override_speed = getfloatfield_default(
399 L, 2, "speed", co->m_physics_override_speed);
400 co->m_physics_override_jump = getfloatfield_default(
401 L, 2, "jump", co->m_physics_override_jump);
402 co->m_physics_override_gravity = getfloatfield_default(
403 L, 2, "gravity", co->m_physics_override_gravity);
404 co->m_physics_override_sneak = getboolfield_default(
405 L, 2, "sneak", co->m_physics_override_sneak);
406 co->m_physics_override_sneak_glitch = getboolfield_default(
407 L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
408 co->m_physics_override_new_move = getboolfield_default(
409 L, 2, "new_move", co->m_physics_override_new_move);
410 co->m_physics_override_sent = false;
412 // old, non-table format
413 if (!lua_isnil(L, 2)) {
414 co->m_physics_override_speed = lua_tonumber(L, 2);
415 co->m_physics_override_sent = false;
417 if (!lua_isnil(L, 3)) {
418 co->m_physics_override_jump = lua_tonumber(L, 3);
419 co->m_physics_override_sent = false;
421 if (!lua_isnil(L, 4)) {
422 co->m_physics_override_gravity = lua_tonumber(L, 4);
423 co->m_physics_override_sent = false;
429 // get_physics_override(self)
430 int ObjectRef::l_get_physics_override(lua_State *L)
432 NO_MAP_LOCK_REQUIRED;
433 ObjectRef *ref = checkobject(L, 1);
434 PlayerSAO *co = (PlayerSAO *)getobject(ref);
439 lua_pushnumber(L, co->m_physics_override_speed);
440 lua_setfield(L, -2, "speed");
441 lua_pushnumber(L, co->m_physics_override_jump);
442 lua_setfield(L, -2, "jump");
443 lua_pushnumber(L, co->m_physics_override_gravity);
444 lua_setfield(L, -2, "gravity");
445 lua_pushboolean(L, co->m_physics_override_sneak);
446 lua_setfield(L, -2, "sneak");
447 lua_pushboolean(L, co->m_physics_override_sneak_glitch);
448 lua_setfield(L, -2, "sneak_glitch");
449 lua_pushboolean(L, co->m_physics_override_new_move);
450 lua_setfield(L, -2, "new_move");
454 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
455 int ObjectRef::l_set_animation(lua_State *L)
457 NO_MAP_LOCK_REQUIRED;
458 ObjectRef *ref = checkobject(L, 1);
459 ServerActiveObject *co = getobject(ref);
460 if (co == NULL) return 0;
462 v2f frames = v2f(1, 1);
463 if (!lua_isnil(L, 2))
464 frames = read_v2f(L, 2);
465 float frame_speed = 15;
466 if (!lua_isnil(L, 3))
467 frame_speed = lua_tonumber(L, 3);
468 float frame_blend = 0;
469 if (!lua_isnil(L, 4))
470 frame_blend = lua_tonumber(L, 4);
471 bool frame_loop = true;
472 if (lua_isboolean(L, 5))
473 frame_loop = lua_toboolean(L, 5);
474 co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
478 // get_animation(self)
479 int ObjectRef::l_get_animation(lua_State *L)
481 NO_MAP_LOCK_REQUIRED;
482 ObjectRef *ref = checkobject(L, 1);
483 ServerActiveObject *co = getobject(ref);
487 v2f frames = v2f(1,1);
488 float frame_speed = 15;
489 float frame_blend = 0;
490 bool frame_loop = true;
491 co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
494 lua_pushnumber(L, frame_speed);
495 lua_pushnumber(L, frame_blend);
496 lua_pushboolean(L, frame_loop);
500 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
501 int ObjectRef::l_set_local_animation(lua_State *L)
503 NO_MAP_LOCK_REQUIRED;
504 ObjectRef *ref = checkobject(L, 1);
505 RemotePlayer *player = getplayer(ref);
510 for (int i=0;i<4;i++) {
511 if (!lua_isnil(L, 2+1))
512 frames[i] = read_v2s32(L, 2+i);
514 float frame_speed = 30;
515 if (!lua_isnil(L, 6))
516 frame_speed = lua_tonumber(L, 6);
518 getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
519 lua_pushboolean(L, true);
523 // get_local_animation(self)
524 int ObjectRef::l_get_local_animation(lua_State *L)
527 ObjectRef *ref = checkobject(L, 1);
528 RemotePlayer *player = getplayer(ref);
534 player->getLocalAnimations(frames, &frame_speed);
536 for (const v2s32 &frame : frames) {
537 push_v2s32(L, frame);
540 lua_pushnumber(L, frame_speed);
544 // set_eye_offset(self, v3f first pv, v3f third pv)
545 int ObjectRef::l_set_eye_offset(lua_State *L)
547 NO_MAP_LOCK_REQUIRED;
548 ObjectRef *ref = checkobject(L, 1);
549 RemotePlayer *player = getplayer(ref);
553 v3f offset_first = v3f(0, 0, 0);
554 v3f offset_third = v3f(0, 0, 0);
556 if (!lua_isnil(L, 2))
557 offset_first = read_v3f(L, 2);
558 if (!lua_isnil(L, 3))
559 offset_third = read_v3f(L, 3);
561 // Prevent abuse of offset values (keep player always visible)
562 offset_third.X = rangelim(offset_third.X,-10,10);
563 offset_third.Z = rangelim(offset_third.Z,-5,5);
564 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
565 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
567 getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
568 lua_pushboolean(L, true);
572 // get_eye_offset(self)
573 int ObjectRef::l_get_eye_offset(lua_State *L)
575 NO_MAP_LOCK_REQUIRED;
576 ObjectRef *ref = checkobject(L, 1);
577 RemotePlayer *player = getplayer(ref);
581 push_v3f(L, player->eye_offset_first);
582 push_v3f(L, player->eye_offset_third);
586 // set_animation_frame_speed(self, frame_speed)
587 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
589 NO_MAP_LOCK_REQUIRED;
590 ObjectRef *ref = checkobject(L, 1);
591 ServerActiveObject *co = getobject(ref);
596 if (!lua_isnil(L, 2)) {
597 float frame_speed = lua_tonumber(L, 2);
598 co->setAnimationSpeed(frame_speed);
599 lua_pushboolean(L, true);
601 lua_pushboolean(L, false);
606 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
607 int ObjectRef::l_set_bone_position(lua_State *L)
609 NO_MAP_LOCK_REQUIRED;
610 ObjectRef *ref = checkobject(L, 1);
611 ServerActiveObject *co = getobject(ref);
612 if (co == NULL) return 0;
615 if (!lua_isnil(L, 2))
616 bone = lua_tostring(L, 2);
617 v3f position = v3f(0, 0, 0);
618 if (!lua_isnil(L, 3))
619 position = check_v3f(L, 3);
620 v3f rotation = v3f(0, 0, 0);
621 if (!lua_isnil(L, 4))
622 rotation = check_v3f(L, 4);
623 co->setBonePosition(bone, position, rotation);
627 // get_bone_position(self, bone)
628 int ObjectRef::l_get_bone_position(lua_State *L)
630 NO_MAP_LOCK_REQUIRED;
631 ObjectRef *ref = checkobject(L, 1);
632 ServerActiveObject *co = getobject(ref);
637 if (!lua_isnil(L, 2))
638 bone = lua_tostring(L, 2);
640 v3f position = v3f(0, 0, 0);
641 v3f rotation = v3f(0, 0, 0);
642 co->getBonePosition(bone, &position, &rotation);
644 push_v3f(L, position);
645 push_v3f(L, rotation);
649 // set_attach(self, parent, bone, position, rotation)
650 int ObjectRef::l_set_attach(lua_State *L)
654 ObjectRef *ref = checkobject(L, 1);
655 ObjectRef *parent_ref = checkobject(L, 2);
656 ServerActiveObject *co = getobject(ref);
657 ServerActiveObject *parent = getobject(parent_ref);
665 v3f position = v3f(0, 0, 0);
666 v3f rotation = v3f(0, 0, 0);
667 co->getAttachment(&parent_id, &bone, &position, &rotation);
669 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
670 old_parent->removeAttachmentChild(co->getId());
674 if (!lua_isnil(L, 3))
675 bone = lua_tostring(L, 3);
676 position = v3f(0, 0, 0);
677 if (!lua_isnil(L, 4))
678 position = read_v3f(L, 4);
679 rotation = v3f(0, 0, 0);
680 if (!lua_isnil(L, 5))
681 rotation = read_v3f(L, 5);
682 co->setAttachment(parent->getId(), bone, position, rotation);
683 parent->addAttachmentChild(co->getId());
688 int ObjectRef::l_get_attach(lua_State *L)
692 ObjectRef *ref = checkobject(L, 1);
693 ServerActiveObject *co = getobject(ref);
700 v3f position = v3f(0, 0, 0);
701 v3f rotation = v3f(0, 0, 0);
702 co->getAttachment(&parent_id, &bone, &position, &rotation);
705 ServerActiveObject *parent = env->getActiveObject(parent_id);
707 getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
708 lua_pushlstring(L, bone.c_str(), bone.size());
709 push_v3f(L, position);
710 push_v3f(L, rotation);
715 int ObjectRef::l_set_detach(lua_State *L)
719 ObjectRef *ref = checkobject(L, 1);
720 ServerActiveObject *co = getobject(ref);
728 co->getAttachment(&parent_id, &bone, &position, &rotation);
729 ServerActiveObject *parent = NULL;
731 parent = env->getActiveObject(parent_id);
732 co->setAttachment(0, "", position, rotation);
734 co->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
738 parent->removeAttachmentChild(co->getId());
742 // set_properties(self, properties)
743 int ObjectRef::l_set_properties(lua_State *L)
745 NO_MAP_LOCK_REQUIRED;
746 ObjectRef *ref = checkobject(L, 1);
747 ServerActiveObject *co = getobject(ref);
748 if (co == NULL) return 0;
749 ObjectProperties *prop = co->accessObjectProperties();
752 read_object_properties(L, 2, prop, getServer(L)->idef());
753 if (prop->hp_max < co->getHP()) {
754 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
755 co->setHP(prop->hp_max, reason);
756 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
757 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
759 co->notifyObjectPropertiesModified();
763 // get_properties(self)
764 int ObjectRef::l_get_properties(lua_State *L)
766 NO_MAP_LOCK_REQUIRED;
767 ObjectRef *ref = checkobject(L, 1);
768 ServerActiveObject *co = getobject(ref);
771 ObjectProperties *prop = co->accessObjectProperties();
774 push_object_properties(L, prop);
779 int ObjectRef::l_is_player(lua_State *L)
781 NO_MAP_LOCK_REQUIRED;
782 ObjectRef *ref = checkobject(L, 1);
783 RemotePlayer *player = getplayer(ref);
784 lua_pushboolean(L, (player != NULL));
788 // set_nametag_attributes(self, attributes)
789 int ObjectRef::l_set_nametag_attributes(lua_State *L)
791 NO_MAP_LOCK_REQUIRED;
792 ObjectRef *ref = checkobject(L, 1);
793 ServerActiveObject *co = getobject(ref);
797 ObjectProperties *prop = co->accessObjectProperties();
801 lua_getfield(L, 2, "color");
802 if (!lua_isnil(L, -1)) {
803 video::SColor color = prop->nametag_color;
804 read_color(L, -1, &color);
805 prop->nametag_color = color;
809 std::string nametag = getstringfield_default(L, 2, "text", "");
810 prop->nametag = nametag;
812 co->notifyObjectPropertiesModified();
813 lua_pushboolean(L, true);
817 // get_nametag_attributes(self)
818 int ObjectRef::l_get_nametag_attributes(lua_State *L)
820 NO_MAP_LOCK_REQUIRED;
821 ObjectRef *ref = checkobject(L, 1);
822 ServerActiveObject *co = getobject(ref);
826 ObjectProperties *prop = co->accessObjectProperties();
830 video::SColor color = prop->nametag_color;
833 push_ARGB8(L, color);
834 lua_setfield(L, -2, "color");
835 lua_pushstring(L, prop->nametag.c_str());
836 lua_setfield(L, -2, "text");
840 /* LuaEntitySAO-only */
842 // set_velocity(self, {x=num, y=num, z=num})
843 int ObjectRef::l_set_velocity(lua_State *L)
845 NO_MAP_LOCK_REQUIRED;
846 ObjectRef *ref = checkobject(L, 1);
847 LuaEntitySAO *co = getluaobject(ref);
848 if (co == NULL) return 0;
849 v3f pos = checkFloatPos(L, 2);
851 co->setVelocity(pos);
855 // add_velocity(self, {x=num, y=num, z=num})
856 int ObjectRef::l_add_velocity(lua_State *L)
858 NO_MAP_LOCK_REQUIRED;
859 ObjectRef *ref = checkobject(L, 1);
860 LuaEntitySAO *co = getluaobject(ref);
863 v3f pos = checkFloatPos(L, 2);
865 co->addVelocity(pos);
869 // get_velocity(self)
870 int ObjectRef::l_get_velocity(lua_State *L)
872 NO_MAP_LOCK_REQUIRED;
873 ObjectRef *ref = checkobject(L, 1);
874 LuaEntitySAO *co = getluaobject(ref);
875 if (co == NULL) return 0;
877 v3f v = co->getVelocity();
882 // set_acceleration(self, {x=num, y=num, z=num})
883 int ObjectRef::l_set_acceleration(lua_State *L)
885 NO_MAP_LOCK_REQUIRED;
886 ObjectRef *ref = checkobject(L, 1);
887 LuaEntitySAO *co = getluaobject(ref);
888 if (co == NULL) return 0;
890 v3f pos = checkFloatPos(L, 2);
892 co->setAcceleration(pos);
896 // get_acceleration(self)
897 int ObjectRef::l_get_acceleration(lua_State *L)
899 NO_MAP_LOCK_REQUIRED;
900 ObjectRef *ref = checkobject(L, 1);
901 LuaEntitySAO *co = getluaobject(ref);
902 if (co == NULL) return 0;
904 v3f v = co->getAcceleration();
909 // set_yaw(self, radians)
910 int ObjectRef::l_set_yaw(lua_State *L)
912 NO_MAP_LOCK_REQUIRED;
913 ObjectRef *ref = checkobject(L, 1);
914 LuaEntitySAO *co = getluaobject(ref);
915 if (co == NULL) return 0;
916 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
923 int ObjectRef::l_get_yaw(lua_State *L)
925 NO_MAP_LOCK_REQUIRED;
926 ObjectRef *ref = checkobject(L, 1);
927 LuaEntitySAO *co = getluaobject(ref);
928 if (co == NULL) return 0;
930 float yaw = co->getYaw() * core::DEGTORAD;
931 lua_pushnumber(L, yaw);
935 // set_texture_mod(self, mod)
936 int ObjectRef::l_set_texture_mod(lua_State *L)
938 NO_MAP_LOCK_REQUIRED;
939 ObjectRef *ref = checkobject(L, 1);
940 LuaEntitySAO *co = getluaobject(ref);
941 if (co == NULL) return 0;
943 std::string mod = luaL_checkstring(L, 2);
944 co->setTextureMod(mod);
948 // get_texture_mod(self)
949 int ObjectRef::l_get_texture_mod(lua_State *L)
951 NO_MAP_LOCK_REQUIRED;
952 ObjectRef *ref = checkobject(L, 1);
953 LuaEntitySAO *co = getluaobject(ref);
954 if (co == NULL) return 0;
956 std::string mod = co->getTextureMod();
957 lua_pushstring(L, mod.c_str());
961 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
962 // select_horiz_by_yawpitch=false)
963 int ObjectRef::l_set_sprite(lua_State *L)
965 NO_MAP_LOCK_REQUIRED;
966 ObjectRef *ref = checkobject(L, 1);
967 LuaEntitySAO *co = getluaobject(ref);
968 if (co == NULL) return 0;
971 if (!lua_isnil(L, 2))
972 p = read_v2s16(L, 2);
974 if (!lua_isnil(L, 3))
975 num_frames = lua_tonumber(L, 3);
976 float framelength = 0.2;
977 if (!lua_isnil(L, 4))
978 framelength = lua_tonumber(L, 4);
979 bool select_horiz_by_yawpitch = false;
980 if (!lua_isnil(L, 5))
981 select_horiz_by_yawpitch = lua_toboolean(L, 5);
982 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
987 // get_entity_name(self)
988 int ObjectRef::l_get_entity_name(lua_State *L)
990 NO_MAP_LOCK_REQUIRED;
991 ObjectRef *ref = checkobject(L, 1);
992 LuaEntitySAO *co = getluaobject(ref);
993 log_deprecated(L,"Deprecated call to \"get_entity_name");
994 if (co == NULL) return 0;
996 std::string name = co->getName();
997 lua_pushstring(L, name.c_str());
1001 // get_luaentity(self)
1002 int ObjectRef::l_get_luaentity(lua_State *L)
1004 NO_MAP_LOCK_REQUIRED;
1005 ObjectRef *ref = checkobject(L, 1);
1006 LuaEntitySAO *co = getluaobject(ref);
1007 if (co == NULL) return 0;
1009 luaentity_get(L, co->getId());
1015 // is_player_connected(self)
1016 int ObjectRef::l_is_player_connected(lua_State *L)
1018 NO_MAP_LOCK_REQUIRED;
1019 ObjectRef *ref = checkobject(L, 1);
1020 RemotePlayer *player = getplayer(ref);
1021 lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
1025 // get_player_name(self)
1026 int ObjectRef::l_get_player_name(lua_State *L)
1028 NO_MAP_LOCK_REQUIRED;
1029 ObjectRef *ref = checkobject(L, 1);
1030 RemotePlayer *player = getplayer(ref);
1031 if (player == NULL) {
1032 lua_pushlstring(L, "", 0);
1036 lua_pushstring(L, player->getName());
1040 // get_player_velocity(self)
1041 int ObjectRef::l_get_player_velocity(lua_State *L)
1043 NO_MAP_LOCK_REQUIRED;
1044 ObjectRef *ref = checkobject(L, 1);
1045 RemotePlayer *player = getplayer(ref);
1046 if (player == NULL) {
1051 push_v3f(L, player->getSpeed() / BS);
1055 // get_look_dir(self)
1056 int ObjectRef::l_get_look_dir(lua_State *L)
1058 NO_MAP_LOCK_REQUIRED;
1059 ObjectRef *ref = checkobject(L, 1);
1060 PlayerSAO* co = getplayersao(ref);
1061 if (co == NULL) return 0;
1063 float pitch = co->getRadPitchDep();
1064 float yaw = co->getRadYawDep();
1065 v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
1072 // get_look_pitch(self)
1073 int ObjectRef::l_get_look_pitch(lua_State *L)
1075 NO_MAP_LOCK_REQUIRED;
1078 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1080 ObjectRef *ref = checkobject(L, 1);
1081 PlayerSAO* co = getplayersao(ref);
1082 if (co == NULL) return 0;
1084 lua_pushnumber(L, co->getRadPitchDep());
1089 // get_look_yaw(self)
1090 int ObjectRef::l_get_look_yaw(lua_State *L)
1092 NO_MAP_LOCK_REQUIRED;
1095 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1097 ObjectRef *ref = checkobject(L, 1);
1098 PlayerSAO* co = getplayersao(ref);
1099 if (co == NULL) return 0;
1101 lua_pushnumber(L, co->getRadYawDep());
1105 // get_look_pitch2(self)
1106 int ObjectRef::l_get_look_vertical(lua_State *L)
1108 NO_MAP_LOCK_REQUIRED;
1109 ObjectRef *ref = checkobject(L, 1);
1110 PlayerSAO* co = getplayersao(ref);
1111 if (co == NULL) return 0;
1113 lua_pushnumber(L, co->getRadPitch());
1117 // get_look_yaw2(self)
1118 int ObjectRef::l_get_look_horizontal(lua_State *L)
1120 NO_MAP_LOCK_REQUIRED;
1121 ObjectRef *ref = checkobject(L, 1);
1122 PlayerSAO* co = getplayersao(ref);
1123 if (co == NULL) return 0;
1125 lua_pushnumber(L, co->getRadYaw());
1129 // set_look_vertical(self, radians)
1130 int ObjectRef::l_set_look_vertical(lua_State *L)
1132 NO_MAP_LOCK_REQUIRED;
1133 ObjectRef *ref = checkobject(L, 1);
1134 PlayerSAO* co = getplayersao(ref);
1135 if (co == NULL) return 0;
1136 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1138 co->setPitchAndSend(pitch);
1142 // set_look_horizontal(self, radians)
1143 int ObjectRef::l_set_look_horizontal(lua_State *L)
1145 NO_MAP_LOCK_REQUIRED;
1146 ObjectRef *ref = checkobject(L, 1);
1147 PlayerSAO* co = getplayersao(ref);
1148 if (co == NULL) return 0;
1149 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1151 co->setYawAndSend(yaw);
1156 // set_look_pitch(self, radians)
1157 int ObjectRef::l_set_look_pitch(lua_State *L)
1159 NO_MAP_LOCK_REQUIRED;
1162 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1164 ObjectRef *ref = checkobject(L, 1);
1165 PlayerSAO* co = getplayersao(ref);
1166 if (co == NULL) return 0;
1167 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1169 co->setPitchAndSend(pitch);
1174 // set_look_yaw(self, radians)
1175 int ObjectRef::l_set_look_yaw(lua_State *L)
1177 NO_MAP_LOCK_REQUIRED;
1180 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1182 ObjectRef *ref = checkobject(L, 1);
1183 PlayerSAO* co = getplayersao(ref);
1184 if (co == NULL) return 0;
1185 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1187 co->setYawAndSend(yaw);
1191 // set_breath(self, breath)
1192 int ObjectRef::l_set_breath(lua_State *L)
1194 NO_MAP_LOCK_REQUIRED;
1195 ObjectRef *ref = checkobject(L, 1);
1196 PlayerSAO* co = getplayersao(ref);
1197 if (co == NULL) return 0;
1198 u16 breath = luaL_checknumber(L, 2);
1199 co->setBreath(breath);
1205 int ObjectRef::l_get_breath(lua_State *L)
1207 NO_MAP_LOCK_REQUIRED;
1208 ObjectRef *ref = checkobject(L, 1);
1209 PlayerSAO* co = getplayersao(ref);
1210 if (co == NULL) return 0;
1212 u16 breath = co->getBreath();
1213 lua_pushinteger (L, breath);
1217 // set_attribute(self, attribute, value)
1218 int ObjectRef::l_set_attribute(lua_State *L)
1220 ObjectRef *ref = checkobject(L, 1);
1221 PlayerSAO* co = getplayersao(ref);
1225 std::string attr = luaL_checkstring(L, 2);
1226 if (lua_isnil(L, 3)) {
1227 co->getMeta().removeString(attr);
1229 std::string value = luaL_checkstring(L, 3);
1230 co->getMeta().setString(attr, value);
1235 // get_attribute(self, attribute)
1236 int ObjectRef::l_get_attribute(lua_State *L)
1238 ObjectRef *ref = checkobject(L, 1);
1239 PlayerSAO* co = getplayersao(ref);
1243 std::string attr = luaL_checkstring(L, 2);
1246 if (co->getMeta().getStringToRef(attr, value)) {
1247 lua_pushstring(L, value.c_str());
1255 // get_meta(self, attribute)
1256 int ObjectRef::l_get_meta(lua_State *L)
1258 ObjectRef *ref = checkobject(L, 1);
1259 PlayerSAO *co = getplayersao(ref);
1263 PlayerMetaRef::create(L, &co->getMeta());
1268 // set_inventory_formspec(self, formspec)
1269 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1271 NO_MAP_LOCK_REQUIRED;
1272 ObjectRef *ref = checkobject(L, 1);
1273 RemotePlayer *player = getplayer(ref);
1274 if (player == NULL) return 0;
1275 std::string formspec = luaL_checkstring(L, 2);
1277 player->inventory_formspec = formspec;
1278 getServer(L)->reportInventoryFormspecModified(player->getName());
1279 lua_pushboolean(L, true);
1283 // get_inventory_formspec(self) -> formspec
1284 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1286 NO_MAP_LOCK_REQUIRED;
1287 ObjectRef *ref = checkobject(L, 1);
1288 RemotePlayer *player = getplayer(ref);
1289 if (player == NULL) return 0;
1291 std::string formspec = player->inventory_formspec;
1292 lua_pushlstring(L, formspec.c_str(), formspec.size());
1296 // set_formspec_prepend(self, formspec)
1297 int ObjectRef::l_set_formspec_prepend(lua_State *L)
1299 NO_MAP_LOCK_REQUIRED;
1300 ObjectRef *ref = checkobject(L, 1);
1301 RemotePlayer *player = getplayer(ref);
1305 std::string formspec = luaL_checkstring(L, 2);
1307 player->formspec_prepend = formspec;
1308 getServer(L)->reportFormspecPrependModified(player->getName());
1309 lua_pushboolean(L, true);
1313 // get_formspec_prepend(self) -> formspec
1314 int ObjectRef::l_get_formspec_prepend(lua_State *L)
1316 NO_MAP_LOCK_REQUIRED;
1317 ObjectRef *ref = checkobject(L, 1);
1318 RemotePlayer *player = getplayer(ref);
1322 std::string formspec = player->formspec_prepend;
1323 lua_pushlstring(L, formspec.c_str(), formspec.size());
1327 // get_player_control(self)
1328 int ObjectRef::l_get_player_control(lua_State *L)
1330 NO_MAP_LOCK_REQUIRED;
1331 ObjectRef *ref = checkobject(L, 1);
1332 RemotePlayer *player = getplayer(ref);
1333 if (player == NULL) {
1334 lua_pushlstring(L, "", 0);
1338 const PlayerControl &control = player->getPlayerControl();
1340 lua_pushboolean(L, control.up);
1341 lua_setfield(L, -2, "up");
1342 lua_pushboolean(L, control.down);
1343 lua_setfield(L, -2, "down");
1344 lua_pushboolean(L, control.left);
1345 lua_setfield(L, -2, "left");
1346 lua_pushboolean(L, control.right);
1347 lua_setfield(L, -2, "right");
1348 lua_pushboolean(L, control.jump);
1349 lua_setfield(L, -2, "jump");
1350 lua_pushboolean(L, control.aux1);
1351 lua_setfield(L, -2, "aux1");
1352 lua_pushboolean(L, control.sneak);
1353 lua_setfield(L, -2, "sneak");
1354 lua_pushboolean(L, control.LMB);
1355 lua_setfield(L, -2, "LMB");
1356 lua_pushboolean(L, control.RMB);
1357 lua_setfield(L, -2, "RMB");
1361 // get_player_control_bits(self)
1362 int ObjectRef::l_get_player_control_bits(lua_State *L)
1364 NO_MAP_LOCK_REQUIRED;
1365 ObjectRef *ref = checkobject(L, 1);
1366 RemotePlayer *player = getplayer(ref);
1367 if (player == NULL) {
1368 lua_pushlstring(L, "", 0);
1372 lua_pushnumber(L, player->keyPressed);
1376 // hud_add(self, form)
1377 int ObjectRef::l_hud_add(lua_State *L)
1379 NO_MAP_LOCK_REQUIRED;
1380 ObjectRef *ref = checkobject(L, 1);
1381 RemotePlayer *player = getplayer(ref);
1385 HudElement *elem = new HudElement;
1386 read_hud_element(L, elem);
1388 u32 id = getServer(L)->hudAdd(player, elem);
1389 if (id == U32_MAX) {
1394 lua_pushnumber(L, id);
1398 // hud_remove(self, id)
1399 int ObjectRef::l_hud_remove(lua_State *L)
1401 NO_MAP_LOCK_REQUIRED;
1402 ObjectRef *ref = checkobject(L, 1);
1403 RemotePlayer *player = getplayer(ref);
1408 if (!lua_isnil(L, 2))
1409 id = lua_tonumber(L, 2);
1411 if (!getServer(L)->hudRemove(player, id))
1414 lua_pushboolean(L, true);
1418 // hud_change(self, id, stat, data)
1419 int ObjectRef::l_hud_change(lua_State *L)
1421 NO_MAP_LOCK_REQUIRED;
1422 ObjectRef *ref = checkobject(L, 1);
1423 RemotePlayer *player = getplayer(ref);
1427 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1429 HudElement *e = player->getHud(id);
1434 HudElementStat stat = read_hud_change(L, e, &value);
1436 getServer(L)->hudChange(player, id, stat, value);
1438 lua_pushboolean(L, true);
1442 // hud_get(self, id)
1443 int ObjectRef::l_hud_get(lua_State *L)
1445 NO_MAP_LOCK_REQUIRED;
1446 ObjectRef *ref = checkobject(L, 1);
1447 RemotePlayer *player = getplayer(ref);
1451 u32 id = lua_tonumber(L, -1);
1453 HudElement *e = player->getHud(id);
1456 push_hud_element(L, e);
1460 // hud_set_flags(self, flags)
1461 int ObjectRef::l_hud_set_flags(lua_State *L)
1463 NO_MAP_LOCK_REQUIRED;
1464 ObjectRef *ref = checkobject(L, 1);
1465 RemotePlayer *player = getplayer(ref);
1473 const EnumString *esp = es_HudBuiltinElement;
1474 for (int i = 0; esp[i].str; i++) {
1475 if (getboolfield(L, 2, esp[i].str, flag)) {
1476 flags |= esp[i].num * flag;
1480 if (!getServer(L)->hudSetFlags(player, flags, mask))
1483 lua_pushboolean(L, true);
1487 int ObjectRef::l_hud_get_flags(lua_State *L)
1489 NO_MAP_LOCK_REQUIRED;
1490 ObjectRef *ref = checkobject(L, 1);
1491 RemotePlayer *player = getplayer(ref);
1496 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1497 lua_setfield(L, -2, "hotbar");
1498 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1499 lua_setfield(L, -2, "healthbar");
1500 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1501 lua_setfield(L, -2, "crosshair");
1502 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1503 lua_setfield(L, -2, "wielditem");
1504 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1505 lua_setfield(L, -2, "breathbar");
1506 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1507 lua_setfield(L, -2, "minimap");
1508 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
1509 lua_setfield(L, -2, "minimap_radar");
1514 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1515 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1517 NO_MAP_LOCK_REQUIRED;
1518 ObjectRef *ref = checkobject(L, 1);
1519 RemotePlayer *player = getplayer(ref);
1523 s32 hotbar_itemcount = lua_tonumber(L, 2);
1525 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1528 lua_pushboolean(L, true);
1532 // hud_get_hotbar_itemcount(self)
1533 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1535 NO_MAP_LOCK_REQUIRED;
1536 ObjectRef *ref = checkobject(L, 1);
1537 RemotePlayer *player = getplayer(ref);
1541 lua_pushnumber(L, player->getHotbarItemcount());
1545 // hud_set_hotbar_image(self, name)
1546 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1548 NO_MAP_LOCK_REQUIRED;
1549 ObjectRef *ref = checkobject(L, 1);
1550 RemotePlayer *player = getplayer(ref);
1554 std::string name = lua_tostring(L, 2);
1556 getServer(L)->hudSetHotbarImage(player, name);
1560 // hud_get_hotbar_image(self)
1561 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1563 NO_MAP_LOCK_REQUIRED;
1564 ObjectRef *ref = checkobject(L, 1);
1565 RemotePlayer *player = getplayer(ref);
1569 const std::string &name = player->getHotbarImage();
1570 lua_pushlstring(L, name.c_str(), name.size());
1574 // hud_set_hotbar_selected_image(self, name)
1575 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1577 NO_MAP_LOCK_REQUIRED;
1578 ObjectRef *ref = checkobject(L, 1);
1579 RemotePlayer *player = getplayer(ref);
1583 std::string name = lua_tostring(L, 2);
1585 getServer(L)->hudSetHotbarSelectedImage(player, name);
1589 // hud_get_hotbar_selected_image(self)
1590 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1592 NO_MAP_LOCK_REQUIRED;
1593 ObjectRef *ref = checkobject(L, 1);
1594 RemotePlayer *player = getplayer(ref);
1598 const std::string &name = player->getHotbarSelectedImage();
1599 lua_pushlstring(L, name.c_str(), name.size());
1603 // set_sky(self, bgcolor, type, list, clouds = true)
1604 int ObjectRef::l_set_sky(lua_State *L)
1606 NO_MAP_LOCK_REQUIRED;
1607 ObjectRef *ref = checkobject(L, 1);
1608 RemotePlayer *player = getplayer(ref);
1612 video::SColor bgcolor(255,255,255,255);
1613 read_color(L, 2, &bgcolor);
1615 std::string type = luaL_checkstring(L, 3);
1617 std::vector<std::string> params;
1618 if (lua_istable(L, 4)) {
1620 while (lua_next(L, 4) != 0) {
1621 // key at index -2 and value at index -1
1622 if (lua_isstring(L, -1))
1623 params.emplace_back(lua_tostring(L, -1));
1625 params.emplace_back("");
1626 // removes value, keeps key for next iteration
1631 if (type == "skybox" && params.size() != 6)
1632 throw LuaError("skybox expects 6 textures");
1635 if (lua_isboolean(L, 5))
1636 clouds = lua_toboolean(L, 5);
1638 getServer(L)->setSky(player, bgcolor, type, params, clouds);
1639 lua_pushboolean(L, true);
1644 int ObjectRef::l_get_sky(lua_State *L)
1646 NO_MAP_LOCK_REQUIRED;
1647 ObjectRef *ref = checkobject(L, 1);
1648 RemotePlayer *player = getplayer(ref);
1651 video::SColor bgcolor(255, 255, 255, 255);
1653 std::vector<std::string> params;
1656 player->getSky(&bgcolor, &type, ¶ms, &clouds);
1657 type = type.empty() ? "regular" : type;
1659 push_ARGB8(L, bgcolor);
1660 lua_pushlstring(L, type.c_str(), type.size());
1663 for (const std::string ¶m : params) {
1664 lua_pushlstring(L, param.c_str(), param.size());
1665 lua_rawseti(L, -2, i);
1668 lua_pushboolean(L, clouds);
1672 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1673 int ObjectRef::l_set_clouds(lua_State *L)
1675 NO_MAP_LOCK_REQUIRED;
1676 ObjectRef *ref = checkobject(L, 1);
1677 RemotePlayer *player = getplayer(ref);
1680 if (!lua_istable(L, 2))
1683 CloudParams cloud_params = player->getCloudParams();
1685 cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1687 lua_getfield(L, 2, "color");
1688 if (!lua_isnil(L, -1))
1689 read_color(L, -1, &cloud_params.color_bright);
1691 lua_getfield(L, 2, "ambient");
1692 if (!lua_isnil(L, -1))
1693 read_color(L, -1, &cloud_params.color_ambient);
1696 cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
1697 cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1699 lua_getfield(L, 2, "speed");
1700 if (lua_istable(L, -1)) {
1702 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1703 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1704 cloud_params.speed = new_speed;
1708 getServer(L)->setClouds(player, cloud_params);
1709 lua_pushboolean(L, true);
1713 int ObjectRef::l_get_clouds(lua_State *L)
1715 NO_MAP_LOCK_REQUIRED;
1716 ObjectRef *ref = checkobject(L, 1);
1717 RemotePlayer *player = getplayer(ref);
1720 const CloudParams &cloud_params = player->getCloudParams();
1723 lua_pushnumber(L, cloud_params.density);
1724 lua_setfield(L, -2, "density");
1725 push_ARGB8(L, cloud_params.color_bright);
1726 lua_setfield(L, -2, "color");
1727 push_ARGB8(L, cloud_params.color_ambient);
1728 lua_setfield(L, -2, "ambient");
1729 lua_pushnumber(L, cloud_params.height);
1730 lua_setfield(L, -2, "height");
1731 lua_pushnumber(L, cloud_params.thickness);
1732 lua_setfield(L, -2, "thickness");
1734 lua_pushnumber(L, cloud_params.speed.X);
1735 lua_setfield(L, -2, "x");
1736 lua_pushnumber(L, cloud_params.speed.Y);
1737 lua_setfield(L, -2, "y");
1738 lua_setfield(L, -2, "speed");
1744 // override_day_night_ratio(self, brightness=0...1)
1745 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1747 NO_MAP_LOCK_REQUIRED;
1748 ObjectRef *ref = checkobject(L, 1);
1749 RemotePlayer *player = getplayer(ref);
1753 bool do_override = false;
1755 if (!lua_isnil(L, 2)) {
1757 ratio = luaL_checknumber(L, 2);
1760 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1763 lua_pushboolean(L, true);
1767 // get_day_night_ratio(self)
1768 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1770 NO_MAP_LOCK_REQUIRED;
1771 ObjectRef *ref = checkobject(L, 1);
1772 RemotePlayer *player = getplayer(ref);
1778 player->getDayNightRatio(&do_override, &ratio);
1781 lua_pushnumber(L, ratio);
1788 ObjectRef::ObjectRef(ServerActiveObject *object):
1791 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1794 // Creates an ObjectRef and leaves it on top of stack
1795 // Not callable from Lua; all references are created on the C side.
1796 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1798 ObjectRef *o = new ObjectRef(object);
1799 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1800 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1801 luaL_getmetatable(L, className);
1802 lua_setmetatable(L, -2);
1805 void ObjectRef::set_null(lua_State *L)
1807 ObjectRef *o = checkobject(L, -1);
1811 void ObjectRef::Register(lua_State *L)
1814 int methodtable = lua_gettop(L);
1815 luaL_newmetatable(L, className);
1816 int metatable = lua_gettop(L);
1818 lua_pushliteral(L, "__metatable");
1819 lua_pushvalue(L, methodtable);
1820 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1822 lua_pushliteral(L, "__index");
1823 lua_pushvalue(L, methodtable);
1824 lua_settable(L, metatable);
1826 lua_pushliteral(L, "__gc");
1827 lua_pushcfunction(L, gc_object);
1828 lua_settable(L, metatable);
1830 lua_pop(L, 1); // drop metatable
1832 luaL_openlib(L, 0, methods, 0); // fill methodtable
1833 lua_pop(L, 1); // drop methodtable
1835 // Cannot be created from Lua
1836 //lua_register(L, className, create_object);
1839 const char ObjectRef::className[] = "ObjectRef";
1840 const luaL_Reg ObjectRef::methods[] = {
1841 // ServerActiveObject
1842 luamethod(ObjectRef, remove),
1843 luamethod_aliased(ObjectRef, get_pos, getpos),
1844 luamethod_aliased(ObjectRef, set_pos, setpos),
1845 luamethod_aliased(ObjectRef, move_to, moveto),
1846 luamethod(ObjectRef, punch),
1847 luamethod(ObjectRef, right_click),
1848 luamethod(ObjectRef, set_hp),
1849 luamethod(ObjectRef, get_hp),
1850 luamethod(ObjectRef, get_inventory),
1851 luamethod(ObjectRef, get_wield_list),
1852 luamethod(ObjectRef, get_wield_index),
1853 luamethod(ObjectRef, get_wielded_item),
1854 luamethod(ObjectRef, set_wielded_item),
1855 luamethod(ObjectRef, set_armor_groups),
1856 luamethod(ObjectRef, get_armor_groups),
1857 luamethod(ObjectRef, set_animation),
1858 luamethod(ObjectRef, get_animation),
1859 luamethod(ObjectRef, set_animation_frame_speed),
1860 luamethod(ObjectRef, set_bone_position),
1861 luamethod(ObjectRef, get_bone_position),
1862 luamethod(ObjectRef, set_attach),
1863 luamethod(ObjectRef, get_attach),
1864 luamethod(ObjectRef, set_detach),
1865 luamethod(ObjectRef, set_properties),
1866 luamethod(ObjectRef, get_properties),
1867 luamethod(ObjectRef, set_nametag_attributes),
1868 luamethod(ObjectRef, get_nametag_attributes),
1869 // LuaEntitySAO-only
1870 luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1871 luamethod(ObjectRef, add_velocity),
1872 luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1873 luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1874 luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1875 luamethod_aliased(ObjectRef, set_yaw, setyaw),
1876 luamethod_aliased(ObjectRef, get_yaw, getyaw),
1877 luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1878 luamethod_aliased(ObjectRef, set_sprite, setsprite),
1879 luamethod(ObjectRef, get_entity_name),
1880 luamethod(ObjectRef, get_luaentity),
1882 luamethod(ObjectRef, is_player),
1883 luamethod(ObjectRef, is_player_connected),
1884 luamethod(ObjectRef, get_player_name),
1885 luamethod(ObjectRef, get_player_velocity),
1886 luamethod(ObjectRef, get_look_dir),
1887 luamethod(ObjectRef, get_look_pitch),
1888 luamethod(ObjectRef, get_look_yaw),
1889 luamethod(ObjectRef, get_look_vertical),
1890 luamethod(ObjectRef, get_look_horizontal),
1891 luamethod(ObjectRef, set_look_horizontal),
1892 luamethod(ObjectRef, set_look_vertical),
1893 luamethod(ObjectRef, set_look_yaw),
1894 luamethod(ObjectRef, set_look_pitch),
1895 luamethod(ObjectRef, get_breath),
1896 luamethod(ObjectRef, set_breath),
1897 luamethod(ObjectRef, get_attribute),
1898 luamethod(ObjectRef, set_attribute),
1899 luamethod(ObjectRef, get_meta),
1900 luamethod(ObjectRef, set_inventory_formspec),
1901 luamethod(ObjectRef, get_inventory_formspec),
1902 luamethod(ObjectRef, set_formspec_prepend),
1903 luamethod(ObjectRef, get_formspec_prepend),
1904 luamethod(ObjectRef, get_player_control),
1905 luamethod(ObjectRef, get_player_control_bits),
1906 luamethod(ObjectRef, set_physics_override),
1907 luamethod(ObjectRef, get_physics_override),
1908 luamethod(ObjectRef, hud_add),
1909 luamethod(ObjectRef, hud_remove),
1910 luamethod(ObjectRef, hud_change),
1911 luamethod(ObjectRef, hud_get),
1912 luamethod(ObjectRef, hud_set_flags),
1913 luamethod(ObjectRef, hud_get_flags),
1914 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1915 luamethod(ObjectRef, hud_get_hotbar_itemcount),
1916 luamethod(ObjectRef, hud_set_hotbar_image),
1917 luamethod(ObjectRef, hud_get_hotbar_image),
1918 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1919 luamethod(ObjectRef, hud_get_hotbar_selected_image),
1920 luamethod(ObjectRef, set_sky),
1921 luamethod(ObjectRef, get_sky),
1922 luamethod(ObjectRef, set_clouds),
1923 luamethod(ObjectRef, get_clouds),
1924 luamethod(ObjectRef, override_day_night_ratio),
1925 luamethod(ObjectRef, get_day_night_ratio),
1926 luamethod(ObjectRef, set_local_animation),
1927 luamethod(ObjectRef, get_local_animation),
1928 luamethod(ObjectRef, set_eye_offset),
1929 luamethod(ObjectRef, get_eye_offset),