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