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-catch {code} {
70 global errorInfo errorCode
74 debug "ERREXPECT CATCH |$rc|$rv|$errorCode|$errorInfo|"
75 if {$rc==1 && ![string compare YARRG-ERREXPECT [lindex $errorCode 0]]} {
76 return [list 1 $rv [lindex $errorCode 1] $errorInfo]
80 return -code $rc -errorinfo $errorInfo -errorcode $errorCode $rv
84 #---------- argument parsing ----------
88 if {$ai >= [llength $argv]} {
89 badusage "option [lindex $argv [expr {$ai-1}]] needs a value"
91 set v [lindex $argv $ai]
96 set notes_loc vessel-notes
97 set scraper {./yppedia-ocean-scraper --chart}
104 while {[regexp {^\-} [set arg [lindex $argv $ai]]]} {
106 switch -exact -- $arg {
108 --pirate { glset pirate [string totitle [nextarg]] }
109 --ocean { glset ocean [string totitle [nextarg]] }
110 --clipboard-file { load-clipboard-file [nextarg] }
111 --local-html-dir { lappend scraper --local-html-dir=[nextarg] }
112 --notes { glset notes_loc [nextarg] }
113 --debug { incr debug }
114 default { badusage "unknown option $arg" }
117 set argv [lrange $argv $ai end]
118 if {[llength $argv]} { badusage "non-option args not allowed" }
121 proc argdefaults {} {
122 global ocean notes_loc pirate scraper
123 if {![info exists ocean] || ![info exists pirate]} {
124 set cmd {./yarrg --find-window-only --quiet}
125 if {[info exists ocean]} { lappend cmd --ocean $ocean }
126 if {[info exists pirate]} { lappend cmd --pirate $pirate }
127 manyset [split [eval exec $cmd] " "] ocean pirate
129 lappend scraper $ocean
133 #---------- loading and parsing the vessel notes ----------
136 global notes_loc notes_data
137 if {[regexp {^\w+\:} $notes_loc]} {
139 debug "FETCHING NOTES $notes_loc"
140 set req [::http::geturl $notes_loc]
141 switch -glob [::http::status $req].[::http::ncode $req] {
143 ok.* { error "retrieving vessel-notes: [::http::code $req]" }
144 * { error "Retrieving vessel-notes: [::http::error $req]" }
146 set newdata [::http::data $req]
149 debug "READING NOTES $notes_loc"
150 set vn [open $notes_loc]
151 set newdata [read $vn]
154 set notes_data $newdata
157 proc parse-notes {} {
158 global notes_data notes
159 catch { unset notes }
162 foreach l [split $notes_data "\n"] {
164 errexpect-setline $lno $l
165 set l [string trim $l]
166 if {![string length $l]} continue
167 if {[regexp {^\#} $l]} continue
168 if {![regexp -expanded \
169 {^ (\d+) (?: \s+([^=]*?) )? \s* =
171 (?: \s+ (\S+) )?)? $} \
172 $l dummy vid vname owner note]} {
173 errexpect-error "badly formatted"
175 set vname [string trim $vname]
176 if {[info exists notes($vid)]} {
177 errexpect-error "duplicate vesselid $vid"
179 set notes($vid) [list $lno $vname $owner $note]
183 proc note-info {lno vid name description} {
185 lappend note_infos [list $lno $vid $name $description]
188 proc display-note-infos {} {
189 global note_infos note_missings notes
191 set nmissing [llength $note_missings]
192 debug "display-note-infos $nmissing [array size notes]"
194 if {[llength $note_infos]} {
195 set tiny "[llength $note_infos] warnings"
196 } elseif {$nmissing && [array size notes]} {
197 set tiny "$nmissing missing"
204 foreach info $note_infos {
205 manyset $info lno vid name description
206 append infodata "vessel"
207 append infodata " $vid"
208 if {[string length $name]} { append infodata " $name" }
209 append infodata ": " $description "\n"
213 append infodata "$nmissing vessels not mentioned in notes:\n"
215 foreach info [lsort $note_missings] {
216 manyset $info island name vid
217 if {[string compare $island $last_island]} {
218 append infodata "# $island:\n"
219 set last_island $island
221 append infodata [format "%-9d %-29s =\n" $vid $name]
225 parser-control-failed-core .ctrl.notes notes \
228 "[llength $note_infos] warnings;\
229 $nmissing vessels missing" \
230 "Full description of warnings and missing vessels:" \
234 #---------- loading and parsing the clipboard (vessel locations) ----------
237 global pirate notes_used note_missings newnotes
239 switch -exact [errexpect-arrayget vi vesselClass] {
240 smsloop { set sz 00sl }
241 lgsloop { set sz 01ct }
243 longship { set sz 03ls }
244 baghlah { set sz 04bg }
245 merchbrig { set sz 05mb }
246 warbrig { set sz 06wb }
247 xebec { set sz 07xe }
248 warfrig { set sz 08wf }
249 merchgal { set sz 09mg }
250 grandfrig { set sz 10gf }
251 default { errexpect-error "unknown class" }
254 switch -exact [errexpect-arrayget vi vesselSubclass] {
256 icy { append abbrev F }
257 default { errexpect-error "unknown subclass ?" }
259 switch -exact [errexpect-arrayget vi isLocked]/[ \
260 errexpect-arrayget vi isBattleReady] {
261 true/false { append abbrev 2- }
262 false/false { append abbrev 1+ }
263 false/true { append abbrev 0* }
264 default { errexpect-error "unexpected isLocked/isBattleReady" }
266 switch -exact [errexpect-arrayget vi inPort] {
268 false { append abbrev ? }
269 default { errexpect-error "unexpected inPort" }
271 set vid [errexpect-arrayget vi vesselId]
272 upvar #0 notes($vid) note
273 set realname [errexpect-arrayget vi vesselName]
274 set island [errexpect-arrayget vi islandName]
278 if {[info exists note]} {
279 manyset $note lno notename owner xabbrev
280 if {[string compare -nocase $realname $notename]} {
281 note-info $lno $vid $realname \
282 "notes say name is $notename - perhaps renamed"
284 if {[string length $owner]} {
285 if {![string compare $owner $pirate]} {
291 append abbrev $xabbrev
292 set notes_used($vid) 1
295 lappend note_missings [list $island $realname $vid]
297 lappend newnotes [list $vid $realname $owner $xabbrev]
299 set kk "$island $abbrev"
300 upvar #0 count($kk) k
301 if {![info exists k]} { set k 0 }
306 proc parse-clipboard {} {
307 global clipboard count notes notes_used newnotes
309 catch { unset count }
310 catch { unset notes_used }
312 glset note_missings {}
316 set itemre { (\w+) = ([^=]*) }
317 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
321 foreach l [split $clipboard "\n"] {
323 errexpect-setline $lno $l
324 if {![string length $l]} continue
327 if {![regexp -expanded $manyitemre $l dummy \
328 thiskey thisval rhs]} {
329 errexpect-error "badly formatted"
331 set vi($thiskey) $thisval
332 if {![string length $rhs]} break
333 regsub {^, } $rhs {} rhs
339 if {[llength $newnotes]} {
340 foreach vid [lsort [array names notes]] {
341 if {![info exists notes_used($vid)]} {
342 manyset $notes($vid) lno notename
343 note-info $lno $vid $notename \
344 "vessel in notes no longer found"
350 proc load-clipboard-file {fn} {
352 glset clipboard [read $f]
357 #---------- loading and parsing the chart ----------
361 debug "FETCHING CHART"
362 set chart [eval exec $scraper [list | perl -we {
367 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
368 sub { sprintf "%d %d", @_; },
369 sub { printf "archlabel %d %d %s\n", @_; },
370 sub { printf "island %s %s\n", @_; },
371 sub { printf "league %s %s %s.\n", @_; },
372 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
374 STDOUT->error and die $!;
383 return [expr {$c * $scale}]
386 proc chart-got/archlabel {args} { }
387 proc chart-got/island {x y args} {
388 # debug "ISLE $x $y $args"
389 global canvas isleloc
390 set isleloc($args) [list $x $y]
392 # $canvas create oval \
393 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
394 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
396 $canvas create text [coord $x] [coord $y] \
397 -text $args -anchor s
399 proc chart-got/league {x1 y1 x2 y2 kind} {
400 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
402 set l [$canvas create line \
403 [coord $x1] [coord $y1] \
404 [coord $x2] [coord $y2]]
405 if {![string compare $kind .]} {
406 $canvas itemconfigure $l -dash .
411 global chart count isleloc canvas
415 foreach l [split $chart "\n"] {
416 # debug "CHART-GOT $l"
417 set proc [lindex $l 0]
418 eval chart-got/$proc [lrange $l 1 end]
421 set lastislandname {}
422 foreach key [lsort [array names count]] {
424 # debug "SHOWING $key $c"
425 regexp {^(.*) (\S+)$} $key dummy islandname abbrev
426 if {[string compare $lastislandname $islandname]} {
427 manyset $isleloc($islandname) x y
430 set lastislandname $islandname
434 regsub -all {[0-9]} $text {} text
436 set text [format "%2d%s" $c $text]
438 set text [format " %s" $text]
440 set id [$canvas create text $x $y \
441 -anchor nw -font fixed \
443 set bbox [$canvas bbox $id]
444 set bid [eval $canvas create rectangle $bbox -fill white]
445 $canvas lower $bid $id
446 manyset $bbox dummy dummy dummy y
450 panner::updatecanvas-bbox .ctrl.pan
454 #---------- parser error reporting ----------
456 proc parser-control-create {w base invokebuttontext etl_title} {
458 button $w.do -text $invokebuttontext -command invoke_$base
460 frame $w.resframe -width 120 -height 32
461 button $w.resframe.res -text {} -anchor nw \
462 -padx 1 -pady 1 -borderwidth 0 -justify left
463 glset deffont_$base [$w.resframe.res cget -font]
464 place $w.resframe.res -relx 0.5 -y 0 -anchor n
467 pack $w.resframe -side top -expand y -fill both
472 wm title $eb "where-vessels - $etl_title"
474 label $eb.title -text $etl_title
475 pack $eb.title -side top
477 button $eb.close -text Close -command [list wm withdraw $eb]
478 pack $eb.close -side bottom
480 frame $eb.emsg -bd 2 -relief groove
481 label $eb.emsg.lab -text "Error:"
482 text $eb.emsg.text -height 1
483 pack $eb.emsg.text -side bottom
484 pack $eb.emsg.lab -side left
486 pack $eb.emsg -side top -pady 2
488 frame $eb.text -bd 2 -relief groove
489 pack $eb.text -side bottom -pady 2
493 text $eb.text.text -width 85 \
494 -xscrollcommand [list $eb.text.xscroll set] \
495 -yscrollcommand [list $eb.text.yscroll set]
496 $eb.text.text tag configure error \
497 -background red -foreground white
499 scrollbar $eb.text.xscroll -orient horizontal \
500 -command [list $eb.text.text xview]
501 scrollbar $eb.text.yscroll -orient vertical \
502 -command [list $eb.text.text yview]
504 grid configure $eb.text.lab -row 0 -column 0 -sticky w
505 grid configure $eb.text.text -row 1 -column 0
506 grid configure $eb.text.yscroll -sticky ns -row 1 -column 1
507 grid configure $eb.text.xscroll -sticky ew -row 2 -column 0
510 proc parser-control-ok-core {w base background show} {
511 debug "parser-control-ok-core $w $base $background $show"
512 upvar #0 deffont_$base deffont
513 $w.resframe.res configure \
514 -background $background -disabledforeground black -font $deffont \
515 -state disabled -command {} \
518 proc parser-control-ok {w base show} {
519 parser-control-ok-core $w $base green $show
521 proc parser-control-none {w base show} {
522 parser-control-ok-core $w $base blue $show
524 proc parser-control-failed-core {w base foreground background smallfont
525 tiny summary fulldesc fulldata} {
526 debug "parser-control-failed-core $w $base $summary $fulldesc"
527 upvar #0 deffont_$base deffont
530 $eb.emsg.text delete 0.0 end
531 $eb.emsg.text insert end $summary
533 $eb.text.lab configure -text $fulldesc
534 $eb.text.text delete 0.0 end
535 $eb.text.text insert end $fulldata
537 regsub -all {.{18}} $tiny "&\n" ewrap
545 $w.resframe.res configure \
546 -background $background -foreground $foreground -font $font \
547 -state normal -command [list wm deiconify $eb] \
551 proc parser-control-failed-expected {w base emsg lno ei fulldesc newdata} {
554 set line [lindex [split $ei "\n"] 0]
555 debug "parser-control-failed-expected: $w $base: $lno: $emsg\n $line"
557 parser-control-failed-core $w $base \
559 "err: [string trim $emsg]: \"$line\"" \
560 "at line $lno: $emsg" \
563 $eb.text.text tag add error $lno.0 $lno.end
564 $eb.text.text see $lno.0
566 proc parser-control-failed-unexpected {w base emsg ei} {
568 parser-control-failed-core $w $base \
570 $emsg $emsg "Details and stack trace:" $ei
573 proc reparse {base varname old fulldesc okshow noneshow parse ok} {
574 upvar #0 $varname var
575 manyset [errexpect-catch {
577 if {[string length [string trim $var]]} {
578 parser-control-ok .ctrl.$base $base $okshow
580 parser-control-none .ctrl.$base $base $noneshow
582 }] failed emsg lno ei
584 parser-control-failed-expected .ctrl.$base $base \
585 $emsg $lno $ei $fulldesc $var
593 #---------- main user interface ----------
595 proc widgets-setup {} {
596 global canvas debug pirate ocean
598 frame .f -border 1 -relief groove
601 pack $canvas -expand 1 -fill both
602 pack .f -expand 1 -fill both -side left
605 pack .ctrl -side right
607 debug "BBOX [$canvas bbox all]"
609 panner::canvas-scroll-bbox .f.c
610 panner::create .ctrl.pan .f.c 120 120 $debug
612 pack .ctrl.pan -side top -pady 10 -padx 5
614 pack .ctrl.zoom -side top
616 button .ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2}
617 button .ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2}
618 pack .ctrl.zoom.out .ctrl.zoom.in -side left
620 parser-control-create .ctrl.acquire \
622 "Clipboard parsing error" \
624 pack .ctrl.acquire -side top -pady 2
626 parser-control-create .ctrl.notes \
627 notes "Reload notes" \
628 "Vessel notes loading report" \
630 pack .ctrl.notes -side top -pady 2
632 wm geometry . 1024x480
633 wm title . "where-vessels - $pirate on the $ocean ocean"
638 set nscale [expr "\$scale $extail"]
639 debug "ZOOM $scale $nscale"
640 if {$nscale < 1 || $nscale > 200} return
645 proc invoke_acquire {} {
646 global clipboard errorInfo
650 set clipboard [clipboard get]
652 parser-control-failed-unexpected .ctrl.acquire acquire \
653 $emsg "fetching clipboard:\n\n$errorInfo"
658 clipboard $old "Clipboard contents:" { acquired ok } { no vessels } {
666 proc invoke_notes {} {
667 global notes_data errorInfo notes_loc
673 parser-control-failed-unexpected .ctrl.notes notes \
674 $emsg "loading $notes_loc:\n\n$errorInfo"
679 notes_data $old "Vessel notes:" "loaded ok" { no notes } {
688 #---------- main program ----------
692 httpclientsetup where-vessels
697 after idle invoke_notes