-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.pl
81 lines (66 loc) · 1.78 KB
/
graph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env perl
use strict;
use warnings;
use File::Slurp;
use JSON;
use GraphViz;
use v5.16;
my %combined;
my %graph;
for my $f (<zones/*.json>) {
my $contents = read_file($f);
my $data = JSON::decode_json($contents);
for my $type (qw/loc obj/) {
$combined{$type} = {%{$combined{$type} || {}}, %{$data->{$type} || {}}};
}
}
while (my ($k, $v) = each %{$combined{loc}}) {
while (my ($dir, $exit) = each %{$v->{exits} || {}}) {
if ($exit =~ s/^\^//) {
if (!$combined{obj}{$exit}) {
warn "MISSING DEST: $k";
next;
}
my $dest_loc = $combined{obj}{$exit}{location};
$dest_loc =~ s/.+://;
$combined{loc}{$k}{exits}{$dir} = $dest_loc;
}
}
$graph{$k} = $combined{loc}{$k}{exits};
}
my $count = 1;
my %opp = (
n => 's',
s => 'n',
e => 'w',
w => 'e',
u => 'd',
d => 'u',
);
my %unseen = map {; $_ => 1} keys %graph;
while (scalar keys(%unseen)) {
my $node = (keys %unseen)[0];
my $g = GraphViz->new(layout => 'fdp', directed => 0);
$g->add_node($node, cluster => $count);
my %subgraph;
my $recur;
$recur = sub {
my ($node, $from) = @_;
return unless delete($unseen{$node});
$g->add_edge($from => $node) if $from;
while (my ($exit, $dest) = each $graph{$node}) {
next unless length($exit) == 1; # skip diagonals for now
if (($graph{$dest}{$opp{$exit}}||'') eq $node) {
no warnings 'recursion';
$recur->($graph{$node}{$exit}, $node);
}
}
};
$recur->($node);
open my $fh, '>', "$count.png";
binmode $fh;
print $fh $g->as_png;
close $fh;
warn "Wrote to $count.png\n";
++$count;
}