« blib/lib/Games/RolePlay/MapGen/Exporter/PNG.pm
↑ Summary
blib/lib/Games/RolePlay/MapGen/MapQueue.pm »
| 1 | # vi:set filetype=perl: | ||
| 2 | |||
| 3 | package Games::RolePlay::MapGen::Exporter::SVG; | ||
| 4 | |||
| 5 | use common::sense; | ||
| 6 | use Carp; | ||
| 7 | use Games::RolePlay::MapGen::Exporter::SVG::_GDSVG; | ||
| 8 | use Math::Trig qw(deg2rad); | ||
| 9 | |||
| 10 | 1; | ||
| 11 | |||
| 12 | # new {{{ | ||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | my $this = bless {o => {@_}}, $class; | ||
| 16 | |||
| 17 | return $this; | ||
| 18 | } | ||
| 19 | # }}} | ||
| 20 | # go {{{ | ||
| 21 | sub go { | ||
| 22 | my $this = shift; | ||
| 23 | my $opts = {@_}; | ||
| 24 | |||
| 25 | for my $k (keys %{ $this->{o} }) { | ||
| 26 | $opts->{$k} = $this->{o}{$k} if not exists $opts->{$k}; | ||
| 27 | } | ||
| 28 | |||
| 29 | croak "ERROR: fname is a required option for " . ref($this) . "::go()" unless $opts->{fname}; | ||
| 30 | croak "ERROR: _the_map is a required option for " . ref($this) . "::go()" unless ref($opts->{_the_map}); | ||
| 31 | |||
| 32 | my $map = $this->genmap($opts); | ||
| 33 | unless( $opts->{fname} eq "-retonly" ) { | ||
| 34 | open _MAP_OUT, ">$opts->{fname}" or die "ERROR: couldn't open $opts->{fname} for write: $!"; | ||
| 35 | print _MAP_OUT $map->svg; # the format should really be an option... at some point | ||
| 36 | close _MAP_OUT; | ||
| 37 | } | ||
| 38 | |||
| 39 | return $map; | ||
| 40 | } | ||
| 41 | # }}} | ||
| 42 | |||
| 43 | # gen_cell_size {{{ | ||
| 44 | sub gen_cell_size { | ||
| 45 | my $this = shift; | ||
| 46 | my $opts = shift; | ||
| 47 | |||
| 48 | if( $opts->{cell_size} ) { | ||
| 49 | die "ERROR: illegal cell size '$opts->{cell_size}'" unless $opts->{cell_size} =~ m/^(\d+)x(\d+)/; | ||
| 50 | $opts->{x_size} = $1; | ||
| 51 | $opts->{y_size} = $2; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | # }}} | ||
| 55 | # genmap {{{ | ||
| 56 | sub genmap { | ||
| 57 | my $this = shift; | ||
| 58 | my $opts = shift; | ||
| 59 | my $map = $opts->{_the_map}; | ||
| 60 | |||
| 61 | $this->gen_cell_size($opts); | ||
| 62 | |||
| 63 | my $gd = new Games::RolePlay::MapGen::Exporter::SVG::_GDSVG(1+($opts->{x_size} * @{$map->[0]}), 1+($opts->{y_size} * @$map)); | ||
| 64 | |||
| 65 | my $white = $gd->colorAllocate(0xff, 0xff, 0xff); | ||
| 66 | my $black = $gd->colorAllocate(0x00, 0x00, 0x00); | ||
| 67 | my $elgrey = $gd->colorAllocate(0xf5, 0xf5, 0xf5); # extremely light grey | ||
| 68 | my $vlgrey = $gd->colorAllocate(0xe5, 0xe5, 0xe5); # very light grey | ||
| 69 | my $lgrey = $gd->colorAllocate(0xcc, 0xcc, 0xcc); | ||
| 70 | my $dgrey = $gd->colorAllocate(0x60, 0x60, 0x60); | ||
| 71 | my $grey = $gd->colorAllocate(0x90, 0x90, 0x90); | ||
| 72 | my $blue = $gd->colorAllocate(0x00, 0x00, 0xbb); | ||
| 73 | my $red = $gd->colorAllocate(0xbb, 0x00, 0x00); | ||
| 74 | my $green = $gd->colorAllocate(0x00, 0xbb, 0x00); | ||
| 75 | my $purple = $gd->colorAllocate(0xff, 0x00, 0xff); | ||
| 76 | my $brown = $gd->colorAllocate(0xaa, 0x90, 0x00); | ||
| 77 | |||
| 78 | my $door_arc_color = $lgrey; | ||
| 79 | my $door_color = $brown; | ||
| 80 | my $wall_color = $black; | ||
| 81 | my $fog_tile_color = $lgrey; # fog-tile color | ||
| 82 | my $wall_tile_color = $dgrey; # wall-tile color | ||
| 83 | my $corridor_color = $white; # corridor-tile color | ||
| 84 | my $open_color1 = $elgrey; # tile edges | ||
| 85 | my $open_color2 = $vlgrey; # tile ticks | ||
| 86 | |||
| 87 | my $D = 5; # the border around debugging marks | ||
| 88 | my $B = 1; # the border around the filled rectangles for empty tiles | ||
| 89 | my $L = 1; # the length of the cell ticks in open borders | ||
| 90 | $L++; # $L is one less than it seems... | ||
| 91 | |||
| 92 | my ($dm, $dM) = (1, 4); # akin to L, but for doors (door minor horrizontal, door minor vertical and door major) | ||
| 93 | my ($sm, $sM) = (3, 8); # status mark dimensions | ||
| 94 | my ($wx, $wy) = ( $opts->{x_size}*2-$dM*4, $opts->{y_size}*2-$dM*4 ); # the width and height of the door-arcs (cell size) | ||
| 95 | |||
| 96 | my $am = $dm +1; # the arc displacement is just a little bigger so it doesn't overlap the doors... | ||
| 97 | |||
| 98 | my $oa = 45; # show doors open by this amount | ||
| 99 | my $do = $oa; # show doors the same amount? or a little bit different? | ||
| 100 | $do -= 10; # this looks kinda neat. | ||
| 101 | |||
| 102 | my $or = deg2rad( $do ); | ||
| 103 | my $sr = sin( $or ); # we'll be using this, kthx... | ||
| 104 | |||
| 105 | GRID: { | ||
| 106 | my $x = @{$map->[0]}*$opts->{x_size}; | ||
| 107 | my $y; | ||
| 108 | |||
| 109 | for my $i (0 .. @$map) { | ||
| 110 | $y = $i*$opts->{y_size}; | ||
| 111 | |||
| 112 | $gd->line( (0, $y) => ($x, $y) => $wall_color ); | ||
| 113 | } | ||
| 114 | |||
| 115 | $y = @$map*$opts->{y_size}; | ||
| 116 | for my $i (0 .. @{$map->[0]}) { | ||
| 117 | $x = $i*$opts->{x_size}; | ||
| 118 | |||
| 119 | my $r = $gd->line( ($x, 0) => ($x, $y) => $wall_color ); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | for my $i (0 .. $#$map) { | ||
| 124 | my $jend = $#{$map->[$i]}; | ||
| 125 | |||
| 126 | for my $j (0 .. $jend) { | ||
| 127 | my $t = $map->[$i][$j]; | ||
| 128 | my $xp = $j * $opts->{x_size}; # min x | ||
| 129 | my $yp = $i * $opts->{y_size}; # min y | ||
| 130 | my $Xp = $xp + $opts->{x_size}; # max x | ||
| 131 | my $Yp = $yp + $opts->{y_size}; # max y | ||
| 132 | |||
| 133 | my $ns_l = (($Xp-$dM) - ($xp+$dM)); # for the doors... | ||
| 134 | my $ew_l = (($Yp-$dM) - ($yp+$dM)); | ||
| 135 | my $ns_h = int ($ns_l * $sr); | ||
| 136 | my $ew_h = int ($ew_l * $sr); | ||
| 137 | my $ns_b = int( sqrt( $ns_l ** 2 - $ns_h ** 2 ) ); | ||
| 138 | my $ew_b = int( sqrt( $ew_l ** 2 - $ew_h ** 2 ) ); | ||
| 139 | |||
| 140 | $opts->{t_cb}->() if exists $opts->{t_cb}; | ||
| 141 | |||
| 142 | $gd->line( $xp+$L, $yp => $Xp-$L, $yp, $open_color1 ) if $t->{od}{n} == 1; # == 1 doesn't match doors... | ||
| 143 | $gd->line( $xp, $yp+$L, => $xp, $Yp-$L, $open_color1 ) if $t->{od}{w} == 1; | ||
| 144 | |||
| 145 | if( $t->{od}{n} == 1 and $t->{od}{w} == 1 ) { # == 1 doesn't match doors | ||
| 146 | if( $t->{nb}{n}{od}{w} == 1 and $t->{nb}{w}{od}{n} == 1 ) { | ||
| 147 | $gd->line( $xp-$L, $yp => $xp+$L, $yp, $open_color2 ); | ||
| 148 | $gd->line( $xp, $yp-$L => $xp, $yp+$L, $open_color2 ); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | if( not $t->{type} ) { | ||
| 153 | $gd->filledRectangle( $xp+$B, $yp+$B => $Xp-$B, $Yp-$B, $wall_tile_color ); | ||
| 154 | |||
| 155 | } elsif( $t->{type} eq "fog" ) { | ||
| 156 | my ($_xm, $_ym) = (0,0); | ||
| 157 | my ($_Xm, $_Ym) = (0,0); | ||
| 158 | |||
| 159 | $_xm -- if (my $wf = ($t->{nb}{w}{type} and $t->{nb}{w}{type} eq "fog")); | ||
| 160 | $_ym -- if (my $nf = ($t->{nb}{n}{type} and $t->{nb}{n}{type} eq "fog")); | ||
| 161 | my $ef = ($t->{nb}{e}{type} and $t->{nb}{e}{type} eq "fog"); | ||
| 162 | my $sf = ($t->{nb}{s}{type} and $t->{nb}{s}{type} eq "fog"); | ||
| 163 | |||
| 164 | $_xm += 3 if not $wf and $t->{od}{w}; | ||
| 165 | $_ym += 3 if not $nf and $t->{od}{n}; | ||
| 166 | $_Xm -= 3 if not $ef and $t->{od}{e}; | ||
| 167 | $_Ym -= 3 if not $sf and $t->{od}{s}; | ||
| 168 | |||
| 169 | $gd->filledRectangle( $xp+$B+$_xm, $yp+$B+$_ym => $Xp-$B+$_Xm, $Yp-$B+$_Ym, $fog_tile_color ); | ||
| 170 | |||
| 171 | } elsif( $t->{type} eq "corridor" and $corridor_color != $white ) { | ||
| 172 | $gd->filledRectangle( $xp+$B, $yp+$B => $Xp-$B, $Yp-$B, $corridor_color ); | ||
| 173 | } | ||
| 174 | |||
| 175 | # NOTE: we never need to draw s and e doors, that just duplicates efforts | ||
| 176 | for my $dir (qw(n w)) { | ||
| 177 | if( ref(my $door = $t->{od}{$dir}) ) { | ||
| 178 | my @q1 = ( $dir eq "n" ? ($xp+$dM, $yp-$dm) : ($xp-$dm, $yp+$dM) ); | ||
| 179 | my @q2 = ( $dir eq "n" ? ($Xp-$dM, $yp+$dm) : ($xp+$dm, $Yp-$dM) ); | ||
| 180 | |||
| 181 | if( not $door->{secret} and not $door->{'open'} ) { | ||
| 182 | # Regular old unlocked, open, unstock, unhidden doors are these cute little rectangles. | ||
| 183 | |||
| 184 | $gd->filledRectangle( @q1 => @q2, $door_color ); | ||
| 185 | } | ||
| 186 | |||
| 187 | if( $door->{'open'} ) { | ||
| 188 | $gd->filledRectangle( @q1 => @q2, $white ); | ||
| 189 | } | ||
| 190 | |||
| 191 | # Here, we draw the diagonal line and arc indicating how the door opens. | ||
| 192 | my $oi = "$dir$door->{open_dir}{major}$door->{open_dir}{minor}"; | ||
| 193 | |||
| 194 | # draw the door line/arcs ... sadly, this is a 8 part if-else block {{{ | ||
| 195 | if( $oi eq "nne" ) { # same as above, but $Yp changes to $yp | ||
| 196 | $gd->arc( $Xp-$dM, $yp-$am, $wx, $wy, 180, 180+$oa, $door_arc_color ); | ||
| 197 | my @l = ( $Xp-$dM, $yp-$am => ($Xp-$dM)-$ns_b, $yp-$am-$ns_h ); | ||
| 198 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 199 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 200 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 201 | $gd->line(@l, $door_color); | ||
| 202 | |||
| 203 | } else { | ||
| 204 | $gd->line(@l, $door_arc_color); | ||
| 205 | } | ||
| 206 | |||
| 207 | } elsif( $oi eq "nse" ) { # same as above, but $Yp to $yp | ||
| 208 | $gd->arc( $Xp-$dM, $yp+$am, $wx, $wy, 180-$oa, 180, $door_arc_color ); | ||
| 209 | my @l = ( $Xp-$dM, $yp+$am => ($Xp-$dM)-$ns_b, $yp+$am+$ns_h ); | ||
| 210 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 211 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 212 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 213 | $gd->line(@l, $door_color); | ||
| 214 | |||
| 215 | } else { | ||
| 216 | $gd->line(@l, $door_arc_color); | ||
| 217 | } | ||
| 218 | |||
| 219 | } elsif( $oi eq "nnw" ) { # same as above, but $yp | ||
| 220 | $gd->arc( $xp+$dM, $yp-$am, $wx, $wy, 360-$oa, 360, $door_arc_color ); | ||
| 221 | my @l = ( $xp+$dM, $yp-$am => ($xp+$dM)+$ns_b, $yp-$am-$ns_h ); | ||
| 222 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 223 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 224 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 225 | $gd->line(@l, $door_color); | ||
| 226 | |||
| 227 | } else { | ||
| 228 | $gd->line(@l, $door_arc_color); | ||
| 229 | } | ||
| 230 | |||
| 231 | } elsif( $oi eq "nsw" ) { # same as above, but $yp | ||
| 232 | $gd->arc( $xp+$dM, $yp+$am, $wx, $wy, 360, 360+$oa, $door_arc_color ); | ||
| 233 | my @l = ( $xp+$dM, $yp+$am => ($xp+$dM)+$ns_b, $yp+$am+$ns_h ); | ||
| 234 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 235 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 236 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 237 | $gd->line(@l, $door_color); | ||
| 238 | |||
| 239 | } else { | ||
| 240 | $gd->line(@l, $door_arc_color); | ||
| 241 | } | ||
| 242 | |||
| 243 | } elsif( $oi eq "wen" ) { # same as above but $Xp to $xp | ||
| 244 | $gd->arc( $xp+$am, $yp+$dM, $wx, $wy, 90-$oa, 90, $door_arc_color ); | ||
| 245 | my @l = ( $xp+$am, $yp+$dM => $xp+$am+$ew_h, ($yp+$dM)+$ew_b ); | ||
| 246 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 247 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 248 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 249 | $gd->line(@l, $door_color); | ||
| 250 | |||
| 251 | } else { | ||
| 252 | $gd->line(@l, $door_arc_color); | ||
| 253 | } | ||
| 254 | |||
| 255 | } elsif( $oi eq "wwn" ) { # same as above but $Xp to $xp | ||
| 256 | $gd->arc( $xp-$am, $yp+$dM, $wx, $wy, 90, 90+$oa, $door_arc_color ); | ||
| 257 | my @l = ( $xp-$am, $yp+$dM => $xp-$am-$ew_h, ($yp+$dM)+$ew_b ); | ||
| 258 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 259 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 260 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 261 | $gd->line(@l, $door_color); | ||
| 262 | |||
| 263 | } else { | ||
| 264 | $gd->line(@l, $door_arc_color); | ||
| 265 | } | ||
| 266 | |||
| 267 | } elsif( $oi eq "wes" ) { # same as above, but $xp | ||
| 268 | $gd->arc( $xp+$am, $Yp-$dM, $wx, $wy, 270, 270+$oa, $door_arc_color ); | ||
| 269 | my @l = ( $xp+$am, $Yp-$dM => $xp+$am+$ew_h, ($Yp-$dM)-$ew_b ); | ||
| 270 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 271 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 272 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 273 | $gd->line(@l, $door_color); | ||
| 274 | |||
| 275 | } else { | ||
| 276 | $gd->line(@l, $door_arc_color); | ||
| 277 | } | ||
| 278 | |||
| 279 | } elsif( $oi eq "wws" ) { # same as above, but $xp | ||
| 280 | $gd->arc( $xp-$am, $Yp-$dM, $wx, $wy, 270-$oa, 270, $door_arc_color ); | ||
| 281 | my @l = ( $xp-$am, $Yp-$dM => $xp-$am-$ew_h, ($Yp-$dM)-$ew_b ); | ||
| 282 | if( $door->{'open'} and not $door->{secret} ) { | ||
| 283 | $gd->line(@l, $door_color); $l[1]+=1; $l[3]+=1; | ||
| 284 | $gd->line(@l, $door_color); $l[1]-=2; $l[3]-=2; | ||
| 285 | $gd->line(@l, $door_color); | ||
| 286 | |||
| 287 | } else { | ||
| 288 | $gd->line(@l, $door_arc_color); | ||
| 289 | } | ||
| 290 | |||
| 291 | } | ||
| 292 | # }}} | ||
| 293 | |||
| 294 | unless( $door->{'open'} ) { | ||
| 295 | if( $door->{locked} ) { | ||
| 296 | my @l1 = ( $dir eq "n" ? ($xp+$sM, $yp-$sm) : ($xp-$sm, $yp+$sM) ); | ||
| 297 | my @l2 = ( $dir eq "n" ? ($xp+$sM, $yp+$sm) : ($xp+$sm, $yp+$sM) ); | ||
| 298 | |||
| 299 | $gd->line( @l1 => @l2 => $red ); | ||
| 300 | } | ||
| 301 | |||
| 302 | if( $door->{stuck} ) { | ||
| 303 | my @l1 = ( $dir eq "n" ? ($Xp-$sM, $yp-$sm) : ($xp-$sm, $Yp-$sM) ); | ||
| 304 | my @l2 = ( $dir eq "n" ? ($Xp-$sM, $yp+$sm) : ($xp+$sm, $Yp-$sM) ); | ||
| 305 | |||
| 306 | $gd->line( @l1 => @l2 => $blue ); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | if( $t->{DEBUG_red_mark} ) { | ||
| 313 | $gd->filledRectangle( $xp+$D, $yp+$D => $Xp-$D, $Yp-$D, $red ); | ||
| 314 | } | ||
| 315 | |||
| 316 | if( $t->{DEBUG_blue_mark} ) { | ||
| 317 | $gd->filledRectangle( $xp+$D, $yp+$D => $Xp-$D, $Yp-$D, $blue ); | ||
| 318 | } | ||
| 319 | |||
| 320 | if( $t->{DEBUG_green_mark} ) { | ||
| 321 | $gd->filledRectangle( $xp+$D, $yp+$D => $Xp-$D, $Yp-$D, $green ); | ||
| 322 | } | ||
| 323 | |||
| 324 | if( $t->{DEBUG_purple_mark} ) { | ||
| 325 | $gd->filledRectangle( $xp+$D, $yp+$D => $Xp-$D, $Yp-$D, $purple ); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | for my $t (map(@$_, @$map)) { | ||
| 331 | for my $d (keys %{ $t->{od} }) { | ||
| 332 | if( ref( my $door = $t->{od}{$d} ) ) { | ||
| 333 | delete $door->{_drawn}; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | return $gd; | ||
| 339 | } | ||
| 340 | # }}} | ||
| 341 | |||
| 342 | __END__ |