2 #include <msp/geometry/box.h>
3 #include <msp/geometry/transformedshape.h>
6 #include "terraintype.h"
13 Terrain::Terrain(Layout &l, const TerrainType &t):
29 Terrain *Terrain::clone(Layout *to_layout) const
31 Terrain *terrain = new Terrain((to_layout ? *to_layout : layout), type);
32 terrain->set_size(width, height);
33 for(unsigned i=0; i<tiles.size(); ++i)
34 terrain->tiles[i] = tiles[i];
38 void Terrain::set_position(const Vector &p)
44 void Terrain::set_rotation(const Angle &r)
50 void Terrain::set_size(unsigned w, unsigned h)
53 throw invalid_argument("Terrain::set_size");
55 vector<Tile> new_tiles(w*h);
56 for(unsigned y=0; (y<h && y<height); ++y)
57 for(unsigned x=0; (x<w && x<width); ++x)
58 new_tiles[x+y*w] = tiles[x+y*width];
62 swap(tiles, new_tiles);
65 float ts = type.get_tile_size();
66 Vector dim(width*ts, height*ts, ts);
67 shape = new Geometry::TransformedShape<float, 3>(
68 Geometry::Box<float>(dim),
69 Transform::translation(dim/2.0f));
71 signal_size_changed.emit(width, height);
74 const Terrain::Tile &Terrain::get_tile(unsigned x, unsigned y) const
76 if(x>=width || y>=height)
77 throw out_of_range("Terrain::get_tile");
78 return tiles[x+y*width];
81 void Terrain::set_node_elevation(const NodeCoordinates &c, float elev, bool joined)
83 if(c.x>=width || c.y>=height || c.i>=4)
84 throw out_of_range("Terrain::set_node_elevation");
86 float eg = type.get_elevation_granularity();
87 elev = floor(elev/eg+0.5)*eg;
91 float ref = tiles[c.x+c.y*width].nodes[c.i].elevation;
92 for(unsigned i=0; i<4; ++i)
94 unsigned x = c.x+c.i%2-i%2;
95 unsigned y = c.y+c.i/2-i/2;
96 if(x<width && y<height)
98 Tile &tile = tiles[x+y*width];
99 if(tile.nodes[i].elevation==ref)
101 tile.set_node_elevation(i, elev);
102 signal_tile_changed.emit(x, y);
109 tiles[c.x+c.y*width].set_node_elevation(c.i, elev);
110 signal_tile_changed.emit(c.x, c.y);
114 float Terrain::get_node_elevation(const NodeCoordinates &c) const
116 if(c.x>=width || c.y>=height || c.i>=4)
117 throw out_of_range("Terrain::get_node_elevation");
119 return tiles[c.x+c.y*width].nodes[c.i].elevation;
122 Vector Terrain::get_node_position(const NodeCoordinates &c) const
124 if(c.x>=width || c.y>=height || c.i>=4)
125 throw out_of_range("Terrain::get_node_position");
127 const Tile &tile = tiles[c.x+c.y*width];
128 float tile_size = type.get_tile_size();
129 Transform trans = Transform::translation(position)*
130 Transform::rotation(rotation, Vector(0, 0, 1));
131 return trans.transform(Vector((c.x+c.i%2)*tile_size, (c.y+c.i/2)*tile_size, tile.nodes[c.i].elevation));
134 Terrain::NodeCoordinates Terrain::get_closest_node(const Ray &ray) const
136 Transform reverse_trans = Transform::rotation(rotation, Vector(0, 0, -1))*
137 Transform::translation(-position);
138 Ray local_ray = reverse_trans.transform(ray);
140 float ts = type.get_tile_size();
141 NodeCoordinates coords;
142 float closest_dist = -1;
143 for(unsigned y=0; y<height; ++y)
144 for(unsigned x=0; x<width; ++x)
145 for(unsigned i=0; i<4; ++i)
147 NodeCoordinates c(x, y, i);
148 Vector node_pos((x+0.25+(i%2)*0.5)*ts, (y+0.25+(i/2)*0.5)*ts, get_node_elevation(c));
149 Vector v = node_pos-local_ray.get_start();
150 float dist = (v-local_ray.get_direction()*dot(local_ray.get_direction(), v)).norm();
151 if(closest_dist<0 || dist<closest_dist)
161 void Terrain::save(list<DataFile::Statement> &st) const
163 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
164 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
165 st.push_back((DataFile::Statement("size"), width, height));
166 for(vector<Tile>::const_iterator i=tiles.begin(); i!=tiles.end(); ++i)
168 DataFile::Statement ss("tile");
175 Terrain::Node::Node():
181 void Terrain::Node::save(list<DataFile::Statement> &st) const
183 st.push_back((DataFile::Statement("elevation"), elevation));
184 st.push_back((DataFile::Statement("ground"), ground));
188 Terrain::Tile::Tile():
189 secondary_axis(false)
192 void Terrain::Tile::set_node_elevation(unsigned i, float e)
194 nodes[i].elevation = e;
198 void Terrain::Tile::update_axis()
200 secondary_axis = (abs(nodes[1].elevation-nodes[2].elevation) < abs(nodes[0].elevation-nodes[3].elevation));
203 void Terrain::Tile::save(list<DataFile::Statement> &st) const
205 float elevation_mode = find_mode(&Node::elevation);
206 unsigned ground_mode = find_mode(&Node::ground);
208 st.push_back((DataFile::Statement("elevation"), elevation_mode));
210 st.push_back((DataFile::Statement("ground"), ground_mode));
212 for(unsigned i=0; i<4; ++i)
214 if(nodes[i].elevation==elevation_mode && nodes[i].ground==ground_mode)
217 DataFile::Statement ss("node");
219 if(nodes[i].elevation!=elevation_mode)
220 ss.sub.push_back((DataFile::Statement("elevation"), nodes[i].elevation));
221 if(nodes[i].ground!=ground_mode)
222 ss.sub.push_back((DataFile::Statement("ground"), nodes[i].ground));
228 T Terrain::Tile::find_mode(T Node::*member) const
230 for(unsigned i=0; i<3; ++i)
231 for(unsigned j=i+1; j<4; ++j)
232 if(nodes[i].*member==nodes[j].*member)
233 return nodes[i].*member;
238 Terrain::NodeCoordinates::NodeCoordinates():
244 Terrain::NodeCoordinates::NodeCoordinates(unsigned x_, unsigned y_, unsigned i_):
251 Terrain::Loader::Loader(Terrain &t):
252 DataFile::ObjectLoader<Terrain>(t),
255 add("position", &Loader::position);
256 add("rotation", &Loader::rotation);
257 add("size", &Loader::size);
258 add("tile", &Loader::tile);
259 add("tile", &Loader::tile_coords);
262 void Terrain::Loader::position(float x, float y, float z)
264 obj.set_position(Vector(x, y, z));
267 void Terrain::Loader::rotation(float a)
269 obj.set_rotation(Angle::from_radians(a));
272 void Terrain::Loader::size(unsigned w, unsigned h)
277 void Terrain::Loader::tile()
279 if(next_tile>=obj.tiles.size())
280 throw runtime_error("Terrain::Loader::tile");
282 Tile &t = obj.tiles[next_tile];
283 Tile::Loader ldr(obj, t);
286 unsigned x = next_tile%obj.width;
287 unsigned y = next_tile/obj.width;
289 obj.signal_tile_changed.emit(x, y);
292 void Terrain::Loader::tile_coords(unsigned x, unsigned y)
294 if(x>=obj.width || y>=obj.height)
295 throw out_of_range("Terrain::Loader::tile");
296 next_tile = x+y*obj.width;
301 Terrain::Node::Loader::Loader(Terrain &t, Node &n):
302 DataFile::ObjectLoader<Node>(n),
305 add("ground", &Loader::ground);
306 add("elevation", &Node::elevation);
309 void Terrain::Node::Loader::ground(unsigned g)
311 if(g>=terrain.type.get_n_surface_types())
312 throw out_of_range("Tile::Loader::surface");
317 Terrain::Tile::Loader::Loader(Terrain &t, Tile &l):
318 DataFile::ObjectLoader<Tile>(l),
321 add("ground", &Loader::ground);
322 add("elevation", &Loader::elevation);
323 add("node", &Loader::node);
326 void Terrain::Tile::Loader::ground(unsigned g)
328 if(g>=terrain.type.get_n_surface_types())
329 throw out_of_range("Tile::Loader::surface");
330 for(unsigned i=0; i<4; ++i)
331 obj.nodes[i].ground = g;
334 void Terrain::Tile::Loader::elevation(float h)
336 for(unsigned i=0; i<4; ++i)
337 obj.nodes[i].elevation = h;
340 void Terrain::Tile::Loader::node(unsigned i)
343 throw out_of_range("Tile::Loader::node");
344 Node::Loader ldr(terrain, obj.nodes[i]);