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}
112 while {[regexp {^\-} [set arg [lindex $argv $ai]]]} {
114 switch -exact -- $arg {
116 --pirate { glset pirate [string totitle [nextarg]] }
117 --ocean { glset ocean [string totitle [nextarg]] }
118 --clipboard-file { load-clipboard-file [nextarg] }
119 --local-html-dir { lappend scraper --local-html-dir=[nextarg] }
120 --notes { glset notes_loc [nextarg] }
121 --debug { incr debug }
122 default { badusage "unknown option $arg" }
125 set argv [lrange $argv $ai end]
126 if {[llength $argv]} { badusage "non-option args not allowed" }
129 proc argdefaults {} {
130 global ocean notes_loc pirate scraper
131 if {![info exists ocean] || ![info exists pirate]} {
132 set cmd {./yarrg --find-window-only --quiet}
133 if {[info exists ocean]} { lappend cmd --ocean $ocean }
134 if {[info exists pirate]} { lappend cmd --pirate $pirate }
135 manyset [split [eval exec $cmd] " "] ocean pirate
137 lappend scraper $ocean
141 #---------- loading and parsing the vessel notes ----------
144 global notes_loc notes_data
145 if {[regexp {^\w+\:} $notes_loc]} {
147 debug "FETCHING NOTES $notes_loc"
148 set req [::http::geturl $notes_loc]
149 switch -glob [::http::status $req].[::http::ncode $req] {
151 ok.* { error "retrieving vessel-notes: [::http::code $req]" }
152 * { error "Retrieving vessel-notes: [::http::error $req]" }
154 set newdata [::http::data $req]
157 debug "READING NOTES $notes_loc"
158 set vn [open $notes_loc]
159 set newdata [read $vn]
162 set notes_data $newdata
165 proc parse-notes {} {
166 global notes_data notes
167 catch { unset notes }
170 foreach l [split $notes_data "\n"] {
172 errexpect-setline $lno $l
173 set l [string trim $l]
174 if {![string length $l]} continue
175 if {[regexp {^\#} $l]} continue
176 if {![regexp -expanded \
177 {^ (\d+) (?: \s+([^=]*?) )? \s* =
179 (?: \s+ (\S+) )?)? $} \
180 $l dummy vid vname owner note]} {
181 errexpect-error "badly formatted"
183 set vname [string trim $vname]
184 if {[info exists notes($vid)]} {
185 errexpect-error "duplicate vesselid $vid"
187 set notes($vid) [list $lno $vname $owner $note]
191 proc note-info {lno vid name island description} {
193 lappend note_infos [list $lno $vid $name $island $description]
196 proc display-note-infos {} {
197 global note_infos note_missings notes
199 set nmissing [llength $note_missings]
200 debug "display-note-infos $nmissing [array size notes]"
202 if {[llength $note_infos]} {
203 set tiny "[llength $note_infos] warnings"
204 } elseif {$nmissing && [array size notes]} {
205 set tiny "$nmissing missing"
212 foreach info $note_infos {
213 manyset $info lno vid name island description
214 append infodata "vessel"
215 append infodata " $vid"
216 if {[string length $name]} { append infodata " $name" }
217 if {[string length $island]} { append infodata " ($island)" }
218 append infodata ": " $description "\n"
222 if {[string length $infodata]} { append infodata "\n" }
223 append infodata "$nmissing vessel(s) not mentioned in notes:\n"
225 foreach info [lsort $note_missings] {
226 manyset $info island name vid
227 if {[string compare $island $last_island]} {
228 append infodata "# $island:\n"
229 set last_island $island
231 append infodata [format "%-9d %-29s =\n" $vid $name]
235 parser-control-failed-core .ctrl.notes notes \
238 "[llength $note_infos] warning(s);\
239 $nmissing vessel(s) missing" \
240 "Full description of warnings and missing vessels:" \
244 #---------- vessel properties ----------
246 proc vesselclasses-init {} {
247 global vc_game2code vc_code2abbrev vc_code2full
248 foreach {game code abbrev full} {
252 longship dm ls Longship
253 baghlah em bg Baghlah
254 merchbrig fm mb {Merchant Brig}
255 warbrig gm wb {War Brig}
257 warfrig im wf {War Frigate}
258 merchgal jm mg {Merchant Galleon}
259 grandfrig km gf {Grand Frigate}
261 set vc_game2code($game) $code
262 set vc_code2abbrev($code) $abbrev
263 set vc_code2full($code) $full
267 proc code2abbrev {code} {
268 global vc_code2abbrev
270 manyset [split $code _] inport class subclass lockown xabbrev
271 manyset [split $lockown ""] lock notown
274 append abbrev [lindex {? {}} $inport]
275 append abbrev $vc_code2abbrev($class)
276 append abbrev $subclass
277 append abbrev [lindex {* + -} $lock]
278 append abbrev [lindex {= - ?} [regsub {\D} $notown 2]]
279 append abbrev $xabbrev
281 debug "CODE2ABBREV $code $abbrev"
285 proc show-report-decode {code} {
288 manyset [split $code _] inport classcode subclass lockown xabbrev
289 manyset [split $lockown ""] lock notown
291 report-set inport [lindex {{At Sea} {In port}} $inport]
292 report-set class $vc_code2full($classcode)
294 switch -exact $subclass {
295 {} { report-set subclass {Ordinary} }
296 F { report-set subclass {"Frost class"} }
297 default { report-set subclass "Subclass \"$subclass\"" }
300 report-set lock [lindex {
301 {Battle ready} {Unlocked} {Locked}
304 switch -exact $notown {
305 0 { report-set own "Yours" }
306 1 { report-set own "Other pirate's" }
307 U { report-set own "Owner not known" }
308 M { report-set own "Missing from notes" }
309 default { report-set own "?? $notown" }
312 if {[string length $xabbrev]} {
313 report-set xabbrev "Notes flags: $xabbrev"
315 report-set xabbrev "No flags in notes"
319 #---------- loading and parsing the clipboard (vessel locations) ----------
322 global pirate notes_used note_missings newnotes
326 lappend codel [errexpect-arrayget-boolean vi inPort]
328 set gameclass [errexpect-arrayget vi vesselClass]
329 upvar #0 vc_game2code($gameclass) class
330 if {![info exists class]} { errexpect-error "unexpected vesselClass"}
333 set subclass [errexpect-arrayget vi vesselSubclass]
334 switch -exact $subclass {
335 null { lappend codel {} }
336 icy { lappend codel F }
337 default { lappend codel ($subclass) }
340 switch -exact [errexpect-arrayget vi isLocked]/[ \
341 errexpect-arrayget vi isBattleReady] {
342 true/false { set lock 2 }
343 false/false { set lock 1 }
344 false/true { set lock 0 }
345 default { errexpect-error "unexpected isLocked/isBattleReady" }
348 set vid [errexpect-arrayget vi vesselId]
349 upvar #0 notes($vid) note
350 set realname [errexpect-arrayget vi vesselName]
351 set island [errexpect-arrayget vi islandName]
355 if {[info exists note]} {
356 manyset $note lno notename owner xabbrev
357 if {[string compare -nocase $realname $notename]} {
358 note-info $lno $vid $realname $island \
359 "notes say name is $notename"
361 if {[string length $owner]} {
362 if {![string compare $owner $pirate]} {
370 append abbrev $xabbrev
371 set notes_used($vid) 1
375 lappend note_missings [list $island $realname $vid]
378 lappend codel "$lock$notown" $xabbrev
379 lappend newnotes [list $vid $realname $owner $xabbrev]
380 set kk "$island [join $codel _]"
381 upvar #0 found($kk) k
382 lappend k [list $vid $realname]
384 debug "CODED $kk $vid $realname"
388 proc parse-clipboard {} {
389 global clipboard found notes notes_used newnotes
391 catch { unset found }
392 catch { unset notes_used }
394 glset note_missings {}
398 set itemre { (\w+) = ([^=]*) }
399 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
403 foreach l [split $clipboard "\n"] {
405 errexpect-setline $lno $l
406 if {![string length $l]} continue
409 if {![regexp -expanded $manyitemre $l dummy \
410 thiskey thisval rhs]} {
411 errexpect-error "badly formatted"
413 set vi($thiskey) $thisval
414 if {![string length $rhs]} break
415 regsub {^, } $rhs {} rhs
421 if {[llength $newnotes]} {
422 foreach vid [lsort [array names notes]] {
423 if {![info exists notes_used($vid)]} {
424 manyset $notes($vid) lno notename
425 note-info $lno $vid $notename {} \
426 "vessel in notes no longer found"
432 proc load-clipboard-file {fn} {
434 glset clipboard [read $f]
439 #---------- loading and parsing the chart ----------
443 debug "FETCHING CHART"
444 set chart [eval exec $scraper [list | perl -we {
449 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
450 sub { sprintf "%d %d", @_; },
451 sub { printf "archlabel %d %d %s\n", @_; },
452 sub { printf "island %s %s\n", @_; },
453 sub { printf "league %s %s %s.\n", @_; },
454 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
456 STDOUT->error and die $!;
465 return [expr {$c * $scale}]
468 proc chart-got/archlabel {args} { }
469 proc chart-got/island {x y args} {
470 # debug "ISLE $x $y $args"
471 global canvas isleloc
472 set isleloc($args) [list $x $y]
474 # $canvas create oval \
475 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
476 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
478 $canvas create text [coord $x] [coord $y] \
479 -text $args -anchor s
481 proc chart-got/league {x1 y1 x2 y2 kind} {
482 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
484 set l [$canvas create line \
485 [coord $x1] [coord $y1] \
486 [coord $x2] [coord $y2]]
487 if {![string compare $kind .]} {
488 $canvas itemconfigure $l -dash .
493 global chart found isleloc canvas
497 foreach l [split $chart "\n"] {
498 # debug "CHART-GOT $l"
499 set proc [lindex $l 0]
500 eval chart-got/$proc [lrange $l 1 end]
503 set lastislandname {}
504 foreach key [lsort [array names found]] {
505 set c [llength $found($key)]
506 # debug "SHOWING $key $c"
507 regexp {^(.*) (\S+)$} $key dummy islandname code
509 set abbrev [code2abbrev $code]
511 if {[string compare $lastislandname $islandname]} {
512 manyset $isleloc($islandname) x y
515 set lastislandname $islandname
519 regsub -all {[0-9]} $text {} text
521 set text [format "%2d%s" $c $text]
523 set text [format " %s" $text]
525 set id [$canvas create text $x $y \
526 -anchor nw -font fixed \
528 set bbox [$canvas bbox $id]
529 set bid [eval $canvas create rectangle $bbox -fill white]
530 $canvas lower $bid $id
531 $canvas bind $id <ButtonPress> [list show-report $islandname $code]
532 $canvas bind $bid <ButtonPress> [list show-report $islandname $code]
533 manyset $bbox dummy dummy dummy y
537 panner::updatecanvas-bbox .ctrl.pan
541 #---------- parser error reporting ----------
543 proc parser-control-create {w base invokebuttontext etl_title} {
545 button $w.do -text $invokebuttontext -command invoke_$base
547 frame $w.resframe -width 120 -height 32
548 button $w.resframe.res -text {} -anchor nw \
549 -padx 1 -pady 1 -borderwidth 0 -justify left
550 glset deffont_$base [$w.resframe.res cget -font]
551 place $w.resframe.res -relx 0.5 -y 0 -anchor n
554 pack $w.resframe -side top -expand y -fill both
559 wm title $eb "where-vessels - $etl_title"
561 label $eb.title -text $etl_title
562 pack $eb.title -side top
564 button $eb.close -text Close -command [list wm withdraw $eb]
565 pack $eb.close -side bottom
567 frame $eb.emsg -bd 2 -relief groove
568 label $eb.emsg.lab -text "Error:"
569 text $eb.emsg.text -height 1
570 pack $eb.emsg.text -side bottom
571 pack $eb.emsg.lab -side left
573 pack $eb.emsg -side top -pady 2
575 frame $eb.text -bd 2 -relief groove
576 pack $eb.text -side bottom -pady 2
580 text $eb.text.text -width 85 \
581 -xscrollcommand [list $eb.text.xscroll set] \
582 -yscrollcommand [list $eb.text.yscroll set]
583 $eb.text.text tag configure error \
584 -background red -foreground white
586 scrollbar $eb.text.xscroll -orient horizontal \
587 -command [list $eb.text.text xview]
588 scrollbar $eb.text.yscroll -orient vertical \
589 -command [list $eb.text.text yview]
591 grid configure $eb.text.lab -row 0 -column 0 -sticky w
592 grid configure $eb.text.text -row 1 -column 0
593 grid configure $eb.text.yscroll -sticky ns -row 1 -column 1
594 grid configure $eb.text.xscroll -sticky ew -row 2 -column 0
597 proc parser-control-ok-core {w base background show} {
598 debug "parser-control-ok-core $w $base $background $show"
599 upvar #0 deffont_$base deffont
600 $w.resframe.res configure \
601 -background $background -disabledforeground black -font $deffont \
602 -state disabled -command {} \
605 proc parser-control-ok {w base show} {
606 parser-control-ok-core $w $base green $show
608 proc parser-control-none {w base show} {
609 parser-control-ok-core $w $base blue $show
611 proc parser-control-failed-core {w base foreground background smallfont
612 tiny summary fulldesc fulldata} {
613 debug "parser-control-failed-core $w $base $summary $fulldesc"
614 upvar #0 deffont_$base deffont
617 $eb.emsg.text delete 0.0 end
618 $eb.emsg.text insert end $summary
620 $eb.text.lab configure -text $fulldesc
621 $eb.text.text delete 0.0 end
622 $eb.text.text insert end $fulldata
624 regsub -all {.{18}} $tiny "&\n" ewrap
632 $w.resframe.res configure \
633 -background $background -foreground $foreground -font $font \
634 -state normal -command [list wm deiconify $eb] \
638 proc parser-control-failed-expected {w base emsg lno ei fulldesc newdata} {
641 set line [lindex [split $ei "\n"] 0]
642 debug "parser-control-failed-expected: $w $base: $lno: $emsg\n $line"
644 parser-control-failed-core $w $base \
646 "err: [string trim $emsg]: \"$line\"" \
647 "at line $lno: $emsg" \
650 $eb.text.text tag add error $lno.0 $lno.end
651 $eb.text.text see $lno.0
653 proc parser-control-failed-unexpected {w base emsg ei} {
655 parser-control-failed-core $w $base \
657 $emsg $emsg "Details and stack trace:" $ei
660 proc reparse {base varname old fulldesc okshow noneshow parse ok} {
661 upvar #0 $varname var
662 manyset [errexpect-catch {
664 if {[string length [string trim $var]]} {
665 parser-control-ok .ctrl.$base $base $okshow
667 parser-control-none .ctrl.$base $base $noneshow
669 }] failed emsg lno ei
671 parser-control-failed-expected .ctrl.$base $base \
672 $emsg $lno $ei $fulldesc $var
680 #---------- main user interface ----------
682 proc widgets-setup {} {
683 global canvas debug pirate ocean
685 wm geometry . 1024x480
686 wm title . "where-vessels - $pirate on the $ocean ocean"
690 frame .f -border 1 -relief groove
693 pack $canvas -expand 1 -fill both
694 pack .f -expand 1 -fill both -side left
696 #----- control panel -----
699 pack .ctrl -side left -anchor n
701 debug "BBOX [$canvas bbox all]"
703 panner::canvas-scroll-bbox .f.c
704 panner::create .ctrl.pan .f.c 120 120 $debug
706 pack .ctrl.pan -side top -pady 10 -padx 5
708 pack .ctrl.zoom -side top
710 button .ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2}
711 button .ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2}
712 pack .ctrl.zoom.out .ctrl.zoom.in -side left
714 parser-control-create .ctrl.acquire \
716 "Clipboard parsing error" \
718 pack .ctrl.acquire -side top -pady 2
720 parser-control-create .ctrl.notes \
721 notes "Reload notes" \
722 "Vessel notes loading report" \
724 pack .ctrl.notes -side top -pady 2
726 #----- decoding etc. report -----
729 pack .report -side left -anchor n
731 label .report.island -text { }
733 frame .report.abbrev -background black
734 glset report_abbrev { }
735 entry .report.abbrev.abbrev -state readonly \
736 -textvariable report_abbrev \
737 -borderwidth 0 -relief flat -width 0 \
738 -highlightbackground white \
739 -readonlybackground white -foreground black
740 pack .report.abbrev.abbrev -side left -padx 1 -pady 1
743 label .report.code.lab -text Code:
744 glset report_code { }
745 entry .report.code.code -state readonly -textvariable report_code -width 15
746 pack .report.code.lab .report.code.code -side left
747 frame .report.details -bd 2 -relief groove -padx 2 -pady 2
749 listbox .report.list -height 5
751 pack .report.island .report.abbrev .report.details \
752 .report.list .report.code -side top
753 pack configure .report.details -fill x
755 foreach sw {inport class subclass lock own xabbrev} {
756 label .report.details.$sw -text { }
757 pack .report.details.$sw -side top -anchor w
761 proc report-set {sw val} { .report.details.$sw configure -text $val }
763 proc show-report {islandname code} {
764 .report.island configure -text $islandname
765 glset report_code $code
766 glset report_abbrev [code2abbrev $code]
768 show-report-decode $code
770 set kk "$islandname $code"
771 upvar #0 found($kk) k
773 .report.list delete 0 end
776 manyset $entry vid name
777 .report.list insert end $name
783 set nscale [expr "\$scale $extail"]
784 debug "ZOOM $scale $nscale"
785 if {$nscale < 1 || $nscale > 200} return
790 proc invoke_acquire {} {
791 global clipboard errorInfo
795 set clipboard [clipboard get]
797 parser-control-failed-unexpected .ctrl.acquire acquire \
798 $emsg "fetching clipboard:\n\n$errorInfo"
803 clipboard $old "Clipboard contents:" { acquired ok } { no vessels } {
811 proc invoke_notes {} {
812 global notes_data errorInfo notes_loc
818 parser-control-failed-unexpected .ctrl.notes notes \
819 $emsg "loading $notes_loc:\n\n$errorInfo"
824 notes_data $old "Vessel notes:" "loaded ok" { no notes } {
833 #---------- main program ----------
839 httpclientsetup where-vessels
844 if {[catch { parse-clipboard } emsg]} {
845 puts stderr "$emsg\n$errorInfo"
848 after idle invoke_notes