-
Notifications
You must be signed in to change notification settings - Fork 0
/
wfc_interface_test.gd
189 lines (134 loc) · 4.55 KB
/
wfc_interface_test.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# InterfaceTest
# Static output size
# rules: random 0-1
static func wfc_interface_test(w: int, h: int) -> Array[int]:
var result: Array[int] = [];
result.resize(w*h);
result.fill(0);
for i in w:
for j in h:
result[j*w+i] = i % 2;
return result;
static var posibilityMap = [];
static var paternSize = Vector2(10,7);
static var patern: Array[int] = [
1,1,1,0,0,0,0,0,0,0,
1,2,1,0,0,0,0,0,0,0,
1,1,1,0,4,0,0,0,0,0,
0,0,0,0,4,0,0,0,0,0,
0,3,3,0,4,0,0,1,2,0,
0,3,3,0,4,0,0,3,4,0,
0,0,0,0,0,0,0,0,0,0,
]
static var cardSize = Vector2(3,3);
static var cardRadius = Vector2(ceil(cardSize[0] / 2), ceil(cardSize[1] / 2));
static var deck;
static func init(size):
deck = build_deck(patern, paternSize, cardSize);
print('deck ', deck.size());
for c in deck:
print(c)
posibilityMap.resize(size);
posibilityMap.fill(deck.duplicate());
static func wfc_v1(w: int, h: int, data, onlyOneStep: bool) -> Array[int]:
var result: Array[int] = data;
var cardCenterIndex = floor(cardSize[0] / 2)*cardSize[0] + floor(cardSize[1] / 2);
var steps = 1 if onlyOneStep else w*h;
for _unused in steps:
print('------------------------------ ', _unused);
var minValueFields = get_min_posibility_fields(posibilityMap);
print('minValueFields: ', minValueFields);
# Field we will be filling
var fieldIndex = minValueFields.pick_random();
#var fieldIndex = minValueFields[0];
if(fieldIndex == null):
print('NULL!');
continue;
print('fieldIndex', fieldIndex);
# Card we will be using
var card = posibilityMap[fieldIndex].pick_random();
#var card = posibilityMap[fieldIndex][0];
print('CARD: ', card);
var valueToSet = card[cardCenterIndex]
print('PREV VALUE: ', result[fieldIndex]);
print('SET ', fieldIndex, ' <=', valueToSet);
result[fieldIndex] = valueToSet;
posibilityMap[fieldIndex] = [];
var fieldY = floor(fieldIndex / w);
var fieldX = fieldIndex % w;
for x in range(-cardRadius[0]+1, cardRadius[0]):
for y in range(-cardRadius[1]+1, cardRadius[1]):
#print(x, ' | ', y);
if x == 0 && y == 0:
continue;
if fieldX + x < 0 || fieldX + x >= w:
continue;
if fieldY + y < 0 || fieldY + y >= h:
continue;
#print({
#'fieldIndex': fieldIndex,
#'fieldX': fieldX,
#'fieldY': fieldY,
#'x': x,
#'y': y,
#'w': w,
#'h': h,
#});
var fieldIndexToReduce = fieldIndex+(y*w)+x;
#print('fieldIndexToReduce: ', fieldIndexToReduce);
var fieldToReduce: Array = posibilityMap[fieldIndexToReduce];
#print('CHECKING: ', fieldIndexToReduce)
#print('pre reduce ', posibilityMap[fieldIndexToReduce].size())
var cardFieldIndex = (cardCenterIndex-(y*cardSize[0]))-x;
#print('cardFieldIndex: ', cardFieldIndex);
if (fieldToReduce.size() == 0):
#print('validate: ', fieldIndexToReduce);
var validCardIndex = (cardCenterIndex+(y*cardSize[0]))+x;
#if card[validCardIndex ] != result[fieldIndexToReduce]:
#print(card[validCardIndex ], ' ----- ', result[fieldIndexToReduce]);
posibilityMap[fieldIndexToReduce] = fieldToReduce.filter(func(c):
var r = c[cardFieldIndex] == valueToSet;
#if(r):
#print('ALLOW:', c);
return r
);
#print('post reduce #2: ', posibilityMap[fieldIndexToReduce].size())
#print('result: ', result);
return result;
static func get_min_posibility_fields(map: Array) -> Array[int]:
var minValue = 999999;
var result: Array[int] = [];
var size = map.size();
for i in size:
var fieldPosibilitySize = map[i].size();
if fieldPosibilitySize == 0:
continue;
if fieldPosibilitySize > minValue:
continue;
if fieldPosibilitySize == minValue:
result.append(i);
if fieldPosibilitySize < minValue:
minValue = fieldPosibilitySize;
result = [i];
print('minValue: ', minValue)
return result
static func build_deck(patern: Array[int], patternSize: Vector2, cardSize: Vector2):
var result = [];
# Iterujemy po kazdym polu w paternie
for i in patternSize[0]:
for j in patternSize[1]:
var card = [];
for x in range(-cardRadius[0]+1, cardRadius[0]):
for y in range(-cardRadius[1]+1, cardRadius[1]):
if (i + x < 0 || i + x >= patternSize[0]):
#print('null x: ', i+x);
card.append(null);
continue;
if (j + y < 0 || j + y >= patternSize[1]):
#print('null y: ', j+y);
card.append(null);
continue;
card.append(patern[(j+y)*patternSize[0] + i+x]);
print(card);
result.append(card);
return result;