NodeResolver: Remove NodeResolveMethod

This simplifies NodeResolver logic and makes some interfaces cleaner.
This commit is contained in:
kwolekr 2015-05-07 02:34:15 -04:00
parent d720fd5644
commit 656575b59d
9 changed files with 103 additions and 169 deletions

@ -2180,15 +2180,6 @@ These functions return the leftover itemstack.
* `options` is a table containing the following optional parameters:
* If `use_comments` is true and `format` is "lua", the Lua code generated will have (X, Z)
* position comments for every X row generated in the schematic data for easier reading.
* If `register_after_load` is true, then `schematic`, if not yet loaded, will be registered
* after loading and persist in memory.
* node_resolve_method can be one of either "none", "direct", or "deferred" (default: "none")
* This sets the way method by with node names are mapped to their content IDs, if loaded:
* "none" performs no node resolution and preserves all node names from the schematic definition
* "direct" performs an immediate lookup of content ID, given all the nodes that have been
* registered up to this point in script execution
* "deferred" pends node resolution until after the script registration phase has ended
* In practice, it is recommended to use "none" in nearly all use cases.
### Misc.
* `minetest.get_connected_players()`: returns list of `ObjectRefs`

@ -57,7 +57,7 @@ BiomeManager::BiomeManager(IGameDef *gamedef) :
b->m_nodenames.push_back("mapgen_water_source");
b->m_nodenames.push_back("mapgen_river_water_source");
b->m_nodenames.push_back("air");
m_ndef->pendNodeResolve(b, NODE_RESOLVE_DEFERRED);
m_ndef->pendNodeResolve(b);
add(b);
}

@ -340,7 +340,7 @@ size_t DecoSchematic::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p)
bool force_placement = (flags & DECO_FORCE_PLACEMENT);
schematic->blitToVManip(p, vm, rot, force_placement, m_ndef);
schematic->blitToVManip(p, vm, rot, force_placement);
return 1;
}

