Map meta + lua

This commit is contained in:
jachoo 2012-02-18 11:37:41 +01:00
parent e5d5d597dd
commit fe021900fb
3 changed files with 157 additions and 88 deletions

View File

@ -1901,7 +1901,7 @@ void Map::nodeMetadataStep(float dtime,
ServerMap::ServerMap(std::string savedir, IGameDef *gamedef): ServerMap::ServerMap(std::string savedir, IGameDef *gamedef):
Map(dout_server, gamedef), Map(dout_server, gamedef),
m_seed(0), m_seed(0),
m_map_metadata_changed(true), //m_map_metadata_changed(true),
m_database( new Database(savedir + DIR_DELIM "map.sqlite") ), m_database( new Database(savedir + DIR_DELIM "map.sqlite") ),
m_blocks( m_database->getTable<v3s16,binary_t>("blocks",true) ), m_blocks( m_database->getTable<v3s16,binary_t>("blocks",true) ),
m_map_meta( m_database->getTable<std::string>("map_meta") ), m_map_meta( m_database->getTable<std::string>("map_meta") ),
@ -2609,10 +2609,10 @@ void ServerMap::save(ModifiedState save_level)
infostream<<"ServerMap: Saving whole map, this can take time." infostream<<"ServerMap: Saving whole map, this can take time."
<<std::endl; <<std::endl;
if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN) //if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN)
{ //{
saveMapMeta(); saveMapMeta();
} //}
// Profile modified reasons // Profile modified reasons
Profiler modprofiler; Profiler modprofiler;
@ -2707,8 +2707,8 @@ void ServerMap::saveMapMeta()
bool success = true; bool success = true;
if(m_map_meta.put("seed",m_seed)) success = false; if(m_map_meta.put("seed",m_seed)) success = false;
if(success) m_map_metadata_changed = false; //if(success) m_map_metadata_changed = false;
else infostream<<"ERROR: ServerMap::saveMapMeta() failed"<<std::endl; if(!success) infostream<<"ERROR: ServerMap::saveMapMeta() failed"<<std::endl;
} }
void ServerMap::loadMapMeta() void ServerMap::loadMapMeta()
@ -2752,7 +2752,7 @@ void ServerMap::loadMapMeta()
//failed to load metadata //failed to load metadata
if(m_database->isNew()) if(m_database->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 else
throw FileNotGoodException("Failed to load map metadata"); throw FileNotGoodException("Failed to load map metadata");
} }

View File

