3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
9 YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10 The YARRG website is covered by the GNU Affero GPL v3 or later, which
11 basically means that every installation of the website will let you
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Affero General Public License as
16 published by the Free Software Foundation, either version 3 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Affero General Public License for more details.
24 You should have received a copy of the GNU Affero General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28 are used without permission. This program is not endorsed or
29 sponsored by Three Rings.
32 This Mason component simply defines how to interpret capacities.
45 my @mv_names= qw(mass volume);
46 my @mv_units= qw(kg l);
48 my (@mv)= (undef,undef);
49 return ('',@mv) unless $string =~ m/\S/;
52 my ($signum,$signopstr)= (+1,undef);
55 my $last_signopstr= 'NONE';
57 my $canon_numeric= sub {
59 sprintf "%g%s", $val, $mv_units[$mvi];
62 my $parse_values= sub {
64 $debugf->("TERM VALUES '$_'");
69 expected_error("Initial term specifies".
70 " $mv_names[$mvi] more than once.")
74 expected_error("Cannot add or subtract mass to/from volume")
75 unless defined $mv[$mvi];
76 $mv[$mvi] += $signum * $val;
78 push @canon, $canon_numeric->($val,$mvi);
81 $debugf->("VALUE '$_'");
82 my $iqtyrex= '[1-9] \d{0,8}';
83 my $fqtyrex= '\d{1,9} \. \d{0,3} |' . $iqtyrex;
84 if (s/^( $fqtyrex ) \s* kg \s+ //xo) { $def->(0, $1 ); }
85 elsif (s/^( $fqtyrex ) \s* t \s+ //xo) { $def->(0, $1 * 1000.0 ); }
86 elsif (s/^( $fqtyrex ) \s* l \s+ //xo) { $def->(1, $1 ); }
87 elsif (s/^( $fqtyrex ) \s* kl \s+ //xo) { $def->(1, $1 * 1000.0 ); }
88 elsif (s/^( $iqtyrex ) \s* ([a-z ]+) \s+ //xo) {
89 my ($qty,$spec) = ($1,$2);
90 $debugf->("VALUE COMMOD $qty '$spec'");
92 $dbh->prepare("SELECT commodname,unitmass,unitvolume
93 FROM commods WHERE commodname LIKE ?");
94 my ($emsg,$commod,@umv)=
95 dbw_lookup_string($spec,$commodsth,1,0,0,
96 "No commodity or unit matches \`$spec'",
97 "Ambiguous commodity (or unit) \`$spec'",
99 expected_error($emsg) if defined $emsg;
100 $debugf->("VALUE COMMOD FOUND '$commod' @umv");
101 foreach my $mvi (0,1) {
102 next unless defined $mv[$mvi];
103 $mv[$mvi] += $signum * $qty * $umv[$mvi] * 0.001;
105 push @canon, sprintf "%d", $qty;
106 push @canon, $commod;
109 expected_error("Did not understand value \`$_'");
114 my $parse_term= sub {
116 $debugf->("TERM '$_' signum=$signum");
118 expected_error("empty term in capacity") unless m/\S/;
119 if (m/^\s*(\d{1,2}(?:\.\d{0,4})?)\%\s*$/) {
120 $debugf->("TERM PERCENT $1");
121 expected_error("percentage may not be first item")
123 my $pct= 100.0 + $signum * $1;
128 push @canon, sprintf "%g%%", $pct;
129 } elsif (!m/[^a-z]/) {
130 $debugf->("TERM NAME");
131 expected_error("Name (should be unit or commodity) \`$_'".
132 " without preceding quantity")
134 my $sth= $dbh->prepare("SELECT name,mass,volume".
135 " FROM vessels WHERE name LIKE ?");
136 my ($emsg,$ship,@smv)=
137 dbw_lookup_string($_,$sth,1,1,2,
138 "Ship name `$_' not understood.",
139 "Too many matching ship types.",
140 sub { "Ambiguous - could be $_[0]" });
141 expected_error($emsg) if defined $emsg;
142 $debugf->("TERM NAME SHIP '$ship' @smv");
152 while ($string =~ s/^(.*?)(\bminus\b|-|\bplus\b|\+)//) {
154 my @nextsign= $2 =~ m/^p|^\+/ ? (+1,'+') : (-1,'-');
156 $debugf->("GROUP S='$2'");
158 ($signum,$signopstr)= @nextsign;
159 push @canon, ($last_signopstr=$signopstr)
160 if $signopstr ne $last_signopstr;
162 $parse_term->($string);
164 my $canon= join ' ', @canon;
168 foreach my $mvi (0,1) {
169 next unless defined $mv[$mvi];
170 $canon .= ' '.$canon_numeric->($mv[$mvi], $mvi);
175 $debugf->("FINISHING canon='$canon'");
177 foreach my $mvi (0,1) {
178 next unless defined $mv[$mvi];
179 next if $mv[$mvi] >= 0;
180 expected_error(sprintf "%s limit is negative: %s",
181 ucfirst($mv_names[$mvi]), $canon_numeric->($mv[$mvi], $mvi));
184 return ($canon, @mv);