我正在尝试从int[][]
创建一个大型2D数组LinkedHashMap
,其中包含一些我正在研究的A * Pathfinder的较小数组。
Pathfinder正在使用的Map
以较小的块流式传输到客户端,并转换为Pathfinder的简化版本。
Map<Coord, int[][]> pfmapcache = new LinkedHashMap<Coord, int[][]>(9, 0.75f, true);
Coord看起来像这样:Coord(0,0)
或Coord(-1,0)
....等等,int[][]
总是int[100][100]
大。
现在我想创建一个新的大int[][]
,它将包含所有较小的数组,其中小数组Coord(0,0)
将位于新的大数组的中心。
int[][] largearray = [-1,1][0,1][1,1]
[-1,0][0,0][1,0]
[-1,-1][0,-1][1,-1]
在这个例子中,大数组将int[300][300]
大。
2.如果将新的小数组添加到pfmapcache
。
int[][] largearray = [][][1,2]
[-1,1][0,1][1,1]
[-1,0][0,0][1,0]
[-1,-1][0,-1][1,-1]
我不必将较小的数组存储在pfmapcache
中我可以添加它们,因为它们是使用2个小数组合等创建的。但是数组的负位置与原始数据相关不知道如何将它们结合起来并保留它们的相对位置。
第一次在这里发帖,如果我需要澄清一些事情,请告诉我。
答案 0 :(得分:0)
您想知道如何将现有的路径寻找器算法与分块地图一起使用。
这时您需要在数据表示和数据使用之间放置abstraction layer(如Landscape
类)。
问:路径查找算法是否需要知道它在网格,分块网格,稀疏矩阵或更奇特的表示上工作?
A:不是。否。探路者只需要知道一件事:'我从哪里可以得到这个?'
你应该通过只使用以下类来放弃任何关于你的世界在网格上的事实:
public interface IdealLandscape {
Map<Point, Integer> neighbours(Point location); // returns all neighbours, and the cost to get there
}
但是我理解你现有的实现'知道'有关网格,附加值是隐含的,你正在使用点作为(x,y)。但是在引入块时丢失了这个,所以使用网格不再起作用了。因此,让我们尽可能轻松地进行抽象。这是计划:
public interface Landscape {
public int getHeight(int x, int y); // Assuming you're storing height in your int[][] map?
}
这非常简单:
只需将map[i][j]
替换为landscape.getHeight(i, j)
使用非常简单的GridLandscape实现,如:
public class GridLandscape implements Landscape {
int[][] map;
public GridLandscape(...){
map = // Build it somehow
}
@Override
public int getHeight(int x, int y){
return map[x][y]; // Maybe check bounds here ?
}
}
现在你的地图被抽象掉了,你知道你的探路者在它上面工作了,你可以用你的地图取代它!
public class ChunkedGridLandscape implements Landscape {
private static final int CHUNK_SIZE = 300;
Map<Coord, int[][]> mapCache = new LinkedHashMap<>(9, 0.75f, true);
Coord centerChunkCoord;
public ChunkedGridLandscape(Map<Coord, int[][]> pfmapcache, Coord centerChunkCoord){
this.mapCache = pfmapcache;
this.centerChunkCoord = centerChunkCoord;
}
@Override
public int getHeight(int x, int y){
// compute chunk coord
int chunkX = x / CHUNK_SIZE - centerChunkCoord .getX();
int chunkX = y / CHUNK_SIZE - centerChunkCoord .getY();
Coord chunkCoord = new Coord(chunkX, chunkY);
// Now retrieve the correct chunk
int[][] chunk = mapCache.get(chunkCoord); // Careful: is the chunk already loaded?
// Now retrieve the height within the chunk
int xInChunk = (x + chunkX*CHUNK_SIZE) % CHUNK_SIZE; // Made positive again!
int yInChunk = (y + chunkY*CHUNK_SIZE) % CHUNK_SIZE; // Made positive again!
// We have everything !
return chunk[xInChunk][yInChunk];
}
}
得知:您的Coord
班级需要正确重载equals
和hashCode
个方法!
这应该立即与您的探路者一起使用。享受!