+source yarrglib.tcl
+source panner.tcl
+package require http
+
+#---------- general utilities ----------
+
+set debug 0
+proc debug {m} {
+ global debug
+ if {$debug} { puts "DEBUG $m" }
+}
+
+proc badusage {m} {
+ puts stderr "where-vessels: bad usage: $m"
+ exit 1
+}
+
+proc glset {n val} {
+ upvar #0 $n var
+ set var $val
+}
+
+#---------- expecting certain errors ----------
+
+proc errexpect-setline {lno line} {
+ glset errexpect_lno $lno
+ glset errexpect_line $line
+}
+
+proc errexpect-error {m} {
+ global errexpect_line errexpect_lno
+ error $m "$errexpect_line\n" [list YARRG-ERREXPECT $errexpect_lno]
+}
+
+proc errexpect-arrayget {arrayvar key} {
+ upvar 1 $arrayvar av
+ upvar 1 ${arrayvar}($key) v
+ if {[info exists v]} { return $v }
+ errexpect-error "undefined $key"
+}
+
+proc errexpect-arrayget-boolean {arrayvar key} {
+ switch -exact [uplevel 1 [list errexpect-arrayget $arrayvar $key]] {
+ true { return 1 }
+ false { return 0 }
+ default { errexpect-error "unexpected $key" }
+ }
+}
+
+proc errexpect-catch {code} {
+ global errorInfo errorCode
+ set rc [catch {
+ uplevel 1 $code
+ } rv]
+ debug "ERREXPECT CATCH |$rc|$rv|$errorCode|$errorInfo|"
+ if {$rc==1 && ![string compare YARRG-ERREXPECT [lindex $errorCode 0]]} {
+ return [list 1 $rv [lindex $errorCode 1] $errorInfo]
+ } elseif {$rc==0} {
+ return [list 0 $rv]
+ } else {
+ return -code $rc -errorinfo $errorInfo -errorcode $errorCode $rv
+ }
+}
+
+#---------- argument parsing ----------
+
+proc nextarg {} {
+ global ai argv
+ if {$ai >= [llength $argv]} {
+ badusage "option [lindex $argv [expr {$ai-1}]] needs a value"
+ }
+ set v [lindex $argv $ai]
+ incr ai
+ return $v
+}
+
+set notes_loc {}
+set scraper {./yppedia-ocean-scraper --chart}
+set info_cache _vessel-info-cache
+set info_source rsync.yarrg.chiark.net::yarrg/vessel-info
+set filter_lockown_separate 0
+
+proc parseargs {} {
+ global ai argv
+ global debug scraper
+ set ai 0
+
+ while {[regexp {^\-} [set arg [lindex $argv $ai]]]} {
+ incr ai
+ switch -exact -- $arg {
+ -- { break }
+ --pirate { glset pirate [string totitle [nextarg]] }
+ --ocean { glset ocean [string totitle [nextarg]] }
+ --clipboard-file { load-clipboard-file [nextarg] }
+ --local-html-dir { lappend scraper --local-html-dir=[nextarg] }
+ --notes { glset notes_loc [nextarg] }
+ --vessel-info-source { glset info_source [nextarg] }
+ --filter-separate-lock-owner { glset filter_lockown_separate 1 }
+ --debug { incr debug }
+ default { badusage "unknown option $arg" }
+ }
+ }
+ set argv [lrange $argv $ai end]
+ if {[llength $argv]} { badusage "non-option args not allowed" }
+}
+
+proc have-notes {} {
+ global notes_loc
+ return [string length $notes_loc]
+}
+
+proc argdefaults {} {
+ global ocean notes_loc pirate scraper
+ if {![info exists ocean] ||
+ (![info exists pirate] && [string length $notes_loc])} {
+ set cmd {./yarrg --find-window-only --quiet}
+ if {[info exists ocean]} { lappend cmd --ocean $ocean }
+ if {[info exists pirate]} { lappend cmd --pirate $pirate }
+ manyset [split [eval exec $cmd] " "] ocean pirate
+ if {![llength $ocean] || ![llength $pirate]} {
+ error "$ocean $pirate ?"
+ }
+ }
+ if {![info exists pirate]} {
+ set pirate {}
+ glset filter_lockown_separate 1
+ }
+ if {![have-notes]} {
+ glset filter_lockown_separate 1
+ }
+
+ lappend scraper $ocean
+}
+
+
+#---------- loading and parsing the vessel notes ----------
+
+proc load-notes {} {
+ global notes_loc notes_data
+ if {[regexp {^\w+\:} $notes_loc]} {
+ update
+ debug "FETCHING NOTES $notes_loc"
+ set req [::http::geturl $notes_loc]
+ switch -glob [::http::status $req].[::http::ncode $req] {
+ ok.200 { }
+ ok.* { error "retrieving vessel-notes: [::http::code $req]" }
+ * { error "Retrieving vessel-notes: [::http::error $req]" }
+ }
+ set newdata [::http::data $req]
+ ::http::cleanup $req
+ } else {
+ debug "READING NOTES $notes_loc"
+ set vn [open $notes_loc]
+ set newdata [read $vn]
+ close $vn
+ }
+ set notes_data $newdata
+}
+
+proc parse-notes {} {
+ global notes_data notes
+ catch { unset notes }
+
+ set lno 0
+ foreach l [split $notes_data "\n"] {
+ incr lno
+ errexpect-setline $lno $l
+ set l [string trim $l]
+ if {![string length $l]} continue
+ if {[regexp {^\#} $l]} continue
+ if {![regexp -expanded \
+ {^ (\d+) (?: \s+([^=]*?) )? \s* =
+ (?: \s* (\S+)
+ (?: \s+ (\S+) )?)? $} \
+ $l dummy vid vname owner note]} {
+ errexpect-error "badly formatted"
+ }
+ set vname [string trim $vname]
+ if {[info exists notes($vid)]} {
+ errexpect-error "duplicate vesselid $vid"
+ }
+ set notes($vid) [list $lno $vname $owner $note]
+ }
+}
+
+proc note-info {lno vid name island description} {
+ global note_infos
+ lappend note_infos [list $lno $vid $name $island $description]
+}
+
+proc display-note-infos {} {
+ global note_infos note_missings notes
+
+ set nmissing [llength $note_missings]
+ debug "display-note-infos $nmissing [array size notes]"
+
+ if {[llength $note_infos]} {
+ set tiny "[llength $note_infos] warning(s)"
+ } elseif {$nmissing && [array size notes]} {
+ set tiny "$nmissing missing"
+ } else {
+ return
+ }
+
+ set infodata {}
+
+ foreach info $note_infos {
+ manyset $info lno vid name island description
+ append infodata "vessel"
+ append infodata " $vid"
+ if {[string length $name]} { append infodata " $name" }
+ if {[string length $island]} { append infodata " ($island)" }
+ append infodata ": " $description "\n"
+ }
+
+ if {$nmissing} {
+ if {[string length $infodata]} { append infodata "\n" }
+ append infodata "$nmissing vessel(s) not mentioned in notes:\n"
+ set last_island {}
+ foreach info [lsort $note_missings] {
+ manyset $info island name vid
+ if {[string compare $island $last_island]} {
+ append infodata "# $island:\n"
+ set last_island $island
+ }
+ append infodata [format "%-9d %-29s =\n" $vid $name]
+ }
+ }
+
+ parser-control-failed-core .cp.ctrl.notes notes \
+ white blue 0 \
+ $tiny \
+ "[llength $note_infos] warning(s);\
+ $nmissing vessel(s) missing" \
+ "Full description of warnings and missing vessels:" \
+ $infodata
+}
+
+#---------- vessel info and icons ----------
+
+proc info-cache-update {} {
+ global info_source info_cache
+ file mkdir $info_cache
+ exec sh -c "cp -u icons/* $info_cache/."
+
+ if {[string length $info_source]} {
+ set cmdl [list \
+ rsync -udLKtOzm \
+ --exclude=*~ --exclude=*.bak --exclude=.* --exclude=*.tmp \
+ $info_source/ $info_cache 2>@ stderr]
+ debug "INFO-CACHE $cmdl"
+ eval exec $cmdl
+ }
+
+ set f [open $info_cache/vessel-info]
+ glset vessel_size_data [read $f]
+ close $f
+}
+
+proc vesselinfo-init {} {
+ global vc_game2code vc_code2abbrev vc_code2full vc_codes
+
+ global vessel_size_data
+ manyset $vessel_size_data sizeinfos subclassinfos
+
+ set vc_codes {}
+ foreach {game code abbrev full} $sizeinfos {
+ if {![regexp {^[a-z][a-z]$} $code code]} { error "bad code" }
+ if {![regexp {^[a-z][a-z]$} $abbrev abbrev]} { error "bad abbrev" }
+ lappend vc_codes $code
+ set vc_game2code($game) $code
+ set vc_code2abbrev($code) $abbrev
+ set vc_code2full($code) $full
+ load-icon $abbrev
+ }
+
+ global vsc_code2report
+ global vsc_game2code
+ set vsc_game2code(null) {}
+ set vsc_code2report() Ordinary
+ set vsc_code2report(!) "(Special/L.E.)"
+ foreach {game code full} $subclassinfos {
+ if {![regexp {^[A-Z]$} $code code]} { error "bad code" }
+ set vsc_game2code($game) $code
+ set vsc_code2report($code) $full
+ }
+
+ load-icon atsea
+ set owners {ours dot query}
+ foreach b $owners { load-icon $b }
+ foreach a {battle borrow dot} {
+ load-icon $a
+ foreach b $owners { load-icon-combine $a $b }
+ }
+}
+
+proc load-icon {icon} {
+ global info_cache
+ image create bitmap icon/$icon -file $info_cache/$icon.xbm
+}
+
+proc load-icon-combine {args} {
+ global info_cache
+ set cmd {}
+ set delim "pnmcat -lr "
+ foreach icon $args {
+ append cmd $delim " <(xbmtopbm $info_cache/$icon.xbm)"
+ set delim " <(pbmmake -white 1 1)"
+ }
+ append cmd " | pbmtoxbm"
+ debug "load-icon-combine $cmd"
+ image create bitmap icon/[join $args +] -data [exec bash -c $cmd]
+}
+
+#---------- vessel properties ----------
+
+proc code-lockown2icon {lockown} {
+ manyset [split $lockown ""] lock notown
+ set l "
+ [lindex {battle borrow dot} $lock]
+ [lindex {ours dot query {} {} dot} $notown]
+ "
+ if {[llength $l]} { return icon/[join $l +] } { return {} }
+}
+
+proc canvas-horiz-stack {xvar xoff y bind type args} {
+ upvar 1 $xvar x
+ upvar 1 canvas canvas
+ set id [eval $canvas create $type [expr {$x+$xoff}] $y $args]
+ set bbox [$canvas bbox $id]
+# debug "CANVAS-HORIZ-STACK $type $x $xoff $id $bbox [list $args]"
+ set x [lindex $bbox 2]
+ $canvas bind $id <ButtonPress> $bind
+ return $id
+}
+
+proc code2canvas {code canvas x yvar qty qtylen bind} {
+ global vc_code2abbrev
+ upvar 1 $yvar y
+
+ manyset [split $code _] inport size subclass lockown xabbrev
+
+ set stackx $x
+ incr stackx 2
+ set imy [expr {$y+2}]
+
+ if {!$inport} { incr qtylen -1 }
+ if {$qtylen<=0} { set qtylen {} }
+ set qty [format "%${qtylen}s" $qty]
+
+ set qtyid [canvas-horiz-stack stackx 0 $y $bind \
+ text -anchor nw -font fixed -text $qty]
+
+ if {!$inport} {
+ canvas-horiz-stack stackx 0 $imy $bind \
+ image -anchor nw -image icon/atsea
+ incr stackx
+ }
+
+ upvar #0 vc_code2abbrev($size) vcabb
+ if {![info exists vcabb]} {
+ set vcabb vc-$size
+ image create bitmap icon/$vcabb -data \
+ [exec pbmtext -builtin fixed $size | pnminvert | pnmcrop >t.pnm]
+ }
+ canvas-horiz-stack stackx -1 $imy $bind \
+ image -anchor nw -image icon/$vcabb
+
+ if {[string length $subclass]} {
+ canvas-horiz-stack stackx 0 $y $bind \
+ text -anchor nw -font fixed -text \
+ $subclass
+ }
+
+ incr stackx
+ set lockownicon [code-lockown2icon $lockown]
+ if {[string length $lockownicon]} {
+ canvas-horiz-stack stackx 0 $imy $bind \
+ image -anchor nw -image $lockownicon
+ incr stackx
+ }
+
+ if {[string length $xabbrev]} {
+ canvas-horiz-stack stackx 0 $y $bind \
+ text -anchor nw -font fixed -text \
+ $xabbrev
+ }
+
+ set bbox [$canvas bbox $qtyid]
+ set ny [lindex $bbox 3]
+ set bid [$canvas create rectangle \
+ $x $y $stackx $ny \
+ -fill white]
+
+ set y $ny
+ $canvas lower $bid $qtyid
+
+ $canvas bind $bid <ButtonPress> $bind
+}
+
+proc show-report-decode {code} {
+ global vc_code2full
+
+ smash-prepare
+
+ manyset [split $code _] inport sizecode subclass lockown xabbrev
+ manyset [split $lockown ""] lock notown
+
+ report-set inport [lindex {{At Sea} {In port}} $inport]
+
+ upvar #0 vc_code2full($sizecode) sizefull
+ upvar #0 smash_sizeinexact($sizecode) sizeinexact
+ set size_report $sizefull
+ if {[info exists sizeinexact]} { set size_report "($sizefull+)" }
+ report-set size $size_report
+
+ global smash_subclass
+ if {$smash_subclass >= 2} {
+ report-set subclass "(Any class)"
+ } elseif {[
+ upvar #0 vsc_code2report($subclass) subclass_report
+ info exists subclass_report
+ ]} {
+ report-set subclass $subclass_report
+ } else {
+ report-set subclass "Class \"$subclass\""
+ }
+
+ report-set lock [lindex {
+ {Battle ready} {Unlocked} {Locked}
+ {(All lock states)} {(Not battle ready)}
+ } $lock]
+
+ if {[have-notes]} {
+ switch -exact $notown {
+ 0 { report-set own "Yours" }
+ 1 { report-set own "Other pirate's" }
+ 2 { report-set own "Owner unknown" }
+ 3 { report-set own "(All ownerships)" }
+ 4 - 5 { report-set own "(Yours/unknown)" }
+ default { report-set own "?? $notown" }
+ }
+ }
+
+ global smash_xabbrev_map
+ if {![have-notes]} {
+ } elseif {[llength $smash_xabbrev_map]} {
+ if {[string length $xabbrev]} {
+ report-set xabbrev "(Flags: $xabbrev)"
+ } else {
+ report-set xabbrev "(No flags)"
+ }
+ } else {
+ if {[string length $xabbrev]} {
+ report-set xabbrev "Notes flags: $xabbrev"
+ } else {
+ report-set xabbrev "No flags in notes"
+ }
+ }
+}
+
+#---------- common to smashing and filtering ----------
+
+proc make-control {parent ctrl label ekind} {
+ debug "MAKE-CONTROL [list $parent $ctrl $label $ekind]"
+ label $parent.lab_$ctrl -text $label -justify left
+ $ekind $parent.$ctrl
+ manyset [grid size $parent] dummy row
+ incr row
+ grid configure $parent.lab_$ctrl -row $row -column 0 -sticky nw -pady 4
+ grid configure $parent.$ctrl -row $row -column 1 -sticky w -pady 3
+ return $parent.$ctrl
+}
+
+proc begin-control-grid {cw count rows inrow} {
+ if {!$inrow} { set inrow [expr {($count + $rows) / $rows}] }
+ upvar #0 control_grid_properties($cw) props
+ set props [list $rows $inrow]
+ return $cw
+}
+
+proc make-control-grid-elem {cw kind ix ekind args} {
+ upvar #0 control_grid_properties($cw) props
+ manyset $props rows inrow
+
+ set ew $cw.$ix
+
+ debug "MAKE-CONTROL-GRID-ELEM $cw $kind $ix $ekind $rows $inrow $ew"
+
+ eval [list $ekind $ew] $args
+
+ switch -exact $kind {
+ ix {
+ grid configure $ew -sticky sw \
+ -row [expr {$ix / $inrow}] \
+ -column [expr {$ix % $inrow}]
+ }
+ final {
+ grid configure $ew -sticky se \
+ -row [expr {$rows-1}] \
+ -column [expr {$inrow-1}]
+ }
+ default {
+ error "$kind ?"
+ }
+ }
+ return $ew
+}
+
+proc control-tickbox-flip {varsvn values} {
+ upvar #0 $varsvn vars
+ foreach val $values {
+ set vars($val) [expr {!$vars($val)}]
+ }
+ redraw-needed c.-tickbox-flip $varsvn $values
+}
+
+proc populate-control-grid-tickboxes {cw rows inrow varsvn values flipvalues
+ label_kind valvn default_get label_get} {
+ debug "POPULATE-CONTROL-GRID-TICKBOXES $cw $rows $inrow $varsvn\
+ [list $values] $label_kind $valvn"
+
+ upvar #0 $varsvn vars
+ upvar 1 $valvn val
+ set count [llength $values]
+
+ begin-control-grid $cw $count $rows $inrow
+
+ for {set ix 0} {$ix < $count} {incr ix} {
+ set val [lindex $values $ix]
+ set vars($val) [uplevel 1 $default_get]
+ set ew [make-control-grid-elem $cw ix $ix checkbutton \
+ -variable ${varsvn}($val) \
+ -font fixed \
+ -command [list redraw-needed c.-g.-tickbox $cw $val]]
+ $ew configure -$label_kind [uplevel 1 $label_get]
+ switch -exact $label_kind {
+ image { $ew configure -height 16 }
+ }
+ }
+ [make-control-grid-elem $cw final invert button] \
+ configure \
+ -text flip -command [list control-tickbox-flip $varsvn $flipvalues] \
+ -padx 0 -pady 0
+}
+
+#---------- smashing ----------
+
+proc smash-code {code} {
+ manyset [split $code _] inport size subclass lockown xabbrev
+
+ upvar #0 smash_sizemap($size) smsize
+
+ global smash_subclass
+ if {$smash_subclass > 1} {
+ set subclass {}
+ } elseif {$smash_subclass && [string length $subclass]} {
+ set subclass !
+ }
+
+ global smash_owner
+ switch $smash_owner {
+ 0 { }
+ 1 { regsub {[12]$} $lockown 5 lockown }
+ 2 {
+ if {[regexp {^0.} $lockown]} {
+ # battle ready / all lock states
+ set lockown 03
+ } elseif {[regexp {^.0} $lockown]} {
+ # not battle ready / yours
+ set lockown 40
+ } else {
+ # state (not battle ready) / not known to be yours
+ regsub {.$} $lockown 4 lockown
+ }
+ }
+ 3 { regsub {.$} $lockown {3} lockown }
+ 4 { set lockown 33 }
+ }
+
+ global smash_xabbrev_map
+ set xabbrev [string map $smash_xabbrev_map $xabbrev]
+
+ return [join [list $inport $smsize $subclass $lockown $xabbrev] _]
+}
+
+proc smash-prepare {} {
+ global vc_codes smash_sizemap smash_size smash_sizeinexact
+ set mapto {}
+ catch { unset smash_sizeplus }
+ foreach size $vc_codes {
+ if {!$smash_size($size)} {
+ set mapto $size
+ } else {
+ set smash_sizeinexact($mapto) 1
+ }
+ set smash_sizemap($size) $mapto
+ }
+
+ global smash_xabbrev_a smash_xabbrev_b smash_xabbrev_map
+ set smash_xabbrev_map {}
+ foreach a [split $smash_xabbrev_a ""] b [split $smash_xabbrev_b ""] {
+ if {![string length $a]} continue
+ lappend smash_xabbrev_map $a $b
+ }
+ debug "SMASH-PREPAE xabbrev_map=[list $smash_xabbrev_map]"
+}
+
+proc make-smasher {sma label ekind} {
+ return [make-control .smash $sma $label $ekind]
+}
+
+proc make-radio-smasher {sma label variable descs rows inrow} {
+ set w [make-smasher $sma $label frame]
+ begin-control-grid $w [llength $descs] $rows $inrow
+ for {set i 0} {$i < [llength $descs]} {incr i} {
+ make-control-grid-elem $w ix $i \
+ radiobutton \
+ -variable $variable -value $i -command redraw-needed \
+ -text [lindex $descs $i]
+ }
+}
+
+proc make-smashers {} {
+ global vc_codes vc_code2abbrev
+ set cw [make-smasher size "Size\n round\n down" frame]
+ populate-control-grid-tickboxes $cw 2 0 smash_size \
+ $vc_codes [lrange $vc_codes 1 end] \
+ image val { expr 0 } { expr {"icon/$vc_code2abbrev($val)"} }
+ $cw.0 configure -state disabled
+
+ glset smash_subclass 0
+ make-radio-smasher subclass Class smash_subclass \
+ {Show Normal/LE Hide} 1 0
+
+ glset smash_owner [expr {[have-notes] ? 0 : 3}]
+ make-radio-smasher owner Owner smash_owner \
+ {Show Yours? {For you} Lock Hide} 2 3
+
+ set cw [make-smasher xabbrev "Flags" frame]
+ foreach ix {1 3} ab {a b} width {14 12} {
+ set vn smash_xabbrev_$ab
+ global $vn
+ set $vn {}
+ entry $cw.$ix -textvariable $vn -width $width
+ trace add variable $vn write [list redraw-needed $vn]
+ }
+ set ix 0
+ foreach str {y/ / /d} { label $cw.$ix -text $str; incr ix 2 }
+ eval pack [lsort [winfo children $cw]] -side left
+}
+
+#---------- filtering ----------
+
+set filters {}
+
+proc filter-values/size {} { global vc_codes; return $vc_codes }
+proc filter-icon/size {code} {
+ upvar #0 vc_code2abbrev($code) abb
+ return icon/$abb
+}
+proc filter-default/size {code} { return 1 }
+proc filter-says-yes/size {codel} {
+ set sizecode [lindex $codel 1]
+ upvar #0 filter_size($sizecode) yes
+ return $yes
+}
+
+proc filter-values/lock {} { return {0 1 2} }
+proc filter-icon/lock {lock} { return [code-lockown2icon ${lock}3] }
+proc filter-default/lock {lock} { return 1 }
+proc filter-says-yes/lock {codel} {
+ regexp {^.} [lindex $codel 3] lock
+ upvar #0 filter_lock($lock) yes
+ debug "FILTER-SAYS-YES/LOCK $codel $lock $yes"
+ return $yes
+}
+
+proc filter-values/own {} { return {0 1 2} }
+proc filter-icon/own {own} { return [code-lockown2icon 3${own}] }
+proc filter-default/own {own} { return 1 }
+proc filter-says-yes/own {codel} {
+ regexp {.$} [lindex $codel 3] own
+ upvar #0 filter_own($own) yes
+ debug "FILTER-SAYS-YES/OWN $codel $own $yes"
+ return $yes
+}
+
+proc filter-values/lockown {} {
+ foreach lv {0 1 2} {
+ foreach ov {0 1 2} {
+ lappend vals "$lv$ov"
+ }
+ }
+ return $vals
+}
+proc filter-icon/lockown {lockown} { return [code-lockown2icon $lockown] }
+proc filter-default/lockown {lockown} {
+ return [regexp {^[01]|^2[^1]} $lockown]
+}
+proc filter-says-yes/lockown {codel} {
+ set lockown [lindex $codel 3]
+ upvar #0 filter_lockown($lockown) yes
+ debug "FILTER-SAYS-YES/LOCKOWN $codel $lockown $yes"
+ return $yes
+}
+
+proc filter-validate/xabbre {re} {
+ if {[catch {
+ regexp -- $re {}
+ } emsg]} {
+ regsub {^.*:\s*} $emsg {} emsg
+ regsub {^.*(.{30})$} $emsg {\1} emsg
+ return $emsg
+ }
+ return {}
+}
+proc filter-says-yes/xabbre {codel} {
+ global filter_xabbre
+ set xabbrev [lindex $codel 4]
+ return [regexp -- $filter_xabbre $xabbrev]
+}
+
+proc make-tickbox-filter {fil label rows inrow} {
+ set values [filter-values/$fil]
+
+ if {![catch { info args filter-icon/$fil }]} {
+ set label_get { filter-icon/$fil $val }
+ set label_kind image
+ } else {
+ set label_get { filter-map/$fil $val }
+ set label_kind text