]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/common/c_content.cpp
Rework use_texture_alpha to provide three opaque/clip/blend modes
[minetest.git] / src / script / common / c_content.cpp
index 8d94235ce261b95fb26f72e85c4a88141a2f31b6..ecab7baa1af4bec0bcc66f330ed67ab93f20f3c4 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"
@@ -28,10 +29,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 "mg_schematic.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>
@@ -55,8 +56,11 @@ 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);
+       getstringfield(L, index, "wield_overlay", def.wield_overlay);
        getstringfield(L, index, "palette", def.palette_image);
 
        // Read item color.
@@ -79,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",
-                       "Deprecated; use tool_capabilities");
-
        lua_getfield(L, index, "tool_capabilities");
        if(lua_istable(L, -1)){
                def.tool_capabilities = new ToolCapabilities(
@@ -91,7 +92,7 @@ void read_item_definition(lua_State* L, int index,
        // If name is "" (hand), ensure there are ToolCapabilities
        // because it will be looked up there whenever any other item has
        // no ToolCapabilities
-       if(def.name == "" && def.tool_capabilities == NULL){
+       if (def.name.empty() && def.tool_capabilities == NULL){
                def.tool_capabilities = new ToolCapabilities();
        }
 
@@ -100,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);
@@ -138,12 +140,18 @@ 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");
        lua_pushstring(L, type.c_str());
        lua_setfield(L, -2, "type");
        lua_pushstring(L, i.inventory_image.c_str());
        lua_setfield(L, -2, "inventory_image");
+       lua_pushstring(L, i.inventory_overlay.c_str());
+       lua_setfield(L, -2, "inventory_overlay");
        lua_pushstring(L, i.wield_image.c_str());
        lua_setfield(L, -2, "wield_image");
+       lua_pushstring(L, i.wield_overlay.c_str());
+       lua_setfield(L, -2, "wield_overlay");
        lua_pushstring(L, i.palette_image.c_str());
        lua_setfield(L, -2, "palette_image");
        push_ARGB8(L, i.color);
@@ -156,12 +164,8 @@ 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) {
-               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));
+       if (i.tool_capabilities) {
+               push_tool_capabilities(L, *i.tool_capabilities);
                lua_setfield(L, -2, "tool_capabilities");
        }
        push_groups(L, i.groups);
@@ -176,32 +180,68 @@ 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;
-       if(!lua_istable(L, index))
+       if (lua_isnil(L, index))
                return;
 
-       prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
+       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);
 
+               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");
-       if(lua_istable(L, -1))
+       bool collisionbox_defined = lua_istable(L, -1);
+       if (collisionbox_defined)
                prop->collisionbox = read_aabb3f(L, -1, 1.0);
        lua_pop(L, 1);
 
+       lua_getfield(L, -1, "selectionbox");
+       if (lua_istable(L, -1))
+               prop->selectionbox = read_aabb3f(L, -1, 1.0);
+       else if (collisionbox_defined)
+               prop->selectionbox = prop->collisionbox;
+       lua_pop(L, 1);
+
+       getboolfield(L, -1, "pointable", prop->pointable);
        getstringfield(L, -1, "visual", prop->visual);
 
        getstringfield(L, -1, "mesh", prop->mesh);
 
        lua_getfield(L, -1, "visual_size");
-       if(lua_istable(L, -1))
-               prop->visual_size = read_v2f(L, -1);
+       if (lua_istable(L, -1)) {
+               // Backwards compatibility: Also accept { x = ?, y = ? }
+               v2f scale_xy = read_v2f(L, -1);
+
+               f32 scale_z = scale_xy.X;
+               lua_getfield(L, -1, "z");
+               if (lua_isnumber(L, -1))
+                       scale_z = lua_tonumber(L, -1);
+               lua_pop(L, 1);
+
+               prop->visual_size = v3f(scale_xy.X, scale_xy.Y, scale_z);
+       }
        lua_pop(L, 1);
 
        lua_getfield(L, -1, "textures");
