10 my $ocean= 'Midnight';
13 my $widists= Graph::Undirected->new();
14 my $wiarchs= Graph::Undirected->new();
21 my $dbdists= Graph::Undirected->new();
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]); }
30 if (@ARGV && $ARGV[0] eq '--debug') {
32 open DEBUG, ">&STDOUT" or die $!;
35 open DEBUG, ">/dev/null" or die $!;
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");
46 $winode2lines{$n}{$.}++;
50 sub parse_yppedia_map () {
51 # We don't even bother with tag soup; instead we do line-oriented parsing.
55 s/^\s*//; chomp; s/\s+$//; s/\s+/ /g;
57 s/\{\{Chart\ style\|[^{}]*\}\}//g;
58 next unless m/\{\{/; # only interested in chart template stuff
60 my ($x,$y, $arch,$island,$solid,$dirn);
61 my $nn= sub { return nn_xy($x,$y) };
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) {
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) {
83 my ($bx,$by) = ($x,$y);
84 if ($dirn eq '-') { $bx+=2; }
85 elsif ($dirn eq '\\') { $bx++; $by++; }
86 elsif ($dirn eq '/') { $x++; $by++; }
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;
94 printf DEBUG "%2d,%-2d league %-6s %s\n", $x,$y,
95 $solid?'solid':'dotted', $dirn;
97 m/^\{\{ chart\ head \}\}$/xi
101 warning("line $.: ignoring incomprehensible: $_");
106 sub parse_database_map () {
108 $sth= $dbh->prepare('SELECT islandname, archipelago FROM islands');
110 while ($row= $sth->fetchrow_hashref) {
111 print DEBUG "database-island $row->{'islandname'}".
112 " $row->{'archipelago'}\n";
113 $dbisland2arch{$row->{'islandname'}}= $row->{'archipelago'};
115 $sth= $dbh->prepare('SELECT dist, a.islandname a, b.islandname b
117 JOIN islands AS a ON dists.aiid==a.islandid
118 JOIN islands AS b ON dists.biid==b.islandid');
120 while ($row= $sth->fetchrow_hashref) {
121 $dbdists->add_weighted_edge($row->{'a'}, $row->{'b'}, $row->{'dist'});
125 sub process_yppedia_graphs () {
127 # Prune the LP database by eliminating boring intermediate vertices
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;
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;
143 # Check that it's connected.
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} });
155 # Assign archipelagoes according to the source-info file
157 foreach my $arch (sort keys %{ $oceans{$ocean} }) {
158 foreach my $islename (sort keys %{ $oceans{$ocean}{$arch} }) {
159 my $islenode= $wiisland2node{$islename};
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;
173 # Compute all-pairs-shortest-paths on dist, which is the
174 # actual distances between all LPs.
176 my $wialldists= $widists->APSP_Floyd_Warshall();
178 # Assign archipelago labels to groups of islands
180 foreach my $label (@wiarchlabels) {
181 my ($ax,$ay,$arch) = @$label;
182 my $best_ccmulti= -1;
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
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;
203 $best_ccmulti= $ccmulti;
205 die 'no island vertices?!' unless defined $best_n;
206 my $ccix= $wiarchs->connected_component_by_vertex($best_n);
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);
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".
222 $wiccix2arch{$ccix}= $arch;
223 # print "$ccix $arch ::\n$desc\n";
226 # Assign islands not labelled above to archipelagoes.
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
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();
242 foreach my $sourceccix (0..$#ccs_useful) {
243 next if defined $wiccix2arch{$sourceccix};
244 next unless $ccs_useful[$sourceccix];
246 my @sourcecc= $wiarchs->connected_component_by_index($sourceccix);
247 my @islandnodes= grep { $winode2island{$_} } @sourcecc;
248 next unless @islandnodes; # don't care, then
250 foreach my $islandnode (@islandnodes) {
251 printf DEBUG "%-5s arch-join-need cc%-2d %s\n",
252 $islandnode, $sourceccix, $winode2island{$islandnode};
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;
272 die "no possible target ?!" unless defined $best_target;
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 : "-";
281 push @assignments, [ $sourceccix, $arch ];
283 foreach my $assign (@assignments) {
284 $wiccix2arch{$assign->[0]}= $assign->[1];
288 sub winode2arch ($) {
290 my $ccix= $wiarchs->connected_component_by_vertex($node);
291 return $wiccix2arch{$ccix};
293 sub wiisland2arch ($) {
295 my $node= $wiisland2node{$island};
296 die "$island ?" unless defined $node;
297 return winode2arch($node);
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");
307 my $wiarch= winode2arch($node);
308 if (!defined $wiarch) {
309 error("island has no arch: $island");
312 my $dbarch= $dbisland2arch{$island};
313 if ($wiarch ne $dbarch) {
314 change("change archipelago from $dbarch to $wiarch".
315 " for island $island");
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");
325 # We check arches of non-new islands above
327 change("new island in $wiarch: $island");
332 parse_info_serverside();
336 parse_database_map();
337 process_yppedia_graphs();
338 compare_island_lists();