Chunnel: Use hash table for tunnel search, change tunnel fields and savegame format.

This commit is contained in:
Jonathan G Rennison
2017-03-05 14:32:17 +00:00
parent ad15b47f72
commit 89a0a9c182
5 changed files with 54 additions and 27 deletions

View File

@@ -22,6 +22,21 @@ TunnelPool _tunnel_pool("Tunnel");
INSTANTIATE_POOL_METHODS(Tunnel)
static std::unordered_map<TileIndex, TunnelID> tunnel_tile_index_map;
static std::unordered_multimap<uint64, Tunnel*> tunnel_axis_height_index;
static uint64 GetTunnelAxisHeightCacheKey(TileIndex tile, uint8 height, bool y_axis) {
if (y_axis) {
// tunnel extends along Y axis (DIAGDIR_SE from north end), has same X values
return TileX(tile) | (((uint64) height) << 24) | (((uint64) 1) << 32);
} else {
// tunnel extends along X axis (DIAGDIR_SW from north end), has same Y values
return TileY(tile) | (((uint64) height) << 24);
}
}
static inline uint64 GetTunnelAxisHeightCacheKey(const Tunnel* t) {
return GetTunnelAxisHeightCacheKey(t->tile_n, t->height, t->tile_s - t->tile_n > MapMaxX());
}
/**
* Clean up a tunnel tile
@@ -34,6 +49,17 @@ Tunnel::~Tunnel()
tunnel_tile_index_map.erase(this->tile_n);
tunnel_tile_index_map.erase(this->tile_s);
}
auto range = tunnel_axis_height_index.equal_range(GetTunnelAxisHeightCacheKey(this));
bool have_erased = false;
for (auto it = range.first; it != range.second; ++it) {
if (it->second == this) {
tunnel_axis_height_index.erase(it);
have_erased = true;
break;
}
}
assert(have_erased);
}
/**
@@ -45,6 +71,8 @@ void Tunnel::UpdateIndexes()
tunnel_tile_index_map[this->tile_n] = this->index;
tunnel_tile_index_map[this->tile_s] = this->index;
}
tunnel_axis_height_index.emplace(GetTunnelAxisHeightCacheKey(this), this);
}
/**
@@ -53,6 +81,7 @@ void Tunnel::UpdateIndexes()
void Tunnel::PreCleanPool()
{
tunnel_tile_index_map.clear();
tunnel_axis_height_index.clear();
}
TunnelID GetTunnelIndexByLookup(TileIndex t)
@@ -74,6 +103,24 @@ TileIndex GetOtherTunnelEnd(TileIndex tile)
return t->tile_n == tile ? t->tile_s : t->tile_n;
}
static inline bool IsTunnelInWaySingleAxis(TileIndex tile, int z, bool chunnel_allowed, bool y_axis, TileIndexDiff tile_diff)
{
const auto tunnels = tunnel_axis_height_index.equal_range(GetTunnelAxisHeightCacheKey(tile, z, y_axis));
for (auto it = tunnels.first; it != tunnels.second; ++it) {
const Tunnel *t = it->second;
if (t->tile_n > tile || tile > t->tile_s) continue;
if (t->is_chunnel && chunnel_allowed) {
/* Only if tunnel was built over water is terraforming is allowed between portals. */
const TileIndexDiff delta = tile_diff * 4; // 4 tiles ramp.
if (tile < t->tile_n + delta || t->tile_s - delta < tile) return true;
continue;
}
return true;
}
return false;
}
/**
* Is there a tunnel in the way in any direction?
* @param tile the tile to search from.
@@ -83,26 +130,5 @@ TileIndex GetOtherTunnelEnd(TileIndex tile)
*/
bool IsTunnelInWay(TileIndex tile, int z, bool chunnel_allowed)
{
uint x = TileX(tile);
uint y = TileY(tile);
Tunnel *t;
FOR_ALL_TUNNELS(t) {
if (t->tile_n > tile || tile > t->tile_s) continue;
if (t->tile_s - t->tile_n > MapMaxX()){
if (TileX(t->tile_n) != x || (int)TileHeight(t->tile_n) != z) continue; // dir DIAGDIR_SE
} else {
if (TileY(t->tile_n) != y || (int)TileHeight(t->tile_n) != z) continue; // dir DIAGDIR_SW
}
if (t->is_chunnel && chunnel_allowed) {
/* Only if tunnel was build over water terraforming is allowed between portals. */
TileIndexDiff delta = GetTunnelBridgeDirection(t->tile_n) == DIAGDIR_SE ? TileOffsByDiagDir(DIAGDIR_SE) * 4 : 4; // 4 tiles ramp.
if (tile < t->tile_n + delta || t->tile_s - delta < tile) return true;
continue;
}
return true;
}
return false;
return IsTunnelInWaySingleAxis(tile, z, chunnel_allowed, false, 1) || IsTunnelInWaySingleAxis(tile, z, chunnel_allowed, true, TileOffsByDiagDir(DIAGDIR_SE));
}