Skip to content

Commit

Permalink
add remap precondition
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBond committed Sep 13, 2024
1 parent bfa0797 commit d6e0dd6
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
96 changes: 96 additions & 0 deletions addons/wfc/examples/demo_wfc_2d_tilemaplayer_multilayer_remap.tscn

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions addons/wfc/problems/2d/preconditions/precondition_2d_remap.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extends WFC2DPrecondition

class_name WFC2DPreconditionRemap

var _node: Node
var _mapper: WFCMapper2D
var _domains_map: Array[WFCBitSet]

func _init(node: Node, mapper: WFCMapper2D, domains_map: Array[WFCBitSet]) -> void:
assert(mapper.supports_map(node))
assert(mapper.size() == domains_map.size())

_node = node
_mapper = mapper
_domains_map = domains_map

func read_domain(coords: Vector2i) -> WFCBitSet:
var read := _mapper.read_cell(_node, coords)

if read < 0:
return null

return _domains_map[read]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extends WFC2DPrecondition2DNullSettings

class_name WFC2DPrecondition2DRemapByMapSettings

@export_node_path
var class_map: NodePath

func _learn_classes(mapper: WFCMapper2D, map: Node) -> Array[WFCBitSet]:
var res: Array[WFCBitSet] = []

for i in range(mapper.size()):
var set := WFCBitSet.new(mapper.size())
set.set_bit(i, true)
res.push_back(set)

var used_rect := mapper.get_used_rect(map)

for row in range(used_rect.position.y, used_rect.end.y):
var row_set := WFCBitSet.new(mapper.size())
for col in range(used_rect.position.x, used_rect.end.x):
var tile := mapper.read_cell(map, Vector2i(col, row))
if tile >= 0:
row_set.set_bit(tile, true)

for tile in row_set.iterator():
res[tile].union_in_place(row_set)

return res

func create_precondition(parameters: CreationParameters) -> WFC2DPrecondition:
assert(class_map != null)
var class_map_node := parameters.generator_node.get_node(class_map)
assert(parameters.problem_settings.rules.mapper.supports_map(class_map_node))

return WFC2DPreconditionRemap.new(
parameters.target_node,
parameters.problem_settings.rules.mapper,
_learn_classes(parameters.problem_settings.rules.mapper, class_map_node)
)

0 comments on commit d6e0dd6

Please sign in to comment.