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 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 | |
5 | # tower of stars: missing craters? |
20 | # tower of stars: missing craters? |
6 | # http://cfmaps.schmorp.de/pup_land/raffle/raffle3_u2.html <-> yellow "doors" show as grey stone as surrounding |
21 | |
|
|
22 | our $VERSION = '1.1'; |
7 | |
23 | |
8 | use strict; |
24 | use strict; |
9 | |
25 | |
10 | use Storable; |
26 | use Storable; |
11 | use List::Util qw(max); |
27 | use List::Util qw(max); |
… | |
… | |
86 | s/\s+$//; |
102 | s/\s+$//; |
87 | if (/^end$/i) { |
103 | if (/^end$/i) { |
88 | last; |
104 | last; |
89 | } elsif (/^arch (\S+)$/) { |
105 | } elsif (/^arch (\S+)$/) { |
90 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
106 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
|
|
107 | } elsif (/^lore$/) { |
|
|
108 | while (<$fh>) { |
|
|
109 | last if /^endlore\s*$/i; |
|
|
110 | $arc{lore} .= $_; |
|
|
111 | } |
91 | } elsif (/^msg$/) { |
112 | } elsif (/^msg$/) { |
92 | while (<$fh>) { |
113 | while (<$fh>) { |
93 | last if /^endmsg\s*$/i; |
114 | last if /^endmsg\s*$/i; |
94 | $arc{msg} .= $_; |
115 | $arc{msg} .= $_; |
95 | } |
116 | } |
… | |
… | |
111 | $more = $prev; |
132 | $more = $prev; |
112 | } elsif (/^object (\S+)$/i) { |
133 | } elsif (/^object (\S+)$/i) { |
113 | my $name = $1; |
134 | my $name = $1; |
114 | my $arc = $parse_block->(_name => $name); |
135 | my $arc = $parse_block->(_name => $name); |
115 | |
136 | |
|
|
137 | if ($more) { |
|
|
138 | $more->{more} = $arc; |
|
|
139 | } else { |
116 | $arc{$name} = $arc; |
140 | $arc{$name} = $arc; |
117 | $more->{more} = $arc if $more; |
141 | } |
118 | |
142 | |
119 | $prev = $arc; |
143 | $prev = $arc; |
120 | $more = undef; |
144 | $more = undef; |
121 | } elsif (/^arch (\S+)$/i) { |
145 | } elsif (/^arch (\S+)$/i) { |
122 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
146 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
… | |
… | |
166 | |
190 | |
167 | my %meta; |
191 | my %meta; |
168 | |
192 | |
169 | my ($mapx, $mapy); |
193 | my ($mapx, $mapy); |
170 | |
194 | |
171 | my $map = $meta{map} = []; |
195 | my $map; |
172 | |
196 | |
173 | for (@{ $mapa->{arch} }) { |
197 | for (@{ $mapa->{arch} }) { |
|
|
198 | my ($x, $y) = ($_->{x}, $_->{y}); |
|
|
199 | |
174 | if ($_->{_name} eq "map") { |
200 | if ($_->{_name} eq "map") { |
175 | $meta{info} = $_; |
201 | $meta{info} = $_; |
176 | |
202 | |
177 | $mapx = $_->{width} || $_->{x}; |
203 | $mapx = $_->{width} || $x; |
178 | $mapy = $_->{height} || $_->{y}; |
204 | $mapy = $_->{height} || $y; |
179 | } else { |
205 | } else { |
180 | push @{ $map->[$_->{x}][$_->{y}] }, $_; |
206 | push @{ $map->[$x][$y] }, $_; |
181 | |
207 | |
182 | # arch map is unreliable w.r.t. width and height |
208 | # arch map is unreliable w.r.t. width and height |
183 | $mapx = $_->{x} + 1 if $mapx <= $_->{x}; |
209 | $mapx = $x + 1 if $mapx <= $x; |
184 | $mapy = $_->{y} + 1 if $mapy <= $_->{y}; |
210 | $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}; |
211 | #$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}; |
212 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
187 | } |
213 | } |
188 | } |
214 | } |
189 | |
215 | |
190 | $meta{width} = $mapx; |
216 | $meta{width} = $mapx; |
191 | $meta{height} = $mapy; |
217 | $meta{height} = $mapy; |
192 | |
218 | |
193 | my %map_face; |
|
|
194 | my %draw_info; |
219 | my %draw_info; |
|
|
220 | my %map_info; |
195 | |
221 | |
196 | # first pass, gather face stacking order, border and corner info |
222 | # first pass, gather face stacking order, border and corner info |
197 | for my $x (0 .. $mapx - 1) { |
223 | for my $x (0 .. $mapx - 1) { |
198 | my $col = $map->[$x]; |
224 | my $col = $map->[$x]; |
199 | for my $y (0 .. $mapy - 1) { |
225 | for my $y (0 .. $mapy - 1) { |
… | |
… | |
203 | my $a = $as->[$layer]; |
229 | my $a = $as->[$layer]; |
204 | |
230 | |
205 | my $o = $arch->{$a->{_name}} |
231 | my $o = $arch->{$a->{_name}} |
206 | or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
232 | or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
207 | |
233 | |
208 | my $A = { %$o, %$a }; |
234 | my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; |
209 | |
235 | my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
210 | my $smoothlevel = $A->{smoothlevel}; |
|
|
211 | my $level = $smoothlevel ? $smoothlevel |
236 | my $level = $smoothlevel ? $smoothlevel |
212 | : $A->{is_floor} ? 0 |
237 | : $is_floor ? 0 |
213 | : 256 + $layer; |
238 | : 256 + $layer; |
214 | |
239 | |
215 | while ($o) { |
240 | while ($o) { |
|
|
241 | my $face = $a->{face} || $o->{face}; |
|
|
242 | |
216 | my $pb = tile $A->{face} |
243 | my $pb = tile $face |
217 | or (warn "$mapname: face '$A->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
244 | or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
218 | |
245 | |
219 | my $mx = $x + $o->{x}; |
246 | my $mx = $x + $o->{x}; |
220 | my $my = $y + $o->{y}; |
247 | my $my = $y + $o->{y}; |
221 | |
248 | |
222 | last if 0 > $mx || $mx >= $mapx |
249 | last if 0 > $mx || $mx >= $mapx |
223 | || 0 > $my || $my >= $mapy; |
250 | || 0 > $my || $my >= $mapy; |
224 | |
251 | |
225 | # this is very ugly (some tiles are 32x33 or worse) |
252 | # this is very ugly (some tiles are 32x33 or worse) |
226 | my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
253 | my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
227 | |
254 | |
|
|
255 | my $dx = $bigface ? $o->{x} : 0; |
|
|
256 | my $dy = $bigface ? $o->{y} : 0; |
|
|
257 | |
|
|
258 | push @{ $map_info{$level}{$mx, $my} }, $a; |
|
|
259 | |
|
|
260 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
|
|
261 | |
228 | if (my $sface = $smooth->{$A->{face}}) { |
262 | if (my $sface = $smooth->{$face}) { |
229 | $bigface and die "can't handle bigfaces with smoothing ($A->{face})\n"; |
263 | $bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
230 | |
264 | |
231 | # full tile |
265 | # full tile |
232 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
266 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
233 | |
267 | |
234 | # borders |
268 | # borders |
… | |
… | |
242 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
276 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
243 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
277 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
244 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
278 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
245 | } |
279 | } |
246 | |
280 | |
247 | my $dx = $bigface ? $o->{x} : 0; |
|
|
248 | my $dy = $bigface ? $o->{y} : 0; |
|
|
249 | |
|
|
250 | $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
|
|
251 | |
|
|
252 | $o = $o->{more}; |
281 | $o = $o->{more}; |
253 | } |
282 | } |
254 | } |
283 | } |
255 | } |
284 | } |
256 | } |
285 | } |
257 | |
286 | |
258 | my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
287 | my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
259 | or die; |
288 | or die; |
260 | $map_pb->fill (0x00000000); |
289 | $map_pb->fill (0xffffff00); |
261 | |
290 | |
262 | # second pass, render all the stuff |
291 | # second pass, render the map |
263 | for my $level (sort { $a <=> $b } keys %draw_info) { |
292 | for my $level (sort { $a <=> $b } keys %draw_info) { |
264 | my $v = $draw_info{$level}; |
293 | my $v = $draw_info{$level}; |
265 | while (my ($sface, $info) = each %$v) { |
294 | while (my ($sface, $info) = each %$v) { |
266 | my $pb = tile $sface |
295 | my $pb = tile $sface |
267 | or die "no smooth face $sface\n"; |
296 | or die "no smooth face $sface\n"; |
… | |
… | |
314 | } |
343 | } |
315 | } |
344 | } |
316 | } |
345 | } |
317 | } |
346 | } |
318 | |
347 | |
|
|
348 | # third pass, gather meta info |
|
|
349 | for my $level (sort { $a <=> $b } keys %map_info) { |
|
|
350 | my $info = $map_info{$level}; |
|
|
351 | |
|
|
352 | while (my ($xy, $as) = each %$info) { |
|
|
353 | my ($x, $y) = split $;, $xy; |
|
|
354 | |
|
|
355 | next if $x < 0 || $x >= $mapx |
|
|
356 | || $y < 0 || $y >= $mapy; |
|
|
357 | |
|
|
358 | push @{ $meta{map}[$x][$y] }, $_ for @$as; |
|
|
359 | } |
|
|
360 | } |
|
|
361 | |
319 | ($map_pb, \%meta) |
362 | ($map_pb, \%meta) |
320 | } |
363 | } |
321 | |
364 | |
322 | for my $file (@ARGV) { |
365 | for my $file (@ARGV) { |
323 | my $mapa = read_arch $file; |
366 | my $mapa = read_arch $file; |
324 | my ($pb, $meta) = cfmap_render $mapa, $file; |
367 | my ($pb, $meta) = cfmap_render $mapa, $file; |
325 | $pb->save ("$file.png~", "png"); |
368 | $pb->save ("$file.png~", "png"); |
326 | system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
369 | system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
327 | system "mogrify", "-colors" => 65536, "$file.png~"; |
370 | #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
328 | system "pngcrush", "-q", "-m" => 7, "-rem" => "alla", "-reduce", "$file.png~", "$file.png"; |
371 | system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; |
329 | unlink "$file.png~"; |
372 | unlink "$file.png~"; |
330 | Storable::nstore $meta, "$file.pst"; |
373 | Storable::nstore $meta, "$file.pst"; |
331 | } |
374 | } |
332 | |
375 | |
333 | |
376 | |