+#---------- vessel properties ----------
+
+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_class_data [read $f]
+ close $f
+}
+
+proc vesselclasses-init {} {
+ global vc_game2code vc_code2abbrev vc_code2full vc_codes
+
+ global vessel_class_data
+ manyset $vessel_class_data classinfos subclassinfos
+
+ set vc_codes {}
+ foreach {game code abbrev full} $classinfos {
+ 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
+ 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
+ foreach a {battle borrow dot} {
+ foreach b {ours dot query} {
+ load-icon-combine $a $b
+ }
+ }
+}
+
+proc load-icon {icon} {
+ image create bitmap icon/$icon -file icons/$icon.xbm
+}
+
+proc load-icon-combine {args} {
+ set cmd {}
+ set delim "pnmcat -lr "
+ foreach icon $args {
+ append cmd $delim " <(xbmtopbm icons/$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]
+}
+
+proc code-lockown2icon {lockown} {
+ manyset [split $lockown ""] lock notown
+ return icon/[
+ lindex {battle borrow dot} $lock
+ ]+[
+ lindex {ours dot query} $notown
+ ]
+}
+
+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]
+ 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 class 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($class) vcabb
+ if {![info exists vcabb]} {
+ set vcabb vc-$class
+ image create bitmap icon/$vcabb -data \
+ [exec pbmtext -builtin fixed $class | 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
+ canvas-horiz-stack stackx 0 $imy $bind \
+ image -anchor nw -image [code-lockown2icon $lockown]
+ 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
+
+ manyset [split $code _] inport classcode subclass lockown xabbrev
+ manyset [split $lockown ""] lock notown
+
+ report-set inport [lindex {{At Sea} {In port}} $inport]
+ report-set class $vc_code2full($classcode)
+
+ upvar #0 vsc_code2report($subclass) subclass_report
+ if {[info exists subclass_report]} {
+ report-set subclass $subclass_report
+ } else {
+ report-set subclass "Subclass \"$subclass\""
+ }
+
+ report-set lock [lindex {
+ {Battle ready} {Unlocked} {Locked}
+ } $lock]
+
+ switch -exact $notown {
+ 0 { report-set own "Yours" }
+ 1 { report-set own "Other pirate's" }
+ 2 { report-set own "Owner unknown" }
+ default { report-set own "?? $notown" }
+ }
+
+ if {[string length $xabbrev]} {
+ report-set xabbrev "Notes flags: $xabbrev"
+ } else {
+ report-set xabbrev "No flags in notes"
+ }
+}
+
+#---------- 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/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
+ 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 filter-tickbox-flip {fil} {
+ upvar #0 filter_$fil vars
+ set values [filter-values/$fil]
+ foreach val $values {
+ set vars($val) [expr {!$vars($val)}]
+ }
+ redraw-needed
+}
+
+proc make-tickbox-filter {fil label rows inrow} {
+ upvar #0 filter_$fil vars
+ set fw [make-filter tickbox $fil $label frame]
+ set values [filter-values/$fil]
+ set nvalues [llength $values]
+ if {!$inrow} {
+ set inrow [expr {($nvalues + $rows) / $rows}]
+ }
+ set noicons [catch { info args filter-icon/$fil }]
+ for {set ix 0} {$ix < $nvalues} {incr ix} {
+ set val [lindex $values $ix]
+ set vars($val) [filter-default/$fil $val]
+ checkbutton $fw.$ix -variable filter_${fil}($val) \
+ -font fixed -command redraw-needed
+ if {!$noicons} {
+ $fw.$ix configure -image [filter-icon/$fil $val] -height 16
+ } else {
+ $fw.$ix configure -text [filter-map/$fil $val]
+ }
+ grid configure $fw.$ix -sticky sw \
+ -row [expr {$ix / $inrow}] \
+ -column [expr {$ix % $inrow}]
+ }
+ button $fw.invert -text flip -command [list filter-tickbox-flip $fil] \
+ -padx 0 -pady 0
+ grid configure $fw.invert -sticky se \
+ -row [expr {$rows-1}] \
+ -column [expr {$inrow-1}]
+}
+
+proc entry-filter-changed {fw fil n1 n2 op} {
+ global errorInfo
+ upvar #0 filter_$fil realvar
+ upvar #0 filterentered_$fil entryvar
+ global def_background
+ debug "entry-filter-changed $fw $fil $entryvar"
+ if {[catch {
+ set error [filter-validate/$fil $entryvar]
+ if {[string length $error]} {
+ $fw.error configure -text $error -foreground white -background red
+ } else {
+ $fw.error configure -text { } -background $def_background
+ set realvar $entryvar
+ redraw-needed
+ }
+ } emsg]} {
+ puts stderr "FILTER CHECK ERROR $emsg $errorInfo"
+ }
+}
+
+proc make-entry-filter {fil label def} {
+ global filterentered_$fil
+ upvar #0 filter_$fil realvar
+ set realvar $def
+ set fw [make-filter entry $fil $label frame]
+ entry $fw.entry -textvariable filterentered_$fil
+ label $fw.error
+ glset def_background [$fw.error cget -background]
+ trace add variable filterentered_$fil write \
+ [list entry-filter-changed $fw $fil]
+ pack $fw.entry $fw.error -side top -anchor w
+}
+
+proc make-filter {kind fil label ekind} {
+ global filters
+ label .filter.lab_$fil -text $label -justify left
+ $ekind .filter.$fil
+ lappend filters $fil
+ set nfilters [llength $filters]
+ grid configure .filter.lab_$fil -row $nfilters -column 0 -sticky nw -pady 4
+ grid configure .filter.$fil -row $nfilters -column 1 -sticky w -pady 3
+ return .filter.$fil
+}
+
+proc make-filters {} {
+ make-tickbox-filter size Size 2 0
+ make-tickbox-filter lockown "Lock/\nowner" 2 6
+ make-entry-filter xabbre "Flags\n regexp" {}
+}
+
+proc filterstyle-changed {n1 n2 op} {
+ global filterstyle
+ debug "filterstyle-changed $filterstyle"
+ redraw-needed
+}
+
+proc filters-say-yes {code} {
+ global filters filterstyle
+ debug "filters-say-yes $code"
+ set codel [split $code _]
+ set lockown [lindex $codel 3]
+ switch -exact $filterstyle {
+ 0 { return 1 }
+ 1 { return [filter-default/lockown $lockown] }
+ 2 { return [regexp {^.0} $lockown] }
+ 3 { }
+ default { error $filterstyle }
+ }
+
+ foreach fil $filters {
+ if {![filter-says-yes/$fil $codel]} { return 0 }
+ }
+ return 1
+}
+