10 my $widists= Graph::Undirected->new();
11 my $wiarchs= Graph::Undirected->new();
19 my $dbdists= Graph::Undirected->new();
23 sub perr ($$) { print STDERR "$_[0]: $_[1]\n"; $msgcount{$_[0]}++; }
24 sub warning ($) { perr("warning",$_[0]); }
25 sub error ($) { perr("error", $_[0]); }
26 sub change ($) { perr("change", $_[0]); }
28 if ($ARGV[0] eq '--debug') {
30 open DEBUG, ">&STDOUT" or die $!;
33 open DEBUG, ">/dev/null" or die $!;
40 my $tp= (0+$x ^ 0+$y) & 1;
41 defined $parity or $parity=$tp;
42 $tp==$parity or warning("line $.: parity error $x,$y is $tp not $parity");
44 $winode2lines{$n}{$.}++;
48 sub parse_yppedia_map () {
49 # We don't even bother with tag soup; instead we do line-oriented parsing.
53 s/^\s*//; chomp; s/\s+$//; s/\s+/ /g;
55 s/\{\{Chart\ style\|[^{}]*\}\}//g;
56 next unless m/\{\{/; # only interested in chart template stuff
58 my ($x,$y, $arch,$island,$solid,$dirn);
59 my $nn= sub { return nn_xy($x,$y) };
62 m/^\{\{ chart\ label \|(\d+)\|(\d+)\| .*
63 \'\[\[ [^][\']* \| (\S+)\ archipelago \]\]\'*\}\}$/xi) {
64 printf DEBUG "%2d,%-2d arch %s\n", $x,$y,$arch;
65 push @wiarchlabels, [ $x,$y,$arch ];
66 } elsif (($x,$y,$island) =
67 m/^\{\{ chart\ island\ icon \|(\d+)\|(\d+)\|
68 ([^| ][^|]*[^| ]) \| .*\}\}$/xi) {
70 $wiisland2node{$island}= $n;
71 $winode2island{$n}= $island;
72 $widists->add_vertex($n);
73 $wiarchs->add_vertex($n);
74 #print "\$g->add_vertex('$n');\n";
75 printf DEBUG "%2d,%-2d island %s\n", $x,$y,$island;
76 } elsif (($solid,$x,$y,$dirn) =
77 m/^\{\{ chart\ league((?:\ solid)?) \|(\d+)\|(\d+)\|
78 ([-\/\\o]) \| .*\}\}$/xi) {
81 my ($bx,$by) = ($x,$y);
82 if ($dirn eq '-') { $bx+=2; }
83 elsif ($dirn eq '\\') { $bx++; $by++; }
84 elsif ($dirn eq '/') { $x++; $by++; }
87 $widists->add_weighted_edge($nn->(), nn_xy($bx,$by), 1);
88 $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
89 $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
90 #print "\$g->add_edge('".$nn->()."','".nn_xy($bx,$by)."');\n" if $solid;
92 printf DEBUG "%2d,%-2d league %-6s %s\n", $x,$y,
93 $solid?'solid':'dotted', $dirn;
95 m/^\{\{ chart\ head \}\}$/xi
99 warning("line $.: ignoring incomprehensible: $_");
104 sub parse_database_map () {
106 $sth= $dbh->prepare('SELECT islandname, archipelago FROM islands');
108 while ($row= $sth->fetchrow_hashref) {
109 print DEBUG "database-island $row->{'islandname'}".
110 " $row->{'archipelago'}\n";
111 $dbisland2arch{$row->{'islandname'}}= $row->{'archipelago'};
113 $sth= $dbh->prepare('SELECT dist, a.islandname a, b.islandname b
115 JOIN islands AS a ON dists.aiid==a.islandid
116 JOIN islands AS b ON dists.biid==b.islandid');
118 while ($row= $sth->fetchrow_hashref) {
119 $dbdists->add_weighted_edge($row->{'a'}, $row->{'b'}, $row->{'dist'});
123 sub process_yppedia_graphs () {
124 # Prune the LP database by eliminating boring intermediate vertices
125 foreach my $delete ($widists->vertices()) {
126 next if exists $winode2island{$delete};
127 my @neigh= $widists->neighbours($delete);
128 next unless @neigh==2;
129 # my @aneigh= $wiarchs->has_vertex($delete)
130 # ? $wiarchs->neighbours($delete) : ();
131 # next unless @aneigh==0 || @aneigh==2;
133 map { $weight += $widists->get_edge_weight($delete, $_) } @neigh;
134 $widists->add_weighted_edge(@neigh, $weight);
135 $widists->delete_vertex($delete);
136 printf DEBUG "%-5s elide %5s %-5s %2d\n", $delete, @neigh, $weight;
139 # Check that it's connected.
140 foreach my $cc ($widists->connected_components()) {
141 next if 2*@$cc > $widists->vertices();
142 my $m= "disconnected league point(s):";
143 foreach my $n (@$cc) {
144 $m .= "\n LP $n, def. yppedia line(s): ".
145 join(',', sort keys %{ $winode2lines{$n} });
150 # Compute all-pairs-shortest-paths on dist, which is the
151 # actual distances between all LPs.
152 my $wialldists= $widists->APSP_Floyd_Warshall();
154 # Assign archipelago labels to groups of islands
155 foreach my $label (@wiarchlabels) {
156 my ($ax,$ay,$arch) = @$label;
157 my $best_ccmulti= -1;
160 # print DEBUG "$ax,$ay arch-island-search $arch\n";
161 $ay += 1; $ax += 2; # coords are rather to the top left of label
162 foreach my $vertex ($wiarchs->vertices()) {
163 next unless exists $winode2island{$vertex};
164 my $ccix= $wiarchs->connected_component_by_vertex($vertex);
165 my @cc= $wiarchs->connected_component_by_index($ccix);
166 my $ccmulti= @cc > 1;
167 my ($vx,$vy) = split /,/, $vertex;
168 my $d2= ($vx-$ax)*($vx-$ax) + ($vy-$ay)*($vy-$ay);
169 my $cmp= $ccmulti <=> $best_ccmulti
171 printf DEBUG "%2d,%-2d arch-island-search %5s d2=%4d ccix=%-2d".
172 " cc=%2d ccmulti=%d cmp=%-2d %s\n",
173 $ax,$ay, $vertex, $d2, $ccix, scalar(@cc), $ccmulti, $cmp,
174 $winode2island{$vertex};
175 next unless $cmp > 0;
178 $best_ccmulti= $ccmulti;
180 die 'no island vertices?!' unless defined $best_n;
181 printf DEBUG "%2d,%-2d arch-island-select %-5s d2=%-2d %-10s %s\n",
182 $ax,$ay, $best_n, $best_d2, $arch, $winode2island{$best_n};
183 my $ccix= $wiarchs->connected_component_by_vertex($best_n);
184 my $desc= join "\n", map {
185 my $in= $winode2island{$_};
186 " LP $_". (defined $in ? ", $in" : "");
187 } sort $wiarchs->connected_component_by_index($ccix);
189 if (exists $wiccix2arch{$ccix}) {
190 error("archipelago determination failed, wrongly merged:\n".
191 " archipelago $arch\n".
192 " archipelago $wiccix2arch{$ccix}\n".
196 $wiccix2arch{$ccix}= $arch;
197 # print "$ccix $arch ::\n$desc\n";
200 # Assign islands not labelled above to archipelagoes.
202 # We do this by, for each connected component (set of islands
203 # linked by purchaseable charts), searching for the nearest other
204 # connected component which has already been assigned an arch.
205 # `Nearest' means shortest distance of unpurchaseable charts, in
208 # fixme need some hints
210 # we need only consider vertices which weren't `boring intermediate
211 # vertices' (removed during optimisation as being of order 2)
212 my @ccs_useful= map {
213 [ grep { $widists->has_vertex($_) } @$_ ]
214 } $wiarchs->connected_components();
216 foreach my $sourceccix (0..$#ccs_useful) {
217 next if defined $wiccix2arch{$sourceccix};
219 my $sourcecc= $ccs_useful[$sourceccix];
220 my @islandnodes= grep { $winode2island{$_} } @$sourcecc;
221 next unless @islandnodes; # don't care, then
223 my $best_dist= 9999999;
225 foreach my $targetccix (0..$#ccs_useful) {
226 next unless defined $wiccix2arch{$targetccix}; # not helpful
227 my $targetcc= $ccs_useful[$targetccix];
228 foreach my $target (@$targetcc) {
229 foreach my $source (@$sourcecc) {
230 my $target_dist= $wialldists->path_length($target,$source);
231 next if $target_dist >= $best_dist;
232 $best_dist= $target_dist;
233 $best_target= $target;
237 # die "no possible target ?!" unless defined $best_target;
241 # foreach my $node (sort keys %winode2island) {
242 # my $island= $winode2island{$node};
243 # my $arch= winode2arch($node);
244 # next if defined $arch;
245 # my $ccix= $wiarchs->connected_component_by_vertex($node);
246 # my @cc= $wiarchs->connected_component_by_index($ccix);
247 # @cc= grep { defined $winode2island{$_} } @cc;
248 # # We search for the best:
249 # # - member of this connected component
253 sub winode2arch ($) {
255 my $ccix= $wiarchs->connected_component_by_vertex($node);
256 return $wiccix2arch{$ccix};
258 sub wiisland2arch ($) {
260 my $node= $wiisland2node{$island};
261 die "$island ?" unless defined $node;
262 return winode2arch($node);
265 sub compare_island_lists () {
266 foreach my $island (sort keys %dbisland2arch) {
267 my $node= $wiisland2node{$island};
268 if (!defined $node) {
269 error("would delete island: $island");
272 my $wiarch= winode2arch($node);
273 if (!defined $wiarch) {
274 error("island has no arch: $island");
277 my $dbarch= $dbisland2arch{$island};
278 if ($wiarch ne $dbarch) {
279 change("change archipelago from $dbarch to $wiarch".
280 " for island $island");
283 foreach my $island (sort keys %wiisland2node) {
284 my $dbarch= $dbisland2arch{$island};
285 if (!defined $dbarch) {
286 my $wiarch= wiisland2arch($island);
287 if (!defined $wiarch) {
288 error("new island has no arch: $island");
290 # We check arches of non-new islands above
292 change("new island in $wiarch: $island");
297 db_setocean('Midnight');
300 parse_database_map();
301 process_yppedia_graphs();
302 compare_island_lists();