summaryrefslogtreecommitdiffstats
path: root/lib/colonial_twilight/fln_bot_rules.rb
blob: 1df920942867978231d31dc02fc87f1fa4049ade (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#! /usr/bin/env ruby
# frozen_string_literal: true

module ColonialTwilight
  module FLNBotRules
    def dbg(msg, ret)
      return if @debug.zero?

      case @debug
      when 1 then puts "  #{msg}" if ret
      else puts "  #{msg} : #{ret ? 'YES' : 'NO'}"
      end
    end

    def pass?(board = @board)
      # if resources = 0 && Op Limited as only choice
      r = board.fln_resources.zero? && limited_op_only?
      dbg 'PASS', r
      r
    end

    def terror1?(board = @board)
      # if no FLN base is (pop 0 && 0 FLN underground or pop 1+ && 1- FLN underground)
      r = !board.has do |s|
        s.fln_bases.positive? && ((s.pop.zero? && s.fln_underground.zero?) || (!s.pop.zero? && s.fln_underground < 2))
      end
      dbg 'TERROR 1', r
      r
    end

    def terror2?(_board = nil)
      # if GOV is first eligible && will be second eligible
      r = !first_eligible? && will_be_next_first_eligible?
      dbg 'TERROR 2', r
      r
    end

    def rally1?(board = @board)
      # rally would place a base (rally 1 or 2)
      r = board.available_fln_bases.positive? && board.has { |s| (may_rally_1_in?(s) || may_rally_2_in?(s)) }
      dbg 'RALLY 1', r
      r
    end

    def rally2?(board = @board)
      # if #FLN bases * 2 > #FLN at FLN bases + 1d6/2
      a = board.count(&:fln_bases) * 2
      b = board.count { |s| s.fln_bases.zero? ? 0 : s.guerrillas }
      r = a > (b + d6 / 2)
      dbg 'RALLY 2', r
      r
    end

    # Rally

    def may_rally_1_in?(space)
      # 3+ FLN and no GOV (unless limited_op_only))
      r = may_rally_in?(space) && may_add_base_in?(space) && space.guerrillas >= 3 &&
          (limited_op_only? ? true : space.gov_cubes.zero?)
      dbg "  may_rally_1_in : #{space.name}", r
      r
    end

    def may_rally_2_in?(space)
      # 4+ FLN
      r = may_rally_in?(space) && may_add_base_in?(space) && space.guerrillas >= 4
      dbg "  may_rally_2_in : #{space.name}", r
      r
    end

    def may_rally_3_in?(space)
      # at FLN bases, with 2- FLN underground or 0 fln_underground in country or 0 pop
      r = may_rally_in?(space) && !space.fln_bases.zero? &&
          (space.country? || space.pop.zero? ? space.fln_underground.zero? : space.fln_underground < 2)
      dbg "  may_rally_3_in : #{space.name}", r
      r
    end

    def rally_3_priority(spaces)
      # Algeria -> with cubes -> pop 1+ -> least FLN underground
      f = _filter(spaces) { |s| !s.country? }
      f = _filter(f) { |s| s.gov_cubes.positive? }
      f = _filter(f) { |s| s.pop.positive? }
      _min(f, :fln_underground)
    end

    def may_rally_5_in?(space)
      # non-city at support with 0 FLN underground
      r = may_rally_in?(space) && !space.city? && space.support? && space.fln_underground.zero?
      dbg "  may_rally_5_in : #{space.name}", r
      r
    end

    def rally_5_priority(spaces)
      # highest population
      _max(spaces, :pop)
    end

    def may_rally_6_in?(space, already_rallied)
      # 2+ pop to agitate after rally
      r = (already_rallied || may_rally_in?(space)) && space.pop > 1 && (space.terror.positive? || !space.oppose?)
      if r
        # may agitate if : FLN base or control after rally
        n = already_rallied ? 0 : place_guerrillas_in(space).values.sum
        r &= (space.fln_bases.positive? || (space.gov < (space.fln + n)))
      end
      dbg "  may_rally_6_in : #{space.name}", r
      r
    end

    def rally_6_priority(spaces)
      # max pop, min terror, support : reference ?
      f = _max(spaces, :pop)
      f = _min(f, :terror)
      _filter(f, &:support?)
      # FIXME: maybe already selected first, or not
    end

    def may_rally_7_in?(space)
      r = may_rally_in?(space)
      dbg "  may_rally_7_in : #{space.name}", r
      r
    end

    def rally_7_priority(spaces)
      # highest population -> gain FLN control -> remove Gov control -> city -> least terror
      f = _max(spaces, :pop)
      f = _filter(f) { |s| s.gov < s.fln + place_guerrillas_in(s).values.sum }
      f = _filter(f) { |s| s.gov == s.fln + place_guerrillas_in(s).values.sum }
      f = _filter(f, &:city?)
      _min(f, :terror)
    end

    def may_rally_8_in?(space)
      r = may_rally_in?(space) && !space.guerrillas.zero? && space.fln_bases.zero?
      dbg "  may_rally_8_in : #{space.name}", r
      r
    end

    def rally_8_priority(spaces)
      # Algeria -> most Guerrillas -> no gov cubes
      f = _filter(spaces) { |s| !s.country? }
      f = _max(f, :guerrillas)
      _filter(f) { |s| s.gov_cubes.zero? }
    end

    def may_rally_9_in?(space)
      r = may_agitate_in?(space) && (space.terror.positive? || !space.oppose?)
      dbg "  may_rally_9_in : #{space.name}", r
      r
    end

    def rally_9_priority(spaces, resources)
      _filter(spaces) { |s| !s.oppose? && resources > s.terror }
    end

    # Extort

    def may_extort_0_in?(space)
      r = may_extort_in?(space) && space.fln_underground > (space.fln_bases.zero? ? 0 : 1)
      dbg "  may_extort_0_in : #{space.name}", r
      r
    end

    def extort_priority(spaces)
      # 2+ guerrillas, 3+ if gov cubes and fln base -> Country -> anywhere if still at 0
      f = _filter(spaces) { |s| s.guerrillas > (s.gov_cubes.positive? && s.fln_bases.positive? ? 2 : 1) }
      _filter(f, &:country?)
    end

    # Subvert

    def may_subvert_1_in?(space, num)
      # to remove last cubes
      r = may_subvert_in?(space) && (space.french_cubes.zero? && space.algerian_cubes <= num)
      dbg "  may_subvert_1_in : #{space.name}", r
      r
    end

    def subvert_1_priority(spaces)
      # Police -> Troop
      _max(spaces, :algerian_police)
    end

    def may_subvert_2_in?(space)
      # to replace 1 Algerian Police
      r = may_subvert_in?(space) && space.algerian_police.positive?
      dbg "  may_subvert_2_in : #{space.name}", r
      r
    end

    # 8.1.2 - Procedure Guidelines

    def _filter(spaces, &block)
      return spaces if spaces.empty?

      (f = spaces.select(&block)).empty? ? spaces : f
    end

    def _max(spaces, sym)
      return spaces if spaces.empty?

      v = spaces.max { |a, b| a.send(sym) <=> b.send(sym) }.send(sym)
      spaces.select { |s| s.send(sym) == v }
    end

    def _min(spaces, sym)
      return spaces if spaces.empty?

      v = spaces.min { |a, b| a.send(sym) <=> b.send(sym) }.send(sym)
      spaces.select { |s| s.send(sym) == v }
    end

    def available_fln_bases?(board = @board)
      board.available_fln_bases.positive?
    end

    def may_add_base_in?(space)
      space.guerrillas > 2 && (space.fln_bases < (space.country? ? space.max_bases : 1))
    end

    def max_placable_guerrillas_in?(space)
      max_placable_guerrillas(space).clamp(0, space.fln_bases.positive? ? (space.pop + 1 - space.guerrillas) : 666)
    end

    def place_guerrillas_in(space, board = @board)
      n = max_placable_guerrillas_in?(space)
      h = { space: 0 } # do not select space
      n -= h[:available] = (a = board.available_fln_underground) >= n ? n : a
      while n.positive? && !(spaces = _remove_guerrillas_priority(board.spaces, h)).empty?
        s = spaces.sample
        n -= h[s] = (g = _removable_guerrillas(s)) >= n ? n : g
      end
      h.reject { |_k, v| v.zero? } # FIXME: in empty? maybe hide active guerrillas ?
    end

    def pick_guerrillas_from(board = @board)
      return :available if board.available_fln_underground.positive?

      _remove_guerrillas_priority(board.spaces).sample
    end

    # 1) place: outofplay -> available | bases -> guerrillas if choice
    # 2) place: underground first unless from map then place active first flipped as underground
    # 3) march: underground -> active, unless march would activate then move active first

    # applied as last filter in FLNBot#_priority
    def place_guerrillas_priority(spaces)
      # 4) support -> with friendly pieces -> random
      f = _filter(spaces, &:support?)
      _filter(f) { |s| s.guerrillas.positive? }
    end

    # place_guerrillas_in
    def _removable_guerrillas(space)
      # 5) active only, leave 2 guerrillas at base or support
      a = (a = space.fln_underground) > 2 ? 2 : a
      n = space.fln_active - (space.support? || space.fln_bases.positive? ? (2 - a) : 0)
      n.positive? ? n : 0
    end

    def _not_selected(spaces, selected)
      spaces.reject { |s| selected.key?(s) }
    end

    # place_guerrillas_in
    def _remove_guerrillas_priority(spaces, selected = {})
      # 5) #removable_guerrillas then most guerrillas first
      return [] if (l = _not_selected(spaces, selected).select { |s| _removable_guerrillas(s).positive? }).empty?

      _max(l, :guerrillas)
    end

    # not used yet
    def remove_from(space, num = 1)
      # 6) remove active -> underground -> base
      h = {}
      num -= h[:fln_active] = (s = space.fln_active) >= num ? num : s
      num -= h[:fln_underground] = (s = space.fln_underground) >= num ? num : s
      h[:fln_bases] = (s = space.fln_bases) >= num ? num : s
      h
    end

    # 7) remove gov : map -> availabe (base -> french -> algerian; troops -> police)
    # 8) reduce : commitment -> support -> france track -> gov resource
    # 9) shift : support -> oppose | best combined; remove terror only if also shift
    # 10) random
  end
end