From: Mikko Rasa Date: Fri, 22 Nov 2013 11:09:16 +0000 (+0200) Subject: Produce more compact saves of terrains X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e536be49146f43aae6aaaa4455eb44d8e170a773;p=r2c2.git Produce more compact saves of terrains --- diff --git a/source/libr2c2/terrain.cpp b/source/libr2c2/terrain.cpp index 7b8f9f5..e012bc9 100644 --- a/source/libr2c2/terrain.cpp +++ b/source/libr2c2/terrain.cpp @@ -201,26 +201,38 @@ void Terrain::Tile::update_axis() void Terrain::Tile::save(list &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 +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), diff --git a/source/libr2c2/terrain.h b/source/libr2c2/terrain.h index 01571cc..5b5a889 100644 --- a/source/libr2c2/terrain.h +++ b/source/libr2c2/terrain.h @@ -72,6 +72,8 @@ public: void set_node_elevation(unsigned, float); void update_axis(); void save(std::list &) const; + template + T find_mode(T Node::*) const; }; struct NodeCoordinates