Преглед изворни кода

generate graph from coordinates

mesinger пре 6 година
родитељ
комит
eea9cea42e

+ 0 - 3
src/me/hammerle/supersnuvi/Main.java

@@ -3,9 +3,6 @@ package me.hammerle.supersnuvi;
 import me.hammerle.supersnuvi.gamelogic.Level;
 import me.hammerle.supersnuvi.gamelogic.Level;
 import me.hammerle.supersnuvi.gamelogic.pathfinding.LevelGraphBuilder;
 import me.hammerle.supersnuvi.gamelogic.pathfinding.LevelGraphBuilder;
 
 
-import java.util.List;
-import java.util.Map;
-
 public class Main
 public class Main
 {
 {
     public static void main(String[] args)
     public static void main(String[] args)

+ 1 - 1
src/me/hammerle/supersnuvi/gamelogic/pathfinding/IGraph.java

@@ -7,5 +7,5 @@ import java.util.Set;
  */
  */
 public interface IGraph {
 public interface IGraph {
 
 
-    Set<? extends INode> getNodes();
+    Set<LevelGraphNode> getNodes();
 }
 }

+ 5 - 5
src/me/hammerle/supersnuvi/gamelogic/pathfinding/LevelGraph.java

@@ -9,7 +9,7 @@ import java.util.Set;
  */
  */
 public class LevelGraph implements IGraph, IModifieableGraph {
 public class LevelGraph implements IGraph, IModifieableGraph {
 
 
-    private Set<PathNode> nodes;
+    private Set<LevelGraphNode> nodes;
 
 
     public LevelGraph(){
     public LevelGraph(){
 
 
@@ -18,21 +18,21 @@ public class LevelGraph implements IGraph, IModifieableGraph {
 
 
 
 
     @Override
     @Override
-    public Set<PathNode> getNodes() {
+    public Set<LevelGraphNode> getNodes() {
         return nodes;
         return nodes;
     }
     }
 
 
     @Override
     @Override
     public void addNode(INode node) {
     public void addNode(INode node) {
 
 
-        if(node instanceof PathNode)
-            nodes.add((PathNode) node);
+        if(node instanceof LevelGraphNode)
+            nodes.add((LevelGraphNode) node);
     }
     }
 
 
     @Override
     @Override
     public void removeNode(INode node) {
     public void removeNode(INode node) {
 
 
-        if(node instanceof PathNode)
+        if(node instanceof LevelGraphNode)
             nodes.remove(node);
             nodes.remove(node);
     }
     }
 }
 }

+ 54 - 6
src/me/hammerle/supersnuvi/gamelogic/pathfinding/LevelGraphBuilder.java

@@ -1,7 +1,6 @@
 package me.hammerle.supersnuvi.gamelogic.pathfinding;
 package me.hammerle.supersnuvi.gamelogic.pathfinding;
 
 
 import me.hammerle.supersnuvi.Game;
 import me.hammerle.supersnuvi.Game;
-import me.hammerle.supersnuvi.gamelogic.ILevel;
 import me.hammerle.supersnuvi.gamelogic.Level;
 import me.hammerle.supersnuvi.gamelogic.Level;
 import me.hammerle.supersnuvi.gamelogic.LevelData;
 import me.hammerle.supersnuvi.gamelogic.LevelData;
 import me.hammerle.supersnuvi.tiles.Tile;
 import me.hammerle.supersnuvi.tiles.Tile;
@@ -15,6 +14,13 @@ public class LevelGraphBuilder {
     public 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){
     public LevelGraph getLevelAsGraph(Level lvl){
 
 
         if(lvl == null) return null;
         if(lvl == null) return null;
@@ -27,11 +33,39 @@ public class LevelGraphBuilder {
 
 
         if(collisionTileCoordinates.size() == 0) return null;
         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;
         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
      * @param collisionTileCoordinates
      * @return list of integer pairs
      * @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<>();
         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()){
         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);
             row.remove(0);
         }
         }
 
 
+        if(row.size() == 0) return result;
+
         int curr = result.pop();
         int curr = result.pop();
         if(row.get(0) < curr - 1){
         if(row.get(0) < curr - 1){
 
 

+ 2 - 2
src/me/hammerle/supersnuvi/gamelogic/pathfinding/PathEdge.java → src/me/hammerle/supersnuvi/gamelogic/pathfinding/LevelGraphEdge.java

@@ -4,11 +4,11 @@ package me.hammerle.supersnuvi.gamelogic.pathfinding;
  * edge of the graph, which represents the level layout
  * edge of the graph, which represents the level layout
  * used for pathfinding
  * used for pathfinding
  */
  */
-public class PathEdge extends SimpleEdge implements IDirectedEdge {
+public class LevelGraphEdge extends SimpleEdge implements IDirectedEdge {
 
 
     protected int direction;
     protected int direction;
 
 
-    public PathEdge(PathNode node1, PathNode node2, double weight, int direction) {
+    public LevelGraphEdge(LevelGraphNode node1, LevelGraphNode node2, double weight, int direction) {
         super(node1, node2, weight);
         super(node1, node2, weight);
 
 
         this.direction = direction;
         this.direction = direction;

+ 52 - 0
src/me/hammerle/supersnuvi/gamelogic/pathfinding/LevelGraphNode.java

@@ -0,0 +1,52 @@
+package me.hammerle.supersnuvi.gamelogic.pathfinding;
+
+import me.hammerle.supersnuvi.util.Pair;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * node of the graph, which represents the level layout
+ * used for pathfinding
+ */
+public class LevelGraphNode implements INode, IModifieableNode{
+
+    private Pair<Integer, Integer> levelCoordinates;
+    private Set<LevelGraphEdge> edges;
+
+    public LevelGraphNode(int x, int y) {
+
+        levelCoordinates = new Pair<>(x, y);
+        edges = new HashSet<>();
+    }
+
+    public LevelGraphNode(Pair<Integer, Integer> levelCoordinates){
+        this.levelCoordinates = levelCoordinates;
+        edges = new HashSet<>();
+    }
+
+    @Override
+    public void addEdge(IEdge edge) {
+
+        if(edge instanceof LevelGraphEdge)
+            edges.add((LevelGraphEdge) edge);
+    }
+
+    @Override
+    public void removeEdge(IEdge edge) {
+
+        if(edge instanceof LevelGraphEdge)
+            edges.remove(edge);
+    }
+
+    @Override
+    public Set<LevelGraphEdge> getEdges() {
+
+        return edges;
+    }
+
+    @Override
+    public String toString() {
+        return "(" + Integer.toString(levelCoordinates.getLhs()) + ", " + Integer.toString(levelCoordinates.getRhs()) + ")";
+    }
+}

+ 0 - 43
src/me/hammerle/supersnuvi/gamelogic/pathfinding/PathNode.java

@@ -1,43 +0,0 @@
-package me.hammerle.supersnuvi.gamelogic.pathfinding;
-
-import me.hammerle.supersnuvi.util.Pair;
-
-import java.util.HashSet;
-import java.util.Set;
-
-/**
- * node of the graph, which represents the level layout
- * used for pathfinding
- */
-public class PathNode implements INode, IModifieableNode{
-
-    private Pair<Integer, Integer> levelCoordinates;
-    private Set<PathEdge> edges;
-
-    public PathNode(int x, int y) {
-
-        levelCoordinates = new Pair<>(x, y);
-        edges = new HashSet<>();
-    }
-
-
-    @Override
-    public void addEdge(IEdge edge) {
-
-        if(edge instanceof PathEdge)
-            edges.add((PathEdge) edge);
-    }
-
-    @Override
-    public void removeEdge(IEdge edge) {
-
-        if(edge instanceof PathEdge)
-            edges.remove(edge);
-    }
-
-    @Override
-    public Set<PathEdge> getEdges() {
-
-        return edges;
-    }
-}

+ 3 - 3
src/me/hammerle/supersnuvi/gamelogic/pathfinding/SimpleEdge.java

@@ -7,11 +7,11 @@ package me.hammerle.supersnuvi.gamelogic.pathfinding;
 */
 */
 public class SimpleEdge implements IEdge {
 public class SimpleEdge implements IEdge {
 
 
-    protected PathNode node1;
-    protected PathNode node2;
+    protected LevelGraphNode node1;
+    protected LevelGraphNode node2;
     protected double weight;
     protected double weight;
 
 
-    public SimpleEdge(PathNode node1, PathNode node2, double weight) {
+    public SimpleEdge(LevelGraphNode node1, LevelGraphNode node2, double weight) {
         this.node1 = node1;
         this.node1 = node1;
         this.node2 = node2;
         this.node2 = node2;
         this.weight = weight;
         this.weight = weight;