]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/common/c_content.cpp
scriptapi: Some small optimizations to value pushing (#9669)
[dragonfireclient.git] / src / script / common / c_content.cpp
index 793485e25e07fe8335edc67f021d20b7e6482a79..d73ecedab5fbf61b85a68d2d7412eeaf52c2582d 100644 (file)
@@ -28,10 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include "log.h"
 #include "tool.h"
-#include "serverobject.h"
 #include "porting.h"
 #include "mapgen/mg_schematic.h"
 #include "noise.h"
+#include "server/player_sao.h"
 #include "util/pointedthing.h"
 #include "debug.h" // For FATAL_ERROR
 #include <json/json.h>
@@ -82,7 +82,7 @@ void read_item_definition(lua_State* L, int index,
        getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
 
        warn_if_field_exists(L, index, "tool_digging_properties",
-                       "Deprecated; use tool_capabilities");
+                       "Obsolete; use tool_capabilities");
 
        lua_getfield(L, index, "tool_capabilities");
        if(lua_istable(L, -1)){
@@ -163,11 +163,7 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
        lua_pushboolean(L, i.liquids_pointable);
        lua_setfield(L, -2, "liquids_pointable");
        if (i.type == ITEM_TOOL) {
-               push_tool_capabilities(L, ToolCapabilities(
-                       i.tool_capabilities->full_punch_interval,
-                       i.tool_capabilities->max_drop_level,
-                       i.tool_capabilities->groupcaps,
-                       i.tool_capabilities->damageGroups));
+               push_tool_capabilities(L, *i.tool_capabilities);
                lua_setfield(L, -2, "tool_capabilities");
        }
        push_groups(L, i.groups);
@@ -182,7 +178,7 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
 
 /******************************************************************************/
 void read_object_properties(lua_State *L, int index,
-               ObjectProperties *prop, IItemDefManager *idef)
+               ServerActiveObject *sao, ObjectProperties *prop, IItemDefManager *idef)
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
@@ -190,15 +186,27 @@ void read_object_properties(lua_State *L, int index,
                return;
 
        int hp_max = 0;
-       if (getintfield(L, -1, "hp_max", hp_max))
+       if (getintfield(L, -1, "hp_max", hp_max)) {
                prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
 
-       getintfield(L, -1, "breath_max", prop->breath_max);
+               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);
+               }
+       }
+
+       if (getintfield(L, -1, "breath_max", prop->breath_max)) {
+               if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
+                       PlayerSAO *player = (PlayerSAO *)sao;
+                       if (prop->breath_max < player->getBreath())
+                               player->setBreath(prop->breath_max);
+               }
+       }
        getboolfield(L, -1, "physical", prop->physical);
        getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
 
-       getfloatfield(L, -1, "weight", prop->weight);
-
        lua_getfield(L, -1, "collisionbox");
        bool collisionbox_defined = lua_istable(L, -1);
        if (collisionbox_defined)
@@ -329,8 +337,6 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "physical");
        lua_pushboolean(L, prop->collideWithObjects);
        lua_setfield(L, -2, "collide_with_objects");
-       lua_pushnumber(L, prop->weight);
-       lua_setfield(L, -2, "weight");
        push_aabb3f(L, prop->collisionbox);
        lua_setfield(L, -2, "collisionbox");
        push_aabb3f(L, prop->selectionbox);
@@ -344,7 +350,7 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        push_v3f(L, prop->visual_size);
        lua_setfield(L, -2, "visual_size");
 
-       lua_newtable(L);
+       lua_createtable(L, prop->textures.size(), 0);
        u16 i = 1;
        for (const std::string &texture : prop->textures) {
                lua_pushlstring(L, texture.c_str(), texture.size());
@@ -352,7 +358,7 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        }
        lua_setfield(L, -2, "textures");
 
-       lua_newtable(L);
+       lua_createtable(L, prop->colors.size(), 0);
        i = 1;
        for (const video::SColor &color : prop->colors) {
                push_ARGB8(L, color);
@@ -632,19 +638,19 @@ ContentFeatures read_content_features(lua_State *L, int index)
                warningstream << "Node " << f.name.c_str()
                        << " has a palette, but not a suitable paramtype2." << std::endl;
 
-       // Warn about some deprecated fields
+       // Warn about some obsolete fields
        warn_if_field_exists(L, index, "wall_mounted",
-                       "Deprecated; use paramtype2 = 'wallmounted'");
+                       "Obsolete; use paramtype2 = 'wallmounted'");
        warn_if_field_exists(L, index, "light_propagates",
-                       "Deprecated; determined from paramtype");
+                       "Obsolete; determined from paramtype");
        warn_if_field_exists(L, index, "dug_item",
-                       "Deprecated; use 'drop' field");
+                       "Obsolete; use 'drop' field");
        warn_if_field_exists(L, index, "extra_dug_item",
-                       "Deprecated; use 'drop' field");
+                       "Obsolete; use 'drop' field");
        warn_if_field_exists(L, index, "extra_dug_item_rarity",
-                       "Deprecated; use 'drop' field");
+                       "Obsolete; use 'drop' field");
        warn_if_field_exists(L, index, "metadata_name",
-                       "Deprecated; use on_add and metadata callbacks");
+                       "Obsolete; use on_add and metadata callbacks");
 
        // True for all ground-like things like stone and mud, false for eg. trees
        getboolfield(L, index, "is_ground_content", f.is_ground_content);
