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 ::http::geturl $notes_loc
141 switch -glob [::http::status].[::http::ncode] {
143 ok.* { error "retrieving vessel-notes $url: [::http::code]" }
144 * { error "retrieving vessel-notes $url: [::http::error]" }
146 set newdata [::http::data]
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 }
160 foreach l [split $notes_data "\n"] {
161 regsub -all {\t+} $l "\t" l
162 manyset [split $l "\t"] vname vid owner note
165 set notes($nk) [list $owner $note]
170 #---------- loading and parsing the clipboard (vessel locations) ----------
173 global pirate notes_used
175 switch -exact [errexpect-arrayget vi vesselClass] {
176 smsloop { set sz 00sl }
177 lgsloop { set sz 01ct }
179 longship { set sz 03ls }
180 baghlah { set sz 04bg }
181 merchbrig { set sz 05mb }
182 warbrig { set sz 06wb }
183 xebec { set sz 07xe }
184 warfrig { set sz 08wf }
185 merchgal { set sz 09mg }
186 grandfrig { set sz 10gf }
187 default { errexpect-error "unknown class" }
190 switch -exact [errexpect-arrayget vi vesselSubclass] {
192 icy { append abbrev F }
193 default { errexpect-error "unknown subclass ?" }
195 switch -exact [errexpect-arrayget vi isLocked]/[ \
196 errexpect-arrayget vi isBattleReady] {
197 true/false { append abbrev 2- }
198 false/false { append abbrev 1+ }
199 false/true { append abbrev 0* }
200 default { errexpect-error "unexpected isLocked/isBattleReady" }
202 switch -exact [errexpect-arrayget vi inPort] {
204 false { append abbrev ? }
205 default { errexpect-error "unexpected inPort" }
207 set nk [errexpect-arrayget vi vesselId].[errexpect-arrayget vi vesselName]
208 upvar #0 notes($nk) note
209 if {[info exists note]} {
210 manyset $note owner xabbrev
211 if {![string compare $owner $pirate]} {
216 append abbrev $xabbrev
217 set notes_used($nk) 1
221 set kk "[errexpect-arrayget vi islandName] $abbrev"
222 upvar #0 count($kk) k
223 if {![info exists k]} { set k 0 }
228 proc parse-clipboard {} {
229 global clipboard count notes notes_used
231 catch { unset count }
232 catch { unset notes_used }
234 set itemre { (\w+) = ([^=]*) }
235 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
239 foreach l [split $clipboard "\n"] {
241 errexpect-setline $lno $l
242 if {![string length $l]} continue
245 if {![regexp -expanded $manyitemre $l dummy \
246 thiskey thisval rhs]} {
247 errexpect-error "badly formatted"
249 set vi($thiskey) $thisval
250 if {![string length $rhs]} break
251 regsub {^, } $rhs {} rhs
257 foreach nk [lsort [array names notes]] {
258 if {![info exists notes_used($nk)]} {
259 debug "IGNORED NOTE $nk"
264 proc load-clipboard-file {fn} {
266 glset clipboard [read $f]
271 #---------- loading and parsing the chart ----------
275 debug "FETCHING CHART"
276 set chart [eval exec $scraper [list | perl -we {
281 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
282 sub { sprintf "%d %d", @_; },
283 sub { printf "archlabel %d %d %s\n", @_; },
284 sub { printf "island %s %s\n", @_; },
285 sub { printf "league %s %s %s.\n", @_; },
286 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
288 STDOUT->error and die $!;
297 return [expr {$c * $scale}]
300 proc chart-got/archlabel {args} { }
301 proc chart-got/island {x y args} {
302 # debug "ISLE $x $y $args"
303 global canvas isleloc
304 set isleloc($args) [list $x $y]
306 # $canvas create oval \
307 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
308 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
310 $canvas create text [coord $x] [coord $y] \
311 -text $args -anchor s
313 proc chart-got/league {x1 y1 x2 y2 kind} {
314 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
316 set l [$canvas create line \
317 [coord $x1] [coord $y1] \
318 [coord $x2] [coord $y2]]
319 if {![string compare $kind .]} {
320 $canvas itemconfigure $l -dash .
325 global chart count isleloc canvas
329 foreach l [split $chart "\n"] {
330 # debug "CHART-GOT $l"
331 set proc [lindex $l 0]
332 eval chart-got/$proc [lrange $l 1 end]
335 set lastislandname {}
336 foreach key [lsort [array names count]] {
338 # debug "SHOWING $key $c"
339 regexp {^(.*) (\S+)$} $key dummy islandname abbrev
340 if {[string compare $lastislandname $islandname]} {
341 manyset $isleloc($islandname) x y
344 set lastislandname $islandname
348 regsub -all {[0-9]} $text {} text
350 set text [format "%2d%s" $c $text]
352 set text [format " %s" $text]
354 set id [$canvas create text $x $y \
355 -anchor nw -font fixed \
357 set bbox [$canvas bbox $id]
358 set bid [eval $canvas create rectangle $bbox -fill white]
359 $canvas lower $bid $id
360 manyset $bbox dummy dummy dummy y
364 panner::updatecanvas-bbox .ctrl.pan
368 #---------- user interface ----------
370 proc widgets-setup {} {
371 global canvas debug acquire_deffont
373 frame .f -border 1 -relief groove
376 pack $canvas -expand 1 -fill both
377 pack .f -expand 1 -fill both -side left
380 pack .ctrl -side right
382 debug "BBOX [$canvas bbox all]"
384 panner::canvas-scroll-bbox .f.c
385 panner::create .ctrl.pan .f.c 120 120 $debug
387 pack .ctrl.pan -side top -pady 10 -padx 5
389 pack .ctrl.zoom -side top
391 button .ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2}
392 button .ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2}
393 pack .ctrl.zoom.out .ctrl.zoom.in -side left
395 parser-control-create .ctrl.acquire \
397 "Clipboard parsing error" \
399 pack .ctrl.acquire -side top -pady 2
401 parser-control-create .ctrl.notes \
402 notes "Reload notes" \
403 "Vessel notes loading error" \
405 pack .ctrl.notes -side top -pady 2
407 wm geometry . 1024x480
410 proc parser-control-create {w base invokebuttontext etl_title} {
412 button $w.do -text $invokebuttontext -command invoke_$base
414 frame $w.resframe -width 120 -height 32
415 button $w.resframe.res -text {} -anchor nw \
416 -padx 1 -pady 1 -borderwidth 0 -justify left
417 glset deffont_$base [$w.resframe.res cget -font]
418 place $w.resframe.res -relx 0.5 -y 0 -anchor n
421 pack $w.resframe -side top -expand y -fill both
426 wm title $eb "$etl_title - where-vessels"
428 label $eb.title -text $etl_title
429 pack $eb.title -side top
431 button $eb.close -text Close -command [list wm withdraw $eb]
432 pack $eb.close -side bottom
434 frame $eb.emsg -bd 2 -relief groove
435 label $eb.emsg.lab -text "Error:"
436 text $eb.emsg.text -height 1
437 pack $eb.emsg.text -side bottom
438 pack $eb.emsg.lab -side left
440 pack $eb.emsg -side top -pady 2
442 frame $eb.text -bd 2 -relief groove
443 pack $eb.text -side bottom -pady 2
448 -xscrollcommand [list $eb.text.xscroll set] \
449 -yscrollcommand [list $eb.text.yscroll set]
450 $eb.text.text tag configure error \
451 -background red -foreground white
453 scrollbar $eb.text.xscroll -orient horizontal \
454 -command [list $eb.text.text xview]
455 scrollbar $eb.text.yscroll -orient vertical \
456 -command [list $eb.text.text yview]
458 grid configure $eb.text.lab -row 0 -column 0 -sticky w
459 grid configure $eb.text.text -row 1 -column 0
460 grid configure $eb.text.yscroll -sticky ns -row 1 -column 1
461 grid configure $eb.text.xscroll -sticky ew -row 2 -column 0
464 proc parser-control-ok-core {w base background show} {
465 debug "parser-control-ok-core $w $base $background $show"
466 upvar #0 deffont_$base deffont
467 $w.resframe.res configure \
468 -background $background -disabledforeground black -font $deffont \
469 -state disabled -command {} \
472 proc parser-control-ok {w base show} {
473 parser-control-ok-core $w $base green $show
475 proc parser-control-none {w base show} {
476 parser-control-ok-core $w $base blue $show
478 proc parser-control-failed-core {w base tiny summary fulldesc fulldata} {
479 debug "parser-control-failed-core $w $base $summary $fulldesc"
482 $eb.emsg.text delete 0.0 end
483 $eb.emsg.text insert end $summary
485 $eb.text.lab configure -text $fulldesc
486 $eb.text.text delete 0.0 end
487 $eb.text.text insert end $fulldata
489 regsub -all {.{18}} $tiny "&\n" ewrap
491 $w.resframe.res configure \
492 -background red -foreground white -font fixed \
493 -state normal -command [list wm deiconify $eb] \
497 proc parser-control-failed-expected {w base emsg lno ei fulldesc newdata} {
500 set line [lindex [split $ei "\n"] 0]
501 debug "parser-control-failed-expected: $w $base: $lno: $emsg\n $line"
503 parser-control-failed-core $w $base \
504 "err: [string trim $emsg]: \"$line\"" \
505 "at line $lno: $emsg" \
508 $eb.text.text tag add error $lno.0 $lno.end
509 $eb.text.text see $lno.0
514 set nscale [expr "\$scale $extail"]
515 debug "ZOOM $scale $nscale"
516 if {$nscale < 1 || $nscale > 200} return
521 proc reparse {base varname fulldesc okshow noneshow set parse} {
522 upvar #0 $varname var
525 manyset [errexpect-catch {
527 if {[string length [string trim $var]]} {
528 parser-control-ok .ctrl.$base $base $okshow
530 parser-control-none .ctrl.$base $base $noneshow
532 }] failed emsg lno ei
534 parser-control-failed-expected .ctrl.$base $base \
535 $emsg $lno $ei $fulldesc $var
541 proc invoke_acquire {} {
544 clipboard "Clipboard contents:" { acquired ok } { no vessels } \
545 { set clipboard [clipboard get] } \
550 proc invoke_notes {} {
551 global notes_data errorInfo notes_loc
553 reparse notes notes_data "Vessel notes:" "notes reloaded" { no notes } \
558 parser-control-failed-core .ctrl.notes notes \
559 $emsg $emsg "Details and stack trace:" \
560 "loading $notes_loc:\n\n$errorInfo"
568 #---------- main program ----------
572 httpclientsetup where-vessels