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 description} {
193 lappend note_infos [list $lno $vid $name $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 description
214 append infodata "vessel"
215 append infodata " $vid"
216 if {[string length $name]} { append infodata " $name" }
217 append infodata ": " $description "\n"
221 append infodata "$nmissing vessels not mentioned in notes:\n"
223 foreach info [lsort $note_missings] {
224 manyset $info island name vid
225 if {[string compare $island $last_island]} {
226 append infodata "# $island:\n"
227 set last_island $island
229 append infodata [format "%-9d %-29s =\n" $vid $name]
233 parser-control-failed-core .ctrl.notes notes \
236 "[llength $note_infos] warnings;\
237 $nmissing vessels missing" \
238 "Full description of warnings and missing vessels:" \
242 #---------- vessel properties ----------
244 proc vesselclasses-init {} {
245 global vc_game2code vc_code2abbrev vc_abbrev2full
246 foreach {game code abbrev full} {
250 longship dm ls Longship
251 baghlah em bg Baghlah
252 merchbrig fm mb {Merchant Brig}
253 warbrig gm wb {War Brig}
255 warfrig im wf {War Frigate}
256 merchgal jm mg {Merchant Galleon}
257 grandfrig km gf {Grand Frigate}
259 set vc_game2code($game) $code
260 set vc_code2abbrev($code) $abbrev
261 set vc_abbrev2full($abbrev) $full
265 proc code2abbrev {code} {
266 global vc_code2abbrev
268 manyset [split $code _] inport class subclass lockown xabbrev
269 manyset [split $lockown ""] lock own
272 append abbrev [lindex {? {}} $inport]
273 append abbrev $vc_code2abbrev($class)
274 append abbrev $subclass
275 append abbrev [lindex {* + -} $lock]
276 append abbrev [lindex {- = ?} [regsub {\D} $own 2]]
277 append abbrev $xabbrev
279 debug "CODE2ABBREV $code $abbrev"
283 #---------- loading and parsing the clipboard (vessel locations) ----------
286 global pirate notes_used note_missings newnotes
290 lappend codel [errexpect-arrayget-boolean vi inPort]
292 set gameclass [errexpect-arrayget vi vesselClass]
293 upvar #0 vc_game2code($gameclass) class
294 if {![info exists class]} { errexpect-error "unexpected vesselClass"}
297 set subclass [errexpect-arrayget vi vesselSubclass]
298 switch -exact $subclass {
299 null { lappend codel {} }
300 icy { lappend codel F }
301 default { lappend codel ($subclass) }
304 switch -exact [errexpect-arrayget vi isLocked]/[ \
305 errexpect-arrayget vi isBattleReady] {
306 true/false { set lock 2 }
307 false/false { set lock 1 }
308 false/true { set lock 0 }
309 default { errexpect-error "unexpected isLocked/isBattleReady" }
312 set vid [errexpect-arrayget vi vesselId]
313 upvar #0 notes($vid) note
314 set realname [errexpect-arrayget vi vesselName]
315 set island [errexpect-arrayget vi islandName]
319 if {[info exists note]} {
320 manyset $note lno notename owner xabbrev
321 if {[string compare -nocase $realname $notename]} {
322 note-info $lno $vid $realname $island \
323 "notes say name is $notename - perhaps renamed"
325 if {[string length $owner]} {
326 if {![string compare $owner $pirate]} {
334 append abbrev $xabbrev
335 set notes_used($vid) 1
339 lappend note_missings [list $island $realname $vid]
342 lappend codel "$lock$own" $xabbrev
343 lappend newnotes [list $vid $realname $owner $xabbrev]
344 set kk "$island [join $codel _]"
345 upvar #0 count($kk) k
346 if {![info exists k]} { set k 0 }
349 debug "CODED $kk $vid $realname"
353 proc parse-clipboard {} {
354 global clipboard count notes notes_used newnotes
356 catch { unset count }
357 catch { unset notes_used }
359 glset note_missings {}
363 set itemre { (\w+) = ([^=]*) }
364 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
368 foreach l [split $clipboard "\n"] {
370 errexpect-setline $lno $l
371 if {![string length $l]} continue
374 if {![regexp -expanded $manyitemre $l dummy \
375 thiskey thisval rhs]} {
376 errexpect-error "badly formatted"
378 set vi($thiskey) $thisval
379 if {![string length $rhs]} break
380 regsub {^, } $rhs {} rhs
386 if {[llength $newnotes]} {
387 foreach vid [lsort [array names notes]] {
388 if {![info exists notes_used($vid)]} {
389 manyset $notes($vid) lno notename
390 note-info $lno $vid $notename {} \
391 "vessel in notes no longer found"
397 proc load-clipboard-file {fn} {
399 glset clipboard [read $f]
404 #---------- loading and parsing the chart ----------
408 debug "FETCHING CHART"
409 set chart [eval exec $scraper [list | perl -we {
414 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
415 sub { sprintf "%d %d", @_; },
416 sub { printf "archlabel %d %d %s\n", @_; },
417 sub { printf "island %s %s\n", @_; },
418 sub { printf "league %s %s %s.\n", @_; },
419 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
421 STDOUT->error and die $!;
430 return [expr {$c * $scale}]
433 proc chart-got/archlabel {args} { }
434 proc chart-got/island {x y args} {
435 # debug "ISLE $x $y $args"
436 global canvas isleloc
437 set isleloc($args) [list $x $y]
439 # $canvas create oval \
440 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
441 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
443 $canvas create text [coord $x] [coord $y] \
444 -text $args -anchor s
446 proc chart-got/league {x1 y1 x2 y2 kind} {
447 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
449 set l [$canvas create line \
450 [coord $x1] [coord $y1] \
451 [coord $x2] [coord $y2]]
452 if {![string compare $kind .]} {
453 $canvas itemconfigure $l -dash .
458 global chart count isleloc canvas
462 foreach l [split $chart "\n"] {
463 # debug "CHART-GOT $l"
464 set proc [lindex $l 0]
465 eval chart-got/$proc [lrange $l 1 end]
468 set lastislandname {}
469 foreach key [lsort [array names count]] {
471 # debug "SHOWING $key $c"
472 regexp {^(.*) (\S+)$} $key dummy islandname code
474 set abbrev [code2abbrev $code]
476 if {[string compare $lastislandname $islandname]} {
477 manyset $isleloc($islandname) x y
480 set lastislandname $islandname
484 regsub -all {[0-9]} $text {} text
486 set text [format "%2d%s" $c $text]
488 set text [format " %s" $text]
490 set id [$canvas create text $x $y \
491 -anchor nw -font fixed \
493 set bbox [$canvas bbox $id]
494 set bid [eval $canvas create rectangle $bbox -fill white]
495 $canvas lower $bid $id
496 manyset $bbox dummy dummy dummy y
500 panner::updatecanvas-bbox .ctrl.pan
504 #---------- parser error reporting ----------
506 proc parser-control-create {w base invokebuttontext etl_title} {
508 button $w.do -text $invokebuttontext -command invoke_$base
510 frame $w.resframe -width 120 -height 32
511 button $w.resframe.res -text {} -anchor nw \
512 -padx 1 -pady 1 -borderwidth 0 -justify left
513 glset deffont_$base [$w.resframe.res cget -font]
514 place $w.resframe.res -relx 0.5 -y 0 -anchor n
517 pack $w.resframe -side top -expand y -fill both
522 wm title $eb "where-vessels - $etl_title"
524 label $eb.title -text $etl_title
525 pack $eb.title -side top
527 button $eb.close -text Close -command [list wm withdraw $eb]
528 pack $eb.close -side bottom
530 frame $eb.emsg -bd 2 -relief groove
531 label $eb.emsg.lab -text "Error:"
532 text $eb.emsg.text -height 1
533 pack $eb.emsg.text -side bottom
534 pack $eb.emsg.lab -side left
536 pack $eb.emsg -side top -pady 2
538 frame $eb.text -bd 2 -relief groove
539 pack $eb.text -side bottom -pady 2
543 text $eb.text.text -width 85 \
544 -xscrollcommand [list $eb.text.xscroll set] \
545 -yscrollcommand [list $eb.text.yscroll set]
546 $eb.text.text tag configure error \
547 -background red -foreground white
549 scrollbar $eb.text.xscroll -orient horizontal \
550 -command [list $eb.text.text xview]
551 scrollbar $eb.text.yscroll -orient vertical \
552 -command [list $eb.text.text yview]
554 grid configure $eb.text.lab -row 0 -column 0 -sticky w
555 grid configure $eb.text.text -row 1 -column 0
556 grid configure $eb.text.yscroll -sticky ns -row 1 -column 1
557 grid configure $eb.text.xscroll -sticky ew -row 2 -column 0
560 proc parser-control-ok-core {w base background show} {
561 debug "parser-control-ok-core $w $base $background $show"
562 upvar #0 deffont_$base deffont
563 $w.resframe.res configure \
564 -background $background -disabledforeground black -font $deffont \
565 -state disabled -command {} \
568 proc parser-control-ok {w base show} {
569 parser-control-ok-core $w $base green $show
571 proc parser-control-none {w base show} {
572 parser-control-ok-core $w $base blue $show
574 proc parser-control-failed-core {w base foreground background smallfont
575 tiny summary fulldesc fulldata} {
576 debug "parser-control-failed-core $w $base $summary $fulldesc"
577 upvar #0 deffont_$base deffont
580 $eb.emsg.text delete 0.0 end
581 $eb.emsg.text insert end $summary
583 $eb.text.lab configure -text $fulldesc
584 $eb.text.text delete 0.0 end
585 $eb.text.text insert end $fulldata
587 regsub -all {.{18}} $tiny "&\n" ewrap
595 $w.resframe.res configure \
596 -background $background -foreground $foreground -font $font \
597 -state normal -command [list wm deiconify $eb] \
601 proc parser-control-failed-expected {w base emsg lno ei fulldesc newdata} {
604 set line [lindex [split $ei "\n"] 0]
605 debug "parser-control-failed-expected: $w $base: $lno: $emsg\n $line"
607 parser-control-failed-core $w $base \
609 "err: [string trim $emsg]: \"$line\"" \
610 "at line $lno: $emsg" \
613 $eb.text.text tag add error $lno.0 $lno.end
614 $eb.text.text see $lno.0
616 proc parser-control-failed-unexpected {w base emsg ei} {
618 parser-control-failed-core $w $base \
620 $emsg $emsg "Details and stack trace:" $ei
623 proc reparse {base varname old fulldesc okshow noneshow parse ok} {
624 upvar #0 $varname var
625 manyset [errexpect-catch {
627 if {[string length [string trim $var]]} {
628 parser-control-ok .ctrl.$base $base $okshow
630 parser-control-none .ctrl.$base $base $noneshow
632 }] failed emsg lno ei
634 parser-control-failed-expected .ctrl.$base $base \
635 $emsg $lno $ei $fulldesc $var
643 #---------- main user interface ----------
645 proc widgets-setup {} {
646 global canvas debug pirate ocean
648 frame .f -border 1 -relief groove
651 pack $canvas -expand 1 -fill both
652 pack .f -expand 1 -fill both -side left
655 pack .ctrl -side right
657 debug "BBOX [$canvas bbox all]"
659 panner::canvas-scroll-bbox .f.c
660 panner::create .ctrl.pan .f.c 120 120 $debug
662 pack .ctrl.pan -side top -pady 10 -padx 5
664 pack .ctrl.zoom -side top
666 button .ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2}
667 button .ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2}
668 pack .ctrl.zoom.out .ctrl.zoom.in -side left
670 parser-control-create .ctrl.acquire \
672 "Clipboard parsing error" \
674 pack .ctrl.acquire -side top -pady 2
676 parser-control-create .ctrl.notes \
677 notes "Reload notes" \
678 "Vessel notes loading report" \
680 pack .ctrl.notes -side top -pady 2
682 wm geometry . 1024x480
683 wm title . "where-vessels - $pirate on the $ocean ocean"
688 set nscale [expr "\$scale $extail"]
689 debug "ZOOM $scale $nscale"
690 if {$nscale < 1 || $nscale > 200} return
695 proc invoke_acquire {} {
696 global clipboard errorInfo
700 set clipboard [clipboard get]
702 parser-control-failed-unexpected .ctrl.acquire acquire \
703 $emsg "fetching clipboard:\n\n$errorInfo"
708 clipboard $old "Clipboard contents:" { acquired ok } { no vessels } {
716 proc invoke_notes {} {
717 global notes_data errorInfo notes_loc
723 parser-control-failed-unexpected .ctrl.notes notes \
724 $emsg "loading $notes_loc:\n\n$errorInfo"
729 notes_data $old "Vessel notes:" "loaded ok" { no notes } {
738 #---------- main program ----------
744 httpclientsetup where-vessels
749 if {[catch { parse-clipboard } emsg]} {
750 puts stderr "$emsg\n$errorInfo"
753 after idle invoke_notes