]> git.tdb.fi Git - r2c2.git/commitdiff
Produce more compact saves of terrains
authorMikko Rasa <tdb@tdb.fi>
Fri, 22 Nov 2013 11:09:16 +0000 (13:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 22 Nov 2013 11:09:16 +0000 (13:09 +0200)
source/libr2c2/terrain.cpp
source/libr2c2/terrain.h

index 7b8f9f5778c3d0fe84f73f5c9cac2bd04d2029b2..e012bc9426e927feaf5733c189899e4ea05208c8 100644 (file)
@@ -201,26 +201,38 @@ void Terrain::Tile::update_axis()
 
 void Terrain::Tile::save(list<DataFile::Statement> &st) const
 {
-       bool flat = true;
-       for(unsigned i=1; (flat && i<4); ++i)
-               flat = (nodes[i].elevation==nodes[0].elevation && nodes[i].ground==nodes[0].ground && nodes[i].wall==nodes[0].wall);
-       if(flat)
-       {
-               st.push_back((DataFile::Statement("elevation"), nodes[0].elevation));
-               st.push_back((DataFile::Statement("ground"), nodes[0].ground));
-       }
-       else
+       float elevation_mode = find_mode(&Node::elevation);
+       unsigned ground_mode = find_mode(&Node::ground);
+       if(elevation_mode)
+               st.push_back((DataFile::Statement("elevation"), elevation_mode));
+       if(ground_mode)
+               st.push_back((DataFile::Statement("ground"), ground_mode));
+
+       for(unsigned i=0; i<4; ++i)
        {
-               for(unsigned i=0; i<4; ++i)
-               {
-                       DataFile::Statement ss("node");
-                       ss.append(i);
-                       nodes[i].save(ss.sub);
-                       st.push_back(ss);
-               }
+               if(nodes[i].elevation==elevation_mode && nodes[i].ground==ground_mode)
+                       continue;
+
+               DataFile::Statement ss("node");
+               ss.append(i);
+               if(nodes[i].elevation!=elevation_mode)
+                       ss.sub.push_back((DataFile::Statement("elevation"), nodes[i].elevation));
+               if(nodes[i].ground!=ground_mode)
+                       ss.sub.push_back((DataFile::Statement("ground"), nodes[i].ground));
+               st.push_back(ss);
        }
 }
 
+template<typename T>
+T Terrain::Tile::find_mode(T Node::*member) const
+{
+       for(unsigned i=0; i<3; ++i)
+               for(unsigned j=i+1; j<4; ++j)
+                       if(nodes[i].*member==nodes[j].*member)
+                               return nodes[i].*member;
+       return T();
+}
+
 
 Terrain::NodeCoordinates::NodeCoordinates():
        x(0),
index 01571cc32806de9f6afb0fbfb167bf0876d828cd..5b5a889a51289f5e69eed9bb13d58832e900fe31 100644 (file)
@@ -72,6 +72,8 @@ public:
                void set_node_elevation(unsigned, float);
                void update_axis();
                void save(std::list<Msp::DataFile::Statement> &) const;
+               template<typename T>
+               T find_mode(T Node::*) const;
        };
 
        struct NodeCoordinates