@@ -212,9 +252,9 @@ void read_object_properties(lua_State *L, int index,
                while(lua_next(L, table) != 0){
                        // key at index -2 and value at index -1
                        if(lua_isstring(L, -1))
-                               prop->textures.push_back(lua_tostring(L, -1));
+                               prop->textures.emplace_back(lua_tostring(L, -1));
                        else
-                               prop->textures.push_back("");
+                               prop->textures.emplace_back("");
                        // removes value, keeps key for next iteration
                        lua_pop(L, 1);
                }
@@ -245,9 +285,11 @@ void read_object_properties(lua_State *L, int index,
 
        getboolfield(L, -1, "is_visible", prop->is_visible);
        getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
-       getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
        if (getfloatfield(L, -1, "stepheight", prop->stepheight))
                prop->stepheight *= BS;
+       getfloatfield(L, -1, "eye_height", prop->eye_height);
+
+       getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
        lua_getfield(L, -1, "automatic_face_movement_dir");
        if (lua_isnumber(L, -1)) {
                prop->automatic_face_movement_dir = true;
@@ -258,6 +300,7 @@ void read_object_properties(lua_State *L, int index,
        }
        lua_pop(L, 1);
        getboolfield(L, -1, "backface_culling", prop->backface_culling);
+       getintfield(L, -1, "glow", prop->glow);
 
        getstringfield(L, -1, "nametag", prop->nametag);
        lua_getfield(L, -1, "nametag_color");
@@ -273,11 +316,21 @@ void read_object_properties(lua_State *L, int index,
                prop->automatic_face_movement_max_rotation_per_sec = luaL_checknumber(L, -1);
        }
        lua_pop(L, 1);
+
        getstringfield(L, -1, "infotext", prop->infotext);
+       getboolfield(L, -1, "static_save", prop->static_save);
+
        lua_getfield(L, -1, "wield_item");
        if (!lua_isnil(L, -1))
                prop->wield_item = read_item(L, -1, idef).getItemString();
        lua_pop(L, 1);
+
+       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);
 }
 
 /******************************************************************************/
@@ -286,36 +339,38 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_newtable(L);
        lua_pushnumber(L, prop->hp_max);
        lua_setfield(L, -2, "hp_max");
+       lua_pushnumber(L, prop->breath_max);
+       lua_setfield(L, -2, "breath_max");
        lua_pushboolean(L, prop->physical);
        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);
+       lua_setfield(L, -2, "selectionbox");
+       lua_pushboolean(L, prop->pointable);
+       lua_setfield(L, -2, "pointable");
        lua_pushlstring(L, prop->visual.c_str(), prop->visual.size());
        lua_setfield(L, -2, "visual");
        lua_pushlstring(L, prop->mesh.c_str(), prop->mesh.size());
        lua_setfield(L, -2, "mesh");
-       push_v2f(L, prop->visual_size);
+       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 (std::vector<std::string>::iterator it = prop->textures.begin();
-                       it != prop->textures.end(); ++it) {
-               lua_pushlstring(L, it->c_str(), it->size());
-               lua_rawseti(L, -2, i);
+       for (const std::string &texture : prop->textures) {
+               lua_pushlstring(L, texture.c_str(), texture.size());
+               lua_rawseti(L, -2, i++);
        }
        lua_setfield(L, -2, "textures");
 
-       lua_newtable(L);
+       lua_createtable(L, prop->colors.size(), 0);
        i = 1;
-       for (std::vector<video::SColor>::iterator it = prop->colors.begin();
-                       it != prop->colors.end(); ++it) {
-               push_ARGB8(L, *it);
-               lua_rawseti(L, -2, i);
+       for (const video::SColor &color : prop->colors) {
+               push_ARGB8(L, color);
+               lua_rawseti(L, -2, i++);
        }
        lua_setfield(L, -2, "colors");
 
@@ -327,10 +382,12 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "is_visible");
        lua_pushboolean(L, prop->makes_footstep_sound);
        lua_setfield(L, -2, "makes_footstep_sound");
-       lua_pushnumber(L, prop->automatic_rotate);
-       lua_setfield(L, -2, "automatic_rotate");
        lua_pushnumber(L, prop->stepheight / BS);
        lua_setfield(L, -2, "stepheight");
+       lua_pushnumber(L, prop->eye_height);
+       lua_setfield(L, -2, "eye_height");
+       lua_pushnumber(L, prop->automatic_rotate);
+       lua_setfield(L, -2, "automatic_rotate");
        if (prop->automatic_face_movement_dir)
                lua_pushnumber(L, prop->automatic_face_movement_dir_offset);
        else
@@ -338,6 +395,8 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "automatic_face_movement_dir");
        lua_pushboolean(L, prop->backface_culling);
        lua_setfield(L, -2, "backface_culling");
+       lua_pushnumber(L, prop->glow);
+       lua_setfield(L, -2, "glow");
        lua_pushlstring(L, prop->nametag.c_str(), prop->nametag.size());
        lua_setfield(L, -2, "nametag");
        push_ARGB8(L, prop->nametag_color);
