From e0b4859e7c904232253905571721003a2cb76040 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Fri, 26 Mar 2021 13:15:10 +0100 Subject: Add ClientObjectRef:remove --- src/script/lua_api/l_clientobject.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/script/lua_api/l_clientobject.cpp') diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 521aba023..76d0d65ab 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -173,6 +173,15 @@ int ClientObjectRef::l_rightclick(lua_State *L) return 0; } +int ClientObjectRef::l_remove(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + ClientActiveObject *cao = get_cao(ref); + getClient(L)->getEnv().removeActiveObject(cao->getId()); + + return 0; +} + ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object) { } -- cgit v1.2.3 From 7d7d4d675cd066a9dcd4467ff99c471a7ae09b88 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 10 May 2021 15:41:23 +0200 Subject: Add ClientObjectRef.get_properties --- doc/client_lua_api.txt | 7 ++++--- src/script/lua_api/l_clientobject.cpp | 24 +++++++++++++++++++----- src/script/lua_api/l_clientobject.h | 3 +++ 3 files changed, 26 insertions(+), 8 deletions(-) (limited to 'src/script/lua_api/l_clientobject.cpp') diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index d7a8b6bce..bc78d5dda 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -1411,9 +1411,10 @@ This is basically a reference to a C++ `GenericCAO`. * `is_player()`: returns true if the object is a player * `is_local_player()`: returns true if the object is the local player * `get_attach()`: returns parent or nil if it isn't attached. -* `get_nametag()`: returns the nametag (string) -* `get_item_textures()`: returns the textures -* `get_max_hp()`: returns the maximum heath +* `get_nametag()`: returns the nametag (deprecated, use get_properties().nametag instead) +* `get_item_textures()`: returns the textures (deprecated, use get_properties().textures instead) +* `get_max_hp()`: returns the maximum heath (deprecated, use get_properties().hp_max instead) +* `get_properties()`: returns object property table * `punch()`: punches the object * `rightclick()`: rightclicks the object * `remove()`: removes the object permanently diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 76d0d65ab..7b9c4c3fa 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_clientobject.h" #include "l_internal.h" #include "common/c_converter.h" +#include "common/c_content.h" #include "client/client.h" #include "object_properties.h" #include "util/pointedthing.h" @@ -118,6 +119,7 @@ int ClientObjectRef::l_get_attach(lua_State *L) int ClientObjectRef::l_get_nametag(lua_State *L) { + log_deprecated(L,"Deprecated call to get_nametag, use get_properties().nametag instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); ObjectProperties *props = gcao->getProperties(); @@ -127,6 +129,7 @@ int ClientObjectRef::l_get_nametag(lua_State *L) int ClientObjectRef::l_get_item_textures(lua_State *L) { + log_deprecated(L,"Deprecated call to get_item_textures, use get_properties().textures instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); ObjectProperties *props = gcao->getProperties(); @@ -138,20 +141,30 @@ int ClientObjectRef::l_get_item_textures(lua_State *L) return 1; } -int ClientObjectRef::l_get_hp(lua_State *L) +int ClientObjectRef::l_get_max_hp(lua_State *L) { + log_deprecated(L,"Deprecated call to get_max_hp, use get_properties().hp_max instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - lua_pushnumber(L, gcao->getHp()); + ObjectProperties *props = gcao->getProperties(); + lua_pushnumber(L, props->hp_max); return 1; } -int ClientObjectRef::l_get_max_hp(lua_State *L) +int ClientObjectRef::l_get_properties(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - ObjectProperties *props = gcao->getProperties(); - lua_pushnumber(L, props->hp_max); + ObjectProperties *prop = gcao->getProperties(); + push_object_properties(L, prop); + return 1; +} + +int ClientObjectRef::l_get_hp(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + lua_pushnumber(L, gcao->getHp()); return 1; } @@ -245,6 +258,7 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos), luamethod(ClientObjectRef, get_attach), luamethod(ClientObjectRef, get_nametag), luamethod(ClientObjectRef, get_item_textures), + luamethod(ClientObjectRef, get_properties), luamethod(ClientObjectRef, get_hp), luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch), luamethod(ClientObjectRef, rightclick), {0, 0}}; diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h index ebc0f2a90..160b6c4fe 100644 --- a/src/script/lua_api/l_clientobject.h +++ b/src/script/lua_api/l_clientobject.h @@ -78,6 +78,9 @@ private: // get_textures(self) static int l_get_item_textures(lua_State *L); + // get_properties(self) + static int l_get_properties(lua_State *L); + // get_hp(self) static int l_get_hp(lua_State *L); -- cgit v1.2.3 From 6dc7a65d9e23a8bd20f729e041e84615b2313deb Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 10 May 2021 16:07:31 +0200 Subject: Add ClientObjectRef:set_properties --- doc/client_lua_api.txt | 1 + src/client/content_cao.cpp | 107 ++++++++++++++++++---------------- src/client/content_cao.h | 8 ++- src/script/common/c_content.cpp | 4 +- src/script/lua_api/l_clientobject.cpp | 11 ++++ src/script/lua_api/l_clientobject.h | 5 +- 6 files changed, 79 insertions(+), 57 deletions(-) (limited to 'src/script/lua_api/l_clientobject.cpp') diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index bc78d5dda..8b955db31 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -1414,6 +1414,7 @@ This is basically a reference to a C++ `GenericCAO`. * `get_nametag()`: returns the nametag (deprecated, use get_properties().nametag instead) * `get_item_textures()`: returns the textures (deprecated, use get_properties().textures instead) * `get_max_hp()`: returns the maximum heath (deprecated, use get_properties().hp_max instead) +* `set_properties(object property table)` * `get_properties()`: returns object property table * `punch()`: punches the object * `rightclick()`: rightclicks the object diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index 36eb55597..27ba0b6dd 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -831,13 +831,13 @@ void GenericCAO::addToScene(ITextureSource *tsrc) } void GenericCAO::updateLight(u32 day_night_ratio) -{ +{ if (m_glow < 0) return; u8 light_at_pos = 0; bool pos_ok = false; - + v3s16 pos[3]; u16 npos = getLightPosition(pos); for (u16 i = 0; i < npos; i++) { @@ -1629,6 +1629,57 @@ bool GenericCAO::visualExpiryRequired(const ObjectProperties &new_) const (uses_legacy_texture && old.textures != new_.textures); } +void GenericCAO::setProperties(ObjectProperties newprops) +{ + // Check what exactly changed + bool expire_visuals = visualExpiryRequired(newprops); + bool textures_changed = m_prop.textures != newprops.textures; + + // Apply changes + m_prop = std::move(newprops); + + m_selection_box = m_prop.selectionbox; + m_selection_box.MinEdge *= BS; + m_selection_box.MaxEdge *= BS; + + m_tx_size.X = 1.0f / m_prop.spritediv.X; + m_tx_size.Y = 1.0f / m_prop.spritediv.Y; + + if(!m_initial_tx_basepos_set){ + m_initial_tx_basepos_set = true; + m_tx_basepos = m_prop.initial_sprite_basepos; + } + if (m_is_local_player) { + LocalPlayer *player = m_env->getLocalPlayer(); + player->makes_footstep_sound = m_prop.makes_footstep_sound; + aabb3f collision_box = m_prop.collisionbox; + collision_box.MinEdge *= BS; + collision_box.MaxEdge *= BS; + player->setCollisionbox(collision_box); + player->setEyeHeight(m_prop.eye_height); + player->setZoomFOV(m_prop.zoom_fov); + } + + if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty()) + m_prop.nametag = m_name; + if (m_is_local_player) + m_prop.show_on_minimap = false; + + if (expire_visuals) { + expireVisuals(); + } else { + infostream << "GenericCAO: properties updated but expiring visuals" + << " not necessary" << std::endl; + if (textures_changed) { + // don't update while punch texture modifier is active + if (m_reset_textures_timer < 0) + updateTextures(m_current_texture_modifier); + } + updateNametag(); + updateMarker(); + } +} + void GenericCAO::processMessage(const std::string &data) { //infostream<<"GenericCAO: Got message"<getLocalPlayer(); - player->makes_footstep_sound = m_prop.makes_footstep_sound; - aabb3f collision_box = m_prop.collisionbox; - collision_box.MinEdge *= BS; - collision_box.MaxEdge *= BS; - player->setCollisionbox(collision_box); - player->setEyeHeight(m_prop.eye_height); - player->setZoomFOV(m_prop.zoom_fov); - } - - if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty()) - m_prop.nametag = m_name; - if (m_is_local_player) - m_prop.show_on_minimap = false; - - if (expire_visuals) { - expireVisuals(); - } else { - infostream << "GenericCAO: properties updated but expiring visuals" - << " not necessary" << std::endl; - if (textures_changed) { - // don't update while punch texture modifier is active - if (m_reset_textures_timer < 0) - updateTextures(m_current_texture_modifier); - } - updateNametag(); - updateMarker(); - } } else if (cmd == AO_CMD_UPDATE_POSITION) { // Not sent by the server if this object is an attachment. // We might however get here if the server notices the object being detached before the client. @@ -1752,10 +1757,10 @@ void GenericCAO::processMessage(const std::string &data) if(m_is_local_player) { Client *client = m_env->getGameDef(); - + if (client->modsLoaded() && client->getScript()->on_recieve_physics_override(override_speed, override_jump, override_gravity, sneak, sneak_glitch, new_move)) return; - + LocalPlayer *player = m_env->getLocalPlayer(); player->physics_override_speed = override_speed; player->physics_override_jump = override_jump; diff --git a/src/client/content_cao.h b/src/client/content_cao.h index 09c26bd9c..360c30995 100644 --- a/src/client/content_cao.h +++ b/src/client/content_cao.h @@ -181,7 +181,7 @@ public: { return m_velocity; } - + inline const u16 getHp() const { return m_hp; @@ -307,13 +307,15 @@ public: { return m_prop.infotext; } - + float m_waiting_for_reattach; - + ObjectProperties *getProperties() { return &m_prop; } + void setProperties(ObjectProperties newprops); + void updateMeshCulling(); }; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index eeaf69da1..8543b70ce 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -200,7 +200,7 @@ void read_object_properties(lua_State *L, int index, if (getintfield(L, -1, "hp_max", hp_max)) { prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX); - if (prop->hp_max < sao->getHP()) { + if (sao && prop->hp_max < sao->getHP()) { PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP); sao->setHP(prop->hp_max, reason); if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) @@ -209,7 +209,7 @@ void read_object_properties(lua_State *L, int index, } if (getintfield(L, -1, "breath_max", prop->breath_max)) { - if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) { + if (sao && sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) { PlayerSAO *player = (PlayerSAO *)sao; if (prop->breath_max < player->getBreath()) player->setBreath(prop->breath_max); diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 7b9c4c3fa..0147fd48b 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -160,6 +160,16 @@ int ClientObjectRef::l_get_properties(lua_State *L) return 1; } +int ClientObjectRef::l_set_properties(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + ObjectProperties prop = *gcao->getProperties(); + read_object_properties(L, 2, nullptr, &prop, getClient(L)->idef()); + gcao->setProperties(prop); + return 1; +} + int ClientObjectRef::l_get_hp(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); @@ -259,6 +269,7 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos), luamethod(ClientObjectRef, get_nametag), luamethod(ClientObjectRef, get_item_textures), luamethod(ClientObjectRef, get_properties), + luamethod(ClientObjectRef, set_properties), luamethod(ClientObjectRef, get_hp), luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch), luamethod(ClientObjectRef, rightclick), {0, 0}}; diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h index 160b6c4fe..22d2f4a1c 100644 --- a/src/script/lua_api/l_clientobject.h +++ b/src/script/lua_api/l_clientobject.h @@ -75,12 +75,15 @@ private: // get_nametag(self) static int l_get_nametag(lua_State *L); - // get_textures(self) + // get_item_textures(self) static int l_get_item_textures(lua_State *L); // get_properties(self) static int l_get_properties(lua_State *L); + // set_properties(self, properties) + static int l_set_properties(lua_State *L); + // get_hp(self) static int l_get_hp(lua_State *L); -- cgit v1.2.3 From c86dcd0f682f76339989afec255bf3d7078db096 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 10 May 2021 20:45:05 +0200 Subject: Add on_object_hp_change callback and nametag images --- builtin/client/register.lua | 1 + doc/client_lua_api.txt | 3 +++ src/client/camera.cpp | 23 +++++++++++++++++++---- src/client/camera.h | 33 ++++++++++++++++++++++++++++++--- src/client/content_cao.cpp | 6 +++++- src/client/content_cao.h | 2 ++ src/client/hud.cpp | 2 +- src/script/cpp_api/s_client.cpp | 15 +++++++++++++++ src/script/cpp_api/s_client.h | 1 + src/script/lua_api/l_clientobject.cpp | 23 +++++++++++++++++++++-- src/script/lua_api/l_clientobject.h | 3 +++ 11 files changed, 101 insertions(+), 11 deletions(-) (limited to 'src/script/lua_api/l_clientobject.cpp') diff --git a/builtin/client/register.lua b/builtin/client/register.lua index 5bf634699..835ec5002 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -106,6 +106,7 @@ core.registered_on_recieve_physics_override, core.register_on_recieve_physics_ov core.registered_on_play_sound, core.register_on_play_sound = make_registration() core.registered_on_spawn_particle, core.register_on_spawn_particle = make_registration() core.registered_on_object_properties_change, core.register_on_object_properties_change = make_registration() +core.registered_on_object_hp_change, core.register_on_object_hp_change = make_registration() core.registered_nodes = {} core.registered_items = {} diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index 2728ed632..2e347ec47 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -766,6 +766,8 @@ Call these functions only at load time! * `minetest.register_on_object_properties_change(function(obj))` * Called every time the properties of an object are changed server-side * May modify the object's properties without the fear of infinite recursion +* `minetest.register_on_object_hp_change(function(obj))` + * Called every time the hp of an object are changes server-side ### Setting-related * `minetest.settings`: Settings object containing all of the settings from the @@ -1422,6 +1424,7 @@ This is basically a reference to a C++ `GenericCAO`. * `punch()`: punches the object * `rightclick()`: rightclicks the object * `remove()`: removes the object permanently +* `set_nametag_images(images)`: Provides a list of images to be drawn below the nametag ### `Raycast` diff --git a/src/client/camera.cpp b/src/client/camera.cpp index 3afd45c55..854d9eae8 100644 --- a/src/client/camera.cpp +++ b/src/client/camera.cpp @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "constants.h" #include "fontengine.h" +#include "guiscalingfilter.h" #include "script/scripting_client.h" #define CAMERA_OFFSET_STEP 200 @@ -715,7 +716,7 @@ void Camera::drawNametags() screen_pos.Y = screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2; core::rect size(0, 0, textsize.Width, textsize.Height); - core::rect bg_size(-2, 0, textsize.Width+2, textsize.Height); + core::rect bg_size(-2, 0, std::max(textsize.Width+2, (u32) nametag->images_dim.Width), textsize.Height + nametag->images_dim.Height); auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds); if (bgcolor.getAlpha() != 0) @@ -724,15 +725,29 @@ void Camera::drawNametags() font->draw( translate_string(utf8_to_wide(nametag->text)).c_str(), size + screen_pos, nametag->textcolor); + + v2s32 image_pos(screen_pos); + image_pos.Y += textsize.Height; + + const video::SColor color(255, 255, 255, 255); + const video::SColor colors[] = {color, color, color, color}; + + for (video::ITexture *texture : nametag->images) { + core::dimension2di imgsize(texture->getOriginalSize()); + core::rect rect(core::position2d(0, 0), imgsize); + draw2DImageFilterScaled(driver, texture, rect + image_pos, rect, NULL, colors, true); + image_pos += core::dimension2di(imgsize.Width, 0); + } + } } } - Nametag *Camera::addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, - Optional bgcolor, const v3f &pos) + Optional bgcolor, const v3f &pos, + const std::vector &images) { - Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos); + Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos, m_client->tsrc(), images); m_nametags.push_back(nametag); return nametag; } diff --git a/src/client/camera.h b/src/client/camera.h index 6fd8d9aa7..c162df515 100644 --- a/src/client/camera.h +++ b/src/client/camera.h @@ -39,18 +39,44 @@ struct Nametag video::SColor textcolor; Optional bgcolor; v3f pos; + ITextureSource *texture_source; + std::vector images; + core::dimension2di images_dim; Nametag(scene::ISceneNode *a_parent_node, const std::string &text, const video::SColor &textcolor, const Optional &bgcolor, - const v3f &pos): + const v3f &pos, + ITextureSource *tsrc, + const std::vector &image_names): parent_node(a_parent_node), text(text), textcolor(textcolor), bgcolor(bgcolor), - pos(pos) + pos(pos), + texture_source(tsrc), + images(), + images_dim(0, 0) { + setImages(image_names); + } + + void setImages(const std::vector &image_names) + { + images.clear(); + images_dim = core::dimension2di(0, 0); + + for (const std::string &image_name : image_names) { + video::ITexture *texture = texture_source->getTexture(image_name); + core::dimension2di imgsize(texture->getOriginalSize()); + + images_dim.Width += imgsize.Width; + if (images_dim.Height < imgsize.Height) + images_dim.Height = imgsize.Height; + + images.push_back(texture); + } } video::SColor getBgColor(bool use_fallback) const @@ -185,7 +211,8 @@ public: Nametag *addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, - Optional bgcolor, const v3f &pos); + Optional bgcolor, const v3f &pos, + const std::vector &image_names); void removeNametag(Nametag *nametag); diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index ea034f629..84d200a73 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -962,13 +962,14 @@ void GenericCAO::updateNametag() // Add nametag m_nametag = m_client->getCamera()->addNametag(node, m_prop.nametag, m_prop.nametag_color, - m_prop.nametag_bgcolor, pos); + m_prop.nametag_bgcolor, pos, nametag_images); } else { // Update nametag m_nametag->text = m_prop.nametag; m_nametag->textcolor = m_prop.nametag_color; m_nametag->bgcolor = m_prop.nametag_bgcolor; m_nametag->pos = pos; + m_nametag->setImages(nametag_images); } } @@ -1863,6 +1864,9 @@ void GenericCAO::processMessage(const std::string &data) // Same as 'ObjectRef::l_remove' if (!m_is_player) clearChildAttachments(); + } else { + if (m_client->modsLoaded()) + m_client->getScript()->on_object_hp_change(m_id); } } else if (cmd == AO_CMD_UPDATE_ARMOR_GROUPS) { m_armor_groups.clear(); diff --git a/src/client/content_cao.h b/src/client/content_cao.h index 360c30995..450023d19 100644 --- a/src/client/content_cao.h +++ b/src/client/content_cao.h @@ -318,4 +318,6 @@ public: void setProperties(ObjectProperties newprops); void updateMeshCulling(); + + std::vector nametag_images = {}; }; diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 74c1828e3..5971948fd 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -149,7 +149,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect& rect, bool selected) { if (selected) { - /* draw hihlighting around selected item */ + /* draw highlighting around selected item */ if (use_hotbar_selected_image) { core::rect imgrect2 = rect; imgrect2.UpperLeftCorner.X -= (m_padding*2); diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index b8decf2cd..2231cf573 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -308,6 +308,21 @@ void ScriptApiClient::on_object_properties_change(s16 id) runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } +void ScriptApiClient::on_object_hp_change(s16 id) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.on_object_hp_change + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_object_hp_change"); + + // Push data + ClientObjectRef::create(L, id); + + // Call functions + runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); +} + bool ScriptApiClient::on_inventory_open(Inventory *inventory) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 62921b528..a2babfac8 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -64,6 +64,7 @@ public: bool on_play_sound(SimpleSoundSpec spec); bool on_spawn_particle(struct ParticleParameters param); void on_object_properties_change(s16 id); + void on_object_hp_change(s16 id); bool on_inventory_open(Inventory *inventory); void open_enderchest(); diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 0147fd48b..8a4647d45 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -205,6 +205,23 @@ int ClientObjectRef::l_remove(lua_State *L) return 0; } +int ClientObjectRef::l_set_nametag_images(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + gcao->nametag_images.clear(); + if(lua_istable(L, 2)){ + lua_pushnil(L); + while(lua_next(L, 2) != 0){ + gcao->nametag_images.push_back(lua_tostring(L, -1)); + lua_pop(L, 1); + } + } + gcao->updateNametag(); + + return 0; +} + ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object) { } @@ -271,5 +288,7 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos), luamethod(ClientObjectRef, get_properties), luamethod(ClientObjectRef, set_properties), luamethod(ClientObjectRef, get_hp), - luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch), - luamethod(ClientObjectRef, rightclick), {0, 0}}; + luamethod(ClientObjectRef, get_max_hp), + luamethod(ClientObjectRef, punch), + luamethod(ClientObjectRef, rightclick), + luamethod(ClientObjectRef, set_nametag_images), {0, 0}}; diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h index 22d2f4a1c..60d10dcf6 100644 --- a/src/script/lua_api/l_clientobject.h +++ b/src/script/lua_api/l_clientobject.h @@ -98,4 +98,7 @@ private: // remove(self) static int l_remove(lua_State *L); + + // set_nametag_images(self, images) + static int l_set_nametag_images(lua_State *L); }; -- cgit v1.2.3 From 4f613bbf5118ebe8c3610514e7f4206e930783bf Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Tue, 11 May 2021 14:07:30 +0200 Subject: Include tile definitions in get_node_def; Client-side minetest.object_refs table --- builtin/client/register.lua | 1 + builtin/common/misc_helpers.lua | 9 +++++ builtin/game/item.lua | 9 ----- doc/client_lua_api.txt | 61 +++++++++++++++++++++++++++++++++ src/client/clientenvironment.cpp | 11 +++++- src/client/game.cpp | 2 +- src/script/common/c_content.cpp | 63 ++++++++++++++++++++++++++++++----- src/script/cpp_api/s_base.cpp | 16 ++++++--- src/script/cpp_api/s_base.h | 5 +-- src/script/cpp_api/s_client.cpp | 4 +-- src/script/lua_api/l_client.cpp | 2 +- src/script/lua_api/l_clientobject.cpp | 59 ++++++++++++++++++++++++++++---- src/script/lua_api/l_clientobject.h | 2 ++ src/script/lua_api/l_localplayer.cpp | 2 +- src/serverenvironment.cpp | 8 ++--- 15 files changed, 212 insertions(+), 42 deletions(-) (limited to 'src/script/lua_api/l_clientobject.cpp') diff --git a/builtin/client/register.lua b/builtin/client/register.lua index 835ec5002..6a6d8e13c 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -110,3 +110,4 @@ core.registered_on_object_hp_change, core.register_on_object_hp_change = make_re core.registered_nodes = {} core.registered_items = {} +core.object_refs = {} diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index b86d68f5f..308e2c7c7 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -785,3 +785,12 @@ end function core.is_nan(number) return number ~= number end + +function core.inventorycube(img1, img2, img3) + img2 = img2 or img1 + img3 = img3 or img1 + return "[inventorycube" + .. "{" .. img1:gsub("%^", "&") + .. "{" .. img2:gsub("%^", "&") + .. "{" .. img3:gsub("%^", "&") +end diff --git a/builtin/game/item.lua b/builtin/game/item.lua index dc0247e5f..cc0314e67 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -15,15 +15,6 @@ end -- Item definition helpers -- -function core.inventorycube(img1, img2, img3) - img2 = img2 or img1 - img3 = img3 or img1 - return "[inventorycube" - .. "{" .. img1:gsub("%^", "&") - .. "{" .. img2:gsub("%^", "&") - .. "{" .. img3:gsub("%^", "&") -end - function core.dir_to_facedir(dir, is6d) --account for y if requested if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index 2e347ec47..e33fe0e3a 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -934,6 +934,8 @@ Call these functions only at load time! * Convert between two privilege representations ### Client Environment +* `minetest.object_refs` + * Map of object references, indexed by active object id * `minetest.get_player_names()` * Returns list of player names on server (nil if CSM_RF_READ_PLAYERINFO is enabled by server) * `minetest.get_objects_inside_radius(pos, radius)`: returns a list of @@ -1454,6 +1456,8 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or ----------------- ### Definitions +* `minetest.inventorycube(img1, img2, img3)` + * Returns a string for making an image of a cube (useful as an item image) * `minetest.get_node_def(nodename)` * Returns [node definition](#node-definition) table of `nodename` * `minetest.get_item_def(itemstring)` @@ -1465,10 +1469,67 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or {light_source=minetest.LIGHT_MAX})` * Doesnt really work yet an causes strange bugs, I'm working to make is better + +#### Tile definition + +* `"image.png"` +* `{name="image.png", animation={Tile Animation definition}}` +* `{name="image.png", backface_culling=bool, align_style="node"/"world"/"user", scale=int}` + * backface culling enabled by default for most nodes + * align style determines whether the texture will be rotated with the node + or kept aligned with its surroundings. "user" means that client + setting will be used, similar to `glasslike_framed_optional`. + Note: supported by solid nodes and nodeboxes only. + * scale is used to make texture span several (exactly `scale`) nodes, + instead of just one, in each direction. Works for world-aligned + textures only. + Note that as the effect is applied on per-mapblock basis, `16` should + be equally divisible by `scale` or you may get wrong results. +* `{name="image.png", color=ColorSpec}` + * the texture's color will be multiplied with this color. + * the tile's color overrides the owning node's color in all cases. + +##### Tile definition + + { + type = "vertical_frames", + + aspect_w = 16, + -- Width of a frame in pixels + + aspect_h = 16, + -- Height of a frame in pixels + + length = 3.0, + -- Full loop length + } + + { + type = "sheet_2d", + + frames_w = 5, + -- Width in number of frames + + frames_h = 3, + -- Height in number of frames + + frame_length = 0.5, + -- Length of a single frame + } + #### Node Definition ```lua { + tiles = {tile definition 1, def2, def3, def4, def5, def6}, + -- Textures of node; +Y, -Y, +X, -X, +Z, -Z + overlay_tiles = {tile definition 1, def2, def3, def4, def5, def6}, + -- Same as `tiles`, but these textures are drawn on top of the base + -- tiles. This is used to colorize only specific parts of the + -- texture. If the texture name is an empty string, that overlay is not + -- drawn + special_tiles = {tile definition 1, Tile definition 2}, + -- Special textures of node; used rarely. has_on_construct = bool, -- Whether the node has the on_construct callback defined has_on_destruct = bool, -- Whether the node has the on_destruct callback defined has_after_destruct = bool, -- Whether the node has the after_destruct callback defined diff --git a/src/client/clientenvironment.cpp b/src/client/clientenvironment.cpp index fd56c8f44..8e0d00bc9 100644 --- a/src/client/clientenvironment.cpp +++ b/src/client/clientenvironment.cpp @@ -352,6 +352,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type, { ClientActiveObject* obj = ClientActiveObject::create((ActiveObjectType) type, m_client, this); + if(obj == NULL) { infostream<<"ClientEnvironment::addActiveObject(): " @@ -362,6 +363,9 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type, obj->setId(id); + if (m_client->modsLoaded()) + m_client->getScript()->addObjectReference(dynamic_cast(obj)); + try { obj->initialize(init_data); @@ -394,9 +398,14 @@ void ClientEnvironment::removeActiveObject(u16 id) { // Get current attachment childs to detach them visually std::unordered_set attachment_childs; - if (auto *obj = getActiveObject(id)) + auto *obj = getActiveObject(id); + if (obj) { attachment_childs = obj->getAttachmentChildIds(); + if (m_client->modsLoaded()) + m_client->getScript()->removeObjectReference(dynamic_cast(obj)); + } + m_ao_manager.removeObject(id); // Perform a proper detach in Irrlicht diff --git a/src/client/game.cpp b/src/client/game.cpp index 104a6e374..e1f2fbe75 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -734,7 +734,7 @@ bool Game::connectToServer(const GameStartData &start_data, } else { wait_time += dtime; // Only time out if we aren't waiting for the server we started - if (!start_data.isSinglePlayer() && wait_time > 10) { + if (!start_data.local_server && !start_data.isSinglePlayer() && wait_time > 10) { *error_message = "Connection timed out."; errorstream << *error_message << std::endl; break; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 8543b70ce..e56d07cc6 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -515,6 +515,35 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype) return tiledef; } +/******************************************************************************/ +void push_tiledef(lua_State *L, TileDef tiledef) +{ + lua_newtable(L); + setstringfield(L, -1, "name", tiledef.name); + setboolfield(L, -1, "backface_culling", tiledef.backface_culling); + setboolfield(L, -1, "tileable_horizontal", tiledef.tileable_horizontal); + setboolfield(L, -1, "tileable_vertical", tiledef.tileable_vertical); + std::string align_style; + switch (tiledef.align_style) { + case ALIGN_STYLE_USER_DEFINED: + align_style = "user"; + break; + case ALIGN_STYLE_WORLD: + align_style = "world"; + break; + default: + align_style = "node"; + } + setstringfield(L, -1, "align_style", align_style); + setintfield(L, -1, "scale", tiledef.scale); + if (tiledef.has_color) { + push_ARGB8(L, tiledef.color); + lua_setfield(L, -2, "color"); + } + push_animation_definition(L, tiledef.animation); + lua_setfield(L, -2, "animation"); +} + /******************************************************************************/ void read_content_features(lua_State *L, ContentFeatures &f, int index) { @@ -835,9 +864,32 @@ void push_content_features(lua_State *L, const ContentFeatures &c) std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str); std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str); - /* Missing "tiles" because I don't see a usecase (at least not yet). */ + lua_newtable(L); + // tiles lua_newtable(L); + for (int i = 0; i < 6; i++) { + push_tiledef(L, c.tiledef[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setfield(L, -2, "tiles"); + + // overlay_tiles + lua_newtable(L); + for (int i = 0; i < 6; i++) { + push_tiledef(L, c.tiledef_overlay[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setfield(L, -2, "overlay_tiles"); + + // special_tiles + lua_newtable(L); + for (int i = 0; i < CF_SPECIAL_COUNT; i++) { + push_tiledef(L, c.tiledef_special[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setfield(L, -2, "special_tiles"); + lua_pushboolean(L, c.has_on_construct); lua_setfield(L, -2, "has_on_construct"); lua_pushboolean(L, c.has_on_destruct); @@ -1886,14 +1938,7 @@ void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm, } else if (pointed.type == POINTEDTHING_OBJECT) { lua_pushstring(L, "object"); lua_setfield(L, -2, "type"); - if (csm) { -#ifndef SERVER - ClientObjectRef::create(L, pointed.object_id); -#endif - } else { - push_objectRef(L, pointed.object_id); - } - + push_objectRef(L, pointed.object_id); lua_setfield(L, -2, "ref"); } else { lua_pushstring(L, "nothing"); diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 1d62d8b65..867f61e0c 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "cpp_api/s_internal.h" #include "cpp_api/s_security.h" #include "lua_api/l_object.h" +#include "lua_api/l_clientobject.h" #include "common/c_converter.h" #include "server/player_sao.h" #include "filesys.h" @@ -354,13 +355,16 @@ void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn) * since we lose control over the ref and the contained pointer. */ -void ScriptApiBase::addObjectReference(ServerActiveObject *cobj) +void ScriptApiBase::addObjectReference(ActiveObject *cobj) { SCRIPTAPI_PRECHECKHEADER //infostream<<"scriptapi_add_object_reference: id="<getId()<(cobj)); + else + ObjectRef::create(L, dynamic_cast(cobj)); // Puts ObjectRef (as userdata) on stack int object = lua_gettop(L); // Get core.object_refs table @@ -375,7 +379,7 @@ void ScriptApiBase::addObjectReference(ServerActiveObject *cobj) lua_settable(L, objectstable); } -void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj) +void ScriptApiBase::removeObjectReference(ActiveObject *cobj) { SCRIPTAPI_PRECHECKHEADER //infostream<<"scriptapi_rm_object_reference: id="<getId()<getId()); // Push id lua_gettable(L, objectstable); // Set object reference to NULL - ObjectRef::set_null(L); + if (m_type == ScriptingType::Client) + ClientObjectRef::set_null(L); + else + ObjectRef::set_null(L); lua_pop(L, 1); // pop object // Set object_refs[id] = nil @@ -413,7 +420,6 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L, << ", this is probably a bug." << std::endl; } } - void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason) { if (reason.hasLuaReference()) diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index 36331ad37..a7a2c7203 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -73,6 +73,7 @@ class Game; class IGameDef; class Environment; class GUIEngine; +class ActiveObject; class ServerActiveObject; struct PlayerHPChangeReason; @@ -99,8 +100,8 @@ public: RunCallbacksMode mode, const char *fxn); /* object */ - void addObjectReference(ServerActiveObject *cobj); - void removeObjectReference(ServerActiveObject *cobj); + void addObjectReference(ActiveObject *cobj); + void removeObjectReference(ActiveObject *cobj); IGameDef *getGameDef() { return m_gamedef; } Server* getServer(); diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 2231cf573..7971e4081 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -302,7 +302,7 @@ void ScriptApiClient::on_object_properties_change(s16 id) lua_getfield(L, -1, "registered_on_object_properties_change"); // Push data - ClientObjectRef::create(L, id); + push_objectRef(L, id); // Call functions runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); @@ -317,7 +317,7 @@ void ScriptApiClient::on_object_hp_change(s16 id) lua_getfield(L, -1, "registered_on_object_hp_change"); // Push data - ClientObjectRef::create(L, id); + push_objectRef(L, id); // Call functions runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 484af2ec3..916983982 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -520,7 +520,7 @@ int ModApiClient::l_get_objects_inside_radius(lua_State *L) int i = 0; lua_createtable(L, objs.size(), 0); for (const auto obj : objs) { - ClientObjectRef::create(L, obj.obj); // TODO: getObjectRefOrCreate + push_objectRef(L, obj.obj->getId()); lua_rawseti(L, -2, ++i); } return 1; diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 8a4647d45..5a1123169 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -48,6 +48,8 @@ ClientActiveObject *ClientObjectRef::get_cao(ClientObjectRef *ref) GenericCAO *ClientObjectRef::get_generic_cao(ClientObjectRef *ref, lua_State *L) { ClientActiveObject *obj = get_cao(ref); + if (! obj) + return nullptr; ClientEnvironment &env = getClient(L)->getEnv(); GenericCAO *gcao = env.getGenericCAO(obj->getId()); return gcao; @@ -57,6 +59,8 @@ int ClientObjectRef::l_get_pos(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); ClientActiveObject *cao = get_cao(ref); + if (! cao) + return 0; push_v3f(L, cao->getPosition() / BS); return 1; } @@ -65,6 +69,8 @@ int ClientObjectRef::l_get_velocity(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; push_v3f(L, gcao->getVelocity() / BS); return 1; } @@ -73,6 +79,8 @@ int ClientObjectRef::l_get_acceleration(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; push_v3f(L, gcao->getAcceleration() / BS); return 1; } @@ -81,6 +89,8 @@ int ClientObjectRef::l_get_rotation(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; push_v3f(L, gcao->getRotation()); return 1; } @@ -89,6 +99,8 @@ int ClientObjectRef::l_is_player(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; lua_pushboolean(L, gcao->isPlayer()); return 1; } @@ -97,6 +109,8 @@ int ClientObjectRef::l_is_local_player(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; lua_pushboolean(L, gcao->isLocalPlayer()); return 1; } @@ -105,6 +119,8 @@ int ClientObjectRef::l_get_name(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; lua_pushstring(L, gcao->getName().c_str()); return 1; } @@ -113,7 +129,12 @@ int ClientObjectRef::l_get_attach(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - create(L, gcao->getParent()); + if (! gcao) + return 0; + ClientActiveObject *parent = gcao->getParent(); + if (! parent) + return 0; + push_objectRef(L, parent->getId()); return 1; } @@ -122,6 +143,8 @@ int ClientObjectRef::l_get_nametag(lua_State *L) log_deprecated(L,"Deprecated call to get_nametag, use get_properties().nametag instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; ObjectProperties *props = gcao->getProperties(); lua_pushstring(L, props->nametag.c_str()); return 1; @@ -132,6 +155,8 @@ int ClientObjectRef::l_get_item_textures(lua_State *L) log_deprecated(L,"Deprecated call to get_item_textures, use get_properties().textures instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; ObjectProperties *props = gcao->getProperties(); lua_newtable(L); @@ -146,6 +171,8 @@ int ClientObjectRef::l_get_max_hp(lua_State *L) log_deprecated(L,"Deprecated call to get_max_hp, use get_properties().hp_max instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; ObjectProperties *props = gcao->getProperties(); lua_pushnumber(L, props->hp_max); return 1; @@ -155,6 +182,8 @@ int ClientObjectRef::l_get_properties(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; ObjectProperties *prop = gcao->getProperties(); push_object_properties(L, prop); return 1; @@ -164,6 +193,8 @@ int ClientObjectRef::l_set_properties(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; ObjectProperties prop = *gcao->getProperties(); read_object_properties(L, 2, nullptr, &prop, getClient(L)->idef()); gcao->setProperties(prop); @@ -174,6 +205,8 @@ int ClientObjectRef::l_get_hp(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; lua_pushnumber(L, gcao->getHp()); return 1; } @@ -182,6 +215,8 @@ int ClientObjectRef::l_punch(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; PointedThing pointed(gcao->getId(), v3f(0, 0, 0), v3s16(0, 0, 0), 0); getClient(L)->interact(INTERACT_START_DIGGING, pointed); return 0; @@ -191,6 +226,8 @@ int ClientObjectRef::l_rightclick(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; PointedThing pointed(gcao->getId(), v3f(0, 0, 0), v3s16(0, 0, 0), 0); getClient(L)->interact(INTERACT_PLACE, pointed); return 0; @@ -200,6 +237,8 @@ int ClientObjectRef::l_remove(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); ClientActiveObject *cao = get_cao(ref); + if (! cao) + return 0; getClient(L)->getEnv().removeActiveObject(cao->getId()); return 0; @@ -209,6 +248,8 @@ int ClientObjectRef::l_set_nametag_images(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); + if (! gcao) + return 0; gcao->nametag_images.clear(); if(lua_istable(L, 2)){ lua_pushnil(L); @@ -228,12 +269,10 @@ ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object) void ClientObjectRef::create(lua_State *L, ClientActiveObject *object) { - if (object) { - ClientObjectRef *o = new ClientObjectRef(object); - *(void **)(lua_newuserdata(L, sizeof(void *))) = o; - luaL_getmetatable(L, className); - lua_setmetatable(L, -2); - } + ClientObjectRef *o = new ClientObjectRef(object); + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, className); + lua_setmetatable(L, -2); } void ClientObjectRef::create(lua_State *L, s16 id) @@ -241,6 +280,12 @@ void ClientObjectRef::create(lua_State *L, s16 id) create(L, ((ClientEnvironment *)getEnv(L))->getActiveObject(id)); } +void ClientObjectRef::set_null(lua_State *L) +{ + ClientObjectRef *obj = checkobject(L, -1); + obj->m_object = nullptr; +} + int ClientObjectRef::gc_object(lua_State *L) { ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1)); diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h index 60d10dcf6..c4c95cb41 100644 --- a/src/script/lua_api/l_clientobject.h +++ b/src/script/lua_api/l_clientobject.h @@ -35,6 +35,8 @@ public: static void create(lua_State *L, ClientActiveObject *object); static void create(lua_State *L, s16 id); + static void set_null(lua_State *L); + static ClientObjectRef *checkobject(lua_State *L, int narg); private: diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 747657016..b8673379a 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -483,7 +483,7 @@ int LuaLocalPlayer::l_get_object(lua_State *L) ClientEnvironment &env = getClient(L)->getEnv(); ClientActiveObject *obj = env.getGenericCAO(player->getCAO()->getId()); - ClientObjectRef::create(L, obj); + push_objectRef(L, obj->getId()); return 1; } diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 3d9ba132b..cd5ac0753 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -1170,7 +1170,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) // Tell the object about removal obj->removingFromEnvironment(); // Deregister in scripting api - m_script->removeObjectReference(obj); + m_script->removeObjectReference(dynamic_cast(obj)); // Delete active object if (obj->environmentDeletes()) @@ -1736,7 +1736,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, } // Register reference in scripting api (must be done before post-init) - m_script->addObjectReference(object); + m_script->addObjectReference(dynamic_cast(object)); // Post-initialize object object->addedToEnvironment(dtime_s); @@ -1826,7 +1826,7 @@ void ServerEnvironment::removeRemovedObjects() // Tell the object about removal obj->removingFromEnvironment(); // Deregister in scripting api - m_script->removeObjectReference(obj); + m_script->removeObjectReference(dynamic_cast(obj)); // Delete if (obj->environmentDeletes()) @@ -2091,7 +2091,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete) // Tell the object about removal obj->removingFromEnvironment(); // Deregister in scripting api - m_script->removeObjectReference(obj); + m_script->removeObjectReference(dynamic_cast(obj)); // Delete active object if (obj->environmentDeletes()) -- cgit v1.2.3 From d08242316688ce8ac10dcf94a2cfede21e65be7f Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 13 May 2021 08:24:07 +0200 Subject: Fix format --- src/content/mods.cpp | 15 ++++++---- src/script/lua_api/l_clientobject.cpp | 56 ++++++++++++++++++----------------- src/server/serverinventorymgr.h | 3 +- 3 files changed, 41 insertions(+), 33 deletions(-) (limited to 'src/script/lua_api/l_clientobject.cpp') diff --git a/src/content/mods.cpp b/src/content/mods.cpp index 434004b29..f791fa797 100644 --- a/src/content/mods.cpp +++ b/src/content/mods.cpp @@ -50,7 +50,8 @@ static void log_mod_deprecation(const ModSpec &spec, const std::string &warning) auto handling_mode = get_deprecated_handling_mode(); if (handling_mode != DeprecatedHandlingMode::Ignore) { std::ostringstream os; - os << warning << " (" << spec.name << " at " << spec.path << ")" << std::endl; + os << warning << " (" << spec.name << " at " << spec.path << ")" + << std::endl; if (handling_mode == DeprecatedHandlingMode::Error) { throw ModError(os.str()); @@ -89,7 +90,8 @@ void parseModContents(ModSpec &spec) if (info.exists("name")) spec.name = info.get("name"); else - log_mod_deprecation(spec, "Mods not having a mod.conf file with the name is deprecated."); + log_mod_deprecation(spec, "Mods not having a mod.conf file with " + "the name is deprecated."); if (info.exists("author")) spec.author = info.get("author"); @@ -130,7 +132,8 @@ void parseModContents(ModSpec &spec) std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str()); if (is.good()) - log_mod_deprecation(spec, "depends.txt is deprecated, please use mod.conf instead."); + log_mod_deprecation(spec, "depends.txt is deprecated, " + "please use mod.conf instead."); while (is.good()) { std::string dep; @@ -152,8 +155,10 @@ void parseModContents(ModSpec &spec) if (info.exists("description")) spec.desc = info.get("description"); - else if (fs::ReadFile(spec.path + DIR_DELIM + "description.txt", spec.desc)) - log_mod_deprecation(spec, "description.txt is deprecated, please use mod.conf instead."); + else if (fs::ReadFile(spec.path + DIR_DELIM + "description.txt", + spec.desc)) + log_mod_deprecation(spec, "description.txt is deprecated, please " + "use mod.conf instead."); } } diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 5a1123169..c093e754e 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -48,7 +48,7 @@ ClientActiveObject *ClientObjectRef::get_cao(ClientObjectRef *ref) GenericCAO *ClientObjectRef::get_generic_cao(ClientObjectRef *ref, lua_State *L) { ClientActiveObject *obj = get_cao(ref); - if (! obj) + if (!obj) return nullptr; ClientEnvironment &env = getClient(L)->getEnv(); GenericCAO *gcao = env.getGenericCAO(obj->getId()); @@ -59,7 +59,7 @@ int ClientObjectRef::l_get_pos(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); ClientActiveObject *cao = get_cao(ref); - if (! cao) + if (!cao) return 0; push_v3f(L, cao->getPosition() / BS); return 1; @@ -69,7 +69,7 @@ int ClientObjectRef::l_get_velocity(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; push_v3f(L, gcao->getVelocity() / BS); return 1; @@ -79,7 +79,7 @@ int ClientObjectRef::l_get_acceleration(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; push_v3f(L, gcao->getAcceleration() / BS); return 1; @@ -89,7 +89,7 @@ int ClientObjectRef::l_get_rotation(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; push_v3f(L, gcao->getRotation()); return 1; @@ -99,7 +99,7 @@ int ClientObjectRef::l_is_player(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; lua_pushboolean(L, gcao->isPlayer()); return 1; @@ -109,7 +109,7 @@ int ClientObjectRef::l_is_local_player(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; lua_pushboolean(L, gcao->isLocalPlayer()); return 1; @@ -119,7 +119,7 @@ int ClientObjectRef::l_get_name(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; lua_pushstring(L, gcao->getName().c_str()); return 1; @@ -129,10 +129,10 @@ int ClientObjectRef::l_get_attach(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; ClientActiveObject *parent = gcao->getParent(); - if (! parent) + if (!parent) return 0; push_objectRef(L, parent->getId()); return 1; @@ -140,10 +140,11 @@ int ClientObjectRef::l_get_attach(lua_State *L) int ClientObjectRef::l_get_nametag(lua_State *L) { - log_deprecated(L,"Deprecated call to get_nametag, use get_properties().nametag instead"); + log_deprecated(L, "Deprecated call to get_nametag, use get_properties().nametag " + "instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; ObjectProperties *props = gcao->getProperties(); lua_pushstring(L, props->nametag.c_str()); @@ -152,10 +153,11 @@ int ClientObjectRef::l_get_nametag(lua_State *L) int ClientObjectRef::l_get_item_textures(lua_State *L) { - log_deprecated(L,"Deprecated call to get_item_textures, use get_properties().textures instead"); + log_deprecated(L, "Deprecated call to get_item_textures, use " + "get_properties().textures instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; ObjectProperties *props = gcao->getProperties(); lua_newtable(L); @@ -168,10 +170,11 @@ int ClientObjectRef::l_get_item_textures(lua_State *L) int ClientObjectRef::l_get_max_hp(lua_State *L) { - log_deprecated(L,"Deprecated call to get_max_hp, use get_properties().hp_max instead"); + log_deprecated(L, "Deprecated call to get_max_hp, use get_properties().hp_max " + "instead"); ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; ObjectProperties *props = gcao->getProperties(); lua_pushnumber(L, props->hp_max); @@ -182,7 +185,7 @@ int ClientObjectRef::l_get_properties(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; ObjectProperties *prop = gcao->getProperties(); push_object_properties(L, prop); @@ -193,7 +196,7 @@ int ClientObjectRef::l_set_properties(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; ObjectProperties prop = *gcao->getProperties(); read_object_properties(L, 2, nullptr, &prop, getClient(L)->idef()); @@ -205,7 +208,7 @@ int ClientObjectRef::l_get_hp(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; lua_pushnumber(L, gcao->getHp()); return 1; @@ -215,7 +218,7 @@ int ClientObjectRef::l_punch(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; PointedThing pointed(gcao->getId(), v3f(0, 0, 0), v3s16(0, 0, 0), 0); getClient(L)->interact(INTERACT_START_DIGGING, pointed); @@ -226,7 +229,7 @@ int ClientObjectRef::l_rightclick(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; PointedThing pointed(gcao->getId(), v3f(0, 0, 0), v3s16(0, 0, 0), 0); getClient(L)->interact(INTERACT_PLACE, pointed); @@ -237,7 +240,7 @@ int ClientObjectRef::l_remove(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); ClientActiveObject *cao = get_cao(ref); - if (! cao) + if (!cao) return 0; getClient(L)->getEnv().removeActiveObject(cao->getId()); @@ -248,12 +251,12 @@ int ClientObjectRef::l_set_nametag_images(lua_State *L) { ClientObjectRef *ref = checkobject(L, 1); GenericCAO *gcao = get_generic_cao(ref, L); - if (! gcao) + if (!gcao) return 0; gcao->nametag_images.clear(); - if(lua_istable(L, 2)){ + if (lua_istable(L, 2)) { lua_pushnil(L); - while(lua_next(L, 2) != 0){ + while (lua_next(L, 2) != 0) { gcao->nametag_images.push_back(lua_tostring(L, -1)); lua_pop(L, 1); } @@ -333,7 +336,6 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos), luamethod(ClientObjectRef, get_properties), luamethod(ClientObjectRef, set_properties), luamethod(ClientObjectRef, get_hp), - luamethod(ClientObjectRef, get_max_hp), - luamethod(ClientObjectRef, punch), + luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch), luamethod(ClientObjectRef, rightclick), luamethod(ClientObjectRef, set_nametag_images), {0, 0}}; diff --git a/src/server/serverinventorymgr.h b/src/server/serverinventorymgr.h index 0e4b72415..b6541bd3c 100644 --- a/src/server/serverinventorymgr.h +++ b/src/server/serverinventorymgr.h @@ -43,7 +43,8 @@ public: Inventory *createDetachedInventory(const std::string &name, IItemDefManager *idef, const std::string &player = ""); bool removeDetachedInventory(const std::string &name); - bool checkDetachedInventoryAccess(const InventoryLocation &loc, const std::string &player) const; + bool checkDetachedInventoryAccess( + const InventoryLocation &loc, const std::string &player) const; void sendDetachedInventories(const std::string &peer_name, bool incremental, std::function apply_cb); -- cgit v1.2.3