Fix unit test if there isn't a localhost address (for example FreeBSD jails), second...
[oweals/minetest.git] / src / nodedef.cpp
index 7dbaaee9cf07179dbe6a32dcacf901455fbe2942..bcf51a072e1429e9f75d8c93b6e1e1786564fa6b 100644 (file)
@@ -227,7 +227,6 @@ void ContentFeatures::reset()
        liquid_alternative_source = "";
        liquid_viscosity = 0;
        liquid_renewable = true;
-       freezemelt = "";
        liquid_range = LIQUID_LEVEL_MAX+1;
        drowning = 0;
        light_source = 0;
@@ -389,8 +388,8 @@ public:
        virtual ~CNodeDefManager();
        void clear();
        virtual IWritableNodeDefManager *clone();
-       virtual const ContentFeatures& get(content_t c) const;
-       virtual const ContentFeatures& get(const MapNode &n) const;
+       inline virtual const ContentFeatures& get(content_t c) const;
+       inline virtual const ContentFeatures& get(const MapNode &n) const;
        virtual bool getId(const std::string &name, content_t &result) const;
        virtual content_t getId(const std::string &name) const;
        virtual void getIds(const std::string &name, std::set<content_t> &result) const;
@@ -402,7 +401,18 @@ public:
        virtual void updateTextures(IGameDef *gamedef);
        void serialize(std::ostream &os, u16 protocol_version);
        void deSerialize(std::istream &is);
-       virtual NodeResolver *getResolver();
+
+       inline virtual bool getNodeRegistrationStatus() const;
+       inline virtual void setNodeRegistrationStatus(bool completed);
+
+       virtual void pendNodeResolve(NodeResolveInfo *nri);
+       virtual void cancelNodeResolve(NodeResolver *resolver);
+       virtual void runNodeResolverCallbacks();
+
+       virtual bool getIdFromResolveInfo(NodeResolveInfo *nri,
+               const std::string &node_alt, content_t c_fallback, content_t &result);
+       virtual bool getIdsFromResolveInfo(NodeResolveInfo *nri,
+               std::vector<content_t> &result);
 
 private:
        void addNameIdMapping(content_t i, std::string name);
@@ -432,13 +442,15 @@ private:
        // Next possibly free id
        content_t m_next_id;
 
-       // NodeResolver to queue pending node resolutions
-       NodeResolver m_resolver;
+       // List of node strings and node resolver callbacks to perform
+       std::list<NodeResolveInfo *> m_pending_node_lookups;
+
+       // True when all nodes have been registered
+       bool m_node_registration_complete;
 };
 
 
-CNodeDefManager::CNodeDefManager() :
-       m_resolver(this)
+CNodeDefManager::CNodeDefManager()
 {
        clear();
 }
@@ -466,6 +478,14 @@ void CNodeDefManager::clear()
        m_group_to_items.clear();
        m_next_id = 0;
 
+       m_node_registration_complete = false;
+       for (std::list<NodeResolveInfo *>::iterator
+                       it = m_pending_node_lookups.begin();
+                       it != m_pending_node_lookups.end();
+                       ++it)
+               delete *it;
+       m_pending_node_lookups.clear();
+
        u32 initial_length = 0;
        initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
        initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
@@ -530,16 +550,14 @@ IWritableNodeDefManager *CNodeDefManager::clone()
 }
 
 
-const ContentFeatures& CNodeDefManager::get(content_t c) const
+inline const ContentFeatures& CNodeDefManager::get(content_t c) const
 {
-       if (c < m_content_features.size())
-               return m_content_features[c];
-       else
-               return m_content_features[CONTENT_UNKNOWN];
+       return c < m_content_features.size()
+                       ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
 }
 
 
-const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
+inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
 {
        return get(n.getContent());
 }
@@ -701,7 +719,7 @@ void CNodeDefManager::updateTextures(IGameDef *gamedef)
 #ifndef SERVER
        infostream << "CNodeDefManager::updateTextures(): Updating "
                "textures in node definitions" << std::endl;
-       
+
        ITextureSource *tsrc = gamedef->tsrc();
        IShaderSource *shdsrc = gamedef->getShaderSource();
        scene::ISceneManager* smgr = gamedef->getSceneManager();
@@ -856,8 +874,9 @@ void CNodeDefManager::updateTextures(IGameDef *gamedef)
                                v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
                                scaleMesh(f->mesh_ptr[0], scale);
                                recalculateBoundingBox(f->mesh_ptr[0]);
+                               meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
                        }
-               } else if ((f->drawtype == NDT_NODEBOX) && 
+               } else if ((f->drawtype == NDT_NODEBOX) &&
                                ((f->node_box.type == NODEBOX_REGULAR) ||
                                (f->node_box.type == NODEBOX_FIXED)) &&
                                (!f->node_box.fixed.empty())) {
@@ -868,6 +887,7 @@ void CNodeDefManager::updateTextures(IGameDef *gamedef)
                        v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
                        scaleMesh(f->mesh_ptr[0], scale);
                        recalculateBoundingBox(f->mesh_ptr[0]);
+                       meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
                }
 
                //Cache 6dfacedir and wallmounted rotated clones of meshes