@@ -834,7 +840,7 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
        lua_pushnumber(L, c.connect_sides);
        lua_setfield(L, -2, "connect_sides");
 
-       lua_newtable(L);
+       lua_createtable(L, c.connects_to.size(), 0);
        u16 i = 1;
        for (const std::string &it : c.connects_to) {
                lua_pushlstring(L, it.c_str(), it.size());
@@ -957,7 +963,7 @@ void push_nodebox(lua_State *L, const NodeBox &box)
 
 void push_box(lua_State *L, const std::vector<aabb3f> &box)
 {
-       lua_newtable(L);
+       lua_createtable(L, box.size(), 0);
        u8 i = 1;
        for (const aabb3f &it : box) {
                push_aabb3f(L, it);
@@ -1012,6 +1018,7 @@ void read_server_sound_params(lua_State *L, int index,
                params.max_hear_distance = BS*getfloatfield_default(L, index,
                                "max_hear_distance", params.max_hear_distance/BS);
                getboolfield(L, index, "loop", params.loop);
+               getstringfield(L, index, "exclude_player", params.exclude_player);
        }
 }
 
@@ -1033,7 +1040,7 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
 
 void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
 {
-       lua_newtable(L);
+       lua_createtable(L, 0, 3);
        lua_pushstring(L, spec.name.c_str());
        lua_setfield(L, -2, "name");
        lua_pushnumber(L, spec.gain);
@@ -1093,7 +1100,7 @@ MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef)
        lua_getfield(L, index, "name");
        if (!lua_isstring(L, -1))
                throw LuaError("Node name is not set or is not a string!");
-       const char *name = lua_tostring(L, -1);
+       std::string name = lua_tostring(L, -1);
        lua_pop(L, 1);
 
        u8 param1 = 0;
@@ -1108,18 +1115,22 @@ MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef)
                param2 = lua_tonumber(L, -1);
        lua_pop(L, 1);
 
-       return {ndef, name, param1, param2};
+       content_t id = CONTENT_IGNORE;
+       if (!ndef->getId(name, id))
+               throw LuaError("\"" + name + "\" is not a registered node!");
+
+       return {id, param1, param2};
 }
 
 /******************************************************************************/
 void pushnode(lua_State *L, const MapNode &n, const NodeDefManager *ndef)
 {
-       lua_newtable(L);
+       lua_createtable(L, 0, 3);
        lua_pushstring(L, ndef->get(n).name.c_str());
        lua_setfield(L, -2, "name");
-       lua_pushnumber(L, n.getParam1());
+       lua_pushinteger(L, n.getParam1());
        lua_setfield(L, -2, "param1");
-       lua_pushnumber(L, n.getParam2());
+       lua_pushinteger(L, n.getParam2());
        lua_setfield(L, -2, "param2");
 }
 
