]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/common/c_content.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / script / common / c_content.cpp
index e9090e73368ef35bdb0f53c7f0bfc61627f472ff..0bdeaab9ee276a518bb926b01cd516f65cd0faa0 100644 (file)
@@ -120,6 +120,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);
 }
 
 /******************************************************************************/
@@ -141,8 +143,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");
-       lua_pushstring(L, i.short_description.c_str());
-       lua_setfield(L, -2, "short_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());
@@ -196,16 +200,14 @@ 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)
-                               sao->getEnv()->getGameDef()->SendPlayerHPOrDie((PlayerSAO *)sao, reason);
                }
        }
 
        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);
@@ -313,6 +315,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)) {
@@ -404,6 +417,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());
@@ -493,6 +513,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)
 {
@@ -666,7 +715,8 @@ void read_content_features(lua_State *L, ContentFeatures &f, 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;
 
@@ -701,6 +751,9 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
        // the slowest possible
        f.liquid_viscosity = getintfield_default(L, index,
                        "liquid_viscosity", f.liquid_viscosity);
+       // If move_resistance is not set explicitly,
+       // move_resistance is equal to liquid_viscosity
+       f.move_resistance = f.liquid_viscosity;
        f.liquid_range = getintfield_default(L, index,
                        "liquid_range", f.liquid_range);
        f.leveled = getintfield_default(L, index, "leveled", f.leveled);
@@ -804,6 +857,21 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
        getstringfield(L, index, "node_dig_prediction",
                f.node_dig_prediction);
 
+       // How much the node slows down players, ranging from 1 to 7,
+       // the higher, the slower.
+       f.move_resistance = getintfield_default(L, index,
+                       "move_resistance", f.move_resistance);
+
+       // Whether e.g. players in this node will have liquid movement physics
+       lua_getfield(L, index, "liquid_move_physics");
+       if(lua_isboolean(L, -1)) {
+               f.liquid_move_physics = lua_toboolean(L, -1);
+       } else if(lua_isnil(L, -1)) {
+               f.liquid_move_physics = f.liquid_type != LIQUID_NONE;
+       } else {
+               errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl;
+       }
+       lua_pop(L, 1);
 }
 
 void push_content_features(lua_State *L, const ContentFeatures &c)
@@ -813,9 +881,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);
@@ -931,6 +1022,10 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
        lua_setfield(L, -2, "legacy_wallmounted");
        lua_pushstring(L, c.node_dig_prediction.c_str());
        lua_setfield(L, -2, "node_dig_prediction");
+       lua_pushnumber(L, c.move_resistance);
+       lua_setfield(L, -2, "move_resistance");
+       lua_pushboolean(L, c.liquid_move_physics);
+       lua_setfield(L, -2, "liquid_move_physics");
 }
 
 /******************************************************************************/
@@ -1311,59 +1406,53 @@ void push_tool_capabilities(lua_State *L,
 }
 
 /******************************************************************************/
-void push_inventory(lua_State *L, Inventory *inventory)
+void push_inventory_list(lua_State *L, const InventoryList &invlist)
+{
+       push_items(L, invlist.getItems());
+}
+
+/******************************************************************************/
+void push_inventory_lists(lua_State *L, const Inventory &inv)
 {
-       std::vector<const InventoryList*> lists = inventory->getLists();
-       std::vector<const InventoryList*>::iterator iter = lists.begin();
+       const auto &lists = inv.getLists();
        lua_createtable(L, 0, lists.size());
-       for (; iter != lists.end(); iter++) {
-               const char* name = (*iter)->getName().c_str();
-               lua_pushstring(L, name);
-               push_inventory_list(L, inventory, name);
+       for(const InventoryList *list : lists) {
+               const std::string &name = list->getName();
+               lua_pushlstring(L, name.c_str(), name.size());
+               push_inventory_list(L, *list);
                lua_rawset(L, -3);
        }
 }
 
 /******************************************************************************/
 void read_inventory_list(lua_State *L, int tableindex,
-               Inventory *inv, const char *name, Server* srv, int forcesize)
+               Inventory *inv, const char *name, IGameDef *gdef, int forcesize)
 {
        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
-       std::vector<ItemStack> items = read_items(L, tableindex,srv);
-       int listsize = (forcesize != -1) ? forcesize : items.size();
+
+       // Get Lua-specified items to insert into the list
+       std::vector<ItemStack> items = read_items(L, tableindex, gdef);
+       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++;
-       }
-       while(forcesize != -1 && index < forcesize){
-               invlist->deleteItem(index);
-               index++;
+       if (!invlist) {
+               luaL_error(L, "inventory list: cannot create list named '%s'", name);
+               return;
        }
-}
 
