1 |
#!/opt/bin/perl |
2 |
|
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 |
our $VERSION = '1.2'; |
21 |
|
22 |
use strict; |
23 |
|
24 |
use Storable; |
25 |
use List::Util qw(max); |
26 |
|
27 |
use Gtk2; |
28 |
|
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 } |
35 |
|
36 |
sub read_pak($) { |
37 |
my ($path) = @_; |
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 |
|
189 |
sub cfmap_render($;$) { |
190 |
my ($mapa, $mapname) = @_; |
191 |
|
192 |
my %meta; |
193 |
|
194 |
my ($mapx, $mapy); |
195 |
|
196 |
my $map; |
197 |
|
198 |
for (@{ $mapa->{arch} }) { |
199 |
my ($x, $y) = ($_->{x}, $_->{y}); |
200 |
|
201 |
if ($_->{_name} eq "map") { |
202 |
$meta{info} = $_; |
203 |
|
204 |
$mapx = $_->{width} || $x; |
205 |
$mapy = $_->{height} || $y; |
206 |
} else { |
207 |
push @{ $map->[$x][$y] }, $_; |
208 |
|
209 |
# arch map is unreliable w.r.t. width and height |
210 |
$mapx = $x + 1 if $mapx <= $x; |
211 |
$mapy = $y + 1 if $mapy <= $y; |
212 |
#$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
213 |
#$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
214 |
} |
215 |
} |
216 |
|
217 |
$meta{width} = $mapx; |
218 |
$meta{height} = $mapy; |
219 |
|
220 |
my %draw_info; |
221 |
my %map_info; |
222 |
|
223 |
# first pass, gather face stacking order, border and corner info |
224 |
for my $x (0 .. $mapx - 1) { |
225 |
my $col = $map->[$x]; |
226 |
for my $y (0 .. $mapy - 1) { |
227 |
my $as = $col->[$y] || []; |
228 |
|
229 |
my $minsmooth = 0; |
230 |
|
231 |
for my $layer (0 .. $#$as) { |
232 |
my $a = $as->[$layer]; |
233 |
|
234 |
my $o = $arch->{$a->{_name}} |
235 |
or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
236 |
|
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; |
246 |
|
247 |
$level -= 100 * 256 if $o->{_name} eq "blocked"; |
248 |
|
249 |
while ($o) { |
250 |
my $face = $a->{face} || $o->{face}; |
251 |
|
252 |
my $pb = tile $face |
253 |
or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
254 |
|
255 |
my $mx = $x + $o->{x}; |
256 |
my $my = $y + $o->{y}; |
257 |
|
258 |
last if 0 > $mx || $mx >= $mapx |
259 |
|| 0 > $my || $my >= $mapy; |
260 |
|
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; |
268 |
|
269 |
$draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
270 |
|
271 |
if (my $sface = $smooth->{$face}) { |
272 |
$bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
273 |
|
274 |
# full tile |
275 |
$draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
276 |
|
277 |
# borders |
278 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; |
279 |
$draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; |
280 |
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; |
281 |
$draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; |
282 |
|
283 |
# corners |
284 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
285 |
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
286 |
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
287 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
288 |
} |
289 |
|
290 |
$o = $o->{more}; |
291 |
$level = ($layer + 1000) * 2; # put "big things" on top, no matter what |
292 |
} |
293 |
} |
294 |
} |
295 |
} |
296 |
|
297 |
my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
298 |
or die; |
299 |
$map_pb->fill (0xffffff00); |
300 |
|
301 |
# second pass, render the map |
302 |
for my $level (sort { $a <=> $b } keys %draw_info) { |
303 |
my $v = $draw_info{$level}; |
304 |
while (my ($sface, $info) = each %$v) { |
305 |
my $pb = tile $sface |
306 |
or die "no smooth face $sface\n"; |
307 |
|
308 |
while (my ($xy, $bits) = each %$info) { |
309 |
my ($x, $y) = split $;, $xy; |
310 |
|
311 |
next if $x < 0 || $x >= $mapx |
312 |
|| $y < 0 || $y >= $mapy; |
313 |
|
314 |
# bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb |
315 |
# f full tile draw with x|y bigface displacement |
316 |
# n do not draw borders&corners |
317 |
# c draw these corners, but... |
318 |
# C ... not these |
319 |
# b draw these borders |
320 |
|
321 |
if ($bits & 0x2000) { |
322 |
my $dx = (($bits >> 24) & 0xff) - 128; |
323 |
my $dy = (($bits >> 16) & 0xff) - 128; |
324 |
|
325 |
$pb->composite ($map_pb, |
326 |
$x * T, $y * T, |
327 |
T, T, |
328 |
($x - $dx) * T, ($y - $dy) * T, 1, 1, |
329 |
"nearest", |
330 |
255 |
331 |
); |
332 |
} |
333 |
|
334 |
unless ($bits & 0x1000) { |
335 |
my $border = $bits & 0xf; |
336 |
my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; |
337 |
|
338 |
$pb->composite ($map_pb, |
339 |
$x * T, $y * T, |
340 |
T, T, |
341 |
($x - $border) * T, $y * T, 1, 1, |
342 |
"nearest", |
343 |
255 |
344 |
) if $border; |
345 |
|
346 |
$pb->composite ($map_pb, |
347 |
$x * T, $y * T, |
348 |
T, T, |
349 |
($x - $corner) * T, ($y - 1) * T, 1, 1, |
350 |
"nearest", |
351 |
255 |
352 |
) if $corner; |
353 |
} |
354 |
} |
355 |
} |
356 |
} |
357 |
|
358 |
# third pass, gather meta info |
359 |
for my $level (sort { $a <=> $b } keys %map_info) { |
360 |
my $info = $map_info{$level}; |
361 |
|
362 |
while (my ($xy, $as) = each %$info) { |
363 |
my ($x, $y) = split $;, $xy; |
364 |
|
365 |
next if $x < 0 || $x >= $mapx |
366 |
|| $y < 0 || $y >= $mapy; |
367 |
|
368 |
push @{ $meta{map}[$x][$y] }, $_ for @$as; |
369 |
} |
370 |
} |
371 |
|
372 |
($map_pb, \%meta) |
373 |
} |
374 |
|
375 |
for my $file (@ARGV) { |
376 |
my $mapa = read_arch $file; |
377 |
my ($pb, $meta) = cfmap_render $mapa, $file; |
378 |
$pb->save ("$file.png~~", "png"); |
379 |
system "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
380 |
#system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
381 |
system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~"; |
382 |
# system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
383 |
unlink "$file.png~~"; |
384 |
Storable::nstore $meta, "$file.pst"; |
385 |
utime +(stat $file)[8,9], "$file.pst"; |
386 |
utime +(stat $file)[8,9], "$file.png~"; |
387 |
rename "$file.png~", "$file.png"; |
388 |
} |
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|