--- cfmaps/cfmap2png 2005/11/18 12:50:24 1.6 +++ cfmaps/cfmap2png 2007/02/14 02:33:47 1.21 @@ -1,6 +1,36 @@ #!/opt/bin/perl -# tower of stars: missing craters? +# cfarch2png - convert crossfire maps to png+metadata +# Copyright (C) 2005,2007 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 + +# Quoth The master himself: +# +# Object ordering is basically like this: +# top face: players or monsters. If none on the space, object with highest +# visibility value - if equal, then top object in terms of object stacking on the map. +# middle face: Object with highest visibility (of monster/player on a space). If +# no monster/player, then object with second highest visibility, or if all equal, +# second top object relative to map stacking. +# Bottom object: the highest object that is a floor type object. +# +# ... i believe that anytime, but it still doesn't mention the smoothlevel +# interaction :( + +our $VERSION = '1.211'; use strict; @@ -9,8 +39,6 @@ use Gtk2; -our $VERSION = '1.0'; - #init Gtk2::Gdk; my $LIB = $ENV{CROSSFIRE_LIBDIR} @@ -27,7 +55,7 @@ } or do { my %pak; - open my $fh, "<:raw", $path + open my $fh, "<:raw:perlio", $path or die "$_[0]: $!"; while (<$fh>) { my ($type, $id, $len, $path) = split; @@ -50,7 +78,7 @@ } or do { my %smooth; - open my $fh, "<:raw", $path + open my $fh, "<:raw:perlio", $path or die "$path: $!"; while (<$fh>) { next if /^\s*($|#)/; @@ -59,6 +87,7 @@ } Storable::nstore \%smooth, "$path.pst"; + utime +(stat $path)[8,9], "$path.pst"; \%smooth } @@ -75,7 +104,7 @@ my %arc; my ($more, $prev); - open my $fh, "<:raw", $path + open my $fh, "<:utf8", $path or die "$path: $!"; my $parse_block; $parse_block = sub { @@ -136,8 +165,10 @@ undef $parse_block; # work around bug in perl not freeing $fh etc. - Storable::nstore \%arc, "$path.pst" - if $cache; + if ($cache) { + Storable::nstore \%arc, "$path.pst"; + utime +(stat $path)[8,9], "$path.pst"; + } \%arc } @@ -175,20 +206,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}; } @@ -198,13 +231,16 @@ $meta{height} = $mapy; my %draw_info; + my %map_info; # first pass, gather face stacking order, border and corner info for my $x (0 .. $mapx - 1) { my $col = $map->[$x]; for my $y (0 .. $mapy - 1) { my $as = $col->[$y] || []; - + + my $minsmooth = 0; + for my $layer (0 .. $#$as) { my $a = $as->[$layer]; @@ -212,10 +248,16 @@ or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 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 ? 0 - : 256 + $layer; + + # hack to ensure somewhat correct ordering in case of conflicting + # smoothlevel/stacking order + $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel; + $minsmooth = $smoothlevel; + + #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; + my $level = $smoothlevel + $layer * 256; + + $level -= 100 * 256 if $o->{_name} eq "blocked"; while ($o) { my $face = $a->{face} || $o->{face}; @@ -232,6 +274,13 @@ # this is very ugly (some tiles are 32x33 or worse) my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; + 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"; @@ -251,12 +300,8 @@ $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; } - my $dx = $bigface ? $o->{x} : 0; - my $dy = $bigface ? $o->{y} : 0; - - $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); - $o = $o->{more}; + $level = ($layer + 1000) * 2; # put "big things" on top, no matter what } } } @@ -264,9 +309,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) { @@ -279,11 +324,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) { @@ -323,18 +368,38 @@ } } + # 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; + $file =~ s/\.map$//; my ($pb, $meta) = cfmap_render $mapa, $file; - $pb->save ("$file.png~", "png"); - system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; + $pb->save ("$file.png~~", "png"); + system "gm", "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"; - unlink "$file.png~"; + 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"; + utime +(stat $file)[8,9], "$file.pst"; + utime +(stat $file)[8,9], "$file.jpg"; + utime +(stat $file)[8,9], "$file.png~"; + rename "$file.png~", "$file.png"; }