ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
(Generate patch)

Comparing cfmaps/cfmap2png (file contents):
Revision 1.10 by root, Sun Nov 20 01:31:21 2005 UTC vs.
Revision 1.27 by root, Thu Oct 22 03:02:00 2009 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# cfarch2png - convert crossfire maps to png+metadata 3# cfmap2png - convert deliantra maps to png+metadata
4# Copyright (C) 2005 Marc Lehmann <gvpe@schmorp.de> 4# Copyright (C) 2005,2007,2008,2009 Marc Lehmann <cfmaps@schmorp.de>
5# 5#
6# CFARCH2PNG is free software; you can redistribute it and/or modify 6# CFARCH2PNG is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by 7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or 8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version. 9# (at your option) any later version.
15# 15#
16# You should have received a copy of the GNU General Public License 16# You should have received a copy of the GNU General Public License
17# along with gvpe; if not, write to the Free Software 17# along with gvpe; if not, write to the Free Software
18# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 19
20# tower of stars: missing craters? 20# Quoth The master himself:
21#
22# Object ordering is basically like this:
23# top face: players or monsters. If none on the space, object with highest
24# visibility value - if equal, then top object in terms of object stacking on the map.
25# middle face: Object with highest visibility (of monster/player on a space). If
26# no monster/player, then object with second highest visibility, or if all equal,
27# second top object relative to map stacking.
28# Bottom object: the highest object that is a floor type object.
29#
30# ... I believe that anytime, but it still doesn't mention the smoothlevel
31# interaction :(
21 32
22our $VERSION = '1.1'; 33our $VERSION = '2.012';
23 34
24use strict; 35use strict;
25 36
26use Storable; 37use Deliantra;
27use List::Util qw(max); 38use List::Util qw(max);
28 39
29use Gtk2; 40use Gtk2;
30 41
31#init Gtk2::Gdk;
32
33my $LIB = $ENV{CROSSFIRE_LIBDIR}
34 or die "\$CROSSFIRE_LIBDIR must be set\n";
35
36sub T (){ 32 } 42sub T (){ 32 }
37 43
38sub read_pak($) { 44Deliantra::load_archetypes;
39 my ($path) = @_; 45Deliantra::load_tilecache;
40
41 eval {
42 -M "$path.pst" < -M $path
43 && Storable::retrieve "$path.pst"
44 } or do {
45 my %pak;
46
47 open my $fh, "<:raw", $path
48 or die "$_[0]: $!";
49 while (<$fh>) {
50 my ($type, $id, $len, $path) = split;
51 $path =~ s/.*\///;
52 read $fh, $pak{$path}, $len;
53 }
54
55 Storable::nstore \%pak, "$path.pst";
56
57 \%pak
58 }
59}
60
61sub read_smooth($) {
62 my ($path) = @_;
63
64 eval {
65 -M "$path.pst" < -M $path
66 && Storable::retrieve "$path.pst"
67 } or do {
68 my %smooth;
69
70 open my $fh, "<:raw", $path
71 or die "$path: $!";
72 while (<$fh>) {
73 next if /^\s*($|#)/;
74
75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
76 }
77
78 Storable::nstore \%smooth, "$path.pst";
79
80 \%smooth
81 }
82}
83
84sub read_arch($;$) {
85 my ($path, $cache) = @_;
86
87 eval {
88 $cache
89 && -M "$path.pst" < -M $path
90 && Storable::retrieve "$path.pst"
91 } or do {
92 my %arc;
93 my ($more, $prev);
94
95 open my $fh, "<:raw", $path
96 or die "$path: $!";
97
98 my $parse_block; $parse_block = sub {
99 my %arc = @_;
100
101 while (<$fh>) {
102 s/\s+$//;
103 if (/^end$/i) {
104 last;
105 } elsif (/^arch (\S+)$/) {
106 push @{ $arc{inventory} }, $parse_block->(_name => $1);
107 } elsif (/^lore$/) {
108 while (<$fh>) {
109 last if /^endlore\s*$/i;
110 $arc{lore} .= $_;
111 }
112 } elsif (/^msg$/) {
113 while (<$fh>) {
114 last if /^endmsg\s*$/i;
115 $arc{msg} .= $_;
116 }
117 } elsif (/^(\S+)\s*(.*)$/) {
118 $arc{lc $1} = $2;
119 } elsif (/^\s*($|#)/) {
120 #
121 } else {
122 warn "$path: unparsable line '$_' in arch $arc{_name}";
123 }
124 }
125
126 \%arc
127 };
128
129 while (<$fh>) {
130 s/\s+$//;
131 if (/^more$/i) {
132 $more = $prev;
133 } elsif (/^object (\S+)$/i) {
134 my $name = $1;
135 my $arc = $parse_block->(_name => $name);
136
137 if ($more) {
138 $more->{more} = $arc;
139 } else {
140 $arc{$name} = $arc;
141 }
142
143 $prev = $arc;
144 $more = undef;
145 } elsif (/^arch (\S+)$/i) {
146 push @{ $arc{arch} }, $parse_block->(_name => $1);
147 } elsif (/^\s*($|#)/) {
148 #
149 } else {
150 warn "$path: unparseable top-level line '$_'";
151 }
152 }
153
154 undef $parse_block; # work around bug in perl not freeing $fh etc.
155
156 Storable::nstore \%arc, "$path.pst"
157 if $cache;
158
159 \%arc
160 }
161}
162
163my $arch = read_arch "$LIB/archetypes", 1;
164my $tile = read_pak "$LIB/crossfire.0";
165my $smooth = read_smooth "$LIB/smooth";
166
167sub tile($) {
168 my $name = $_[0];
169
170 my $pb = $tile->{$name}
171 or return;
172
173 unless (ref $pb) {
174 my $file = "/tmp/map2png.$$~";
175
176 open my $fh, ">:raw", $file
177 or die "$file: $!";
178 print $fh $pb;
179 close $fh;
180
181 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
182 unlink $file;
183 }
184
185 $pb
186}
187 46
188sub cfmap_render($;$) { 47sub cfmap_render($;$) {
189 my ($mapa, $mapname) = @_; 48 my ($mapa, $mapname) = @_;
190 49
191 my %meta; 50 my %meta;
222 # first pass, gather face stacking order, border and corner info 81 # first pass, gather face stacking order, border and corner info
223 for my $x (0 .. $mapx - 1) { 82 for my $x (0 .. $mapx - 1) {
224 my $col = $map->[$x]; 83 my $col = $map->[$x];
225 for my $y (0 .. $mapy - 1) { 84 for my $y (0 .. $mapy - 1) {
226 my $as = $col->[$y] || []; 85 my $as = $col->[$y] || [];
227 86
228 for my $layer (0 .. $#$as) { 87 for my $layer (0 .. $#$as) {
229 my $a = $as->[$layer]; 88 my $a = $as->[$layer];
230 89
231 my $o = $arch->{$a->{_name}} 90 my $o = $ARCH{$a->{_name}}
232 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 91 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
233 92
234 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; 93 my $level = $layer * 256;
235 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
236 my $level = $smoothlevel ? $smoothlevel
237 : $is_floor ? 0
238 : 256 + $layer;
239 94
240 while ($o) { 95 while ($o) {
241 my $face = $a->{face} || $o->{face}; 96 my $face = $a->{face} || $o->{face};
242 97
243 my $pb = tile $face 98 $FACE{$face}
99 or $face =~ s/\.1(\d\d)$/.x$1/;
100
101 $FACE{$face}
244 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; 102 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
103
104 my $idx = $FACE{$face}{idx};
245 105
246 my $mx = $x + $o->{x}; 106 my $mx = $x + $o->{x};
247 my $my = $y + $o->{y}; 107 my $my = $y + $o->{y};
248 108
249 last if 0 > $mx || $mx >= $mapx 109 last if 0 > $mx || $mx >= $mapx
250 || 0 > $my || $my >= $mapy; 110 || 0 > $my || $my >= $mapy;
251 111
252 # this is very ugly (some tiles are 32x33 or worse)
253 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
254
255 my $dx = $bigface ? $o->{x} : 0;
256 my $dy = $bigface ? $o->{y} : 0;
257
258 push @{ $map_info{$level}{$mx, $my} }, $a; 112 push @{ $map_info{$level}{$mx, $my} }, $a;
259 113
260 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 114 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
261 115
262 if (my $sface = $smooth->{$face}) { 116 if (0) { # disable smoothing for the time being
263 $bigface and die "can't handle bigfaces with smoothing ($face)\n"; 117 if (my $sface = $FACEDATA{faceinfo}{$face}{smooth}) {
118 $FACE{$sface}
119 or (warn "$mapname: smoothface '$sface' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
120 my $idx = $FACE{$sface}{idx};
121 my $level = $layer * 256 + $FACEDATA{faceinfo}{$face}{smoothlevel};
264 122
265 # full tile 123 # full tile
266 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 124 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
267 125
268 # borders 126 # borders
269 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; 127 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
270 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; 128 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
271 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; 129 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
272 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; 130 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
273 131
274 # corners 132 # corners
275 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; 133 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
276 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 134 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
277 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 135 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
278 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 136 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
137 }
279 } 138 }
280 139
281 $o = $o->{more}; 140 $o = $o->{more};
282 } 141 }
283 } 142 }
284 } 143 }
285 } 144 }
286 145
287 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 146 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
288 or die; 147 or die;
289 $map_pb->fill (0xffffff00); 148 $map_pb->fill (0xffffff00);
290 149
291 # second pass, render the map 150 # second pass, render the map
292 for my $level (sort { $a <=> $b } keys %draw_info) { 151 for my $level (sort { $a <=> $b } keys %draw_info) {
293 my $v = $draw_info{$level}; 152 my $v = $draw_info{$level};
294 while (my ($sface, $info) = each %$v) { 153 while (my ($face, $info) = each %$v) {
295 my $pb = tile $sface
296 or die "no smooth face $sface\n";
297
298 while (my ($xy, $bits) = each %$info) { 154 while (my ($xy, $bits) = each %$info) {
299 my ($x, $y) = split $;, $xy; 155 my ($x, $y) = split $;, $xy;
300 156
301 next if $x < 0 || $x >= $mapx 157 next if $x < 0 || $x >= $mapx
302 || $y < 0 || $y >= $mapy; 158 || $y < 0 || $y >= $mapy;
303 159
304 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 160 # bits is __fn cccc CCCC bbbb
305 # X don't draw
306 # F full tile draw with x|y bigface displacement 161 # f full tile draw with x|y bigface displacement
307 # c maybe draw these corners
308 # C do not draw these corners 162 # n do not draw borders&corners
163 # c draw these corners, but...
164 # C ... not these
309 # b draw these borders 165 # b draw these borders
310 166
311 if ($bits & 0x2000) { 167 if ($bits & 0x2000) {
312 my $dx = (($bits >> 24) & 0xff) - 128; 168 my $ix = $face % CACHESTRIDE;
313 my $dy = (($bits >> 16) & 0xff) - 128; 169 my $iy = int $face / CACHESTRIDE;
314 170
315 $pb->composite ($map_pb, 171 $TILE->composite ($map_pb,
316 $x * T, $y * T, 172 $x * T, $y * T,
317 T, T, 173 T, T,
318 ($x - $dx) * T, ($y - $dy) * T, 1, 1, 174 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
319 "nearest", 175 "nearest",
320 255 176 255
321 ); 177 );
322 } 178 }
323 179
324 unless ($bits & 0x1000) { 180 unless ($bits & 0x1000) {
325 my $border = $bits & 0xf; 181 my $border = $face + ($bits & 0xf);
326 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; 182 my $bx = $border % CACHESTRIDE;
327 183 my $by = int $border / CACHESTRIDE;
328 $pb->composite ($map_pb, 184 $TILE->composite ($map_pb,
329 $x * T, $y * T, 185 $x * T, $y * T,
330 T, T, 186 T, T,
331 ($x - $border) * T, $y * T, 1, 1, 187 ($x - $bx) * T, ($y - $by) * T, 1, 1,
332 "nearest", 188 "nearest",
333 255 189 255
334 ) if $border; 190 ) if $border;
335 191
192 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
193 my $cx = $corner % CACHESTRIDE;
194 my $cy = int $corner / CACHESTRIDE;
336 $pb->composite ($map_pb, 195 $TILE->composite ($map_pb,
337 $x * T, $y * T, 196 $x * T, $y * T,
338 T, T, 197 T, T,
339 ($x - $corner) * T, ($y - 1) * T, 1, 1, 198 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
340 "nearest", 199 "nearest",
341 255 200 255
342 ) if $corner; 201 ) if $corner;
343 } 202 }
344 } 203 }
361 220
362 ($map_pb, \%meta) 221 ($map_pb, \%meta)
363} 222}
364 223
365for my $file (@ARGV) { 224for my $file (@ARGV) {
225 my $stderr;
226 $SIG{__WARN__} = sub {
227 $stderr .= $_[0];
228 print STDERR $_[0];
229 };
230
231 $file =~ s/\.map$//;
232
233 eval {
366 my $mapa = read_arch $file; 234 my $mapa = read_arch "$file.map";
367 my ($pb, $meta) = cfmap_render $mapa, $file; 235 my ($pb, $meta) = cfmap_render $mapa, $file;
368 $pb->save ("$file.png~", "png"); 236 $pb->save ("$file.png~~", "png");
369 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; 237 system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
370 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 238 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
371 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; 239 system "optipng", "-q", "-out", "$file.png~", "$file.png~~";
240# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
372 unlink "$file.png~"; 241 unlink "$file.png~~";
373 Storable::nstore $meta, "$file.pst"; 242 utime +(stat "$file.map")[8,9], "$file.jpg" or die "$file.jpg: $!";
243 utime +(stat "$file.map")[8,9], "$file.png~" or die "$file.png~: $!";
244 rename "$file.png~", "$file.png";
245 1
246 } or warn $@;
247
248 if (length $stderr) {
249 open my $fh, ">$file.png.err~"
250 or die "$file.png.err~: $!";
251 syswrite $fh, $stderr;
252 close $fh;
253 rename "$file.png.err~", "$file.png.err";
254 } else {
255 unlink "$file.png.err";
256 }
374} 257}
375 258
376 259
377 260
378 261

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines