This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
701 lines (624 loc) · 26.1 KB
/
map.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Copyright (c) 2024 Michael D Henderson. All rights reserved.
package main
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/mdhender/ottomap/config"
"github.com/mdhender/ottomap/coords"
"github.com/mdhender/ottomap/directions"
"github.com/mdhender/ottomap/domain"
"github.com/mdhender/ottomap/lbmoves"
"github.com/mdhender/ottomap/parsers/report"
"github.com/mdhender/ottomap/reports"
"github.com/mdhender/ottomap/wxx"
"github.com/spf13/cobra"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
var argsMap struct {
config string // path to configuration file
clanId string // clan id to use
turnId string // turn id to use
debug struct {
units bool
sectionMaps bool
}
show struct {
gridCenters bool
gridCoords bool
gridNumbers bool
}
}
var cmdMap = &cobra.Command{
Use: "map",
Short: "Create a map from a report",
Long: `Load a parsed report and create a map.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if strings.TrimSpace(argsMap.config) != argsMap.config {
log.Fatalf("map: config: leading or trailing spaces are not allowed\n")
} else if path, err := filepath.Abs(argsMap.config); err != nil {
log.Printf("map: config: output: %q\n", argsMap.config)
log.Printf("map: config: %v\n", err)
} else if sb, err := os.Stat(path); err != nil {
log.Fatalf("map: config: %v\n", err)
} else if !sb.Mode().IsRegular() {
log.Fatalf("map: config: %v is not a file\n", path)
} else {
argsIndexReports.config = path
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
log.Printf("maps: todo: detect when a unit is created as an after-move action\n")
log.Printf("map: config: file %s\n", argsMap.config)
cfg, err := config.Load(argsMap.config)
if err != nil {
log.Fatalf("map: config: %v\n", err)
}
if len(cfg.Reports) == 0 {
log.Fatalf("map: config: no reports\n")
}
log.Printf("map: config: path %s\n", cfg.Path)
log.Printf("map: config: output %s\n", cfg.OutputPath)
cfg.Inputs.ClanId = argsMap.clanId
log.Printf("map: config: clan %q\n", cfg.Inputs.ClanId)
if argsMap.show.gridCoords && argsMap.show.gridNumbers {
argsMap.show.gridNumbers = false
}
if argsMap.debug.sectionMaps {
panic("this needs to be fixed")
}
// if turn id is not on the command line, use the current turn from the configuration.
if argsMap.turnId == "" {
// assumes that the configuration's reports are sorted by turn id.
rptCurr := cfg.Reports[len(cfg.Reports)-1]
cfg.Inputs.TurnId = rptCurr.TurnId
cfg.Inputs.Year = rptCurr.Year
cfg.Inputs.Month = rptCurr.Month
} else {
// convert command line's yyyy-mm to year, month
if yyyy, mm, ok := strings.Cut(argsMap.turnId, "-"); !ok {
log.Fatalf("map: invalid turn %q\n", argsMap.turnId)
} else if yyyy = strings.TrimSpace(yyyy); yyyy == "" {
log.Fatalf("map: invalid turn %q\n", argsMap.turnId)
} else if cfg.Inputs.Year, err = strconv.Atoi(yyyy); err != nil {
log.Fatalf("map: invalid turn %q: year %v\n", argsMap.turnId, err)
} else if mm = strings.TrimSpace(mm); mm == "" {
log.Fatalf("map: invalid turn %q\n", argsMap.turnId)
} else if cfg.Inputs.Month, err = strconv.Atoi(mm); err != nil {
log.Fatalf("map: invalid turn %q: month %v\n", argsMap.turnId, err)
} else {
cfg.Inputs.TurnId = fmt.Sprintf("%04d-%02d", cfg.Inputs.Year, cfg.Inputs.Month)
}
}
log.Printf("map: config: turn year %4d\n", cfg.Inputs.Year)
log.Printf("map: config: turn month %4d\n", cfg.Inputs.Month)
// update the ignore flag based on the turn from the configuration
for _, rpt := range cfg.Reports {
if rpt.Clan == argsMap.clanId {
rptTurnId := fmt.Sprintf("%04d-%02d", rpt.Year, rpt.Month)
if rptTurnId > cfg.Inputs.TurnId {
rpt.Ignore = true
}
}
}
// collect the reports that we're going to process
var allReports []*reports.Report
for _, rpt := range cfg.Reports {
if rpt.Ignore {
continue
}
allReports = append(allReports, rpt)
}
if len(allReports) == 0 {
log.Fatalf("map: files: no files matched constraints\n")
}
//log.Printf("map: reports %d\n", len(allReports))
log.Printf("map: todo: followers are not updated after movement\n")
log.Printf("map: todo: hexes are not assigned for each step in the results\n")
log.Printf("map: todo: named hexes that are only in the status line are missed\n")
log.Printf("map: todo: walk the hex reports and update grid as well as ending coordinates\n")
// create a map for every movement result we have
var allMovementResults []*lbmoves.MovementResults
// this hex is the clan's starting hex from the first turn report
var originHex *wxx.Hex
// parse the report files into a single map
for _, rpt := range cfg.Reports {
if rpt.Ignore {
if cfg.Inputs.ShowIgnoredReports {
log.Printf("map: report %s: ignored report\n", rpt.Id)
}
continue
}
// load the report file
data, err := os.ReadFile(rpt.Path)
if err != nil {
log.Fatalf("map: report %s: %v", rpt.Path, err)
}
//log.Printf("map: report %s: loaded %8d bytes\n", rpt.Id, len(data))
// split the report into sections before parsing it
rpt.Sections, err = reports.Sections(data, cfg.Inputs.ShowSkippedSections)
//log.Printf("map: report %s: loaded %8d sections\n", rpt.Id, len(rpt.Sections))
if err != nil {
for _, section := range rpt.Sections {
if section.Error != nil {
log.Printf("map: report %s: section %s: %v\n", rpt.Id, section.Id, section.Error)
}
}
log.Fatalf("map: report %s: please fix errors listed above, then restart\n", rpt.Id)
}
// each section may contain multiple movement results, so we need to collect them all
// parse the report, stopping if there's an error
for _, section := range rpt.Sections {
//log.Printf("map: report %s: section %2s: need to extract grid hex data\n", rpt.Id, section.Id)
turnId, unitId, prevGridCoords := rpt.TurnId, section.UnitId, section.PrevCoords
missingPrevGridCoords := prevGridCoords == ""
if missingPrevGridCoords {
// this can happen when a unit is created as an after-move action.
// it can also happen when a unit is created as a before-move action and doesn't move.
log.Printf("map: report %s: section %2s: turn %s: unit %-6s: warning: missing coordinates\n", rpt.Id, section.Id, turnId, unitId)
} else if strings.HasPrefix(prevGridCoords, "##") {
// remove these and let the walk logic populate them
//log.Printf("map: report %s: section %2s: turn %s: unit %-6s: stripping %q\n", rpt.Id, section.Id, turnId, unitId, prevGridCoords)
prevGridCoords = ""
} else {
//log.Printf("map: report %s: section %2s: turn %s: unit %-6s: keeping %q\n", rpt.Id, section.Id, turnId, unitId, prevGridCoords)
}
mrl := &lbmoves.MovementResults{
TurnId: turnId,
UnitId: unitId,
StartingGridCoordinates: prevGridCoords,
}
allMovementResults = append(allMovementResults, mrl)
if section.StatusLine == nil {
log.Fatalf("map: report %s: section %2s: no status line\n", rpt.Id, section.Id)
} else {
steps, err := lbmoves.ParseMoveResults(turnId, unitId, section.StatusLine, cfg.Inputs.ShowSteps)
if err != nil {
log.Fatalf("map: report %s: section %2s: %v\n", rpt.Id, section.Id, err)
} else if len(steps) != 1 {
log.Fatalf("map: report %s: section %2s: want 1 step, got %d\n", rpt.Id, section.Id, len(steps))
}
mrl.StatusLine = steps[0]
}
if section.FollowsLine != nil {
//log.Printf("map: report %s: section %2s: follows %q\n", rpt.Id, section.Id, section.FollowsLine)
steps, err := lbmoves.ParseMoveResults(turnId, unitId, section.FollowsLine, cfg.Inputs.ShowSteps)
if err != nil {
log.Fatalf("map: report %s: section %2s: %v\n", rpt.Id, section.Id, err)
}
mrl.Follows = steps[0].Follows
}
if section.MovementLine != nil {
//log.Printf("map: report %s: section %2s: moves %q\n", rpt.Id, section.Id, section.MovementLine)
steps, err := lbmoves.ParseMoveResults(turnId, unitId, section.MovementLine, cfg.Inputs.ShowSteps)
if err != nil {
log.Fatalf("map: report %s: section %2s: %v\n", rpt.Id, section.Id, err)
}
mrl.MovementReports = append(mrl.MovementReports, steps...)
}
for _, scoutLine := range section.ScoutLines {
if scoutLine != nil {
//log.Printf("map: report %s: section %2s: scouts %q\n", rpt.Id, section.Id, scoutLine)
steps, err := lbmoves.ParseMoveResults(turnId, unitId, scoutLine, cfg.Inputs.ShowSteps)
if err != nil {
log.Fatalf("map: report %s: section %2s: %v\n", rpt.Id, section.Id, err)
}
mrl.ScoutReports = append(mrl.ScoutReports, steps)
}
}
if missingPrevGridCoords {
// todo: report on if we were able to find the previous coordinates or not
}
}
}
if len(allMovementResults) == 0 {
log.Fatalf("map: no movement results found\n")
}
// users are required to provide starting grid coordinates if they're not already in the report
log.Printf("map: starting grid coordinates: %q\n", allMovementResults[0].StartingGridCoordinates)
if strings.HasPrefix(allMovementResults[0].StartingGridCoordinates, "##") {
log.Printf("map: warning: hidden grid origin: %q\n", allMovementResults[0].StartingGridCoordinates)
if cfg.Inputs.GridOriginId == "" {
log.Fatalf("map: starting grid coordinates must be specified\n")
}
allMovementResults[0].StartingGridCoordinates = cfg.Inputs.GridOriginId + strings.TrimPrefix(allMovementResults[0].StartingGridCoordinates, "##")
log.Printf("map: warning: grid origin set to %q\n", allMovementResults[0].StartingGridCoordinates)
}
movementResultsMap := map[string]*lbmoves.MovementResults{}
for _, mrl := range allMovementResults {
movementResultsMap[fmt.Sprintf("%s.%s", mrl.TurnId, mrl.UnitId)] = mrl
}
// assume that unit moves are in order and create unit follower links
for _, mrl := range allMovementResults {
if mrl.Follows == "" {
continue
}
turnUnitStepId := fmt.Sprintf("%s.%s", mrl.TurnId, mrl.Follows)
theOtherUnit, ok := movementResultsMap[turnUnitStepId]
if !ok {
log.Fatalf("map: turn %s: unit %-8s: follows: %-8s: turn %s not found\n", mrl.TurnId, mrl.UnitId, mrl.Follows, turnUnitStepId)
}
theOtherUnit.Followers = append(theOtherUnit.Followers, mrl)
}
if cfg.Inputs.ShowSteps {
for _, mrl := range allMovementResults {
if boo, err := json.MarshalIndent(mrl.StatusLine, "", "\t"); err == nil {
fmt.Printf("status: %s\n", string(boo))
}
for _, mr := range mrl.MovementReports {
if boo, err := json.MarshalIndent(mr, "", "\t"); err == nil {
fmt.Printf("movement report: %s\n", string(boo))
}
}
for _, scout := range mrl.ScoutReports {
for _, mr := range scout {
if boo, err := json.MarshalIndent(mr, "", "\t"); err == nil {
fmt.Printf("scout report: %s\n", string(boo))
}
}
}
}
}
// get a list of all the turn ids for later use
var allTurnIds []string
turnIdCounter := map[string]int{}
for _, mrl := range allMovementResults {
turnIdCounter[mrl.TurnId] = turnIdCounter[mrl.TurnId] + 1
}
for k := range turnIdCounter {
allTurnIds = append(allTurnIds, k)
}
sort.Strings(allTurnIds)
allHexes := map[string]*wxx.Hex{}
consolidatedMap := &wxx.WXX{}
// unitNode is a unit that will be added to the map.
// it will contain all the unit's moves. the parent
// link is included to help with linking moves together.
type unitNode struct {
Id string
Parent *unitNode
Moves []*report.Unit
}
// initialize the world hex map from the first unit's status line
if len(allMovementResults) == 0 {
log.Fatalf("map: error: no movement results\n")
}
originTurnId := ""
for _, mrl := range allMovementResults[:1] { // just the first result of the first turn
log.Printf("map: %s: %-6s: casting about for origin\n", mrl.TurnId, mrl.UnitId)
originTurnId = mrl.TurnId
statusLine := mrl.StatusLine
start := mrl.StartingGridCoordinates
if start == "" {
log.Fatalf("map: %s: %-6s: origin is not defined for first unit's first turn\n", mrl.TurnId, mrl.UnitId)
}
originGridCoords, err := coords.StringToGridCoords(start)
if err != nil {
log.Fatalf("map: %s: %-6s: toGridCoords: error %v\n", mrl.TurnId, mrl.UnitId, err)
}
log.Printf("map: %s: %-6s: status line: grid %s\n", mrl.TurnId, mrl.UnitId, originGridCoords)
originMapCoords, err := originGridCoords.ToMapCoords()
if err != nil {
log.Fatalf("map: %s: %-6s: toMapCoords: error %v\n", mrl.TurnId, mrl.UnitId, err)
}
gridColumn, gridRow := originMapCoords.GridColumnRow()
originHex = &wxx.Hex{
GridId: originMapCoords.GridId(),
GridCoords: originMapCoords.GridString(),
Offset: wxx.Offset{Column: gridColumn, Row: gridRow},
Terrain: statusLine.Terrain,
Features: wxx.Features{
IsOrigin: true,
Created: originTurnId,
Updated: originTurnId,
Visited: originTurnId,
},
}
log.Printf("map: %s: %-6s: status line: %s\n", mrl.TurnId, mrl.UnitId, statusLine.Terrain)
// stuff the origin into our consolidated map because it may never show up again
allHexes[originMapCoords.GridString()] = originHex
}
if originHex == nil {
log.Printf("map: %s: origin is not defined\n", originTurnId)
panic("assert(originHex != nil")
}
// ensure that status line is present for all turns
for _, mrl := range allMovementResults {
if mrl.StatusLine == nil {
log.Fatalf("map: %s: %-6s: status line is missing\n", mrl.TurnId, mrl.UnitId)
}
}
// process the movement results one turn at a time
for n, turnId := range allTurnIds {
// process the movement results for this turn
unitId, unitOrigin, unitEnding := "", "", ""
for _, mrl := range allMovementResults {
if mrl.TurnId != turnId {
continue
}
if unitId == "" {
unitId, unitOrigin = mrl.UnitId, mrl.StartingGridCoordinates
} else if unitId != mrl.UnitId {
log.Printf("turn %-8s: unit %-8s: origin %-8s: ended %-8s\n", turnId, unitId, unitOrigin, unitEnding)
unitId = mrl.UnitId
}
start := mrl.StartingGridCoordinates
if start == "" {
// attempt to use the coordinates from the parent's move
parentTurnUnitStepId := fmt.Sprintf("%s.%s", turnId, lbmoves.ParentId(mrl.UnitId))
//log.Printf("map: %s: %-6s: start %s: parent %-6s: turnId %q\n", mrl.TurnId, mrl.UnitId, start, lbmoves.ParentId(mrl.UnitId), parentTurnUnitStepId)
parentTurn, ok := movementResultsMap[parentTurnUnitStepId]
if !ok {
log.Fatalf("map: %s: %-6s: start %s: parent %-6s: turnId %q: missing\n", mrl.TurnId, mrl.UnitId, start, lbmoves.ParentId(mrl.UnitId), parentTurn.TurnId)
} else if parentTurn.StartingGridCoordinates == "" {
log.Printf("map: %s: %-6s: start %s: parent %-6s: turnId %q: start %q\n", mrl.TurnId, mrl.UnitId, start, lbmoves.ParentId(mrl.UnitId), parentTurn.TurnId, parentTurn.StartingGridCoordinates)
panic(`assert(parentTurn.StartingGridCoordinates != "")`)
}
start, mrl.StartingGridCoordinates = parentTurn.StartingGridCoordinates, parentTurn.StartingGridCoordinates
//log.Printf("map: %s: %-6s: start %s: parent %-6s: turnId %q: start %q\n", mrl.TurnId, mrl.UnitId, start, lbmoves.ParentId(mrl.UnitId), parentTurn.TurnId, parentTurn.StartingGridCoordinates)
}
//log.Printf("map: %s: %-6s: origin %s\n", mrl.TurnId, mrl.UnitId, start)
currGridCoords, err := coords.StringToGridCoords(start)
if err != nil {
log.Fatalf("map: %s: %-6s: toGridCoords: error %v\n", mrl.TurnId, mrl.UnitId, err)
}
//log.Printf("map: %s: %-6s: step %2d: grid %s\n", mrl.TurnId, mrl.UnitId, n, currGridCoords)
currMapCoords, err := currGridCoords.ToMapCoords()
if err != nil {
log.Fatalf("map: %s: %-6s: toMapCoords: error %v\n", mrl.TurnId, mrl.UnitId, err)
}
debugStep := false
for stepNo, step := range mrl.MovementReports {
//log.Printf("map: %s: %-6s: step %2d: mapc %s\n", mrl.TurnId, mrl.UnitId, stepNo+1, currMapCoords)
nextCoords := walk(turnId, mrl.UnitId, allHexes, stepNo+1, step, currMapCoords, debugStep)
//log.Printf("map: %s: %-6s: step %2d: mapc %s next %s\n", mrl.TurnId, mrl.UnitId, stepNo+1, currMapCoords, nextCoords)
currMapCoords = nextCoords
}
//log.Printf("map: %s: %-6s: steps %2d start %s: curr %s\n", mrl.TurnId, mrl.UnitId, len(mrl.MovementReports), start, currMapCoords.GridString())
mrl.EndingGridCoordinates = currMapCoords.GridString()
unitEnding = mrl.EndingGridCoordinates
}
if unitId != "" {
log.Printf("turn %-8s: unit %-8s: origin %-8s: ended %-8s\n", turnId, unitId, unitOrigin, unitEnding)
}
// resolve "follows" links for this turn
for _, mrl := range allMovementResults {
if mrl.TurnId != turnId || mrl.Followers == nil {
continue
}
for _, follower := range mrl.Followers {
//log.Printf("map: %s: %-6s: follower %-8q %q -> %q\n", mrl.TurnId, mrl.UnitId, follower.UnitId, follower.EndingGridCoordinates, mrl.EndingGridCoordinates)
follower.EndingGridCoordinates = mrl.EndingGridCoordinates
}
}
// process the scout results for this turn
for _, mrl := range allMovementResults {
if mrl.TurnId != turnId {
continue
}
for scoutNo, scout := range mrl.ScoutReports {
// each scout starts at the unit's current location
startGridCoords, err := coords.StringToGridCoords(mrl.EndingGridCoordinates)
if err != nil {
log.Fatalf("map: %s: %-6s: toGridCoords: error %v\n", mrl.TurnId, mrl.UnitId, err)
}
//log.Printf("map: %s: %-6s: scout %d: grid %s\n", mrl.TurnId, mrl.UnitId, scoutNo+1, startGridCoords)
currMapCoords, err := startGridCoords.ToMapCoords()
if err != nil {
log.Fatalf("map: %s: %-6s: scout %d: toMapCoords: error %v\n", mrl.TurnId, mrl.UnitId, scoutNo+1, err)
}
debugStep := false
movementReports := scout
for stepNo, step := range movementReports {
//log.Printf("map: %s: %-6s: scout %d: step %2d: mapc %s\n", mrl.TurnId, mrl.UnitId, scoutNo+1, stepNo+1, currMapCoords)
nextCoords := walk(turnId, mrl.UnitId, allHexes, stepNo+1, step, currMapCoords, debugStep)
//log.Printf("map: %s: %-6s: scout %d: step %2d: mapc %s next %s\n", mrl.TurnId, mrl.UnitId, scoutNo+1, stepNo+1, currMapCoords, nextCoords)
currMapCoords = nextCoords
}
log.Printf("turn %-8s: unit %-8s: origin %-8s: ended %-8s\n", turnId, fmt.Sprintf("%ss%d", mrl.UnitId, scoutNo+1), startGridCoords.String(), currMapCoords.GridString())
}
}
// if there are more turns to process, then the ending position this turn becomes the starting position for the next turn
if n < len(allTurnIds)-1 {
missingEnds, nextTurnId := 0, allTurnIds[n+1]
//log.Printf("map: turnId %s: next turnId %s\n", turnId, nextTurnId)
for _, mrl := range allMovementResults {
if mrl.TurnId != turnId {
continue
}
//log.Printf("map: %s: %-6s: start %s: end: %s\n", mrl.TurnId, mrl.UnitId, mrl.StartingGridCoordinates, mrl.EndingGridCoordinates)
if mrl.EndingGridCoordinates == "" {
log.Printf("map: %s: %-6s: start %s: end: missing\n", mrl.TurnId, mrl.UnitId, mrl.StartingGridCoordinates)
missingEnds++
}
// does the unit have a move next turn? if they don't, we're hosed since we can't look forward multiple turns yet.
nextTurnUnitStepId := fmt.Sprintf("%s.%s", nextTurnId, mrl.UnitId)
nextTurn, ok := movementResultsMap[nextTurnUnitStepId]
if !ok {
log.Fatalf("map: %s: %-6s: start %s: end: missing next turn\n", mrl.TurnId, mrl.UnitId, mrl.StartingGridCoordinates)
}
nextTurn.StartingGridCoordinates = mrl.EndingGridCoordinates
//log.Printf("map: %s: %-6s: start %s: end: %s <-- next turn\n", nextTurn.TurnId, nextTurn.UnitId, nextTurn.StartingGridCoordinates, nextTurn.EndingGridCoordinates)
}
if missingEnds != 0 {
log.Fatalf("map: %s: error: walk did not populate %d units\n", turnId, missingEnds)
}
}
var newHexes, updatedHexes []*wxx.Hex
for _, hex := range allHexes {
if hex.Features.Created == turnId {
newHexes = append(newHexes, hex)
} else if hex.Features.Updated == turnId {
updatedHexes = append(updatedHexes, hex)
}
if hex.GridCoords == "" {
panic("!")
}
}
if err := consolidatedMap.MergeHexes(turnId, newHexes); err != nil {
log.Printf("error: wxx: mergeHexes: newHexes: %v\n", err)
return err
} else if err = consolidatedMap.MergeHexes(turnId, updatedHexes); err != nil {
log.Printf("error: wxx: mergeHexes: updatedHexes: %v\n", err)
return err
}
if argsMap.show.gridCoords {
consolidatedMap.AddGridCoords()
} else if argsMap.show.gridNumbers {
consolidatedMap.AddGridNumbering()
}
if argsMap.debug.sectionMaps {
panic("this needs to be fixed")
}
// now we can create the Worldographer map!
//log.Printf("map: world %6d: consolidated %6d\n", len(worldMap), len(consolidatedMap))
mapName := filepath.Join(cfg.OutputPath, fmt.Sprintf("%s.%s.wxx", turnId, argsMap.clanId))
if err := consolidatedMap.Create(mapName, argsMap.show.gridCenters); err != nil {
log.Printf("map: creating %s\n", mapName)
log.Fatal(err)
}
log.Printf("map: created %s\n", mapName)
}
if argsMap.show.gridCoords {
consolidatedMap.AddGridCoords()
} else if argsMap.show.gridNumbers {
consolidatedMap.AddGridNumbering()
}
// now we can create the Worldographer map!
mapName := filepath.Join(cfg.OutputPath, fmt.Sprintf("%s.wxx", argsMap.clanId))
if err := consolidatedMap.Create(mapName, argsMap.show.gridCenters); err != nil {
log.Printf("map: creating %s\n", mapName)
log.Fatal(err)
}
log.Printf("map: created %s\n", mapName)
return nil
},
}
type gridHexes struct {
Grid string // the grid in the big map
Hexes map[string]*wxx.Hex // key is hex coordinates
}
func walk(turnId, unitId string, worldHexMap map[string]*wxx.Hex, stepNo int, step *lbmoves.Step, start coords.Map, debugStep bool) coords.Map {
if debugStep {
if buf, err := json.MarshalIndent(step, "", "\t"); err == nil {
log.Printf("debugStep: step %s\n", string(buf))
}
}
//log.Printf("map: %s: %-6s: step %2d: mapc %s\n", turnId, unitId, stepNo, start)
// advance a hex if the move succeeded
curr := start
if step.Result == lbmoves.Succeeded {
if step.Attempted == directions.DUnknown {
panic(fmt.Sprintf("assert(step.Attempted != %d)", int(step.Attempted)))
}
curr = start.Add(step.Attempted)
}
// fetch hex based on the current map coordinates (create as needed)
fetchHex := func(mc coords.Map, defaultTerrain domain.Terrain) *wxx.Hex {
daHex, ok := worldHexMap[mc.GridString()]
if !ok { // create a new hex with normalized map coordinates (to the grid's origin)
gridColumn, gridRow := mc.GridColumnRow()
daHex = &wxx.Hex{
GridId: mc.GridId(),
GridCoords: mc.GridString(),
Offset: wxx.Offset{Column: gridColumn, Row: gridRow},
Terrain: defaultTerrain,
Features: wxx.Features{
Created: turnId,
},
}
worldHexMap[mc.GridString()] = daHex
}
return daHex
}
daHex := fetchHex(curr, step.Terrain)
//log.Printf("map: %s: %-6s: step %2d: mapc %s: grid id %q\n", turnId, unitId, stepNo, curr, curr.GridId())
daHex.Features.Updated = turnId
if step.Result == lbmoves.Succeeded {
daHex.Features.Visited = turnId
if step.Settlement != nil {
//log.Printf(">>> %+v\n", *step.Settlement)
daHex.Features.Settlement = &wxx.Settlement{
UUID: uuid.New().String(),
Name: step.Settlement.Name,
}
}
}
if step.Result == lbmoves.Blocked {
// log.Printf("step: blocked: attempted %s\n", step.Attempted)
if step.BlockedBy == nil {
panic("assert(blockedBy != nil")
}
// log.Printf("step: blocked: edge: %+v\n", *step.BlockedBy)
}
if step.Result == lbmoves.Prohibited {
if step.ProhibitedFrom == nil {
panic("assert(step.ProhibitedFrom != nil)")
}
if step.ProhibitedFrom.Direction == directions.DUnknown {
panic(fmt.Sprintf("assert(prohibitedFrom.Direction != %d)", int(step.ProhibitedFrom.Direction)))
}
neighborMapCoords := curr.Add(step.ProhibitedFrom.Direction)
//log.Printf("map: %s: %-6s: step %2d: curr %s: pro %s\n", turnId, unitId, stepNo, curr, neighborMapCoords)
daNeighborHex := fetchHex(neighborMapCoords, step.ProhibitedFrom.Terrain)
if daNeighborHex.Terrain != step.ProhibitedFrom.Terrain {
panic(fmt.Sprintf("assert(daNeighborHex.Terrain == %d)", int(step.ProhibitedFrom.Terrain)))
}
}
// update the hex's neighbors
for _, neighbor := range step.Neighbors {
if neighbor.Direction == directions.DUnknown {
panic(fmt.Sprintf("assert(neighbor.Direction != %d)", int(neighbor.Direction)))
}
neighborMapCoords := curr.Add(neighbor.Direction)
//log.Printf("map: %s: %-6s: step %2d: curr %s: nbr %s\n", turnId, unitId, stepNo, curr, neighborMapCoords)
daNeighborHex := fetchHex(neighborMapCoords, neighbor.Terrain)
if daNeighborHex.Terrain != neighbor.Terrain {
log.Printf("map: %s: %-6s: step %2d: nbr: want %s, got %s\n", turnId, unitId, stepNo, neighbor.Terrain, daNeighborHex.Terrain)
panic(fmt.Sprintf("assert(daNeighborHex.Terrain == %d)", int(neighbor.Terrain)))
}
}
// update the hex edges
for _, edge := range step.Edges {
switch edge.Edge {
case domain.ENone:
// ignore, shouldn't ever happen
case domain.EFord:
daHex.Features.Edges.Ford = append(daHex.Features.Edges.Ford, edge.Direction)
case domain.ERiver:
daHex.Features.Edges.River = append(daHex.Features.Edges.River, edge.Direction)
case domain.EPass:
daHex.Features.Edges.Pass = append(daHex.Features.Edges.Pass, edge.Direction)
default:
panic(fmt.Sprintf("assert(edge != %d)", edge.Edge))
}
}
// update the hex resources
if step.Resources != domain.RNone {
if daHex.Features.Resources == domain.RNone {
daHex.Features.Resources = step.Resources
} else if daHex.Features.Resources != step.Resources {
log.Printf("why? changing %q to %q\n", daHex.Features.Resources, step.Resources)
panic("!")
}
}
switch step.Result {
case lbmoves.Blocked:
case lbmoves.ExhaustedMovementPoints:
case lbmoves.Follows:
case lbmoves.Prohibited:
case lbmoves.Status:
case lbmoves.Succeeded:
case lbmoves.StayedInPlace:
case lbmoves.Vanished:
default:
panic(fmt.Sprintf("assert(step.Result != %d)", int(step.Result)))
}
return curr
}