@@ -346,8 +405,20 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
        lua_setfield(L, -2, "automatic_face_movement_max_rotation_per_sec");
        lua_pushlstring(L, prop->infotext.c_str(), prop->infotext.size());
        lua_setfield(L, -2, "infotext");
+       lua_pushboolean(L, prop->static_save);
+       lua_setfield(L, -2, "static_save");
        lua_pushlstring(L, prop->wield_item.c_str(), prop->wield_item.size());
        lua_setfield(L, -2, "wield_item");
+       lua_pushnumber(L, prop->zoom_fov);
+       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");
 }
 
 /******************************************************************************/
@@ -396,6 +467,16 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
                        L, index, "tileable_horizontal", default_tiling);
                tiledef.tileable_vertical = getboolfield_default(
                        L, index, "tileable_vertical", default_tiling);
+               std::string align_style;
+               if (getstringfield(L, index, "align_style", align_style)) {
+                       if (align_style == "user")
+                               tiledef.align_style = ALIGN_STYLE_USER_DEFINED;
+                       else if (align_style == "world")
+                               tiledef.align_style = ALIGN_STYLE_WORLD;
+                       else
+                               tiledef.align_style = ALIGN_STYLE_NODE;
+               }
+               tiledef.scale = getintfield_default(L, index, "scale", 0);
                // color = ...
                lua_getfield(L, index, "color");
                tiledef.has_color = read_color(L, -1, &tiledef.color);
@@ -410,13 +491,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;
@@ -539,22 +618,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);
@@ -564,27 +660,13 @@ ContentFeatures read_content_features(lua_State *L, int index)
        f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
                        ScriptApiNode::es_ContentParamType2, CPT2_NONE);
 
-       if (f.palette_name != "" &&
+       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))
                warningstream << "Node " << f.name.c_str()
                        << " has a palette, but not a suitable paramtype2." << std::endl;
 
-       // Warn about some deprecated fields
-       warn_if_field_exists(L, index, "wall_mounted",
-                       "Deprecated; use paramtype2 = 'wallmounted'");
-       warn_if_field_exists(L, index, "light_propagates",
-                       "Deprecated; determined from paramtype");
-       warn_if_field_exists(L, index, "dug_item",
-                       "Deprecated; use 'drop' field");
-       warn_if_field_exists(L, index, "extra_dug_item",
-                       "Deprecated; use 'drop' field");
-       warn_if_field_exists(L, index, "extra_dug_item_rarity",
-                       "Deprecated; use 'drop' field");
-       warn_if_field_exists(L, index, "metadata_name",
-                       "Deprecated; 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);
@@ -619,6 +701,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,
@@ -646,7 +730,7 @@ ContentFeatures read_content_features(lua_State *L, int index)
                lua_pushnil(L);
                while (lua_next(L, table) != 0) {
                        // Value at -1
-                       f.connects_to.push_back(lua_tostring(L, -1));
+                       f.connects_to.emplace_back(lua_tostring(L, -1));
                        lua_pop(L, 1);
                }
        }
@@ -713,7 +797,10 @@ ContentFeatures read_content_features(lua_State *L, int index)
        }
        lua_pop(L, 1);
 
-       return f;
+       // Node immediately placed by client when node is dug
+       getstringfield(L, index, "node_dig_prediction",
+               f.node_dig_prediction);
+
 }
 
 void push_content_features(lua_State *L, const ContentFeatures &c)
@@ -769,12 +856,11 @@ 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 (std::vector<std::string>::const_iterator it = c.connects_to.begin();
-                       it != c.connects_to.end(); ++it) {
-               lua_pushlstring(L, it->c_str(), it->size());
-               lua_rawseti(L, -2, i);
+       for (const std::string &it : c.connects_to) {
+               lua_pushlstring(L, it.c_str(), it.size());
+               lua_rawseti(L, -2, i++);
        }
        lua_setfield(L, -2, "connects_to");
 
@@ -782,6 +868,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);
@@ -838,6 +926,8 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
        lua_setfield(L, -2, "legacy_facedir_simple");
        lua_pushboolean(L, c.legacy_wallmounted);
        lua_setfield(L, -2, "legacy_wallmounted");
+       lua_pushstring(L, c.node_dig_prediction.c_str());
+       lua_setfield(L, -2, "node_dig_prediction");
 }
 
 /******************************************************************************/