@ -94,9 +94,11 @@ void Schematic::resolveNodeNames()
}
void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot,
bool force_placement, INodeDefManager *ndef)
void Schematic::blitToVManip(v3s16 p, MMVManip *vm,
Rotation rot, bool force_placement)
{
sanity_check(m_ndef != NULL);
int xstride = 1;
int ystride = size.X;
int zstride = size.X * size.Y;
@ -163,7 +165,7 @@ void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot,
vm->m_data[vi].param1 = 0;
if (rot)
vm->m_data[vi].rotateAlongYAxis(ndef, rot);
vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
}
}
y_map++;
@ -171,10 +173,12 @@ void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot,
}
void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
bool force_placement, INodeDefManager *ndef)
void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
Rotation rot, bool force_placement)
{
assert(schemdata != NULL); // Pre-condition
sanity_check(m_ndef != NULL);
MMVManip *vm = new MMVManip(map);
if (rot == ROTATE_RAND)
@ -194,7 +198,7 @@ void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
vm->initialEmerge(bp1, bp2);
blitToVManip(p, vm, rot, force_placement, ndef);
blitToVManip(p, vm, rot, force_placement);
std::map<v3s16, MapBlock *> lighting_modified_blocks;
std::map<v3s16, MapBlock *> modified_blocks;
@ -215,7 +219,8 @@ void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
}
bool Schematic::deserializeFromMts(std::istream *is, std::vector<std::string> *names)
bool Schematic::deserializeFromMts(std::istream *is,
std::vector<std::string> *names)
{
std::istream &ss = *is;
content_t cignore = CONTENT_IGNORE;
@ -279,7 +284,8 @@ bool Schematic::deserializeFromMts(std::istream *is, std::vector<std::string> *n
}
bool Schematic::serializeToMts(std::ostream *os)
bool Schematic::serializeToMts(std::ostream *os,
const std::vector<std::string> &names)
{
std::ostream &ss = *os;
@ -290,24 +296,20 @@ bool Schematic::serializeToMts(std::ostream *os)
for (int y = 0; y != size.Y; y++) // Y slice probabilities
writeU8(ss, slice_probs[y]);
std::vector<content_t> usednodes;
int nodecount = size.X * size.Y * size.Z;
build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
u16 numids = usednodes.size();
writeU16(ss, numids); // name count
for (int i = 0; i != numids; i++)
ss << serializeString(getNodeName(usednodes[i])); // node names
writeU16(ss, names.size()); // name count
for (size_t i = 0; i != names.size(); i++)
ss << serializeString(names[i]); // node names
// compressed bulk node data
MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
schemdata, nodecount, 2, 2, true);
schemdata, size.X * size.Y * size.Z, 2, 2, true);
return true;
}
bool Schematic::serializeToLua(std::ostream *os, bool use_comments)
bool Schematic::serializeToLua(std::ostream *os,
const std::vector<std::string> &names, bool use_comments)
{
std::ostream &ss = *os;
@ -350,7 +352,7 @@ bool Schematic::serializeToLua(std::ostream *os, bool use_comments)
for (u16 x = 0; x != size.X; x++, i++) {
ss << "\t\t{"
<< "name=\"" << getNodeName(schemdata[i].getContent())
<< "name=\"" << names[schemdata[i].getContent()]
<< "\", param1=" << (u16)schemdata[i].param1
<< ", param2=" << (u16)schemdata[i].param2
<< "}," << std::endl;
@ -367,8 +369,7 @@ bool Schematic::serializeToLua(std::ostream *os, bool use_comments)
bool Schematic::loadSchematicFromFile(const std::string &filename,
INodeDefManager *ndef, StringMap *replace_names,
NodeResolveMethod resolve_method)
INodeDefManager *ndef, StringMap *replace_names)
{
std::ifstream is(filename.c_str(), std::ios_base::binary);
if (!is.good()) {
@ -392,7 +393,8 @@ bool Schematic::loadSchematicFromFile(const std::string &filename,
m_nnlistsizes.push_back(m_nodenames.size() - origsize);
ndef->pendNodeResolve(this, resolve_method);
if (ndef)
ndef->pendNodeResolve(this);
return true;
}
@ -400,8 +402,33 @@ bool Schematic::loadSchematicFromFile(const std::string &filename,
bool Schematic::saveSchematicToFile(const std::string &filename)
{
MapNode *orig_schemdata = schemdata;
std::vector<std::string> ndef_nodenames;
std::vector<std::string> *names;
// Only carry out the modification if we know the nodes
// were resolved at this point
if (m_resolve_done) {
names = &ndef_nodenames;
u32 volume = size.X * size.Y * size.Z;
schemdata = new MapNode[volume];
for (u32 i = 0; i != volume; i++)
schemdata[i] = orig_schemdata[i];
generate_nodelist_and_update_ids(schemdata, volume, names, m_ndef);
} else { // otherwise, use the names we have on hand in the list
names = &m_nodenames;
}
std::ostringstream os(std::ios_base::binary);
serializeToMts(&os);
serializeToMts(&os, *names);
if (m_resolve_done) {
delete []schemdata;
schemdata = orig_schemdata;
}
return fs::safeWriteToFile(filename, os.str());
}
@ -461,13 +488,13 @@ void Schematic::applyProbabilities(v3s16 p0,
}
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
std::vector<content_t> *usednodes)
void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
std::vector<std::string> *usednodes, INodeDefManager *ndef)
{
std::map<content_t, content_t> nodeidmap;
content_t numids = 0;
for (u32 i = 0; i != nodecount; i++) {
for (size_t i = 0; i != nodecount; i++) {
content_t id;
content_t c = nodes[i].getContent();
@ -476,7 +503,7 @@ void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
id = numids;
numids++;
usednodes->push_back(c);
usednodes->push_back(ndef->get(c).name);
nodeidmap.insert(std::make_pair(c, id));
} else {
id = it->second;

@ -100,20 +100,20 @@ public:
void updateContentIds();
void blitToVManip(v3s16 p, MMVManip *vm,
Rotation rot, bool force_placement, INodeDefManager *ndef);
Rotation rot, bool force_placement);
bool loadSchematicFromFile(const std::string &filename, INodeDefManager *ndef,
StringMap *replace_names, NodeResolveMethod resolve_method);
StringMap *replace_names);
bool saveSchematicToFile(const std::string &filename);
bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
bool deserializeFromMts(std::istream *is, std::vector<std::string> *names_out);
bool serializeToMts(std::ostream *os);
bool serializeToLua(std::ostream *os, bool use_comments);
bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
bool serializeToMts(std::ostream *os, const std::vector<std::string> &names);
bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
bool use_comments);
void placeStructure(Map *map, v3s16 p, u32 flags,
Rotation rot, bool force_placement, INodeDefManager *nef);
Rotation rot, bool force_placement);
void applyProbabilities(v3s16 p0,
std::vector<std::pair<v3s16, u8> > *plist,
std::vector<std::pair<s16, u8> > *splist);
@ -140,8 +140,7 @@ private:
IGameDef *m_gamedef;
};
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
std::vector<content_t> *usednodes);
void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
std::vector<std::string> *usednodes, INodeDefManager *ndef);
#endif

