--- cfmaps/cfmap2png 2007/04/13 18:09:23 1.22 +++ cfmaps/cfmap2png 2008/01/06 21:12:00 1.25 @@ -1,7 +1,7 @@ #!/opt/bin/perl -# cfarch2png - convert crossfire maps to png+metadata -# Copyright (C) 2005,2007 Marc Lehmann +# cfarch2png - convert deliantra maps to png+metadata +# Copyright (C) 2005,2007,2008 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 @@ -27,177 +27,22 @@ # 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 +# ... I believe that anytime, but it still doesn't mention the smoothlevel # interaction :( -our $VERSION = '1.212'; +our $VERSION = '2.002'; use strict; -use Storable; +use Deliantra; use List::Util qw(max); use Gtk2; -#init Gtk2::Gdk; - -my $LIB = $ENV{CROSSFIRE_LIBDIR} - or die "\$CROSSFIRE_LIBDIR must be set\n"; - sub T (){ 32 } -sub read_pak($) { - my ($path) = @_; - - eval { - -M "$path.pst" < -M $path - && Storable::retrieve "$path.pst" - } or do { - my %pak; - - open my $fh, "<:raw:perlio", $path - or die "$_[0]: $!"; - while (<$fh>) { - my ($type, $id, $len, $path) = split; - $path =~ s/.*\///; - read $fh, $pak{$path}, $len; - } - - Storable::nstore \%pak, "$path.pst"; - - \%pak - } -} - -sub read_smooth($) { - my ($path) = @_; - - eval { - -M "$path.pst" < -M $path - && Storable::retrieve "$path.pst" - } or do { - my %smooth; - - open my $fh, "<:raw:perlio", $path - or die "$path: $!"; - while (<$fh>) { - next if /^\s*($|#)/; - - $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; - } - - Storable::nstore \%smooth, "$path.pst"; - utime +(stat $path)[8,9], "$path.pst"; - - \%smooth - } -} - -sub read_arch($;$) { - my ($path, $cache) = @_; - - eval { - $cache - && -M "$path.pst" < -M $path - && Storable::retrieve "$path.pst" - } or do { - my %arc; - my ($more, $prev); - - open my $fh, "<:utf8", $path - or die "$path: $!"; - - my $parse_block; $parse_block = sub { - my %arc = @_; - - while (<$fh>) { - s/\s+$//; - if (/^end$/i) { - last; - } elsif (/^arch (\S+)$/) { - 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; - $arc{msg} .= $_; - } - } elsif (/^(\S+)\s*(.*)$/) { - $arc{lc $1} = $2; - } elsif (/^\s*($|#)/) { - # - } else { - warn "$path: unparsable line '$_' in arch $arc{_name}"; - } - } - - \%arc - }; - - while (<$fh>) { - s/\s+$//; - if (/^more$/i) { - $more = $prev; - } elsif (/^object (\S+)$/i) { - my $name = $1; - my $arc = $parse_block->(_name => $name); - - if ($more) { - $more->{more} = $arc; - } else { - $arc{$name} = $arc; - } - - $prev = $arc; - $more = undef; - } elsif (/^arch (\S+)$/i) { - push @{ $arc{arch} }, $parse_block->(_name => $1); - } elsif (/^\s*($|#)/) { - # - } else { - warn "$path: unparseable top-level line '$_'"; - } - } - - undef $parse_block; # work around bug in perl not freeing $fh etc. - - if ($cache) { - Storable::nstore \%arc, "$path.pst"; - utime +(stat $path)[8,9], "$path.pst"; - } - - \%arc - } -} - -my $arch = read_arch "$LIB/archetypes", 1; -my $tile = read_pak "$LIB/crossfire.0"; -my $smooth = read_smooth "$LIB/smooth"; - -sub tile($) { - my $name = $_[0]; - - my $pb = $tile->{$name} - or return; - - unless (ref $pb) { - my $file = "/tmp/map2png.$$~"; - - open my $fh, ">:raw", $file - or die "$file: $!"; - print $fh $pb; - close $fh; - - $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file; - unlink $file; - } - - $pb -} +Deliantra::load_archetypes; +Deliantra::load_tilecache; sub cfmap_render($;$) { my ($mapa, $mapname) = @_; @@ -239,92 +84,77 @@ for my $y (0 .. $mapy - 1) { my $as = $col->[$y] || []; - my $minsmooth = 0; - for my $layer (0 .. $#$as) { my $a = $as->[$layer]; - my $o = $arch->{$a->{_name}} + my $o = $ARCH{$a->{_name}} or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; - my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; - - # 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"; + my $level = $layer * 256; while ($o) { my $face = $a->{face} || $o->{face}; - my $pb = tile $face + $FACE{$face} or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; + my $idx = $FACE{$face}{idx}; + my $mx = $x + $o->{x}; my $my = $y + $o->{y}; last if 0 > $mx || $mx >= $mapx || 0 > $my || $my >= $mapy; - # 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); + $draw_info{$level}{$idx}{$mx, $my} |= 0x2000; - if (my $sface = $smooth->{$face}) { - $bigface and die "can't handle bigfaces with smoothing ($face)\n"; + if (0) { # disable smoothing for the time being + if (my $sface = $FACEDATA{faceinfo}{$face}{smooth}) { + $FACE{$sface} + or (warn "$mapname: smoothface '$sface' not found for arch '$a->{_name}' at ($x|$y)\n"), last; + my $idx = $FACE{$sface}{idx}; + my $level = $layer * 256 + $FACEDATA{faceinfo}{$face}{smoothlevel}; # full tile - $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; + $draw_info{$level}{$idx}{$mx , $my } |= 0x1000; # borders - $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0091; - $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0032; - $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; - $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; + $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091; + $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032; + $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064; + $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8; # corners - $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; + $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100; + $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200; + $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400; + $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800; + } } $o = $o->{more}; - $level = ($layer + 1000) * 2; # put "big things" on top, no matter what } } } } my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T - or die; + or die; $map_pb->fill (0xffffff00); # 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) { - my $pb = tile $sface - or die "no smooth face $sface\n"; - + while (my ($face, $info) = each %$v) { while (my ($xy, $bits) = each %$info) { my ($x, $y) = split $;, $xy; next if $x < 0 || $x >= $mapx || $y < 0 || $y >= $mapy; - # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb + # bits is __fn cccc CCCC bbbb # f full tile draw with x|y bigface displacement # n do not draw borders&corners # c draw these corners, but... @@ -332,34 +162,37 @@ # b draw these borders if ($bits & 0x2000) { - my $dx = (($bits >> 24) & 0xff) - 128; - my $dy = (($bits >> 16) & 0xff) - 128; + my $ix = $face % CACHESTRIDE; + my $iy = int $face / CACHESTRIDE; - $pb->composite ($map_pb, + $TILE->composite ($map_pb, $x * T, $y * T, T, T, - ($x - $dx) * T, ($y - $dy) * T, 1, 1, + ($x - $ix) * T, ($y - $iy) * T, 1, 1, "nearest", 255 ); } unless ($bits & 0x1000) { - my $border = $bits & 0xf; - my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; - - $pb->composite ($map_pb, + my $border = $face + ($bits & 0xf); + my $bx = $border % CACHESTRIDE; + my $by = int $border / CACHESTRIDE; + $TILE->composite ($map_pb, $x * T, $y * T, T, T, - ($x - $border) * T, $y * T, 1, 1, + ($x - $bx) * T, ($y - $by) * T, 1, 1, "nearest", 255 ) if $border; - $pb->composite ($map_pb, + my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf); + my $cx = $corner % CACHESTRIDE; + my $cy = int $corner / CACHESTRIDE; + $TILE->composite ($map_pb, $x * T, $y * T, T, T, - ($x - $corner) * T, ($y - 1) * T, 1, 1, + ($x - $cx) * T, ($y - $cy) * T, 1, 1, "nearest", 255 ) if $corner; @@ -392,11 +225,9 @@ $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~"; + system "optipng", "-q", "-out", "$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";