faster pathfinding. From here, neighbors is slow.
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
[clojure.data.priority-map :refer [priority-map]])
|
[clojure.data.priority-map :refer [priority-map]])
|
||||||
(:import [com.badlogic.gdx.files FileHandle]
|
(:import [com.badlogic.gdx.files FileHandle]
|
||||||
[com.badlogic.gdx Files]
|
[com.badlogic.gdx Files]
|
||||||
|
[java.lang Math]
|
||||||
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
|
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
|
||||||
PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture
|
PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture
|
||||||
VertexAttributes$Usage]))
|
VertexAttributes$Usage]))
|
||||||
@@ -16,63 +17,71 @@
|
|||||||
(doseq [row (take-nth skip my-map)]
|
(doseq [row (take-nth skip my-map)]
|
||||||
(println (take-nth skip (map {1 \space 0 "W" "X" "X" "." "."} row))))))
|
(println (take-nth skip (map {1 \space 0 "W" "X" "X" "." "."} row))))))
|
||||||
|
|
||||||
(defn random-map [] (-> (vec (take (/ 240 4) (repeatedly (fn [] (vec (take (/ 320 4) (repeatedly (fn [] (rand-nth [1 1 1 1 1 5 0])))))))))
|
(defn random-map [] (-> (vec (take (/ 240 4) (repeatedly (fn [] (vec (take (/ 320 4) (repeatedly (fn [] (rand-nth [1 1 1 5 5 0])))))))))
|
||||||
(update-in [1 1] (constantly 1))
|
(update-in [1 1] (constantly 1))
|
||||||
(update-in [50 50] (constantly 1))))
|
(update-in [50 50] (constantly 1))))
|
||||||
|
|
||||||
|
|
||||||
(defn neighbors [[x y] my-map]
|
(defn neighbors [[^long x ^long y] my-map]
|
||||||
(let [candidates [[(dec x) (dec y)] [x (dec y)] [(inc x) (dec y)]
|
(let [candidates [[^long (dec x) ^long (dec y)] [x ^long (dec y)] [^long (inc x) ^long (dec y)]
|
||||||
[(dec x) y] [(inc x) y]
|
[^long (dec x) y] [^long (inc x) y]
|
||||||
[(dec x) (inc y)] [x (inc y)] [(inc x) (inc y)]]]
|
[^long (dec x) ^long (inc y)] [x ^long (inc y)] [^long (inc x) ^long (inc y)]]
|
||||||
(remove #(= 0 (get-in my-map (reverse %)))
|
width (count (first my-map))
|
||||||
(filter (fn [[x y]] (and (< -1 x (count (first my-map)))
|
height (count my-map)]
|
||||||
(< -1 y (count my-map)))) candidates))))
|
(vec (remove #(= 0 (get-in my-map (reverse %)))
|
||||||
|
(filter (fn [[x y]] (and (< -1 x width)
|
||||||
|
(< -1 y height))) candidates)))))
|
||||||
|
|
||||||
|
|
||||||
(defn resolve-path [came-from play-loc target-loc]
|
(defn resolve-path [came-from play-loc target-loc]
|
||||||
(if (nil? (came-from target-loc))
|
(let [came-from (into {} came-from)]
|
||||||
nil
|
|
||||||
(loop [path []
|
|
||||||
current-node target-loc]
|
|
||||||
(if (or (= current-node play-loc)
|
|
||||||
(nil? current-node))
|
|
||||||
(reverse (map (fn [[x y]] [x y]) (conj path current-node)))
|
|
||||||
(recur
|
|
||||||
(conj path current-node)
|
|
||||||
(came-from current-node))))))
|
|
||||||
|
|
||||||
(defn heuristic [[goal-x goal-y] [current-x current-y]]
|
|
||||||
(let [dist-x (Math/abs (- goal-x current-x ))
|
(if (nil? (came-from target-loc))
|
||||||
dist-y (Math/abs (- goal-y current-y))
|
nil
|
||||||
d2 (Math/sqrt 2)]
|
(loop [path []
|
||||||
(+ dist-x dist-y (* (- d2 1)
|
current-node (vec target-loc)]
|
||||||
(min dist-x dist-y)))))
|
(if (or (= current-node play-loc)
|
||||||
|
(nil? current-node))
|
||||||
|
(reverse (map (fn [[x y]] [x y]) (conj path current-node)))
|
||||||
|
(recur
|
||||||
|
(conj path current-node)
|
||||||
|
(came-from (vec current-node))))))))
|
||||||
|
|
||||||
|
(def d2 ^double (- (Math/sqrt 2) 1))
|
||||||
|
(defn heuristic [^long goal-x ^long goal-y ^long current-x ^long current-y]
|
||||||
|
(let [dist-x ^long (if (< goal-x current-x)
|
||||||
|
^long (- current-x goal-x)
|
||||||
|
^long (- goal-x current-x ))
|
||||||
|
dist-y ^long (if (< goal-y current-y)
|
||||||
|
^long (- current-y goal-y)
|
||||||
|
^long (- goal-y current-y))]
|
||||||
|
^long (+ dist-x dist-y (* ^double d2
|
||||||
|
^long (min dist-x dist-y)))))
|
||||||
|
|
||||||
(defn visit-all [my-map play-loc target-loc]
|
(defn visit-all [my-map play-loc target-loc]
|
||||||
(if (= 0 (get-in my-map (reverse target-loc)))
|
(let [play-loc (vec play-loc)
|
||||||
nil
|
target-log (vec target-loc)]
|
||||||
(loop [cost-so-far {play-loc 0}
|
(if (= 0 (get-in my-map (reverse target-loc)))
|
||||||
came-from {}
|
nil
|
||||||
fronteir (priority-map play-loc 0)]
|
(let [cost-so-far ^java.util.HashMap (java.util.HashMap. {play-loc 0})
|
||||||
(let [current-loc (first (keys fronteir))]
|
came-from ^java.util.HashMap (java.util.HashMap.)
|
||||||
(if (or (empty? fronteir)
|
fronteir ^java.util.PriorityQueue (java.util.PriorityQueue. 100 (comparator (fn [a b] (< (:cost a) (:cost b)))))]
|
||||||
(= current-loc target-loc))
|
(.offer fronteir {:cost 0 :loc play-loc})
|
||||||
(resolve-path came-from play-loc target-loc)
|
(loop [current-loc (.poll fronteir)]
|
||||||
(let [neighbors (neighbors current-loc my-map)
|
(if (or (nil? current-loc)
|
||||||
[cost-so-far came-from fronteir] (reduce (fn [[cost-so-far came-from fronteir] neighbor]
|
(= (:loc current-loc) target-loc))
|
||||||
(let [new-cost (+ (cost-so-far current-loc) (get-in my-map (reverse neighbor)))]
|
(resolve-path came-from play-loc target-loc)
|
||||||
(if (or (nil? (cost-so-far neighbor))
|
(do (doseq [neighbor (neighbors (:loc current-loc) my-map)]
|
||||||
(< new-cost (cost-so-far neighbor)))
|
(let [cost-for-neighbor (.get cost-so-far neighbor)
|
||||||
[(assoc cost-so-far neighbor new-cost)
|
new-cost (+ (.get cost-so-far (:loc current-loc)) (get-in my-map (reverse neighbor)))]
|
||||||
(assoc came-from neighbor current-loc)
|
(when (or (nil? cost-for-neighbor)
|
||||||
(assoc fronteir neighbor (+ new-cost (heuristic target-loc neighbor)))]
|
(< new-cost cost-for-neighbor))
|
||||||
[cost-so-far came-from fronteir]
|
(.put came-from (vec neighbor) (vec (:loc current-loc)))
|
||||||
)))
|
(.put cost-so-far (vec neighbor) new-cost)
|
||||||
[cost-so-far came-from fronteir]
|
(.offer fronteir {:cost (+ new-cost (heuristic (first target-loc) (second target-loc) (first neighbor) (second neighbor)))
|
||||||
neighbors)]
|
:loc neighbor}))))
|
||||||
|
(recur (.poll fronteir)))))))))
|
||||||
(recur cost-so-far came-from (dissoc fronteir current-loc))))))))
|
|
||||||
|
|
||||||
|
|
||||||
(defn print-resolved [path my-map]
|
(defn print-resolved [path my-map]
|
||||||
@@ -87,8 +96,11 @@
|
|||||||
nil)
|
nil)
|
||||||
|
|
||||||
(defn test-pathfind []
|
(defn test-pathfind []
|
||||||
(let [my-map (random-map)]
|
(let [my-map (random-map)
|
||||||
(print-resolved (visit-all my-map [1 1] [50 50]) my-map)))
|
path (visit-all my-map [1 1] [50 50])]
|
||||||
|
(println "Test")
|
||||||
|
(print-resolved path my-map)
|
||||||
|
(println path)))
|
||||||
|
|
||||||
(defn map-from-resource [filename]
|
(defn map-from-resource [filename]
|
||||||
(let [pm (pixmap filename)
|
(let [pm (pixmap filename)
|
||||||
|
|||||||
Reference in New Issue
Block a user