@ -406,7 +406,7 @@ public:
inline virtual bool getNodeRegistrationStatus() const;
inline virtual void setNodeRegistrationStatus(bool completed);
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how);
virtual void pendNodeResolve(NodeResolver *nr);
virtual bool cancelNodeResolveCallback(NodeResolver *nr);
virtual void runNodeResolveCallbacks();
virtual void resetNodeResolveState();
@ -1294,23 +1294,13 @@ inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
}
void CNodeDefManager::pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)
void CNodeDefManager::pendNodeResolve(NodeResolver *nr)
{
nr->m_ndef = this;
switch (how) {
case NODE_RESOLVE_NONE:
break;
case NODE_RESOLVE_DIRECT:
if (m_node_registration_complete)
nr->nodeResolveInternal();
break;
case NODE_RESOLVE_DEFERRED:
if (m_node_registration_complete)
nr->nodeResolveInternal();
else
m_pending_resolve_callbacks.push_back(nr);
break;
}
else
m_pending_resolve_callbacks.push_back(nr);
}
@ -1385,19 +1375,6 @@ void NodeResolver::nodeResolveInternal()
}
const std::string &NodeResolver::getNodeName(content_t c) const
{
if (c < m_nodenames.size())
return m_nodenames[c];
if (m_ndef)
return m_ndef->get(c).name;
static const std::string unknown_str("unknown");
return unknown_str;
}
bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
const std::string &node_alt, content_t c_fallback)
{

@ -282,12 +282,6 @@ struct ContentFeatures
}
};
enum NodeResolveMethod {
NODE_RESOLVE_NONE,
NODE_RESOLVE_DIRECT,
NODE_RESOLVE_DEFERRED,
};
class INodeDefManager {
public:
INodeDefManager(){}
@ -306,7 +300,7 @@ public:
virtual bool getNodeRegistrationStatus() const=0;
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0;
virtual void pendNodeResolve(NodeResolver *nr)=0;
virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
};
@ -353,7 +347,7 @@ public:
virtual bool getNodeRegistrationStatus() const=0;
virtual void setNodeRegistrationStatus(bool completed)=0;
virtual void pendNodeResolve(NodeResolver *nr, NodeResolveMethod how)=0;
virtual void pendNodeResolve(NodeResolver *nr)=0;
virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
virtual void runNodeResolveCallbacks()=0;
virtual void resetNodeResolveState()=0;
@ -371,7 +365,6 @@ public:
const std::string &node_alt, content_t c_fallback);
bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
const std::string &getNodeName(content_t c) const;
void nodeResolveInternal();

