2 # show your vessels on a map
4 # This is part of ypp-sc-tools, a set of third-party tools for assisting
5 # players of Yohoho Puzzle Pirates.
7 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
23 # are used without permission. This program is not endorsed or
24 # sponsored by Three Rings.
32 #---------- general utilities ----------
37 if {$debug} { puts "DEBUG $m" }
41 puts stderr "where-vessels: bad usage: $m"
50 #---------- expecting certain errors ----------
52 proc errexpect-setline {lno line} {
53 glset errexpect_lno $lno
54 glset errexpect_line $line
57 proc errexpect-error {m} {
58 global errexpect_line errexpect_lno
59 error $m "$errexpect_line\n" [list YARRG-ERREXPECT $errexpect_lno]
62 proc errexpect-arrayget {arrayvar key} {
64 upvar 1 ${arrayvar}($key) v
65 if {[info exists v]} { return $v }
66 errexpect-error "undefined $key"
69 proc errexpect-arrayget-boolean {arrayvar key} {
70 switch -exact [uplevel 1 [list errexpect-arrayget $arrayvar $key]] {
73 default { errexpect-error "unexpected $key" }
77 proc errexpect-catch {code} {
78 global errorInfo errorCode
82 debug "ERREXPECT CATCH |$rc|$rv|$errorCode|$errorInfo|"
83 if {$rc==1 && ![string compare YARRG-ERREXPECT [lindex $errorCode 0]]} {
84 return [list 1 $rv [lindex $errorCode 1] $errorInfo]
88 return -code $rc -errorinfo $errorInfo -errorcode $errorCode $rv
92 #---------- argument parsing ----------
96 if {$ai >= [llength $argv]} {
97 badusage "option [lindex $argv [expr {$ai-1}]] needs a value"
99 set v [lindex $argv $ai]
104 set notes_loc vessel-notes
105 set scraper {./yppedia-ocean-scraper --chart}
106 set info_cache _vessel-info-cache
107 set info_source rsync.yarrg.chiark.net::yarrg/vessel-info
114 while {[regexp {^\-} [set arg [lindex $argv $ai]]]} {
116 switch -exact -- $arg {
118 --pirate { glset pirate [string totitle [nextarg]] }
119 --ocean { glset ocean [string totitle [nextarg]] }
120 --clipboard-file { load-clipboard-file [nextarg] }
121 --local-html-dir { lappend scraper --local-html-dir=[nextarg] }
122 --notes { glset notes_loc [nextarg] }
123 --vessel-info-source { glset info_source [nextarg] }
124 --debug { incr debug }
125 default { badusage "unknown option $arg" }
128 set argv [lrange $argv $ai end]
129 if {[llength $argv]} { badusage "non-option args not allowed" }
132 proc argdefaults {} {
133 global ocean notes_loc pirate scraper
134 if {![info exists ocean] || ![info exists pirate]} {
135 set cmd {./yarrg --find-window-only --quiet}
136 if {[info exists ocean]} { lappend cmd --ocean $ocean }
137 if {[info exists pirate]} { lappend cmd --pirate $pirate }
138 manyset [split [eval exec $cmd] " "] ocean pirate
139 if {![llength $ocean] || ![llength $pirate]} {
140 error "$ocean $pirate ?"
143 lappend scraper $ocean
147 #---------- loading and parsing the vessel notes ----------
150 global notes_loc notes_data
151 if {[regexp {^\w+\:} $notes_loc]} {
153 debug "FETCHING NOTES $notes_loc"
154 set req [::http::geturl $notes_loc]
155 switch -glob [::http::status $req].[::http::ncode $req] {
157 ok.* { error "retrieving vessel-notes: [::http::code $req]" }
158 * { error "Retrieving vessel-notes: [::http::error $req]" }
160 set newdata [::http::data $req]
163 debug "READING NOTES $notes_loc"
164 set vn [open $notes_loc]
165 set newdata [read $vn]
168 set notes_data $newdata
171 proc parse-notes {} {
172 global notes_data notes
173 catch { unset notes }
176 foreach l [split $notes_data "\n"] {
178 errexpect-setline $lno $l
179 set l [string trim $l]
180 if {![string length $l]} continue
181 if {[regexp {^\#} $l]} continue
182 if {![regexp -expanded \
183 {^ (\d+) (?: \s+([^=]*?) )? \s* =
185 (?: \s+ (\S+) )?)? $} \
186 $l dummy vid vname owner note]} {
187 errexpect-error "badly formatted"
189 set vname [string trim $vname]
190 if {[info exists notes($vid)]} {
191 errexpect-error "duplicate vesselid $vid"
193 set notes($vid) [list $lno $vname $owner $note]
197 proc note-info {lno vid name island description} {
199 lappend note_infos [list $lno $vid $name $island $description]
202 proc display-note-infos {} {
203 global note_infos note_missings notes
205 set nmissing [llength $note_missings]
206 debug "display-note-infos $nmissing [array size notes]"
208 if {[llength $note_infos]} {
209 set tiny "[llength $note_infos] warning(s)"
210 } elseif {$nmissing && [array size notes]} {
211 set tiny "$nmissing missing"
218 foreach info $note_infos {
219 manyset $info lno vid name island description
220 append infodata "vessel"
221 append infodata " $vid"
222 if {[string length $name]} { append infodata " $name" }
223 if {[string length $island]} { append infodata " ($island)" }
224 append infodata ": " $description "\n"
228 if {[string length $infodata]} { append infodata "\n" }
229 append infodata "$nmissing vessel(s) not mentioned in notes:\n"
231 foreach info [lsort $note_missings] {
232 manyset $info island name vid
233 if {[string compare $island $last_island]} {
234 append infodata "# $island:\n"
235 set last_island $island
237 append infodata [format "%-9d %-29s =\n" $vid $name]
241 parser-control-failed-core .cp.ctrl.notes notes \
244 "[llength $note_infos] warning(s);\
245 $nmissing vessel(s) missing" \
246 "Full description of warnings and missing vessels:" \
250 #---------- vessel properties ----------
252 proc info-cache-update {} {
253 global info_source info_cache
254 file mkdir $info_cache
255 exec sh -c "cp -u icons/* $info_cache/."
257 if {[string length $info_source]} {
260 --exclude=*~ --exclude=*.bak --exclude=.* --exclude=*.tmp \
261 $info_source/ $info_cache 2>@ stderr]
262 debug "INFO-CACHE $cmdl"
266 set f [open $info_cache/vessel-info]
267 glset vessel_size_data [read $f]
271 proc vesselinfo-init {} {
272 global vc_game2code vc_code2abbrev vc_code2full vc_codes
274 global vessel_size_data
275 manyset $vessel_size_data sizeinfos subclassinfos
278 foreach {game code abbrev full} $sizeinfos {
279 if {![regexp {^[a-z][a-z]$} $code code]} { error "bad code" }
280 if {![regexp {^[a-z][a-z]$} $abbrev abbrev]} { error "bad abbrev" }
281 lappend vc_codes $code
282 set vc_game2code($game) $code
283 set vc_code2abbrev($code) $abbrev
284 set vc_code2full($code) $full
288 global vsc_code2report
290 set vsc_game2code(null) {}
291 set vsc_code2report() Ordinary
292 set vsc_code2report(!) "(Special/L.E.)"
293 foreach {game code full} $subclassinfos {
294 if {![regexp {^[A-Z]$} $code code]} { error "bad code" }
295 set vsc_game2code($game) $code
296 set vsc_code2report($code) $full
300 set owners {ours dot query}
301 foreach b $owners { load-icon $b }
302 foreach a {battle borrow dot} {
304 foreach b $owners { load-icon-combine $a $b }
308 proc load-icon {icon} {
310 image create bitmap icon/$icon -file $info_cache/$icon.xbm
313 proc load-icon-combine {args} {
316 set delim "pnmcat -lr "
318 append cmd $delim " <(xbmtopbm $info_cache/$icon.xbm)"
319 set delim " <(pbmmake -white 1 1)"
321 append cmd " | pbmtoxbm"
322 debug "load-icon-combine $cmd"
323 image create bitmap icon/[join $args +] -data [exec bash -c $cmd]
326 proc code-lockown2icon {lockown} {
327 manyset [split $lockown ""] lock notown
329 [lindex {battle borrow dot} $lock]
330 [lindex {ours dot query {} {} dot} $notown]
332 if {[llength $l]} { return icon/[join $l +] } { return {} }
335 proc canvas-horiz-stack {xvar xoff y bind type args} {
337 upvar 1 canvas canvas
338 set id [eval $canvas create $type [expr {$x+$xoff}] $y $args]
339 set bbox [$canvas bbox $id]
340 # debug "CANVAS-HORIZ-STACK $type $x $xoff $id $bbox [list $args]"
341 set x [lindex $bbox 2]
342 $canvas bind $id <ButtonPress> $bind
346 proc code2canvas {code canvas x yvar qty qtylen bind} {
347 global vc_code2abbrev
350 manyset [split $code _] inport size subclass lockown xabbrev
354 set imy [expr {$y+2}]
356 if {!$inport} { incr qtylen -1 }
357 if {$qtylen<=0} { set qtylen {} }
358 set qty [format "%${qtylen}s" $qty]
360 set qtyid [canvas-horiz-stack stackx 0 $y $bind \
361 text -anchor nw -font fixed -text $qty]
364 canvas-horiz-stack stackx 0 $imy $bind \
365 image -anchor nw -image icon/atsea
369 upvar #0 vc_code2abbrev($size) vcabb
370 if {![info exists vcabb]} {
372 image create bitmap icon/$vcabb -data \
373 [exec pbmtext -builtin fixed $size | pnminvert | pnmcrop >t.pnm]
375 canvas-horiz-stack stackx -1 $imy $bind \
376 image -anchor nw -image icon/$vcabb
378 if {[string length $subclass]} {
379 canvas-horiz-stack stackx 0 $y $bind \
380 text -anchor nw -font fixed -text \
385 set lockownicon [code-lockown2icon $lockown]
386 if {[string length $lockownicon]} {
387 canvas-horiz-stack stackx 0 $imy $bind \
388 image -anchor nw -image $lockownicon
392 if {[string length $xabbrev]} {
393 canvas-horiz-stack stackx 0 $y $bind \
394 text -anchor nw -font fixed -text \
398 set bbox [$canvas bbox $qtyid]
399 set ny [lindex $bbox 3]
400 set bid [$canvas create rectangle \
405 $canvas lower $bid $qtyid
407 $canvas bind $bid <ButtonPress> $bind
410 proc show-report-decode {code} {
415 manyset [split $code _] inport sizecode subclass lockown xabbrev
416 manyset [split $lockown ""] lock notown
418 report-set inport [lindex {{At Sea} {In port}} $inport]
420 upvar #0 vc_code2full($sizecode) sizefull
421 upvar #0 smash_sizeinexact($sizecode) sizeinexact
422 set size_report $sizefull
423 if {[info exists sizeinexact]} { set size_report "($sizefull+)" }
424 report-set size $size_report
426 global smash_subclass
427 if {$smash_subclass >= 2} {
428 report-set subclass "(Any class)"
430 upvar #0 vsc_code2report($subclass) subclass_report
431 info exists subclass_report
433 report-set subclass $subclass_report
435 report-set subclass "Class \"$subclass\""
438 report-set lock [lindex {
439 {Battle ready} {Unlocked} {Locked}
440 {(All lock states)} {(Not battle ready)}
443 switch -exact $notown {
444 0 { report-set own "Yours" }
445 1 { report-set own "Other pirate's" }
446 2 { report-set own "Owner unknown" }
447 3 { report-set own "(All ownerships)" }
448 4 - 5 { report-set own "(Yours/unknown)" }
449 default { report-set own "?? $notown" }
452 if {[string length $xabbrev]} {
453 report-set xabbrev "Notes flags: $xabbrev"
455 report-set xabbrev "No flags in notes"
459 #---------- common to smashing and filtering ----------
461 proc make-control {parent ctrl label ekind} {
462 debug "MAKE-CONTROL [list $parent $ctrl $label $ekind]"
463 label $parent.lab_$ctrl -text $label -justify left
465 manyset [grid size $parent] dummy row
467 grid configure $parent.lab_$ctrl -row $row -column 0 -sticky nw -pady 4
468 grid configure $parent.$ctrl -row $row -column 1 -sticky w -pady 3
472 proc begin-control-grid {cw count rows inrow} {
473 if {!$inrow} { set inrow [expr {($count + $rows) / $rows}] }
474 upvar #0 control_grid_properties($cw) props
475 set props [list $rows $inrow]
479 proc make-control-grid-elem {cw kind ix ekind args} {
480 upvar #0 control_grid_properties($cw) props
481 manyset $props rows inrow
485 debug "MAKE-CONTROL-GRID-ELEM $cw $kind $ix $ekind $rows $inrow $ew"
487 eval [list $ekind $ew] $args
489 switch -exact $kind {
491 grid configure $ew -sticky sw \
492 -row [expr {$ix / $inrow}] \
493 -column [expr {$ix % $inrow}]
496 grid configure $ew -sticky se \
497 -row [expr {$rows-1}] \
498 -column [expr {$inrow-1}]
507 proc control-tickbox-flip {varsvn values} {
508 upvar #0 $varsvn vars
509 foreach val $values {
510 set vars($val) [expr {!$vars($val)}]
512 redraw-needed c.-tickbox-flip $varsvn $values
515 proc populate-control-grid-tickboxes {cw rows inrow varsvn values flipvalues
516 label_kind valvn default_get label_get} {
517 debug "POPULATE-CONTROL-GRID-TICKBOXES $cw $rows $inrow $varsvn\
518 [list $values] $label_kind $valvn"
520 upvar #0 $varsvn vars
522 set count [llength $values]
524 begin-control-grid $cw $count $rows $inrow
526 for {set ix 0} {$ix < $count} {incr ix} {
527 set val [lindex $values $ix]
528 set vars($val) [uplevel 1 $default_get]
529 set ew [make-control-grid-elem $cw ix $ix checkbutton \
530 -variable ${varsvn}($val) \
532 -command [list redraw-needed c.-g.-tickbox $cw $val]]
533 $ew configure -$label_kind [uplevel 1 $label_get]
534 switch -exact $label_kind {
535 image { $ew configure -height 16 }
538 [make-control-grid-elem $cw final invert button] \
540 -text flip -command [list control-tickbox-flip $varsvn $flipvalues] \
544 #---------- smashing ----------
549 proc smash-code {code} {
550 manyset [split $code _] inport size subclass lockown xabbrev
552 upvar #0 smash_sizemap($size) smsize
554 global smash_subclass
555 if {$smash_subclass > 1} {
557 } elseif {$smash_subclass && [string length $subclass]} {
562 switch $smash_owner {
564 1 { regsub {[12]$} $lockown 5 lockown }
566 if {[regexp {^0.} $lockown]} {
567 # battle ready / all lock states
569 } elseif {[regexp {^.0} $lockown]} {
570 # not battle ready / yours
573 # state (not battle ready) / not known to be yours
574 regsub {.$} $lockown 4 lockown
577 3 { regsub {.$} $lockown {3} lockown }
581 return [join [list $inport $smsize $subclass $lockown $xabbrev] _]
584 proc smash-prepare {} {
585 global vc_codes smash_sizemap smash_size smash_sizeinexact
587 catch { unset smash_sizeplus }
588 foreach size $vc_codes {
589 if {!$smash_size($size)} {
592 set smash_sizeinexact($mapto) 1
594 set smash_sizemap($size) $mapto
598 proc make-smasher {sma label ekind} {
599 return [make-control .smash $sma $label $ekind]
602 proc make-radio-smasher {sma label variable descs rows inrow} {
603 set w [make-smasher $sma $label frame]
604 begin-control-grid $w [llength $descs] $rows $inrow
605 for {set i 0} {$i < [llength $descs]} {incr i} {
606 make-control-grid-elem $w ix $i \
608 -variable $variable -value $i -command redraw-needed \
609 -text [lindex $descs $i]
613 proc make-smashers {} {
614 global vc_codes vc_code2abbrev
615 set cw [make-smasher size "Size\n round\n down" frame]
616 populate-control-grid-tickboxes $cw 2 0 smash_size \
617 $vc_codes [lrange $vc_codes 1 end] \
618 image val { expr 0 } { expr {"icon/$vc_code2abbrev($val)"} }
619 $cw.0 configure -state disabled
621 make-radio-smasher subclass Class smash_subclass \
622 {Show Normal/LE Hide} 1 0
624 make-radio-smasher owner Owner smash_owner \
625 {Show Yours? {For you} Lock Hide} 2 3
628 #---------- filtering ----------
632 proc filter-values/size {} { global vc_codes; return $vc_codes }
633 proc filter-icon/size {code} {
634 upvar #0 vc_code2abbrev($code) abb
637 proc filter-default/size {code} { return 1 }
638 proc filter-says-yes/size {codel} {
639 set sizecode [lindex $codel 1]
640 upvar #0 filter_size($sizecode) yes
644 proc filter-values/lockown {} {
647 lappend vals "$lv$ov"
652 proc filter-icon/lockown {lockown} { return [code-lockown2icon $lockown] }
653 proc filter-default/lockown {lockown} {
654 return [regexp {^[01]|^2[^1]} $lockown]
656 proc filter-says-yes/lockown {codel} {
657 set lockown [lindex $codel 3]
658 upvar #0 filter_lockown($lockown) yes
659 debug "FILTER-SAYS-YES/LOCKOWN $codel $lockown $yes"
663 proc filter-validate/xabbre {re} {
667 regsub {^.*:\s*} $emsg {} emsg
668 regsub {^.*(.{30})$} $emsg {\1} emsg
673 proc filter-says-yes/xabbre {codel} {
675 set xabbrev [lindex $codel 4]
676 return [regexp -- $filter_xabbre $xabbrev]
679 proc make-tickbox-filter {fil label rows inrow} {
680 set values [filter-values/$fil]
682 if {![catch { info args filter-icon/$fil }]} {
683 set label_get { filter-icon/$fil $val }
686 set label_get { filter-map/$fil $val }
690 set fw [make-filter $fil $label frame]
692 populate-control-grid-tickboxes $fw $rows $inrow filter_$fil \
694 $label_kind val { filter-default/$fil $val } $label_get
697 proc entry-filter-changed {fw fil n1 n2 op} {
699 upvar #0 filter_$fil realvar
700 upvar #0 filterentered_$fil entryvar
701 global def_background
702 debug "entry-filter-changed $fw $fil $entryvar"
704 set error [filter-validate/$fil $entryvar]
705 if {[string length $error]} {
706 $fw.error configure -text $error -foreground white -background red
708 $fw.error configure -text { } -background $def_background
709 set realvar $entryvar
713 puts stderr "FILTER CHECK ERROR $emsg $errorInfo"
717 proc make-entry-filter {fil label def} {
718 global filterentered_$fil
719 upvar #0 filter_$fil realvar
721 set fw [make-filter $fil $label frame]
722 entry $fw.entry -textvariable filterentered_$fil
724 glset def_background [$fw.error cget -background]
725 trace add variable filterentered_$fil write \
726 [list entry-filter-changed $fw $fil]
727 pack $fw.entry $fw.error -side top -anchor w
730 proc make-filter {fil label ekind} {
733 return [make-control .filter $fil $label $ekind]
736 proc make-filters {} {
737 make-tickbox-filter size Size 2 0
738 make-tickbox-filter lockown "Lock/\nowner" 2 6
739 make-entry-filter xabbre "Flags\n regexp" {}
742 proc filterstyle-changed {n1 n2 op} {
744 debug "filterstyle-changed $filterstyle"
748 proc filters-say-yes {code} {
749 global filters filterstyle
750 set codel [split $code _]
751 set lockown [lindex $codel 3]
752 switch -exact $filterstyle {
754 1 { return [filter-default/lockown $lockown] }
755 2 { return [regexp {^.0} $lockown] }
757 default { error $filterstyle }
760 foreach fil $filters {
761 if {![filter-says-yes/$fil $codel]} {
762 debug "FILTERS-SAY-YES $code NO $fil"
766 debug "FILTERS-SAY-YES $code YES $filters"
770 #---------- loading and parsing the clipboard (vessel locations) ----------
773 global pirate notes_used note_missings newnotes
777 lappend codel [errexpect-arrayget-boolean vi inPort]
779 set gamesize [errexpect-arrayget vi vesselClass]
780 upvar #0 vc_game2code($gamesize) size
781 if {![info exists size]} {
782 set size "($gamesize)"
783 upvar #0 vc_code2abbrev($size) vcabb
785 set data [exec pbmtext -builtin fixed " $gamesize " \
786 | pnminvert | pnmcrop | pbmtoxbm]
787 debug "INVENTED ICON $vcabb $data"
788 image create bitmap icon/$vcabb -data $data
791 set vc_code2full($size) "Type \"$gamesize\""
795 set gamesubclass [errexpect-arrayget vi vesselSubclass]
796 upvar #0 vsc_game2code($gamesubclass) subclass
797 if {[info exists subclass]} {
798 lappend codel $subclass
800 lappend codel ($gamesubclass)
803 switch -exact [errexpect-arrayget vi isLocked]/[ \
804 errexpect-arrayget vi isBattleReady] {
805 true/false { set lock 2 }
806 false/false { set lock 1 }
807 false/true { set lock 0 }
808 default { errexpect-error "unexpected isLocked/isBattleReady" }
811 set vid [errexpect-arrayget vi vesselId]
812 upvar #0 notes($vid) note
813 set realname [errexpect-arrayget vi vesselName]
814 set island [errexpect-arrayget vi islandName]
818 if {[info exists note]} {
819 manyset $note lno notename owner xabbrev
820 if {[string compare -nocase $realname $notename]} {
821 note-info $lno $vid $realname $island \
822 "notes say name is $notename"
824 if {[string length $owner]} {
825 if {![string compare $owner $pirate]} {
833 append abbrev $xabbrev
834 set notes_used($vid) 1
838 lappend note_missings [list $island $realname $vid]
841 lappend codel "$lock$notown" $xabbrev
842 lappend newnotes [list $vid $realname $owner $xabbrev]
843 set kk "$island [join $codel _]"
844 upvar #0 found($kk) k
845 lappend k [list $vid $realname $owner]
847 debug "CODED $kk $vid $realname"
851 proc parse-clipboard {} {
852 global clipboard found notes notes_used newnotes
854 catch { unset found }
855 catch { unset notes_used }
857 glset note_missings {}
861 set itemre { (\w+) = ([^=]*) }
862 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
866 foreach l [split $clipboard "\n"] {
868 errexpect-setline $lno $l
869 if {![string length $l]} continue
872 if {![regexp -expanded $manyitemre $l dummy \
873 thiskey thisval rhs]} {
874 errexpect-error "badly formatted"
876 set vi($thiskey) $thisval
877 if {![string length $rhs]} break
878 regsub {^, } $rhs {} rhs
884 if {[llength $newnotes]} {
885 foreach vid [lsort [array names notes]] {
886 if {![info exists notes_used($vid)]} {
887 manyset $notes($vid) lno notename
888 note-info $lno $vid $notename {} \
889 "vessel in notes no longer found"
895 proc load-clipboard-file {fn} {
897 glset clipboard [read $f]
902 #---------- loading and parsing the chart ----------
906 debug "FETCHING CHART"
907 set chart [eval exec $scraper [list | perl -we {
912 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
913 sub { sprintf "%d %d", @_; },
914 sub { printf "archlabel %d %d %s\n", @_; },
915 sub { printf "island %s {%s} %s\n", @_; },
916 sub { printf "league %s %s %s.\n", @_; },
917 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
919 STDOUT->error and die $!;
928 return [expr {$c * $scale}]
931 proc chart-got/archlabel {args} { }
932 proc chart-got/island {x y isle sizecol} {
933 debug "ISLE $x $y $isle $sizecol"
934 global canvas isleloc
935 set isleloc($isle) [list $x $y]
937 # $canvas create oval \
938 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
939 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
942 if {[string match *_col $sizecol]} { set colour black }
943 $canvas create text [coord $x] [coord $y] \
944 -text $isle -anchor s -fill $colour
946 proc chart-got/league {x1 y1 x2 y2 kind} {
947 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
949 set l [$canvas create line \
950 [coord $x1] [coord $y1] \
951 [coord $x2] [coord $y2]]
952 if {![string compare $kind .]} {
953 $canvas itemconfigure $l -dash .
957 proc debug-filter-array {array} {
959 set m " FILTER $array"
960 foreach k [lsort [array names a]] {
961 append m " $k=$a($k)"
966 proc redraw-needed {args} {
968 debug "REDRAW NEEDED $args"
969 if {[info exists redraw_after]} return
972 debug " FILTER style $filterstyle"
973 debug-filter-array filter_size
974 debug-filter-array filter_lockown
976 debug " FILTER xabbre $filter_xabbre"
978 set redraw_after [after 250 draw]
982 global chart found isleloc canvas redraw_after islandnames smfound
984 catch { after cancel $redraw_after }
985 catch { unset redraw_after }
989 foreach l [split $chart "\n"] {
990 # debug "CHART-GOT $l"
991 set proc [lindex $l 0]
992 eval chart-got/$proc [lrange $l 1 end]
997 catch { unset smfound }
998 foreach key [lsort [array names found]] {
999 regexp {^(.*) (\S+)$} $key dummy islandname code
1001 if {![filters-say-yes $code]} continue
1003 set smcode [smash-code $code]
1004 debug "smashed $code => $smcode"
1005 set smkey "$islandname $smcode"
1006 foreach vessel $found($key) { lappend smfound($smkey) $vessel }
1010 set lastislandname {}
1011 foreach smkey [lsort [array names smfound]] {
1012 set c [llength $smfound($smkey)]
1013 regexp {^(.*) (\S+)$} $smkey dummy islandname code
1014 debug "SHOWING [list $smkey $c $islandname $code l=$lastislandname]"
1016 if {[string compare $lastislandname $islandname]} {
1017 manyset $isleloc($islandname) x y
1020 set lastislandname $islandname
1021 lappend islandnames $islandname
1022 # debug "START Y $y"
1025 if {$c > 1} { set qty [format %d $c] } else { set qty {} }
1026 code2canvas $code $canvas $x y $qty 2 \
1027 [list show-report $islandname $code]
1031 panner::updatecanvas-bbox .cp.ctrl.pan
1037 #---------- parser error reporting ----------
1039 proc parser-control-create {w base invokebuttontext etl_title} {
1041 button $w.do -text $invokebuttontext -command invoke_$base -pady 3
1043 frame $w.resframe -width 120 -height 32
1044 button $w.resframe.res -text {} -anchor nw \
1045 -padx 1 -pady 1 -borderwidth 0 -justify left
1046 glset deffont_$base [$w.resframe.res cget -font]
1047 place $w.resframe.res -relx 0.5 -y 0 -anchor n
1049 pack $w.do -side top
1050 pack $w.resframe -side top -expand y -fill both
1055 wm title $eb "where-vessels - $etl_title"
1056 wm protocol $eb WM_DELETE_WINDOW [list wm withdraw $eb]
1058 label $eb.title -text $etl_title
1059 pack $eb.title -side top
1061 button $eb.close -text Close -command [list wm withdraw $eb]
1062 pack $eb.close -side bottom
1064 frame $eb.emsg -bd 2 -relief groove
1065 label $eb.emsg.lab -anchor nw -text "Error:"
1066 text $eb.emsg.text -height 1
1067 pack $eb.emsg.text -side bottom -fill x
1068 pack $eb.emsg.lab -side left
1070 pack $eb.emsg -side top -pady 2 -fill x
1072 frame $eb.text -bd 2 -relief groove
1073 pack $eb.text -side bottom -pady 2 -fill both -expand y
1075 label $eb.text.lab -anchor nw
1077 text $eb.text.text -width 85 \
1078 -xscrollcommand [list $eb.text.xscroll set] \
1079 -yscrollcommand [list $eb.text.yscroll set]
1080 $eb.text.text tag configure error \
1081 -background red -foreground white
1083 scrollbar $eb.text.xscroll -orient horizontal \
1084 -command [list $eb.text.text xview]
1085 scrollbar $eb.text.yscroll -orient vertical \
1086 -command [list $eb.text.text yview]
1088 grid configure $eb.text.lab -row 0 -column 0 -sticky w -columnspan 2
1089 grid configure $eb.text.text -row 1 -column 0 -sticky news
1090 grid configure $eb.text.yscroll -sticky ns -row 1 -column 1
1091 grid configure $eb.text.xscroll -sticky ew -row 2 -column 0
1092 grid rowconfigure $eb.text 0 -weight 0
1093 grid rowconfigure $eb.text 1 -weight 1
1094 grid rowconfigure $eb.text 2 -weight 0
1095 grid columnconfigure $eb.text 0 -weight 1
1096 grid columnconfigure $eb.text 1 -weight 0
1099 proc parser-control-ok-core {w base background show} {
1100 debug "parser-control-ok-core $w $base $background $show"
1101 upvar #0 deffont_$base deffont
1102 $w.resframe.res configure \
1103 -background $background -disabledforeground black -font $deffont \
1104 -state disabled -command {} \
1107 proc parser-control-ok {w base show} {
1108 parser-control-ok-core $w $base green $show
1110 proc parser-control-none {w base show} {
1111 parser-control-ok-core $w $base blue $show
1113 proc parser-control-failed-core {w base foreground background smallfont
1114 tiny summary fulldesc fulldata} {
1115 debug "parser-control-failed-core $w $base $summary $fulldesc"
1116 upvar #0 deffont_$base deffont
1119 $eb.emsg.text delete 0.0 end
1120 $eb.emsg.text insert end $summary
1122 $eb.text.lab configure -text $fulldesc
1123 $eb.text.text delete 0.0 end
1124 $eb.text.text insert end $fulldata
1126 regsub -all {.{18}} $tiny "&\n" ewrap
1134 $w.resframe.res configure \
1135 -background $background -foreground $foreground -font $font \
1136 -state normal -command [list wm deiconify $eb] \
1140 proc parser-control-failed-expected {w base emsg lno ei fulldesc newdata} {
1143 set line [lindex [split $ei "\n"] 0]
1144 debug "parser-control-failed-expected: $w $base: $lno: $emsg\n $line"
1146 parser-control-failed-core $w $base \
1148 "err: [string trim $emsg]: \"$line\"" \
1149 "at line $lno: $emsg" \
1152 $eb.text.text tag add error $lno.0 $lno.end
1153 $eb.text.text see $lno.0
1155 proc parser-control-failed-unexpected {w base emsg ei} {
1157 parser-control-failed-core $w $base \
1159 $emsg $emsg "Details and stack trace:" $ei
1162 proc reparse {base varname old fulldesc okshow noneshow parse ok} {
1163 upvar #0 $varname var
1164 manyset [errexpect-catch {
1166 if {[string length [string trim $var]]} {
1167 parser-control-ok .cp.ctrl.$base $base $okshow
1169 parser-control-none .cp.ctrl.$base $base $noneshow
1171 }] failed emsg lno ei
1173 parser-control-failed-expected .cp.ctrl.$base $base \
1174 $emsg $lno $ei $fulldesc $var
1182 #---------- island names selection etc. ----------
1184 proc islandnames-update {} {
1186 .islands.count configure -text [format "ships at %d island(s)" \
1187 [llength $islandnames]]
1190 proc islandnames-select {} {
1191 .islands.clip configure -relief sunken -state disabled
1192 selection own -command islandnames-deselect .islands.clip
1194 proc islandnames-deselect {} {
1195 .islands.clip configure -relief raised -state normal
1198 proc islandnames-handler {offset maxchars} {
1200 return [string range [join $islandnames ", "] \
1201 $offset [expr {$offset+$maxchars-1}]]
1204 #---------- main user interface ----------
1206 proc widgets-setup {} {
1207 global canvas debug pirate ocean filterstyle
1209 wm geometry . 1200x800
1210 wm title . "where-vessels - $pirate on the $ocean ocean"
1214 frame .f -border 1 -relief groove
1217 pack $canvas -expand 1 -fill both
1218 pack .f -expand 1 -fill both -side left
1220 #----- control panels and filter -----
1223 frame .smash -relief groove -bd 2 -padx 1
1224 frame .filter -relief groove -bd 2 -padx 1
1225 frame .islands -pady 2
1226 pack .cp .filter .islands .smash -side top
1228 label .smash.title -text {Display/combine details}
1229 grid .smash.title -row 0 -column 0 -columnspan 2
1232 trace add variable filterstyle write filterstyle-changed
1235 label .filter.title.title -text Show
1236 pack .filter.title.title -side left
1237 for {set fing 0} {$fing < 4} {incr fing} {
1238 radiobutton .filter.title.f$fing \
1239 -variable filterstyle -value $fing \
1240 -text [lindex {All Useable Mine These:} $fing]
1241 pack .filter.title.f$fing -side left
1244 grid configure .filter.title -row 0 -column 0 -columnspan 2
1246 #----- control panel -----
1249 pack .cp.ctrl -side left -anchor n
1251 debug "BBOX [$canvas bbox all]"
1253 panner::canvas-scroll-bbox .f.c
1254 panner::create .cp.ctrl.pan .f.c 120 120 $debug
1256 pack .cp.ctrl.pan -side top -pady 0 -padx 5
1258 pack .cp.ctrl.zoom -side top
1260 button .cp.ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2} -pady 0
1261 button .cp.ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2} -pady 0
1262 pack .cp.ctrl.zoom.out .cp.ctrl.zoom.in -side left
1264 parser-control-create .cp.ctrl.acquire \
1266 "Clipboard parsing error" \
1268 pack .cp.ctrl.acquire -side top -pady 2
1270 parser-control-create .cp.ctrl.notes \
1271 notes "Reload notes" \
1272 "Vessel notes loading report" \
1274 pack .cp.ctrl.notes -side top -pady 2
1276 #----- island name count and copy -----
1278 label .islands.count
1279 button .islands.clip -text "copy island names" -pady 2 -padx 2 \
1280 -command islandnames-select
1281 selection handle .islands.clip islandnames-handler
1282 pack .islands.count .islands.clip -side left
1284 #----- decoding etc. report -----
1287 pack .cp.report -side left -anchor n
1289 label .cp.report.island -text { }
1291 canvas .cp.report.abbrev -width 1 -height 15
1293 frame .cp.report.code
1294 label .cp.report.code.lab -text Code:
1295 glset report_code { }
1296 entry .cp.report.code.code -state readonly \
1297 -textvariable report_code -width 15
1298 pack .cp.report.code.lab .cp.report.code.code -side left
1299 frame .cp.report.details -bd 2 -relief groove -padx 2 -pady 2
1301 listbox .cp.report.list -height 5
1303 pack .cp.report.island .cp.report.abbrev .cp.report.details \
1304 .cp.report.list -side top
1305 #pack .cp.report.code -side top
1306 pack configure .cp.report.details -fill x
1308 foreach sw {inport size subclass lock own xabbrev} {
1309 label .cp.report.details.$sw -text { }
1310 pack .cp.report.details.$sw -side top -anchor w
1314 proc report-set {sw val} { .cp.report.details.$sw configure -text $val }
1316 proc show-report {islandname code} {
1317 .cp.report.island configure -text $islandname
1319 .cp.report.abbrev delete all
1321 code2canvas $code .cp.report.abbrev 5 y {} 0 {}
1322 manyset [.cp.report.abbrev bbox all] minx dummy maxx dummy
1323 .cp.report.abbrev configure -width [expr {$maxx-$minx+4}]
1325 glset report_code $code
1326 show-report-decode $code
1328 set kk "$islandname $code"
1329 upvar #0 smfound($kk) k
1331 .cp.report.list delete 0 end
1334 manyset $elem vid name owner
1335 lappend owned($owner) $name
1338 foreach owner [lsort [array names owned]] {
1339 if {[string length $owner]} {
1340 set owndesc "$owner's"
1342 set owndesc "Owner unknown"
1344 .cp.report.list insert end "$owndesc:"
1345 foreach name $owned($owner) {
1346 .cp.report.list insert end " $name"
1351 proc zoom {extail} {
1353 set nscale [expr "\$scale $extail"]
1354 debug "ZOOM $scale $nscale"
1355 if {$nscale < 1 || $nscale > 200} return
1360 proc invoke_acquire {} {
1361 global clipboard errorInfo
1365 set clipboard [clipboard get]
1367 parser-control-failed-unexpected .cp.ctrl.acquire acquire \
1368 $emsg "fetching clipboard:\n\n$errorInfo"
1373 clipboard $old "Clipboard contents:" { acquired ok } { no vessels } {
1381 proc invoke_notes {} {
1382 global notes_data errorInfo notes_loc
1388 parser-control-failed-unexpected .cp.ctrl.notes notes \
1389 $emsg "loading $notes_loc:\n\n$errorInfo"
1394 notes_data $old "Vessel notes:" "loaded ok" { no notes } {
1403 #---------- main program ----------
1407 httpclientsetup where-vessels
1416 if {[catch { parse-clipboard } emsg]} {
1417 puts stderr "$emsg\n$errorInfo"
1420 after idle invoke_notes
1425 package require Tclx
1426 commandloop -async \
1427 -prompt1 { return "where-vessels% " } \
1428 -prompt2 { return "> " }
1431 # rsync -r --exclude=\*~ yarrg/icons/. ijackson@chiark.greenend.org.uk:/home/ftp/users/ijackson/yarrg/vessel-info/.