@@ -891,12 +981,11 @@ 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 (std::vector<aabb3f>::const_iterator it = box.begin();
-                       it != box.end(); ++it) {
-               push_aabb3f(L, (*it));
-               lua_rawseti(L, -2, i);
+       for (const aabb3f &it : box) {
+               push_aabb3f(L, it);
+               lua_rawseti(L, -2, i++);
        }
 }
 
@@ -947,6 +1036,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);
        }
 }
 
@@ -955,20 +1045,22 @@ 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);
        }
 }
 
 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);
@@ -983,46 +1075,56 @@ 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);
 
-#define NODEBOXREAD(n, s) \
-       do { \
+       nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
+                       ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
+
+#define NODEBOXREAD(n, s){ \
                lua_getfield(L, index, (s)); \
                if (lua_istable(L, -1)) \
                        (n) = read_aabb3f(L, -1, BS); \
                lua_pop(L, 1); \
-       } while (0)
+       }
 
 #define NODEBOXREADVEC(n, s) \
-       do { \
-               lua_getfield(L, index, (s)); \
-               if (lua_istable(L, -1)) \
-                       (n) = read_aabb3f_vector(L, -1, BS); \
-               lua_pop(L, 1); \
-       } while (0)
-               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");
-       }
+       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");
+
        return nodebox;
 }
 
 /******************************************************************************/
-MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
+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;
@@ -1037,18 +1139,22 @@ MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
                param2 = lua_tonumber(L, -1);
        lua_pop(L, 1);
 
-       return MapNode(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, INodeDefManager *ndef)
+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");
 }
 
@@ -1081,7 +1187,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;
                }
@@ -1096,18 +1202,17 @@ ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
 
-       if(lua_isnil(L, index))
-       {
+       if (lua_isnil(L, index)) {
                return ItemStack();
        }
-       else if(lua_isuserdata(L, index))
-       {
+
+       if (lua_isuserdata(L, index)) {
                // Convert from LuaItemStack
                LuaItemStack *o = LuaItemStack::checkobject(L, index);
                return o->getItem();
        }
-       else if(lua_isstring(L, index))
-       {
+
+       if (lua_isstring(L, index)) {
                // Convert from itemstring
                std::string itemstring = lua_tostring(L, index);
                try
@@ -1164,22 +1269,21 @@ 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
-               for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin();
-                       i != toolcap.groupcaps.end(); ++i) {
+               for (const auto &gc_it : toolcap.groupcaps) {
                        // Create groupcap table
                        lua_newtable(L);
-                       const std::string &name = i->first;
-                       const ToolGroupCap &groupcap = i->second;
+                       const std::string &name = gc_it.first;
+                       const ToolGroupCap &groupcap = gc_it.second;
                        // Create subtable "times"
                        lua_newtable(L);
-                       for (std::unordered_map<int, float>::const_iterator
-                                       i = groupcap.times.begin(); i != groupcap.times.end(); ++i) {
-                               lua_pushinteger(L, i->first);
-                               lua_pushnumber(L, i->second);
+                       for (auto time : groupcap.times) {
+                               lua_pushinteger(L, time.first);
+                               lua_pushnumber(L, time.second);
                                lua_settable(L, -3);
                        }
                        // Set subtable "times"
@@ -1195,11 +1299,10 @@ void push_tool_capabilities(lua_State *L,
                //Create damage_groups table
                lua_newtable(L);
                // For each damage group
-               for (DamageGroup::const_iterator i = toolcap.damageGroups.begin();
-                       i != toolcap.damageGroups.end(); ++i) {
+               for (const auto &damageGroup : toolcap.damageGroups) {
                        // Create damage group table
-                       lua_pushinteger(L, i->second);
-                       lua_setfield(L, -2, i->first.c_str());
+                       lua_pushinteger(L, damageGroup.second);
+                       lua_setfield(L, -2, damageGroup.first.c_str());
                }
                lua_setfield(L, -2, "damage_groups");
 }
@@ -1289,6 +1392,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);
@@ -1358,7 +1462,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);
@@ -1367,7 +1471,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);
 }
@@ -1439,17 +1543,22 @@ 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)
+       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);
        }
