|
@@ -1,7 +1,6 @@
|
|
|
package me.hammerle.supersnuvi.gamelogic.pathfinding;
|
|
|
|
|
|
import me.hammerle.supersnuvi.Game;
|
|
|
-import me.hammerle.supersnuvi.gamelogic.ILevel;
|
|
|
import me.hammerle.supersnuvi.gamelogic.Level;
|
|
|
import me.hammerle.supersnuvi.gamelogic.LevelData;
|
|
|
import me.hammerle.supersnuvi.tiles.Tile;
|
|
@@ -15,6 +14,13 @@ public class LevelGraphBuilder {
|
|
|
public LevelGraphBuilder() {
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * returns lvl as graph representation,
|
|
|
+ * which can be used for ai pathfinding
|
|
|
+ *
|
|
|
+ * @param lvl
|
|
|
+ * @return graph of lvl
|
|
|
+ */
|
|
|
public LevelGraph getLevelAsGraph(Level lvl){
|
|
|
|
|
|
if(lvl == null) return null;
|
|
@@ -27,11 +33,39 @@ public class LevelGraphBuilder {
|
|
|
|
|
|
if(collisionTileCoordinates.size() == 0) return null;
|
|
|
|
|
|
- List<Pair<Integer, Integer>> graphCoordinates = getListOfGraphCoordinates(collisionTileCoordinates);
|
|
|
+ NavigableMap<Integer, List<Integer>> graphCoordinates = getListOfGraphCoordinates(collisionTileCoordinates);
|
|
|
|
|
|
if(graphCoordinates.size() == 0) return null;
|
|
|
|
|
|
- return null;
|
|
|
+ LevelGraph graph = new LevelGraph();
|
|
|
+
|
|
|
+ for(final int x : graphCoordinates.keySet()){
|
|
|
+
|
|
|
+ for(final int y : graphCoordinates.get(x)){
|
|
|
+
|
|
|
+ LevelGraphNode curr = new LevelGraphNode(x, y);
|
|
|
+ graph.addNode(curr);
|
|
|
+
|
|
|
+ int next_x = graphCoordinates.higherKey(x);
|
|
|
+
|
|
|
+ for(final int next_y : graphCoordinates.getOrDefault(next_x, new ArrayList<Integer>())){
|
|
|
+
|
|
|
+ LevelGraphNode next = new LevelGraphNode(next_x, next_y);
|
|
|
+
|
|
|
+ IEdge edge = new LevelGraphEdge((LevelGraphNode)curr, (LevelGraphNode)next, 1.0, IDirectedEdge.DIRECTION_DUPLEX);
|
|
|
+
|
|
|
+ curr.addEdge(edge);
|
|
|
+ next.addEdge(edge);
|
|
|
+
|
|
|
+ graph.addNode(next);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(x);
|
|
|
+ System.out.println(graphCoordinates.get(x));
|
|
|
+ }
|
|
|
+
|
|
|
+ return graph;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -78,11 +112,11 @@ public class LevelGraphBuilder {
|
|
|
* @param collisionTileCoordinates
|
|
|
* @return list of integer pairs
|
|
|
*/
|
|
|
- private List<Pair<Integer, Integer>> getListOfGraphCoordinates(Map<Integer, List<Integer>> collisionTileCoordinates){
|
|
|
+ private NavigableMap<Integer, List<Integer>> getListOfGraphCoordinates(Map<Integer, List<Integer>> collisionTileCoordinates){
|
|
|
|
|
|
List<Pair<Integer, Integer>> graphCoordinates = new ArrayList<>();
|
|
|
|
|
|
- if(collisionTileCoordinates == null || collisionTileCoordinates.size() <= 0) return graphCoordinates;
|
|
|
+ if(collisionTileCoordinates == null || collisionTileCoordinates.size() <= 0) return new TreeMap<>();
|
|
|
|
|
|
for(int key : collisionTileCoordinates.keySet()){
|
|
|
|
|
@@ -97,8 +131,20 @@ public class LevelGraphBuilder {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ NavigableMap<Integer, List<Integer>> res = new TreeMap<>();
|
|
|
|
|
|
- return graphCoordinates;
|
|
|
+ for(Pair<Integer, Integer> coord : graphCoordinates){
|
|
|
+
|
|
|
+ int x = coord.getLhs();
|
|
|
+ int y = coord.getRhs();
|
|
|
+
|
|
|
+ if(res.getOrDefault(x, null) == null)
|
|
|
+ res.put(x, new ArrayList<>());
|
|
|
+
|
|
|
+ res.get(x).add(y);
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -120,6 +166,8 @@ public class LevelGraphBuilder {
|
|
|
row.remove(0);
|
|
|
}
|
|
|
|
|
|
+ if(row.size() == 0) return result;
|
|
|
+
|
|
|
int curr = result.pop();
|
|
|
if(row.get(0) < curr - 1){
|
|
|
|