@ -382,6 +382,17 @@ public:
void saveMapMeta(); void saveMapMeta();
void loadMapMeta(); void loadMapMeta();
//gets map meta data from DB
template<class Data> Data getMeta(const std::string& name)
{
return m_map_meta.get<Data>(name);
}
//sets map meta data
template<class Data> bool setMeta(const std::string& name, const Data& val)
{
return m_map_meta.put(name,val);
}
/*void saveChunkMeta(); /*void saveChunkMeta();
void loadChunkMeta();*/ void loadChunkMeta();*/
@ -403,11 +414,29 @@ public:
void saveBlock(MapBlock *block); void saveBlock(MapBlock *block);
// This will generate a sector with getSector if not found. // 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); MapBlock* loadBlock(v3s16 p);
// Database version // Database version
void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false); void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
//block metadata
/*template<class Data> Data getBlockMeta(const v3s16& blockpos, const std::string& name)
{
return m_players_meta.get<Data>(blockpos);
}
template<class Data> Data getBlockMeta(const MapBlock& block, const std::string& name)
{
return m_players_meta.get<Data>(block.getPos());
}
template<class Data> bool setBlockMeta(const v3s16& blockpos, const std::string& name, const Data& val)
{
return m_players_meta.put(blockpos,val);
}
template<class Data> bool setBlockMeta(const MapBlock& block, const std::string& name, const Data& val)
{
return m_players_meta.put(block.getPos(),val);
}*/
// For debug printing // For debug printing
virtual void PrintInfo(std::ostream &out); virtual void PrintInfo(std::ostream &out);
@ -434,7 +463,7 @@ private:
Metadata is re-written on disk only if this is true. Metadata is re-written on disk only if this is true.
This is reset to false when written on disk. 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 SQLite database and tables
@ -443,6 +472,7 @@ private:
Table<v3s16,binary_t>& m_blocks; Table<v3s16,binary_t>& m_blocks;
Table<std::string>& m_map_meta; Table<std::string>& m_map_meta;
Table<v2s16,binary_t>& m_sectors_meta; Table<v2s16,binary_t>& m_sectors_meta;
//Table<v3s16,binary_t>& m_blocks_meta;
}; };
/* /*

View File

@ -3492,6 +3492,83 @@ static int l_get_player_privs(lua_State *L)
} }
//according to _Type, executes _Func<TYPE>(_Params) and returns it to lua
#define GET_META(_Type,_Func,_Params) \
if(_Type=="string"){ \
const std::string val = _Func<std::string> _Params; \
lua_pushstring(L,val.c_str()); \
return 1; \
} \
if(_Type=="int"){ \
int val = _Func<int> _Params; \
lua_pushinteger(L,val); \
return 1; \
} \
if(_Type=="double"){ \
double val = _Func<double> _Params; \
lua_pushnumber(L,val); \
return 1; \
} \
if(_Type=="bool"){ \
int val = _Func<int> _Params; \
lua_pushboolean(L,val != 0); \
return 1; \
} \
if(_Type=="v3s16"){ \
v3s16 val = _Func<v3s16> _Params; \
push_v3s16(L,val); \
return 1; \
} \
if(_Type=="v3f"){ \
v3f val = _Func<v3f> _Params; \
push_v3f(L,val); \
return 1; \
} \
if(_Type=="v3fpos"){ \
v3f val = _Func<v3f> _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) // get_player_meta(player_name,meta_name,type)
// types: string, int, double, bool, v3s16, v3f, v3fpos // types: string, int, double, bool, v3s16, v3f, v3fpos
static int l_get_player_meta(lua_State *L) 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()); Player* player = env->getPlayer(player_name.c_str());
if(!player) throw BaseException(""); if(!player) throw BaseException("");
if(type=="string"){ GET_META(type,env->getPlayerMeta,(*player,meta_name))
const std::string val = env->getPlayerMeta<std::string>(*player,meta_name);
lua_pushstring(L,val.c_str());
return 1;
}
if(type=="int"){
int val = env->getPlayerMeta<int>(*player,meta_name);
lua_pushinteger(L,val);
return 1;
}
if(type=="double"){
double val = env->getPlayerMeta<double>(*player,meta_name);
lua_pushnumber(L,val);
return 1;
}
if(type=="bool"){
int val = env->getPlayerMeta<int>(*player,meta_name);
lua_pushboolean(L,val != 0);
return 1;
}
if(type=="v3s16"){
v3s16 val = env->getPlayerMeta<v3s16>(*player,meta_name);
push_v3s16(L,val);
return 1;
}
if(type=="v3f"){
v3f val = env->getPlayerMeta<v3f>(*player,meta_name);
push_v3f(L,val);
return 1;
}
if(type=="v3fpos"){
v3f val = env->getPlayerMeta<v3f>(*player,meta_name);
pushFloatPos(L,val);
return 1;
}
}catch(std::exception&){} }catch(std::exception&){}
@ -3555,7 +3592,6 @@ static int l_get_player_meta(lua_State *L)
return 1; return 1;
} }
// set_player_meta(player_name,meta_name,type,value) // set_player_meta(player_name,meta_name,type,value)
// types: string, int, double, bool, v3s16, v3f, v3fpos // types: string, int, double, bool, v3s16, v3f, v3fpos
static int l_set_player_meta(lua_State *L) 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()); Player* player = env->getPlayer(player_name.c_str());
if(!player) throw BaseException(""); if(!player) throw BaseException("");
if(type=="string"){ SET_META(type,4,env->setPlayerMeta(*player,meta_name,_value))
const std::string val = luaL_checkstring(L, 4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="int"){ }catch(std::exception&){}
const int val = luaL_checkint(L, 4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="double"){ //we shall not be here if no error
const double val = luaL_checknumber(L, 4); return luaL_error(L,"set_player_meta - error occured");
env->setPlayerMeta(*player,meta_name,val); }
return 0;
}
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"){ // get_map_meta(meta_name,type)
v3s16 val = check_v3s16(L,4); // types: string, int, double, bool, v3s16, v3f, v3fpos
env->setPlayerMeta(*player,meta_name,val); static int l_get_map_meta(lua_State *L)
return 0; {
} const std::string meta_name = luaL_checkstring(L, 1);
const std::string type = luaL_checkstring(L, 2);
if(type=="v3f"){ try{
v3f val = check_v3f(L,4);
env->setPlayerMeta(*player,meta_name,val);
return 0;
}
if(type=="v3fpos"){ ServerEnvironment* env = get_env(L);
v3f val = checkFloatPos(L,4); ServerMap& map = env->getServerMap();
env->setPlayerMeta(*player,meta_name,val);
return 0; 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&){} }catch(std::exception&){}
@ -3702,6 +3739,8 @@ static const struct luaL_Reg minetest_f [] = {
{"get_player_privs", l_get_player_privs}, {"get_player_privs", l_get_player_privs},
{"get_player_meta", l_get_player_meta}, {"get_player_meta", l_get_player_meta},
{"set_player_meta", l_set_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_inventory", l_get_inventory},
{"get_digging_properties", l_get_digging_properties}, {"get_digging_properties", l_get_digging_properties},
{"get_hitting_properties", l_get_hitting_properties}, {"get_hitting_properties", l_get_hitting_properties},