# cc/journeyman/the_great_game/world/location.clj

001 (ns cc.journeyman.the-great-game.world.location
002 "Functions dealing with location in the world."
003 (:require [clojure.math.numeric-tower :refer [expt sqrt]]))
004
005 ;; A 'location' value is a list comprising at most the x/y coordinate location
006 ;; and the ids of the settlement and region (possibly hierarchically) that contain
007 ;; the location. If the x/y is not local to the home of the receiving agent, they
008 ;; won't remember it and won't pass it on; if any of the ids are not interesting
009 ;; So location information will degrade progressively as the item is passed along.
010
011 ;; It is assumed that the `:home` of a character is a location in this sense.
012
013 (defn get-coords
014 "Return the coordinates in the game world of `location`, which may be
015 1. A coordinate pair in the format {:x 5 :y 32};
016 2. A location, as discussed above;
017 3. Any other gameworld object, having a `:location` property whose value
018 is one of the above."
019 [location]
020 (cond
021 (empty? location) nil
022 (map? location)
023 (cond
024 (and (number? (:x location)) (number? (:y location)))
025 location
026 (:location location)
027 (:location location))
028 :else
029 (get-coords (first (remove keyword? location)))))
030
031 (defn distance-between
032 [location-1 location-2]
033 (let [c1 (get-coords location-1)
034 c2 (get-coords location-2)]
035 (when
036 (and c1 c2)
037 (sqrt (+ (expt (- (:x c1) (:x c2)) 2) (expt (- (:y c1) (:y c2)) 2))))))