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 debug "VARS [array names av]"
65 upvar 1 ${arrayvar}($key) v
66 if {[info exists v]} { return $v }
67 errexpect-error "undefined $key"
70 proc errexpect-catch {code} {
71 global errorInfo errorCode
75 debug "ERREXPECT CATCH |$rc|$rv|$errorCode|$errorInfo|"
76 if {$rc==1 && ![string compare YARRG-ERREXPECT [lindex $errorCode 0]]} {
77 return [list 1 $rv [lindex $errorCode 1] $errorInfo]
81 return -code $rc -errorinfo $errorInfo -errorcode $errorCode $rv
85 #---------- argument parsing ----------
89 if {$ai >= [llength $argv]} {
90 badusage "option [lindex $argv [expr {$ai-1}]] needs a value"
92 set v [lindex $argv $ai]
97 set notes_loc vessel-notes
98 set scraper {./yppedia-ocean-scraper --chart}
105 while {[regexp {^\-} [set arg [lindex $argv $ai]]]} {
107 switch -exact -- $arg {
109 --pirate { glset pirate [string totitle [nextarg]] }
110 --ocean { glset ocean [string totitle [nextarg]] }
111 --clipboard-file { load-clipboard-file [nextarg] }
112 --local-html-dir { lappend scraper --local-html-dir=[nextarg] }
113 --notes { glset notes_loc [nextarg] }
114 --debug { incr debug }
115 default { badusage "unknown option $arg" }
118 set argv [lrange $argv $ai end]
119 if {[llength $argv]} { badusage "non-option args not allowed" }
122 proc argdefaults {} {
123 global ocean notes_loc pirate scraper
124 if {![info exists ocean] || ![info exists pirate]} {
125 set cmd {./yarrg --find-window-only --quiet}
126 if {[info exists ocean]} { lappend cmd --ocean $ocean }
127 if {[info exists pirate]} { lappend cmd --pirate $pirate }
128 manyset [split [eval exec $cmd] " "] ocean pirate
130 lappend scraper $ocean
134 #---------- loading and parsing the vessel notes ----------
137 global notes_loc notes
138 catch { unset notes }
139 if {[regexp {^\w+\:} $notes_loc]} {
141 debug "FETCHING NOTES"
142 ::http::geturl $notes_loc
143 switch -glob [::http::status].[::http::ncode] {
145 ok.* { error "retrieving vessel-notes $url: [::http::code]" }
146 * { error "retrieving vessel-notes $url: [::http::error]" }
148 set notes_data [::http::data]
151 set vn [open $notes_loc]
152 set notes_data [read $vn]
155 foreach l [split $notes_data "\n"] {
156 regsub -all {\t+} $l "\t" l
157 manyset [split $l "\t"] vname vid owner note
160 set notes($nk) [list $owner $note]
165 #---------- loading and parsing the clipboard (vessel locations) ----------
168 global pirate notes_used
170 switch -exact [errexpect-arrayget vi vesselClass] {
171 smsloop { set sz 00sl }
172 lgsloop { set sz 01ct }
174 longship { set sz 03ls }
175 baghlah { set sz 04bg }
176 merchbrig { set sz 05mb }
177 warbrig { set sz 06wb }
178 xebec { set sz 07xe }
179 warfrig { set sz 08wf }
180 merchgal { set sz 09mg }
181 grandfrig { set sz 10gf }
182 default { errexpect-error "unknown class" }
185 switch -exact [errexpect-arrayget vi vesselSubclass] {
187 icy { append abbrev F }
188 default { errexpect-error "unknown subclass ?" }
190 switch -exact [errexpect-arrayget vi isLocked]/[ \
191 errexpect-arrayget vi isBattleReady] {
192 true/false { append abbrev 2- }
193 false/false { append abbrev 1+ }
194 false/true { append abbrev 0* }
195 default { errexpect-error "unexpected isLocked/isBattleReady" }
197 switch -exact [errexpect-arrayget vi inPort] {
199 false { append abbrev ? }
200 default { errexpect-error "unexpected inPort" }
202 set nk [errexpect-arrayget vi vesselId].[errexpect-arrayget vi vesselName]
203 upvar #0 notes($nk) note
204 if {[info exists note]} {
205 manyset $note owner xabbrev
206 if {![string compare $owner $pirate]} {
211 append abbrev $xabbrev
212 set notes_used($nk) 1
216 set kk "[errexpect-arrayget vi islandName] $abbrev"
217 upvar #0 count($kk) k
218 if {![info exists k]} { set k 0 }
223 proc parse-clipboard {} {
224 global clipboard count notes notes_used
226 catch { unset count }
227 catch { unset notes_used }
229 set itemre { (\w+) = ([^=]*) }
230 set manyitemre "^\\\[ $itemre ( (?: ,\\ $itemre)* ) \\]\$"
234 foreach l [split $clipboard "\n"] {
236 errexpect-setline $lno $l
237 if {![string length $l]} continue
240 if {![regexp -expanded $manyitemre $l dummy \
241 thiskey thisval rhs]} {
242 errexpect-error "badly formatted"
244 set vi($thiskey) $thisval
245 if {![string length $rhs]} break
246 regsub {^, } $rhs {} rhs
252 foreach nk [lsort [array names notes]] {
253 if {![info exists notes_used($nk)]} {
254 debug "IGNORED NOTE $nk"
259 proc load-clipboard-file {fn} {
261 glset clipboard [read $f]
266 #---------- loading and parsing the chart ----------
270 debug "FETCHING CHART"
271 set chart [eval exec $scraper [list | perl -we {
276 yppedia_chart_parse(\*STDIN, (new IO::File ">/dev/null"),
277 sub { sprintf "%d %d", @_; },
278 sub { printf "archlabel %d %d %s\n", @_; },
279 sub { printf "island %s %s\n", @_; },
280 sub { printf "league %s %s %s.\n", @_; },
281 sub { printf STDERR "warning: %s: incomprehensible: %s", @_; }
283 STDOUT->error and die $!;
292 return [expr {$c * $scale}]
295 proc chart-got/archlabel {args} { }
296 proc chart-got/island {x y args} {
297 # debug "ISLE $x $y $args"
298 global canvas isleloc
299 set isleloc($args) [list $x $y]
301 # $canvas create oval \
302 # [expr {[coord $x] - $sz}] [expr {[coord $y] - $sz}] \
303 # [expr {[coord $x] + $sz}] [expr {[coord $y] + $sz}] \
305 $canvas create text [coord $x] [coord $y] \
306 -text $args -anchor s
308 proc chart-got/league {x1 y1 x2 y2 kind} {
309 # debug "LEAGUE $x1 $y1 $x2 $y2 $kind"
311 set l [$canvas create line \
312 [coord $x1] [coord $y1] \
313 [coord $x2] [coord $y2]]
314 if {![string compare $kind .]} {
315 $canvas itemconfigure $l -dash .
320 global chart count isleloc canvas
324 foreach l [split $chart "\n"] {
325 # debug "CHART-GOT $l"
326 set proc [lindex $l 0]
327 eval chart-got/$proc [lrange $l 1 end]
330 set lastislandname {}
331 foreach key [lsort [array names count]] {
333 # debug "SHOWING $key $c"
334 regexp {^(.*) (\S+)$} $key dummy islandname abbrev
335 if {[string compare $lastislandname $islandname]} {
336 manyset $isleloc($islandname) x y
339 set lastislandname $islandname
343 regsub -all {[0-9]} $text {} text
345 set text [format "%2d%s" $c $text]
347 set text [format " %s" $text]
349 set id [$canvas create text $x $y \
350 -anchor nw -font fixed \
352 set bbox [$canvas bbox $id]
353 set bid [eval $canvas create rectangle $bbox -fill white]
354 $canvas lower $bid $id
355 manyset $bbox dummy dummy dummy y
359 panner::updatecanvas-bbox .ctrl.pan
363 #---------- user interface ----------
365 proc widgets-setup {} {
366 global canvas debug acqdeffont
368 frame .f -border 1 -relief groove
371 pack $canvas -expand 1 -fill both
372 pack .f -expand 1 -fill both -side left
375 pack .ctrl -side right
377 debug "BBOX [$canvas bbox all]"
379 panner::canvas-scroll-bbox .f.c
380 panner::create .ctrl.pan .f.c 120 120 $debug
382 pack .ctrl.pan -side top -pady 10 -padx 5
384 pack .ctrl.zoom -side top
386 button .ctrl.zoom.out -text - -font {Courier 16} -command {zoom /2}
387 button .ctrl.zoom.in -text + -font {Courier 16} -command {zoom *2}
388 pack .ctrl.zoom.out .ctrl.zoom.in -side left
391 button .ctrl.acquire.do -text Acquire -command acquire
393 frame .ctrl.acquire.resframe -width 120 -height 32
394 button .ctrl.acquire.resframe.res -text {} -anchor nw \
395 -padx 1 -pady 1 -borderwidth 0 -justify left
396 glset acqdeffont [.ctrl.acquire.resframe.res cget -font]
397 place .ctrl.acquire.resframe.res -relx 0.5 -y 0 -anchor n
398 # -relheight 1.0 -relwidth 1.0
400 pack .ctrl.acquire.do -side top
401 pack .ctrl.acquire.resframe -side top -expand y -fill both
402 pack .ctrl.acquire -side top
404 toplevel .err_acquire
405 wm withdraw .err_acquire
406 wm title .err_acquire "where-vessels clipboard parsing error"
408 label .err_acquire.title -text "Clipboard parsing error"
409 pack .err_acquire.title -side top
411 button .err_acquire.close -text Close -command {wm withdraw .err_acquire}
412 pack .err_acquire.close -side bottom
414 frame .err_acquire.emsg -bd 2 -relief groove
415 label .err_acquire.emsg.lab -text "Error:"
416 text .err_acquire.emsg.text -height 1
417 pack .err_acquire.emsg.text -side bottom
418 pack .err_acquire.emsg.lab -side left
420 pack .err_acquire.emsg -side top -pady 2
422 frame .err_acquire.text -bd 2 -relief groove
423 pack .err_acquire.text -side bottom -pady 2
425 label .err_acquire.text.lab -text "Clipboard contents:"
427 text .err_acquire.text.text \
428 -xscrollcommand {.err_acquire.text.xscroll set} \
429 -yscrollcommand {.err_acquire.text.yscroll set}
431 scrollbar .err_acquire.text.xscroll -orient horizontal \
432 -command {.err_acquire.text.text xview}
433 scrollbar .err_acquire.text.yscroll -orient vertical \
434 -command {.err_acquire.text.text yview}
436 grid configure .err_acquire.text.lab -row 0 -column 0 -sticky w
437 grid configure .err_acquire.text.text -row 1 -column 0
438 grid configure .err_acquire.text.yscroll -sticky ns -row 1 -column 1
439 grid configure .err_acquire.text.xscroll -sticky ew -row 2 -column 0
441 wm geometry . 1024x480
446 set nscale [expr "\$scale $extail"]
447 debug "ZOOM $scale $nscale"
448 if {$nscale < 1 || $nscale > 200} return
453 proc acquire_showerror {} {
455 tk_messageBox -type ok \
456 -title "where-vessels: clipboard parsing error" \
461 global clipboard acqdeffont acqerr errorInfo
463 set nclipboard [clipboard get]
464 manyset [errexpect-catch {
465 set clipboard $nclipboard
467 .ctrl.acquire.resframe.res configure \
468 -background blue -disabledforeground black -font $acqdeffont \
469 -state disabled -command {} \
470 -text " acquired ok "
471 }] failed emsg lno ei
473 set line [lindex [split $ei "\n"] 0]
474 puts stderr "clipboard parsing failed: line $lno: $emsg\n $line"
475 regsub -all {.{18}} "bad: [string trim $emsg]: \"$line\"" "&\n" ewrap
477 .err_acquire.emsg.text delete 0.0 end
478 .err_acquire.emsg.text insert end "at line $lno: $emsg"
480 .err_acquire.text.text delete 0.0 end
481 .err_acquire.text.text insert end $nclipboard
483 .err_acquire.text.text tag add error $lno.0 $lno.end
484 .err_acquire.text.text tag configure error \
485 -background red -foreground white
486 .err_acquire.text.text see $lno.0
488 .ctrl.acquire.resframe.res configure \
489 -background red -foreground white -font fixed \
490 -state normal -command {wm deiconify .err_acquire} \
499 #---------- main program ----------
503 httpclientsetup where-vessels