Skip to content

Commit

Permalink
Adding ranged, adjacent, flanking, and opposite attack bonuses
Browse files Browse the repository at this point in the history
They default to 1, 1, 2, and 3 respectively. They aren't yet, but will
be configurable.
  • Loading branch information
djwhitt committed Dec 22, 2016
1 parent d199145 commit a1875f7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
28 changes: 25 additions & 3 deletions src/clj/zetawar/game.cljs
Expand Up @@ -434,9 +434,10 @@
(catch :default ex
false)))

;; TODO: add bonuses for flanking, etc.
(defn attack-damage [db attacker defender attacker-terrain defender-terrain]
(let [defender-armor-type (get-in defender [:unit/type :unit-type/armor-type])
[attacker-q attacker-r] (unit-hex attacker)
[defender-q defender-r] (unit-hex defender)
attack-strength (oonly (d/q '[:find ?s
:in $ ?u ?at
:where
Expand Down Expand Up @@ -465,12 +466,33 @@
[?e :terrain-effect/terrain-type ?tt]
[?e :terrain-effect/unit-type ?ut]
[?e :terrain-effect/armor-bonus ?d]]
db (e defender) (e defender-terrain)))]
db (e defender) (e defender-terrain)))
attack-hexes (into #{} (map terrain-hex) (:unit/attacked-from defender))
ranged-attack-hexes (into #{}
(filter #(> (apply hex/distance defender-q defender-r %) 1))
attack-hexes)
adjacent-attack-hexes (into #{}
(filter #(and (apply hex/adjacent? attacker-q attacker-r %)
(apply hex/adjacent? defender-q defender-r %)))
attack-hexes)
opposite-attack-hexes (into #{}
(filter #(do (log/spy [attacker-q attacker-r defender-q defender-r %])
(apply hex/opposite? attacker-q attacker-r defender-q defender-r %)))
attack-hexes)
flanking-attack-hexes (clojure.set/difference attack-hexes
ranged-attack-hexes
adjacent-attack-hexes
opposite-attack-hexes)
;; TODO: get multipliers from game entity
gang-up-bonus (+ (* (count ranged-attack-hexes) 1)
(* (count adjacent-attack-hexes) 1)
(* (count flanking-attack-hexes) 2)
(* (count opposite-attack-hexes) 3))]
(js/Math.round
(max 0 (* (:unit/count attacker)
(+ 0.5 (* 0.05 (+ (- (+ attack-strength attack-bonus)
(+ armor armor-bonus))
(:unit/attacked-count defender)))))))))
gang-up-bonus))))))))

(defn battle-damage
([db game attacker defender]
Expand Down
37 changes: 34 additions & 3 deletions src/clj/zetawar/hex.cljc
@@ -1,5 +1,6 @@
(ns zetawar.hex
(:require
[taoensso.timbre :as log]
[zetawar.util :refer [abs]]))

(def min-q 0)
Expand All @@ -11,34 +12,64 @@
(defn east [q r]
[(inc q) r])

(defn east-of? [q1 r1 q2 r2]
(= [q2 r2] (east q1 r1)))

(defn west [q r]
[(dec q) r])

(defn west-of? [q1 r1 q2 r2]
(= [q2 r2] (west q1 r1)))

(defn northeast [q r]
(if (= (mod r 2) 0)
[q (dec r)]
[(inc q) (dec r)]))

(defn northeast-of? [q1 r1 q2 r2]
(= [q2 r2] (northeast q1 r1)))

(defn northwest [q r]
(if (= (mod r 2) 0)
[(dec q) (dec r)]
[q (dec r)]))

(defn northwest-of? [q1 r1 q2 r2]
(= [q2 r2] (northwest q1 r1)))

(defn southeast [q r]
(if (= (mod r 2) 0)
[q (inc r)]
[(inc q) (inc r)]))

(defn southeast-of? [q1 r1 q2 r2]
(= [q2 r2] (southeast q1 r1)))

(defn southwest [q r]
(if (= (mod r 2) 0)
[(dec q) (inc r)]
[q (inc r)]))

(defn southwest-of? [q1 r1 q2 r2]
(= [q2 r2] (southwest q1 r1)))

;; TODO: make this more elegant
(defn opposite? [q1 r1 q2 r2 q3 r3]
(or (and (east-of? q1 r1 q2 r2) (east-of? q2 r2 q3 r3))
(and (west-of? q1 r1 q2 r2) (west-of? q2 r2 q3 r3))
(and (northwest-of? q1 r1 q2 r2) (northwest-of? q2 r2 q3 r3))
(and (southwest-of? q1 r1 q2 r2) (southwest-of? q2 r2 q3 r3))
(and (northeast-of? q1 r1 q2 r2) (northeast-of? q2 r2 q3 r3))
(and (southeast-of? q1 r1 q2 r2) (southeast-of? q2 r2 q3 r3))))

(def adjacents
(memoize (fn adjacents [q r]
[(east q r) (west q r)
(northeast q r) (northwest q r)
(southeast q r) (southwest q r)])))
#{(east q r) (west q r)
(northeast q r) (northwest q r)
(southeast q r) (southwest q r)})))

(defn adjacent? [q1 r1 q2 r2]
((adjacents q1 r1) [q2 r2]))

(defn offset->cube [q r]
(let [x (- q (/ (- r (mod r 2)) 2))
Expand Down

0 comments on commit a1875f7

Please sign in to comment.