Skip to content

Commit

Permalink
add empty cell remapping; add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBond committed Sep 13, 2024
1 parent d6e0dd6 commit f48c9e1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions addons/wfc/problems/2d/preconditions/precondition_2d_remap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ class_name WFC2DPreconditionRemap
var _node: Node
var _mapper: WFCMapper2D
var _domains_map: Array[WFCBitSet]
var _default_domain: WFCBitSet

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

_node = node
_mapper = mapper
_domains_map = domains_map
_default_domain = default_domain

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

if read < 0:
return null
return _default_domain

return _domains_map[read]
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ extends WFC2DPrecondition2DNullSettings

class_name WFC2DPrecondition2DRemapByMapSettings

## A map that defines possible tile replacements.
##
## The tiles placed in the target map may be replaced by any tiles that are present in same rows of
## this class map.
##
## E.g. if there are tiles A B C D in the tileset, and the class map contains a row of A B and A C,
## then A tile may be replaced by either A, B or C; B - by A or B; C - by A or C; and the tile D
## will always remain as is.
@export_node_path
var class_map: NodePath

## Remap empty cells of target map to tiles from first row of class map.
##
## If not set, empty cells may be filled with any tile.
@export
var remap_empty: bool = false

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

Expand All @@ -31,9 +45,16 @@ 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))

var classes := _learn_classes(parameters.problem_settings.rules.mapper, class_map_node)
var empty_domain: WFCBitSet = null

if remap_empty:
empty_domain = classes[0]

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

0 comments on commit f48c9e1

Please sign in to comment.