Skip to content

Commit

Permalink
Merge pull request #2 from pusewicz/cyberarm-1d_array_optimzation
Browse files Browse the repository at this point in the history
Cyberarm 1d array optimzation
  • Loading branch information
pusewicz authored May 6, 2024
2 parents dec345c + d623b03 commit 102a64e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
17 changes: 9 additions & 8 deletions lib/wave_function_collapse/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ def collapse
self.tiles = [@tiles.max_by { |t| rand**(1.0 / t.probability) }]
end

def neighbors(grid)
return if grid.nil?

@neighbors["#{@x},#{@y}"] ||= begin
up = grid[@x][@y + 1] if grid[@x] && @y < grid[0].length - 1
down = grid[@x][@y - 1] if grid[@x] && @y.positive?
right = grid[@x + 1][@y] if @x < grid.length - 1
left = grid[@x - 1][@y] if @x.positive?
def neighbors(model)
return if model.nil?

@neighbors[model.width * y + x] ||= begin
up = model.cell_at(@x, @y + 1) if @y < model.height - 1
down = model.cell_at(@x, @y - 1) if @y.positive?
right = model.cell_at(@x + 1, @y) if @x < model.width - 1
left = model.cell_at(@x - 1, @y) if @x.positive?

{up: up, down: down, right: right, left: left}
end
end
Expand Down
28 changes: 18 additions & 10 deletions lib/wave_function_collapse/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ def initialize(tiles, width, height)
@tiles = tiles
@width = width.to_i
@height = height.to_i

@grid = Array.new(width) { |x| Array.new(height) { |y| Cell.new(x, y, @tiles.shuffle) } }
@uncollapsed_cells_grid = @grid.flatten.reject(&:collapsed)
@grid = []
@height.times { |y| @width.times { |x| @grid << Cell.new(x, y, @tiles.shuffle) } }
@uncollapsed_cells_grid = @grid.reject(&:collapsed)
@max_entropy = @tiles.length
end

def cell_at(x, y)
@grid[@width * y + x]
end

def complete?
@uncollapsed_cells_grid.empty?
end
Expand Down Expand Up @@ -79,18 +83,18 @@ def random_cell
def generate_grid
x = 0
result = []
lx = @grid.length
while x < lx

while x < @width
rx = result[x] = []
y = 0
pgx = @grid[x]
ly = pgx.length
while y < ly
rx[y] = pgx[y].tile

while y < @height
rx[y] = cell_at(x, y).tile
y += 1
end
x += 1
end

result
end

Expand All @@ -110,7 +114,7 @@ def propagate(source_cell)
end

def evaluate_neighbor(source_cell, evaluation_direction)
neighbor_cell = source_cell.neighbors(@grid)[evaluation_direction]
neighbor_cell = source_cell.neighbors(self)[evaluation_direction]
return if neighbor_cell.nil? || neighbor_cell.collapsed

original_tile_count = neighbor_cell.tiles.length
Expand All @@ -122,23 +126,27 @@ def evaluate_neighbor(source_cell, evaluation_direction)
neighbor_tiles = neighbor_cell.tiles
sci = 0
scil = source_tiles.length

while sci < scil
source_tile = source_tiles[sci]
sci += 1
source_edge_hash = source_tile.send(evaluation_direction)
nci = 0
ncil = neighbor_tiles.length

while nci < ncil
tile = neighbor_tiles[nci]
nci += 1
next if new_tile_ids.has_key?(tile.tileid)

tile_edge_hash = tile.send(opposite_direction)
if tile_edge_hash == source_edge_hash
new_tile_ids[tile.tileid] = true
new_tiles << tile
end
end
end

neighbor_cell.tiles = new_tiles unless new_tiles.empty?
@uncollapsed_cells_grid.delete(neighbor_cell) if neighbor_cell.collapsed

Expand Down
2 changes: 1 addition & 1 deletion lib/wave_function_collapse/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def draw_map
column.reverse.each_with_index do |tile, y|
inverted_y = (y - @model.height + 1).abs

entropy = @model.grid[x][inverted_y].entropy
entropy = @model.cell_at(x, inverted_y).entropy

if entropy > 1
percent_entropy = (entropy.to_f / @model.max_entropy * 255).round
Expand Down
2 changes: 1 addition & 1 deletion test/test_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_initialize

assert_equal 320, model.width
assert_equal 240, model.height
assert_equal 320 * 240, model.grid.map(&:size).sum
assert_equal 320 * 240, model.grid.size
assert_equal 3, model.max_entropy
assert_equal 0, model.percent
refute model.complete?
Expand Down

0 comments on commit 102a64e

Please sign in to comment.