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();
155 foreach my $label (@wiarchlabels) {
156 my ($ax,$ay,$arch) = @$label;
159 # print DEBUG "$ax,$ay arch-island-search $arch\n";
160 $ay += 1; $ax += 2; # coords are rather to the top left of label
161 foreach my $vertex ($wiarchs->vertices()) {
162 next unless exists $winode2island{$vertex};
163 my $ccix= $wiarchs->connected_component_by_vertex($vertex);
164 my @cc= $wiarchs->connected_component_by_index($ccix);
165 my ($vx,$vy) = split /,/, $vertex;
166 my $d2= ($vx-$ax)*($vx-$ax) + ($vy-$ay)*($vy-$ay);
168 # "%2d,%-2d arch-island-search %s d2=%d ccix=%d cc=%d %s\n",
169 # $ax,$ay, $vertex, $d2, $ccix, scalar(@cc),
170 # $winode2island{$vertex};
172 next unless $d2 < $d2best;
176 die 'no island vertices?!' unless defined $best;
177 printf DEBUG "%2d,%-2d arch-island-select %-5s d2=%-2d %-10s %s\n",
178 $ax,$ay, $best, $d2best, $arch, $winode2island{$best};
179 my $ccix= $wiarchs->connected_component_by_vertex($best);
180 my $desc= join "\n", map {
181 my $in= $winode2island{$_};
182 " LP $_". (defined $in ? ", $in" : "");
183 } sort $wiarchs->connected_component_by_index($ccix);
185 if (exists $wiccix2arch{$ccix}) {
186 error("archipelago determination failed, wrongly merged:\n".
187 " archipelago $arch\n".
188 " archipelago $wiccix2arch{$ccix}\n".
192 $wiccix2arch{$ccix}= $arch;
193 # print "$ccix $arch ::\n$desc\n";
197 sub winode2arch ($) {
199 my $ccix= $wiarchs->connected_component_by_vertex($node);
200 return $wiccix2arch{$ccix};
202 sub wiisland2arch ($) {
204 my $node= $wiisland2node{$island};
205 die "$island ?" unless defined $node;
206 return winode2arch($node);
209 sub compare_island_lists () {
210 foreach my $island (sort keys %dbisland2arch) {
211 my $node= $wiisland2node{$island};
212 if (!defined $node) {
213 error("would delete island: $island");
216 my $wiarch= winode2arch($node);
217 if (!defined $wiarch) {
218 error("island has no arch: $island");
221 my $dbarch= $dbisland2arch{$island};
222 if ($wiarch ne $dbarch) {
223 change("change archipelago from $dbarch to $wiarch".
224 " for island $island");
227 foreach my $island (sort keys %wiisland2node) {
228 my $dbarch= $dbisland2arch{$island};
229 if (!defined $dbarch) {
230 my $wiarch= wiisland2arch($island);
231 if (!defined $wiarch) {
232 error("new island has no arch: $island");
234 # We check arches of non-new islands above
236 change("new island in $wiarch: $island");
241 db_setocean('Midnight');
244 parse_database_map();
245 process_yppedia_graphs();
246 compare_island_lists();