1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Clean up read_tiledef and related parts a bit

This commit is contained in:
sfan5 2025-05-13 17:43:30 +02:00
parent d17f22f536
commit 452160cd00
3 changed files with 51 additions and 37 deletions

View file

@ -191,8 +191,6 @@ struct TileSpec
bool world_aligned = false; bool world_aligned = false;
//! Tile rotation. //! Tile rotation.
TileRotation rotation = TileRotation::None; TileRotation rotation = TileRotation::None;
//! This much light does the tile emit.
u8 emissive_light = 0;
//! The first is base texture, the second is overlay. //! The first is base texture, the second is overlay.
TileLayer layers[MAX_TILE_LAYERS]; TileLayer layers[MAX_TILE_LAYERS];
}; };

View file

@ -56,9 +56,16 @@ void read_item_definition(lua_State* L, int index,
if (index < 0) if (index < 0)
index = lua_gettop(L) + 1 + index; index = lua_gettop(L) + 1 + index;
def.type = (ItemType)getenumfield(L, index, "type", def.name.clear();
es_ItemType, ITEM_NONE);
getstringfield(L, index, "name", def.name); getstringfield(L, index, "name", def.name);
{
auto str = getstringfield_default(L, index, "type", "");
if (!string_to_enum(es_ItemType, def.type, str))
warningstream << "Item " << def.name
<< " has unknown type \"" << str << '"' << std::endl;
}
getstringfield(L, index, "description", def.description); getstringfield(L, index, "description", def.description);
getstringfield(L, index, "short_description", def.short_description); getstringfield(L, index, "short_description", def.short_description);
getstringfield(L, index, "inventory_image", def.inventory_image); getstringfield(L, index, "inventory_image", def.inventory_image);
@ -605,9 +612,6 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype, bool special)
case NDT_PLANTLIKE: case NDT_PLANTLIKE:
case NDT_FIRELIKE: case NDT_FIRELIKE:
default_tiling = false; default_tiling = false;
// "break" is omitted here intentionaly, as PLANTLIKE
// FIRELIKE drawtype both should default to having
// backface_culling to false.
[[fallthrough]]; [[fallthrough]];
case NDT_MESH: case NDT_MESH:
case NDT_LIQUID: case NDT_LIQUID:
@ -621,7 +625,6 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype, bool special)
break; break;
} }
// key at index -2 and value at index
if(lua_isstring(L, index)){ if(lua_isstring(L, index)){
// "default_lava.png" // "default_lava.png"
tiledef.name = lua_tostring(L, index); tiledef.name = lua_tostring(L, index);
@ -634,7 +637,10 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype, bool special)
// name="default_lava.png" // name="default_lava.png"
tiledef.name.clear(); tiledef.name.clear();
getstringfield(L, index, "name", tiledef.name); getstringfield(L, index, "name", tiledef.name);
getstringfield(L, index, "image", tiledef.name); // MaterialSpec compat. warn_if_field_exists(L, index, "image", "TileDef",
"Deprecated: new name is \"name\".");
getstringfield(L, index, "image", tiledef.name);
tiledef.backface_culling = getboolfield_default( tiledef.backface_culling = getboolfield_default(
L, index, "backface_culling", default_culling); L, index, "backface_culling", default_culling);
tiledef.tileable_horizontal = getboolfield_default( tiledef.tileable_horizontal = getboolfield_default(
@ -659,6 +665,9 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype, bool special)
lua_getfield(L, index, "animation"); lua_getfield(L, index, "animation");
tiledef.animation = read_animation_definition(L, -1); tiledef.animation = read_animation_definition(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
} else if (!lua_isnil(L, index)) {
// TODO: should be an error
errorstream << "TileDef: Invalid type! (expected string or table)" << std::endl;
} }
return tiledef; return tiledef;
@ -672,13 +681,13 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
/* Cache existence of some callbacks */ /* Cache existence of some callbacks */
lua_getfield(L, index, "on_construct"); lua_getfield(L, index, "on_construct");
if(!lua_isnil(L, -1)) f.has_on_construct = true; f.has_on_construct = !lua_isnil(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "on_destruct"); lua_getfield(L, index, "on_destruct");
if(!lua_isnil(L, -1)) f.has_on_destruct = true; f.has_on_destruct = !lua_isnil(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "after_destruct"); lua_getfield(L, index, "after_destruct");
if(!lua_isnil(L, -1)) f.has_after_destruct = true; f.has_after_destruct = !lua_isnil(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "on_rightclick"); lua_getfield(L, index, "on_rightclick");
@ -695,8 +704,13 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
/* Visual definition */ /* Visual definition */
f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype", {
ScriptApiNode::es_DrawType,NDT_NORMAL); auto str = getstringfield_default(L, index, "drawtype", "");
if (!string_to_enum(ScriptApiNode::es_DrawType, f.drawtype, str))
warningstream << "Node " << f.name
<< " has unknown drawtype \"" << str << '"' << std::endl;
}
getfloatfield(L, index, "visual_scale", f.visual_scale); getfloatfield(L, index, "visual_scale", f.visual_scale);
/* Meshnode model filename */ /* Meshnode model filename */
@ -796,10 +810,7 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
if (lua_toboolean(L, -1)) if (lua_toboolean(L, -1))
f.alpha = (f.drawtype == NDT_NORMAL) ? ALPHAMODE_CLIP : ALPHAMODE_BLEND; f.alpha = (f.drawtype == NDT_NORMAL) ? ALPHAMODE_CLIP : ALPHAMODE_BLEND;
} else if (check_field_or_nil(L, -1, LUA_TSTRING, "use_texture_alpha")) { } else if (check_field_or_nil(L, -1, LUA_TSTRING, "use_texture_alpha")) {
int result = f.alpha; string_to_enum(ScriptApiNode::es_TextureAlphaMode, f.alpha, lua_tostring(L, -1));
string_to_enum(ScriptApiNode::es_TextureAlphaMode, result,
std::string(lua_tostring(L, -1)));
f.alpha = static_cast<enum AlphaMode>(result);
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -817,10 +828,18 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
getboolfield(L, index, "post_effect_color_shaded", f.post_effect_color_shaded); getboolfield(L, index, "post_effect_color_shaded", f.post_effect_color_shaded);
f.param_type = (ContentParamType)getenumfield(L, index, "paramtype", {
ScriptApiNode::es_ContentParamType, CPT_NONE); auto str = getstringfield_default(L, index, "paramtype", "");
f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2", if (!string_to_enum(ScriptApiNode::es_ContentParamType, f.param_type, str))
ScriptApiNode::es_ContentParamType2, CPT2_NONE); warningstream << "Node " << f.name
<< " has unknown paramtype \"" << str << '"' << std::endl;
}
{
auto str = getstringfield_default(L, index, "paramtype2", "");
if (!string_to_enum(ScriptApiNode::es_ContentParamType2, f.param_type_2, str))
warningstream << "Node " << f.name
<< " has unknown paramtype2 \"" << str << '"' << std::endl;
}
if (!f.palette_name.empty() && if (!f.palette_name.empty() &&
!(f.param_type_2 == CPT2_COLOR || !(f.param_type_2 == CPT2_COLOR ||
@ -855,8 +874,12 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
// Liquids flow into and replace node // Liquids flow into and replace node
getboolfield(L, index, "floodable", f.floodable); getboolfield(L, index, "floodable", f.floodable);
// Whether the node is non-liquid, source liquid or flowing liquid // Whether the node is non-liquid, source liquid or flowing liquid
f.liquid_type = (LiquidType)getenumfield(L, index, "liquidtype", {
ScriptApiNode::es_LiquidType, LIQUID_NONE); auto str = getstringfield_default(L, index, "liquidtype", "");
if (!string_to_enum(ScriptApiNode::es_LiquidType, f.liquid_type, str))
warningstream << "Node " << f.name
<< " has unknown liquidtype \"" << str << '"' << std::endl;
}
// If the content is liquid, this is the flowing version of the liquid. // If the content is liquid, this is the flowing version of the liquid.
getstringfield(L, index, "liquid_alternative_flowing", getstringfield(L, index, "liquid_alternative_flowing",
f.liquid_alternative_flowing); f.liquid_alternative_flowing);
@ -915,7 +938,7 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, table) != 0) { while (lua_next(L, table) != 0) {
// Value at -1 // Value at -1
std::string side(lua_tostring(L, -1)); std::string_view side(lua_tostring(L, -1));
// Note faces are flipped to make checking easier // Note faces are flipped to make checking easier
if (side == "top") if (side == "top")
f.connect_sides |= 2; f.connect_sides |= 2;
@ -986,6 +1009,7 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
} else if(lua_isnil(L, -1)) { } else if(lua_isnil(L, -1)) {
f.liquid_move_physics = f.liquid_type != LIQUID_NONE; f.liquid_move_physics = f.liquid_type != LIQUID_NONE;
} else { } else {
// TODO: should be an error
errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl; errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl;
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -1805,10 +1829,8 @@ WearBarParams read_wear_bar_params(
auto blend = WearBarParams::BLEND_MODE_CONSTANT; auto blend = WearBarParams::BLEND_MODE_CONSTANT;
lua_getfield(L, stack_idx, "blend"); lua_getfield(L, stack_idx, "blend");
if (check_field_or_nil(L, -1, LUA_TSTRING, "blend")) { if (check_field_or_nil(L, -1, LUA_TSTRING, "blend")) {
int blendInt; if (!string_to_enum(WearBarParams::es_BlendMode, blend, lua_tostring(L, -1)))
if (!string_to_enum(WearBarParams::es_BlendMode, blendInt, std::string(lua_tostring(L, -1))))
throw LuaError("Invalid wear bar color blend mode"); throw LuaError("Invalid wear bar color blend mode");
blend = static_cast<WearBarParams::BlendMode>(blendInt);
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -2395,14 +2417,9 @@ void push_hud_element(lua_State *L, HudElement *elem)
bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value) bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value)
{ {
std::string statstr = lua_tostring(L, 3); std::string statstr = lua_tostring(L, 3);
{ if (!string_to_enum(es_HudElementStat, stat, statstr)) {
int statint; script_log_unique(L, "Unknown HUD stat type: " + statstr, warningstream);
if (!string_to_enum(es_HudElementStat, statint, statstr)) { return false;
script_log_unique(L, "Unknown HUD stat type: " + statstr, warningstream);
return false;
}
stat = static_cast<HudElementStat>(statint);
} }
switch (stat) { switch (stat) {

View file

@ -38,7 +38,6 @@ public:
static struct EnumString es_ContentParamType[]; static struct EnumString es_ContentParamType[];
static struct EnumString es_ContentParamType2[]; static struct EnumString es_ContentParamType2[];
static struct EnumString es_LiquidType[]; static struct EnumString es_LiquidType[];
static struct EnumString es_LiquidMoveType[];
static struct EnumString es_NodeBoxType[]; static struct EnumString es_NodeBoxType[];
static struct EnumString es_TextureAlphaMode[]; static struct EnumString es_TextureAlphaMode[];
}; };