-
Notifications
You must be signed in to change notification settings - Fork 1
/
overmapper-2.go
617 lines (588 loc) · 14.8 KB
/
overmapper-2.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// See LICENSE.txt for licensing information.
package main
import (
"bytes"
"encoding/gob"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
// Terrain holds possible symbols (for different orientations) and color name
// string for an overmap terrain entity.
type Terrain struct {
Symbols []string
Color string
}
// MapStats holds dimensional data over given set of maps.
type MapStats struct {
MinX, MaxX int
MinY, MaxY int
Width, Height int
Seen int
Area int
Coverage float64
}
// Recalc recalculates Width and Height based on the dimensions.
func (s *MapStats) Recalc() {
s.Width = -s.MinX + 1 + s.MaxX
s.Height = -s.MinY + 1 + s.MaxY
s.Area = s.Width * s.Height
s.Coverage = (float64(s.Seen) / float64(s.Area)) * 100.0
}
// String retruns a string representation of the map statistics.
func (s *MapStats) String() string {
return fmt.Sprintf("Maps range from %dx%d to %dx%d for an area of %dx%d with %d/%d - %.2f visited",
s.MinX, s.MinY, s.MaxX, s.MaxY, s.Width, s.Height, s.Seen, s.Area, s.Coverage)
}
var (
flagVerbose bool // be verbose
flagPlain bool // convert to plain-text
flagAxes bool // print coordinate axes
flagWidth int // overmap width
flagHeight int // overmap height
flagLayer int // overmap layer
termap = make(map[string]Terrain, 1024) // global overmap terrain map
lines = map[int32]string{ // mappings for line glyphs
4194424: "\u2502",
4194417: "\u2500",
4194413: "\u2514",
4194412: "\u250C",
4194411: "\u2510",
4194410: "\u2518",
4194420: "\u251C",
4194422: "\u2534",
4194421: "\u2524",
4194423: "\u252C",
4194414: "\u253C",
}
roads = map[string]string{ // mappings for road glyphs
"north": "│",
"south": "│",
"west": "─",
"east": "─",
"nesw": "┼",
"esw": "┬",
"nsw": "┤",
"new": "┴",
"nes": "├",
"ns": "│",
"ew": "─",
"wn": "┘",
"ne": "└",
"sw": "┐",
"es": "┌",
}
dirs = []string{"north", "east", "south", "west", "ns", "ew", "sn", "we"} // possible orientation etc. strings
htmlStart = `<html><head><style>
body { background: black; color: #aaaaaa; }
.cl_white { color: #ffffff; }
.cl_blue { color: #0000ff; }
.cl_red { color: #ff0000; }
.cl_brown { color: #a52a2a; }
.cl_green { color: #008000; }
.cl_cyan { color: #00ffff; }
.cl_dark_gray { color: #a9a9a9; }
.cl_magenta { color: #ff00ff; }
.cl_yellow { color: #ffff00; }
.cl_light_blue { color: #add8e6; }
.cl_light_green { color: #90ee90; }
.cl_light_red { color: #ff5555; }
.cl_i_ltred { color: black; background: #ff5555; }
.cl_light_gray { color: #d3d3d3; }
.cl_i_ltgray { color: black; background: #d3d3d3; }
.cl_light_cyan { color: #e0ffff; }
.cl_ltgray_yellow { color: #d3d3d3; background: #ffff00; }
.cl_pink { color: #ffc0cb; }
.cl_yellow_magenta { color: #ffff00; background: #ff00ff; }
.cl_white_magenta { color: #ffffff; background: #ff00ff; }
.cl_i_magenta { color: black; background: #ff00ff; }
.cl_pink_magenta { color: #ffc0cb; background: #ff00ff; }
.cl_i_green { color: black; background: #008000; }
.cl_i_brown { color: black; background: #a52a2a; }
.cl_h_yellow { color: #ffff00; background: #0000ff; }
.cl_h_dkgray { color: #a9a9a9; background: #0000ff; }
.cl_i_ltblue { color: black; background: #add8e6; }
.cl_i_blue { color: black; background: #0000ff; }
.cl_i_red { color: black; background: #ff0000; }
.cl_ltgreen_yellow { color: #d3d3d3; background: #ffff00; }
.cl_white_white { background: white; }
.cl_i_ltcyan { color: black; background: #e0ffff; }
.cl_yellow_cyan { color: #ffff00; background: #00ffff; }
</style><meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
</head><body><pre>`
htmlEnd = `</pre></body></html>`
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options...] <command> <path>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n Options:\n\n")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, `
Commands:
prepare /path/to/cdda
Convert C:DDA overmap terrain data to internal format.
File 'terrain.dat' will be created in current directory.
NOTE: You need to have the above file for convert to work.
You should also re-run it periodically to update the data
(e.g. if you see missing tile messages when converting).
convert /path/to/save > /path/to/output.file
This will try to convert your save data to HTML by default.
It will output to stdout so you most likely want to redirect it
to a file ("> test.html"). You can change the format using the "-p"
option (for plain text).`)
os.Exit(1)
}
flag.BoolVar(&flagVerbose, "v", false, "be verbose")
flag.BoolVar(&flagPlain, "p", false, "convert to plain-text")
flag.BoolVar(&flagAxes, "a", false, "print coordinate axes")
flag.IntVar(&flagWidth, "ow", 180, "overmap width")
flag.IntVar(&flagHeight, "oh", 180, "overmap height")
flag.IntVar(&flagLayer, "ol", 10, "overmap layer")
}
func main() {
flag.Parse()
if flag.NArg() != 2 {
flag.Usage()
}
switch flag.Arg(0) {
case "prepare":
runPrepare(flag.Arg(1))
if flagVerbose {
fmt.Fprintf(os.Stderr, "Parsed %d terrain items\n", len(termap))
}
saveTermap()
case "convert":
loadTermap()
runConvert(flag.Arg(1))
default:
flag.Usage()
}
}
// die prints the error to stderr end exits.
func die(err error) {
fmt.Fprintln(os.Stderr, "FATAL: ", err)
os.Exit(2)
}
// warn prints a warning to stderr.
func warn(str string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "WARN: "+str, args...)
}
// oMapElem holds overmap data from a save.
type oMapElem struct {
ID string
Len int
}
// Custom unmarshaling due to the structure of the overmap data in a save.
func (e *oMapElem) UnmarshalJSON(b []byte) (err error) {
var id string
var length int
tgt := []interface{}{&id, &length}
if err := json.Unmarshal(b, &tgt); err != nil {
return err
}
e.ID = id
e.Len = length
return
}
// getLayer returns a parsed overmap layer or dies trying.
func getLayer(which int, path string) (layer []oMapElem, err error) {
which++ // as we start from 1 due to silly delimiter counting later on
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
if _, err = file.Seek(12, os.SEEK_CUR); err != nil {
return
}
dec := json.NewDecoder(file)
current := 0
level := 0
for {
t, err := dec.Token()
if err != nil {
die(err)
}
d, ok := t.(json.Delim)
if !ok {
continue
}
if d == '[' {
level++
} else if d == ']' {
level--
}
if level == 1 {
current++
}
if current == which {
break
}
}
err = dec.Decode(&layer)
return
}
// runConvert is the main entry point for conversion.
// It will die on fatal errors.
func runConvert(root string) {
maps, stats, err := getMaps(root)
if err != nil {
die(err)
}
rowLineBufs := make([][]bytes.Buffer, stats.Width)
for x := 0; x < stats.Width; x++ {
rowLineBufs[x] = make([]bytes.Buffer, flagHeight)
}
if !flagPlain {
fmt.Println(htmlStart)
}
for y, row := range maps {
for x, mapPath := range row {
if mapPath == "" {
for line := 0; line < flagHeight; line++ {
for i := 0; i < flagWidth; i++ {
rowLineBufs[x][line].Write([]byte(" "))
}
}
continue
}
data, err := getLayer(flagLayer, mapPath)
if err != nil {
die(err)
}
line := 0
pos := 0
for _, e := range data {
color, symbol := getTer(e.ID)
if !flagPlain {
if symbol == "<" {
symbol = "<"
} else if symbol == ">" {
symbol = ">"
}
}
for e.Len > 0 {
lineLeft := flagWidth - pos
var howMany int
if lineLeft < e.Len {
howMany = lineLeft
} else {
howMany = e.Len
}
if color != "" && !flagPlain {
rowLineBufs[x][line].WriteString("<span class=\"cl_")
rowLineBufs[x][line].WriteString(color)
rowLineBufs[x][line].WriteString("\">")
}
for c := 0; c < howMany; c++ {
rowLineBufs[x][line].WriteString(symbol)
}
if color != "" && !flagPlain {
rowLineBufs[x][line].WriteString("</span>")
}
pos += howMany
e.Len -= howMany
if pos == flagWidth {
pos = 0
line++
}
}
}
}
if flagAxes && y == 0 {
fmt.Print("______")
for x := 0; x < stats.Width; x++ {
marker := fmt.Sprintf("| %d", stats.MinX+x)
fmt.Print(marker)
for c := 0; c < (flagWidth - len(marker)); c++ {
fmt.Print(" ")
}
}
fmt.Println()
}
for line := 0; line < flagHeight; line++ {
if flagAxes {
if line == 0 {
fmt.Printf("%5d ", stats.MinY+y)
} else if line == (flagHeight - 1) {
fmt.Print("______")
} else {
fmt.Print(" ")
}
}
for _, bufs := range rowLineBufs {
bufs[line].WriteTo(os.Stdout)
bufs[line].Reset()
}
fmt.Printf("\n")
}
}
if !flagPlain {
fmt.Println(htmlEnd)
}
}
// getTer tries to retrieve color and symbol strings for a given overmap
// terrain id. It handles rotations, lines and roads (as special cases).
func getTer(id string) (color, symbol string) {
tile, ok := termap[id]
if ok {
return tile.Color, tile.Symbols[0]
}
splitAt := strings.LastIndex(id, "_")
if splitAt < 1 {
warn("Couldn't split id: %s\n", id)
return "", "?"
}
suffix := id[splitAt+1:]
nid := id[0:splitAt]
if nid == "road" {
symbol, ok = roads[suffix]
if !ok {
warn("No road suffix for %s (%s)\n", suffix, id)
return "", "?"
}
return
}
tile, ok = termap[nid]
if !ok {
warn("Tile not found %s (%s %s)\n", nid, id, suffix)
return "", "?"
}
dir := 0
for i, d := range dirs {
if d == suffix {
dir = i
break
}
}
dir = dir % 4
if dir < len(tile.Symbols) {
symbol = tile.Symbols[dir]
} else {
symbol = tile.Symbols[0]
}
color = tile.Color
return
}
// getMaps looks over a save directory and composes an array of arrays
// of maps for processing. The returned array has dimensions of the extent
// of the overmaps saved, with empty overmaps being empty strings.
// It also returns overall dimensional stats.
func getMaps(root string) (maps [][]string, stats *MapStats, err error) {
type coord struct {
x, y int
}
stats = &MapStats{}
seen := make(map[coord]string)
err = filepath.Walk(root,
func(path string, info os.FileInfo, ierr error) (oerr error) {
if ierr != nil {
return ierr
}
if info.IsDir() {
return nil
}
fname := filepath.Base(path)
if ok, _ := filepath.Match("o.*", fname); !ok {
return nil
}
if flagVerbose {
fmt.Fprintf(os.Stderr, "File: %s\n", path)
}
parts := strings.Split(fname, ".")
if len(parts) != 3 {
warn("Malformed file match: %s\n", fname)
return nil
}
x, oerr := strconv.Atoi(parts[1])
if oerr != nil {
return
}
y, oerr := strconv.Atoi(parts[2])
if oerr != nil {
return
}
seen[coord{x, y}] = path
stats.Seen++
if x < stats.MinX {
stats.MinX = x
}
if x > stats.MaxX {
stats.MaxX = x
}
if y < stats.MinY {
stats.MinY = y
}
if y > stats.MaxY {
stats.MaxY = y
}
return nil
})
stats.Recalc()
if flagVerbose {
fmt.Fprintln(os.Stderr, stats)
}
maps = make([][]string, stats.Height)
for y := 0; y < stats.Height; y++ {
maps[y] = make([]string, stats.Width)
for x := 0; x < stats.Width; x++ {
rx := stats.MinX + x
ry := stats.MinY + y
if path, ok := seen[coord{rx, ry}]; ok {
maps[y][x] = path
}
}
}
return
}
// runPrepare is the main entry point for data preprocessing.
// It will die on fatal errors.
// The idea here is that we walk all the possible JSONs paths and try to
// extract anything that looks like a valid overmap terrain entity.
// We can then gob-encode what we found and have it ready to use for
// all future converts.
func runPrepare(root string) {
err := processTerrainData(filepath.Join(root, "data", "json",
"overmap_terrain.json"))
if err != nil {
die(err)
}
err = filepath.Walk(filepath.Join(root, "data", "json", "mapgen"), walkForMapgen)
if err != nil {
die(err)
}
err = filepath.Walk(filepath.Join(root, "data", "mods"), walkForMods)
if err != nil {
die(err)
}
}
// walkForMapgen walks recursively looking for JSON files and tries to extract
// overmap terrain entities form them.
func walkForMapgen(path string, info os.FileInfo, ierr error) (oerr error) {
if ierr != nil {
return ierr
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".json" {
return nil
}
return processTerrainData(path)
}
// walkForMods walks recursively looking for JSON files and tries to extract
// overmap terrain entities form them.
func walkForMods(path string, info os.FileInfo, ierr error) (oerr error) {
if ierr != nil {
return ierr
}
if info.IsDir() {
return nil
}
if filepath.Base(path) != "overmap_terrain.json" {
return nil
}
return processTerrainData(path)
}
// processTerrainData tries to extract all overmap entities form a given
// JSON file.
func processTerrainData(path string) (err error) {
type oter struct {
ID string `json:"id"`
Color string `json:"color"`
Syms interface{} `json:"sym"`
}
if flagVerbose {
fmt.Fprintf(os.Stderr, "File: %s\n", path)
}
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
dec := json.NewDecoder(file)
_, err = dec.Token() // Skip over '['
if err != nil {
return
}
var rawTer oter
for dec.More() {
err = dec.Decode(&rawTer)
if err != nil {
warn("Bad item format: %s\n", err)
continue
}
if rawTer.Syms == nil {
continue
}
if _, ok := termap[rawTer.ID]; ok {
warn("Terrain %v already defined\n", rawTer.ID)
}
var syms []string
switch tsym := rawTer.Syms.(type) {
case float64:
syms = make([]string, 1)
syms[0] = parseSym(int32(tsym))
case []interface{}:
syms = make([]string, len(tsym))
for i, ttsym := range tsym {
// below will blow up with appropriate message on wrong type
syms[i] = parseSym(int32(ttsym.(float64)))
}
default:
warn("Unkown sym type: %v\n", tsym)
continue
}
ter := Terrain{syms, rawTer.Color}
termap[rawTer.ID] = ter
if flagVerbose {
fmt.Fprintf(os.Stderr, "Parsed terrain: %v = %v\n", rawTer.ID, ter)
}
}
return
}
// parseSym tries to parse a symbol (integer) into a string.
func parseSym(raw int32) string {
if raw <= 255 {
return string(raw)
}
if str, ok := lines[raw]; ok {
return str
}
warn("No LINE found for: %v\n", raw)
return "?"
}
// saveTermap saves the global terrain data or dies.
func saveTermap() {
file, err := os.Create("terrain.dat")
if err != nil {
die(err)
}
defer file.Close()
enc := gob.NewEncoder(file)
if err = enc.Encode(termap); err != nil {
die(err)
}
if flagVerbose {
fmt.Fprintf(os.Stderr, "Saved terrain map to terrain.dat\n")
}
}
// loadTermap loads the global terrain data or dies.
func loadTermap() {
file, err := os.Open("terrain.dat")
if err != nil {
die(err)
}
defer file.Close()
dec := gob.NewDecoder(file)
if err = dec.Decode(&termap); err != nil {
die(err)
}
if flagVerbose {
fmt.Fprintf(os.Stderr, "Loaded terrain map (%d items)\n", len(termap))
}
}