-
Notifications
You must be signed in to change notification settings - Fork 14
/
mapper_2d.gd
123 lines (104 loc) · 4.02 KB
/
mapper_2d.gd
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
extends Resource
## Mapper for 2D WFC
##
## The name [b]mapper[/b] describes two functions of this class:
## it provides an interface between the algorithm and a [b]map[/b] node and
## [b]map[/b]s tiles from format specific for certain map type to (and from) numbers used by the WFC
## algorithm.
## [br]
## Mapper allows the algorithm to access a map node as something like a 2D-array of numbers.
class_name WFCMapper2D
## Learn tile types from given map node.
func learn_from(_map: Node):
@warning_ignore("assert_always_false")
assert(false)
## Returns rect of target map that contains all non-empty cells.
func get_used_rect(_map: Node) -> Rect2i:
@warning_ignore("assert_always_false")
assert(false)
return Rect2i()
## Read cell from map and return a mapped code.
## [br]
## Returns a negative value if cell is empty or mapping for the cell is missing.
func read_cell(_map: Node, _coords: Vector2i) -> int:
@warning_ignore("assert_always_false")
assert(false)
return -1
## Read metadata attribute values associated with given cell type.
## [br]
## May return array of multiple values if cell type consists of multiple objects having metadata.
## E.g. combinations of different tiles in multi-layer tilemap.
func read_tile_meta(_tile: int, _meta_name: String) -> Array:
@warning_ignore("assert_always_false")
assert(false)
return []
## Reads meta of given tile (see [method read_tile_meta]) and converts it to a single boolean value.
## [br]
## Returns [code]true[/code] iff there is at least one truthy meta value.
func read_tile_meta_boolean(tile: int, meta_name: String) -> bool:
for v in read_tile_meta(tile, meta_name):
if v:
return true
return false
## Name of a metadata attribute/custom data layer (as interpreted by [method read_tile_meta]) used
## to read tile probabilities.
@export
var probability_meta_key: String = "wfc_probability"
## Read probability value assigned to given tile type.
## [br]
## By default uses values from metadata attribute using name from probability_meta_key property.
## Sub-classes may override this behavior.
func read_tile_probability(tile: int) -> float:
if tile < 0:
return 0.0
assert(tile < size())
var probability := 1.0
for p in read_tile_meta(tile, probability_meta_key):
probability *= p
return probability
## Write a cell to map.
## [br]
## [param _code] should be inside acceptable range for mapped codes.
func write_cell(_map: Node, _coords: Vector2i, _code: int):
@warning_ignore("assert_always_false")
assert(false)
## Returns number of cell types known by the mapper.
func size() -> int:
@warning_ignore("assert_always_false")
assert(false)
return 0
## Check if this mapper is capable of working with given map node.
func supports_map(_map: Node) -> bool:
@warning_ignore("assert_always_false")
assert(false)
return false
## Reset state (everything learned in [method learn_from] calls) of this mapper.
func clear():
@warning_ignore("assert_always_false")
assert(false)
## Return true if this mapper is ready to read/write a map.
func is_ready() -> bool:
return size() > 0
## Values returned by [method get_initial_rule].
enum InitialRule {
UNKNOWN = 0,
ALLOWWED = 1,
FORBIDDEN = 2,
}
## Get initial WFC rule for a pair of tiles.
## [br]
## Checks if [param _tile1] and [param _tile2] can be adjacent according to the rules discoverable
## by the mapper itself.
## Returns [member WFCMapper2D.InitialRule.ALLOWED] if they can,
## [member WFCMapper2D.InitialRule.FORBIDDEN] if they cannot and
## [member WFCMapper2D.InitialRule.UNKNOWN] if it's neither explicitly allowed or forbidden.
func get_initial_rule(_tile1: int, _tile2: int, _direction: Vector2i) -> InitialRule:
return InitialRule.UNKNOWN
## Returns [code]true[/code] if there are some WFC rules discoverable by this mapper.
## I.e. if [method WFCMapper2D.get_initial_rule] doesn't always return
## [member WFCMapper2D.InitialRule.UNKNOWN].
## [br]
## It may still return [code]true[/code] if [method WFCMapper2D.get_initial_rule] always returns
## [member WFCMapper2D.InitialRule.UNKNOWN].
func has_initial_rules() -> bool:
return false