@ -92,14 +92,6 @@ struct EnumString ModApiMapgen::es_SchematicFormatType[] =
{0, NULL},
};
struct EnumString ModApiMapgen::es_NodeResolveMethod[] =
{
{NODE_RESOLVE_NONE, "none"},
{NODE_RESOLVE_DIRECT, "direct"},
{NODE_RESOLVE_DEFERRED, "deferred"},
{0, NULL},
};
ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
Biome *get_or_load_biome(lua_State *L, int index,
@ -109,14 +101,11 @@ size_t get_biome_list(lua_State *L, int index,
BiomeManager *biomemgr, std::set<u8> *biome_id_list);
Schematic *get_or_load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names,
bool register_on_load=true,
NodeResolveMethod resolve_method=NODE_RESOLVE_DEFERRED);
SchematicManager *schemmgr, StringMap *replace_names);
Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
StringMap *replace_names, NodeResolveMethod resolve_method);
StringMap *replace_names);
Schematic *load_schematic_from_def(lua_State *L, int index,
INodeDefManager *ndef, StringMap *replace_names,
NodeResolveMethod resolve_method);
INodeDefManager *ndef, StringMap *replace_names);
bool read_schematic_def(lua_State *L, int index,
Schematic *schem, std::vector<std::string> *names);
@ -145,9 +134,7 @@ ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
///////////////////////////////////////////////////////////////////////////////
Schematic *get_or_load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names,
bool register_on_load,
NodeResolveMethod resolve_method)
SchematicManager *schemmgr, StringMap *replace_names)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
@ -157,13 +144,10 @@ Schematic *get_or_load_schematic(lua_State *L, int index,
return schem;
schem = load_schematic(L, index, schemmgr->getNodeDef(),
replace_names, resolve_method);
replace_names);
if (!schem)
return NULL;
if (!register_on_load)
return schem;
if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
delete schem;
return NULL;
@ -174,7 +158,7 @@ Schematic *get_or_load_schematic(lua_State *L, int index,
Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
StringMap *replace_names, NodeResolveMethod resolve_method)
StringMap *replace_names)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
@ -183,7 +167,7 @@ Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
if (lua_istable(L, index)) {
schem = load_schematic_from_def(L, index, ndef,
replace_names, resolve_method);
replace_names);
if (!schem) {
delete schem;
return NULL;
@ -198,7 +182,7 @@ Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
if (!schem->loadSchematicFromFile(filepath, ndef,
replace_names, resolve_method)) {
replace_names)) {
delete schem;
return NULL;
}
@ -208,8 +192,8 @@ Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
}
Schematic *load_schematic_from_def(lua_State *L, int index, INodeDefManager *ndef,
StringMap *replace_names, NodeResolveMethod resolve_method)
Schematic *load_schematic_from_def(lua_State *L, int index,
INodeDefManager *ndef, StringMap *replace_names)
{
Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
@ -230,7 +214,7 @@ Schematic *load_schematic_from_def(lua_State *L, int index, INodeDefManager *nde
}
}
ndef->pendNodeResolve(schem, resolve_method);
ndef->pendNodeResolve(schem);
return schem;
}
@ -406,7 +390,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
nn.push_back(getstringfield_default(L, index, "node_water", ""));
nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
nn.push_back(getstringfield_default(L, index, "node_dust", ""));
ndef->pendNodeResolve(b, NODE_RESOLVE_DEFERRED);
ndef->pendNodeResolve(b);
return b;
}
@ -770,7 +754,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
return 0;
}
ndef->pendNodeResolve(deco, NODE_RESOLVE_DEFERRED);
ndef->pendNodeResolve(deco);
ObjDefHandle handle = decomgr->add(deco);
if (handle == OBJDEF_INVALID_HANDLE) {
@ -928,7 +912,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
ore->m_nnlistsizes.push_back(nnames);
ndef->pendNodeResolve(ore, NODE_RESOLVE_DEFERRED);
ndef->pendNodeResolve(ore);
lua_pushinteger(L, handle);
return 1;
@ -945,7 +929,7 @@ int ModApiMapgen::l_register_schematic(lua_State *L)
read_schematic_replacements(L, 2, &replace_names);
Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
&replace_names, NODE_RESOLVE_DEFERRED);
&replace_names);
if (!schem)
return 0;
@ -1138,8 +1122,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
return 0;
}
schem->placeStructure(map, p, 0, (Rotation)rot, force_placement,
schemmgr->getNodeDef());
schem->placeStructure(map, p, 0, (Rotation)rot, force_placement);
lua_pushboolean(L, true);
return 1;
@ -1151,14 +1134,15 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L)
SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
//// Read options
NodeResolveMethod resolve_method = (NodeResolveMethod)getenumfield(L, 3,
"node_resolve_method", es_NodeResolveMethod, NODE_RESOLVE_NONE);
bool register_on_load = getboolfield_default(L, 3, "register_on_load", false);
bool use_comments = getboolfield_default(L, 3, "use_lua_comments", false);
//// Read schematic
Schematic *schem = get_or_load_schematic(L, 1, schemmgr, NULL,
register_on_load, resolve_method);
//// Get schematic
bool was_loaded = false;
Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
if (!schem) {
schem = load_schematic(L, 1, NULL, NULL);
was_loaded = true;
}
if (!schem) {
errorstream << "serialize_schematic: failed to get schematic" << std::endl;
return 0;
@ -1174,15 +1158,18 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L)
std::ostringstream os(std::ios_base::binary);
switch (schem_format) {
case SCHEM_FMT_MTS:
schem->serializeToMts(&os);
schem->serializeToMts(&os, schem->m_nodenames);
break;
case SCHEM_FMT_LUA:
schem->serializeToLua(&os, use_comments);
schem->serializeToLua(&os, schem->m_nodenames, use_comments);
break;
default:
return 0;
}
if (was_loaded)
delete schem;
std::string ser = os.str();
lua_pushlstring(L, ser.c_str(), ser.length());
return 1;

