// set_attach(self, parent, bone, position, rotation) int ObjectRef::l_set_attach(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ObjectRef *parent_ref = checkobject(L, 2); ServerActiveObject *co = getobject(ref); ServerActiveObject *parent = getobject(parent_ref); if(co == NULL) return 0; if(parent == NULL) return 0; // Do it std::string bone = ""; if(!lua_isnil(L, 3)) bone = lua_tostring(L, 3); v3f position = v3f(0, 0, 0); if(!lua_isnil(L, 4)) position = read_v3f(L, 4); v3f rotation = v3f(0, 0, 0); if(!lua_isnil(L, 5)) rotation = read_v3f(L, 5); co->setAttachment(parent->getId(), bone, position, rotation); return 0; }
// setpos(self, pos) int ObjectRef::l_setpos(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); //LuaEntitySAO *co = getluaobject(ref); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // pos v3f pos = checkFloatPos(L, 2); // Do it co->setPos(pos); return 0; }
// punch(self, puncher, time_from_last_punch, tool_capabilities, dir) int ObjectRef::l_punch(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ObjectRef *puncher_ref = checkobject(L, 2); ServerActiveObject *co = getobject(ref); ServerActiveObject *puncher = getobject(puncher_ref); if (co == NULL) return 0; if (puncher == NULL) return 0; v3f dir; if (lua_type(L, 5) != LUA_TTABLE) dir = co->getBasePosition() - puncher->getBasePosition(); else dir = read_v3f(L, 5); float time_from_last_punch = 1000000; if (lua_isnumber(L, 3)) time_from_last_punch = lua_tonumber(L, 3); ToolCapabilities toolcap = read_tool_capabilities(L, 4); dir.normalize(); s16 src_original_hp = co->getHP(); s16 dst_origin_hp = puncher->getHP(); // Do it co->punch(dir, &toolcap, puncher, time_from_last_punch); // If the punched is a player, and its HP changed if (src_original_hp != co->getHP() && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) { getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co); } // If the puncher is a player, and its HP changed if (dst_origin_hp != puncher->getHP() && puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) { getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher); } return 0; }
// get_properties(self) int ObjectRef::l_get_properties(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; ObjectProperties *prop = co->accessObjectProperties(); if (!prop) return 0; push_object_properties(L, prop); return 1; }
// set_properties(self, properties) int ObjectRef::l_set_properties(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; ObjectProperties *prop = co->accessObjectProperties(); if(!prop) return 0; read_object_properties(L, 2, prop); co->notifyObjectPropertiesModified(); return 0; }
// get_inventory(self) int ObjectRef::l_get_inventory(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it InventoryLocation loc = co->getInventoryLocation(); if (getServer(L)->getInventory(loc) != NULL) InvRef::create(L, loc); else lua_pushnil(L); // An object may have no inventory (nil) return 1; }
// punch(self, puncher, time_from_last_punch, tool_capabilities, dir) int ObjectRef::l_punch(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ObjectRef *puncher_ref = checkobject(L, 2); ServerActiveObject *co = getobject(ref); ServerActiveObject *puncher = getobject(puncher_ref); if(co == NULL) return 0; if(puncher == NULL) return 0; v3f dir; if(lua_type(L, 5) != LUA_TTABLE) dir = co->getBasePosition() - puncher->getBasePosition(); else dir = read_v3f(L, 5); float time_from_last_punch = 1000000; if(lua_isnumber(L, 3)) time_from_last_punch = lua_tonumber(L, 3); ToolCapabilities toolcap = read_tool_capabilities(L, 4); dir.normalize(); // Do it co->punch(dir, &toolcap, puncher, time_from_last_punch); return 0; }
// get_wielded_item(self) int ObjectRef::l_get_wielded_item(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) { // Empty ItemStack LuaItemStack::create(L, ItemStack()); return 1; } // Do it LuaItemStack::create(L, co->getWieldedItem()); return 1; }
int LuaMinimap::l_set_mode(lua_State *L) { LuaMinimap *ref = checkobject(L, 1); Minimap *m = getobject(ref); s32 mode = lua_tointeger(L, 2); if (mode < MINIMAP_MODE_OFF || mode >= MINIMAP_MODE_COUNT) { return 0; } m->setMinimapMode((MinimapMode) mode); return 1; }
int LuaMinimap::l_hide(lua_State *L) { Client *client = getClient(L); assert(client); LuaMinimap *ref = checkobject(L, 1); Minimap *m = getobject(ref); if (m->getMinimapMode() != MINIMAP_MODE_OFF) m->setMinimapMode(MINIMAP_MODE_OFF); client->showMinimap(false); return 1; }
// set_attach(self, parent, bone, position, rotation) int ObjectRef::l_set_attach(lua_State *L) { GET_ENV_PTR; ObjectRef *ref = checkobject(L, 1); ObjectRef *parent_ref = checkobject(L, 2); ServerActiveObject *co = getobject(ref); ServerActiveObject *parent = getobject(parent_ref); if (co == NULL) return 0; if (parent == NULL) return 0; // Do it int parent_id = 0; std::string bone = ""; v3f position = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0); co->getAttachment(&parent_id, &bone, &position, &rotation); if (parent_id) { ServerActiveObject *old_parent = env->getActiveObject(parent_id); if (old_parent) old_parent->removeAttachmentChild(co->getId()); } bone = ""; if (!lua_isnil(L, 3)) bone = lua_tostring(L, 3); position = v3f(0, 0, 0); if (!lua_isnil(L, 4)) position = read_v3f(L, 4); rotation = v3f(0, 0, 0); if (!lua_isnil(L, 5)) rotation = read_v3f(L, 5); co->setAttachment(parent->getId(), bone, position, rotation); parent->addAttachmentChild(co->getId()); return 0; }
// move_to(self, pos, continuous=false) int ObjectRef::l_move_to(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); //LuaEntitySAO *co = getluaobject(ref); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // pos v3f pos = checkFloatPos(L, 2); // continuous bool continuous = readParam<bool>(L, 3); // Do it co->moveTo(pos, continuous); return 0; }
// set_hp(self, hp) // hp = number of hitpoints (2 * number of hearts) // returns: nil int ObjectRef::l_set_hp(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); luaL_checknumber(L, 2); ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; int hp = lua_tonumber(L, 2); /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId() <<" hp="<<hp<<std::endl;*/ // Do it co->setHP(hp); // Return return 0; }
// set_wielded_item(self, itemstack or itemstring or table or nil) int ObjectRef::l_set_wielded_item(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it ItemStack item = read_item(L, 2, getServer(L)->idef()); bool success = co->setWieldedItem(item); if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) { getServer(L)->SendInventory(((PlayerSAO*)co)); } lua_pushboolean(L, success); return 1; }
// get_pos(self) // returns: {x=num, y=num, z=num} int ObjectRef::l_get_pos(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; v3f pos = co->getBasePosition() / BS; lua_newtable(L); lua_pushnumber(L, pos.X); lua_setfield(L, -2, "x"); lua_pushnumber(L, pos.Y); lua_setfield(L, -2, "y"); lua_pushnumber(L, pos.Z); lua_setfield(L, -2, "z"); return 1; }
int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_newtable(L); lua_pushnumber(L, player->movement_acceleration_default); lua_setfield(L, -2, "default"); lua_pushnumber(L, player->movement_acceleration_air); lua_setfield(L, -2, "air"); lua_pushnumber(L, player->movement_acceleration_fast); lua_setfield(L, -2, "fast"); return 1; }
void readobj( /* read in an object file or stream */ char *inpspec ) { OBJECT lastobj; FILE *infp; char buf[2048]; int c; lastobj = nobjects; if (inpspec == NULL) { infp = stdin; inpspec = "standard input"; } else if (inpspec[0] == '!') { if ((infp = popen(inpspec+1, "r")) == NULL) { sprintf(errmsg, "cannot execute \"%s\"", inpspec); error(SYSTEM, errmsg); } } else if ((infp = fopen(inpspec, "r")) == NULL) { sprintf(errmsg, "cannot open scene file \"%s\"", inpspec); error(SYSTEM, errmsg); } while ((c = getc(infp)) != EOF) { if (isspace(c)) continue; if (c == '#') { /* comment */ fgets(buf, sizeof(buf), infp); } else if (c == '!') { /* command */ ungetc(c, infp); fgetline(buf, sizeof(buf), infp); readobj(buf); } else { /* object */ ungetc(c, infp); getobject(inpspec, infp); } } if (inpspec[0] == '!') pclose(infp); else if (infp != stdin) fclose(infp); if (nobjects == lastobj) { sprintf(errmsg, "(%s): empty file", inpspec); error(WARNING, errmsg); } }
// get_hp(self) // returns: number of hitpoints (2 * number of hearts) // 0 if not applicable to this type of object int ObjectRef::l_get_hp(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) { // Default hp is 1 lua_pushnumber(L, 1); return 1; } int hp = co->getHP(); /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId() <<" hp="<<hp<<std::endl;*/ // Return lua_pushnumber(L, hp); return 1; }
// set_hp(self, hp) // hp = number of hitpoints (2 * number of hearts) // returns: nil int ObjectRef::l_set_hp(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); luaL_checknumber(L, 2); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; int hp = lua_tonumber(L, 2); /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId() <<" hp="<<hp<<std::endl;*/ // Do it co->setHP(hp); if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co); // Return return 0; }
// remove(self) int ObjectRef::l_remove(lua_State *L) { GET_ENV_PTR; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) return 0; co->clearChildAttachments(); co->clearParentAttachment(); verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl; co->m_pending_removal = true; return 0; }
// set_animation_frame_speed(self, frame_speed) int ObjectRef::l_set_animation_frame_speed(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it if (!lua_isnil(L, 2)) { float frame_speed = lua_tonumber(L, 2); co->setAnimationSpeed(frame_speed); lua_pushboolean(L, true); } else { lua_pushboolean(L, false); } return 1; }
int LuaMinimap::l_show(lua_State *L) { // If minimap is disabled by config, don't show it. if (!g_settings->getBool("enable_minimap")) return 1; Client *client = getClient(L); assert(client); LuaMinimap *ref = checkobject(L, 1); Minimap *m = getobject(ref); if (m->getMinimapMode() == MINIMAP_MODE_OFF) m->setMinimapMode(MINIMAP_MODE_SURFACEx1); client->showMinimap(true); return 1; }
// set_properties(self, properties) int ObjectRef::l_set_properties(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; ObjectProperties *prop = co->accessObjectProperties(); if (!prop) return 0; read_object_properties(L, 2, prop, getServer(L)->idef()); if (prop->hp_max < co->getHP()) { PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP); co->setHP(prop->hp_max, reason); if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason); } co->notifyObjectPropertiesModified(); return 0; }
// set_bone_position(self, std::string bone, v3f position, v3f rotation) int ObjectRef::l_set_bone_position(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it std::string bone; if (!lua_isnil(L, 2)) bone = readParam<std::string>(L, 2); v3f position = v3f(0, 0, 0); if (!lua_isnil(L, 3)) position = check_v3f(L, 3); v3f rotation = v3f(0, 0, 0); if (!lua_isnil(L, 4)) rotation = check_v3f(L, 4); co->setBonePosition(bone, position, rotation); return 0; }
// set_animation(self, frame_range, frame_speed, frame_blend) int ObjectRef::l_set_animation(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; // Do it v2f frames = v2f(1, 1); if(!lua_isnil(L, 2)) frames = read_v2f(L, 2); float frame_speed = 15; if(!lua_isnil(L, 3)) frame_speed = lua_tonumber(L, 3); float frame_blend = 0; if(!lua_isnil(L, 4)) frame_blend = lua_tonumber(L, 4); co->setAnimation(frames, frame_speed, frame_blend); return 0; }
// get_animation(self) int ObjectRef::l_get_animation(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it v2f frames = v2f(1,1); float frame_speed = 15; float frame_blend = 0; bool frame_loop = true; co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop); push_v2f(L, frames); lua_pushnumber(L, frame_speed); lua_pushnumber(L, frame_blend); lua_pushboolean(L, frame_loop); return 4; }
// set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity) int ObjectRef::l_set_physics_override(lua_State *L) { ObjectRef *ref = checkobject(L, 1); PlayerSAO *co = (PlayerSAO *) getobject(ref); if(co == NULL) return 0; // Do it if(!lua_isnil(L, 2)){ co->m_physics_override_speed = lua_tonumber(L, 2); co->m_physics_override_sent = false; } if(!lua_isnil(L, 3)){ co->m_physics_override_jump = lua_tonumber(L, 3); co->m_physics_override_sent = false; } if(!lua_isnil(L, 4)){ co->m_physics_override_gravity = lua_tonumber(L, 4); co->m_physics_override_sent = false; } return 0; }
int LuaLocalPlayer::l_get_movement(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_newtable(L); lua_pushnumber(L, player->movement_liquid_fluidity); lua_setfield(L, -2, "liquid_fluidity"); lua_pushnumber(L, player->movement_liquid_fluidity_smooth); lua_setfield(L, -2, "liquid_fluidity_smooth"); lua_pushnumber(L, player->movement_liquid_sink); lua_setfield(L, -2, "liquid_sink"); lua_pushnumber(L, player->movement_gravity); lua_setfield(L, -2, "gravity"); return 1; }
static void persistuserdata(PersistInfo *pi) { /* perms reftbl ... udata */ if(persistspecialobject(pi, 0)) { /* perms reftbl ... udata */ return; } else { /* Use literal persistence */ int length = uvalue(getobject(pi->L, -2))->uv.len; pi->writer(pi->L, &length, sizeof(int), pi->ud); pi->writer(pi->L, lua_touserdata(pi->L, -1), length, pi->ud); if(!lua_getmetatable(pi->L, -1)) { /* perms reftbl ... udata */ lua_pushnil(pi->L); /* perms reftbl ... udata mt/nil */ } persist(pi); lua_pop(pi->L, 1); /* perms reftbl ... udata */ } }
// get_bone_position(self, bone) int ObjectRef::l_get_bone_position(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it std::string bone = ""; if (!lua_isnil(L, 2)) bone = lua_tostring(L, 2); v3f position = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0); co->getBonePosition(bone, &position, &rotation); push_v3f(L, position); push_v3f(L, rotation); return 2; }