@@ -879,7 +899,7 @@ void CNodeDefManager::updateTextures(IGameDef *gamedef)
                                meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
                        }
                } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
-                       static const u8 wm_to_6d[6] =   {20, 0, 16, 12, 8, 4}; 
+                       static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
                        for (u16 j = 1; j < 6; j++) {
                                f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
                                rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
@@ -888,7 +908,7 @@ void CNodeDefManager::updateTextures(IGameDef *gamedef)
                        }
                        rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
                        recalculateBoundingBox(f->mesh_ptr[0]);
-                       meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);                     
+                       meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
                }
        }
 #endif
@@ -934,7 +954,10 @@ void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
                tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
        } else {
                std::ostringstream os(std::ios::binary);
+               tile->frames.resize(frame_count);
+
                for (int i = 0; i < frame_count; i++) {
+
                        FrameSpec frame;
 
                        os.str("");
@@ -1032,12 +1055,6 @@ void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
 }
 
 
-NodeResolver *CNodeDefManager::getResolver()
-{
-       return &m_resolver;
-}
-
-
 IWritableNodeDefManager *createNodeDefManager()
 {
        return new CNodeDefManager();
@@ -1148,7 +1165,7 @@ void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
                writeU8(os, drowning);
                writeU8(os, leveled);
                writeU8(os, liquid_range);
-       } else 
+       } else
                throw SerializationError("ContentFeatures::serialize(): "
                        "Unsupported version requested");
 }
@@ -1264,163 +1281,129 @@ void ContentFeatures::deSerializeOld(std::istream &is, int version)
        }
 }
 
-/*
-       NodeResolver
-*/
 
-NodeResolver::NodeResolver(INodeDefManager *ndef)
+inline bool CNodeDefManager::getNodeRegistrationStatus() const
 {
-       m_ndef = ndef;
-       m_is_node_registration_complete = false;
+       return m_node_registration_complete;
 }
 
 
-NodeResolver::~NodeResolver()
+inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
 {
-       while (!m_pending_contents.empty()) {
-               NodeResolveInfo *nri = m_pending_contents.front();
-               m_pending_contents.pop_front();
-               delete nri;
-       }
+       m_node_registration_complete = completed;
 }
 
 
