-
Notifications
You must be signed in to change notification settings - Fork 4
/
simple_tiled_model.lua
290 lines (247 loc) · 7.78 KB
/
simple_tiled_model.lua
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--[[---------------------------------------------------------------------------
-- Copyright (c) 2020 Miku AuahDark
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--]]---------------------------------------------------------------------------
local path = (...):gsub("%.simple_tiled_model$","")
local love = require("love")
local Luaoop = require(path..".Luaoop")
local util = require(path..".util")
local Model = require(path..".model")
local SimpleTiledModel = Luaoop.class("NWFC.SimpleTiledModel", Model)
local symmetryTable = {
L = {
cardinality = 4,
a = function(i) return (i + 1) % 4 end,
b = function(i) return (i % 2 == 0) and (i + 1) or (i - 1) end
},
T = {
cardinality = 4,
a = function(i) return (i + 1) % 4 end,
b = function(i) return (i % 2 == 0) and i or (4 - i) end
},
I = {
cardinality = 2,
a = function(i) return 1 - i end,
b = function(i) return i end
},
["\\"] = {
cardinality = 2,
a = function(i) return 1 - i end,
b = function(i) return 1 - i end
},
X = {
cardinality = 1,
a = function(i) return i end,
b = function(i) return i end
}
}
function SimpleTiledModel:__construct(data, subsetName, width, height, periodic, black)
Model.__construct(self, self, width, height)
-- Data structure:
--[[
data = {
size = tile_size or 16,
unique = false or true,
tileImage = {
[tilename] = love.image.newImageData("path to file"),
...
}
tiles = {
{
name = tile_name,
symmetry = symmetry or "X",
weight = weight or 1.0
}
},
neighbors = {
{left, right},
{left, right},
...
{left, right}
},
subsets = {
[name] = {
"tile1", "tile2", ...
}
}
}
]]
self.periodic = not(not(periodic))
self.black = not(not(black))
self.tilesize = data.size or 16
local unique = not(not(data.unique))
local subset = nil
if subsetName ~= nil then
subset = assert(data.subsets[subsetName], "subset not found")
end
local function tile(f)
local result = {}
for i = 1, self.tilesize * self.tilesize do
result[i] = f((i - 1) % self.tilesize, (i - 1) / self.tilesize)
end
return result
end
local function rotate(a)
return tile(function(x, y)
return a[self.tilesize - y + x * self.tilesize]
end)
end
self.tiles = {}
self.tilenames = {}
local action = {}
local firstOccurrence = {}
for _, tileObject in ipairs(data.tiles) do
local tilename = tileObject.name
if subset == nil or util.contains(subset, tilename) then
local sym = symmetryTable[tileObject.symmetry or "X"] or symmetryTable.X;
self.T = #action
firstOccurrence[tilename] = self.T
for t = 0, sym.cardinality - 1 do
local mapt = {}
mapt[1] = t
mapt[2] = sym.a(t)
mapt[3] = sym.a(mapt[2])
mapt[4] = sym.a(mapt[3])
mapt[5] = sym.b(t)
mapt[6] = sym.b(mapt[2])
mapt[7] = sym.b(mapt[3])
mapt[8] = sym.b(mapt[4])
for s = 1, 8 do
mapt[s] = mapt[s] + self.T
end
action[#action + 1] = mapt
end
if unique then
for t = 0, sym.cardinality - 1 do
local target = tilename.." "..t
local imageData = assert(data.tileImage[target], "missing tileimage")
self.tiles[#self.tiles + 1] = tile(function(x, y)
return util.colorToNumber(love.math.colorToBytes(imageData:getPixel(x, y)))
end)
self.tilenames[#self.tilenames + 1] = target
end
else
local imageData = assert(data.tileImage[tilename], "missing tileimage")
self.tiles[#self.tiles + 1] = tile(function(x, y)
return util.colorToNumber(love.math.colorToBytes(imageData:getPixel(x, y)))
end)
self.tilenames[#self.tilenames + 1] = tilename.." 0"
for t = 1, sym.cardinality - 1 do
self.tiles[#self.tiles + 1] = rotate(self.tiles[self.T + t])
self.tilenames[#self.tilenames + 1] = tilename.." "..t
end
end
for _ = 1, sym.cardinality do
self.weights[#self.weights + 1] = tonumber(tileObject.weight) or 1
end
end
end
self.T = #action
local tempPropagator = {}
for d = 1, 4 do
tempPropagator[d] = {}
self.propagator[d] = {}
for t = 1, self.T do
tempPropagator[d][t] = {}
self.propagator[d][t] = {}
for u = 1, self.T do
tempPropagator[d][t][u] = false
end
end
end
for _, neighbor in ipairs(data.neighbors) do
local left = util.splitString(neighbor.left, " ")
local right = util.splitString(neighbor.right, " ")
if subset == nil or (util.contains(subset, left[1]) and util.contains(subset, right[1])) then
local L = action[firstOccurrence[left[1]] + 1][#left == 1 and 1 or (left[2] + 1)]
local D = action[L + 1][2]
local R = action[firstOccurrence[right[1]] + 1][#right == 1 and 1 or (right[2] + 1)]
local U = action[R + 1][2]
tempPropagator[1][R + 1][L + 1] = true
tempPropagator[1][action[R + 1][7] + 1][action[L + 1][7] + 1] = true
tempPropagator[1][action[L + 1][5] + 1][action[R + 1][5] + 1] = true
tempPropagator[1][action[L + 1][3] + 1][action[R + 1][3] + 1] = true
tempPropagator[2][U + 1][D + 1] = true
tempPropagator[2][action[D + 1][7] + 1][action[U + 1][7] + 1] = true
tempPropagator[2][action[U + 1][5] + 1][action[D + 1][5] + 1] = true
tempPropagator[2][action[D + 1][3] + 1][action[U + 1][3] + 1] = true
end
end
for t = 0, self.T * self.T - 1 do
local t2 = math.floor(t / self.T) + 1
local t1 = t % self.T + 1
tempPropagator[3][t2][t1] = tempPropagator[1][t1][t2]
tempPropagator[4][t2][t1] = tempPropagator[2][t1][t2]
end
local sparsePropagator = {}
for d = 1, 4 do
local sp = {}
for i = 1, self.T do
sp[i] = {}
end
sparsePropagator[d] = sp
end
for d = 1, 4 do
for t1 = 1, self.T do
local sp = sparsePropagator[d][t1]
local tp = tempPropagator[d][t1]
for t2 = 1, self.T do
if tp[t2] then
sp[#sp + 1] = t2 - 1
end
end
local stt = {}
for i = 1, #sp do
stt[i] = 0
end
self.propagator[d][t1] = stt
for st = 1, #sp do
stt[st] = sp[st]
end
end
end
end
function SimpleTiledModel:isOnBoundary(x, y)
return not(self.periodic) and (x < 0 or y < 0 or x >= self.FMX or y >= self.FMY)
end
function SimpleTiledModel:toImageData(image)
image = image or love.image.newImageData(self.FMX * self.tilesize, self.FMY * self.tilesize)
assert(
image:getWidth() == self.FMX * self.tilesize and
image:getHeight() == self.FMY * self.tilesize,
"invalid image size"
)
if not(self.imageDataFuncObserved) then
self.imageDataFuncObserved = function(x, y)
local px = math.floor(x / self.tilesize)
local py = math.floor(y / self.tilesize)
local tile = self.tiles[self.observed[px + py * self.FMX + 1]]
local tx = x % self.tilesize
local ty = y % self.tilesize
return love.math.colorFromBytes(util.colorFromNumber(tile[ty * self.tilesize + tx + 1]))
end
end
if not(self.observed) then
error("TODO non-observed")
end
image:mapPixel(self.observed and self.imageDataFuncObserved or self.imageDataFunc)
return image
end
return SimpleTiledModel