--- cfmaps/cfmap2png 2005/11/17 12:15:19 1.2 +++ cfmaps/cfmap2png 2005/11/30 08:05:59 1.15 @@ -1,9 +1,26 @@ #!/opt/bin/perl -# bugs: http://cfmaps.schmorp.de/brest/apartments/brest_town_house.html <- walls -# whaling... icecaves... water is red? +# cfarch2png - convert crossfire maps to png+metadata +# Copyright (C) 2005 Marc Lehmann +# +# CFARCH2PNG is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with gvpe; if not, write to the Free Software +# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + # tower of stars: missing craters? -# http://cfmaps.schmorp.de/pup_land/raffle/raffle3_u2.html <-> yellow "doors" show as grey stone as surrounding +# world_108_123 (8|18), hole below grass but shouldn't? + +our $VERSION = '1.11'; use strict; @@ -87,7 +104,12 @@ if (/^end$/i) { last; } elsif (/^arch (\S+)$/) { - push @{ $arc{subarch} }, $parse_block->(_name => $1); + push @{ $arc{inventory} }, $parse_block->(_name => $1); + } elsif (/^lore$/) { + while (<$fh>) { + last if /^endlore\s*$/i; + $arc{lore} .= $_; + } } elsif (/^msg$/) { while (<$fh>) { last if /^endmsg\s*$/i; @@ -113,8 +135,11 @@ my $name = $1; my $arc = $parse_block->(_name => $name); - $arc{$name} = $arc; - $more->{more} = $arc if $more; + if ($more) { + $more->{more} = $arc; + } else { + $arc{$name} = $arc; + } $prev = $arc; $more = undef; @@ -168,20 +193,22 @@ my ($mapx, $mapy); - my $map = $meta{map} = []; + my $map; for (@{ $mapa->{arch} }) { + my ($x, $y) = ($_->{x}, $_->{y}); + if ($_->{_name} eq "map") { $meta{info} = $_; - $mapx = $_->{width} || $_->{x}; - $mapy = $_->{height} || $_->{y}; + $mapx = $_->{width} || $x; + $mapy = $_->{height} || $y; } else { - push @{ $map->[$_->{x}][$_->{y}] }, $_; + push @{ $map->[$x][$y] }, $_; # arch map is unreliable w.r.t. width and height - $mapx = $_->{x} + 1 if $mapx <= $_->{x}; - $mapy = $_->{y} + 1 if $mapy <= $_->{y}; + $mapx = $x + 1 if $mapx <= $x; + $mapy = $y + 1 if $mapy <= $y; #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; } @@ -190,10 +217,8 @@ $meta{width} = $mapx; $meta{height} = $mapy; - my %map_face; my %draw_info; - - my $OBJ_LEVEL = 2**32; # higher than any valid (or in-use) smooth_level + my %map_info; # first pass, gather face stacking order, border and corner info for my $x (0 .. $mapx - 1) { @@ -204,27 +229,20 @@ for my $layer (0 .. $#$as) { my $a = $as->[$layer]; - my $o = $a; - delete $o->{x}; - delete $o->{y}; - - my $os = $arch->{$a->{_name}} + my $o = $arch->{$a->{_name}} or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; - for (1..10) { - $o = { %$os, %$o }; - last unless exists $os->{other_arch}; - $o = $arch->{$os->{other_arch}}; - } - - my $smooth_level = $o->{smoothlevel}; - my $level = $smooth_level ? $smooth_level - : $o->{is_floor} ? 0 - : $OBJ_LEVEL + $layer; + my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; + my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; + my $level = $smoothlevel ? $smoothlevel + : $is_floor ? $layer - 1000 + : $layer + 1000; while ($o) { - my $pb = tile $o->{face} - or (warn "$mapname: face '$o->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; + my $face = $a->{face} || $o->{face}; + + my $pb = tile $face + or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; my $mx = $x + $o->{x}; my $my = $y + $o->{y}; @@ -235,32 +253,32 @@ # this is very ugly (some tiles are 32x33 or worse) my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; - if (my $sface = $smooth->{$o->{face}}) { - $bigface and die "can't handle big faces with smoothing ($o->{face})\n"; + my $dx = $bigface ? $o->{x} : 0; + my $dy = $bigface ? $o->{y} : 0; + + push @{ $map_info{$level}{$mx, $my} }, $a; + + $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); + + if (my $sface = $smooth->{$face}) { + $bigface and die "can't handle bigfaces with smoothing ($face)\n"; # full tile - $draw_info{$smooth_level}{$sface}{$mx , $my } |= 0x1000; + $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; # borders - $draw_info{$smooth_level}{$sface}{$mx + 1, $my } |= 0x0031; - $draw_info{$smooth_level}{$sface}{$mx , $my + 1} |= 0x0092; - $draw_info{$smooth_level}{$sface}{$mx - 1, $my } |= 0x0064; - $draw_info{$smooth_level}{$sface}{$mx , $my - 1} |= 0x00c8; + $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; + $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; + $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; + $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; # corners - $draw_info{$smooth_level}{$sface}{$mx + 1, $my + 1} |= 0x0100; - $draw_info{$smooth_level}{$sface}{$mx - 1, $my + 1} |= 0x0200; - $draw_info{$smooth_level}{$sface}{$mx - 1, $my - 1} |= 0x0400; - $draw_info{$smooth_level}{$sface}{$mx + 1, $my - 1} |= 0x0800; - } else { - $smooth_level = 0; + $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; + $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; + $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; + $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; } - my $dx = $bigface ? $o->{x} : 0; - my $dy = $bigface ? $o->{y} : 0; - - $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); - $o = $o->{more}; } } @@ -269,9 +287,9 @@ my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T or die; - $map_pb->fill (0x00000000); + $map_pb->fill (0xffffff00); - # second pass, render all the stuff + # second pass, render the map for my $level (sort { $a <=> $b } keys %draw_info) { my $v = $draw_info{$level}; while (my ($sface, $info) = each %$v) { @@ -284,11 +302,11 @@ next if $x < 0 || $x >= $mapx || $y < 0 || $y >= $mapy; - # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB - # X don't draw - # F full tile draw with x|y bigface displacement - # c maybe draw these corners - # C do not draw these corners + # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb + # f full tile draw with x|y bigface displacement + # n do not draw borders&corners + # c draw these corners, but... + # C ... not these # b draw these borders if ($bits & 0x2000) { @@ -328,13 +346,32 @@ } } + # third pass, gather meta info + for my $level (sort { $a <=> $b } keys %map_info) { + my $info = $map_info{$level}; + + while (my ($xy, $as) = each %$info) { + my ($x, $y) = split $;, $xy; + + next if $x < 0 || $x >= $mapx + || $y < 0 || $y >= $mapy; + + push @{ $meta{map}[$x][$y] }, $_ for @$as; + } + } + ($map_pb, \%meta) } for my $file (@ARGV) { my $mapa = read_arch $file; my ($pb, $meta) = cfmap_render $mapa, $file; - $pb->save ("$file.png", "png"); + $pb->save ("$file.png~", "png"); + system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; + #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency + system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; +# system "pngnq <\Q$file.png~\E >\Q$file.png\E"; + unlink "$file.png~"; Storable::nstore $meta, "$file.pst"; }