From 562ac3bce9fae076562bd2e92e7d330c296ac1b0 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Tue, 28 Feb 2012 19:45:23 +0200 Subject: Digging time groups WIP --- src/scriptapi.cpp | 251 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 161 insertions(+), 90 deletions(-) (limited to 'src/scriptapi.cpp') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 8350c75f1..2ee727b3f 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -45,6 +45,7 @@ extern "C" { #include "mapblock.h" // For getNodeBlockPos #include "content_nodemeta.h" #include "utility.h" +#include "tool.h" static void stackDump(lua_State *L, std::ostream &o) { @@ -416,14 +417,6 @@ struct EnumString es_NodeBoxType[] = {0, NULL}, }; -struct EnumString es_Diggability[] = -{ - {DIGGABLE_NOT, "not"}, - {DIGGABLE_NORMAL, "normal"}, - {DIGGABLE_CONSTANT, "constant"}, - {0, NULL}, -}; - /* C struct <-> Lua table converter functions */ @@ -612,6 +605,7 @@ static core::aabbox3d read_aabbox3df32(lua_State *L, int index, f32 scale) return box; } +#if 0 /* MaterialProperties */ @@ -630,87 +624,156 @@ static MaterialProperties read_material_properties( getfloatfield(L, -1, "flammability", prop.flammability); return prop; } +#endif /* - ToolDiggingProperties + Groups +*/ +static void read_groups(lua_State *L, int index, + std::map &result) +{ + result.clear(); + lua_pushnil(L); + if(index < 0) + index -= 1; + 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; + // removes value, keeps key for next iteration + lua_pop(L, 1); + } +} + +/* + ToolCapabilities */ -static ToolDiggingProperties read_tool_digging_properties( +static ToolCapabilities read_tool_capabilities( lua_State *L, int table) { - ToolDiggingProperties prop; - getfloatfield(L, table, "full_punch_interval", prop.full_punch_interval); - getfloatfield(L, table, "basetime", prop.basetime); - getfloatfield(L, table, "dt_weight", prop.dt_weight); - getfloatfield(L, table, "dt_crackiness", prop.dt_crackiness); - getfloatfield(L, table, "dt_crumbliness", prop.dt_crumbliness); - getfloatfield(L, table, "dt_cuttability", prop.dt_cuttability); - getfloatfield(L, table, "basedurability", prop.basedurability); - getfloatfield(L, table, "dd_weight", prop.dd_weight); - getfloatfield(L, table, "dd_crackiness", prop.dd_crackiness); - getfloatfield(L, table, "dd_crumbliness", prop.dd_crumbliness); - getfloatfield(L, table, "dd_cuttability", prop.dd_cuttability); - return prop; + ToolCapabilities toolcap; + getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval); + getintfield(L, table, "max_drop_level", toolcap.max_drop_level); + lua_getfield(L, table, "groupcaps"); + if(lua_istable(L, -1)){ + int table_groupcaps = lua_gettop(L); + lua_pushnil(L); + while(lua_next(L, table_groupcaps) != 0){ + // key at index -2 and value at index -1 + std::string groupname = luaL_checkstring(L, -2); + if(lua_istable(L, -1)){ + int table_groupcap = lua_gettop(L); + // This will be created + ToolGroupCap groupcap; + // Read simple parameters + getfloatfield(L, table_groupcap, "maxwear", groupcap.maxwear); + getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel); + // Read "times" table + lua_getfield(L, table_groupcap, "times"); + if(lua_istable(L, -1)){ + int table_times = lua_gettop(L); + lua_pushnil(L); + while(lua_next(L, table_times) != 0){ + // key at index -2 and value at index -1 + int rating = luaL_checkinteger(L, -2); + float time = luaL_checknumber(L, -1); + groupcap.times[rating] = time; + // removes value, keeps key for next iteration + lua_pop(L, 1); + } + } + lua_pop(L, 1); + // Insert groupcap into toolcap + toolcap.groupcaps[groupname] = groupcap; + } + // removes value, keeps key for next iteration + lua_pop(L, 1); + } + } + lua_pop(L, 1); + return toolcap; } -static void set_tool_digging_properties(lua_State *L, int table, - const ToolDiggingProperties &prop) +static void set_tool_capabilities(lua_State *L, int table, + const ToolCapabilities &toolcap) { - setfloatfield(L, table, "full_punch_interval", prop.full_punch_interval); - setfloatfield(L, table, "basetime", prop.basetime); - setfloatfield(L, table, "dt_weight", prop.dt_weight); - setfloatfield(L, table, "dt_crackiness", prop.dt_crackiness); - setfloatfield(L, table, "dt_crumbliness", prop.dt_crumbliness); - setfloatfield(L, table, "dt_cuttability", prop.dt_cuttability); - setfloatfield(L, table, "basedurability", prop.basedurability); - setfloatfield(L, table, "dd_weight", prop.dd_weight); - setfloatfield(L, table, "dd_crackiness", prop.dd_crackiness); - setfloatfield(L, table, "dd_crumbliness", prop.dd_crumbliness); - setfloatfield(L, table, "dd_cuttability", prop.dd_cuttability); + setfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval); + setintfield(L, table, "max_drop_level", toolcap.max_drop_level); + // Create groupcaps table + lua_newtable(L); + // For each groupcap + for(std::map::const_iterator + i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){ + // Create groupcap table + lua_newtable(L); + const std::string &name = i->first; + const ToolGroupCap &groupcap = i->second; + // Create subtable "times" + lua_newtable(L); + for(std::map::const_iterator + i = groupcap.times.begin(); i != groupcap.times.end(); i++){ + int rating = i->first; + float time = i->second; + lua_pushinteger(L, rating); + lua_pushnumber(L, time); + lua_settable(L, -3); + } + // Set subtable "times" + lua_setfield(L, -2, "times"); + // Set simple parameters + setfloatfield(L, -1, "maxwear", groupcap.maxwear); + setintfield(L, -1, "maxlevel", groupcap.maxlevel); + // Insert groupcap table into groupcaps table + lua_setfield(L, -2, name.c_str()); + } + // Set groupcaps table + lua_setfield(L, -2, "groupcaps"); } -static void push_tool_digging_properties(lua_State *L, - const ToolDiggingProperties &prop) +static void push_tool_capabilities(lua_State *L, + const ToolCapabilities &prop) { lua_newtable(L); - set_tool_digging_properties(L, -1, prop); + set_tool_capabilities(L, -1, prop); } /* - DiggingProperties + DigParams */ -static void set_digging_properties(lua_State *L, int table, - const DiggingProperties &prop) +static void set_dig_params(lua_State *L, int table, + const DigParams ¶ms) { - setboolfield(L, table, "diggable", prop.diggable); - setfloatfield(L, table, "time", prop.time); - setintfield(L, table, "wear", prop.wear); + setboolfield(L, table, "diggable", params.diggable); + setfloatfield(L, table, "time", params.time); + setintfield(L, table, "wear", params.wear); } -static void push_digging_properties(lua_State *L, - const DiggingProperties &prop) +static void push_dig_params(lua_State *L, + const DigParams ¶ms) { lua_newtable(L); - set_digging_properties(L, -1, prop); + set_dig_params(L, -1, params); } /* - HittingProperties + HitParams */ -static void set_hitting_properties(lua_State *L, int table, - const HittingProperties &prop) +static void set_hit_params(lua_State *L, int table, + const HitParams ¶ms) { - setintfield(L, table, "hp", prop.hp); - setintfield(L, table, "wear", prop.wear); + setintfield(L, table, "hp", params.hp); + setintfield(L, table, "wear", params.wear); } -static void push_hitting_properties(lua_State *L, - const HittingProperties &prop) +static void push_hit_params(lua_State *L, + const HitParams ¶ms) { lua_newtable(L); - set_hitting_properties(L, -1, prop); + set_hit_params(L, -1, params); } /* @@ -778,20 +841,26 @@ static ItemDefinition read_item_definition(lua_State *L, int index) getboolfield(L, index, "liquids_pointable", def.liquids_pointable); - lua_getfield(L, index, "tool_digging_properties"); + 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_digging_properties = new ToolDiggingProperties( - read_tool_digging_properties(L, -1)); + def.tool_capabilities = new ToolCapabilities( + read_tool_capabilities(L, -1)); } - lua_pop(L, 1); - // If name is "" (hand), ensure there are ToolDiggingProperties + // If name is "" (hand), ensure there are ToolCapabilities // because it will be looked up there whenever any other item has - // no ToolDiggingProperties - if(def.name == "" && def.tool_digging_properties == NULL){ - def.tool_digging_properties = new ToolDiggingProperties(); + // no ToolCapabilities + if(def.name == "" && def.tool_capabilities == NULL){ + def.tool_capabilities = new ToolCapabilities(); } + lua_getfield(L, index, "groups"); + read_groups(L, -1, def.groups); + lua_pop(L, 1); + return def; } @@ -805,8 +874,14 @@ static ContentFeatures read_content_features(lua_State *L, int index) index = lua_gettop(L) + 1 + index; ContentFeatures f; + /* Name */ getstringfield(L, index, "name", f.name); + /* Groups */ + lua_getfield(L, index, "groups"); + read_groups(L, -1, f.groups); + lua_pop(L, 1); + /* Visual definition */ f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype", es_DrawType, @@ -958,12 +1033,6 @@ static ContentFeatures read_content_features(lua_State *L, int index) } lua_pop(L, 1); - lua_getfield(L, index, "material"); - if(lua_istable(L, -1)){ - f.material = read_material_properties(L, -1); - } - lua_pop(L, 1); - // Set to true if paramtype used to be 'facedir_simple' getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple); // Set to true if wall_mounted used to be set to true @@ -1215,16 +1284,16 @@ private: return 1; } - // get_tool_digging_properties(self) -> table + // get_tool_capabilities(self) -> table // Returns the effective tool digging properties. // Returns those of the hand ("") if this item has none associated. - static int l_get_tool_digging_properties(lua_State *L) + static int l_get_tool_capabilities(lua_State *L) { LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - const ToolDiggingProperties &prop = - item.getToolDiggingProperties(get_server(L)->idef()); - push_tool_digging_properties(L, prop); + const ToolCapabilities &prop = + item.getToolCapabilities(get_server(L)->idef()); + push_tool_capabilities(L, prop); return 1; } @@ -1386,7 +1455,7 @@ const luaL_reg LuaItemStack::methods[] = { method(LuaItemStack, get_free_space), method(LuaItemStack, is_known), method(LuaItemStack, get_definition), - method(LuaItemStack, get_tool_digging_properties), + method(LuaItemStack, get_tool_capabilities), method(LuaItemStack, add_wear), method(LuaItemStack, add_item), method(LuaItemStack, item_fits), @@ -3513,28 +3582,30 @@ static int l_get_inventory(lua_State *L) return 1; } -// get_digging_properties(material_properties, tool_digging_properties[, time_from_last_punch]) -static int l_get_digging_properties(lua_State *L) +// get_dig_params(groups, tool_capabilities[, time_from_last_punch]) +static int l_get_dig_params(lua_State *L) { - MaterialProperties mp = read_material_properties(L, 1); - ToolDiggingProperties tp = read_tool_digging_properties(L, 2); + std::map groups; + read_groups(L, 1, groups); + ToolCapabilities tp = read_tool_capabilities(L, 2); if(lua_isnoneornil(L, 3)) - push_digging_properties(L, getDiggingProperties(&mp, &tp)); + push_dig_params(L, getDigParams(groups, &tp)); else - push_digging_properties(L, getDiggingProperties(&mp, &tp, + push_dig_params(L, getDigParams(groups, &tp, luaL_checknumber(L, 3))); return 1; } -// get_hitting_properties(material_properties, tool_digging_properties[, time_from_last_punch]) -static int l_get_hitting_properties(lua_State *L) +// get_hit_params(groups, tool_capabilities[, time_from_last_punch]) +static int l_get_hit_params(lua_State *L) { - MaterialProperties mp = read_material_properties(L, 1); - ToolDiggingProperties tp = read_tool_digging_properties(L, 2); + std::map groups; + read_groups(L, 1, groups); + ToolCapabilities tp = read_tool_capabilities(L, 2); if(lua_isnoneornil(L, 3)) - push_hitting_properties(L, getHittingProperties(&mp, &tp)); + push_hit_params(L, getHitParams(groups, &tp)); else - push_hitting_properties(L, getHittingProperties(&mp, &tp, + push_hit_params(L, getHitParams(groups, &tp, luaL_checknumber(L, 3))); return 1; } @@ -3580,8 +3651,8 @@ static const struct luaL_Reg minetest_f [] = { {"chat_send_player", l_chat_send_player}, {"get_player_privs", l_get_player_privs}, {"get_inventory", l_get_inventory}, - {"get_digging_properties", l_get_digging_properties}, - {"get_hitting_properties", l_get_hitting_properties}, + {"get_dig_params", l_get_dig_params}, + {"get_hit_params", l_get_hit_params}, {"get_current_modname", l_get_current_modname}, {"get_modpath", l_get_modpath}, {"get_worldpath", l_get_worldpath}, -- cgit v1.2.3