-int NodeResolver::addNode(std::string n_wanted, std::string n_alt,
-               content_t c_fallback, content_t *content)
+void CNodeDefManager::pendNodeResolve(NodeResolveInfo *nri)
 {
-       if (m_is_node_registration_complete) {
-               if (m_ndef->getId(n_wanted, *content))
-                       return NR_STATUS_SUCCESS;
-
-               if (n_alt == "" || !m_ndef->getId(n_alt, *content)) {
-                       *content = c_fallback;
-                       return NR_STATUS_FAILURE;
-               }
-
-               return NR_STATUS_SUCCESS;
+       nri->resolver->m_ndef = this;
+       if (m_node_registration_complete) {
+               nri->resolver->resolveNodeNames(nri);
+               nri->resolver->m_lookup_done = true;
+               delete nri;
        } else {
-               NodeResolveInfo *nfi = new NodeResolveInfo;
-               nfi->n_wanted   = n_wanted;
-               nfi->n_alt      = n_alt;
-               nfi->c_fallback = c_fallback;
-               nfi->output     = content;
-
-               m_pending_contents.push_back(nfi);
-
-               return NR_STATUS_PENDING;
+               m_pending_node_lookups.push_back(nri);
        }
 }
 
 
-int NodeResolver::addNodeList(const char *nodename,
-               std::vector<content_t> *content_vec)
+void CNodeDefManager::cancelNodeResolve(NodeResolver *resolver)
 {
-       if (m_is_node_registration_complete) {
-               std::set<content_t> idset;
-               std::set<content_t>::iterator it;
+       for (std::list<NodeResolveInfo *>::iterator
+                       it = m_pending_node_lookups.begin();
+                       it != m_pending_node_lookups.end();
+                       ++it) {
+               NodeResolveInfo *nri = *it;
+               if (resolver == nri->resolver) {
+                       it = m_pending_node_lookups.erase(it);
+                       delete nri;
+               }
+       }
+}
 
-               m_ndef->getIds(nodename, idset);
-               for (it = idset.begin(); it != idset.end(); ++it)
-                       content_vec->push_back(*it);
 
-               return idset.size() ? NR_STATUS_SUCCESS : NR_STATUS_FAILURE;
-       } else {
-               m_pending_content_vecs.push_back(
-                       std::make_pair(std::string(nodename), content_vec));
-               return NR_STATUS_PENDING;
+void CNodeDefManager::runNodeResolverCallbacks()
+{
+       while (!m_pending_node_lookups.empty()) {
+               NodeResolveInfo *nri = m_pending_node_lookups.front();
+               m_pending_node_lookups.pop_front();
+               nri->resolver->resolveNodeNames(nri);
+               nri->resolver->m_lookup_done = true;
+               delete nri;
        }
 }
 
 
-bool NodeResolver::cancelNode(content_t *content)
+bool CNodeDefManager::getIdFromResolveInfo(NodeResolveInfo *nri,
+       const std::string &node_alt, content_t c_fallback, content_t &result)
 {
-       bool found = false;
-
-       std::list<NodeResolveInfo *>::iterator it = m_pending_contents.begin();
-       while (it != m_pending_contents.end()) {
-               NodeResolveInfo *nfi = *it;
-               if (nfi->output == content) {
-                       it = m_pending_contents.erase(it);
-                       delete nfi;
-                       found = true;
-               }
+       if (nri->nodenames.empty()) {
+               result = c_fallback;
+               errorstream << "Resolver empty nodename list" << std::endl;
+               return false;
        }
 
-       return found;
-}
+       content_t c;
+       std::string name = nri->nodenames.front();
+       nri->nodenames.pop_front();
 
+       bool success = getId(name, c);
+       if (!success && node_alt != "") {
+               name = node_alt;
+               success = getId(name, c);
+       }
 
-int NodeResolver::cancelNodeList(std::vector<content_t> *content_vec)
-{
-       int num_canceled = 0;
-
-       std::list<std::pair<std::string, std::vector<content_t> *> >::iterator it;
-       it = m_pending_content_vecs.begin();
-       while (it != m_pending_content_vecs.end()) {
-               if (it->second == content_vec) {
-                       it = m_pending_content_vecs.erase(it);
-                       num_canceled++;
-               }
+       if (!success) {
+               errorstream << "Resolver: Failed to resolve node name '" << name
+                       << "'." << std::endl;
+               c = c_fallback;
        }
 
-       return num_canceled;
+       result = c;
+       return success;
 }
 
 
-int NodeResolver::resolveNodes()
+bool CNodeDefManager::getIdsFromResolveInfo(NodeResolveInfo *nri,
+       std::vector<content_t> &result)
 {
-       int num_failed = 0;
+       bool success = true;
 
-       //// Resolve pending single node name -> content ID mappings
-       while (!m_pending_contents.empty()) {
-               NodeResolveInfo *nri = m_pending_contents.front();              
-               m_pending_contents.pop_front();
+       if (nri->nodelistinfo.empty()) {
+               errorstream << "Resolver: Empty nodelistinfo list" << std::endl;
+               return false;
+       }
 
-               bool success = true;
-               if (!m_ndef->getId(nri->n_wanted, *nri->output)) {
-                       success = (nri->n_alt != "") ?
-                               m_ndef->getId(nri->n_alt, *nri->output) : false;
-               }
+       NodeListInfo listinfo = nri->nodelistinfo.front();
+       nri->nodelistinfo.pop_front();
 
-               if (!success) {
-                       *nri->output = nri->c_fallback;
-                       num_failed++;
-                       errorstream << "NodeResolver::resolveNodes():  Failed to "
-                               "resolve '" << nri->n_wanted;
-                       if (nri->n_alt != "")
-                               errorstream << "' and '" << nri->n_alt;
-                       errorstream << "'" << std::endl;
+       while (listinfo.length--) {
+               if (nri->nodenames.empty()) {
+                       errorstream << "Resolver: Empty nodename list" << std::endl;
+                       return false;
                }
 
-               delete nri;
-       }
-
-       //// Resolve pending node names and add to content_t vector
-       while (!m_pending_content_vecs.empty()) {
-               std::pair<std::string, std::vector<content_t> *> item =
-                       m_pending_content_vecs.front();
-               m_pending_content_vecs.pop_front();
-
-               std::string &name = item.first;
-               std::vector<content_t> *output = item.second;
-               
-               std::set<content_t> idset;
-               std::set<content_t>::iterator it;
-
-               m_ndef->getIds(name, idset);
-               for (it = idset.begin(); it != idset.end(); ++it)
-                       output->push_back(*it);
-
-               if (idset.size() == 0) {
-                       num_failed++;
-                       errorstream << "NodeResolver::resolveNodes():  Failed to "
-                               "resolve '" << name << "'" << std::endl;
+               content_t c;
+               std::string name = nri->nodenames.front();
+               nri->nodenames.pop_front();
+
+               if (name.substr(0,6) != "group:") {
+                       if (getId(name, c)) {
+                               result.push_back(c);
+                       } else if (listinfo.all_required) {
+                               errorstream << "Resolver: Failed to resolve node name '" << name
+                                       << "'." << std::endl;
+                               result.push_back(listinfo.c_fallback);
+                               success = false;
+                       }
+               } else {
+                       std::set<content_t> cids;
+                       std::set<content_t>::iterator it;
+                       getIds(name, cids);
+                       for (it = cids.begin(); it != cids.end(); ++it)
+                               result.push_back(*it);
                }
        }
 
-       //// Mark node registration as complete so future resolve
-       //// requests are satisfied immediately
-       m_is_node_registration_complete = true;
-
-       return num_failed;
+       return success;
 }