From 2db6b07de1e45c56f6135363939dbb3781336514 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 20 Jun 2021 17:21:50 +0200 Subject: Inventory: show error on invalid list names (#11368) --- src/script/common/c_content.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/script/common/c_content.cpp') diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 52baeae9d..f8cc40927 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1350,26 +1350,28 @@ void read_inventory_list(lua_State *L, int tableindex, { if(tableindex < 0) tableindex = lua_gettop(L) + 1 + tableindex; + // If nil, delete list if(lua_isnil(L, tableindex)){ inv->deleteList(name); return; } - // Otherwise set list + + // Get Lua-specified items to insert into the list std::vector items = read_items(L, tableindex,srv); - int listsize = (forcesize != -1) ? forcesize : items.size(); + size_t listsize = (forcesize > 0) ? forcesize : items.size(); + + // Create or clear list InventoryList *invlist = inv->addList(name, listsize); - int index = 0; - for(std::vector::const_iterator - i = items.begin(); i != items.end(); ++i){ - if(forcesize != -1 && index == forcesize) - break; - invlist->changeItem(index, *i); - index++; + if (!invlist) { + luaL_error(L, "inventory list: cannot create list named '%s'", name); + return; } - while(forcesize != -1 && index < forcesize){ - invlist->deleteItem(index); - index++; + + for (size_t i = 0; i < items.size(); ++i) { + if (i == listsize) + break; // Truncate provided list of items + invlist->changeItem(i, items[i]); } } -- cgit v1.2.3 From f2fd4432625ee5cf0380bdd006cd1f15d053b12f Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Wed, 30 Jun 2021 20:39:38 +0200 Subject: Inventory: Make addList() consistent (#11382) Fixes list clearing for inv:set_list() using same size, since 2db6b07. addList() now clears the list in all cases. Use setSize() to resize without clearing. --- src/inventory.cpp | 15 ++++++--------- src/inventory.h | 2 +- src/script/common/c_content.cpp | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) (limited to 'src/script/common/c_content.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index fc1aaf371..b3bed623a 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -938,19 +938,16 @@ void Inventory::deSerialize(std::istream &is) InventoryList * Inventory::addList(const std::string &name, u32 size) { setModified(); + + // Remove existing lists s32 i = getListIndex(name); - if(i != -1) - { - if(m_lists[i]->getSize() != size) - { - delete m_lists[i]; - m_lists[i] = new InventoryList(name, size, m_itemdef); - m_lists[i]->setModified(); - } + if (i != -1) { + delete m_lists[i]; + m_lists[i] = new InventoryList(name, size, m_itemdef); + m_lists[i]->setModified(); return m_lists[i]; } - //don't create list with invalid name if (name.find(' ') != std::string::npos) return nullptr; diff --git a/src/inventory.h b/src/inventory.h index fbf995fab..6c84f5fd1 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -298,7 +298,7 @@ public: void serialize(std::ostream &os, bool incremental = false) const; void deSerialize(std::istream &is); - // Adds a new list or clears and resizes an existing one + // Creates a new list if none exists or truncates existing lists InventoryList * addList(const std::string &name, u32 size); InventoryList * getList(const std::string &name); const InventoryList * getList(const std::string &name) const; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index f8cc40927..a0b45982a 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1359,9 +1359,9 @@ void read_inventory_list(lua_State *L, int tableindex, // Get Lua-specified items to insert into the list std::vector items = read_items(L, tableindex,srv); - size_t listsize = (forcesize > 0) ? forcesize : items.size(); + size_t listsize = (forcesize >= 0) ? forcesize : items.size(); - // Create or clear list + // Create or resize/clear list InventoryList *invlist = inv->addList(name, listsize); if (!invlist) { luaL_error(L, "inventory list: cannot create list named '%s'", name); -- cgit v1.2.3 From 6e8aebf4327ed43ade17cbe8e6cf652edc099651 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 27 Jul 2021 19:11:46 +0200 Subject: Add bold, italic and monospace font styling for HUD text elements (#11478) Co-authored-by: Elias Fleckenstein --- doc/client_lua_api.txt | 2 + doc/lua_api.txt | 3 ++ games/devtest/mods/testhud/init.lua | 81 +++++++++++++++++++++++++++++++++++++ games/devtest/mods/testhud/mod.conf | 2 + src/client/clientevent.h | 2 +- src/client/game.cpp | 3 ++ src/client/hud.cpp | 30 ++++++-------- src/hud.cpp | 1 + src/hud.h | 6 +++ src/network/clientpackethandler.cpp | 5 ++- src/script/common/c_content.cpp | 9 +++++ src/server.cpp | 7 +--- 12 files changed, 127 insertions(+), 24 deletions(-) create mode 100644 games/devtest/mods/testhud/init.lua create mode 100644 games/devtest/mods/testhud/mod.conf (limited to 'src/script/common/c_content.cpp') diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index d239594f7..24d23b0b5 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -1314,6 +1314,8 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or -- ^ See "HUD Element Types" size = { x=100, y=100 }, -- default {x=0, y=0} -- ^ Size of element in pixels + style = 0, + -- ^ For "text" elements sets font style: bitfield with 1 = bold, 2 = italic, 4 = monospace } ``` diff --git a/doc/lua_api.txt b/doc/lua_api.txt index bb94829a5..7ee9a3f2c 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -8447,6 +8447,9 @@ Used by `Player:hud_add`. Returned by `Player:hud_get`. z_index = 0, -- Z index : lower z-index HUDs are displayed behind higher z-index HUDs + + style = 0, + -- For "text" elements sets font style: bitfield with 1 = bold, 2 = italic, 4 = monospace } Particle definition diff --git a/games/devtest/mods/testhud/init.lua b/games/devtest/mods/testhud/init.lua new file mode 100644 index 000000000..2fa12fd71 --- /dev/null +++ b/games/devtest/mods/testhud/init.lua @@ -0,0 +1,81 @@ +local player_huds = {} + +local states = { + {0, "Normal font"}, + {1, "Bold font"}, + {2, "Italic font"}, + {3, "Bold and italic font"}, + {4, "Monospace font"}, + {5, "Bold and monospace font"}, + {7, "ZOMG all the font styles"}, +} + + +local default_def = { + hud_elem_type = "text", + position = {x = 0.5, y = 0.5}, + scale = {x = 2, y = 2}, + alignment = { x = 0, y = 0 }, +} + +local function add_hud(player, state) + local def = table.copy(default_def) + local statetbl = states[state] + def.offset = {x = 0, y = 32 * state} + def.style = statetbl[1] + def.text = statetbl[2] + return player:hud_add(def) +end + +minetest.register_on_leaveplayer(function(player) + player_huds[player:get_player_name()] = nil +end) + +local etime = 0 +local state = 0 + +minetest.register_globalstep(function(dtime) + etime = etime + dtime + if etime < 1 then + return + end + etime = 0 + for _, player in ipairs(minetest.get_connected_players()) do + local huds = player_huds[player:get_player_name()] + if huds then + for i, hud_id in ipairs(huds) do + local statetbl = states[(state + i) % #states + 1] + player:hud_change(hud_id, "style", statetbl[1]) + player:hud_change(hud_id, "text", statetbl[2]) + end + end + end + state = state + 1 +end) + +minetest.register_chatcommand("hudfonts", { + params = "", + description = "Show/Hide some text on the HUD with various font options", + func = function(name, param) + local player = minetest.get_player_by_name(name) + local param = tonumber(param) or 0 + param = math.min(math.max(param, 1), #states) + if player_huds[name] == nil then + player_huds[name] = {} + for i = 1, param do + table.insert(player_huds[name], add_hud(player, i)) + end + minetest.chat_send_player(name, ("%d HUD element(s) added."):format(param)) + else + local huds = player_huds[name] + if huds then + for _, hud_id in ipairs(huds) do + player:hud_remove(hud_id) + end + minetest.chat_send_player(name, "All HUD elements removed.") + end + player_huds[name] = nil + end + return true + end, +}) diff --git a/games/devtest/mods/testhud/mod.conf b/games/devtest/mods/testhud/mod.conf new file mode 100644 index 000000000..ed9f65c59 --- /dev/null +++ b/games/devtest/mods/testhud/mod.conf @@ -0,0 +1,2 @@ +name = testhud +description = For testing HUD functionality diff --git a/src/client/clientevent.h b/src/client/clientevent.h index 2215aecbd..17d3aedd6 100644 --- a/src/client/clientevent.h +++ b/src/client/clientevent.h @@ -59,7 +59,7 @@ struct ClientEventHudAdd v2f pos, scale; std::string name; std::string text, text2; - u32 number, item, dir; + u32 number, item, dir, style; v2f align, offset; v3f world_pos; v2s32 size; diff --git a/src/client/game.cpp b/src/client/game.cpp index 6fc57c8cc..73ad73e0e 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -2730,6 +2730,7 @@ void Game::handleClientEvent_HudAdd(ClientEvent *event, CameraOrientation *cam) e->size = event->hudadd->size; e->z_index = event->hudadd->z_index; e->text2 = event->hudadd->text2; + e->style = event->hudadd->style; m_hud_server_to_client[server_id] = player->addHud(e); delete event->hudadd; @@ -2795,6 +2796,8 @@ void Game::handleClientEvent_HudChange(ClientEvent *event, CameraOrientation *ca CASE_SET(HUD_STAT_Z_INDEX, z_index, data); CASE_SET(HUD_STAT_TEXT2, text2, sdata); + + CASE_SET(HUD_STAT_STYLE, style, data); } #undef CASE_SET diff --git a/src/client/hud.cpp b/src/client/hud.cpp index fbfc886d2..c5bf0f2f8 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -331,8 +331,8 @@ bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *p void Hud::drawLuaElements(const v3s16 &camera_offset) { - u32 text_height = g_fontengine->getTextHeight(); - irr::gui::IGUIFont* font = g_fontengine->getFont(); + const u32 text_height = g_fontengine->getTextHeight(); + gui::IGUIFont *const font = g_fontengine->getFont(); // Reorder elements by z_index std::vector elems; @@ -356,38 +356,34 @@ void Hud::drawLuaElements(const v3s16 &camera_offset) floor(e->pos.Y * (float) m_screensize.Y + 0.5)); switch (e->type) { case HUD_ELEM_TEXT: { - irr::gui::IGUIFont *textfont = font; unsigned int font_size = g_fontengine->getDefaultFontSize(); if (e->size.X > 0) font_size *= e->size.X; - if (font_size != g_fontengine->getDefaultFontSize()) - textfont = g_fontengine->getFont(font_size); +#ifdef __ANDROID__ + // The text size on Android is not proportional with the actual scaling + // FIXME: why do we have such a weird unportable hack?? + if (font_size > 3 && e->offset.X < -20) + font_size -= 3; +#endif + auto textfont = g_fontengine->getFont(FontSpec(font_size, + (e->style & HUD_STYLE_MONO) ? FM_Mono : FM_Unspecified, + e->style & HUD_STYLE_BOLD, e->style & HUD_STYLE_ITALIC)); video::SColor color(255, (e->number >> 16) & 0xFF, (e->number >> 8) & 0xFF, (e->number >> 0) & 0xFF); std::wstring text = unescape_translate(utf8_to_wide(e->text)); core::dimension2d textsize = textfont->getDimension(text.c_str()); -#ifdef __ANDROID__ - // The text size on Android is not proportional with the actual scaling - irr::gui::IGUIFont *font_scaled = font_size <= 3 ? - textfont : g_fontengine->getFont(font_size - 3); - if (e->offset.X < -20) - textsize = font_scaled->getDimension(text.c_str()); -#endif + v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2), (e->align.Y - 1.0) * (textsize.Height / 2)); core::rect size(0, 0, e->scale.X * m_scale_factor, text_height * e->scale.Y * m_scale_factor); v2s32 offs(e->offset.X * m_scale_factor, e->offset.Y * m_scale_factor); -#ifdef __ANDROID__ - if (e->offset.X < -20) - font_scaled->draw(text.c_str(), size + pos + offset + offs, color); - else -#endif + { textfont->draw(text.c_str(), size + pos + offset + offs, color); } diff --git a/src/hud.cpp b/src/hud.cpp index 1791e04df..e4ad7940f 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -50,6 +50,7 @@ const struct EnumString es_HudElementStat[] = {HUD_STAT_SIZE, "size"}, {HUD_STAT_Z_INDEX, "z_index"}, {HUD_STAT_TEXT2, "text2"}, + {HUD_STAT_STYLE, "style"}, {0, NULL}, }; diff --git a/src/hud.h b/src/hud.h index a0613ae98..769966688 100644 --- a/src/hud.h +++ b/src/hud.h @@ -33,6 +33,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #define HUD_CORNER_LOWER 1 #define HUD_CORNER_CENTER 2 +#define HUD_STYLE_BOLD 1 +#define HUD_STYLE_ITALIC 2 +#define HUD_STYLE_MONO 4 + // Note that these visibility flags do not determine if the hud items are // actually drawn, but rather, whether to draw the item should the rest // of the game state permit it. @@ -78,6 +82,7 @@ enum HudElementStat { HUD_STAT_SIZE, HUD_STAT_Z_INDEX, HUD_STAT_TEXT2, + HUD_STAT_STYLE, }; enum HudCompassDir { @@ -102,6 +107,7 @@ struct HudElement { v2s32 size; s16 z_index = 0; std::string text2; + u32 style; }; extern const EnumString es_HudElementType[]; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index b86bdac18..50f497959 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -1061,6 +1061,7 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt) v2s32 size; s16 z_index = 0; std::string text2; + u32 style = 0; *pkt >> server_id >> type >> pos >> name >> scale >> text >> number >> item >> dir >> align >> offset; @@ -1069,6 +1070,7 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt) *pkt >> size; *pkt >> z_index; *pkt >> text2; + *pkt >> style; } catch(PacketError &e) {}; ClientEvent *event = new ClientEvent(); @@ -1089,6 +1091,7 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt) event->hudadd->size = size; event->hudadd->z_index = z_index; event->hudadd->text2 = text2; + event->hudadd->style = style; m_client_event_queue.push(event); } @@ -1123,7 +1126,7 @@ void Client::handleCommand_HudChange(NetworkPacket* pkt) *pkt >> sdata; else if (stat == HUD_STAT_WORLD_POS) *pkt >> v3fdata; - else if (stat == HUD_STAT_SIZE ) + else if (stat == HUD_STAT_SIZE) *pkt >> v2s32data; else *pkt >> intdata; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index a0b45982a..235016be0 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1928,6 +1928,8 @@ void read_hud_element(lua_State *L, HudElement *elem) elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f(); lua_pop(L, 1); + elem->style = getintfield_default(L, 2, "style", 0); + /* check for known deprecated element usage */ if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) log_deprecated(L,"Deprecated usage of statbar without size!"); @@ -1982,6 +1984,9 @@ void push_hud_element(lua_State *L, HudElement *elem) lua_pushstring(L, elem->text2.c_str()); lua_setfield(L, -2, "text2"); + + lua_pushinteger(L, elem->style); + lua_setfield(L, -2, "style"); } HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value) @@ -2050,6 +2055,10 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value) elem->text2 = luaL_checkstring(L, 4); *value = &elem->text2; break; + case HUD_STAT_STYLE: + elem->style = luaL_checknumber(L, 4); + *value = &elem->style; + break; } return stat; } diff --git a/src/server.cpp b/src/server.cpp index c47596a97..4d2cd8e55 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1638,7 +1638,7 @@ void Server::SendHUDAdd(session_t peer_id, u32 id, HudElement *form) pkt << id << (u8) form->type << form->pos << form->name << form->scale << form->text << form->number << form->item << form->dir << form->align << form->offset << form->world_pos << form->size - << form->z_index << form->text2; + << form->z_index << form->text2 << form->style; Send(&pkt); } @@ -1673,10 +1673,7 @@ void Server::SendHUDChange(session_t peer_id, u32 id, HudElementStat stat, void case HUD_STAT_SIZE: pkt << *(v2s32 *) value; break; - case HUD_STAT_NUMBER: - case HUD_STAT_ITEM: - case HUD_STAT_DIR: - default: + default: // all other types pkt << *(u32 *) value; break; } -- cgit v1.2.3 From 0c1e9603db32b4281974fdd8b8e3b505148be47e Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 21 Aug 2021 20:04:04 +0200 Subject: HUD: Reject and warn on invalid stat types (#11548) This comes into play on older servers which do not know the "stat" type. Warnings are only logged once to avoid spam within globalstep callbacks --- src/network/clientpackethandler.cpp | 34 ++++++++++++++++--------- src/script/common/c_content.cpp | 19 ++++++++------ src/script/common/c_content.h | 10 ++++---- src/script/common/c_internal.cpp | 48 ++++++++++++++++++++++++++---------- src/script/common/c_internal.h | 6 +++-- src/script/lua_api/l_localplayer.cpp | 5 ++-- src/script/lua_api/l_object.cpp | 8 +++--- 7 files changed, 86 insertions(+), 44 deletions(-) (limited to 'src/script/common/c_content.cpp') diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 50f497959..a631a3178 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -1119,17 +1119,29 @@ void Client::handleCommand_HudChange(NetworkPacket* pkt) *pkt >> server_id >> stat; - if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE || - stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET) - *pkt >> v2fdata; - else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT || stat == HUD_STAT_TEXT2) - *pkt >> sdata; - else if (stat == HUD_STAT_WORLD_POS) - *pkt >> v3fdata; - else if (stat == HUD_STAT_SIZE) - *pkt >> v2s32data; - else - *pkt >> intdata; + // Keep in sync with:server.cpp -> SendHUDChange + switch ((HudElementStat)stat) { + case HUD_STAT_POS: + case HUD_STAT_SCALE: + case HUD_STAT_ALIGN: + case HUD_STAT_OFFSET: + *pkt >> v2fdata; + break; + case HUD_STAT_NAME: + case HUD_STAT_TEXT: + case HUD_STAT_TEXT2: + *pkt >> sdata; + break; + case HUD_STAT_WORLD_POS: + *pkt >> v3fdata; + break; + case HUD_STAT_SIZE: + *pkt >> v2s32data; + break; + default: + *pkt >> intdata; + break; + } ClientEvent *event = new ClientEvent(); event->type = CE_HUDCHANGE; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 235016be0..f13287375 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1989,15 +1989,17 @@ void push_hud_element(lua_State *L, HudElement *elem) lua_setfield(L, -2, "style"); } -HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value) +bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value) { - HudElementStat stat = HUD_STAT_NUMBER; - std::string statstr; - if (lua_isstring(L, 3)) { + std::string statstr = lua_tostring(L, 3); + { int statint; - statstr = lua_tostring(L, 3); - stat = string_to_enum(es_HudElementStat, statint, statstr) ? - (HudElementStat)statint : stat; + if (!string_to_enum(es_HudElementStat, statint, statstr)) { + script_log_unique(L, "Unknown HUD stat type: " + statstr, warningstream); + return false; + } + + stat = (HudElementStat)statint; } switch (stat) { @@ -2060,7 +2062,8 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value) *value = &elem->style; break; } - return stat; + + return true; } /******************************************************************************/ diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 4dc614706..e762604a4 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -193,12 +193,12 @@ void read_json_value (lua_State *L, Json::Value &root, void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm = false, bool hitpoint = false); -void push_objectRef (lua_State *L, const u16 id); +void push_objectRef (lua_State *L, const u16 id); -void read_hud_element (lua_State *L, HudElement *elem); +void read_hud_element (lua_State *L, HudElement *elem); -void push_hud_element (lua_State *L, HudElement *elem); +void push_hud_element (lua_State *L, HudElement *elem); -HudElementStat read_hud_change (lua_State *L, HudElement *elem, void **value); +bool read_hud_change (lua_State *L, HudElementStat &stat, HudElement *elem, void **value); -void push_collision_move_result(lua_State *L, const collisionMoveResult &res); +void push_collision_move_result(lua_State *L, const collisionMoveResult &res); diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp index ad5f836c5..66f6a9b98 100644 --- a/src/script/common/c_internal.cpp +++ b/src/script/common/c_internal.cpp @@ -18,10 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "common/c_internal.h" +#include "util/numeric.h" #include "debug.h" #include "log.h" #include "porting.h" #include "settings.h" +#include // std::find std::string script_get_backtrace(lua_State *L) { @@ -135,24 +137,35 @@ void script_run_callbacks_f(lua_State *L, int nargs, lua_remove(L, error_handler); } -static void script_log(lua_State *L, const std::string &message, - std::ostream &log_to, bool do_error, int stack_depth) +static void script_log_add_source(lua_State *L, std::string &message, int stack_depth) { lua_Debug ar; - log_to << message << " "; if (lua_getstack(L, stack_depth, &ar)) { FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed"); - log_to << "(at " << ar.short_src << ":" << ar.currentline << ")"; + message.append(" (at " + std::string(ar.short_src) + ":" + + std::to_string(ar.currentline) + ")"); } else { - log_to << "(at ?:?)"; + message.append(" (at ?:?)"); } - log_to << std::endl; +} - if (do_error) - script_error(L, LUA_ERRRUN, NULL, NULL); - else - infostream << script_get_backtrace(L) << std::endl; +bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to, + int stack_depth) +{ + thread_local std::vector logged_messages; + + script_log_add_source(L, message, stack_depth); + u64 hash = murmur_hash_64_ua(message.data(), message.length(), 0xBADBABE); + + if (std::find(logged_messages.begin(), logged_messages.end(), hash) + == logged_messages.end()) { + + logged_messages.emplace_back(hash); + log_to << message << std::endl; + return true; + } + return false; } DeprecatedHandlingMode get_deprecated_handling_mode() @@ -174,9 +187,18 @@ DeprecatedHandlingMode get_deprecated_handling_mode() return ret; } -void log_deprecated(lua_State *L, const std::string &message, int stack_depth) +void log_deprecated(lua_State *L, std::string message, int stack_depth) { DeprecatedHandlingMode mode = get_deprecated_handling_mode(); - if (mode != DeprecatedHandlingMode::Ignore) - script_log(L, message, warningstream, mode == DeprecatedHandlingMode::Error, stack_depth); + if (mode == DeprecatedHandlingMode::Ignore) + return; + + script_log_add_source(L, message, stack_depth); + warningstream << message << std::endl; + + if (mode == DeprecatedHandlingMode::Error) + script_error(L, LUA_ERRRUN, NULL, NULL); + else + infostream << script_get_backtrace(L) << std::endl; } + diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h index 452c2dd5e..4ddbed232 100644 --- a/src/script/common/c_internal.h +++ b/src/script/common/c_internal.h @@ -114,6 +114,9 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f void script_run_callbacks_f(lua_State *L, int nargs, RunCallbacksMode mode, const char *fxn); +bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to, + int stack_depth = 1); + enum class DeprecatedHandlingMode { Ignore, Log, @@ -134,5 +137,4 @@ DeprecatedHandlingMode get_deprecated_handling_mode(); * @param message The deprecation method * @param stack_depth How far on the stack to the first user function (ie: not builtin or core) */ -void log_deprecated(lua_State *L, const std::string &message, - int stack_depth=1); +void log_deprecated(lua_State *L, std::string message, int stack_depth = 1); diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 59d9ea5f8..77a692f08 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -369,10 +369,11 @@ int LuaLocalPlayer::l_hud_change(lua_State *L) if (!element) return 0; + HudElementStat stat; void *unused; - read_hud_change(L, element, &unused); + bool ok = read_hud_change(L, stat, element, &unused); - lua_pushboolean(L, true); + lua_pushboolean(L, ok); return 1; } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index c404cb63c..c915fa9e1 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1555,12 +1555,14 @@ int ObjectRef::l_hud_change(lua_State *L) if (elem == nullptr) return 0; + HudElementStat stat; void *value = nullptr; - HudElementStat stat = read_hud_change(L, elem, &value); + bool ok = read_hud_change(L, stat, elem, &value); - getServer(L)->hudChange(player, id, stat, value); + if (ok) + getServer(L)->hudChange(player, id, stat, value); - lua_pushboolean(L, true); + lua_pushboolean(L, ok); return 1; } -- cgit v1.2.3 From 3f1adb49ae8da5bb02bea52609524d3645b6a665 Mon Sep 17 00:00:00 2001 From: savilli <78875209+savilli@users.noreply.github.com> Date: Sat, 28 Aug 2021 12:14:16 +0200 Subject: Remove redundant on_dieplayer calls --- src/network/serverpackethandler.cpp | 16 ---------------- src/script/common/c_content.cpp | 2 -- src/script/lua_api/l_object.cpp | 18 ------------------ src/server.cpp | 3 +-- src/server/player_sao.cpp | 4 ++-- 5 files changed, 3 insertions(+), 40 deletions(-) (limited to 'src/script/common/c_content.cpp') diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index dc5864be3..77fde2a66 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -828,7 +828,6 @@ void Server::handleCommand_Damage(NetworkPacket* pkt) PlayerHPChangeReason reason(PlayerHPChangeReason::FALL); playersao->setHP((s32)playersao->getHP() - (s32)damage, reason); - SendPlayerHPOrDie(playersao, reason); } } @@ -1113,9 +1112,6 @@ void Server::handleCommand_Interact(NetworkPacket *pkt) float time_from_last_punch = playersao->resetTimeFromLastPunch(); - u16 src_original_hp = pointed_object->getHP(); - u16 dst_origin_hp = playersao->getHP(); - u16 wear = pointed_object->punch(dir, &toolcap, playersao, time_from_last_punch); @@ -1125,18 +1121,6 @@ void Server::handleCommand_Interact(NetworkPacket *pkt) if (changed) playersao->setWieldedItem(selected_item); - // If the object is a player and its HP changed - if (src_original_hp != pointed_object->getHP() && - pointed_object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { - SendPlayerHPOrDie((PlayerSAO *)pointed_object, - PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, playersao)); - } - - // If the puncher is a player and its HP changed - if (dst_origin_hp != playersao->getHP()) - SendPlayerHPOrDie(playersao, - PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, pointed_object)); - return; } // action == INTERACT_START_DIGGING diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index f13287375..5a095fd8f 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -200,8 +200,6 @@ void read_object_properties(lua_State *L, int index, if (prop->hp_max < sao->getHP()) { PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP); sao->setHP(prop->hp_max, reason); - if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) - sao->getEnv()->getGameDef()->SendPlayerHPOrDie((PlayerSAO *)sao, reason); } } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index c8fa7d806..b7185f7ec 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -172,27 +172,11 @@ int ObjectRef::l_punch(lua_State *L) float time_from_last_punch = readParam(L, 3, 1000000.0f); ToolCapabilities toolcap = read_tool_capabilities(L, 4); v3f dir = readParam(L, 5, sao->getBasePosition() - puncher->getBasePosition()); - dir.normalize(); - u16 src_original_hp = sao->getHP(); - u16 dst_origin_hp = puncher->getHP(); u16 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch); lua_pushnumber(L, wear); - // If the punched is a player, and its HP changed - if (src_original_hp != sao->getHP() && - sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) { - getServer(L)->SendPlayerHPOrDie((PlayerSAO *)sao, - PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher)); - } - - // 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, - PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, sao)); - } return 1; } @@ -238,8 +222,6 @@ int ObjectRef::l_set_hp(lua_State *L) } sao->setHP(hp, reason); - if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) - getServer(L)->SendPlayerHPOrDie((PlayerSAO *)sao, reason); if (reason.hasLuaReference()) luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference); return 0; diff --git a/src/server.cpp b/src/server.cpp index 8339faa76..b96db1209 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1079,8 +1079,7 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id) if (playersao->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); else - SendPlayerHPOrDie(playersao, - PlayerHPChangeReason(PlayerHPChangeReason::SET_HP)); + SendPlayerHP(peer_id); // Send Breath SendPlayerBreath(playersao); diff --git a/src/server/player_sao.cpp b/src/server/player_sao.cpp index 0d31f2e0b..d4d036726 100644 --- a/src/server/player_sao.cpp +++ b/src/server/player_sao.cpp @@ -167,7 +167,6 @@ void PlayerSAO::step(float dtime, bool send_recommended) if (m_breath == 0) { PlayerHPChangeReason reason(PlayerHPChangeReason::DROWNING); setHP(m_hp - c.drowning, reason); - m_env->getGameDef()->SendPlayerHPOrDie(this, reason); } } } @@ -216,7 +215,6 @@ void PlayerSAO::step(float dtime, bool send_recommended) s32 newhp = (s32)m_hp - (s32)damage_per_second; PlayerHPChangeReason reason(PlayerHPChangeReason::NODE_DAMAGE, nodename); setHP(newhp, reason); - m_env->getGameDef()->SendPlayerHPOrDie(this, reason); } } @@ -491,6 +489,8 @@ void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason) // Update properties on death if ((hp == 0) != (oldhp == 0)) m_properties_sent = false; + + m_env->getGameDef()->SendPlayerHPOrDie(this, reason); } void PlayerSAO::setBreath(const u16 breath, bool send) -- cgit v1.2.3