@ -50,12 +50,6 @@ void TestNodeResolver::runTests(IGameDef *gamedef)
ndef->resetNodeResolveState();
TEST(testPendingResolveCancellation, ndef);
ndef->resetNodeResolveState();
TEST(testDirectResolveMethod, ndef);
ndef->resetNodeResolveState();
TEST(testNoneResolveMethod, ndef);
}
class Foobar : public NodeResolver {
@ -136,7 +130,7 @@ void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
foobar.m_nodenames.push_back("default:desert_stone");
foobar.m_nodenames.push_back("default:shnitzle");
ndef->pendNodeResolve(&foobar, NODE_RESOLVE_DEFERRED);
ndef->pendNodeResolve(&foobar);
UASSERT(foobar.m_ndef == ndef);
ndef->setNodeRegistrationStatus(true);
@ -193,14 +187,14 @@ void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *n
foobaz1.test_content2 = 5678;
foobaz1.m_nodenames.push_back("default:dirt_with_grass");
foobaz1.m_nodenames.push_back("default:abloobloobloo");
ndef->pendNodeResolve(&foobaz1, NODE_RESOLVE_DEFERRED);
ndef->pendNodeResolve(&foobaz1);
Foobaz foobaz2;
foobaz2.test_content1 = 1234;
foobaz2.test_content2 = 5678;
foobaz2.m_nodenames.push_back("default:dirt_with_grass");
foobaz2.m_nodenames.push_back("default:abloobloobloo");
ndef->pendNodeResolve(&foobaz2, NODE_RESOLVE_DEFERRED);
ndef->pendNodeResolve(&foobaz2);
ndef->cancelNodeResolveCallback(&foobaz1);
@ -212,37 +206,3 @@ void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *n
UASSERT(foobaz2.test_content1 == t_CONTENT_GRASS);
UASSERT(foobaz2.test_content2 == CONTENT_IGNORE);
}
void TestNodeResolver::testDirectResolveMethod(IWritableNodeDefManager *ndef)
{
Foobaz foobaz;
foobaz.m_nodenames.push_back("default:dirt_with_grass");
foobaz.m_nodenames.push_back("default:abloobloobloo");
UASSERTEQ(std::string, foobaz.getNodeName(1), "default:abloobloobloo");
ndef->pendNodeResolve(&foobaz, NODE_RESOLVE_DIRECT);
UASSERTEQ(content_t, foobaz.test_content1, t_CONTENT_GRASS);
UASSERTEQ(content_t, foobaz.test_content2, CONTENT_IGNORE);
// We expect this to be *different* because the resolution of this node had
// failed. The internal nodename buffer is cleared and lookups should now
// use the nodedef manager.
UASSERT(foobaz.getNodeName(1) != "default:abloobloobloo");
}
void TestNodeResolver::testNoneResolveMethod(IWritableNodeDefManager *ndef)
{
Foobaz foobaz;
foobaz.m_nodenames.push_back("default:dirt_with_grass");
foobaz.m_nodenames.push_back("default:abloobloobloo");
ndef->pendNodeResolve(&foobaz, NODE_RESOLVE_NONE);
UASSERTEQ(std::string, foobaz.getNodeName(1), "default:abloobloobloo");
}