@@ -1152,7 +1163,7 @@ bool string_to_enum(const EnumString *spec, int &result,
 {
        const EnumString *esp = spec;
        while(esp->str){
-               if(str == std::string(esp->str)){
+               if (!strcmp(str.c_str(), esp->str)) {
                        result = esp->num;
                        return true;
                }
@@ -1234,7 +1245,8 @@ void push_tool_capabilities(lua_State *L,
 {
        lua_newtable(L);
        setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
-               setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
+       setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
+       setintfield(L, -1, "punch_attack_uses", toolcap.punch_attack_uses);
                // Create groupcaps table
                lua_newtable(L);
                // For each groupcap
@@ -1356,6 +1368,7 @@ ToolCapabilities read_tool_capabilities(
        ToolCapabilities toolcap;
        getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
        getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
+       getintfield(L, table, "punch_attack_uses", toolcap.punch_attack_uses);
        lua_getfield(L, table, "groupcaps");
        if(lua_istable(L, -1)){
                int table_groupcaps = lua_gettop(L);
@@ -1425,7 +1438,7 @@ ToolCapabilities read_tool_capabilities(
 /******************************************************************************/
 void push_dig_params(lua_State *L,const DigParams &params)
 {
-       lua_newtable(L);
+       lua_createtable(L, 0, 3);
        setboolfield(L, -1, "diggable", params.diggable);
        setfloatfield(L, -1, "time", params.time);
        setintfield(L, -1, "wear", params.wear);
@@ -1434,7 +1447,7 @@ void push_dig_params(lua_State *L,const DigParams &params)
 /******************************************************************************/
 void push_hit_params(lua_State *L,const HitParams &params)
 {
-       lua_newtable(L);
+       lua_createtable(L, 0, 3);
        setintfield(L, -1, "hp", params.hp);
        setintfield(L, -1, "wear", params.wear);
 }
@@ -1510,13 +1523,15 @@ void read_groups(lua_State *L, int index, ItemGroupList &result)
                return;
        result.clear();
        lua_pushnil(L);
-       if(index < 0)
+       if (index < 0)
                index -= 1;
-       while(lua_next(L, index) != 0){
+       while (lua_next(L, index) != 0) {
                // key at index -2 and value at index -1
                std::string name = luaL_checkstring(L, -2);
                int rating = luaL_checkinteger(L, -1);
-               result[name] = rating;
+               // zero rating indicates not in the group
+               if (rating != 0)
+                       result[name] = rating;
                // removes value, keeps key for next iteration
                lua_pop(L, 1);
        }
@@ -1525,9 +1540,9 @@ void read_groups(lua_State *L, int index, ItemGroupList &result)
 /******************************************************************************/
 void push_groups(lua_State *L, const ItemGroupList &groups)
 {
-       lua_newtable(L);
+       lua_createtable(L, 0, groups.size());
        for (const auto &group : groups) {
-               lua_pushnumber(L, group.second);
+               lua_pushinteger(L, group.second);
                lua_setfield(L, -2, group.first.c_str());
        }
 }
@@ -1572,7 +1587,7 @@ void luaentity_get(lua_State *L, u16 id)
        lua_getglobal(L, "core");
        lua_getfield(L, -1, "luaentities");
        luaL_checktype(L, -1, LUA_TTABLE);
-       lua_pushnumber(L, id);
+       lua_pushinteger(L, id);
        lua_gettable(L, -2);
        lua_remove(L, -2); // Remove luaentities
        lua_remove(L, -2); // Remove core
@@ -1674,7 +1689,7 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
                        lua_pushboolean(L, value.asInt());
                        break;
                case Json::arrayValue:
-                       lua_newtable(L);
+                       lua_createtable(L, value.size(), 0);
                        for (Json::Value::const_iterator it = value.begin();
                                        it != value.end(); ++it) {
                                push_json_value_helper(L, *it, nullindex);
@@ -1682,7 +1697,7 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
                        }
                        break;
                case Json::objectValue:
-                       lua_newtable(L);
+                       lua_createtable(L, 0, value.size());
                        for (Json::Value::const_iterator it = value.begin();
                                        it != value.end(); ++it) {
 #ifndef JSONCPP_STRING
@@ -1809,7 +1824,7 @@ void push_objectRef(lua_State *L, const u16 id)
        lua_getglobal(L, "core");
        lua_getfield(L, -1, "object_refs");
        luaL_checktype(L, -1, LUA_TTABLE);
-       lua_pushnumber(L, id);
+       lua_pushinteger(L, id);
        lua_gettable(L, -2);
        lua_remove(L, -2); // object_refs
        lua_remove(L, -2); // core
@@ -1832,11 +1847,17 @@ void read_hud_element(lua_State *L, HudElement *elem)
        elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
        lua_pop(L, 1);
 
-       elem->name   = getstringfield_default(L, 2, "name", "");
-       elem->text   = getstringfield_default(L, 2, "text", "");
-       elem->number = getintfield_default(L, 2, "number", 0);
-       elem->item   = getintfield_default(L, 2, "item", 0);
-       elem->dir    = getintfield_default(L, 2, "direction", 0);
+       elem->name    = getstringfield_default(L, 2, "name", "");
+       elem->text    = getstringfield_default(L, 2, "text", "");
+       elem->number  = getintfield_default(L, 2, "number", 0);
+       if (elem->type == HUD_ELEM_WAYPOINT)
+               // waypoints reuse the item field to store precision, item = precision + 1
+               elem->item = getintfield_default(L, 2, "precision", -1) + 1;
+       else
+               elem->item = getintfield_default(L, 2, "item", 0);
+       elem->dir     = getintfield_default(L, 2, "direction", 0);
+       elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX,
+                       getintfield_default(L, 2, "z_index", 0)));
 
        // Deprecated, only for compatibility's sake
        if (elem->dir == 0)
@@ -1902,6 +1923,9 @@ void push_hud_element(lua_State *L, HudElement *elem)
 
        push_v3f(L, elem->world_pos);
        lua_setfield(L, -2, "world_pos");
+
+       lua_pushnumber(L, elem->z_index);
+       lua_setfield(L, -2, "z_index");
 }
 
 HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
@@ -1959,6 +1983,10 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
                        elem->size = read_v2s32(L, 4);
                        *value = &elem->size;
                        break;
+               case HUD_STAT_Z_INDEX:
+                       elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX, luaL_checknumber(L, 4)));
+                       *value = &elem->z_index;
+                       break;
        }
        return stat;
 }