1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | # bugs: http://cfmaps.schmorp.de/brest/apartments/brest_town_house.html <- walls |
3 | # cfarch2png - convert crossfire maps to png+metadata |
4 | # whaling... icecaves... water is red? |
4 | # Copyright (C) 2005,2007 Marc Lehmann <cfmaps@schmorp.de> |
5 | # tower of stars: missing craters? |
5 | # |
6 | # http://cfmaps.schmorp.de/pup_land/raffle/raffle3_u2.html <-> yellow "doors" show as grey stone as surrounding |
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.212'; |
7 | |
34 | |
8 | use strict; |
35 | use strict; |
9 | |
36 | |
10 | use Storable; |
37 | use Storable; |
11 | use List::Util qw(max); |
38 | use List::Util qw(max); |
… | |
… | |
26 | -M "$path.pst" < -M $path |
53 | -M "$path.pst" < -M $path |
27 | && Storable::retrieve "$path.pst" |
54 | && Storable::retrieve "$path.pst" |
28 | } or do { |
55 | } or do { |
29 | my %pak; |
56 | my %pak; |
30 | |
57 | |
31 | open my $fh, "<:raw", $path |
58 | open my $fh, "<:raw:perlio", $path |
32 | or die "$_[0]: $!"; |
59 | or die "$_[0]: $!"; |
33 | while (<$fh>) { |
60 | while (<$fh>) { |
34 | my ($type, $id, $len, $path) = split; |
61 | my ($type, $id, $len, $path) = split; |
35 | $path =~ s/.*\///; |
62 | $path =~ s/.*\///; |
36 | read $fh, $pak{$path}, $len; |
63 | read $fh, $pak{$path}, $len; |
… | |
… | |
49 | -M "$path.pst" < -M $path |
76 | -M "$path.pst" < -M $path |
50 | && Storable::retrieve "$path.pst" |
77 | && Storable::retrieve "$path.pst" |
51 | } or do { |
78 | } or do { |
52 | my %smooth; |
79 | my %smooth; |
53 | |
80 | |
54 | open my $fh, "<:raw", $path |
81 | open my $fh, "<:raw:perlio", $path |
55 | or die "$path: $!"; |
82 | or die "$path: $!"; |
56 | while (<$fh>) { |
83 | while (<$fh>) { |
57 | next if /^\s*($|#)/; |
84 | next if /^\s*($|#)/; |
58 | |
85 | |
59 | $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
86 | $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
60 | } |
87 | } |
61 | |
88 | |
62 | Storable::nstore \%smooth, "$path.pst"; |
89 | Storable::nstore \%smooth, "$path.pst"; |
|
|
90 | utime +(stat $path)[8,9], "$path.pst"; |
63 | |
91 | |
64 | \%smooth |
92 | \%smooth |
65 | } |
93 | } |
66 | } |
94 | } |
67 | |
95 | |
… | |
… | |
74 | && Storable::retrieve "$path.pst" |
102 | && Storable::retrieve "$path.pst" |
75 | } or do { |
103 | } or do { |
76 | my %arc; |
104 | my %arc; |
77 | my ($more, $prev); |
105 | my ($more, $prev); |
78 | |
106 | |
79 | open my $fh, "<:raw", $path |
107 | open my $fh, "<:utf8", $path |
80 | or die "$path: $!"; |
108 | or die "$path: $!"; |
81 | |
109 | |
82 | my $parse_block; $parse_block = sub { |
110 | my $parse_block; $parse_block = sub { |
83 | my %arc = @_; |
111 | my %arc = @_; |
84 | |
112 | |
85 | while (<$fh>) { |
113 | while (<$fh>) { |
86 | s/\s+$//; |
114 | s/\s+$//; |
87 | if (/^end$/i) { |
115 | if (/^end$/i) { |
88 | last; |
116 | last; |
89 | } elsif (/^arch (\S+)$/) { |
117 | } elsif (/^arch (\S+)$/) { |
90 | push @{ $arc{subarch} }, $parse_block->(_name => $1); |
118 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
|
|
119 | } elsif (/^lore$/) { |
|
|
120 | while (<$fh>) { |
|
|
121 | last if /^endlore\s*$/i; |
|
|
122 | $arc{lore} .= $_; |
|
|
123 | } |
91 | } elsif (/^msg$/) { |
124 | } elsif (/^msg$/) { |
92 | while (<$fh>) { |
125 | while (<$fh>) { |
93 | last if /^endmsg\s*$/i; |
126 | last if /^endmsg\s*$/i; |
94 | $arc{msg} .= $_; |
127 | $arc{msg} .= $_; |
95 | } |
128 | } |
… | |
… | |
111 | $more = $prev; |
144 | $more = $prev; |
112 | } elsif (/^object (\S+)$/i) { |
145 | } elsif (/^object (\S+)$/i) { |
113 | my $name = $1; |
146 | my $name = $1; |
114 | my $arc = $parse_block->(_name => $name); |
147 | my $arc = $parse_block->(_name => $name); |
115 | |
148 | |
|
|
149 | if ($more) { |
|
|
150 | $more->{more} = $arc; |
|
|
151 | } else { |
116 | $arc{$name} = $arc; |
152 | $arc{$name} = $arc; |
117 | $more->{more} = $arc if $more; |
153 | } |
118 | |
154 | |
119 | $prev = $arc; |
155 | $prev = $arc; |
120 | $more = undef; |
156 | $more = undef; |
121 | } elsif (/^arch (\S+)$/i) { |
157 | } elsif (/^arch (\S+)$/i) { |
122 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
158 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
… | |
… | |
127 | } |
163 | } |
128 | } |
164 | } |
129 | |
165 | |
130 | 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. |
131 | |
167 | |
|
|
168 | if ($cache) { |
132 | Storable::nstore \%arc, "$path.pst" |
169 | Storable::nstore \%arc, "$path.pst"; |
133 | if $cache; |
170 | utime +(stat $path)[8,9], "$path.pst"; |
|
|
171 | } |
134 | |
172 | |
135 | \%arc |
173 | \%arc |
136 | } |
174 | } |
137 | } |
175 | } |
138 | |
176 | |
… | |
… | |
166 | |
204 | |
167 | my %meta; |
205 | my %meta; |
168 | |
206 | |
169 | my ($mapx, $mapy); |
207 | my ($mapx, $mapy); |
170 | |
208 | |
171 | my $map = $meta{map} = []; |
209 | my $map; |
172 | |
210 | |
173 | for (@{ $mapa->{arch} }) { |
211 | for (@{ $mapa->{arch} }) { |
|
|
212 | my ($x, $y) = ($_->{x}, $_->{y}); |
|
|
213 | |
174 | if ($_->{_name} eq "map") { |
214 | if ($_->{_name} eq "map") { |
175 | $meta{info} = $_; |
215 | $meta{info} = $_; |
176 | |
216 | |
177 | $mapx = $_->{width} || $_->{x}; |
217 | $mapx = $_->{width} || $x; |
178 | $mapy = $_->{height} || $_->{y}; |
218 | $mapy = $_->{height} || $y; |
179 | } else { |
219 | } else { |
180 | push @{ $map->[$_->{x}][$_->{y}] }, $_; |
220 | push @{ $map->[$x][$y] }, $_; |
181 | |
221 | |
182 | # arch map is unreliable w.r.t. width and height |
222 | # arch map is unreliable w.r.t. width and height |
183 | $mapx = $_->{x} + 1 if $mapx <= $_->{x}; |
223 | $mapx = $x + 1 if $mapx <= $x; |
184 | $mapy = $_->{y} + 1 if $mapy <= $_->{y}; |
224 | $mapy = $y + 1 if $mapy <= $y; |
185 | #$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}; |
186 | #$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}; |
187 | } |
227 | } |
188 | } |
228 | } |
189 | |
229 | |
190 | $meta{width} = $mapx; |
230 | $meta{width} = $mapx; |
191 | $meta{height} = $mapy; |
231 | $meta{height} = $mapy; |
192 | |
232 | |
193 | my %map_face; |
|
|
194 | my %draw_info; |
233 | my %draw_info; |
195 | |
234 | my %map_info; |
196 | my $OBJ_LEVEL = 2**32; # higher than any valid (or in-use) smooth_level |
|
|
197 | |
235 | |
198 | # first pass, gather face stacking order, border and corner info |
236 | # first pass, gather face stacking order, border and corner info |
199 | for my $x (0 .. $mapx - 1) { |
237 | for my $x (0 .. $mapx - 1) { |
200 | my $col = $map->[$x]; |
238 | my $col = $map->[$x]; |
201 | for my $y (0 .. $mapy - 1) { |
239 | for my $y (0 .. $mapy - 1) { |
202 | my $as = $col->[$y] || []; |
240 | my $as = $col->[$y] || []; |
203 | |
241 | |
|
|
242 | my $minsmooth = 0; |
|
|
243 | |
204 | for my $layer (0 .. $#$as) { |
244 | for my $layer (0 .. $#$as) { |
205 | my $a = $as->[$layer]; |
245 | my $a = $as->[$layer]; |
206 | |
246 | |
207 | my $o = $arch->{$a->{_name}} |
247 | my $o = $arch->{$a->{_name}} |
208 | 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; |
209 | |
249 | |
210 | $o = $arch->{$o->{other_arch}} while $o->{other_arch} && !$o->{face}; |
250 | my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; |
211 | |
251 | |
212 | $o or die "arch $a->{_name} undefined at ($x|$y)\n"; |
252 | # hack to ensure somewhat correct ordering in case of conflicting |
213 | |
253 | # smoothlevel/stacking order |
|
|
254 | $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel; |
214 | my $smooth_level = $o->{smoothlevel}; |
255 | $minsmooth = $smoothlevel; |
215 | my $level = $smooth_level ? $smooth_level |
256 | |
216 | : $o->{is_floor} ? 0 |
257 | #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
217 | : $OBJ_LEVEL + $layer; |
258 | my $level = $smoothlevel + $layer * 256; |
|
|
259 | |
|
|
260 | $level -= 100 * 256 if $o->{_name} eq "blocked"; |
218 | |
261 | |
219 | while ($o) { |
262 | while ($o) { |
|
|
263 | my $face = $a->{face} || $o->{face}; |
|
|
264 | |
220 | my $pb = tile $o->{face} |
265 | my $pb = tile $face |
221 | or (warn "$mapname: face '$o->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
266 | or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
222 | |
267 | |
223 | my $mx = $x + $o->{x}; |
268 | my $mx = $x + $o->{x}; |
224 | my $my = $y + $o->{y}; |
269 | my $my = $y + $o->{y}; |
225 | |
270 | |
226 | last if $mx >= $mapx |
271 | last if 0 > $mx || $mx >= $mapx |
227 | || $my >= $mapy; |
272 | || 0 > $my || $my >= $mapy; |
228 | |
273 | |
229 | # this is very ugly (some tiles are 32x33 or worse) |
274 | # this is very ugly (some tiles are 32x33 or worse) |
230 | 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; |
231 | |
276 | |
232 | if (my $sface = $smooth->{$o->{face}}) { |
|
|
233 | $bigface and die "can't handle big faces with smoothing ($o->{face})\n"; |
|
|
234 | |
|
|
235 | # full tile |
|
|
236 | $draw_info{$smooth_level}{$sface}{$mx , $my } |= 0x1000; |
|
|
237 | |
|
|
238 | # borders |
|
|
239 | $draw_info{$smooth_level}{$sface}{$mx + 1, $my } |= 0x0031; |
|
|
240 | $draw_info{$smooth_level}{$sface}{$mx , $my + 1} |= 0x0092; |
|
|
241 | $draw_info{$smooth_level}{$sface}{$mx - 1, $my } |= 0x0064; |
|
|
242 | $draw_info{$smooth_level}{$sface}{$mx , $my - 1} |= 0x00c8; |
|
|
243 | |
|
|
244 | # corners |
|
|
245 | $draw_info{$smooth_level}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
|
|
246 | $draw_info{$smooth_level}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
|
|
247 | $draw_info{$smooth_level}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
|
|
248 | $draw_info{$smooth_level}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
|
|
249 | } else { |
|
|
250 | $smooth_level = 0; |
|
|
251 | } |
|
|
252 | |
|
|
253 | my $dx = $bigface ? $o->{x} : 0; |
277 | my $dx = $bigface ? $o->{x} : 0; |
254 | my $dy = $bigface ? $o->{y} : 0; |
278 | my $dy = $bigface ? $o->{y} : 0; |
255 | |
279 | |
|
|
280 | push @{ $map_info{$level}{$mx, $my} }, $a; |
|
|
281 | |
256 | $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
282 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
|
|
283 | |
|
|
284 | if (my $sface = $smooth->{$face}) { |
|
|
285 | $bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
|
|
286 | |
|
|
287 | # full tile |
|
|
288 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
|
|
289 | |
|
|
290 | # borders |
|
|
291 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0091; |
|
|
292 | $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0032; |
|
|
293 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; |
|
|
294 | $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; |
|
|
295 | |
|
|
296 | # corners |
|
|
297 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
|
|
298 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
|
|
299 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
|
|
300 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
|
|
301 | } |
257 | |
302 | |
258 | $o = $o->{more}; |
303 | $o = $o->{more}; |
|
|
304 | $level = ($layer + 1000) * 2; # put "big things" on top, no matter what |
259 | } |
305 | } |
260 | } |
306 | } |
261 | } |
307 | } |
262 | } |
308 | } |
263 | |
309 | |
264 | 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 |
265 | or die; |
311 | or die; |
266 | $map_pb->fill (0x00000000); |
312 | $map_pb->fill (0xffffff00); |
267 | |
313 | |
268 | # second pass, render all the stuff |
314 | # second pass, render the map |
269 | for my $level (sort { $a <=> $b } keys %draw_info) { |
315 | for my $level (sort { $a <=> $b } keys %draw_info) { |
270 | my $v = $draw_info{$level}; |
316 | my $v = $draw_info{$level}; |
271 | while (my ($sface, $info) = each %$v) { |
317 | while (my ($sface, $info) = each %$v) { |
272 | my $pb = tile $sface |
318 | my $pb = tile $sface |
273 | or die "no smooth face $sface\n"; |
319 | or die "no smooth face $sface\n"; |
… | |
… | |
276 | my ($x, $y) = split $;, $xy; |
322 | my ($x, $y) = split $;, $xy; |
277 | |
323 | |
278 | next if $x < 0 || $x >= $mapx |
324 | next if $x < 0 || $x >= $mapx |
279 | || $y < 0 || $y >= $mapy; |
325 | || $y < 0 || $y >= $mapy; |
280 | |
326 | |
281 | # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB |
327 | # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb |
282 | # X don't draw |
|
|
283 | # F full tile draw with x|y bigface displacement |
328 | # f full tile draw with x|y bigface displacement |
284 | # c maybe draw these corners |
|
|
285 | # C do not draw these corners |
329 | # n do not draw borders&corners |
|
|
330 | # c draw these corners, but... |
|
|
331 | # C ... not these |
286 | # b draw these borders |
332 | # b draw these borders |
287 | |
333 | |
288 | if ($bits & 0x2000) { |
334 | if ($bits & 0x2000) { |
289 | my $dx = (($bits >> 24) & 0xff) - 128; |
335 | my $dx = (($bits >> 24) & 0xff) - 128; |
290 | my $dy = (($bits >> 16) & 0xff) - 128; |
336 | my $dy = (($bits >> 16) & 0xff) - 128; |
… | |
… | |
320 | } |
366 | } |
321 | } |
367 | } |
322 | } |
368 | } |
323 | } |
369 | } |
324 | |
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 | |
325 | ($map_pb, \%meta) |
385 | ($map_pb, \%meta) |
326 | } |
386 | } |
327 | |
387 | |
328 | for my $file (@ARGV) { |
388 | for my $file (@ARGV) { |
329 | my $mapa = read_arch $file; |
389 | my $mapa = read_arch $file; |
|
|
390 | $file =~ s/\.map$//; |
330 | my ($pb, $meta) = cfmap_render $mapa, $file; |
391 | my ($pb, $meta) = cfmap_render $mapa, $file; |
331 | $pb->save ("$file.png", "png"); |
392 | $pb->save ("$file.png~~", "png"); |
|
|
393 | system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
|
|
394 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
|
|
395 | system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~"; |
|
|
396 | # system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
|
|
397 | unlink "$file.png~~"; |
332 | Storable::nstore $meta, "$file.pst"; |
398 | Storable::nstore $meta, "$file.pst"; |
|
|
399 | utime +(stat $file)[8,9], "$file.pst"; |
|
|
400 | utime +(stat $file)[8,9], "$file.jpg"; |
|
|
401 | utime +(stat $file)[8,9], "$file.png~"; |
|
|
402 | rename "$file.png~", "$file.png"; |
333 | } |
403 | } |
334 | |
404 | |
335 | |
405 | |
336 | |
406 | |
337 | |
407 | |