chiark / gitweb /
Properly take ocean forcing from source-info
[ypp-sc-tools.db-test.git] / yarrg / yppedia-chart-parser
1 #!/usr/bin/perl
2
3 use strict (qw(vars));
4 use warnings;
5
6 use Graph::Undirected;
7 use Commods;
8 use CommodsDatabase;
9
10 my $ocean= 'Midnight';
11
12
13 my $widists= Graph::Undirected->new();
14 my $wiarchs= Graph::Undirected->new();
15 my @wiarchlabels;
16 my %wiisland2node;
17 my %winode2island;
18 my %winode2lines;
19 my %wiccix2arch;
20
21 my $dbdists= Graph::Undirected->new();
22 my %dbisland2arch;
23
24 my %msgcount;
25 sub perr ($$) { print STDERR "$_[0]: $_[1]\n"; $msgcount{$_[0]}++; }
26 sub warning ($) { perr("warning",$_[0]); }
27 sub error   ($) { perr("error",  $_[0]); }
28 sub change  ($) { perr("change", $_[0]); }
29
30 if (@ARGV && $ARGV[0] eq '--debug') {
31     shift @ARGV;
32     open DEBUG, ">&STDOUT" or die $!;
33     select(DEBUG); $|=1;
34 } else {
35     open DEBUG, ">/dev/null" or die $!;
36 }
37 select(STDOUT); $|=1;
38
39 my $parity;
40 sub nn_xy ($$) {
41     my ($x,$y) = @_;
42     my $tp= (0+$x ^ 0+$y) & 1;
43     defined $parity or $parity=$tp;
44     $tp==$parity or warning("line $.: parity error $x,$y is $tp not $parity");
45     my $n= "$_[0],$_[1]";
46     $winode2lines{$n}{$.}++;
47     return $n;
48 }
49
50 sub parse_yppedia_map () {
51     # We don't even bother with tag soup; instead we do line-oriented parsing.
52
53     while (<>) {
54         s/\<--.*--\>//g;
55         s/^\s*//; chomp; s/\s+$//; s/\s+/ /g;
56         s/\<\/?(?:b|em)\>//g;
57         s/\{\{Chart\ style\|[^{}]*\}\}//g;
58         next unless m/\{\{/; # only interested in chart template stuff
59
60         my ($x,$y, $arch,$island,$solid,$dirn);
61         my $nn= sub { return nn_xy($x,$y) };
62     
63         if (($x,$y,$arch) =
64             m/^\{\{ chart\ label \|(\d+)\|(\d+)\| .*
65                     \'\[\[ [^][\']* \| (\S+)\ archipelago \]\]\'*\}\}$/xi) {
66             printf DEBUG "%2d,%-2d arch %s\n", $x,$y,$arch;
67             push @wiarchlabels, [ $x,$y,$arch ];
68         } elsif (($x,$y,$island) =
69             m/^\{\{ chart\ island\ icon \|(\d+)\|(\d+)\|
70                     ([^| ][^|]*[^| ]) \| .*\}\}$/xi) {
71             my $n= $nn->();
72             $wiisland2node{$island}= $n;
73             $winode2island{$n}= $island;
74             $widists->add_vertex($n);
75             $wiarchs->add_vertex($n);
76 #print "\$g->add_vertex('$n');\n";
77             printf DEBUG "%2d,%-2d island %s\n", $x,$y,$island;
78         } elsif (($solid,$x,$y,$dirn) =
79             m/^\{\{ chart\ league((?:\ solid)?) \|(\d+)\|(\d+)\|
80                     ([-\/\\o]) \| .*\}\}$/xi) {
81             next if $dirn eq 'o';
82
83             my ($bx,$by) = ($x,$y);
84             if ($dirn eq '-') { $bx+=2; }
85             elsif ($dirn eq '\\') { $bx++; $by++; }
86             elsif ($dirn eq '/') { $x++; $by++; }
87             else { die; }
88
89             $widists->add_weighted_edge($nn->(), nn_xy($bx,$by), 1);
90             $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
91             $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
92 #print "\$g->add_edge('".$nn->()."','".nn_xy($bx,$by)."');\n" if $solid;
93
94             printf DEBUG "%2d,%-2d league %-6s %s\n", $x,$y,
95                 $solid?'solid':'dotted', $dirn;
96         } elsif (
97             m/^\{\{ chart\ head \}\}$/xi
98                  ) {
99             next;
100         } else {
101             warning("line $.: ignoring incomprehensible: $_");
102         }
103     }
104 }
105
106 sub parse_database_map () {
107     my ($row,$sth);
108     $sth= $dbh->prepare('SELECT islandname, archipelago FROM islands');
109     $sth->execute();
110     while ($row= $sth->fetchrow_hashref) {
111         print DEBUG "database-island $row->{'islandname'}".
112                      " $row->{'archipelago'}\n";
113         $dbisland2arch{$row->{'islandname'}}= $row->{'archipelago'};
114     }
115     $sth= $dbh->prepare('SELECT dist, a.islandname a, b.islandname b
116                                 FROM dists
117                                 JOIN islands AS a ON dists.aiid==a.islandid
118                                 JOIN islands AS b ON dists.biid==b.islandid');
119     $sth->execute();
120     while ($row= $sth->fetchrow_hashref) {
121         $dbdists->add_weighted_edge($row->{'a'}, $row->{'b'}, $row->{'dist'});
122     }
123 }                        
124
125 sub process_yppedia_graphs () {
126
127     # Prune the LP database by eliminating boring intermediate vertices
128     #
129     foreach my $delete ($widists->vertices()) {
130         next if exists $winode2island{$delete};
131         my @neigh= $widists->neighbours($delete);
132         next unless @neigh==2;
133 #       my @aneigh= $wiarchs->has_vertex($delete)
134 #           ? $wiarchs->neighbours($delete) : ();
135 #       next unless @aneigh==0 || @aneigh==2;
136         my $weight= 0;
137         map { $weight += $widists->get_edge_weight($delete, $_) } @neigh;
138         $widists->add_weighted_edge(@neigh, $weight);
139         $widists->delete_vertex($delete);
140         printf DEBUG "%-5s elide %5s %-5s %2d\n", $delete, @neigh, $weight;
141     }
142
143     # Check that it's connected.
144     #
145     foreach my $cc ($widists->connected_components()) {
146         next if 2*@$cc > $widists->vertices();
147         my $m= "disconnected league point(s):";
148         foreach my $n (@$cc) {
149             $m .= "\n    LP $n, def. yppedia line(s): ".
150                 join(',', sort keys %{ $winode2lines{$n} });
151         }
152         warning($m);
153     }
154
155     # Assign archipelagoes according to the source-info file
156     #
157     foreach my $arch (sort keys %{ $oceans{$ocean} }) {
158         foreach my $islename (sort keys %{ $oceans{$ocean}{$arch} }) {
159             my $islenode= $wiisland2node{$islename};
160             defined $islenode or
161                 error("island $islename in source-info but not in WP map");
162             my $ccix= $wiarchs->connected_component_by_vertex($islenode);
163             my $oldarch= $wiccix2arch{$ccix};
164             error("island $islename in $arch in source-info".
165                   " connected to $oldarch as well")
166                 if defined $oldarch && $oldarch ne $arch;
167             printf DEBUG "%-5s force-island-arch cc%-2d %-10s %s\n",
168                 $islenode, $ccix, $arch, $islename;
169             $wiccix2arch{$ccix}= $arch;
170         }
171     }
172
173     # Compute all-pairs-shortest-paths on dist, which is the
174     # actual distances between all LPs.
175     #
176     my $wialldists= $widists->APSP_Floyd_Warshall();
177
178     # Assign archipelago labels to groups of islands
179     #
180     foreach my $label (@wiarchlabels) {
181         my ($ax,$ay,$arch) = @$label;
182         my $best_ccmulti= -1;
183         my $best_d2= 0;
184         my $best_n;
185 #       print DEBUG "$ax,$ay arch-island-search $arch\n";
186         $ay += 1;  $ax += 2;  # coords are rather to the top left of label
187         foreach my $vertex ($wiarchs->vertices()) {
188             next unless exists $winode2island{$vertex};
189             my $ccix= $wiarchs->connected_component_by_vertex($vertex);
190             my @cc= $wiarchs->connected_component_by_index($ccix);
191             my $ccmulti= @cc > 1;
192             my ($vx,$vy) = split /,/, $vertex;
193             my $d2= ($vx-$ax)*($vx-$ax) + ($vy-$ay)*($vy-$ay);
194             my $cmp= $ccmulti <=> $best_ccmulti
195                 ||   $best_d2 <=> $d2;
196             printf DEBUG "%2d,%-2d arch-island-search %5s d2=%4d cc%-2d".
197                          " #cc=%2d ccmulti=%d cmp=%2d %s\n",
198                 $ax,$ay, $vertex, $d2, $ccix, scalar(@cc), $ccmulti, $cmp,
199                 $winode2island{$vertex};
200             next unless $cmp > 0;
201             $best_n=       $vertex;
202             $best_d2=      $d2;
203             $best_ccmulti= $ccmulti;
204         }
205         die 'no island vertices?!' unless defined $best_n;
206         my $ccix= $wiarchs->connected_component_by_vertex($best_n);
207         printf DEBUG
208             "%2d,%-2d arch-island-select %-5s d2=%4d cc%-2d     %-10s %s\n",
209             $ax,$ay, $best_n, $ccix, $best_d2, $arch, $winode2island{$best_n};
210         my $desc= join "\n", map {
211             my $in= $winode2island{$_};
212             "    LP $_". (defined $in ? ", $in" : "");
213         } sort $wiarchs->connected_component_by_index($ccix);
214
215         if (exists $wiccix2arch{$ccix} and $wiccix2arch{$ccix} ne $arch) {
216             error("archipelago determination failed, wrongly merged:\n".
217                   "    archipelago $arch\n".
218                   "    archipelago $wiccix2arch{$ccix}\n".
219                   $desc);
220             next;
221         }
222         $wiccix2arch{$ccix}= $arch;
223 #       print "$ccix $arch ::\n$desc\n";
224     }
225
226     # Assign islands not labelled above to archipelagoes.
227     #
228     # We do this by, for each connected component (set of islands
229     # linked by purchaseable charts), searching for the nearest other
230     # connected component which has already been assigned an arch.
231     # `Nearest' means shortest distance of unpurchaseable charts, in
232     # leagues.
233     #
234     # we need only consider vertices which weren't `boring intermediate
235     # vertices' (removed during optimisation as being of order 2)
236     my @ccs_useful= map {
237         [ grep { $widists->has_vertex($_) } @$_ ]
238     } $wiarchs->connected_components();
239
240     my @assignments;
241
242     foreach my $sourceccix (0..$#ccs_useful) {
243         next if defined $wiccix2arch{$sourceccix};
244         next unless $ccs_useful[$sourceccix];
245
246         my @sourcecc= $wiarchs->connected_component_by_index($sourceccix);
247         my @islandnodes= grep { $winode2island{$_} } @sourcecc;
248         next unless @islandnodes; # don't care, then
249
250         foreach my $islandnode (@islandnodes) {
251             printf DEBUG "%-5s arch-join-need cc%-2d             %s\n",
252                 $islandnode, $sourceccix, $winode2island{$islandnode};
253         }
254         my $best_dist= 9999999;
255         my ($best_target, $best_targetccix, $best_source);
256         foreach my $targetccix (0..$#ccs_useful) {
257             next unless defined $wiccix2arch{$targetccix}; # not helpful
258             next unless $ccs_useful[$targetccix];
259             foreach my $target ($wiarchs->
260                          connected_component_by_index($targetccix)) {
261                 foreach my $source (@sourcecc) {
262                     my $target_dist= $wialldists->path_length($target,$source);
263                     next unless defined $target_dist;
264                     next if $target_dist >= $best_dist;
265                     $best_dist= $target_dist;
266                     $best_source= $source;
267                     $best_target= $target;
268                     $best_targetccix= $targetccix;
269                 }
270             }
271         }
272         die "no possible target ?!" unless defined $best_target;
273
274         my $arch= $wiccix2arch{$best_targetccix};
275         my $best_island= $winode2island{$best_target};
276         printf DEBUG "%-5s arch-join-to %-5s dist=%2d cc%-2d  %-10s %s\n",
277             $best_source, $best_target, $best_dist,
278             $best_targetccix, $arch,
279             defined($best_island) ? $best_island : "-";
280
281         push @assignments, [ $sourceccix, $arch ];
282     }
283     foreach my $assign (@assignments) {
284         $wiccix2arch{$assign->[0]}= $assign->[1];
285     }
286 }
287
288 sub winode2arch ($) {
289     my ($node) = @_;
290     my $ccix= $wiarchs->connected_component_by_vertex($node);
291     return $wiccix2arch{$ccix};
292 }
293 sub wiisland2arch ($) {
294     my ($island) = @_;
295     my $node= $wiisland2node{$island};
296     die "$island ?" unless defined $node;
297     return winode2arch($node);
298 }
299
300 sub compare_island_lists () {
301     foreach my $island (sort keys %dbisland2arch) {
302         my $node= $wiisland2node{$island};
303         if (!defined $node) {
304             error("would delete island: $island");
305             next;
306         }
307         my $wiarch= winode2arch($node);
308         if (!defined $wiarch) {
309             error("island has no arch: $island");
310             next;
311         }
312         my $dbarch= $dbisland2arch{$island};
313         if ($wiarch ne $dbarch) {
314             change("change archipelago from $dbarch to $wiarch".
315                    " for island $island");
316         }
317     }
318     foreach my $island (sort keys %wiisland2node) {
319         my $dbarch= $dbisland2arch{$island};
320         if (!defined $dbarch) {
321             my $wiarch= wiisland2arch($island);
322             if (!defined $wiarch) {
323                 error("new island has no arch: $island");
324                 next;
325                 # We check arches of non-new islands above
326             }
327             change("new island in $wiarch: $island");
328         }
329     }
330 }
331
332 parse_info_serverside();
333 db_setocean($ocean);
334 db_connect();
335 parse_yppedia_map();
336 parse_database_map();
337 process_yppedia_graphs();
338 compare_island_lists();