summaryrefslogtreecommitdiff
path: root/src/votann_battle_simulator/weapon_abilities.clj
blob: 146b6823fd5490ef23a5830c8755658569e2c9c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(ns votann-battle-simulator.weapon-abilities
  (:require [votann-battle-simulator.util :as util]
            [votann.records.weapon])
  (:import [votann.records.weapon Weapon]))

(defn blast
  ([target-size]
   (int (/ target-size 5)))
  ([dice unit-size target-size]
   (let [blast-added (+ dice (* unit-size (blast target-size)))]
     (println "Dice count before " dice " after " blast-added)
     blast-added)))

(defn resolve-anti [stat]
  (let [keyword (re-find #"Anti-(\w+)" stat)
        modifier (Integer/parseInt (re-find #"\d+" stat))]
    {:keyword (second keyword)
     :modifier modifier}))

(defn anti [dice type target-keywords]
  (let [resolved-type (resolve-anti type)]
    (if (contains? (set target-keywords) (:keyword resolved-type))
      (map (fn [roll]
             (if (>= roll (:modifier resolved-type))
               6
               roll)) dice)
      dice)))

(defn devastating-wounds [dice]
  (let [mortal-added (vec (map #(if (= 6 %)
                                  99
                                  %) dice))]
    (println "Hit rolls before " dice " after " mortal-added)
    mortal-added))

(defn twin-linked [dice strength toughness]
  (let [re-rolls (vec (cond (>= strength (* toughness 2))
                            (filter #(< % 2) dice)

                            (> strength toughness)
                            (filter #(< % 3) dice)

                            (= strength toughness)
                            (filter #(< % 4) dice)

                            (< strength toughness)
                            (filter #(< % 5) dice)

                            (<= strength (double (/ toughness 2)))
                            (filter #(< % 6) dice)))
        dice-rolls (vec (filter (complement (set re-rolls)) dice))
        twin-linked-rolls (apply conj dice-rolls (util/roll-d6 (count re-rolls)))]
    (println "Wounds before " dice " after " twin-linked-rolls)
    twin-linked-rolls))

(defn rapid-fire [dice modifier]
  (cond
    (re-find #"D\d" modifier)
    (let [rapid-fire-modifier (util/resolve-stat-count modifier)
          rapid-fire-added (+ dice rapid-fire-modifier)]
      (println "Dice count before " dice " after " rapid-fire-added)
      rapid-fire-added)
    :else
    (let [rapid-fire-added (+ dice (Integer/parseInt (re-find #"\d+" modifier)))]
      (println "Dice count before " dice " after " rapid-fire-added)
      rapid-fire-added)))

(defn conversion [dice range]
  (if (> range 12)
    (let [conversion-added (map (fn [roll]
                                     (if (>= roll 4)
                                       6
                                       roll)) dice)]
      (println "Hit count before " dice " after " conversion-added)
      conversion-added)
    dice))

(defn sustained-hits [modifier dice]
  (let [critical (count (filter #(= 6 %) dice))]
    (cond
      (re-find #"D\d" modifier)
      (let [crit-added (apply conj dice
                              (vec (repeat (*
                                            critical
                                            (util/resolve-stat-count modifier))
                                           7)))]
        (println "Hit count before " dice " after " crit-added)
        crit-added)

      :else
      (let [crit-added (apply conj dice
                              (vec (repeat (*
                                            critical
                                            (Integer/parseInt (re-find #"\d+" modifier)))
                                           7)))]
        (println "Hit count after " dice " after " crit-added)
        crit-added)
        )))

(defn resolve-attack-abilities [^Weapon weapon unit-size target-size]
  (loop [abilities (:abilities weapon)
         dice (* unit-size (util/resolve-stat-count (:a weapon)))]
    (if (empty? abilities)
      dice
      (let [ability (first abilities)]
        (cond
          (not (nil? (re-find #"Blast" ability)))
          (do
            (println "Applied Blast attack ability for " (:name weapon))
            (recur (rest abilities)
                   (blast dice unit-size target-size)))

          (not (nil? (re-find #"Rapid Fire" ability)))
          (do
            (println "Applied Rapid fire attack ability  for " (:name weapon))
            (recur (rest abilities)
                   (rapid-fire dice ability)))

          :else (recur (rest abilities)
                       dice))))))

(defn resolve-hit-dice-abilities [^Weapon weapon dice]
  (loop [abilities (:abilities weapon)
         dice-rolls dice]
    (if (empty? abilities)
      dice-rolls
      (let [ability (first abilities)]
        (cond
          (not (nil? (re-find #"Conversion" ability)))
          (do
            (println "Applied Conversion hit ability for " (:name weapon))
            (recur (rest abilities)
                   (conversion dice-rolls 24)))

          (not (nil? (re-find #"Sustained Hits" ability)))
          (do
            (println "Applied Sustained Hits hit ability for " (:name weapon))
            (recur (rest abilities)
                   (sustained-hits ability dice-rolls)))

          :else
          (recur (rest abilities)
                 dice-rolls))))))

(defn resolve-hit-modifier-abilities [^Weapon weapon modifier-abilities dice]
  (loop [abilities (apply conj (:abilities weapon) modifier-abilities)
         modifier 0]
    (if (empty? abilities)
      (let [modifier-added (vec (map #(if (or (= 1 %) (= 6 %))
                                        %
                                        (util/dice-cap (+ (util/modifier-cap modifier) %))) dice))]
        (println "Resolve hit Before " dice " after " modifier-added)
        modifier-added)
      (let [ability (first abilities)]
        (cond
          (not (nil? (re-find #"Heavy" ability)))
          (do
            (println "Applied Heavy hit modifier for " (:name weapon))
            (recur (rest abilities)
                   (util/mod+1 modifier)))

          (not (nil? (re-find #"Judgment Token" ability)))
          (do
            (println "Applied Judgment Token hit modifier for " (:name weapon))
            (recur (rest abilities)
                   (util/mod+1 modifier)))
          :else
          (recur (rest abilities)
                 modifier))))))

(defn resolve-wound-dice-abilites [^Weapon weapon dice strength toughness keywords]
  (loop [abilities (:abilities weapon)
         dice-rolls dice
         modifier 0]
    (if (empty? abilities)
      dice-rolls
      (let [ability (first abilities)]
        (cond
          (not (nil? (re-find #"Anti-" ability)))
          (do
            (println "Applied Anti wound ability for " (:name weapon))
            (recur (rest abilities)
                   (anti dice ability keywords)
                   modifier))

          (not (nil? (re-find #"Devastating Wounds" ability)))
          (do
            (println "Applied Devastating Wounds wound ability for " (:name weapon))
            (recur (rest abilities)
                   (devastating-wounds dice)
                   modifier))

          (not (nil? (re-find #"Twin-Linked" ability)))
          (do
            (println "Twin-Linked wound ability for " (:name weapon))
            (recur (rest abilities)
                   (twin-linked dice strength toughness)
                   modifier))

          :else
          (recur (rest abilities)
                 dice-rolls
                 modifier))))))

(defn resolve-wound-modifier-abilites [^Weapon weapon modifier-abilities dice]
  (loop [abilities (apply conj (:abilities weapon) modifier-abilities)
         modifier 0]
    (if (empty? abilities)
      (let [modifier-added (vec (map #(if (or (= 1 %) (= 6 %))
                                        %
                                        (util/dice-cap (+ (util/modifier-cap modifier) %))) dice))]
        (println "Resolve wound Before " dice " after " modifier-added)
        modifier-added)
      (let [ability (first abilities)]
        (cond
          (not (nil? (re-find #"Judgment Token" ability)))
          (do
            (println "Applied Judgment Token wound modifier for " (:name weapon))
            (recur (rest abilities)
                   (util/mod+1 modifier)))

          :else
          (recur (rest abilities)
                 modifier))))))