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,2007 Marc Lehmann <cfmaps@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. |
… | |
… | |
25 | # middle face: Object with highest visibility (of monster/player on a space). If |
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, |
26 | # no monster/player, then object with second highest visibility, or if all equal, |
27 | # second top object relative to map stacking. |
27 | # second top object relative to map stacking. |
28 | # Bottom object: the highest object that is a floor type object. |
28 | # Bottom object: the highest object that is a floor type object. |
29 | # |
29 | # |
30 | # ... i believe that anytime, but it still doesn't mention the smoothlevel |
30 | # ... I believe that anytime, but it still doesn't mention the smoothlevel |
31 | # interaction :( |
31 | # interaction :( |
32 | |
32 | |
33 | our $VERSION = '1.212'; |
33 | our $VERSION = '2.012'; |
34 | |
34 | |
35 | use strict; |
35 | use strict; |
36 | |
36 | |
37 | use Storable; |
37 | use Deliantra; |
38 | use List::Util qw(max); |
38 | use List::Util qw(max); |
39 | |
39 | |
40 | use Gtk2; |
40 | use Gtk2; |
41 | |
41 | |
42 | #init Gtk2::Gdk; |
|
|
43 | |
|
|
44 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
|
|
45 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
|
|
46 | |
|
|
47 | sub T (){ 32 } |
42 | sub T (){ 32 } |
48 | |
43 | |
49 | sub read_pak($) { |
44 | Deliantra::load_archetypes; |
50 | my ($path) = @_; |
45 | Deliantra::load_tilecache; |
51 | |
|
|
52 | eval { |
|
|
53 | -M "$path.pst" < -M $path |
|
|
54 | && Storable::retrieve "$path.pst" |
|
|
55 | } or do { |
|
|
56 | my %pak; |
|
|
57 | |
|
|
58 | open my $fh, "<:raw:perlio", $path |
|
|
59 | or die "$_[0]: $!"; |
|
|
60 | while (<$fh>) { |
|
|
61 | my ($type, $id, $len, $path) = split; |
|
|
62 | $path =~ s/.*\///; |
|
|
63 | read $fh, $pak{$path}, $len; |
|
|
64 | } |
|
|
65 | |
|
|
66 | Storable::nstore \%pak, "$path.pst"; |
|
|
67 | |
|
|
68 | \%pak |
|
|
69 | } |
|
|
70 | } |
|
|
71 | |
|
|
72 | sub read_smooth($) { |
|
|
73 | my ($path) = @_; |
|
|
74 | |
|
|
75 | eval { |
|
|
76 | -M "$path.pst" < -M $path |
|
|
77 | && Storable::retrieve "$path.pst" |
|
|
78 | } or do { |
|
|
79 | my %smooth; |
|
|
80 | |
|
|
81 | open my $fh, "<:raw:perlio", $path |
|
|
82 | or die "$path: $!"; |
|
|
83 | while (<$fh>) { |
|
|
84 | next if /^\s*($|#)/; |
|
|
85 | |
|
|
86 | $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
|
|
87 | } |
|
|
88 | |
|
|
89 | Storable::nstore \%smooth, "$path.pst"; |
|
|
90 | utime +(stat $path)[8,9], "$path.pst"; |
|
|
91 | |
|
|
92 | \%smooth |
|
|
93 | } |
|
|
94 | } |
|
|
95 | |
|
|
96 | sub read_arch($;$) { |
|
|
97 | my ($path, $cache) = @_; |
|
|
98 | |
|
|
99 | eval { |
|
|
100 | $cache |
|
|
101 | && -M "$path.pst" < -M $path |
|
|
102 | && Storable::retrieve "$path.pst" |
|
|
103 | } or do { |
|
|
104 | my %arc; |
|
|
105 | my ($more, $prev); |
|
|
106 | |
|
|
107 | open my $fh, "<:utf8", $path |
|
|
108 | or die "$path: $!"; |
|
|
109 | |
|
|
110 | my $parse_block; $parse_block = sub { |
|
|
111 | my %arc = @_; |
|
|
112 | |
|
|
113 | while (<$fh>) { |
|
|
114 | s/\s+$//; |
|
|
115 | if (/^end$/i) { |
|
|
116 | last; |
|
|
117 | } elsif (/^arch (\S+)$/) { |
|
|
118 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
|
|
119 | } elsif (/^lore$/) { |
|
|
120 | while (<$fh>) { |
|
|
121 | last if /^endlore\s*$/i; |
|
|
122 | $arc{lore} .= $_; |
|
|
123 | } |
|
|
124 | } elsif (/^msg$/) { |
|
|
125 | while (<$fh>) { |
|
|
126 | last if /^endmsg\s*$/i; |
|
|
127 | $arc{msg} .= $_; |
|
|
128 | } |
|
|
129 | } elsif (/^(\S+)\s*(.*)$/) { |
|
|
130 | $arc{lc $1} = $2; |
|
|
131 | } elsif (/^\s*($|#)/) { |
|
|
132 | # |
|
|
133 | } else { |
|
|
134 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
|
|
135 | } |
|
|
136 | } |
|
|
137 | |
|
|
138 | \%arc |
|
|
139 | }; |
|
|
140 | |
|
|
141 | while (<$fh>) { |
|
|
142 | s/\s+$//; |
|
|
143 | if (/^more$/i) { |
|
|
144 | $more = $prev; |
|
|
145 | } elsif (/^object (\S+)$/i) { |
|
|
146 | my $name = $1; |
|
|
147 | my $arc = $parse_block->(_name => $name); |
|
|
148 | |
|
|
149 | if ($more) { |
|
|
150 | $more->{more} = $arc; |
|
|
151 | } else { |
|
|
152 | $arc{$name} = $arc; |
|
|
153 | } |
|
|
154 | |
|
|
155 | $prev = $arc; |
|
|
156 | $more = undef; |
|
|
157 | } elsif (/^arch (\S+)$/i) { |
|
|
158 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
|
|
159 | } elsif (/^\s*($|#)/) { |
|
|
160 | # |
|
|
161 | } else { |
|
|
162 | warn "$path: unparseable top-level line '$_'"; |
|
|
163 | } |
|
|
164 | } |
|
|
165 | |
|
|
166 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
|
|
167 | |
|
|
168 | if ($cache) { |
|
|
169 | Storable::nstore \%arc, "$path.pst"; |
|
|
170 | utime +(stat $path)[8,9], "$path.pst"; |
|
|
171 | } |
|
|
172 | |
|
|
173 | \%arc |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | my $arch = read_arch "$LIB/archetypes", 1; |
|
|
178 | my $tile = read_pak "$LIB/crossfire.0"; |
|
|
179 | my $smooth = read_smooth "$LIB/smooth"; |
|
|
180 | |
|
|
181 | sub tile($) { |
|
|
182 | my $name = $_[0]; |
|
|
183 | |
|
|
184 | my $pb = $tile->{$name} |
|
|
185 | or return; |
|
|
186 | |
|
|
187 | unless (ref $pb) { |
|
|
188 | my $file = "/tmp/map2png.$$~"; |
|
|
189 | |
|
|
190 | open my $fh, ">:raw", $file |
|
|
191 | or die "$file: $!"; |
|
|
192 | print $fh $pb; |
|
|
193 | close $fh; |
|
|
194 | |
|
|
195 | $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file; |
|
|
196 | unlink $file; |
|
|
197 | } |
|
|
198 | |
|
|
199 | $pb |
|
|
200 | } |
|
|
201 | |
46 | |
202 | sub cfmap_render($;$) { |
47 | sub cfmap_render($;$) { |
203 | my ($mapa, $mapname) = @_; |
48 | my ($mapa, $mapname) = @_; |
204 | |
49 | |
205 | my %meta; |
50 | my %meta; |
… | |
… | |
237 | for my $x (0 .. $mapx - 1) { |
82 | for my $x (0 .. $mapx - 1) { |
238 | my $col = $map->[$x]; |
83 | my $col = $map->[$x]; |
239 | for my $y (0 .. $mapy - 1) { |
84 | for my $y (0 .. $mapy - 1) { |
240 | my $as = $col->[$y] || []; |
85 | my $as = $col->[$y] || []; |
241 | |
86 | |
242 | my $minsmooth = 0; |
|
|
243 | |
|
|
244 | for my $layer (0 .. $#$as) { |
87 | for my $layer (0 .. $#$as) { |
245 | my $a = $as->[$layer]; |
88 | my $a = $as->[$layer]; |
246 | |
89 | |
247 | my $o = $arch->{$a->{_name}} |
90 | my $o = $ARCH{$a->{_name}} |
248 | 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; |
249 | |
92 | |
250 | my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; |
|
|
251 | |
|
|
252 | # hack to ensure somewhat correct ordering in case of conflicting |
|
|
253 | # smoothlevel/stacking order |
|
|
254 | $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel; |
|
|
255 | $minsmooth = $smoothlevel; |
|
|
256 | |
|
|
257 | #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
|
|
258 | my $level = $smoothlevel + $layer * 256; |
93 | my $level = $layer * 256; |
259 | |
|
|
260 | $level -= 100 * 256 if $o->{_name} eq "blocked"; |
|
|
261 | |
94 | |
262 | while ($o) { |
95 | while ($o) { |
263 | my $face = $a->{face} || $o->{face}; |
96 | my $face = $a->{face} || $o->{face}; |
264 | |
97 | |
265 | my $pb = tile $face |
98 | $FACE{$face} |
|
|
99 | or $face =~ s/\.1(\d\d)$/.x$1/; |
|
|
100 | |
|
|
101 | $FACE{$face} |
266 | 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}; |
267 | |
105 | |
268 | my $mx = $x + $o->{x}; |
106 | my $mx = $x + $o->{x}; |
269 | my $my = $y + $o->{y}; |
107 | my $my = $y + $o->{y}; |
270 | |
108 | |
271 | last if 0 > $mx || $mx >= $mapx |
109 | last if 0 > $mx || $mx >= $mapx |
272 | || 0 > $my || $my >= $mapy; |
110 | || 0 > $my || $my >= $mapy; |
273 | |
111 | |
274 | # this is very ugly (some tiles are 32x33 or worse) |
|
|
275 | my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
|
|
276 | |
|
|
277 | my $dx = $bigface ? $o->{x} : 0; |
|
|
278 | my $dy = $bigface ? $o->{y} : 0; |
|
|
279 | |
|
|
280 | push @{ $map_info{$level}{$mx, $my} }, $a; |
112 | push @{ $map_info{$level}{$mx, $my} }, $a; |
281 | |
113 | |
282 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
114 | $draw_info{$level}{$idx}{$mx, $my} |= 0x2000; |
283 | |
115 | |
284 | if (my $sface = $smooth->{$face}) { |
116 | if (0) { # disable smoothing for the time being |
285 | $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}; |
286 | |
122 | |
287 | # full tile |
123 | # full tile |
288 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
124 | $draw_info{$level}{$idx}{$mx , $my } |= 0x1000; |
289 | |
125 | |
290 | # borders |
126 | # borders |
291 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0091; |
127 | $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091; |
292 | $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0032; |
128 | $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032; |
293 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; |
129 | $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064; |
294 | $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; |
130 | $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8; |
295 | |
131 | |
296 | # corners |
132 | # corners |
297 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
133 | $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100; |
298 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
134 | $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200; |
299 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
135 | $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400; |
300 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
136 | $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800; |
301 | } |
137 | } |
|
|
138 | } |
302 | |
139 | |
303 | $o = $o->{more}; |
140 | $o = $o->{more}; |
304 | $level = ($layer + 1000) * 2; # put "big things" on top, no matter what |
|
|
305 | } |
141 | } |
306 | } |
142 | } |
307 | } |
143 | } |
308 | } |
144 | } |
309 | |
145 | |
310 | 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 |
311 | or die; |
147 | or die; |
312 | $map_pb->fill (0xffffff00); |
148 | $map_pb->fill (0xffffff00); |
313 | |
149 | |
314 | # second pass, render the map |
150 | # second pass, render the map |
315 | for my $level (sort { $a <=> $b } keys %draw_info) { |
151 | for my $level (sort { $a <=> $b } keys %draw_info) { |
316 | my $v = $draw_info{$level}; |
152 | my $v = $draw_info{$level}; |
317 | while (my ($sface, $info) = each %$v) { |
153 | while (my ($face, $info) = each %$v) { |
318 | my $pb = tile $sface |
|
|
319 | or die "no smooth face $sface\n"; |
|
|
320 | |
|
|
321 | while (my ($xy, $bits) = each %$info) { |
154 | while (my ($xy, $bits) = each %$info) { |
322 | my ($x, $y) = split $;, $xy; |
155 | my ($x, $y) = split $;, $xy; |
323 | |
156 | |
324 | next if $x < 0 || $x >= $mapx |
157 | next if $x < 0 || $x >= $mapx |
325 | || $y < 0 || $y >= $mapy; |
158 | || $y < 0 || $y >= $mapy; |
326 | |
159 | |
327 | # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb |
160 | # bits is __fn cccc CCCC bbbb |
328 | # f full tile draw with x|y bigface displacement |
161 | # f full tile draw with x|y bigface displacement |
329 | # n do not draw borders&corners |
162 | # n do not draw borders&corners |
330 | # c draw these corners, but... |
163 | # c draw these corners, but... |
331 | # C ... not these |
164 | # C ... not these |
332 | # b draw these borders |
165 | # b draw these borders |
333 | |
166 | |
334 | if ($bits & 0x2000) { |
167 | if ($bits & 0x2000) { |
335 | my $dx = (($bits >> 24) & 0xff) - 128; |
168 | my $ix = $face % CACHESTRIDE; |
336 | my $dy = (($bits >> 16) & 0xff) - 128; |
169 | my $iy = int $face / CACHESTRIDE; |
337 | |
170 | |
338 | $pb->composite ($map_pb, |
171 | $TILE->composite ($map_pb, |
339 | $x * T, $y * T, |
172 | $x * T, $y * T, |
340 | T, T, |
173 | T, T, |
341 | ($x - $dx) * T, ($y - $dy) * T, 1, 1, |
174 | ($x - $ix) * T, ($y - $iy) * T, 1, 1, |
342 | "nearest", |
175 | "nearest", |
343 | 255 |
176 | 255 |
344 | ); |
177 | ); |
345 | } |
178 | } |
346 | |
179 | |
347 | unless ($bits & 0x1000) { |
180 | unless ($bits & 0x1000) { |
348 | my $border = $bits & 0xf; |
181 | my $border = $face + ($bits & 0xf); |
349 | my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; |
182 | my $bx = $border % CACHESTRIDE; |
350 | |
183 | my $by = int $border / CACHESTRIDE; |
351 | $pb->composite ($map_pb, |
184 | $TILE->composite ($map_pb, |
352 | $x * T, $y * T, |
185 | $x * T, $y * T, |
353 | T, T, |
186 | T, T, |
354 | ($x - $border) * T, $y * T, 1, 1, |
187 | ($x - $bx) * T, ($y - $by) * T, 1, 1, |
355 | "nearest", |
188 | "nearest", |
356 | 255 |
189 | 255 |
357 | ) if $border; |
190 | ) if $border; |
358 | |
191 | |
|
|
192 | my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf); |
|
|
193 | my $cx = $corner % CACHESTRIDE; |
|
|
194 | my $cy = int $corner / CACHESTRIDE; |
359 | $pb->composite ($map_pb, |
195 | $TILE->composite ($map_pb, |
360 | $x * T, $y * T, |
196 | $x * T, $y * T, |
361 | T, T, |
197 | T, T, |
362 | ($x - $corner) * T, ($y - 1) * T, 1, 1, |
198 | ($x - $cx) * T, ($y - $cy) * T, 1, 1, |
363 | "nearest", |
199 | "nearest", |
364 | 255 |
200 | 255 |
365 | ) if $corner; |
201 | ) if $corner; |
366 | } |
202 | } |
367 | } |
203 | } |
… | |
… | |
384 | |
220 | |
385 | ($map_pb, \%meta) |
221 | ($map_pb, \%meta) |
386 | } |
222 | } |
387 | |
223 | |
388 | for my $file (@ARGV) { |
224 | for my $file (@ARGV) { |
389 | my $mapa = read_arch $file; |
225 | my $stderr; |
|
|
226 | $SIG{__WARN__} = sub { |
|
|
227 | $stderr .= $_[0]; |
|
|
228 | print STDERR $_[0]; |
|
|
229 | }; |
|
|
230 | |
390 | $file =~ s/\.map$//; |
231 | $file =~ s/\.map$//; |
|
|
232 | |
|
|
233 | eval { |
|
|
234 | my $mapa = read_arch "$file.map"; |
391 | my ($pb, $meta) = cfmap_render $mapa, $file; |
235 | my ($pb, $meta) = cfmap_render $mapa, $file; |
392 | $pb->save ("$file.png~~", "png"); |
236 | $pb->save ("$file.png~~", "png"); |
393 | system "gm", "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"; |
394 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
238 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
395 | system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~"; |
239 | system "optipng", "-q", "-out", "$file.png~", "$file.png~~"; |
396 | # system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
240 | # system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
397 | unlink "$file.png~~"; |
241 | unlink "$file.png~~"; |
398 | Storable::nstore $meta, "$file.pst"; |
242 | utime +(stat "$file.map")[8,9], "$file.jpg" or die "$file.jpg: $!"; |
399 | utime +(stat $file)[8,9], "$file.pst"; |
243 | utime +(stat "$file.map")[8,9], "$file.png~" or die "$file.png~: $!"; |
400 | utime +(stat $file)[8,9], "$file.jpg"; |
|
|
401 | utime +(stat $file)[8,9], "$file.png~"; |
|
|
402 | rename "$file.png~", "$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 | } |
403 | } |
257 | } |
404 | |
258 | |
405 | |
259 | |
406 | |
260 | |
407 | |
261 | |