-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pl
193 lines (173 loc) · 4.91 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#Includes.
use strict;
use warnings;
use GD::Simple;
use FindBin;
use lib "$FindBin::Bin/lib";
use Point;
use Wall;
#File manipulation.
my @settings;
open("filedata", "<maze.settings") or die "Couldn't open file maze.settings, $!";
while(<filedata>) {
@settings = split(' ', $_);
last;
}
close("filedata") || die "Couldn't close maze.settings properly";
my $setsize = @settings;
if ($setsize < 4) {
die "Number of parameters should be at least 4, $setsize!\n";
}
#Resolution and settings
my $EDGE_LONG = $settings[0];
my $EDGE_MULTIPLIER = $settings[1];
my $IMAGE_WIDTH = ($EDGE_LONG * $EDGE_MULTIPLIER) + 1;
my $IMAGE_HEIGHT = $IMAGE_WIDTH;
#Generate grid.
my @cells; #Cells with unique ids.
my @edges; #Not used walls.
my @vmatrix; #Vertical walls.
my @hmatrix; #Horizontal walls.
my $idc = 0; #Counter of id.
for (my $i = 0; $i < $IMAGE_WIDTH / $EDGE_LONG; ++$i) {
for (my $j = 0; $j < $IMAGE_HEIGHT / $EDGE_LONG; ++$j) {
$vmatrix[$i][$j] = Point->new(x => $i * $EDGE_LONG, y => $j * $EDGE_LONG);
$hmatrix[$i][$j] = Point->new(x => $i * $EDGE_LONG, y => $j * $EDGE_LONG);
$edges[$i][$j][0] = Wall->new(x => $i, y => $j, type => 0); #0 represents the vertical wall.
$edges[$i][$j][1] = Wall->new(x => $i, y => $j, type => 1); #1 represents the hotizontal wall.
$cells[$i][$j] = $idc++; #Ascribe id to the cell.
}
}
#Remove walls.
while (1) {
#Check how many ids are there.
#If more than one, continue.
my $idxl = scalar(@cells);
my $idyl = scalar(@{$cells[0]});
my $closeLoop = 1;
for (my $i = 0; $i < $idxl; ++$i) {
for (my $j = 0; $j < $idyl; ++$j) {
if ($cells[$i][$j] != $cells[0][0]) {
$closeLoop = 0;
last;
}
}
}
while (!$closeLoop) {
my $closeInnerLoop = 1;
#Rand x.
my $xl = scalar(@edges);
my $i = int(rand($xl));
#If there are only 2 rows (top and bot).
if ($xl == 2) {
$closeLoop = 1;
last;
}
#Rand y.
my $yl = scalar(@{$edges[$i]});
my $j = int(rand($yl));
#Rand type.
my $tl = 2;
my $it = 0;
if ($edges[$i][$j][0]->getType() == -1) {
$it = 1;
$tl = 1;
}
if ($edges[$i][$j][1]->getType() == -1) {
$it = 0;
$tl = 1;
}
if ($tl == 2) {
$it = int(rand($tl)); #Rand which one.
}
my $t = $edges[$i][$j][$it]->getType(); # 0 - vertical / 1 - horizontal wall
#Set proper x and y, new id and old id.
my $bx = $edges[$i][$j][$it]->getX();
my $by = $edges[$i][$j][$it]->getY();
my $newID = $cells[$bx][$by];
my $oldID = -1;
if ($t == 0) {
# $by stays the same, and <- $bx -> check cell(left, right)
if ($bx == 0 || $bx == ($idxl-1) || ($cells[$bx-1][$by] == $cells[$bx][$by])) {
$closeInnerLoop = 0; #We have the same id in these cells, repeat loop.
} else {
$oldID = $cells[$bx-1][$by];
#print("Removing wall(v) \$bx=$bx \$by=$by\n");
$vmatrix[$bx][$by] = Point->new(x => -1, y => -1);
}
} else {
# $bx stays the same, and <- $by -> check cell(top, bot)
if ($by == 0 || $by == ($idyl-1) || ($cells[$bx][$by-1] == $cells[$bx][$by])) {
$closeInnerLoop = 0; #We have the same id in these cells, repeat loop.
} else {
$oldID = $cells[$bx][$by-1];
$hmatrix[$bx][$by] = Point->new(x => -1, y => -1);
}
}
#There is only one type of wall (v/h).
if ($tl == 1) {
#Delete whole column.
if ($yl == 2) {
splice(@edges, $i, 1);
}
#Delete last wall.
else {
splice(@{$edges[$i]}, $j, 1);
}
}
#There are two walls (v and h).
else {
$edges[$i][$j][$t]->setType(-1);
}
if ($closeInnerLoop) {
# Set new id to old ids.
for (my $k = 0; $k < $idxl; ++$k) {
for (my $l = 0; $l < $idyl; ++$l) {
if ($cells[$k][$l] == $oldID) {
$cells[$k][$l] = $newID;
}
}
}
last; #Break inner loop.
}
}
if ($closeLoop) {
last;
}
}
# Draw walls in image.
my $img = GD::Simple->new($IMAGE_WIDTH, $IMAGE_HEIGHT);
$img->fgcolor($settings[2]);
$img->bgcolor($settings[3]);
$img->rectangle(0, 0, $IMAGE_WIDTH, $IMAGE_HEIGHT);
my $size = scalar(@vmatrix);
for (my $i = 0; $i < $size; ++$i)
{
my $sv = scalar(@{$vmatrix[$i]});
for (my $j = 0; $j < $sv; ++$j) {
if ($vmatrix[$i][$j]->getX() != -1) {
$img->moveTo($vmatrix[$i][$j]->getX(), $vmatrix[$i][$j]->getY());
$img->lineTo($vmatrix[$i][$j]->getX(), $vmatrix[$i][$j]->getY() + $EDGE_LONG);
}
}
my $sh = scalar(@{$hmatrix[$i]});
for (my $j = 0; $j < $sh; ++$j) {
if ($hmatrix[$i][$j]->getX() != -1) {
$img->moveTo($hmatrix[$i][$j]->getX(), $hmatrix[$i][$j]->getY());
$img->lineTo($hmatrix[$i][$j]->getX() + $EDGE_LONG, $hmatrix[$i][$j]->getY());
}
}
}
# Draw start and end.
my $NEW_EDGE_LONG = $EDGE_LONG / 2;
$img->fgcolor('black');
$img->bgcolor('white');
$img->rectangle(2, 2, $NEW_EDGE_LONG - 2, $NEW_EDGE_LONG - 2);
my $corner = $IMAGE_WIDTH - $NEW_EDGE_LONG;
$img->fgcolor('white');
$img->bgcolor('black');
$img->rectangle($corner + 2, $corner + 2, $corner + $NEW_EDGE_LONG - 2, $corner + $NEW_EDGE_LONG - 2);
# Convert into png data.
open my $out, '>', 'maze.png' or die;
binmode $out;
print $out $img->png;