forked from nedbrek/TrueAtlanteans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.tcl
executable file
·2290 lines (1892 loc) · 50.3 KB
/
client.tcl
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package require Tk
lappend ::auto_path [pwd]
package require client_utils
wm withdraw .
### gui constants
set ::zoomLevels {
3
6
13
20
27
45
}
set ::terrainColors {
cavern #f0d800
chasm #d88040
deepforest #00c000
desert #f0d800
forest #00c000
hill #a04018
jungle #205020
lake #0000ff
mountain #704018
mystforest #004000
ocean #000040
plain #ffffc0
swamp #a0a040
tunnels #704018
tundra #00ffff
underforest #00c000
wasteland #d88040
grotto #d88040
nexus #ffffc0
}
### game constants
set ::directions {
Southeast
South
Southwest
North
Northeast
Northwest
}
set ::unitFlags {
GUARD g
AVOID a
BEHIND b
HOLD h
AUTOTAX t
NOAID i
NOCROSS x
SHARE s
}
set ::boats {
Longboat
Clipper
Galleon
}
namespace eval gui {
set currentTurn 0
set viewLevel 1
set prevUnit ""
set prevId ""
set rightX 0
set rightY 0
set forSaleOpen 0
}
##############################################################################
### general utilities
proc lGet {l i} {
return [string trim [lindex $l $i]]
}
# return the union of lists 'a' and 'b'
proc lunion {a b} {
foreach el $a {set ary($el) ""}
foreach el $b {set ary($el) ""}
return [array names ary]
}
##############################################################################
### Atlantis specific utilities
proc getZlevel {} {
set zlevel [expr {$gui::viewLevel - 1}]
set zList [::db eval {select distinct z from terrain order by cast(z as integer)}]
if {$zlevel > [llength $zList]} {
return 1
}
return [lindex $zList $zlevel]
}
##############################################################################
### drawing
# use 1,2,rad3 triangles
proc setN {newN} {
# length of a hexside in pixels
set ::n $newN
# n times radical 3
set ::nrad3 [expr $newN * sqrt(3)]
}
setN 27
# plot one hexagon (flat top, not pointy top) with the upper left vertex at x y
# (NOTE: the left-middle vertex will be left of x)
# place it in canvas obj
# return the item id
proc plot_hex_full {obj x y} {
global n nrad3
set hexId [$obj create polygon \
$x $y \
[expr $x + 2 * $n] $y \
[expr $x + 3 * $n] [expr $y + $nrad3] \
[expr $x + 2 * $n] [expr $y + 2 * $nrad3] \
$x [expr $y + 2 * $nrad3] \
[expr $x - $n] [expr $y + $nrad3] \
-outline darkgray -outlinestipple gray25 -tags hex]
return $hexId
}
proc drawRoad {dir row col} {
set w .t.fR.screen
set x [col2x $col]
set y [row2y $row]
switch -nocase $dir {
n {
set id [$w create line [expr $x + $::n ] [expr $y ] [expr $x + $::n] [expr $y + $::nrad3]]
}
s {
set id [$w create line [expr $x + $::n ] [expr $y + 2*$::nrad3] [expr $x + $::n] [expr $y + $::nrad3]]
}
nw {
set id [$w create line [expr $x - $::n/2] [expr $y + $::nrad3/2] [expr $x + $::n] [expr $y + $::nrad3]]
}
ne {
set id [$w create line [expr $x + 2.5*$::n] [expr $y + $::nrad3/2] [expr $x + $::n] [expr $y + $::nrad3]]
}
sw {
set id [$w create line [expr $x - $::n/2] [expr $y + 1.5*$::nrad3] [expr $x + $::n] [expr $y + $::nrad3]]
}
se {
set id [$w create line [expr $x + 2.5*$::n] [expr $y + 1.5*$::nrad3] [expr $x + $::n] [expr $y + $::nrad3]]
}
}
$w addtag road withtag $id
$w addtag icon withtag $id
$w itemconfigure $id -width 2
}
# calculate the x and y coord of the top-left of the hex
proc col2x {col} {
return [expr ($col * 3 * $::n) + $::n]
}
proc row2y {row} {
set oddRow [expr int($row+.5) & 1]
set yNum [expr $row / 2]
set yOff 0
if {$oddRow} {
set yOff $::nrad3
}
return [expr $yNum * 2 * $::nrad3 + $yOff]
}
# plot the hex (x,y) (where x and y are integers 0..i)
# adds the tag hex_x_y
proc plot_hex_num {obj x y} {
set hexId [plot_hex_full $obj [col2x $x] [row2y $y]]
set tags [$obj itemcget $hexId -tags]
lappend tags [format "hex_%d_%d" $x $y]
$obj itemconfigure $hexId -tags $tags
return $hexId
}
proc drawExitWall {w d x y} {
switch $d {
South {
set id [$w create line [expr $x] [expr $y + 2*$::nrad3] [expr $x + 2*$::n] [expr $y + 2*$::nrad3]]
}
Southeast {
set id [$w create line [expr $x + 2*$::n] [expr $y + 2*$::nrad3] [expr $x + 3*$::n] [expr $y + $::nrad3]]
}
Southwest {
set id [$w create line [expr $x - $::n] [expr $y + $::nrad3] [expr $x] [expr $y + 2*$::nrad3]]
}
North {
set id [$w create line [expr $x] [expr $y] [expr $x + 2*$::n] [expr $y]]
}
Northeast {
set id [$w create line [expr $x + 2*$::n] [expr $y] [expr $x + 3*$::n] [expr $y + $::nrad3]]
}
Northwest {
set id [$w create line [expr $x - $::n] [expr $y + $::nrad3] [expr $x] [expr $y]]
}
}
$w addtag wall withtag $id
$w addtag icon withtag $id
$w itemconfigure $id -width 4
$w itemconfigure $id -fill darkgray
}
proc drawNexus {w data} {
# draw nexus
foreach {col row type city ct rid exitDirs} $data {
set hexId [plot_hex_full $w [col2x [expr {$col+1}]] [row2y [expr {$row+3}]]]
set tags [$w itemcget $hexId -tags]
lappend tags [format "nexus_%d_%d" $col $row]
$w itemconfigure $hexId -tags $tags
}
# draw exits
set exitData [::db eval {
SELECT dir, dest from nexus_exits
}]
foreach {d col row} {Northwest 0 2 North 1 1 Northeast 2 2 Southwest 0 4 South 1 5 Southeast 2 4} {
set loc [dGet $exitData $d]
set hexId [plot_hex_full $w [col2x $col] [row2y $row]]
set tags [$w itemcget $hexId -tags]
set x [lindex $loc 0]
set y [lindex $loc 1]
lappend tags [format "hex_%d_%d" $x $y]
$w itemconfigure $hexId -tags $tags
# terrain
set terrain [db eval {SELECT type FROM terrain WHERE x=$x and y=$y and z=1}]
$w itemconfigure $hexId -fill [dict get $::terrainColors $terrain]
}
$w configure -scrollregion [$w bbox all]
}
# draw all the regions in the db data
proc drawDB {w db} {
$w delete all
set zlevel [getZlevel]
set data [$db eval {
SELECT x, y, type, city, detail.turn, detail.id, detail.exitDirs
FROM terrain left outer join detail
USING(x,y,z)
WHERE z=$zlevel
GROUP BY terrain.x, terrain.y, terrain.z
ORDER BY detail.turn
}]
if {$zlevel == 0} {
drawNexus $w $data
return
}
foreach {col row type city ct rid exitDirs} $data {
if {[info exists drawn($col,$row)]} {continue}
set drawn($col,$row) ""
set hexId [plot_hex_num $w $col $row]
$w itemconfigure $hexId -fill [dict get $::terrainColors $type]
set c [$w coords $hexId]
set x [lindex $c 0]
set y [lindex $c 1]
# draw missing exit walls
if {$exitDirs ne ""} {
foreach d $::directions {
if {[lsearch $exitDirs $d] == -1} {
drawExitWall $w $d $x $y
}
}
}
# draw city icon
if {$city ne ""} {
switch [lGet $city end] {
village {set cityIcon "-"}
town {set cityIcon "+"}
city {set cityIcon "*"}
default {
puts "Unknown city type: '[lindex $city end]'"
set cityIcon "?"
}
}
$w create text [expr $x+$::n] [expr $y+$::nrad3] -text $cityIcon -tags icon
}
# show unit flags
if {$ct == $gui::currentTurn} {
set res [$db eval {
SELECT detail
FROM units
WHERE regionId=$rid
GROUP BY detail
}]
if {[lsearch $res "own"] != -1} {
$w create text [expr $x] [expr $y+2*$::nrad3] -text "@" \
-anchor sw -tags icon
}
if {[lsearch $res "foreign"] != -1} {
$w create text [expr $x+2*$::n] [expr $y+2*$::nrad3] -text "!" \
-anchor se -fill red -tags icon
}
set res [$db eval {
SELECT orders
FROM units
WHERE regionId=$rid
}]
set hasTax 0
set hasProd 0
foreach ol $res {
if {!$hasTax && [ordersMatch $ol "tax"] != -1} {
set hasTax 1
}
if {!$hasProd && [ordersMatch $ol "produce"] != -1} {
set hasProd 1
}
if {!$hasProd && [ordersMatch $ol "build"] != -1} {
set hasProd 1
}
}
if {$hasTax} {
$w create text [expr $x-$::n] [expr $y+$::nrad3] -text "\$" \
-anchor w -fill darkgreen -tags icon
}
if {$hasProd} {
$w create text [expr $x] [expr $y] -text "P" \
-anchor nw -tags icon
}
}
# tag unexplored hexes
if {$ct eq ""} {
set hexOverId [plot_hex_num $w $col $row]
$w itemconfigure $hexOverId -fill gray -stipple gray12
}
# pull buildings
set objects [$db eval {
SELECT desc FROM objects
WHERE regionId=$rid
}]
set hasOtherBuild 0
foreach desc $objects {
if {[regexp -nocase {^Road (.+)} $desc -> dir]} {
drawRoad $dir $row $col
} elseif {$desc eq "Shaft"} {
$w create text [expr $x+2.5*$::n] [expr $y+$::nrad3] -text "H" \
-anchor e -tags icon
} elseif {[lsearch $::boats $desc] != -1} {
# TODO draw ship icon
} elseif {!$hasOtherBuild} {
set hasOtherBuild 1
}
}
if {$hasOtherBuild} {
$w create text [expr $x+2*$::n] [expr $y] -text "B" \
-anchor ne -tags icon
}
}
drawMarkers $w $db
$w configure -scrollregion [$w bbox all]
}
##############################################################################
### gui
proc copyTree {w} {
clipboard clear
set items [$w selection]
foreach i $items {
clipboard append "[$w item $i -text]\t[concat [$w item $i -values] \t]\n"
}
}
bind Treeview <Control-c> {copyTree %W}
proc saveUnitOrders {unit_id w} {
if {![$w edit modified]} {
return
}
set orders [split [string trimright [$w get 1.0 end]] "\n"]
db eval {
UPDATE units SET orders=$orders
WHERE id=$unit_id
}
}
proc orderBoxReset {w} {
if {$gui::prevUnit ne ""} {
saveUnitOrders $gui::prevId $w
}
.t.fL.fItems.t configure -state normal
.t.fL.fItems.t delete 1.0 end
.t.fL.fItems.t configure -state disabled
$w configure -state normal
$w delete 1.0 end
$w edit reset
$w edit modified 0
}
# return the x and y coordinates of the hex under the mouse
proc getSelectionXY {} {
set tags [.t.fR.screen gettags active]
set i [lsearch -regexp $tags {hex_[[:digit:]]+_[[:digit:]]}]
if {$i == -1} {
# check for nexus
set i [lsearch -regexp $tags {nexus_[[:digit:]]+_[[:digit:]]}]
if {$i == -1} { return "" }
set hexTag [lindex $tags $i]
regexp {nexus_([[:digit:]]+)_([[:digit:]]+)} $hexTag -> x y
} else {
set hexTag [lindex $tags $i]
regexp {hex_([[:digit:]]+)_([[:digit:]]+)} $hexTag -> x y
}
return [list $x $y]
}
# make the unit with 'name' active from the combox
proc showUnit {name} {
orderBoxReset .t.fL.tOrd
set w .t.fR.screen
# retrieve unit in this hex
## start with regionId
set xy [getSelectionXY]
if {$xy eq ""} {return}
set x [lindex $xy 0]
set y [lindex $xy 1]
set zlevel [getZlevel]
set detail [db eval {
SELECT id, turn
FROM detail
WHERE x=$x AND y=$y AND z=$zlevel
ORDER BY turn DESC LIMIT 1
}]
## got it, use it to retrieve the unit
set regionId [lindex $detail 0]
set gui::prevUnit $name
set r [extractUnitNameNum $name]
set just_name [lindex $r 0]
set uid [lindex $r 1]
set data [db eval {
SELECT orders, id, items, skills, detail, flags, faction, desc
FROM units
WHERE regionId=$regionId AND uid=$uid
ORDER BY id
}]
set orders [lindex $data 0]
set gui::prevId [lindex $data 1]
set items [lindex $data 2]
set skills [lindex $data 3]
set detail [lindex $data 4]
set flags [lindex $data 5]
set fact [lindex $data 6]
set desc [lindex $data 7]
# fill the items box (stick skills in too)
set t .t.fL.fItems.t
$t configure -state normal
$t insert end "Flags: "
foreach {f l} $::unitFlags {
set v [dGet $flags $f]
if {$v eq "1"} {
$t insert end "$l "
}
}
set v [dGet $flags REVEAL]
if {$v eq "UNIT"} {
$t insert end "r "
} elseif {$v eq "FACTION"} {
$t insert end "R "
}
set v [dGet $flags CONSUME]
if {$v eq "UNIT"} {
$t insert end "c"
} elseif {$v eq "FACTION"} {
$t insert end "C"
}
$t insert end "\n"
$t insert end "Skills: "
if {$skills eq ""} {
$t insert end "<none>\n"
} else {
foreach s $skills {
$t insert end "[join $s]\n"
}
}
if {$detail ne "own"} {
$t insert end "Faction: '$fact'\n"
}
if {$desc ne ""} {
$t insert end "'$desc'\n"
}
$t insert end "-------- [countMen $items] men\n"
foreach i $items {
$t insert end "[join $i]\n"
}
$t configure -state disabled
# done with items
# populate orders box
foreach o $orders {
.t.fL.tOrd insert end "$o\n"
}
.t.fL.tOrd edit modified 0
# don't let people modify foreign unit orders
if {$detail eq "own"} {
.t.fL.tOrd configure -state normal
} else {
.t.fL.tOrd configure -state disabled
}
}
# update the left frame with all the region details
proc displayRegion {x y nexus} {
# clean up
orderBoxReset .t.fL.tOrd
.t.fL.lProd configure -text ""
set mt .t.fL.fMarket.tv
set marketChildren [$mt children {}]
if {[llength $marketChildren]} {
set gui::forSaleOpen [$mt item [lindex $marketChildren 0] -open]
}
$mt delete $marketChildren
# clear current unit, in case there is none
.t.fL.cbMyUnits set ""
.t.fL.cbMyUnits configure -values ""
set t .t.fL.tDesc
$t delete 1.0 end
# pull region terrain info
set zlevel [getZlevel]
if {!$nexus && $zlevel == 0} {set zlevel 1}
set data [db eval {
SELECT type, city, region
FROM terrain
WHERE x=$x AND y=$y AND z=$zlevel
}]
set terrain [lGet $data 0]
if {$terrain ne ""} {
$t insert end "$terrain "
}
$t insert end "($x,$y) in [lGet $data 2]\n"
set city [lGet $data 1]
if {[llength $city]} {
$t insert end "contains [lGet $city 0]"
$t insert end " \[[lGet $city 1]\]\n"
}
# pull the latest turn data
set rdata [db eval {
SELECT turn, weather, wages, pop, race, tax, entertainment, id, products, sells, wants
FROM detail
WHERE x=$x AND y=$y AND z=$zlevel
ORDER BY turn DESC LIMIT 1
}]
# if no detail info, done
if {[llength $rdata] == 0} { return }
set turn [lindex $rdata 0]
$t insert end "Data from turn: $turn\n"
set weather [lindex $rdata 1]
set wages [lindex $rdata 2]
$t insert end "[lGet $rdata 3] peasants "
$t insert end "([lGet $rdata 4]), \$[lGet $rdata 5].\n"
$t insert end "------------------------------------\n"
$t insert end "The weather was [lGet $weather 0] last month;\n"
$t insert end "it will be [lGet $weather 1] next month.\n"
$t insert end "Wages: \$[lGet $wages 0] (Max: \$[lGet $wages 1]).\n"
$t insert end "Entertainment: \$[lindex $rdata 6].\n"
set regionId [lindex $rdata 7]
# pull buildings
set objects [db eval {
SELECT name, desc FROM objects
WHERE regionId=$regionId
}]
foreach {name desc} $objects {
$t insert end "$name - $desc\n"
}
# region resources for production
.t.fL.lProd configure -text [join [lindex $rdata 8]]
# market
set sells [lindex $rdata 9]
set wants [lindex $rdata 10]
if {[llength $sells] == 0} {
.t.fL.fMarket.tv insert {} 0 -text "Nothing for sale" -open $gui::forSaleOpen
} else {
set tvi [.t.fL.fMarket.tv insert {} 0 -text "For sale" -open $gui::forSaleOpen]
}
foreach {i c} $sells {
.t.fL.fMarket.tv insert $tvi end -text "$i @ \$$c"
}
if {[llength $wants] == 0} {
.t.fL.fMarket.tv insert {} end -text "Wanted: nothing"
} else {
set tvi [.t.fL.fMarket.tv insert {} end -text "Wanted"]
}
foreach {i c} $wants {
.t.fL.fMarket.tv insert $tvi end -text "$i @ \$$c"
}
# unit processing
set units [db eval {
SELECT name, uid, detail, faction
FROM units
WHERE regionId=$regionId
ORDER BY detail DESC
}]
## set up units combox
set unitList {}
set state "start"
foreach {name uid detail fact} $units {
if {$detail eq "own"} {
## don't show owned units from the past
if {$turn != $gui::currentTurn} { continue }
set state "own"
} elseif {$state ne "start"} {
set state "start"
lappend unitList "-----"
}
lappend unitList [format {%s (%d)} $name $uid]
}
.t.fL.cbMyUnits configure -values $unitList
if {[llength $unitList] != 0} {
.t.fL.cbMyUnits current 0
showUnit [.t.fL.cbMyUnits get]
}
}
# find the first of 'ids' with canvas item 'type'
proc findType {w ids type} {
foreach i $ids {
if {[$w type $i] eq $type} {
return $i
}
}
return ""
}
proc makeNormal {w tag} {
$w itemconfigure $tag -outline darkgray
$w itemconfigure $tag -outlinestipple gray25
$w itemconfigure $tag -width 1
}
proc makeNotDone {w tag} {
$w itemconfigure $tag -outline blue
$w itemconfigure $tag -outlinestipple ""
$w itemconfigure $tag -width 2
}
# make region x and y selected in w
proc selectRegion {w x y {nexus 0}} {
set baseTag "hex_%d_%d"
if {$nexus} {
set baseTag "nexus_%d_%d"
}
# see if hex is active
set curTags [$w gettags [format $baseTag $x $y]]
if {$curTags eq ""} {return}
set i [lsearch $curTags "active"]
if {$i != -1} {return}
# deselect current active
set oldTags [$w gettags active]
if {$oldTags ne ""} {
if {[lsearch $oldTags "notdone"] != -1} {
# not done hex
makeNotDone $w active
} else {
# normal hex
makeNormal $w active
}
}
# move active tag
$w dtag active
$w addtag active withtag [format $baseTag $x $y]
# show active
$w itemconfigure active -outline red
$w itemconfigure active -outlinestipple ""
$w itemconfigure active -width 4
$w raise active
$w raise icon
displayRegion $x $y $nexus
}
proc arrow {w dir} {
set xy [getSelectionXY]
if {$xy eq ""} {return}
set x [lindex $xy 0]
set y [lindex $xy 1]
switch $dir {
up { incr y -2 }
dn { incr y 2 }
ul { incr x -1; incr y -1 }
ur { incr x 1; incr y -1 }
lr { incr x 1; incr y 1 }
ll { incr x -1; incr y 1 }
lt {
incr x -1
if {$x & 1} {
incr y -1
} else {
incr y 1
}
}
rt {
incr x
if {$x & 1} {
incr y -1
} else {
incr y 1
}
}
}
# wrap around
set maxX [::db eval { SELECT max(cast(x as integer)) FROM terrain }]
if {$x == -1} { set x $maxX }
if {$x > $maxX} { set x 0 }
set curTags [$w gettags [format "hex_%d_%d" $x $y]]
if {$curTags eq ""} { return }
selectRegion $w $x $y
centerHex $w $x $y
}
# process user click on hex
proc hexClick {w x y} {
# where is the click
set hexId [$w find withtag current]
if {$hexId eq ""} {return}
# what if the use clicks on a icon
if {[$w type $hexId] ne "polygon"} {
# find the hex with the icon
set c [$w bbox $hexId]
set x1 [expr [lindex $c 0]+2]
set y1 [expr [lindex $c 1]+2]
set x2 [expr [lindex $c 2]-2]
set y2 [expr [lindex $c 3]-2]
set hexes [$w find overlapping $x1 $y1 $x2 $y2]
set hexId [findType $w $hexes "polygon"]
if {$hexId eq ""} {
return
}
}
# was it already active, then done
set tags [$w gettags $hexId]
set i [lsearch $tags "active"]
if {$i != -1} { return }
set i [lsearch -regexp $tags {hex_[[:digit:]]+_[[:digit:]]}]
if {$i == -1} {
set i [lsearch -regexp $tags {nexus_[[:digit:]]+_[[:digit:]]}]
if {$i != -1} {
set hexTag [lindex $tags $i]
regexp {nexus_([[:digit:]]+)_([[:digit:]]+)} $hexTag -> hx hy
selectRegion $w $hx $hy 1
}
return
}
set hexTag [lindex $tags $i]
regexp {hex_([[:digit:]]+)_([[:digit:]]+)} $hexTag -> hx hy
selectRegion $w $hx $hy
}
# only switch focus if the widgets are part of the same toplevel
proc switchFocus {w} {
set curFocus [focus]
if {$curFocus eq ""} {return}
set sf [split $curFocus "."]
set sw [split $w "."]
if {[lindex $sf 1] eq [lindex $sw 1]} {
focus $w
}
}
##############################################################################
### menu callbacks
# helper for "Add Report"
proc dnLevel {} {
incr gui::viewLevel
drawDB .t.fR.screen db
}
proc upLevel {} {
if {$gui::viewLevel > 1} {
incr gui::viewLevel -1
drawDB .t.fR.screen db
}
}
proc zoomIn {} {
set i [lsearch $::zoomLevels $::n]
if {$i == -1 || $i+1 == [llength $::zoomLevels]} {return}
incr i
setN [lindex $::zoomLevels $i]
drawDB .t.fR.screen db
}
proc zoomOut {} {
set i [lsearch $::zoomLevels $::n]
if {$i == -1 || $i == 0} {return}
incr i -1
setN [lindex $::zoomLevels $i]
drawDB .t.fR.screen db
}
proc newGame {} {
set types {
{{Game Database} {.db}}
{{All Files} *}
}
set ofile [tk_getSaveFile -filetypes $types]
if {$ofile eq ""} { return }
wm title .t "True Atlanteans - [file tail $ofile]"
createDb $ofile
.t.fR.screen delete all
}
proc doOpen {} {
set gui::prevUnit ""
set gui::prevId ""
set types {
{{Game Database} {.db}}
{{All Files} *}
}
set ofile [tk_getOpenFile -filetypes $types]
if {$ofile eq ""} { return }
set errMsg [openDb $ofile]
if {$errMsg ne ""} {
tk_messageBox -message $errMsg
}
set ::men [db eval {select abbr from items where type="race"}]
set gui::currentTurn [db eval {select max(turn) from detail}]
set gui::viewLevel 1
wm title .t "True Atlanteans - [file tail $ofile] Turn $gui::currentTurn"
# pull settings from db
set res [db eval {
SELECT geom_top, zoom_level, view_level, forSale_open
FROM settings WHERE id=1
}]
foreach {geom zoom view forSale} $res {
wm geometry .t $geom
setN [lindex $::zoomLevels $zoom]
set gui::viewLevel $view
set gui::forSaleOpen $forSale
}
drawDB .t.fR.screen db
}
proc doAdd {} {
set ofiles [tk_getOpenFile -multiple 1]
if {$ofiles eq ""} { return }
foreach f $ofiles {
loadData $f
}
set ::men [db eval {select abbr from items where type="race"}]
set gui::currentTurn [db eval {select max(turn) from detail}]
set txt [wm title .t]
wm title .t "True Atlanteans - [lindex $txt 3] Turn $gui::currentTurn"
drawDB .t.fR.screen db
}
proc drawMarkers {w db} {
set zlevel [getZlevel]
set res [$db eval {
SELECT x,y,done FROM active_markers
WHERE z=$zlevel
}]
foreach {x y done} $res {
if {$done} {continue}
$w addtag notdone withtag [format "hex_%d_%d" $x $y]
}
makeNotDone $w notdone
}
proc clearNotDone {w x y} {
# update db
set zlevel [getZlevel]
::db eval {
UPDATE active_markers SET done=1
WHERE x=$x AND y=$y AND z=$zlevel
}
# delete the tag
set hexTag [format "hex_%d_%d" $x $y]
$w dtag $hexTag notdone
# update the map
set curTags [$w gettags $hexTag]
if {[lsearch $curTags "active"] == -1} {