| 1 |
root |
1.1 |
#!/opt/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
# tower of stars: missing craters? |
| 4 |
|
|
|
| 5 |
|
|
use strict; |
| 6 |
|
|
|
| 7 |
|
|
use Storable; |
| 8 |
|
|
use List::Util qw(max); |
| 9 |
|
|
|
| 10 |
|
|
use Gtk2; |
| 11 |
|
|
|
| 12 |
root |
1.4 |
our $VERSION = '1.0'; |
| 13 |
|
|
|
| 14 |
root |
1.1 |
#init Gtk2::Gdk; |
| 15 |
|
|
|
| 16 |
|
|
my $LIB = $ENV{CROSSFIRE_LIBDIR} |
| 17 |
|
|
or die "\$CROSSFIRE_LIBDIR must be set\n"; |
| 18 |
|
|
|
| 19 |
|
|
sub T (){ 32 } |
| 20 |
|
|
|
| 21 |
|
|
sub read_pak($) { |
| 22 |
|
|
my ($path) = @_; |
| 23 |
|
|
|
| 24 |
|
|
eval { |
| 25 |
|
|
-M "$path.pst" < -M $path |
| 26 |
|
|
&& Storable::retrieve "$path.pst" |
| 27 |
|
|
} or do { |
| 28 |
|
|
my %pak; |
| 29 |
|
|
|
| 30 |
|
|
open my $fh, "<:raw", $path |
| 31 |
|
|
or die "$_[0]: $!"; |
| 32 |
|
|
while (<$fh>) { |
| 33 |
|
|
my ($type, $id, $len, $path) = split; |
| 34 |
|
|
$path =~ s/.*\///; |
| 35 |
|
|
read $fh, $pak{$path}, $len; |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
Storable::nstore \%pak, "$path.pst"; |
| 39 |
|
|
|
| 40 |
|
|
\%pak |
| 41 |
|
|
} |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
|
sub read_smooth($) { |
| 45 |
|
|
my ($path) = @_; |
| 46 |
|
|
|
| 47 |
|
|
eval { |
| 48 |
|
|
-M "$path.pst" < -M $path |
| 49 |
|
|
&& Storable::retrieve "$path.pst" |
| 50 |
|
|
} or do { |
| 51 |
|
|
my %smooth; |
| 52 |
|
|
|
| 53 |
|
|
open my $fh, "<:raw", $path |
| 54 |
|
|
or die "$path: $!"; |
| 55 |
|
|
while (<$fh>) { |
| 56 |
|
|
next if /^\s*($|#)/; |
| 57 |
|
|
|
| 58 |
|
|
$smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
|
|
Storable::nstore \%smooth, "$path.pst"; |
| 62 |
|
|
|
| 63 |
|
|
\%smooth |
| 64 |
|
|
} |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
sub read_arch($;$) { |
| 68 |
|
|
my ($path, $cache) = @_; |
| 69 |
|
|
|
| 70 |
|
|
eval { |
| 71 |
|
|
$cache |
| 72 |
|
|
&& -M "$path.pst" < -M $path |
| 73 |
|
|
&& Storable::retrieve "$path.pst" |
| 74 |
|
|
} or do { |
| 75 |
|
|
my %arc; |
| 76 |
|
|
my ($more, $prev); |
| 77 |
|
|
|
| 78 |
|
|
open my $fh, "<:raw", $path |
| 79 |
|
|
or die "$path: $!"; |
| 80 |
|
|
|
| 81 |
|
|
my $parse_block; $parse_block = sub { |
| 82 |
|
|
my %arc = @_; |
| 83 |
|
|
|
| 84 |
|
|
while (<$fh>) { |
| 85 |
|
|
s/\s+$//; |
| 86 |
|
|
if (/^end$/i) { |
| 87 |
|
|
last; |
| 88 |
|
|
} elsif (/^arch (\S+)$/) { |
| 89 |
root |
1.3 |
push @{ $arc{inventory} }, $parse_block->(_name => $1); |
| 90 |
root |
1.4 |
} elsif (/^lore$/) { |
| 91 |
|
|
while (<$fh>) { |
| 92 |
|
|
last if /^endlore\s*$/i; |
| 93 |
|
|
$arc{lore} .= $_; |
| 94 |
|
|
} |
| 95 |
root |
1.1 |
} elsif (/^msg$/) { |
| 96 |
|
|
while (<$fh>) { |
| 97 |
|
|
last if /^endmsg\s*$/i; |
| 98 |
|
|
$arc{msg} .= $_; |
| 99 |
|
|
} |
| 100 |
|
|
} elsif (/^(\S+)\s*(.*)$/) { |
| 101 |
|
|
$arc{lc $1} = $2; |
| 102 |
|
|
} elsif (/^\s*($|#)/) { |
| 103 |
|
|
# |
| 104 |
|
|
} else { |
| 105 |
|
|
warn "$path: unparsable line '$_' in arch $arc{_name}"; |
| 106 |
|
|
} |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
\%arc |
| 110 |
|
|
}; |
| 111 |
|
|
|
| 112 |
|
|
while (<$fh>) { |
| 113 |
|
|
s/\s+$//; |
| 114 |
|
|
if (/^more$/i) { |
| 115 |
|
|
$more = $prev; |
| 116 |
|
|
} elsif (/^object (\S+)$/i) { |
| 117 |
|
|
my $name = $1; |
| 118 |
|
|
my $arc = $parse_block->(_name => $name); |
| 119 |
|
|
|
| 120 |
root |
1.4 |
if ($more) { |
| 121 |
|
|
$more->{more} = $arc; |
| 122 |
|
|
} else { |
| 123 |
|
|
$arc{$name} = $arc; |
| 124 |
|
|
} |
| 125 |
root |
1.1 |
|
| 126 |
|
|
$prev = $arc; |
| 127 |
|
|
$more = undef; |
| 128 |
|
|
} elsif (/^arch (\S+)$/i) { |
| 129 |
|
|
push @{ $arc{arch} }, $parse_block->(_name => $1); |
| 130 |
|
|
} elsif (/^\s*($|#)/) { |
| 131 |
|
|
# |
| 132 |
|
|
} else { |
| 133 |
|
|
warn "$path: unparseable top-level line '$_'"; |
| 134 |
|
|
} |
| 135 |
|
|
} |
| 136 |
|
|
|
| 137 |
|
|
undef $parse_block; # work around bug in perl not freeing $fh etc. |
| 138 |
|
|
|
| 139 |
|
|
Storable::nstore \%arc, "$path.pst" |
| 140 |
|
|
if $cache; |
| 141 |
|
|
|
| 142 |
|
|
\%arc |
| 143 |
|
|
} |
| 144 |
|
|
} |
| 145 |
|
|
|
| 146 |
|
|
my $arch = read_arch "$LIB/archetypes", 1; |
| 147 |
|
|
my $tile = read_pak "$LIB/crossfire.0"; |
| 148 |
|
|
my $smooth = read_smooth "$LIB/smooth"; |
| 149 |
|
|
|
| 150 |
|
|
sub tile($) { |
| 151 |
|
|
my $name = $_[0]; |
| 152 |
|
|
|
| 153 |
|
|
my $pb = $tile->{$name} |
| 154 |
|
|
or return; |
| 155 |
|
|
|
| 156 |
|
|
unless (ref $pb) { |
| 157 |
|
|
my $file = "/tmp/map2png.$$~"; |
| 158 |
|
|
|
| 159 |
|
|
open my $fh, ">:raw", $file |
| 160 |
|
|
or die "$file: $!"; |
| 161 |
|
|
print $fh $pb; |
| 162 |
|
|
close $fh; |
| 163 |
|
|
|
| 164 |
|
|
$pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file; |
| 165 |
|
|
unlink $file; |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
|
|
$pb |
| 169 |
|
|
} |
| 170 |
|
|
|
| 171 |
|
|
sub cfmap_render($;$) { |
| 172 |
|
|
my ($mapa, $mapname) = @_; |
| 173 |
|
|
|
| 174 |
|
|
my %meta; |
| 175 |
|
|
|
| 176 |
|
|
my ($mapx, $mapy); |
| 177 |
|
|
|
| 178 |
|
|
my $map = $meta{map} = []; |
| 179 |
|
|
|
| 180 |
|
|
for (@{ $mapa->{arch} }) { |
| 181 |
|
|
if ($_->{_name} eq "map") { |
| 182 |
|
|
$meta{info} = $_; |
| 183 |
|
|
|
| 184 |
|
|
$mapx = $_->{width} || $_->{x}; |
| 185 |
|
|
$mapy = $_->{height} || $_->{y}; |
| 186 |
|
|
} else { |
| 187 |
|
|
push @{ $map->[$_->{x}][$_->{y}] }, $_; |
| 188 |
|
|
|
| 189 |
|
|
# arch map is unreliable w.r.t. width and height |
| 190 |
|
|
$mapx = $_->{x} + 1 if $mapx <= $_->{x}; |
| 191 |
|
|
$mapy = $_->{y} + 1 if $mapy <= $_->{y}; |
| 192 |
|
|
#$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
| 193 |
|
|
#$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
| 194 |
|
|
} |
| 195 |
|
|
} |
| 196 |
|
|
|
| 197 |
|
|
$meta{width} = $mapx; |
| 198 |
|
|
$meta{height} = $mapy; |
| 199 |
|
|
|
| 200 |
|
|
my %draw_info; |
| 201 |
|
|
|
| 202 |
|
|
# first pass, gather face stacking order, border and corner info |
| 203 |
|
|
for my $x (0 .. $mapx - 1) { |
| 204 |
|
|
my $col = $map->[$x]; |
| 205 |
|
|
for my $y (0 .. $mapy - 1) { |
| 206 |
|
|
my $as = $col->[$y] || []; |
| 207 |
|
|
|
| 208 |
|
|
for my $layer (0 .. $#$as) { |
| 209 |
|
|
my $a = $as->[$layer]; |
| 210 |
|
|
|
| 211 |
root |
1.3 |
my $o = $arch->{$a->{_name}} |
| 212 |
root |
1.1 |
or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
| 213 |
|
|
|
| 214 |
root |
1.5 |
my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; |
| 215 |
|
|
my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
| 216 |
|
|
my $level = $smoothlevel ? $smoothlevel |
| 217 |
|
|
: $is_floor ? 0 |
| 218 |
|
|
: 256 + $layer; |
| 219 |
root |
1.1 |
|
| 220 |
root |
1.5 |
while ($o) { |
| 221 |
|
|
my $face = $a->{face} || $o->{face}; |
| 222 |
root |
1.1 |
|
| 223 |
root |
1.5 |
my $pb = tile $face |
| 224 |
|
|
or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
| 225 |
root |
1.1 |
|
| 226 |
|
|
my $mx = $x + $o->{x}; |
| 227 |
|
|
my $my = $y + $o->{y}; |
| 228 |
|
|
|
| 229 |
root |
1.2 |
last if 0 > $mx || $mx >= $mapx |
| 230 |
|
|
|| 0 > $my || $my >= $mapy; |
| 231 |
root |
1.1 |
|
| 232 |
|
|
# this is very ugly (some tiles are 32x33 or worse) |
| 233 |
|
|
my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
| 234 |
|
|
|
| 235 |
root |
1.5 |
if (my $sface = $smooth->{$face}) { |
| 236 |
|
|
$bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
| 237 |
root |
1.1 |
|
| 238 |
|
|
# full tile |
| 239 |
root |
1.3 |
$draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
| 240 |
root |
1.1 |
|
| 241 |
|
|
# borders |
| 242 |
root |
1.3 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; |
| 243 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; |
| 244 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; |
| 245 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; |
| 246 |
root |
1.1 |
|
| 247 |
|
|
# corners |
| 248 |
root |
1.3 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
| 249 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
| 250 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
| 251 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
| 252 |
root |
1.1 |
} |
| 253 |
|
|
|
| 254 |
|
|
my $dx = $bigface ? $o->{x} : 0; |
| 255 |
|
|
my $dy = $bigface ? $o->{y} : 0; |
| 256 |
|
|
|
| 257 |
root |
1.5 |
$draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
| 258 |
root |
1.1 |
|
| 259 |
|
|
$o = $o->{more}; |
| 260 |
|
|
} |
| 261 |
|
|
} |
| 262 |
|
|
} |
| 263 |
|
|
} |
| 264 |
|
|
|
| 265 |
|
|
my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
| 266 |
|
|
or die; |
| 267 |
|
|
$map_pb->fill (0x00000000); |
| 268 |
|
|
|
| 269 |
|
|
# second pass, render all the stuff |
| 270 |
|
|
for my $level (sort { $a <=> $b } keys %draw_info) { |
| 271 |
|
|
my $v = $draw_info{$level}; |
| 272 |
|
|
while (my ($sface, $info) = each %$v) { |
| 273 |
|
|
my $pb = tile $sface |
| 274 |
|
|
or die "no smooth face $sface\n"; |
| 275 |
|
|
|
| 276 |
|
|
while (my ($xy, $bits) = each %$info) { |
| 277 |
|
|
my ($x, $y) = split $;, $xy; |
| 278 |
|
|
|
| 279 |
|
|
next if $x < 0 || $x >= $mapx |
| 280 |
|
|
|| $y < 0 || $y >= $mapy; |
| 281 |
|
|
|
| 282 |
|
|
# bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB |
| 283 |
|
|
# X don't draw |
| 284 |
|
|
# F full tile draw with x|y bigface displacement |
| 285 |
|
|
# c maybe draw these corners |
| 286 |
|
|
# C do not draw these corners |
| 287 |
|
|
# b draw these borders |
| 288 |
|
|
|
| 289 |
|
|
if ($bits & 0x2000) { |
| 290 |
|
|
my $dx = (($bits >> 24) & 0xff) - 128; |
| 291 |
|
|
my $dy = (($bits >> 16) & 0xff) - 128; |
| 292 |
|
|
|
| 293 |
|
|
$pb->composite ($map_pb, |
| 294 |
|
|
$x * T, $y * T, |
| 295 |
|
|
T, T, |
| 296 |
|
|
($x - $dx) * T, ($y - $dy) * T, 1, 1, |
| 297 |
|
|
"nearest", |
| 298 |
|
|
255 |
| 299 |
|
|
); |
| 300 |
|
|
} |
| 301 |
|
|
|
| 302 |
|
|
unless ($bits & 0x1000) { |
| 303 |
|
|
my $border = $bits & 0xf; |
| 304 |
|
|
my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; |
| 305 |
|
|
|
| 306 |
|
|
$pb->composite ($map_pb, |
| 307 |
|
|
$x * T, $y * T, |
| 308 |
|
|
T, T, |
| 309 |
|
|
($x - $border) * T, $y * T, 1, 1, |
| 310 |
|
|
"nearest", |
| 311 |
|
|
255 |
| 312 |
|
|
) if $border; |
| 313 |
|
|
|
| 314 |
|
|
$pb->composite ($map_pb, |
| 315 |
|
|
$x * T, $y * T, |
| 316 |
|
|
T, T, |
| 317 |
|
|
($x - $corner) * T, ($y - 1) * T, 1, 1, |
| 318 |
|
|
"nearest", |
| 319 |
|
|
255 |
| 320 |
|
|
) if $corner; |
| 321 |
|
|
} |
| 322 |
|
|
} |
| 323 |
|
|
} |
| 324 |
|
|
} |
| 325 |
|
|
|
| 326 |
|
|
($map_pb, \%meta) |
| 327 |
|
|
} |
| 328 |
|
|
|
| 329 |
|
|
for my $file (@ARGV) { |
| 330 |
|
|
my $mapa = read_arch $file; |
| 331 |
|
|
my ($pb, $meta) = cfmap_render $mapa, $file; |
| 332 |
root |
1.3 |
$pb->save ("$file.png~", "png"); |
| 333 |
|
|
system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
| 334 |
root |
1.6 |
#system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
| 335 |
|
|
system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; |
| 336 |
root |
1.3 |
unlink "$file.png~"; |
| 337 |
root |
1.1 |
Storable::nstore $meta, "$file.pst"; |
| 338 |
|
|
} |
| 339 |
|
|
|
| 340 |
|
|
|
| 341 |
|
|
|
| 342 |
|
|
|
| 343 |
|
|
|
| 344 |
|
|
|