Skip to content

Commit 913e03f

Browse files
committed
Use lookup table
1 parent 2d4ce44 commit 913e03f

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

lib/wave_function_collapse/model.rb

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,39 +114,21 @@ def evaluate_neighbor(source_cell, evaluation_direction)
114114

115115
original_tile_count = neighbor_cell.tiles.length
116116
opposite_direction = OPPOSITE_OF[evaluation_direction]
117-
neighbor_tiles = neighbor_cell.tiles
118117

119-
source_tile_edges = source_cell.tiles.map do |source_tile|
120-
case evaluation_direction
121-
when :up then source_tile.up
122-
when :right then source_tile.right
123-
when :down then source_tile.down
124-
when :left then source_tile.left
125-
end
126-
end
127-
128-
opposite_tile_edges = neighbor_tiles.map do |opposite_tile|
129-
case opposite_direction
130-
when :up then opposite_tile.up
131-
when :right then opposite_tile.right
132-
when :down then opposite_tile.down
133-
when :left then opposite_tile.left
134-
end
118+
# Build set of valid edges from source cell
119+
valid_edges = {}
120+
source_cell.tiles.each do |source_tile|
121+
valid_edges[source_tile.__send__(evaluation_direction)] = true
135122
end
136123

124+
# Filter neighbor tiles that have matching edges
125+
neighbor_tiles = neighbor_cell.tiles
137126
new_tiles = []
138-
ntc = neighbor_tiles.length
139127
i = 0
128+
ntc = neighbor_tiles.length
140129
while i < ntc
141-
ii = 0
142-
stel = source_tile_edges.length
143-
while ii < stel
144-
if source_tile_edges[ii] == opposite_tile_edges[i]
145-
new_tiles << neighbor_tiles[i]
146-
break
147-
end
148-
ii = ii.succ
149-
end
130+
tile = neighbor_tiles[i]
131+
new_tiles << tile if valid_edges[tile.__send__(opposite_direction)]
150132
i = i.succ
151133
end
152134

0 commit comments

Comments
 (0)