From bdbdeab0053d9ebbaffea17effeba777b710d390 Mon Sep 17 00:00:00 2001 From: sapier Date: Sat, 23 Feb 2013 18:06:57 +0000 Subject: split scriptapi.cpp Remerge some files in order to reduce number of additional files Make necessary changes for split, rename files, reorganize some bits --- src/scriptapi_common.cpp | 287 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 src/scriptapi_common.cpp (limited to 'src/scriptapi_common.cpp') diff --git a/src/scriptapi_common.cpp b/src/scriptapi_common.cpp new file mode 100644 index 000000000..8ee8d6a84 --- /dev/null +++ b/src/scriptapi_common.cpp @@ -0,0 +1,287 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "scriptapi.h" +#include "scriptapi_common.h" + +extern "C" { +#include "lauxlib.h" +} + +#include "script.h" +#include "scriptapi_types.h" +#include "scriptapi_object.h" + + +Server* get_server(lua_State *L) +{ + // Get server from registry + lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server"); + Server *server = (Server*)lua_touserdata(L, -1); + lua_pop(L, 1); + return server; +} + +ServerEnvironment* get_env(lua_State *L) +{ + // Get environment from registry + lua_getfield(L, LUA_REGISTRYINDEX, "minetest_env"); + ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1); + lua_pop(L, 1); + return env; +} + +void warn_if_field_exists(lua_State *L, int table, + const char *fieldname, const std::string &message) +{ + lua_getfield(L, table, fieldname); + if(!lua_isnil(L, -1)){ + infostream<::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 + setintfield(L, -1, "maxlevel", groupcap.maxlevel); + setintfield(L, -1, "uses", groupcap.uses); + // Insert groupcap table into groupcaps table + lua_setfield(L, -2, name.c_str()); + } + // Set groupcaps table + lua_setfield(L, -2, "groupcaps"); +} + +void push_tool_capabilities(lua_State *L, + const ToolCapabilities &prop) +{ + lua_newtable(L); + set_tool_capabilities(L, -1, prop); +} + +void realitycheck(lua_State *L) +{ + int top = lua_gettop(L); + if(top >= 30){ + dstream<<"Stack is over 30:"<str){ + if(str == std::string(esp->str)){ + result = esp->num; + return true; + } + esp++; + } + return false; +} + +/*bool enum_to_string(const EnumString *spec, std::string &result, + int num) +{ + const EnumString *esp = spec; + while(esp){ + if(num == esp->num){ + result = esp->str; + return true; + } + esp++; + } + return false; +}*/ + +int getenumfield(lua_State *L, int table, + const char *fieldname, const EnumString *spec, int default_) +{ + int result = default_; + string_to_enum(spec, result, + getstringfield_default(L, table, fieldname, "")); + return result; +} -- cgit v1.2.3 From 7d9329ecfe84733cdefa34eab25ee3d124c94c59 Mon Sep 17 00:00:00 2001 From: PilzAdam Date: Thu, 28 Mar 2013 21:40:44 +0100 Subject: New damage system, add damageGroups to ToolCapabilities, bump protocol version --- src/scriptapi_common.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/scriptapi_common.cpp') diff --git a/src/scriptapi_common.cpp b/src/scriptapi_common.cpp index 8ee8d6a84..2d6f6c72b 100644 --- a/src/scriptapi_common.cpp +++ b/src/scriptapi_common.cpp @@ -117,6 +117,20 @@ ToolCapabilities read_tool_capabilities( } } lua_pop(L, 1); + lua_getfield(L, table, "damage_groups"); + if(lua_istable(L, -1)){ + int table_damage_groups = lua_gettop(L); + lua_pushnil(L); + while(lua_next(L, table_damage_groups) != 0){ + // key at index -2 and value at index -1 + std::string groupname = luaL_checkstring(L, -2); + u16 value = luaL_checkinteger(L, -1); + toolcap.damageGroups[groupname] = value; + // removes value, keeps key for next iteration + lua_pop(L, 1); + } + } + lua_pop(L, 1); return toolcap; } @@ -154,6 +168,16 @@ void set_tool_capabilities(lua_State *L, int table, } // Set groupcaps table lua_setfield(L, -2, "groupcaps"); + //Create damage_groups table + lua_newtable(L); + // For each damage group + for(std::map::const_iterator + i = toolcap.damageGroups.begin(); i != toolcap.damageGroups.end(); i++){ + // Create damage group table + lua_pushinteger(L, i->second); + lua_setfield(L, -2, i->first.c_str()); + } + lua_setfield(L, -2, "damage_groups"); } void push_tool_capabilities(lua_State *L, -- cgit v1.2.3