This commit is contained in:
Paul Stadig
2014-09-13 15:31:16 -04:00
parent fd398b5f3f
commit fb91cdcb68

View File

@@ -1,18 +1,6 @@
(ns advent.pathfind (ns advent.pathfind
(:require [play-clj.core :refer :all] (:require [play-clj.core :refer :all])
[play-clj.ui :refer :all] (:import (java.lang Math)))
[play-clj.utils :refer :all]
[play-clj.g2d :refer :all]
[clojure.pprint]
[clojure.data.priority-map :refer [priority-map]])
(:import [com.badlogic.gdx.files FileHandle]
[com.badlogic.gdx Files]
[java.lang Math]
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture
VertexAttributes$Usage]))
(def compare-count (atom 0))
(def scale 2) (def scale 2)
@@ -29,10 +17,10 @@
(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 640 (repeatedly (fn [] (vec (take 480 (repeatedly (fn [] (rand-nth [1 1 1 1 1 0]))))))))) (defn random-map []
(update-in [1 1] (constantly 1)) (-> (vec (take 640 (repeatedly (fn [] (vec (take 480 (repeatedly (fn [] (rand-nth [1 1 1 1 1 0])))))))))
(update-in [639 479] (constantly 1)))) (update-in [1 1] (constantly 1))
(update-in [639 479] (constantly 1))))
(defn neighbors [[^long x ^long y] my-map] (defn neighbors [[^long x ^long y] my-map]
(let [x (long x) (let [x (long x)
@@ -48,12 +36,10 @@
width (count my-map)] width (count my-map)]
(remove #(= 0 (get-in my-map %)) (remove #(= 0 (get-in my-map %))
(filter (fn [[^long x ^long y]] (and (< -1 x width) (filter (fn [[^long x ^long y]] (and (< -1 x width)
(< -1 y height))) candidates)))) (< -1 y height))) candidates))))
(defn resolve-path [came-from play-loc target-loc] (defn resolve-path [came-from play-loc target-loc]
(let [came-from (into {} came-from)] (let [came-from (into {} came-from)]
(if (nil? (came-from target-loc)) (if (nil? (came-from target-loc))
nil nil
(loop [path [] (loop [path []
@@ -65,20 +51,19 @@
(conj path (from-scale current-node)) (conj path (from-scale current-node))
(came-from (vec current-node)))))))) (came-from (vec current-node))))))))
(def d2 ^double (- (Math/sqrt 2) 2)) (def d2 ^double (- (Math/sqrt 2) 2))
(defn heuristic ^long [^long goal-x ^long goal-y ^long current-x ^long current-y] (defn heuristic ^long [^long goal-x ^long goal-y ^long current-x ^long current-y]
(let [dist-x (if (< goal-x current-x) (let [dist-x (if (< goal-x current-x)
(unchecked-subtract current-x goal-x) (unchecked-subtract current-x goal-x)
(unchecked-subtract goal-x current-x )) (unchecked-subtract goal-x current-x ))
dist-y (if (< goal-y current-y) dist-y (if (< goal-y current-y)
(unchecked-subtract current-y goal-y) (unchecked-subtract current-y goal-y)
(unchecked-subtract goal-y current-y)) (unchecked-subtract goal-y current-y))
min-dist ^double (double (min dist-x dist-y))] min-dist ^double (double (min dist-x dist-y))]
(unchecked-add (unchecked-add dist-x dist-y ) (unchecked-add (unchecked-add dist-x dist-y )
(long (unchecked-multiply ^double d2 min-dist))))) (long (unchecked-multiply ^double d2 min-dist)))))
(defn visit-all [my-map play-loc target-loc] (defn visit-all [my-map play-loc target-loc]
(let [play-loc (vec (to-scale play-loc)) (let [play-loc (vec (to-scale play-loc))
target-loc (vec (to-scale target-loc))] target-loc (vec (to-scale target-loc))]
@@ -91,7 +76,6 @@
(loop [current-loc (.poll fronteir)] (loop [current-loc (.poll fronteir)]
(if (or (nil? current-loc) (if (or (nil? current-loc)
(= (:loc current-loc) target-loc)) (= (:loc current-loc) target-loc))
(resolve-path came-from play-loc target-loc) (resolve-path came-from play-loc target-loc)
(do (doseq [neighbor (neighbors (:loc current-loc) my-map)] (do (doseq [neighbor (neighbors (:loc current-loc) my-map)]
(let [cost-for-neighbor (.get cost-so-far neighbor) (let [cost-for-neighbor (.get cost-so-far neighbor)
@@ -104,15 +88,13 @@
:loc neighbor})))) :loc neighbor}))))
(recur (.poll fronteir))))))))) (recur (.poll fronteir)))))))))
(defn print-resolved [path my-map] (defn print-resolved [path my-map]
(doseq [row (reduce (fn [acc path] (doseq [row (reduce (fn [acc path]
(if path (if path
(update-in acc (reverse path) (constantly "X")) (update-in acc (reverse path) (constantly "X"))
acc)) acc))
my-map my-map
path)] path)]
(println (map {1 \space 5 "." 0 "W" "X" "X"} row))) (println (map {1 \space 5 "." 0 "W" "X" "X"} row)))
nil) nil)