]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/common/c_content.cpp
Remove redundant on_dieplayer calls
[minetest.git] / src / script / common / c_content.cpp
index 25dada7578cc6d592ed9c3a699d4402e79aee2a9..5a095fd8f1fb57f0f1247f6e26bc3bec8ea770ff 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "common/c_types.h"
 #include "nodedef.h"
 #include "object_properties.h"
+#include "collision.h"
 #include "cpp_api/s_node.h"
 #include "lua_api/l_object.h"
 #include "lua_api/l_item.h"
@@ -55,6 +56,7 @@ void read_item_definition(lua_State* L, int index,
                        es_ItemType, ITEM_NONE);
        getstringfield(L, index, "name", def.name);
        getstringfield(L, index, "description", def.description);
+       getstringfield(L, index, "short_description", def.short_description);
        getstringfield(L, index, "inventory_image", def.inventory_image);
        getstringfield(L, index, "inventory_overlay", def.inventory_overlay);
        getstringfield(L, index, "wield_image", def.wield_image);
@@ -81,9 +83,6 @@ 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",
-                       "Obsolete; use tool_capabilities");
-
        lua_getfield(L, index, "tool_capabilities");
        if(lua_istable(L, -1)){
                def.tool_capabilities = new ToolCapabilities(
@@ -102,7 +101,8 @@ void read_item_definition(lua_State* L, int index,
        lua_pop(L, 1);
 
        lua_getfield(L, index, "sounds");
-       if(lua_istable(L, -1)){
+       if (!lua_isnil(L, -1)) {
+               luaL_checktype(L, -1, LUA_TTABLE);
                lua_getfield(L, -1, "place");
                read_soundspec(L, -1, def.sound_place);
                lua_pop(L, 1);
@@ -119,6 +119,8 @@ void read_item_definition(lua_State* L, int index,
        // "" = no prediction
        getstringfield(L, index, "node_placement_prediction",
                        def.node_placement_prediction);
+
+       getintfield(L, index, "place_param2", def.place_param2);
 }
 
 /******************************************************************************/
@@ -140,6 +142,10 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
        lua_setfield(L, -2, "name");
        lua_pushstring(L, i.description.c_str());
        lua_setfield(L, -2, "description");
+       if (!i.short_description.empty()) {
+               lua_pushstring(L, i.short_description.c_str());
+               lua_setfield(L, -2, "short_description");
+       }
        lua_pushstring(L, type.c_str());
        lua_setfield(L, -2, "type");
        lua_pushstring(L, i.inventory_image.c_str());
@@ -162,7 +168,7 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
        lua_setfield(L, -2, "usable");
        lua_pushboolean(L, i.liquids_pointable);
        lua_setfield(L, -2, "liquids_pointable");
-       if (i.type == ITEM_TOOL) {
+       if (i.tool_capabilities) {
                push_tool_capabilities(L, *i.tool_capabilities);
                lua_setfield(L, -2, "tool_capabilities");
        }
@@ -182,9 +188,11 @@ void read_object_properties(lua_State *L, int index,
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
-       if(!lua_istable(L, index))
+       if (lua_isnil(L, index))
                return;
 
+       luaL_checktype(L, -1, LUA_TTABLE);
+
        int hp_max = 0;
        if (getintfield(L, -1, "hp_max", hp_max)) {
                prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
@@ -192,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);
                }
        }
 
@@ -306,6 +312,17 @@ void read_object_properties(lua_State *L, int index,
                        prop->nametag_color = color;
        }
        lua_pop(L, 1);
+       lua_getfield(L, -1, "nametag_bgcolor");
+       if (!lua_isnil(L, -1)) {
+               if (lua_toboolean(L, -1)) {
+                       video::SColor color;
+                       if (read_color(L, -1, &color))
+                               prop->nametag_bgcolor = color;
+               } else {
+                       prop->nametag_bgcolor = nullopt;
+               }
+       }
+       lua_pop(L, 1);
 
        lua_getfield(L, -1, "automatic_face_movement_max_rotation_per_sec");
        if (lua_isnumber(L, -1)) {
@@ -323,6 +340,10 @@ void read_object_properties(lua_State *L, int index,
 
        getfloatfield(L, -1, "zoom_fov", prop->zoom_fov);
        getboolfield(L, -1, "use_texture_alpha", prop->use_texture_alpha);
+       getboolfield(L, -1, "shaded", prop->shaded);
+       getboolfield(L, -1, "show_on_minimap", prop->show_on_minimap);
+
+       getstringfield(L, -1, "damage_texture_modifier", prop->damage_texture_modifier);
 }
 
 /******************************************************************************/
@@ -393,6 +414,13 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "nametag");
        push_ARGB8(L, prop->nametag_color);
        lua_setfield(L, -2, "nametag_color");
+       if (prop->nametag_bgcolor) {
+               push_ARGB8(L, prop->nametag_bgcolor.value());
+               lua_setfield(L, -2, "nametag_bgcolor");
+       } else {
+               lua_pushboolean(L, false);
+               lua_setfield(L, -2, "nametag_bgcolor");
+       }
        lua_pushnumber(L, prop->automatic_face_movement_max_rotation_per_sec);
        lua_setfield(L, -2, "automatic_face_movement_max_rotation_per_sec");
        lua_pushlstring(L, prop->infotext.c_str(), prop->infotext.size());
@@ -405,6 +433,12 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "zoom_fov");
        lua_pushboolean(L, prop->use_texture_alpha);
        lua_setfield(L, -2, "use_texture_alpha");
+       lua_pushboolean(L, prop->shaded);
+       lua_setfield(L, -2, "shaded");
+       lua_pushlstring(L, prop->damage_texture_modifier.c_str(), prop->damage_texture_modifier.size());
+       lua_setfield(L, -2, "damage_texture_modifier");
+       lua_pushboolean(L, prop->show_on_minimap);
+       lua_setfield(L, -2, "show_on_minimap");
 }
 
 /******************************************************************************/
@@ -477,13 +511,11 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
 }
 
 /******************************************************************************/
-ContentFeatures read_content_features(lua_State *L, int index)
+void read_content_features(lua_State *L, ContentFeatures &f, int index)
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
 
-       ContentFeatures f;
-
        /* Cache existence of some callbacks */
        lua_getfield(L, index, "on_construct");
        if(!lua_isnil(L, -1)) f.has_on_construct = true;
@@ -606,22 +638,39 @@ ContentFeatures read_content_features(lua_State *L, int index)
        }
        lua_pop(L, 1);
 
-       f.alpha = getintfield_default(L, index, "alpha", 255);
+       /* alpha & use_texture_alpha */
+       // This is a bit complicated due to compatibility
+
+       f.setDefaultAlphaMode();
+
+       warn_if_field_exists(L, index, "alpha",
+               "Obsolete, only limited compatibility provided; "
+               "replaced by \"use_texture_alpha\"");
+       if (getintfield_default(L, index, "alpha", 255) != 255)
+               f.alpha = ALPHAMODE_BLEND;
+
+       lua_getfield(L, index, "use_texture_alpha");
+       if (lua_isboolean(L, -1)) {
+               warn_if_field_exists(L, index, "use_texture_alpha",
+                       "Boolean values are deprecated; use the new choices");
+               if (lua_toboolean(L, -1))
+                       f.alpha = (f.drawtype == NDT_NORMAL) ? ALPHAMODE_CLIP : ALPHAMODE_BLEND;
+       } else if (check_field_or_nil(L, -1, LUA_TSTRING, "use_texture_alpha")) {
+               int result = f.alpha;
+               string_to_enum(ScriptApiNode::es_TextureAlphaMode, result,
+                               std::string(lua_tostring(L, -1)));
+               f.alpha = static_cast<enum AlphaMode>(result);
+       }
+       lua_pop(L, 1);
 
-       bool usealpha = getboolfield_default(L, index,
-                       "use_texture_alpha", false);
-       if (usealpha)
-               f.alpha = 0;
+       /* Other stuff */
 
-       // Read node color.
        lua_getfield(L, index, "color");
        read_color(L, -1, &f.color);
        lua_pop(L, 1);
 
        getstringfield(L, index, "palette", f.palette_name);
 
-       /* Other stuff */
-
        lua_getfield(L, index, "post_effect_color");
        read_color(L, -1, &f.post_effect_color);
        lua_pop(L, 1);
@@ -634,24 +683,11 @@ ContentFeatures read_content_features(lua_State *L, int index)
        if (!f.palette_name.empty() &&
                        !(f.param_type_2 == CPT2_COLOR ||
                        f.param_type_2 == CPT2_COLORED_FACEDIR ||
-                       f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
+                       f.param_type_2 == CPT2_COLORED_WALLMOUNTED ||
+                       f.param_type_2 == CPT2_COLORED_DEGROTATE))
                warningstream << "Node " << f.name.c_str()
                        << " has a palette, but not a suitable paramtype2." << std::endl;
 
-       // Warn about some obsolete fields
-       warn_if_field_exists(L, index, "wall_mounted",
-                       "Obsolete; use paramtype2 = 'wallmounted'");
-       warn_if_field_exists(L, index, "light_propagates",
-                       "Obsolete; determined from paramtype");
-       warn_if_field_exists(L, index, "dug_item",
-                       "Obsolete; use 'drop' field");
-       warn_if_field_exists(L, index, "extra_dug_item",
-                       "Obsolete; use 'drop' field");
-       warn_if_field_exists(L, index, "extra_dug_item_rarity",
-                       "Obsolete; use 'drop' field");
-       warn_if_field_exists(L, index, "metadata_name",
-                       "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);
        f.light_propagates = (f.param_type == CPT_LIGHT);
@@ -686,6 +722,8 @@ ContentFeatures read_content_features(lua_State *L, int index)
        f.liquid_range = getintfield_default(L, index,
                        "liquid_range", f.liquid_range);
        f.leveled = getintfield_default(L, index, "leveled", f.leveled);
+       f.leveled_max = getintfield_default(L, index,
+                       "leveled_max", f.leveled_max);
 
        getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
        f.drowning = getintfield_default(L, index,
@@ -784,7 +822,6 @@ ContentFeatures read_content_features(lua_State *L, int index)
        getstringfield(L, index, "node_dig_prediction",
                f.node_dig_prediction);
 
-       return f;
 }
 
 void push_content_features(lua_State *L, const ContentFeatures &c)
@@ -852,6 +889,8 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
        lua_setfield(L, -2, "post_effect_color");
        lua_pushnumber(L, c.leveled);
        lua_setfield(L, -2, "leveled");
+       lua_pushnumber(L, c.leveled_max);
+       lua_setfield(L, -2, "leveled_max");
        lua_pushboolean(L, c.sunlight_propagates);
        lua_setfield(L, -2, "sunlight_propagates");
        lua_pushnumber(L, c.light_source);
@@ -1027,13 +1066,15 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
-       if(lua_isnil(L, index)){
-       } else if(lua_istable(L, index)){
+       if (lua_isnil(L, index))
+               return;
+
+       if (lua_istable(L, index)) {
                getstringfield(L, index, "name", spec.name);
                getfloatfield(L, index, "gain", spec.gain);
                getfloatfield(L, index, "fade", spec.fade);
                getfloatfield(L, index, "pitch", spec.pitch);
-       } else if(lua_isstring(L, index)){
+       } else if (lua_isstring(L, index)) {
                spec.name = lua_tostring(L, index);
        }
 }
@@ -1055,9 +1096,13 @@ void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
 NodeBox read_nodebox(lua_State *L, int index)
 {
        NodeBox nodebox;
-       if(lua_istable(L, -1)){
-               nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
-                               ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
+       if (lua_isnil(L, -1))
+               return nodebox;
+
+       luaL_checktype(L, -1, LUA_TTABLE);
+
+       nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
+                       ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
 
 #define NODEBOXREAD(n, s){ \
                lua_getfield(L, index, (s)); \
@@ -1067,30 +1112,30 @@ NodeBox read_nodebox(lua_State *L, int index)
        }
 
 #define NODEBOXREADVEC(n, s) \
-               lua_getfield(L, index, (s)); \
-               if (lua_istable(L, -1)) \
-                       (n) = read_aabb3f_vector(L, -1, BS); \
-               lua_pop(L, 1);
+       lua_getfield(L, index, (s)); \
+       if (lua_istable(L, -1)) \
+               (n) = read_aabb3f_vector(L, -1, BS); \
+       lua_pop(L, 1);
+
+       NODEBOXREADVEC(nodebox.fixed, "fixed");
+       NODEBOXREAD(nodebox.wall_top, "wall_top");
+       NODEBOXREAD(nodebox.wall_bottom, "wall_bottom");
+       NODEBOXREAD(nodebox.wall_side, "wall_side");
+       NODEBOXREADVEC(nodebox.connect_top, "connect_top");
+       NODEBOXREADVEC(nodebox.connect_bottom, "connect_bottom");
+       NODEBOXREADVEC(nodebox.connect_front, "connect_front");
+       NODEBOXREADVEC(nodebox.connect_left, "connect_left");
+       NODEBOXREADVEC(nodebox.connect_back, "connect_back");
+       NODEBOXREADVEC(nodebox.connect_right, "connect_right");
+       NODEBOXREADVEC(nodebox.disconnected_top, "disconnected_top");
+       NODEBOXREADVEC(nodebox.disconnected_bottom, "disconnected_bottom");
+       NODEBOXREADVEC(nodebox.disconnected_front, "disconnected_front");
+       NODEBOXREADVEC(nodebox.disconnected_left, "disconnected_left");
+       NODEBOXREADVEC(nodebox.disconnected_back, "disconnected_back");
+       NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
+       NODEBOXREADVEC(nodebox.disconnected, "disconnected");
+       NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
 
-               NODEBOXREADVEC(nodebox.fixed, "fixed");
-               NODEBOXREAD(nodebox.wall_top, "wall_top");
-               NODEBOXREAD(nodebox.wall_bottom, "wall_bottom");
-               NODEBOXREAD(nodebox.wall_side, "wall_side");
-               NODEBOXREADVEC(nodebox.connect_top, "connect_top");
-               NODEBOXREADVEC(nodebox.connect_bottom, "connect_bottom");
-               NODEBOXREADVEC(nodebox.connect_front, "connect_front");
-               NODEBOXREADVEC(nodebox.connect_left, "connect_left");
-               NODEBOXREADVEC(nodebox.connect_back, "connect_back");
-               NODEBOXREADVEC(nodebox.connect_right, "connect_right");
-               NODEBOXREADVEC(nodebox.disconnected_top, "disconnected_top");
-               NODEBOXREADVEC(nodebox.disconnected_bottom, "disconnected_bottom");
-               NODEBOXREADVEC(nodebox.disconnected_front, "disconnected_front");
-               NODEBOXREADVEC(nodebox.disconnected_left, "disconnected_left");
-               NODEBOXREADVEC(nodebox.disconnected_back, "disconnected_back");
-               NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
-               NODEBOXREADVEC(nodebox.disconnected, "disconnected");
-               NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
-       }
        return nodebox;
 }
 
@@ -1303,26 +1348,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<ItemStack> items = read_items(L, tableindex,srv);
-       int listsize = (forcesize != -1) ? forcesize : items.size();
+       size_t listsize = (forcesize >= 0) ? forcesize : items.size();
+
+       // Create or resize/clear list
        InventoryList *invlist = inv->addList(name, listsize);
-       int index = 0;
-       for(std::vector<ItemStack>::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]);
        }
 }
 
