1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | # cfarch2png - convert crossfire maps to png+metadata |
3 | # cfarch2png - convert crossfire maps to png+metadata |
4 | # Copyright (C) 2005 Marc Lehmann <gvpe@schmorp.de> |
4 | # Copyright (C) 2005,2007 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 | |
22 | our $VERSION = '1.1'; |
33 | our $VERSION = '2.001'; |
23 | |
34 | |
24 | use strict; |
35 | use strict; |
25 | |
36 | |
26 | use Storable; |
37 | use Crossfire 1.0; |
27 | use List::Util qw(max); |
38 | use List::Util qw(max); |
28 | |
39 | |
29 | use Gtk2; |
40 | use Gtk2; |
30 | |
41 | |
31 | #init Gtk2::Gdk; |
|
|
32 | |
|
|
33 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
|
|
34 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
|
|
35 | |
|
|
36 | sub T (){ 32 } |
42 | sub T (){ 32 } |
37 | |
43 | |
38 | sub read_pak($) { |
44 | Crossfire::load_archetypes; |
39 | my ($path) = @_; |
45 | Crossfire::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 | |
|
|
61 | sub 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 | |
|
|
84 | sub 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 | |
|
|
163 | my $arch = read_arch "$LIB/archetypes", 1; |
|
|
164 | my $tile = read_pak "$LIB/crossfire.0"; |
|
|
165 | my $smooth = read_smooth "$LIB/smooth"; |
|
|
166 | |
|
|
167 | sub 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 | |
188 | sub cfmap_render($;$) { |
47 | sub 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} |
244 | 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}; |
245 | |
102 | |
246 | my $mx = $x + $o->{x}; |
103 | my $mx = $x + $o->{x}; |
247 | my $my = $y + $o->{y}; |
104 | my $my = $y + $o->{y}; |
248 | |
105 | |
249 | last if 0 > $mx || $mx >= $mapx |
106 | last if 0 > $mx || $mx >= $mapx |
250 | || 0 > $my || $my >= $mapy; |
107 | || 0 > $my || $my >= $mapy; |
251 | |
108 | |
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; |
109 | push @{ $map_info{$level}{$mx, $my} }, $a; |
259 | |
110 | |
260 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
111 | $draw_info{$level}{$idx}{$mx, $my} |= 0x2000; |
261 | |
112 | |
262 | if (my $sface = $smooth->{$face}) { |
113 | if (0) { # disable smoothing for the time being |
263 | $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}; |
264 | |
119 | |
265 | # full tile |
120 | # full tile |
266 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
121 | $draw_info{$level}{$idx}{$mx , $my } |= 0x1000; |
267 | |
122 | |
268 | # borders |
123 | # borders |
269 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; |
124 | $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091; |
270 | $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; |
125 | $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032; |
271 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; |
126 | $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064; |
272 | $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; |
127 | $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8; |
273 | |
128 | |
274 | # corners |
129 | # corners |
275 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
130 | $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100; |
276 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
131 | $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200; |
277 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
132 | $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400; |
278 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
133 | $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800; |
|
|
134 | } |
279 | } |
135 | } |
280 | |
136 | |
281 | $o = $o->{more}; |
137 | $o = $o->{more}; |
282 | } |
138 | } |
283 | } |
139 | } |
284 | } |
140 | } |
285 | } |
141 | } |
286 | |
142 | |
287 | 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 |
288 | or die; |
144 | or die; |
289 | $map_pb->fill (0xffffff00); |
145 | $map_pb->fill (0xffffff00); |
290 | |
146 | |
291 | # second pass, render the map |
147 | # second pass, render the map |
292 | for my $level (sort { $a <=> $b } keys %draw_info) { |
148 | for my $level (sort { $a <=> $b } keys %draw_info) { |
293 | my $v = $draw_info{$level}; |
149 | my $v = $draw_info{$level}; |
294 | while (my ($sface, $info) = each %$v) { |
150 | 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) { |
151 | while (my ($xy, $bits) = each %$info) { |
299 | my ($x, $y) = split $;, $xy; |
152 | my ($x, $y) = split $;, $xy; |
300 | |
153 | |
301 | next if $x < 0 || $x >= $mapx |
154 | next if $x < 0 || $x >= $mapx |
302 | || $y < 0 || $y >= $mapy; |
155 | || $y < 0 || $y >= $mapy; |
303 | |
156 | |
304 | # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB |
157 | # bits is __fn cccc CCCC bbbb |
305 | # X don't draw |
|
|
306 | # F full tile draw with x|y bigface displacement |
158 | # f full tile draw with x|y bigface displacement |
307 | # c maybe draw these corners |
|
|
308 | # C do not draw these corners |
159 | # n do not draw borders&corners |
|
|
160 | # c draw these corners, but... |
|
|
161 | # C ... not these |
309 | # b draw these borders |
162 | # b draw these borders |
310 | |
163 | |
311 | if ($bits & 0x2000) { |
164 | if ($bits & 0x2000) { |
312 | my $dx = (($bits >> 24) & 0xff) - 128; |
165 | my $ix = $face % CACHESTRIDE; |
313 | my $dy = (($bits >> 16) & 0xff) - 128; |
166 | my $iy = int $face / CACHESTRIDE; |
314 | |
167 | |
315 | $pb->composite ($map_pb, |
168 | $TILE->composite ($map_pb, |
316 | $x * T, $y * T, |
169 | $x * T, $y * T, |
317 | T, T, |
170 | T, T, |
318 | ($x - $dx) * T, ($y - $dy) * T, 1, 1, |
171 | ($x - $ix) * T, ($y - $iy) * T, 1, 1, |
319 | "nearest", |
172 | "nearest", |
320 | 255 |
173 | 255 |
321 | ); |
174 | ); |
322 | } |
175 | } |
323 | |
176 | |
324 | unless ($bits & 0x1000) { |
177 | unless ($bits & 0x1000) { |
325 | my $border = $bits & 0xf; |
178 | my $border = $face + ($bits & 0xf); |
326 | my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; |
179 | my $bx = $border % CACHESTRIDE; |
327 | |
180 | my $by = int $border / CACHESTRIDE; |
328 | $pb->composite ($map_pb, |
181 | $TILE->composite ($map_pb, |
329 | $x * T, $y * T, |
182 | $x * T, $y * T, |
330 | T, T, |
183 | T, T, |
331 | ($x - $border) * T, $y * T, 1, 1, |
184 | ($x - $bx) * T, ($y - $by) * T, 1, 1, |
332 | "nearest", |
185 | "nearest", |
333 | 255 |
186 | 255 |
334 | ) if $border; |
187 | ) if $border; |
335 | |
188 | |
|
|
189 | my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf); |
|
|
190 | my $cx = $corner % CACHESTRIDE; |
|
|
191 | my $cy = int $corner / CACHESTRIDE; |
336 | $pb->composite ($map_pb, |
192 | $TILE->composite ($map_pb, |
337 | $x * T, $y * T, |
193 | $x * T, $y * T, |
338 | T, T, |
194 | T, T, |
339 | ($x - $corner) * T, ($y - 1) * T, 1, 1, |
195 | ($x - $cx) * T, ($y - $cy) * T, 1, 1, |
340 | "nearest", |
196 | "nearest", |
341 | 255 |
197 | 255 |
342 | ) if $corner; |
198 | ) if $corner; |
343 | } |
199 | } |
344 | } |
200 | } |
… | |
… | |
362 | ($map_pb, \%meta) |
218 | ($map_pb, \%meta) |
363 | } |
219 | } |
364 | |
220 | |
365 | for my $file (@ARGV) { |
221 | for my $file (@ARGV) { |
366 | my $mapa = read_arch $file; |
222 | my $mapa = read_arch $file; |
|
|
223 | $file =~ s/\.map$//; |
367 | my ($pb, $meta) = cfmap_render $mapa, $file; |
224 | my ($pb, $meta) = cfmap_render $mapa, $file; |
368 | $pb->save ("$file.png~", "png"); |
225 | $pb->save ("$file.png~~", "png"); |
369 | 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"; |
370 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
227 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
371 | system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; |
228 | system "optipng", "-q", "-out", "$file.png~", "$file.png~~"; |
|
|
229 | # system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
372 | unlink "$file.png~"; |
230 | unlink "$file.png~~"; |
373 | Storable::nstore $meta, "$file.pst"; |
231 | Storable::nstore $meta, "$file.pst"; |
|
|
232 | utime +(stat $file)[8,9], "$file.pst"; |
|
|
233 | utime +(stat $file)[8,9], "$file.jpg"; |
|
|
234 | utime +(stat $file)[8,9], "$file.png~"; |
|
|
235 | rename "$file.png~", "$file.png"; |
374 | } |
236 | } |
375 | |
237 | |
376 | |
238 | |
377 | |
239 | |
378 | |
240 | |