@@ -1458,10 +1567,10 @@ void read_groups(lua_State *L, int index, ItemGroupList &result)
 /******************************************************************************/
 void push_groups(lua_State *L, const ItemGroupList &groups)
 {
-       lua_newtable(L);
-       for (ItemGroupList::const_iterator it = groups.begin(); it != groups.end(); ++it) {
-               lua_pushnumber(L, it->second);
-               lua_setfield(L, -2, it->first.c_str());
+       lua_createtable(L, 0, groups.size());
+       for (const auto &group : groups) {
+               lua_pushinteger(L, group.second);
+               lua_setfield(L, -2, group.first.c_str());
        }
 }
 
@@ -1505,7 +1614,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
@@ -1543,13 +1652,13 @@ bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
 void push_noiseparams(lua_State *L, NoiseParams *np)
 {
        lua_newtable(L);
-       lua_pushnumber(L, np->offset);
+       push_float_string(L, np->offset);
        lua_setfield(L, -2, "offset");
-       lua_pushnumber(L, np->scale);
+       push_float_string(L, np->scale);
        lua_setfield(L, -2, "scale");
-       lua_pushnumber(L, np->persist);
+       push_float_string(L, np->persist);
        lua_setfield(L, -2, "persistence");
-       lua_pushnumber(L, np->lacunarity);
+       push_float_string(L, np->lacunarity);
        lua_setfield(L, -2, "lacunarity");
        lua_pushnumber(L, np->seed);
        lua_setfield(L, -2, "seed");
@@ -1560,7 +1669,7 @@ void push_noiseparams(lua_State *L, NoiseParams *np)
                np->flags);
        lua_setfield(L, -2, "flags");
 
-       push_v3f(L, np->spread);
+       push_v3_float_string(L, np->spread);
        lua_setfield(L, -2, "spread");
 }
 
@@ -1572,9 +1681,8 @@ static int push_json_value_getdepth(const Json::Value &value)
                return 1;
 
        int maxdepth = 0;
-       for (Json::Value::const_iterator it = value.begin();
-                       it != value.end(); ++it) {
-               int elemdepth = push_json_value_getdepth(*it);
+       for (const auto &it : value) {
+               int elemdepth = push_json_value_getdepth(it);
                if (elemdepth > maxdepth)
                        maxdepth = elemdepth;
        }
@@ -1590,10 +1698,10 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
                        lua_pushvalue(L, nullindex);
                        break;
                case Json::intValue:
-                       lua_pushinteger(L, value.asInt());
+                       lua_pushinteger(L, value.asLargestInt());
                        break;
                case Json::uintValue:
-                       lua_pushinteger(L, value.asUInt());
+                       lua_pushinteger(L, value.asLargestUInt());
                        break;
                case Json::realValue:
                        lua_pushnumber(L, value.asDouble());
@@ -1608,7 +1716,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);
@@ -1616,10 +1724,10 @@ 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
+#if !defined(JSONCPP_STRING) && (JSONCPP_VERSION_MAJOR < 1 || JSONCPP_VERSION_MINOR < 9)
                                const char *str = it.memberName();
                                lua_pushstring(L, str ? str : "");
 #else
@@ -1646,8 +1754,8 @@ bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
        // of push_json_value_helper is 2, so make sure there a depth * 2 slots
        if (lua_checkstack(L, depth * 2))
                return push_json_value_helper(L, value, nullindex);
-       else
-               return false;
+
+       return false;
 }
 
 // Converts Lua table --> JSON
@@ -1701,7 +1809,8 @@ void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
        lua_pop(L, 1); // Pop value
 }
 
-void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm)
+void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm,
+       bool hitpoint)
 {
        lua_newtable(L);
        if (pointed.type == POINTEDTHING_NODE) {
@@ -1726,6 +1835,14 @@ void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm)
                lua_pushstring(L, "nothing");
                lua_setfield(L, -2, "type");
        }
+       if (hitpoint && (pointed.type != POINTEDTHING_NOTHING)) {
+               push_v3f(L, pointed.intersection_point / BS); // convert to node coords
+               lua_setfield(L, -2, "intersection_point");
+               push_v3s16(L, pointed.intersection_normal);
+               lua_setfield(L, -2, "intersection_normal");
+               lua_pushinteger(L, pointed.box_id + 1); // change to Lua array index
+               lua_setfield(L, -2, "box_id");
+       }
 }
 
 void push_objectRef(lua_State *L, const u16 id)
@@ -1734,8 +1851,236 @@ 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
 }