@@ -1519,8 +1566,11 @@ void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask
 /******************************************************************************/
 void read_groups(lua_State *L, int index, ItemGroupList &result)
 {
-       if (!lua_istable(L,index))
+       if (lua_isnil(L, index))
                return;
+
+       luaL_checktype(L, index, LUA_TTABLE);
+
        result.clear();
        lua_pushnil(L);
        if (index < 0)
@@ -1858,6 +1908,7 @@ void read_hud_element(lua_State *L, HudElement *elem)
        elem->dir     = getintfield_default(L, 2, "direction", 0);
        elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX,
                        getintfield_default(L, 2, "z_index", 0)));
+       elem->text2   = getstringfield_default(L, 2, "text2", "");
 
        // Deprecated, only for compatibility's sake
        if (elem->dir == 0)
@@ -1875,6 +1926,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!");
@@ -1926,16 +1979,25 @@ void push_hud_element(lua_State *L, HudElement *elem)
 
        lua_pushnumber(L, elem->z_index);
        lua_setfield(L, -2, "z_index");
+
+       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)
+bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value)
 {
-       HudElementStat stat = HUD_STAT_NUMBER;
-       if (lua_isstring(L, 3)) {
+       std::string statstr = lua_tostring(L, 3);
+       {
                int statint;
-               std::string 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) {
@@ -1961,6 +2023,8 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
                        break;
                case HUD_STAT_ITEM:
                        elem->item = luaL_checknumber(L, 4);
+                       if (elem->type == HUD_ELEM_WAYPOINT && statstr == "precision")
+                               elem->item++;
                        *value = &elem->item;
                        break;
                case HUD_STAT_DIR:
@@ -1987,6 +2051,71 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
                        elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX, luaL_checknumber(L, 4)));
                        *value = &elem->z_index;
                        break;
+               case HUD_STAT_TEXT2:
+                       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 true;
+}
+
+/******************************************************************************/
+
+// Indices must match values in `enum CollisionType` exactly!!
+static const char *collision_type_str[] = {
+       "node",
+       "object",
+};
+
+// Indices must match values in `enum CollisionAxis` exactly!!
+static const char *collision_axis_str[] = {
+       "x",
+       "y",
+       "z",
+};
+
+void push_collision_move_result(lua_State *L, const collisionMoveResult &res)
+{
+       lua_createtable(L, 0, 4);
+
+       setboolfield(L, -1, "touching_ground", res.touching_ground);
+       setboolfield(L, -1, "collides", res.collides);
+       setboolfield(L, -1, "standing_on_object", res.standing_on_object);
+
+       /* collisions */
+       lua_createtable(L, res.collisions.size(), 0);
+       int i = 1;
+       for (const auto &c : res.collisions) {
+               lua_createtable(L, 0, 5);
+
+               lua_pushstring(L, collision_type_str[c.type]);
+               lua_setfield(L, -2, "type");
+
+               assert(c.axis != COLLISION_AXIS_NONE);
+               lua_pushstring(L, collision_axis_str[c.axis]);
+               lua_setfield(L, -2, "axis");
+
+               if (c.type == COLLISION_NODE) {
+                       push_v3s16(L, c.node_p);
+                       lua_setfield(L, -2, "node_pos");
+               } else if (c.type == COLLISION_OBJECT) {
+                       push_objectRef(L, c.object->getId());
+                       lua_setfield(L, -2, "object");
+               }
+
+               push_v3f(L, c.old_speed / BS);
+               lua_setfield(L, -2, "old_velocity");
+
+               push_v3f(L, c.new_speed / BS);
+               lua_setfield(L, -2, "new_velocity");
+
+               lua_rawseti(L, -2, i++);
        }
-       return stat;
+       lua_setfield(L, -2, "collisions");
+       /**/
 }