-void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
-{
-       InventoryList *invlist = inv->getList(name);
-       if(invlist == NULL){
-               lua_pushnil(L);
-               return;
+       for (size_t i = 0; i < items.size(); ++i) {
+               if (i == listsize)
+                       break; // Truncate provided list of items
+               invlist->changeItem(i, items[i]);
        }
-       std::vector<ItemStack> items;
-       for(u32 i=0; i<invlist->getSize(); i++)
-               items.push_back(invlist->getItem(i));
-       push_items(L, items);
 }
 
 /******************************************************************************/
@@ -1624,7 +1713,7 @@ void push_items(lua_State *L, const std::vector<ItemStack> &items)
 }
 
 /******************************************************************************/
-std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
+std::vector<ItemStack> read_items(lua_State *L, int index, IGameDef *gdef)
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
@@ -1640,7 +1729,7 @@ std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
                if (items.size() < (u32) key) {
                        items.resize(key);
                }
-               items[key - 1] = read_item(L, -1, srv->idef());
+               items[key - 1] = read_item(L, -1, gdef->idef());
                lua_pop(L, 1);
        }
        return items;
@@ -1691,24 +1780,19 @@ bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
 void push_noiseparams(lua_State *L, NoiseParams *np)
 {
        lua_newtable(L);
-       push_float_string(L, np->offset);
-       lua_setfield(L, -2, "offset");
-       push_float_string(L, np->scale);
-       lua_setfield(L, -2, "scale");
-       push_float_string(L, np->persist);
-       lua_setfield(L, -2, "persistence");
-       push_float_string(L, np->lacunarity);
-       lua_setfield(L, -2, "lacunarity");
-       lua_pushnumber(L, np->seed);
-       lua_setfield(L, -2, "seed");
-       lua_pushnumber(L, np->octaves);
-       lua_setfield(L, -2, "octaves");
+       setfloatfield(L, -1, "offset",      np->offset);
+       setfloatfield(L, -1, "scale",       np->scale);
+       setfloatfield(L, -1, "persist",     np->persist);
+       setfloatfield(L, -1, "persistence", np->persist);
+       setfloatfield(L, -1, "lacunarity",  np->lacunarity);
+       setintfield(  L, -1, "seed",        np->seed);
+       setintfield(  L, -1, "octaves",     np->octaves);
 
        push_flags_string(L, flagdesc_noiseparams, np->flags,
                np->flags);
        lua_setfield(L, -2, "flags");
 
-       push_v3_float_string(L, np->spread);
+       push_v3f(L, np->spread);
        lua_setfield(L, -2, "spread");
 }
 
@@ -1862,14 +1946,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");
@@ -1943,6 +2020,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!");
@@ -1970,6 +2049,12 @@ void push_hud_element(lua_State *L, HudElement *elem)
        lua_pushnumber(L, elem->number);
        lua_setfield(L, -2, "number");
 
+       if (elem->type == HUD_ELEM_WAYPOINT) {
+               // waypoints reuse the item field to store precision, precision = item - 1
+               lua_pushnumber(L, elem->item - 1);
+               lua_setfield(L, -2, "precision");
+       }
+       // push the item field for waypoints as well for backwards compatibility
        lua_pushnumber(L, elem->item);
        lua_setfield(L, -2, "item");
 
@@ -1997,17 +2082,22 @@ 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)
+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) {
@@ -2065,8 +2155,13 @@ 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;
+
+       return true;
 }
 
 /******************************************************************************/
@@ -2129,7 +2224,7 @@ void push_collision_move_result(lua_State *L, const collisionMoveResult &res)
 void push_physics_override(lua_State *L, float speed, float jump, float gravity, bool sneak, bool sneak_glitch, bool new_move)
 {
        lua_createtable(L, 0, 6);
-       
+
        lua_pushnumber(L, speed);
        lua_setfield(L, -2, "speed");