-
Notifications
You must be signed in to change notification settings - Fork 181
/
path.rb
317 lines (266 loc) · 9.44 KB
/
path.rb
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# frozen_string_literal: true
require_relative 'base'
module Engine
module Part
class Path < Base
attr_reader :a, :b, :city, :edges, :exit_lanes, :junction, :lanes, :nodes, :offboard,
:stops, :terminal, :town, :track, :ignore
attr_accessor :ignore_gauge_walk, :ignore_gauge_compare
LANES = [[1, 0].freeze, [1, 0].freeze].freeze
MATCHES_BROAD = %i[broad dual].freeze
MATCHES_NARROW = %i[narrow dual].freeze
LANE_INDEX = 1
LANE_WIDTH = 0
def self.decode_lane_spec(x_lane)
if x_lane
[x_lane.to_i, ((x_lane.to_f - x_lane.to_i) * 10).to_i]
else
[1, 0]
end
end
def self.make_lanes(a, b, terminal: nil, lanes: nil, a_lane: nil, b_lane: nil, track: nil, ignore: nil,
ignore_gauge_walk: nil, ignore_gauge_compare: nil)
track ||= :broad
if lanes
Array.new(lanes) do |index|
a_lanes = [lanes, index]
b_lanes = if a.edge? && b.edge?
[lanes, lanes - index - 1]
else
a_lanes
end
Path.new(a, b,
terminal: terminal,
lanes: [a_lanes, b_lanes],
track: track,
ignore: ignore,
ignore_gauge_walk: ignore_gauge_walk,
ignore_gauge_compare: ignore_gauge_compare)
end
else
Path.new(a, b,
terminal: terminal,
lanes: [decode_lane_spec(a_lane), decode_lane_spec(b_lane)],
track: track,
ignore: ignore,
ignore_gauge_walk: ignore_gauge_walk,
ignore_gauge_compare: ignore_gauge_compare)
end
end
def initialize(a, b, terminal: nil, lanes: LANES, track: :broad, ignore: nil,
ignore_gauge_walk: nil, ignore_gauge_compare: nil)
@a = a
@b = b
@terminal = terminal
@lanes = lanes
@edges = []
@stops = []
@nodes = []
@exit_lanes = {}
@track = track
@ignore = ignore
@ignore_gauge_walk = ignore_gauge_walk
@ignore_gauge_compare = ignore_gauge_compare
separate_parts
end
def <=>(other)
id <=> other.id
end
def <=(other)
other_ends = other.ends
ends.all? { |t| other_ends.any? { |o| t <= o } } && (@ignore_gauge_compare || tracks_match?(other))
end
def tracks_match?(other_path, dual_ok: false)
other_track = other_path.track
case @track
when :broad
MATCHES_BROAD.include?(other_track)
when :narrow
MATCHES_NARROW.include?(other_track)
when :dual
dual_ok || other_track == :dual
end
end
def ends
@ends ||= [@a, @b].flat_map do |part|
next part unless part.junction?
part.paths.flat_map do |path|
next [] if path == self
[path.a, path.b].reject(&:junction?)
end
end
end
def connects_to?(other, corporation)
[@a, @b].each do |part|
if part.edge?
edge = part.num
if (neighbor = hex.neighbors[edge])
np_edge = hex.invert(edge)
return true if neighbor == other.hex && other.edges.any? { |e| e.num == np_edge }
end
elsif part.junction?
return true if other.junction == part
elsif other.nodes.include?(part) && !part.blocks?(corporation)
return true
end
end
false
end
# skip: An exit to ignore. Useful to prevent ping-ponging between adjacent hexes.
# jskip: An junction to ignore. May be useful on complex tiles
# visited: a hashset of visited Paths. Used to avoid repeating track segments.
# counter: a hash tracking edges and junctions to avoid reuse
# skip_track: If passed, don't walk on track of that type (ie: :broad track for 1873)
# converging: When true, some predecessor path was part of a converging switch
def walk(
skip: nil,
jskip: nil,
visited: {},
skip_paths: nil,
counter: Hash.new(0),
skip_track: nil,
converging: true,
&block
)
return if visited[self] || skip_paths&.key?(self)
return if @junction && counter[@junction] > 1
return if edges.sum { |edge| counter[edge.id] }.positive?
return if track == skip_track
return if @junction && @terminal
visited[self] = true
counter[@junction] += 1 if @junction
yield self, visited, counter, converging
if @junction && @junction != jskip
@junction.paths.each do |jp|
jp.walk(jskip: @junction, visited: visited, skip_paths: skip_paths, counter: counter, converging: converging, &block)
end
end
edges.each do |edge|
edge_id = edge.id
edge = edge.num
next if edge == skip
next unless (neighbor = hex.neighbors[edge])
counter[edge_id] += 1
np_edge = hex.invert(edge)
neighbor.paths[np_edge].each do |np|
next unless lane_match?(@exit_lanes[edge], np.exit_lanes[np_edge])
next if !@ignore_gauge_walk && !tracks_match?(np, dual_ok: true)
np.walk(skip: np_edge, visited: visited, skip_paths: skip_paths, counter: counter, skip_track: skip_track,
converging: converging || @tile.converging_exit?(edge), &block)
end
counter[edge_id] -= 1
end
visited.delete(self) if converging
counter[@junction] -= 1 if @junction
end
# return true if facing exits on adjacent tiles match up taking lanes into account
def lane_match?(lanes0, lanes1)
return false unless lanes0
return false unless lanes1
if lanes1[LANE_WIDTH] == lanes0[LANE_WIDTH]
lanes1[LANE_INDEX] == lane_invert(lanes0)[LANE_INDEX]
else
lane_match_different_sizes?(lanes0, lanes1)
end
end
def lane_invert(lane)
[lane[LANE_WIDTH], lane[LANE_WIDTH] - lane[LANE_INDEX] - 1]
end
# the important part of matching lanes of different sizes is to just use
# the larger width when doing the inverted comparison; the delta just
# allows the smaller-lane side to be more centered, i.e., it makes 1 lane
# match up with the middle of 3 lanes
def lane_match_different_sizes?(lanes0, lanes1)
lanes_a, lanes_b = [lanes0, lanes1].sort
larger_width = lanes_b[LANE_WIDTH]
delta = ((lanes_b[LANE_WIDTH] - lanes_a[LANE_WIDTH]) / 2).to_i
new_index = lanes_a[LANE_INDEX] + delta
[larger_width, new_index][LANE_INDEX] == lane_invert(lanes_b)[LANE_INDEX]
end
def path?
true
end
def node?
return @_node if defined?(@_node)
@_node = !@nodes.empty?
end
def ignore?
!!@ignore
end
def terminal?
!!@terminal
end
def single?
return @_single if defined?(@_single)
@_single = @lanes.first[LANE_WIDTH] == 1 && @lanes.last[LANE_WIDTH] == 1
end
def exits
@exits ||= @edges.map(&:num)
end
def node_edge
return nil unless @nodes.one?
@node_edge ||= @tile.preferred_city_town_edges[@nodes.first]
end
# like a.num except it works when a is a town/city next to an edge
def a_num
@a_num ||= @a.edge? ? @a.num : node_edge
end
# like b.num except it works when b is a town/city next to an edge
def b_num
@b_num ||= @b.edge? ? @b.num : node_edge
end
def straight?
return @_straight if defined?(@_straight)
@_straight = a_num && b_num && (a_num - b_num).abs == 3
end
def gentle_curve?
return @_gentle_curve if defined?(@_gentle_curve)
@_gentle_curve = a_num && b_num && [2, 2.5, 3.5, 4].include?((a_num - b_num).abs)
end
def rotate(ticks)
path = Path.new(@a.rotate(ticks), @b.rotate(ticks),
terminal: @terminal,
lanes: @lanes,
track: @track,
ignore: @ignore,
ignore_gauge_walk: @ignore_gauge_walk,
ignore_gauge_compare: @ignore_gauge_compare)
path.index = index
path.tile = @tile
path
end
def inspect
name = self.class.name.split('::').last
if single?
"<#{name}: hex: #{hex&.name}, exit: #{exits}, track: #{track}>"
else
"<#{name}: hex: #{hex&.name}, exit: #{exits}, lanes: #{@lanes.first} #{@lanes.last}>"
end
end
private
def separate_parts
[@a, @b].each do |part|
if part.edge?
@edges << part
@exit_lanes[part.num] = @lanes[part == @a ? 0 : 1]
elsif part.offboard?
@offboard = part
@stops << part
@nodes << part
elsif part.city?
@city = part
@stops << part
@nodes << part
elsif part.junction?
@junction = part
elsif part.town?
@town = part
@stops << part
@nodes << part
end
part.lanes = @lanes[part == @a ? 0 : 1]
end
end
end
end
end