diff --git a/src/map.cpp b/src/map.cpp index c00dfef..8e5a7fa 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1901,7 +1901,7 @@ void Map::nodeMetadataStep(float dtime, ServerMap::ServerMap(std::string savedir, IGameDef *gamedef): Map(dout_server, gamedef), m_seed(0), - m_map_metadata_changed(true), + //m_map_metadata_changed(true), m_database( new Database(savedir + DIR_DELIM "map.sqlite") ), m_blocks( m_database->getTable("blocks",true) ), m_map_meta( m_database->getTable("map_meta") ), @@ -2609,10 +2609,10 @@ void ServerMap::save(ModifiedState save_level) infostream<<"ServerMap: Saving whole map, this can take time." <isNew()) - m_map_metadata_changed = true; //if it's a new db, only set this + ;//m_map_metadata_changed = true; //if it's a new db, only set this else throw FileNotGoodException("Failed to load map metadata"); } diff --git a/src/map.h b/src/map.h index 7f56829..978344b 100644 --- a/src/map.h +++ b/src/map.h @@ -381,7 +381,18 @@ public: // Saves map seed and possibly other stuff void saveMapMeta(); void loadMapMeta(); - + + //gets map meta data from DB + template Data getMeta(const std::string& name) + { + return m_map_meta.get(name); + } + //sets map meta data + template bool setMeta(const std::string& name, const Data& val) + { + return m_map_meta.put(name,val); + } + /*void saveChunkMeta(); void loadChunkMeta();*/ @@ -403,11 +414,29 @@ public: void saveBlock(MapBlock *block); // This will generate a sector with getSector if not found. - void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false); + //void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false); MapBlock* loadBlock(v3s16 p); // Database version void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false); + //block metadata + /*template Data getBlockMeta(const v3s16& blockpos, const std::string& name) + { + return m_players_meta.get(blockpos); + } + template Data getBlockMeta(const MapBlock& block, const std::string& name) + { + return m_players_meta.get(block.getPos()); + } + template bool setBlockMeta(const v3s16& blockpos, const std::string& name, const Data& val) + { + return m_players_meta.put(blockpos,val); + } + template bool setBlockMeta(const MapBlock& block, const std::string& name, const Data& val) + { + return m_players_meta.put(block.getPos(),val); + }*/ + // For debug printing virtual void PrintInfo(std::ostream &out); @@ -434,7 +463,7 @@ private: Metadata is re-written on disk only if this is true. This is reset to false when written on disk. */ - bool m_map_metadata_changed; + //bool m_map_metadata_changed; - not need for this now /* SQLite database and tables @@ -443,6 +472,7 @@ private: Table& m_blocks; Table& m_map_meta; Table& m_sectors_meta; + //Table& m_blocks_meta; }; /* diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 2fb49e6..174fc4d 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -3492,6 +3492,83 @@ static int l_get_player_privs(lua_State *L) } +//according to _Type, executes _Func(_Params) and returns it to lua +#define GET_META(_Type,_Func,_Params) \ + if(_Type=="string"){ \ + const std::string val = _Func _Params; \ + lua_pushstring(L,val.c_str()); \ + return 1; \ + } \ + if(_Type=="int"){ \ + int val = _Func _Params; \ + lua_pushinteger(L,val); \ + return 1; \ + } \ + if(_Type=="double"){ \ + double val = _Func _Params; \ + lua_pushnumber(L,val); \ + return 1; \ + } \ + if(_Type=="bool"){ \ + int val = _Func _Params; \ + lua_pushboolean(L,val != 0); \ + return 1; \ + } \ + if(_Type=="v3s16"){ \ + v3s16 val = _Func _Params; \ + push_v3s16(L,val); \ + return 1; \ + } \ + if(_Type=="v3f"){ \ + v3f val = _Func _Params; \ + push_v3f(L,val); \ + return 1; \ + } \ + if(_Type=="v3fpos"){ \ + v3f val = _Func _Params; \ + pushFloatPos(L,val); \ + return 1; \ + } + +//according to _Type, gets lua param from _ValPos and executes _Func. Value read from lua is _value +#define SET_META(_Type,_ValPos,_Func) \ + if(_Type=="string"){ \ + const std::string _value = luaL_checkstring(L, _ValPos); \ + _Func; \ + return 0; \ + } \ + if(_Type=="int"){ \ + const int _value = luaL_checkint(L, _ValPos); \ + _Func; \ + return 0; \ + } \ + if(_Type=="double"){ \ + const double _value = luaL_checknumber(L, _ValPos); \ + _Func; \ + return 0; \ + } \ + if(_Type=="bool"){ \ + if(!lua_isboolean(L,_ValPos)) return luaL_error(L,"bool expected"); \ + const int _value = lua_toboolean(L, _ValPos); \ + _Func; \ + return 0; \ + } \ + if(_Type=="v3s16"){ \ + v3s16 _value = check_v3s16(L,_ValPos); \ + _Func; \ + return 0; \ + } \ + if(_Type=="v3f"){ \ + v3f _value = check_v3f(L,_ValPos); \ + _Func; \ + return 0; \ + } \ + if(_Type=="v3fpos"){ \ + v3f _value = checkFloatPos(L,_ValPos); \ + _Func; \ + return 0; \ + } + // get_player_meta(player_name,meta_name,type) // types: string, int, double, bool, v3s16, v3f, v3fpos static int l_get_player_meta(lua_State *L) @@ -3506,47 +3583,7 @@ static int l_get_player_meta(lua_State *L) Player* player = env->getPlayer(player_name.c_str()); if(!player) throw BaseException(""); - if(type=="string"){ - const std::string val = env->getPlayerMeta(*player,meta_name); - lua_pushstring(L,val.c_str()); - return 1; - } - - if(type=="int"){ - int val = env->getPlayerMeta(*player,meta_name); - lua_pushinteger(L,val); - return 1; - } - - if(type=="double"){ - double val = env->getPlayerMeta(*player,meta_name); - lua_pushnumber(L,val); - return 1; - } - - if(type=="bool"){ - int val = env->getPlayerMeta(*player,meta_name); - lua_pushboolean(L,val != 0); - return 1; - } - - if(type=="v3s16"){ - v3s16 val = env->getPlayerMeta(*player,meta_name); - push_v3s16(L,val); - return 1; - } - - if(type=="v3f"){ - v3f val = env->getPlayerMeta(*player,meta_name); - push_v3f(L,val); - return 1; - } - - if(type=="v3fpos"){ - v3f val = env->getPlayerMeta(*player,meta_name); - pushFloatPos(L,val); - return 1; - } + GET_META(type,env->getPlayerMeta,(*player,meta_name)) }catch(std::exception&){} @@ -3555,7 +3592,6 @@ static int l_get_player_meta(lua_State *L) return 1; } - // set_player_meta(player_name,meta_name,type,value) // types: string, int, double, bool, v3s16, v3f, v3fpos static int l_set_player_meta(lua_State *L) @@ -3570,48 +3606,49 @@ static int l_set_player_meta(lua_State *L) Player* player = env->getPlayer(player_name.c_str()); if(!player) throw BaseException(""); - if(type=="string"){ - const std::string val = luaL_checkstring(L, 4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } + SET_META(type,4,env->setPlayerMeta(*player,meta_name,_value)) - if(type=="int"){ - const int val = luaL_checkint(L, 4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } + }catch(std::exception&){} - if(type=="double"){ - const double val = luaL_checknumber(L, 4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } + //we shall not be here if no error + return luaL_error(L,"set_player_meta - error occured"); +} - if(type=="bool"){ - if(!lua_isboolean(L,4)) return luaL_error(L,"bool expected"); - const int val = lua_toboolean(L, 4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } - if(type=="v3s16"){ - v3s16 val = check_v3s16(L,4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } +// get_map_meta(meta_name,type) +// types: string, int, double, bool, v3s16, v3f, v3fpos +static int l_get_map_meta(lua_State *L) +{ + const std::string meta_name = luaL_checkstring(L, 1); + const std::string type = luaL_checkstring(L, 2); - if(type=="v3f"){ - v3f val = check_v3f(L,4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } + try{ - if(type=="v3fpos"){ - v3f val = checkFloatPos(L,4); - env->setPlayerMeta(*player,meta_name,val); - return 0; - } + ServerEnvironment* env = get_env(L); + ServerMap& map = env->getServerMap(); + + GET_META(type,map.getMeta,(meta_name)) + + }catch(std::exception&){} + + //we shall not be here if no error + lua_pushnil(L); + return 1; +} + +// set_map_meta(meta_name,type,value) +// types: string, int, double, bool, v3s16, v3f, v3fpos +static int l_set_map_meta(lua_State *L) +{ + const std::string meta_name = luaL_checkstring(L, 1); + const std::string type = luaL_checkstring(L, 2); + + try{ + + ServerEnvironment* env = get_env(L); + ServerMap& map = env->getServerMap(); + + SET_META(type,3,map.setMeta(meta_name,_value)) }catch(std::exception&){} @@ -3702,6 +3739,8 @@ static const struct luaL_Reg minetest_f [] = { {"get_player_privs", l_get_player_privs}, {"get_player_meta", l_get_player_meta}, {"set_player_meta", l_set_player_meta}, + {"get_map_meta", l_get_map_meta}, + {"set_map_meta", l_set_map_meta}, {"get_inventory", l_get_inventory}, {"get_digging_properties", l_get_digging_properties}, {"get_hitting_properties", l_get_hitting_properties},