2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
|
|
|
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
|
2023-03-15 17:00:29 +03:00
|
|
|
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using DotRecast.Core;
|
|
|
|
using DotRecast.Detour.TileCache.Io;
|
2023-03-25 09:43:20 +03:00
|
|
|
using static DotRecast.Core.RecastMath;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:09:10 +03:00
|
|
|
namespace DotRecast.Detour.TileCache
|
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
public class TileCache
|
|
|
|
{
|
|
|
|
int m_tileLutSize;
|
2023-03-16 19:09:10 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < Tile hash lookup size (must be pot).
|
|
|
|
int m_tileLutMask;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < Tile hash lookup mask.
|
|
|
|
private readonly CompressedTile[] m_posLookup;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < Tile hash lookup.
|
|
|
|
private CompressedTile m_nextFreeTile;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < Freelist of tiles.
|
|
|
|
private readonly CompressedTile[] m_tiles;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < List of tiles. // TODO: (PP) replace with list
|
|
|
|
private readonly int m_saltBits;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < Number of salt bits in the tile ID.
|
|
|
|
private readonly int m_tileBits;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// < Number of tile bits in the tile ID.
|
|
|
|
private readonly NavMesh m_navmesh;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private readonly TileCacheParams m_params;
|
|
|
|
private readonly TileCacheStorageParams m_storageParams;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private readonly TileCacheCompressor m_tcomp;
|
|
|
|
private readonly TileCacheMeshProcess m_tmproc;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private readonly List<TileCacheObstacle> m_obstacles = new List<TileCacheObstacle>();
|
|
|
|
private TileCacheObstacle m_nextFreeObstacle;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private readonly List<ObstacleRequest> m_reqs = new List<ObstacleRequest>();
|
|
|
|
private readonly List<long> m_update = new List<long>();
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private readonly TileCacheBuilder builder = new TileCacheBuilder();
|
|
|
|
private readonly TileCacheLayerHeaderReader tileReader = new TileCacheLayerHeaderReader();
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private bool Contains(List<long> a, long v)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return a.Contains(v);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// Encodes a tile id.
|
2023-05-05 02:44:48 +03:00
|
|
|
private long EncodeTileId(int salt, int it)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return ((long)salt << m_tileBits) | it;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// Decodes a tile salt.
|
2023-05-05 02:44:48 +03:00
|
|
|
private int DecodeTileIdSalt(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
long saltMask = (1L << m_saltBits) - 1;
|
|
|
|
return (int)((refs >> m_tileBits) & saltMask);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// Decodes a tile id.
|
2023-05-05 02:44:48 +03:00
|
|
|
private int DecodeTileIdTile(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
long tileMask = (1L << m_tileBits) - 1;
|
|
|
|
return (int)(refs & tileMask);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/// Encodes an obstacle id.
|
2023-05-05 02:44:48 +03:00
|
|
|
private long EncodeObstacleId(int salt, int it)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return ((long)salt << 16) | it;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
/// Decodes an obstacle salt.
|
2023-05-05 02:44:48 +03:00
|
|
|
private int DecodeObstacleIdSalt(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
long saltMask = ((long)1 << 16) - 1;
|
|
|
|
return (int)((refs >> 16) & saltMask);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
/// Decodes an obstacle id.
|
2023-05-05 02:44:48 +03:00
|
|
|
private int DecodeObstacleIdObstacle(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
long tileMask = ((long)1 << 16) - 1;
|
|
|
|
return (int)(refs & tileMask);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public TileCache(TileCacheParams option, TileCacheStorageParams storageParams, NavMesh navmesh,
|
|
|
|
TileCacheCompressor tcomp, TileCacheMeshProcess tmprocs)
|
|
|
|
{
|
|
|
|
m_params = option;
|
|
|
|
m_storageParams = storageParams;
|
|
|
|
m_navmesh = navmesh;
|
|
|
|
m_tcomp = tcomp;
|
|
|
|
m_tmproc = tmprocs;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
m_tileLutSize = NextPow2(m_params.maxTiles / 4);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (m_tileLutSize == 0)
|
|
|
|
{
|
|
|
|
m_tileLutSize = 1;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
m_tileLutMask = m_tileLutSize - 1;
|
|
|
|
m_tiles = new CompressedTile[m_params.maxTiles];
|
|
|
|
m_posLookup = new CompressedTile[m_tileLutSize];
|
|
|
|
for (int i = m_params.maxTiles - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
m_tiles[i] = new CompressedTile(i);
|
|
|
|
m_tiles[i].next = m_nextFreeTile;
|
|
|
|
m_nextFreeTile = m_tiles[i];
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
m_tileBits = Ilog2(NextPow2(m_params.maxTiles));
|
2023-03-16 19:48:49 +03:00
|
|
|
m_saltBits = Math.Min(31, 32 - m_tileBits);
|
|
|
|
if (m_saltBits < 10)
|
|
|
|
{
|
|
|
|
throw new Exception("Too few salt bits: " + m_saltBits);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public CompressedTile GetTileByRef(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (refs == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
int tileIndex = DecodeTileIdTile(refs);
|
|
|
|
int tileSalt = DecodeTileIdSalt(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (tileIndex >= m_params.maxTiles)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompressedTile tile = m_tiles[tileIndex];
|
|
|
|
if (tile.salt != tileSalt)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tile;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public List<long> GetTilesAt(int tx, int ty)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
List<long> tiles = new List<long>();
|
|
|
|
|
|
|
|
// Find tile based on hash.
|
2023-05-05 02:44:48 +03:00
|
|
|
int h = NavMesh.ComputeTileHash(tx, ty, m_tileLutMask);
|
2023-03-16 19:48:49 +03:00
|
|
|
CompressedTile tile = m_posLookup[h];
|
|
|
|
while (tile != null)
|
|
|
|
{
|
|
|
|
if (tile.header != null && tile.header.tx == tx && tile.header.ty == ty)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
tiles.Add(GetTileRef(tile));
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tile = tile.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tiles;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
CompressedTile GetTileAt(int tx, int ty, int tlayer)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
// Find tile based on hash.
|
2023-05-05 02:44:48 +03:00
|
|
|
int h = NavMesh.ComputeTileHash(tx, ty, m_tileLutMask);
|
2023-03-16 19:48:49 +03:00
|
|
|
CompressedTile tile = m_posLookup[h];
|
|
|
|
while (tile != null)
|
|
|
|
{
|
|
|
|
if (tile.header != null && tile.header.tx == tx && tile.header.ty == ty && tile.header.tlayer == tlayer)
|
|
|
|
{
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
tile = tile.next;
|
|
|
|
}
|
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public long GetTileRef(CompressedTile tile)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (tile == null)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int it = tile.index;
|
2023-05-05 02:44:48 +03:00
|
|
|
return EncodeTileId(tile.salt, it);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public long GetObstacleRef(TileCacheObstacle ob)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (ob == null)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = ob.index;
|
2023-05-05 02:44:48 +03:00
|
|
|
return EncodeObstacleId(ob.salt, idx);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public TileCacheObstacle GetObstacleByRef(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (refs == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
int idx = DecodeObstacleIdObstacle(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (idx >= m_obstacles.Count)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
TileCacheObstacle ob = m_obstacles[idx];
|
2023-05-05 02:44:48 +03:00
|
|
|
int salt = DecodeObstacleIdSalt(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (ob.salt != salt)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ob;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public long AddTile(byte[] data, int flags)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
// Make sure the data is in right format.
|
|
|
|
ByteBuffer buf = new ByteBuffer(data);
|
2023-05-05 02:44:48 +03:00
|
|
|
buf.Order(m_storageParams.byteOrder);
|
|
|
|
TileCacheLayerHeader header = tileReader.Read(buf, m_storageParams.cCompatibility);
|
2023-03-16 19:48:49 +03:00
|
|
|
// Make sure the location is free.
|
2023-05-05 02:44:48 +03:00
|
|
|
if (GetTileAt(header.tx, header.ty, header.tlayer) != null)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a tile.
|
|
|
|
CompressedTile tile = null;
|
|
|
|
if (m_nextFreeTile != null)
|
|
|
|
{
|
|
|
|
tile = m_nextFreeTile;
|
|
|
|
m_nextFreeTile = tile.next;
|
|
|
|
tile.next = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we could allocate a tile.
|
|
|
|
if (tile == null)
|
|
|
|
{
|
|
|
|
throw new Exception("Out of storage");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert tile into the position lut.
|
2023-05-05 02:44:48 +03:00
|
|
|
int h = NavMesh.ComputeTileHash(header.tx, header.ty, m_tileLutMask);
|
2023-03-16 19:48:49 +03:00
|
|
|
tile.next = m_posLookup[h];
|
|
|
|
m_posLookup[h] = tile;
|
|
|
|
|
|
|
|
// Init tile.
|
|
|
|
tile.header = header;
|
|
|
|
tile.data = data;
|
2023-05-05 02:44:48 +03:00
|
|
|
tile.compressed = Align4(buf.Position());
|
2023-03-16 19:48:49 +03:00
|
|
|
tile.flags = flags;
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
return GetTileRef(tile);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private int Align4(int i)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return (i + 3) & (~3);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public void RemoveTile(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (refs == 0)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid tile ref");
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
int tileIndex = DecodeTileIdTile(refs);
|
|
|
|
int tileSalt = DecodeTileIdSalt(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (tileIndex >= m_params.maxTiles)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid tile index");
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
CompressedTile tile = m_tiles[tileIndex];
|
|
|
|
if (tile.salt != tileSalt)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid tile salt");
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Remove tile from hash lookup.
|
2023-05-05 02:44:48 +03:00
|
|
|
int h = NavMesh.ComputeTileHash(tile.header.tx, tile.header.ty, m_tileLutMask);
|
2023-03-16 19:48:49 +03:00
|
|
|
CompressedTile prev = null;
|
|
|
|
CompressedTile cur = m_posLookup[h];
|
|
|
|
while (cur != null)
|
|
|
|
{
|
|
|
|
if (cur == tile)
|
|
|
|
{
|
|
|
|
if (prev != null)
|
|
|
|
{
|
|
|
|
prev.next = cur.next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_posLookup[h] = cur.next;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
break;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
prev = cur;
|
|
|
|
cur = cur.next;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
tile.header = null;
|
|
|
|
tile.data = null;
|
|
|
|
tile.compressed = 0;
|
|
|
|
tile.flags = 0;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Update salt, salt should never be zero.
|
|
|
|
tile.salt = (tile.salt + 1) & ((1 << m_saltBits) - 1);
|
|
|
|
if (tile.salt == 0)
|
|
|
|
{
|
|
|
|
tile.salt++;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Add to free list.
|
|
|
|
tile.next = m_nextFreeTile;
|
|
|
|
m_nextFreeTile = tile;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Cylinder obstacle
|
2023-05-05 02:44:48 +03:00
|
|
|
public long AddObstacle(Vector3f pos, float radius, float height)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
TileCacheObstacle ob = AllocObstacle();
|
2023-03-16 19:48:49 +03:00
|
|
|
ob.type = TileCacheObstacle.TileCacheObstacleType.CYLINDER;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-04-12 17:53:28 +03:00
|
|
|
ob.pos = pos;
|
2023-03-16 19:48:49 +03:00
|
|
|
ob.radius = radius;
|
|
|
|
ob.height = height;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
return AddObstacleRequest(ob).refs;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Aabb obstacle
|
2023-05-05 02:44:48 +03:00
|
|
|
public long AddBoxObstacle(Vector3f bmin, Vector3f bmax)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
TileCacheObstacle ob = AllocObstacle();
|
2023-03-16 19:48:49 +03:00
|
|
|
ob.type = TileCacheObstacle.TileCacheObstacleType.BOX;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-04-22 06:27:52 +03:00
|
|
|
ob.bmin = bmin;
|
|
|
|
ob.bmax = bmax;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
return AddObstacleRequest(ob).refs;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Box obstacle: can be rotated in Y
|
2023-05-05 02:44:48 +03:00
|
|
|
public long AddBoxObstacle(Vector3f center, Vector3f extents, float yRadians)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
TileCacheObstacle ob = AllocObstacle();
|
2023-03-16 19:48:49 +03:00
|
|
|
ob.type = TileCacheObstacle.TileCacheObstacleType.ORIENTED_BOX;
|
2023-04-12 17:53:28 +03:00
|
|
|
ob.center = center;
|
|
|
|
ob.extents = extents;
|
2023-03-16 19:48:49 +03:00
|
|
|
float coshalf = (float)Math.Cos(0.5f * yRadians);
|
|
|
|
float sinhalf = (float)Math.Sin(-0.5f * yRadians);
|
|
|
|
ob.rotAux[0] = coshalf * sinhalf;
|
|
|
|
ob.rotAux[1] = coshalf * coshalf - 0.5f;
|
2023-05-05 02:44:48 +03:00
|
|
|
return AddObstacleRequest(ob).refs;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private ObstacleRequest AddObstacleRequest(TileCacheObstacle ob)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
ObstacleRequest req = new ObstacleRequest();
|
|
|
|
req.action = ObstacleRequestAction.REQUEST_ADD;
|
2023-05-05 02:44:48 +03:00
|
|
|
req.refs = GetObstacleRef(ob);
|
2023-03-16 19:48:49 +03:00
|
|
|
m_reqs.Add(req);
|
|
|
|
return req;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public void RemoveObstacle(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (refs == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
ObstacleRequest req = new ObstacleRequest();
|
|
|
|
req.action = ObstacleRequestAction.REQUEST_REMOVE;
|
|
|
|
req.refs = refs;
|
|
|
|
m_reqs.Add(req);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private TileCacheObstacle AllocObstacle()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
TileCacheObstacle o = m_nextFreeObstacle;
|
|
|
|
if (o == null)
|
|
|
|
{
|
|
|
|
o = new TileCacheObstacle(m_obstacles.Count);
|
|
|
|
m_obstacles.Add(o);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_nextFreeObstacle = o.next;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
o.state = ObstacleState.DT_OBSTACLE_PROCESSING;
|
|
|
|
o.touched.Clear();
|
|
|
|
o.pending.Clear();
|
|
|
|
o.next = null;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
List<long> QueryTiles(Vector3f bmin, Vector3f bmax)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
List<long> results = new List<long>();
|
|
|
|
float tw = m_params.width * m_params.cs;
|
|
|
|
float th = m_params.height * m_params.cs;
|
2023-04-29 07:18:27 +03:00
|
|
|
int tx0 = (int)Math.Floor((bmin.x - m_params.orig.x) / tw);
|
|
|
|
int tx1 = (int)Math.Floor((bmax.x - m_params.orig.x) / tw);
|
|
|
|
int ty0 = (int)Math.Floor((bmin.z - m_params.orig.z) / th);
|
|
|
|
int ty1 = (int)Math.Floor((bmax.z - m_params.orig.z) / th);
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int ty = ty0; ty <= ty1; ++ty)
|
|
|
|
{
|
|
|
|
for (int tx = tx0; tx <= tx1; ++tx)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
List<long> tiles = GetTilesAt(tx, ty);
|
2023-03-16 19:48:49 +03:00
|
|
|
foreach (long i in tiles)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
CompressedTile tile = m_tiles[DecodeTileIdTile(i)];
|
2023-03-28 18:03:33 +03:00
|
|
|
Vector3f tbmin = new Vector3f();
|
|
|
|
Vector3f tbmax = new Vector3f();
|
2023-05-05 02:44:48 +03:00
|
|
|
CalcTightTileBounds(tile.header, ref tbmin, ref tbmax);
|
|
|
|
if (OverlapBounds(bmin, bmax, tbmin, tbmax))
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
results.Add(i);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
return results;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
/**
|
2023-03-14 08:02:43 +03:00
|
|
|
* Updates the tile cache by rebuilding tiles touched by unfinished obstacle requests.
|
|
|
|
*
|
|
|
|
* @return Returns true if the tile cache is fully up to date with obstacle requests and tile rebuilds. If the tile
|
|
|
|
* cache is up to date another (immediate) call to update will have no effect; otherwise another call will
|
|
|
|
* continue processing obstacle requests and tile rebuilds.
|
|
|
|
*/
|
2023-05-05 02:44:48 +03:00
|
|
|
public bool Update()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (0 == m_update.Count)
|
|
|
|
{
|
|
|
|
// Process requests.
|
|
|
|
foreach (ObstacleRequest req in m_reqs)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
int idx = DecodeObstacleIdObstacle(req.refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (idx >= m_obstacles.Count)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TileCacheObstacle ob = m_obstacles[idx];
|
2023-05-05 02:44:48 +03:00
|
|
|
int salt = DecodeObstacleIdSalt(req.refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (ob.salt != salt)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.action == ObstacleRequestAction.REQUEST_ADD)
|
|
|
|
{
|
|
|
|
// Find touched tiles.
|
2023-03-28 18:03:33 +03:00
|
|
|
Vector3f bmin = new Vector3f();
|
|
|
|
Vector3f bmax = new Vector3f();
|
2023-05-05 02:44:48 +03:00
|
|
|
GetObstacleBounds(ob, ref bmin, ref bmax);
|
|
|
|
ob.touched = QueryTiles(bmin, bmax);
|
2023-03-16 19:48:49 +03:00
|
|
|
// Add tiles to update list.
|
|
|
|
ob.pending.Clear();
|
|
|
|
foreach (long j in ob.touched)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
if (!Contains(m_update, j))
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
m_update.Add(j);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
ob.pending.Add(j);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
else if (req.action == ObstacleRequestAction.REQUEST_REMOVE)
|
|
|
|
{
|
|
|
|
// Prepare to remove obstacle.
|
|
|
|
ob.state = ObstacleState.DT_OBSTACLE_REMOVING;
|
|
|
|
// Add tiles to update list.
|
|
|
|
ob.pending.Clear();
|
|
|
|
foreach (long j in ob.touched)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
if (!Contains(m_update, j))
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
m_update.Add(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.pending.Add(j);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
m_reqs.Clear();
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
// Process updates
|
|
|
|
if (0 < m_update.Count)
|
|
|
|
{
|
|
|
|
long refs = m_update[0];
|
|
|
|
m_update.RemoveAt(0);
|
|
|
|
// Build mesh
|
2023-05-05 02:44:48 +03:00
|
|
|
BuildNavMeshTile(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
// Update obstacle states.
|
|
|
|
for (int i = 0; i < m_obstacles.Count; ++i)
|
|
|
|
{
|
|
|
|
TileCacheObstacle ob = m_obstacles[i];
|
|
|
|
if (ob.state == ObstacleState.DT_OBSTACLE_PROCESSING
|
|
|
|
|| ob.state == ObstacleState.DT_OBSTACLE_REMOVING)
|
|
|
|
{
|
|
|
|
// Remove handled tile from pending list.
|
|
|
|
ob.pending.Remove(refs);
|
|
|
|
|
|
|
|
// If all pending tiles processed, change state.
|
|
|
|
if (0 == ob.pending.Count)
|
|
|
|
{
|
|
|
|
if (ob.state == ObstacleState.DT_OBSTACLE_PROCESSING)
|
|
|
|
{
|
|
|
|
ob.state = ObstacleState.DT_OBSTACLE_PROCESSED;
|
|
|
|
}
|
|
|
|
else if (ob.state == ObstacleState.DT_OBSTACLE_REMOVING)
|
|
|
|
{
|
|
|
|
ob.state = ObstacleState.DT_OBSTACLE_EMPTY;
|
|
|
|
// Update salt, salt should never be zero.
|
|
|
|
ob.salt = (ob.salt + 1) & ((1 << 16) - 1);
|
|
|
|
if (ob.salt == 0)
|
|
|
|
{
|
|
|
|
ob.salt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return obstacle to free list.
|
|
|
|
ob.next = m_nextFreeObstacle;
|
|
|
|
m_nextFreeObstacle = ob;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
return 0 == m_update.Count && 0 == m_reqs.Count;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public void BuildNavMeshTile(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
int idx = DecodeTileIdTile(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (idx > m_params.maxTiles)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid tile index");
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
CompressedTile tile = m_tiles[idx];
|
2023-05-05 02:44:48 +03:00
|
|
|
int salt = DecodeTileIdSalt(refs);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (tile.salt != salt)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid tile salt");
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
int walkableClimbVx = (int)(m_params.walkableClimb / m_params.ch);
|
|
|
|
|
|
|
|
// Decompress tile layer data.
|
2023-05-05 02:44:48 +03:00
|
|
|
TileCacheLayer layer = DecompressTile(tile);
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
// Rasterize obstacles.
|
|
|
|
for (int i = 0; i < m_obstacles.Count; ++i)
|
|
|
|
{
|
|
|
|
TileCacheObstacle ob = m_obstacles[i];
|
|
|
|
if (ob.state == ObstacleState.DT_OBSTACLE_EMPTY || ob.state == ObstacleState.DT_OBSTACLE_REMOVING)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
if (Contains(ob.touched, refs))
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (ob.type == TileCacheObstacle.TileCacheObstacleType.CYLINDER)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
builder.MarkCylinderArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.pos, ob.radius, ob.height, 0);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else if (ob.type == TileCacheObstacle.TileCacheObstacleType.BOX)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
builder.MarkBoxArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.bmin, ob.bmax, 0);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else if (ob.type == TileCacheObstacle.TileCacheObstacleType.ORIENTED_BOX)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
builder.MarkBoxArea(layer, tile.header.bmin, m_params.cs, m_params.ch, ob.center, ob.extents, ob.rotAux, 0);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
|
|
|
// Build navmesh
|
2023-05-05 02:44:48 +03:00
|
|
|
builder.BuildTileCacheRegions(layer, walkableClimbVx);
|
|
|
|
TileCacheContourSet lcset = builder.BuildTileCacheContours(layer, walkableClimbVx,
|
2023-03-14 08:02:43 +03:00
|
|
|
m_params.maxSimplificationError);
|
2023-05-05 02:44:48 +03:00
|
|
|
TileCachePolyMesh polyMesh = builder.BuildTileCachePolyMesh(lcset, m_navmesh.GetMaxVertsPerPoly());
|
2023-03-16 19:48:49 +03:00
|
|
|
// Early out if the mesh tile is empty.
|
|
|
|
if (polyMesh.npolys == 0)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
m_navmesh.RemoveTile(m_navmesh.GetTileRefAt(tile.header.tx, tile.header.ty, tile.header.tlayer));
|
2023-03-16 19:48:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NavMeshDataCreateParams option = new NavMeshDataCreateParams();
|
|
|
|
option.verts = polyMesh.verts;
|
|
|
|
option.vertCount = polyMesh.nverts;
|
|
|
|
option.polys = polyMesh.polys;
|
|
|
|
option.polyAreas = polyMesh.areas;
|
|
|
|
option.polyFlags = polyMesh.flags;
|
|
|
|
option.polyCount = polyMesh.npolys;
|
2023-05-05 02:44:48 +03:00
|
|
|
option.nvp = m_navmesh.GetMaxVertsPerPoly();
|
2023-03-16 19:48:49 +03:00
|
|
|
option.walkableHeight = m_params.walkableHeight;
|
|
|
|
option.walkableRadius = m_params.walkableRadius;
|
|
|
|
option.walkableClimb = m_params.walkableClimb;
|
|
|
|
option.tileX = tile.header.tx;
|
|
|
|
option.tileZ = tile.header.ty;
|
|
|
|
option.tileLayer = tile.header.tlayer;
|
|
|
|
option.cs = m_params.cs;
|
|
|
|
option.ch = m_params.ch;
|
|
|
|
option.buildBvTree = false;
|
|
|
|
option.bmin = tile.header.bmin;
|
|
|
|
option.bmax = tile.header.bmax;
|
|
|
|
if (m_tmproc != null)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
m_tmproc.Process(option);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
MeshData meshData = NavMeshBuilder.CreateNavMeshData(option);
|
2023-03-16 19:48:49 +03:00
|
|
|
// Remove existing tile.
|
2023-05-05 02:44:48 +03:00
|
|
|
m_navmesh.RemoveTile(m_navmesh.GetTileRefAt(tile.header.tx, tile.header.ty, tile.header.tlayer));
|
2023-03-16 19:48:49 +03:00
|
|
|
// Add new tile, or leave the location empty. if (navData) { // Let the
|
|
|
|
if (meshData != null)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
m_navmesh.AddTile(meshData, 0, 0);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public TileCacheLayer DecompressTile(CompressedTile tile)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
TileCacheLayer layer = builder.DecompressTileCacheLayer(m_tcomp, tile.data, m_storageParams.byteOrder,
|
2023-03-14 08:02:43 +03:00
|
|
|
m_storageParams.cCompatibility);
|
2023-03-16 19:48:49 +03:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
void CalcTightTileBounds(TileCacheLayerHeader header, ref Vector3f bmin, ref Vector3f bmax)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
float cs = m_params.cs;
|
2023-04-29 07:18:27 +03:00
|
|
|
bmin.x = header.bmin.x + header.minx * cs;
|
|
|
|
bmin.y = header.bmin.y;
|
|
|
|
bmin.z = header.bmin.z + header.miny * cs;
|
|
|
|
bmax.x = header.bmin.x + (header.maxx + 1) * cs;
|
|
|
|
bmax.y = header.bmax.y;
|
|
|
|
bmax.z = header.bmin.z + (header.maxy + 1) * cs;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
void GetObstacleBounds(TileCacheObstacle ob, ref Vector3f bmin, ref Vector3f bmax)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (ob.type == TileCacheObstacle.TileCacheObstacleType.CYLINDER)
|
|
|
|
{
|
2023-04-29 07:18:27 +03:00
|
|
|
bmin.x = ob.pos.x - ob.radius;
|
|
|
|
bmin.y = ob.pos.y;
|
|
|
|
bmin.z = ob.pos.z - ob.radius;
|
|
|
|
bmax.x = ob.pos.x + ob.radius;
|
|
|
|
bmax.y = ob.pos.y + ob.height;
|
|
|
|
bmax.z = ob.pos.z + ob.radius;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else if (ob.type == TileCacheObstacle.TileCacheObstacleType.BOX)
|
|
|
|
{
|
2023-04-12 17:53:28 +03:00
|
|
|
bmin = ob.bmin;
|
|
|
|
bmax = ob.bmax;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else if (ob.type == TileCacheObstacle.TileCacheObstacleType.ORIENTED_BOX)
|
|
|
|
{
|
2023-04-29 07:18:27 +03:00
|
|
|
float maxr = 1.41f * Math.Max(ob.extents.x, ob.extents.z);
|
|
|
|
bmin.x = ob.center.x - maxr;
|
|
|
|
bmax.x = ob.center.x + maxr;
|
|
|
|
bmin.y = ob.center.y - ob.extents.y;
|
|
|
|
bmax.y = ob.center.y + ob.extents.y;
|
|
|
|
bmin.z = ob.center.z - maxr;
|
|
|
|
bmax.z = ob.center.z + maxr;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public TileCacheParams GetParams()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return m_params;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public TileCacheCompressor GetCompressor()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return m_tcomp;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public int GetTileCount()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return m_params.maxTiles;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public CompressedTile GetTile(int i)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return m_tiles[i];
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public NavMesh GetNavMesh()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
return m_navmesh;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-04-29 07:18:27 +03:00
|
|
|
}
|