-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
433 lines (332 loc) · 10.6 KB
/
main.go
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"encoding/json"
"flag"
"image"
"image/color"
"image/png"
"log"
"math/rand"
"os"
"path/filepath"
"runtime"
"time"
)
type WFCStruct struct {
Texture string `json:"texture"`
Rotation int8 `json:"rotation"`
AllowRotation bool `json:"allow_rotation,omitempty"`
/*
posX
/----\
negY | | PosY
\----/
negX
*/
PosX int64 `json:"pos_x"` // Front
PosY int64 `json:"pos_y"` // Left
NegX int64 `json:"neg_x"` // Back
NegY int64 `json:"neg_y"` // Right
PosZ int64 `json:"pos_z"` // Top
NegZ int64 `json:"neg_z"` // Bottom
}
var (
Structs []WFCStruct
IndexedStructs = make(map[int64]WFCStruct)
PosXIndices = make(map[int64][]int64) // [PosX_ID] -> Index
PosYIndices = make(map[int64][]int64) // [PosY_ID] -> Index
NegXIndices = make(map[int64][]int64) // [NegX_ID] -> Index
NegYIndices = make(map[int64][]int64) // [NegY_ID] -> Index
PosZIndices = make(map[int64][]int64) // [PosZ_ID] -> Index
NegZIndices = make(map[int64][]int64) // [NegZ_ID] -> Index
IndexedPosX = make(map[int64]int64) // [Index] -> PosX_ID
IndexedPosY = make(map[int64]int64) // [Index] -> PosY_ID
IndexedNegX = make(map[int64]int64) // [Index] -> NegX_ID
IndexedNegY = make(map[int64]int64) // [Index] -> NegY_ID
IndexedPosZ = make(map[int64]int64) // [Index] -> PosZ_ID
IndexedNegZ = make(map[int64]int64) // [Index] -> NegZ_ID
IndexedAssets = make(map[string][][]*color.RGBA64)
Indices []int64
// OutputMatrix [Z, [Y, [X]]]
OutputMatrix [][][]int64
XSize int64 // Width Size
YSize int64 // Length Size
ZSize int64 // Height Size
InputJsonFilePath string
OutputMapImageFilePath string
)
func init() {
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
// My current screen size divided by the size of the tiles (16x16)
flag.Int64Var(&XSize, "x", 3840/16, `X - X amount of tiles`)
flag.Int64Var(&YSize, "y", 2160/16, `Y - Y amount of tiles`)
flag.Int64Var(&ZSize, "z", 1, `Z - Z amount of tiles`)
flag.StringVar(&InputJsonFilePath, "input", "input.json", "Input json file path")
flag.StringVar(&OutputMapImageFilePath, "output", "output.png", "Output map image file path")
flag.Parse()
}
func RotateRGBA64Matrix(matrix [][]*color.RGBA64) [][]*color.RGBA64 {
var i, j int
var matrixLen = len(matrix)
var temp *color.RGBA64
for i = 0; i < matrixLen/2; i++ {
for j = i; j < matrixLen-i-1; j++ {
temp = matrix[i][j]
matrix[i][j] = matrix[matrixLen-1-j][i]
matrix[matrixLen-1-j][i] = matrix[matrixLen-1-i][matrixLen-1-j]
matrix[matrixLen-1-i][matrixLen-1-j] = matrix[j][matrixLen-1-i]
matrix[j][matrixLen-1-i] = temp
}
}
return matrix
}
func GetDuplications(firstArray, secondArray []int64) (returnedArray []int64) {
var allKeys = make(map[int64]int8)
var number int64
for _, number = range firstArray {
allKeys[number] = 1
}
for _, number = range secondArray {
var ok bool
if _, ok = allKeys[number]; ok {
returnedArray = append(returnedArray, number)
}
}
return
}
func AddWFCStructIntoStructs(i int64, wfcStruct WFCStruct) int64 {
IndexedStructs[i] = wfcStruct
Indices = append(Indices, i)
IndexedPosX[i] = wfcStruct.PosX
IndexedPosY[i] = wfcStruct.PosY
IndexedNegX[i] = wfcStruct.NegX
IndexedNegY[i] = wfcStruct.NegY
IndexedPosZ[i] = wfcStruct.PosZ
IndexedNegZ[i] = wfcStruct.NegZ
var ok bool
if _, ok = PosXIndices[wfcStruct.PosX]; !ok {
PosXIndices[wfcStruct.PosX] = make([]int64, 0, 10)
}
PosXIndices[wfcStruct.PosX] = append(PosXIndices[wfcStruct.PosX], i)
if _, ok = PosYIndices[wfcStruct.PosY]; !ok {
PosYIndices[wfcStruct.PosY] = make([]int64, 0, 10)
}
PosYIndices[wfcStruct.PosY] = append(PosYIndices[wfcStruct.PosY], i)
if _, ok = NegXIndices[wfcStruct.NegX]; !ok {
NegXIndices[wfcStruct.NegX] = make([]int64, 0, 10)
}
NegXIndices[wfcStruct.NegX] = append(NegXIndices[wfcStruct.NegX], i)
if _, ok = NegYIndices[wfcStruct.NegY]; !ok {
NegYIndices[wfcStruct.NegY] = make([]int64, 0, 10)
}
NegYIndices[wfcStruct.NegY] = append(NegYIndices[wfcStruct.NegY], i)
if _, ok = PosZIndices[wfcStruct.PosZ]; !ok {
PosZIndices[wfcStruct.PosZ] = make([]int64, 0, 10)
}
PosZIndices[wfcStruct.PosZ] = append(PosZIndices[wfcStruct.PosZ], i)
if _, ok = NegZIndices[wfcStruct.NegZ]; !ok {
NegZIndices[wfcStruct.NegZ] = make([]int64, 0, 10)
}
NegZIndices[wfcStruct.NegZ] = append(NegZIndices[wfcStruct.NegZ], i)
return i + 1
}
func GenerateRotations() {
var index int64 = 0
var wfcStruct WFCStruct
for _, wfcStruct = range Structs {
index = AddWFCStructIntoStructs(index, WFCStruct{
Texture: wfcStruct.Texture,
Rotation: 0,
AllowRotation: false,
PosX: wfcStruct.PosX,
PosY: wfcStruct.PosY,
NegX: wfcStruct.NegX,
NegY: wfcStruct.NegY,
PosZ: wfcStruct.PosZ,
NegZ: wfcStruct.NegZ,
})
if !wfcStruct.AllowRotation {
continue
}
index = AddWFCStructIntoStructs(index, WFCStruct{
Texture: wfcStruct.Texture,
Rotation: 1,
AllowRotation: false,
PosX: wfcStruct.PosY,
PosY: wfcStruct.NegX,
NegX: wfcStruct.NegY,
NegY: wfcStruct.PosX,
PosZ: wfcStruct.PosZ,
NegZ: wfcStruct.NegZ,
})
index = AddWFCStructIntoStructs(index, WFCStruct{
Texture: wfcStruct.Texture,
Rotation: 2,
AllowRotation: false,
PosX: wfcStruct.NegX,
PosY: wfcStruct.NegY,
NegX: wfcStruct.PosX,
NegY: wfcStruct.PosY,
PosZ: wfcStruct.PosZ,
NegZ: wfcStruct.NegZ,
})
index = AddWFCStructIntoStructs(index, WFCStruct{
Texture: wfcStruct.Texture,
Rotation: 3,
AllowRotation: false,
PosX: wfcStruct.NegY,
PosY: wfcStruct.PosX,
NegX: wfcStruct.PosY,
NegY: wfcStruct.NegX,
PosZ: wfcStruct.PosZ,
NegZ: wfcStruct.NegZ,
})
}
}
func LoadInput() (err error) {
var structsFile *os.File
if structsFile, err = os.Open(InputJsonFilePath); err != nil {
return
}
var jsonDecoder = json.NewDecoder(structsFile)
if err = jsonDecoder.Decode(&Structs); err != nil {
return
}
return
}
func GenerateMap() (err error) {
rand.Seed(time.Now().Unix() / 10 * 102)
OutputMatrix = make([][][]int64, ZSize)
var z, y, x int64
for z = 0; z < ZSize; z++ {
OutputMatrix[z] = make([][]int64, YSize)
for y = 0; y < YSize; y++ {
OutputMatrix[z][y] = make([]int64, XSize)
for x = 0; x < XSize; x++ {
var acceptedIndices []int64
if x == 0 {
acceptedIndices = Indices
} else {
var leftIndex = OutputMatrix[z][y][x-1]
var leftPosYID = IndexedPosY[leftIndex]
acceptedIndices = NegYIndices[leftPosYID]
}
if y != 0 {
var topIndex = OutputMatrix[z][y-1][x]
var topNegXID = IndexedNegX[topIndex]
var posXIndices = PosXIndices[topNegXID]
acceptedIndices = GetDuplications(acceptedIndices, posXIndices)
}
if z != 0 {
var bottomIndex = OutputMatrix[z-1][y][x]
var topPosZID = IndexedPosZ[bottomIndex]
var posZIndices = NegZIndices[topPosZID]
acceptedIndices = GetDuplications(acceptedIndices, posZIndices)
}
OutputMatrix[z][y][x] = acceptedIndices[rand.Int63n(int64(len(acceptedIndices)))]
}
}
}
return
}
func LoadAssets() (err error) {
for _, wfcStruct := range Structs {
var texturePath = wfcStruct.Texture
var textureName = filepath.Base(texturePath)
var textureFile *os.File
if textureFile, err = os.Open(texturePath); err != nil {
return
}
var textureImage image.Image
if textureImage, err = png.Decode(textureFile); err != nil {
return
}
var imageSize = textureImage.Bounds().Size()
var textureMatrix = make([][]*color.RGBA64, imageSize.X)
var x, y int
for x = 0; x < imageSize.X; x++ {
textureMatrix[x] = make([]*color.RGBA64, imageSize.Y)
for y = 0; y < imageSize.Y; y++ {
var rgba64At = textureImage.(*image.RGBA).RGBA64At(x, y)
textureMatrix[x][y] = &rgba64At
}
}
IndexedAssets[textureName] = textureMatrix
}
return
}
func GenerateMapImage() (err error) {
var outputImage = image.NewRGBA64(image.Rectangle{Max: image.Point{X: int(XSize * 16), Y: int(YSize * 16)}})
var z, y, x int
for z = 0; z < len(OutputMatrix); z++ {
for y = 0; y < len(OutputMatrix[z]); y++ {
for x = 0; x < len(OutputMatrix[z][y]); x++ {
var wfcStruct = IndexedStructs[OutputMatrix[z][y][x]]
var textureName = filepath.Base(wfcStruct.Texture)
var textureMatrix = make([][]*color.RGBA64, len(IndexedAssets[textureName]))
var textureX, textureY int
for textureX = 0; textureX < len(IndexedAssets[textureName]); textureX++ {
textureMatrix[textureX] = make([]*color.RGBA64, len(IndexedAssets[textureName][textureX]))
for textureY = 0; textureY < len(IndexedAssets[textureName][textureX]); textureY++ {
textureMatrix[textureX][textureY] = IndexedAssets[textureName][textureX][textureY]
}
}
var i int8
for {
if i == wfcStruct.Rotation {
break
}
textureMatrix = RotateRGBA64Matrix(textureMatrix)
i = i + 1
}
for textureX = 0; textureX < len(textureMatrix); textureX++ {
for textureY = 0; textureY < len(textureMatrix[textureX]); textureY++ {
outputImage.SetRGBA64((x*len(textureMatrix))+textureX, (y*len(textureMatrix[textureX]))+textureY, *textureMatrix[textureX][textureY])
}
}
}
}
}
var imageFile *os.File
if imageFile, err = os.Create(OutputMapImageFilePath); err != nil {
return
}
err = png.Encode(imageFile, outputImage)
return
}
func main() {
var err error
var startTime = time.Now()
var leadTime = time.Now()
log.Println(`loading input.json...`)
if err = LoadInput(); err != nil {
log.Fatalln(err)
}
log.Printf(`loading input took %s`, time.Since(leadTime).String())
log.Println(`loading assets...`)
leadTime = time.Now()
if err = LoadAssets(); err != nil {
log.Fatalln(err)
}
log.Printf(`loading assets took %s`, time.Since(leadTime).String())
log.Println(`generating rotations...`)
leadTime = time.Now()
GenerateRotations()
log.Printf(`generate rotations took %s`, time.Since(leadTime).String())
log.Println(`generating map...`)
leadTime = time.Now()
if err = GenerateMap(); err != nil {
log.Fatalln(err)
}
log.Printf(`generating map took %s`, time.Since(leadTime).String())
log.Println(`generating map image...`)
leadTime = time.Now()
if err = GenerateMapImage(); err != nil {
log.Fatalln(err)
}
log.Printf(`generating map image took %s`, time.Since(leadTime).String())
log.Printf(`input structures %d`, len(Structs))
log.Printf(`output structures %d`, len(IndexedStructs))
log.Printf(`finished in %s`, time.Since(startTime).String())
}