-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_utils.tcl
641 lines (541 loc) · 12.9 KB
/
client_utils.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
package require atlantis_dbtools
package require atlantis_reader
package require Itcl
package provide client_utils 1.0
# can be stored in db, or calculated from terrain
set max_x 32
set max_y 32
### utilities
proc checkBool {s} {
return [expr {$s != 0 && $s != 1}]
}
proc cleanOrder {o} {
# strip whitespace
set o [string trim $o]
if {[string index $o 0] eq "@"} {
# repeat
set o [string trimleft $o "@"]
if {[string is digit [string index $o 0]]} {
# @n <order>
set o [regsub {[[:digit:]]+} $o ""]
} else {
# it was a simple repeat
}
set o [string trim $o]
}
return $o
}
proc weighItems {items} {
set total_wt 0
foreach i $items {
set count [lindex $i 0]
set full_abbr [lindex $i 2]
if {![regexp {\[(.*)\]} $i -> abbr]} {
continue
}
if {$abbr eq "SILV"} {
continue
}
set data [db onecolumn {SELECT desc FROM items WHERE abbr=$abbr}]
if {$data ne ""} {
set wt [dGet $data Weight]
incr total_wt [expr {$count * $wt}]
}
}
return $total_wt
}
### classes
itcl::class Unit {
public variable db_id
public variable name; # not including "(num)"
public variable num; # just the num
public variable items; # list of items
public variable orders
public variable orig_orders
public variable skills
public variable flags
public variable region
public variable object
public variable parent
constructor {args} {
set db_id [dGet $args Id]
set name [dGet $args Name]
set num [dGet $args Num]
set items [dGet $args Items]
set orders [dGet $args Orders]
set skills [dGet $args Skills]
set flags [dGet $args Flags]
set region [dGet $args Region]
set object [dGet $args Object]
set parent [dGet $args Parent]
set orig_orders $orders
if {$name eq ""} {
if {[lindex $num 0] eq "new"} {
set name $num
} else {
set name "Unit $num"
}
}
}
method print {} {
puts "$name ($num) '$flags' '$skills' '$orders' '$items'"
}
# filter instant orders, including "form <id>"/end and "turn/endturn"
# return new units (created by form)
method filterInstantOrders {}
method moveTo {x y z} {
foreach {cur_x cur_y cur_z} $region {}
if {$cur_z != $z} {
puts "Can't navigate between levels"
return
}
if {$x == $cur_x && $y == $cur_y} {
return
}
# TODO handle weight and weather
if {$cur_y < $y} {
set d "s"
} else {
set d "n"
}
if {$cur_x < $x} {
append d "e"
} elseif {$cur_x > $x} {
append d "w"
}
# TODO decide when to use wrap
# TODO move multiple
set o "MOVE $d"
lappend orders $o
lappend orig_orders $o
}
method countItem {abbr} {
return [::countItem $items $abbr]
}
method setItem {abbr ct} {
set full_abbr [format {[%s]} $abbr]
for {set i 0} {$i < [llength $items]} {incr i} {
set il [lindex $items $i]
if {[lindex $il 2] eq $full_abbr} {
set il [lreplace $il 0 0 $ct]
set items [lreplace $items $i $i $il]
return
}
}
set name [db onecolumn {SELECT name FROM items WHERE abbr=$abbr}]
lappend items [list $ct $name $full_abbr]
}
}
itcl::body Unit::filterInstantOrders {} {
set bool_flags {
avoid
hold
behind
noaid
guard
nocross
autotax
share
}
set enum_flags {
consume
reveal
}
set enum_flag_vals {
""
unit
faction
}
foreach {x y z} $region {}
set new_units [list]
set new_orders [list]
set skip 0
for {set i 0} {$i < [llength $orders]} {incr i} {
set o [cleanOrder [lindex $orders $i]]
set cmd [string tolower [lindex $o 0]]
set upper_cmd [string toupper [lindex $o 0]]
if {$skip} {
if {$cmd eq "endturn"} {
set skip 0
}
continue
}
if {$cmd eq "turn"} {
set skip 1
continue
}
# handle bool flags
if {[lsearch $bool_flags $cmd] != -1} {
set arg [lindex $o 1]
# apply flags
if {$arg == 1} {
# set flag
if {[dict exists $flags $upper_cmd]} {
# already set
} else {
lappend flags $upper_cmd 1
}
} elseif {$arg == 0} {
# remove flag
set ki [lsearch $flags $upper_cmd]
if {$ki != -1} {
# set - unset it
set flags [lreplace $flags $ki $ki+1]
} else {
# not set - nothing to do
}
} else {
puts "$name ($x, $y, $z) $cmd bad argument $arg"
}
continue
}
# enum flags
if {[lsearch $enum_flags $cmd] != -1} {
set arg [lindex $o 1]
if {[lsearch -nocase $enum_flag_vals $arg] == -1} {
puts "$name ($x, $y, $z) $cmd bad argument $arg"
}
# TODO: apply flags
continue
}
# TODO spoils
if {$cmd eq "spoils"} {
continue
}
# anything but form
if {![regexp -nocase {^form +(.*)} $o -> new_id]} {
lappend new_orders $o
continue
}
set new_o [list]
for {incr i} {$i < [llength $orders]} {incr i} {
set o [lindex $orders $i]
if {[regexp -nocase { *end *$} $o]} {
break
}
lappend new_o $o
}
set new_unit [itcl::code [Unit #auto Num "new $new_id" Orders $new_o \
Flags $flags Region $region Object $object Parent $this]]
lappend new_units $new_unit
set tmp [{*}$new_unit filterInstantOrders]
if {$tmp ne ""} {
puts "Error: nested FORM will not work [{*}$u cget -name] ($x, $y, $z)"
itcl:delete object $tmp
}
}
set orders $new_orders
return $new_units
}
### other functions
proc getLevelXscale {z} {
# TODO figure out for particular game
set scales { 1 1 1 2 }
return [lindex $scales $z]
}
proc getLevelYscale {z} {
# TODO figure out for particular game
set scales { 1 1 2 4 }
return [lindex $scales $z]
}
proc getDistance {x1 y1 z1 x2 y2 z2 {penalty 4}} {
set one_x [expr $x1 * [getLevelXscale $z1]]
set one_y [expr $y1 * [getLevelYscale $z1]]
set two_x [expr $x2 * [getLevelXscale $z2]]
set two_y [expr $y2 * [getLevelYscale $z2]]
set maxy [expr {abs($one_y - $two_y)}]
set maxx [expr {abs($one_x - $two_x)}]
set max2 [expr {abs($one_x + $::max_x - $two_x)}]
set maxx [expr {min($maxx, $max2)}]
set max2 [expr {abs($one_x - ($two_x + $::max_x))}]
set maxx [expr {min($maxx, $max2)}]
if {$maxy > $maxx} {
set maxx [expr {($maxx + $maxy) / 2}]
}
if {$z1 != $z2} {
set zdist [expr {abs($z1 -$z2)}]
incr maxx [expr {$penalty * $zdist}]
}
return $maxx
}
proc moveToward {cur_x cur_y cur_z x y z} {
if {$cur_z != $z} {
puts "Can't navigate between levels"
return
}
if {$x == $cur_x && $y == $cur_y} {
return
}
# TODO handle weight and weather
if {$cur_y < $y} {
set d "s"
} else {
set d "n"
}
if {$cur_x < $x} {
append d "e"
} elseif {$cur_x > $x} {
append d "w"
}
# TODO decide when to use wrap
# TODO move multiple
return $d
}
proc moveCoord {x y dir} {
switch $dir {
n { incr y -2 }
s { incr y 2 }
nw { incr x -1; incr y -1 }
ne { incr x 1; incr y -1 }
se { incr x 1; incr y 1 }
sw { incr x -1; incr y 1 }
w {
incr x -1
if {$x & 1} {
incr y -1
} else {
incr y 1
}
}
e {
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 < 0} { set x $maxX }
if {$x > $maxX} { set x 0 }
return [list $x $y]
}
proc parseMan {t} {
set ret [dict create]
set i [lsearch $t "*This race may study *"]
set skill_spec [lindex $t $i]
if {[regexp { *This race may study all skills to level ([[:digit:]]+)} $skill_spec -> all_level]} {
dict set ret ALL $all_level
} elseif {[regexp { *This race may study all non-magic skills to level ([[:digit:]]+)} $skill_spec -> all_level]} {
dict set ret ALL $all_level
} else {
regexp { *This race may study (.*) to level (.*) and all other.* to level ([^ ]*)} $skill_spec -> specs spec_level other_level
dict set ret SPEC_LVL $spec_level
dict set ret OTH_LVL $other_level
foreach s [split $specs ","] {
regexp {.* \[(.*)\]} $s -> skill_name
dict lappend ret SPEC $skill_name
}
# TODO magic level is next list index
}
dict set ret DESC [lreplace $t $i $i]
return $ret
}
proc loadData {filename} {
set tfile [open $filename]
updateDb db [reader::parseFile $tfile]
close $tfile
}
proc writeOrders {fname} {
set f [open $fname "w"]
set pid [::db onecolumn { SELECT player_id FROM settings }]
set ppass [::db onecolumn { SELECT player_pass FROM settings }]
if {$ppass eq ""} {
puts $f "#atlantis $pid"
} else {
puts $f "#atlantis $pid \"$ppass\""
}
set res [::db eval {
SELECT units.name, units.uid, units.orders, detail.x, detail.y, detail.z
FROM detail JOIN units
ON detail.id=units.regionId
WHERE detail.turn=$::currentTurn AND units.detail='own'
ORDER BY detail.z, detail.x, detail.y
}]
set loc ""
foreach {u uid ol x y z} $res {
if {$ol eq ""} continue
set newLoc [list $x $y $z]
if {$loc eq "" || $loc ne $newLoc} {
set loc $newLoc
puts $f ";*** $x $y $z ***"
}
puts $f "unit $uid"
puts $f "; $u"
puts $f "[join $ol "\n"]\n"
}
puts $f "#end"
close $f
}
proc getBuyRace {sells peasants} {
set maxList [list]
set raceList [list]
set priceList [list]
foreach {saleItem cost} $sells {
set race [string trim [lindex $saleItem end] {[]}]
if {[lsearch $::men $race] == -1} {
set fullRace [lrange $saleItem 1 end-1]
if {$peasants ne $fullRace} {
continue
}
lappend ::men $race
}
set ct [lindex $saleItem 0]
lappend raceList [join [lrange $saleItem 1 end]]
lappend maxList $ct
lappend priceList $cost
}
return [list $maxList $raceList $priceList]
}
proc getUnitObjects {id} {
set res [::db eval {
SELECT x, y, z
FROM detail
WHERE id=$id
}]
foreach {x y z} $res {}
set region "$x $y $z"
set ret [list]
set units [::db eval {
SELECT id, name, uid, orders, items, skills, flags
FROM units
WHERE units.regionId=$id AND units.detail='own'
}]
foreach {id name num orders items skills flags} $units {
lappend ret [Unit #auto Id $id \
Name $name Num $num Items $items Orders $orders Skills $skills Flags $flags Region $region Object ""
]
}
return $ret
}
proc findPipeline {units skill} {
set ret [dict create]
set men_level [dict create 1 0 2 0 3 0 4 0 5 0]
set men_total 0
foreach u $units {
set sl [$u cget -skills]
set level [skillLevel $sl $skill]
if {$level == 0} {
continue
}
set il [$u cget -items]
set men [countMen $il]
puts "Found $men at $level"
dict incr men_level $level $men
incr mem_total $men
}
return $ret
}
# output a list like in the original report
proc outputList {f lvals} {
if {$lvals eq ""} {
puts $f " none."
} else {
puts -nonewline $f " [lindex $lvals 0]"
foreach v [lrange $lvals 1 end] {
puts -nonewline $f ", $v"
}
puts $f "."
}
}
proc writeMap {fname} {
set data [db eval {
SELECT x, y, z, type, city, region, wages, pop, race, tax, entertainment, products, sells, wants, detail.exitDirs
FROM terrain join detail
USING(x,y,z)
GROUP BY terrain.x, terrain.y, terrain.z
ORDER BY detail.turn
}]
set f [open $fname "w"]
foreach {x y z type city region wages pop race tax entertainment products sells wants exits} $data {
puts -nonewline $f "$type ($x,$y"
if {$z > 1} {
puts -nonewline $f ",$z"
}
puts -nonewline $f ") in $region"
if {$city ne ""} {
puts -nonewline $f ", contains [lindex $city 0] \[[lindex $city 1]\]"
}
if {$pop ne ""} {
puts -nonewline $f ", $pop peasants ($race), \$$tax"
}
puts $f "."
puts $f "------------------------------------------------------------"
set wg [lindex $wages 0]
set mwg [lindex $wages 1]
if {$wg eq ""} { set wg 0 }
if {$mwg eq ""} { set mwg 0 }
puts $f " Wages: \$$wg (Max: \$$mwg)."
puts -nonewline $f " Wanted:"
outputList $f $wants
puts -nonewline $f " For Sale:"
outputList $f $sells
if {$entertainment eq "none"} { set entertainment 0 }
puts $f " Entertainment available: \$$entertainment."
puts -nonewline $f " Products:"
outputList $f $products
puts $f ""
set dir_map {
North n
Northwest nw
Northeast ne
South s
Southwest sw
Southeast se
}
puts $f "Exits:"
foreach e $exits {
set new_loc [moveCoord $x $y [dict get $dir_map $e]]
foreach {x2 y2} $new_loc {}
set d2 [db eval {SELECT type, region, city FROM terrain WHERE x=$x2 AND y=$y2 AND z=$z}]
foreach {t2 r2 c2} $d2 {}
puts -nonewline $f " $e: $t2 ($x2,$y2"
if {$z > 1} {
puts -nonewline $f ",$z"
}
puts -nonewline $f ") in $r2"
if {$c2 ne ""} {
puts -nonewline $f ", contains [lindex $c2 0] \[[lindex $c2 1]\]"
}
puts $f "."
}
if {[llength $exits] == 0} {
puts $f " none"
}
puts $f ""
puts $f ""
puts $f ""
}
close $f
}
proc readMap {fname} {
set f [open $fname]
set regions ""
set regionData [reader::getRegion $f]
while {$regionData ne ""} {
lappend regions $regionData
set regionData [reader::getRegion $f]
}
close $f
return $regions
}
proc importRegionData {db regions} {
$db eval {BEGIN TRANSACTION}
foreach r $regions {
set loc [dGet $r Location]
set x [lindex $loc 0]
set y [lindex $loc 1]
set z [lindex $loc 2]
set dirs [doExits $db [dGet $r Exits] $z]
set ttype [dGet $r Terrain]
set city [dGet $r Town]
set region [dGet $r Region]
$db eval {
INSERT OR IGNORE INTO terrain VALUES
($x, $y, $z, $ttype, $city, $region);
}
}
$db eval {END TRANSACTION}
}