+
+void read_hud_element(lua_State *L, HudElement *elem)
+{
+       elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
+                                                                       es_HudElementType, HUD_ELEM_TEXT);
+
+       lua_getfield(L, 2, "position");
+       elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+       lua_pop(L, 1);
+
+       lua_getfield(L, 2, "scale");
+       elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+       lua_pop(L, 1);
+
+       lua_getfield(L, 2, "size");
+       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);
+       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)));
+       elem->text2   = getstringfield_default(L, 2, "text2", "");
+
+       // Deprecated, only for compatibility's sake
+       if (elem->dir == 0)
+               elem->dir = getintfield_default(L, 2, "dir", 0);
+
+       lua_getfield(L, 2, "alignment");
+       elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+       lua_pop(L, 1);
+
+       lua_getfield(L, 2, "offset");
+       elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+       lua_pop(L, 1);
+
+       lua_getfield(L, 2, "world_pos");
+       elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
+       lua_pop(L, 1);
+
+       /* check for known deprecated element usage */
+       if ((elem->type  == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
+               log_deprecated(L,"Deprecated usage of statbar without size!");
+}
+
+void push_hud_element(lua_State *L, HudElement *elem)
+{
+       lua_newtable(L);
+
+       lua_pushstring(L, es_HudElementType[(u8)elem->type].str);
+       lua_setfield(L, -2, "type");
+
+       push_v2f(L, elem->pos);
+       lua_setfield(L, -2, "position");
+
+       lua_pushstring(L, elem->name.c_str());
+       lua_setfield(L, -2, "name");
+
+       push_v2f(L, elem->scale);
+       lua_setfield(L, -2, "scale");
+
+       lua_pushstring(L, elem->text.c_str());
+       lua_setfield(L, -2, "text");
+
+       lua_pushnumber(L, elem->number);
+       lua_setfield(L, -2, "number");
+
+       lua_pushnumber(L, elem->item);
+       lua_setfield(L, -2, "item");
+
+       lua_pushnumber(L, elem->dir);
+       lua_setfield(L, -2, "direction");
+
+       push_v2f(L, elem->offset);
+       lua_setfield(L, -2, "offset");
+
+       push_v2f(L, elem->align);
+       lua_setfield(L, -2, "alignment");
+
+       push_v2s32(L, elem->size);
+       lua_setfield(L, -2, "size");
+
+       // Deprecated, only for compatibility's sake
+       lua_pushnumber(L, elem->dir);
+       lua_setfield(L, -2, "dir");
+
+       push_v3f(L, elem->world_pos);
+       lua_setfield(L, -2, "world_pos");
+
+       lua_pushnumber(L, elem->z_index);
+       lua_setfield(L, -2, "z_index");
+
+       lua_pushstring(L, elem->text2.c_str());
+       lua_setfield(L, -2, "text2");
+}
+
+HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
+{
+       HudElementStat stat = HUD_STAT_NUMBER;
+       std::string statstr;
+       if (lua_isstring(L, 3)) {
+               int statint;
+               statstr = lua_tostring(L, 3);
+               stat = string_to_enum(es_HudElementStat, statint, statstr) ?
+                               (HudElementStat)statint : stat;
+       }
+
+       switch (stat) {
+               case HUD_STAT_POS:
+                       elem->pos = read_v2f(L, 4);
+                       *value = &elem->pos;
+                       break;
+               case HUD_STAT_NAME:
+                       elem->name = luaL_checkstring(L, 4);
+                       *value = &elem->name;
+                       break;
+               case HUD_STAT_SCALE:
+                       elem->scale = read_v2f(L, 4);
+                       *value = &elem->scale;
+                       break;
+               case HUD_STAT_TEXT:
+                       elem->text = luaL_checkstring(L, 4);
+                       *value = &elem->text;
+                       break;
+               case HUD_STAT_NUMBER:
+                       elem->number = luaL_checknumber(L, 4);
+                       *value = &elem->number;
+                       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:
+                       elem->dir = luaL_checknumber(L, 4);
+                       *value = &elem->dir;
+                       break;
+               case HUD_STAT_ALIGN:
+                       elem->align = read_v2f(L, 4);
+                       *value = &elem->align;
+                       break;
+               case HUD_STAT_OFFSET:
+                       elem->offset = read_v2f(L, 4);
+                       *value = &elem->offset;
+                       break;
+               case HUD_STAT_WORLD_POS:
+                       elem->world_pos = read_v3f(L, 4);
+                       *value = &elem->world_pos;
+                       break;
+               case HUD_STAT_SIZE:
+                       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;
+               case HUD_STAT_TEXT2:
+                       elem->text2 = luaL_checkstring(L, 4);
+                       *value = &elem->text2;
+                       break;
+       }
+       return stat;
+}
+
+/******************************************************************************/
+
+// 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++);
+       }
+       lua_setfield(L, -2, "collisions");
+       /**/
+}