1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | # tower of stars: missing craters? |
3 | # cfarch2png - convert crossfire maps to png+metadata |
|
|
4 | # Copyright (C) 2005 Marc Lehmann <gvpe@schmorp.de> |
|
|
5 | # |
|
|
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 |
|
|
8 | # the Free Software Foundation; either version 2 of the License, or |
|
|
9 | # (at your option) any later version. |
|
|
10 | # |
|
|
11 | # This program is distributed in the hope that it will be useful, |
|
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
14 | # GNU General Public License for more details. |
|
|
15 | # |
|
|
16 | # You should have received a copy of the GNU General Public License |
|
|
17 | # along with gvpe; if not, write to the Free Software |
|
|
18 | # Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
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 | |
|
|
33 | our $VERSION = '1.21'; |
4 | |
34 | |
5 | use strict; |
35 | use strict; |
6 | |
36 | |
7 | use Storable; |
37 | use Storable; |
8 | use List::Util qw(max); |
38 | use List::Util qw(max); |
9 | |
39 | |
10 | use Gtk2; |
40 | use Gtk2; |
11 | |
|
|
12 | our $VERSION = '1.0'; |
|
|
13 | |
41 | |
14 | #init Gtk2::Gdk; |
42 | #init Gtk2::Gdk; |
15 | |
43 | |
16 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
44 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
17 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
45 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
… | |
… | |
25 | -M "$path.pst" < -M $path |
53 | -M "$path.pst" < -M $path |
26 | && Storable::retrieve "$path.pst" |
54 | && Storable::retrieve "$path.pst" |
27 | } or do { |
55 | } or do { |
28 | my %pak; |
56 | my %pak; |
29 | |
57 | |
30 | open my $fh, "<:raw", $path |
58 | open my $fh, "<:raw:perlio", $path |
31 | or die "$_[0]: $!"; |
59 | or die "$_[0]: $!"; |
32 | while (<$fh>) { |
60 | while (<$fh>) { |
33 | my ($type, $id, $len, $path) = split; |
61 | my ($type, $id, $len, $path) = split; |
34 | $path =~ s/.*\///; |
62 | $path =~ s/.*\///; |
35 | read $fh, $pak{$path}, $len; |
63 | read $fh, $pak{$path}, $len; |
… | |
… | |
48 | -M "$path.pst" < -M $path |
76 | -M "$path.pst" < -M $path |
49 | && Storable::retrieve "$path.pst" |
77 | && Storable::retrieve "$path.pst" |
50 | } or do { |
78 | } or do { |
51 | my %smooth; |
79 | my %smooth; |
52 | |
80 | |
53 | open my $fh, "<:raw", $path |
81 | open my $fh, "<:raw:perlio", $path |
54 | or die "$path: $!"; |
82 | or die "$path: $!"; |
55 | while (<$fh>) { |
83 | while (<$fh>) { |
56 | next if /^\s*($|#)/; |
84 | next if /^\s*($|#)/; |
57 | |
85 | |
58 | $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
86 | $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
59 | } |
87 | } |
60 | |
88 | |
61 | Storable::nstore \%smooth, "$path.pst"; |
89 | Storable::nstore \%smooth, "$path.pst"; |
|
|
90 | utime +(stat $path)[8,9], "$path.pst"; |
62 | |
91 | |
63 | \%smooth |
92 | \%smooth |
64 | } |
93 | } |
65 | } |
94 | } |
66 | |
95 | |
… | |
… | |
73 | && Storable::retrieve "$path.pst" |
102 | && Storable::retrieve "$path.pst" |
74 | } or do { |
103 | } or do { |
75 | my %arc; |
104 | my %arc; |
76 | my ($more, $prev); |
105 | my ($more, $prev); |
77 | |
106 | |
78 | open my $fh, "<:raw", $path |
107 | open my $fh, "<:utf8", $path |
79 | or die "$path: $!"; |
108 | or die "$path: $!"; |
80 | |
109 | |
81 | my $parse_block; $parse_block = sub { |
110 | my $parse_block; $parse_block = sub { |
82 | my %arc = @_; |
111 | my %arc = @_; |
83 | |
112 | |
… | |
… | |
134 | } |
163 | } |
135 | } |
164 | } |
136 | |
165 | |
137 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
166 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
138 | |
167 | |
|
|
168 | if ($cache) { |
139 | Storable::nstore \%arc, "$path.pst" |
169 | Storable::nstore \%arc, "$path.pst"; |
140 | if $cache; |
170 | utime +(stat $path)[8,9], "$path.pst"; |
|
|
171 | } |
141 | |
172 | |
142 | \%arc |
173 | \%arc |
143 | } |
174 | } |
144 | } |
175 | } |
145 | |
176 | |
… | |
… | |
173 | |
204 | |
174 | my %meta; |
205 | my %meta; |
175 | |
206 | |
176 | my ($mapx, $mapy); |
207 | my ($mapx, $mapy); |
177 | |
208 | |
178 | my $map = $meta{map} = []; |
209 | my $map; |
179 | |
210 | |
180 | for (@{ $mapa->{arch} }) { |
211 | for (@{ $mapa->{arch} }) { |
|
|
212 | my ($x, $y) = ($_->{x}, $_->{y}); |
|
|
213 | |
181 | if ($_->{_name} eq "map") { |
214 | if ($_->{_name} eq "map") { |
182 | $meta{info} = $_; |
215 | $meta{info} = $_; |
183 | |
216 | |
184 | $mapx = $_->{width} || $_->{x}; |
217 | $mapx = $_->{width} || $x; |
185 | $mapy = $_->{height} || $_->{y}; |
218 | $mapy = $_->{height} || $y; |
186 | } else { |
219 | } else { |
187 | push @{ $map->[$_->{x}][$_->{y}] }, $_; |
220 | push @{ $map->[$x][$y] }, $_; |
188 | |
221 | |
189 | # arch map is unreliable w.r.t. width and height |
222 | # arch map is unreliable w.r.t. width and height |
190 | $mapx = $_->{x} + 1 if $mapx <= $_->{x}; |
223 | $mapx = $x + 1 if $mapx <= $x; |
191 | $mapy = $_->{y} + 1 if $mapy <= $_->{y}; |
224 | $mapy = $y + 1 if $mapy <= $y; |
192 | #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
225 | #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
193 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
226 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
194 | } |
227 | } |
195 | } |
228 | } |
196 | |
229 | |
197 | $meta{width} = $mapx; |
230 | $meta{width} = $mapx; |
198 | $meta{height} = $mapy; |
231 | $meta{height} = $mapy; |
199 | |
232 | |
200 | my %draw_info; |
233 | my %draw_info; |
|
|
234 | my %map_info; |
201 | |
235 | |
202 | # first pass, gather face stacking order, border and corner info |
236 | # first pass, gather face stacking order, border and corner info |
203 | for my $x (0 .. $mapx - 1) { |
237 | for my $x (0 .. $mapx - 1) { |
204 | my $col = $map->[$x]; |
238 | my $col = $map->[$x]; |
205 | for my $y (0 .. $mapy - 1) { |
239 | for my $y (0 .. $mapy - 1) { |
206 | my $as = $col->[$y] || []; |
240 | my $as = $col->[$y] || []; |
207 | |
241 | |
|
|
242 | my $minsmooth = 0; |
|
|
243 | |
208 | for my $layer (0 .. $#$as) { |
244 | for my $layer (0 .. $#$as) { |
209 | my $a = $as->[$layer]; |
245 | my $a = $as->[$layer]; |
210 | |
246 | |
211 | my $o = $arch->{$a->{_name}} |
247 | my $o = $arch->{$a->{_name}} |
212 | or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
248 | or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
213 | |
249 | |
214 | my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; |
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 | |
215 | my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
257 | #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
216 | my $level = $smoothlevel ? $smoothlevel |
258 | my $level = $smoothlevel + $layer * 256; |
217 | : $is_floor ? 0 |
259 | |
218 | : 256 + $layer; |
260 | $level -= 100 * 256 if $o->{_name} eq "blocked"; |
219 | |
261 | |
220 | while ($o) { |
262 | while ($o) { |
221 | my $face = $a->{face} || $o->{face}; |
263 | my $face = $a->{face} || $o->{face}; |
222 | |
264 | |
223 | my $pb = tile $face |
265 | my $pb = tile $face |
… | |
… | |
229 | last if 0 > $mx || $mx >= $mapx |
271 | last if 0 > $mx || $mx >= $mapx |
230 | || 0 > $my || $my >= $mapy; |
272 | || 0 > $my || $my >= $mapy; |
231 | |
273 | |
232 | # this is very ugly (some tiles are 32x33 or worse) |
274 | # this is very ugly (some tiles are 32x33 or worse) |
233 | my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
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; |
|
|
281 | |
|
|
282 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
234 | |
283 | |
235 | if (my $sface = $smooth->{$face}) { |
284 | if (my $sface = $smooth->{$face}) { |
236 | $bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
285 | $bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
237 | |
286 | |
238 | # full tile |
287 | # full tile |
… | |
… | |
249 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
298 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
250 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
299 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
251 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
300 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
252 | } |
301 | } |
253 | |
302 | |
254 | my $dx = $bigface ? $o->{x} : 0; |
|
|
255 | my $dy = $bigface ? $o->{y} : 0; |
|
|
256 | |
|
|
257 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
|
|
258 | |
|
|
259 | $o = $o->{more}; |
303 | $o = $o->{more}; |
|
|
304 | $level = ($layer + 1000) * 2; # put "big things" on top, no matter what |
260 | } |
305 | } |
261 | } |
306 | } |
262 | } |
307 | } |
263 | } |
308 | } |
264 | |
309 | |
265 | my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
310 | my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
266 | or die; |
311 | or die; |
267 | $map_pb->fill (0x00000000); |
312 | $map_pb->fill (0xffffff00); |
268 | |
313 | |
269 | # second pass, render all the stuff |
314 | # second pass, render the map |
270 | for my $level (sort { $a <=> $b } keys %draw_info) { |
315 | for my $level (sort { $a <=> $b } keys %draw_info) { |
271 | my $v = $draw_info{$level}; |
316 | my $v = $draw_info{$level}; |
272 | while (my ($sface, $info) = each %$v) { |
317 | while (my ($sface, $info) = each %$v) { |
273 | my $pb = tile $sface |
318 | my $pb = tile $sface |
274 | or die "no smooth face $sface\n"; |
319 | or die "no smooth face $sface\n"; |
… | |
… | |
277 | my ($x, $y) = split $;, $xy; |
322 | my ($x, $y) = split $;, $xy; |
278 | |
323 | |
279 | next if $x < 0 || $x >= $mapx |
324 | next if $x < 0 || $x >= $mapx |
280 | || $y < 0 || $y >= $mapy; |
325 | || $y < 0 || $y >= $mapy; |
281 | |
326 | |
282 | # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB |
327 | # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb |
283 | # X don't draw |
|
|
284 | # F full tile draw with x|y bigface displacement |
328 | # f full tile draw with x|y bigface displacement |
285 | # c maybe draw these corners |
|
|
286 | # C do not draw these corners |
329 | # n do not draw borders&corners |
|
|
330 | # c draw these corners, but... |
|
|
331 | # C ... not these |
287 | # b draw these borders |
332 | # b draw these borders |
288 | |
333 | |
289 | if ($bits & 0x2000) { |
334 | if ($bits & 0x2000) { |
290 | my $dx = (($bits >> 24) & 0xff) - 128; |
335 | my $dx = (($bits >> 24) & 0xff) - 128; |
291 | my $dy = (($bits >> 16) & 0xff) - 128; |
336 | my $dy = (($bits >> 16) & 0xff) - 128; |
… | |
… | |
321 | } |
366 | } |
322 | } |
367 | } |
323 | } |
368 | } |
324 | } |
369 | } |
325 | |
370 | |
|
|
371 | # third pass, gather meta info |
|
|
372 | for my $level (sort { $a <=> $b } keys %map_info) { |
|
|
373 | my $info = $map_info{$level}; |
|
|
374 | |
|
|
375 | while (my ($xy, $as) = each %$info) { |
|
|
376 | my ($x, $y) = split $;, $xy; |
|
|
377 | |
|
|
378 | next if $x < 0 || $x >= $mapx |
|
|
379 | || $y < 0 || $y >= $mapy; |
|
|
380 | |
|
|
381 | push @{ $meta{map}[$x][$y] }, $_ for @$as; |
|
|
382 | } |
|
|
383 | } |
|
|
384 | |
326 | ($map_pb, \%meta) |
385 | ($map_pb, \%meta) |
327 | } |
386 | } |
328 | |
387 | |
329 | for my $file (@ARGV) { |
388 | for my $file (@ARGV) { |
330 | my $mapa = read_arch $file; |
389 | my $mapa = read_arch $file; |
331 | my ($pb, $meta) = cfmap_render $mapa, $file; |
390 | my ($pb, $meta) = cfmap_render $mapa, $file; |
332 | $pb->save ("$file.png~", "png"); |
391 | $pb->save ("$file.png~~", "png"); |
333 | system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
392 | system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
334 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
393 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
335 | system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; |
394 | system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~"; |
|
|
395 | # system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
336 | unlink "$file.png~"; |
396 | unlink "$file.png~~"; |
337 | Storable::nstore $meta, "$file.pst"; |
397 | Storable::nstore $meta, "$file.pst"; |
|
|
398 | utime +(stat $file)[8,9], "$file.pst"; |
|
|
399 | utime +(stat $file)[8,9], "$file.jpg"; |
|
|
400 | utime +(stat $file)[8,9], "$file.png~"; |
|
|
401 | rename "$file.png~", "$file.png"; |
338 | } |
402 | } |
339 | |
403 | |
340 | |
404 | |
341 | |
405 | |
342 | |
406 | |