From 7d497ea9608d8758ef1c93e5d8423da6626f04f2 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 25 Jul 2010 11:40:14 +0100 Subject: [PATCH] Locked height map nodes covered by track --- src/Map.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Map.cpp b/src/Map.cpp index d0afc9a..684145d 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -139,6 +139,9 @@ private: // Vertices on the terrain struct HeightMap { Vector pos, normal; + + // How many track segments are locking the height at this node + int lock_count; } *height_map; static const unsigned TILE_NAME_BASE = 1000; // Base of tile naming @@ -197,6 +200,8 @@ private: void draw_start_location() const; void set_station_at(Point point, IStationPtr a_station); void render_highlighted_tiles() const; + void lock_height_at(Point p); + void unlock_height_at(Point p); // Mesh modification void build_mesh(int id, Point bot_left, Point top_right); @@ -276,6 +281,13 @@ void Map::erase_tile(int x, int y) // We have to be a bit careful since a piece of track has multiple // endpoints + vector > locked; + tile.track->get()->get_covers2(locked); + + for (vector >::iterator it = locked.begin(); + it != locked.end(); ++it) + unlock_height_at(*it); + vector > covers; tile.track->get()->get_endpoints(covers); tile.track->get()->get_covers(covers); @@ -325,6 +337,14 @@ void Map::set_track_at(const Point& where, ITrackSegmentPtr track) dirty_tile((*it).x, (*it).y); } + + // Lock every height node touched by this track segment + vector > locked; + track->get_covers2(locked); + + for (vector >::iterator it = locked.begin(); + it != locked.end(); ++it) + lock_height_at(*it); } bool Map::is_valid_track(const Point& where) const @@ -413,6 +433,7 @@ void Map::reset_map(int a_width, int a_depth) v.pos = make_vector(xf, 0.0f, yf); v.normal = make_vector(0.0f, 1.0f, 0.0f); + v.lock_count = 0; } } @@ -1007,6 +1028,7 @@ void Map::tile_vertices(int x, int y, int* indexes) const // a piece of track bool Map::raise_will_cover_track(int x, int y) const { +#if 0 return tile_at(x, y).track || (x < my_width - 1 && tile_at(x + 1, y).track) || (x > 0 && tile_at(x - 1, y).track) @@ -1016,6 +1038,18 @@ bool Map::raise_will_cover_track(int x, int y) const || (x > 0 && y < my_depth - 1 && tile_at(x - 1, y + 1).track) || (x > 0 && y > 0 && tile_at(x - 1, y - 1).track) || (x < my_width - 1 && y > 0 && tile_at(x + 1, y - 1).track); +#else + int indexes[4]; + tile_vertices(x, y, indexes); + + bool ok = true; + for (int i = 0; i < 4; i++) { + debug() << height_map[indexes[i]].lock_count; + ok &= height_map[indexes[i]].lock_count == 0; + } + + return !ok; +#endif } // Changes the height of a complete tile @@ -1036,6 +1070,27 @@ void Map::raise_tile(int x, int y, float delta_height) dirty_tile(x, y); } +void Map::lock_height_at(Point p) +{ + assert(p.x <= my_width); + assert(p.y <= my_depth); + + debug () << __func__ << " p=" << p; + + height_map[p.x + (p.y * (my_depth+1))].lock_count++; +} + +void Map::unlock_height_at(Point p) +{ + assert(p.x <= my_width); + assert(p.y <= my_depth); + + HeightMap& h = height_map[p.x + (p.y * (my_depth+1))]; + + assert(h.lock_count > 0); + h.lock_count--; +} + // Sets the absolute height of a tile void Map::set_tile_height(int x, int y, float h) { @@ -1419,9 +1474,11 @@ void Map::read_height_map(IResource::Handle a_handle) ("Binary file " + a_handle.file_name() + " dimensions are incorrect"); } - for (int i = 0; i < (my_width + 1) * (my_depth + 1); i++) + for (int i = 0; i < (my_width + 1) * (my_depth + 1); i++) { is.read(reinterpret_cast(&height_map[i].pos.y), - sizeof(float)); + sizeof(float)); + height_map[i].lock_count = 0; + } for (int x = 0; x < my_width; x++) { for (int y = 0; y < my_depth; y++) -- 2.39.2