1 |
root |
1.1 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
root |
1.8 |
# 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 |
root |
1.1 |
# tower of stars: missing craters? |
21 |
root |
1.12 |
# world_108_123 (8|18), hole below grass but shouldn't? |
22 |
root |
1.1 |
|
23 |
root |
1.11 |
our $VERSION = '1.11'; |
24 |
root |
1.9 |
|
25 |
root |
1.1 |
use strict; |
26 |
|
|
|
27 |
|
|
use Storable; |
28 |
|
|
use List::Util qw(max); |
29 |
|
|
|
30 |
|
|
use Gtk2; |
31 |
|
|
|
32 |
|
|
#init Gtk2::Gdk; |
33 |
|
|
|
34 |
|
|
my $LIB = $ENV{CROSSFIRE_LIBDIR} |
35 |
|
|
or die "\$CROSSFIRE_LIBDIR must be set\n"; |
36 |
|
|
|
37 |
|
|
sub T (){ 32 } |
38 |
|
|
|
39 |
|
|
sub read_pak($) { |
40 |
|
|
my ($path) = @_; |
41 |
|
|
|
42 |
|
|
eval { |
43 |
|
|
-M "$path.pst" < -M $path |
44 |
|
|
&& Storable::retrieve "$path.pst" |
45 |
|
|
} or do { |
46 |
|
|
my %pak; |
47 |
|
|
|
48 |
|
|
open my $fh, "<:raw", $path |
49 |
|
|
or die "$_[0]: $!"; |
50 |
|
|
while (<$fh>) { |
51 |
|
|
my ($type, $id, $len, $path) = split; |
52 |
|
|
$path =~ s/.*\///; |
53 |
|
|
read $fh, $pak{$path}, $len; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
Storable::nstore \%pak, "$path.pst"; |
57 |
|
|
|
58 |
|
|
\%pak |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
sub read_smooth($) { |
63 |
|
|
my ($path) = @_; |
64 |
|
|
|
65 |
|
|
eval { |
66 |
|
|
-M "$path.pst" < -M $path |
67 |
|
|
&& Storable::retrieve "$path.pst" |
68 |
|
|
} or do { |
69 |
|
|
my %smooth; |
70 |
|
|
|
71 |
|
|
open my $fh, "<:raw", $path |
72 |
|
|
or die "$path: $!"; |
73 |
|
|
while (<$fh>) { |
74 |
|
|
next if /^\s*($|#)/; |
75 |
|
|
|
76 |
|
|
$smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
Storable::nstore \%smooth, "$path.pst"; |
80 |
|
|
|
81 |
|
|
\%smooth |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
sub read_arch($;$) { |
86 |
|
|
my ($path, $cache) = @_; |
87 |
|
|
|
88 |
|
|
eval { |
89 |
|
|
$cache |
90 |
|
|
&& -M "$path.pst" < -M $path |
91 |
|
|
&& Storable::retrieve "$path.pst" |
92 |
|
|
} or do { |
93 |
|
|
my %arc; |
94 |
|
|
my ($more, $prev); |
95 |
|
|
|
96 |
|
|
open my $fh, "<:raw", $path |
97 |
|
|
or die "$path: $!"; |
98 |
|
|
|
99 |
|
|
my $parse_block; $parse_block = sub { |
100 |
|
|
my %arc = @_; |
101 |
|
|
|
102 |
|
|
while (<$fh>) { |
103 |
|
|
s/\s+$//; |
104 |
|
|
if (/^end$/i) { |
105 |
|
|
last; |
106 |
|
|
} elsif (/^arch (\S+)$/) { |
107 |
root |
1.3 |
push @{ $arc{inventory} }, $parse_block->(_name => $1); |
108 |
root |
1.4 |
} elsif (/^lore$/) { |
109 |
|
|
while (<$fh>) { |
110 |
|
|
last if /^endlore\s*$/i; |
111 |
|
|
$arc{lore} .= $_; |
112 |
|
|
} |
113 |
root |
1.1 |
} elsif (/^msg$/) { |
114 |
|
|
while (<$fh>) { |
115 |
|
|
last if /^endmsg\s*$/i; |
116 |
|
|
$arc{msg} .= $_; |
117 |
|
|
} |
118 |
|
|
} elsif (/^(\S+)\s*(.*)$/) { |
119 |
|
|
$arc{lc $1} = $2; |
120 |
|
|
} elsif (/^\s*($|#)/) { |
121 |
|
|
# |
122 |
|
|
} else { |
123 |
|
|
warn "$path: unparsable line '$_' in arch $arc{_name}"; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
\%arc |
128 |
|
|
}; |
129 |
|
|
|
130 |
|
|
while (<$fh>) { |
131 |
|
|
s/\s+$//; |
132 |
|
|
if (/^more$/i) { |
133 |
|
|
$more = $prev; |
134 |
|
|
} elsif (/^object (\S+)$/i) { |
135 |
|
|
my $name = $1; |
136 |
|
|
my $arc = $parse_block->(_name => $name); |
137 |
|
|
|
138 |
root |
1.4 |
if ($more) { |
139 |
|
|
$more->{more} = $arc; |
140 |
|
|
} else { |
141 |
|
|
$arc{$name} = $arc; |
142 |
|
|
} |
143 |
root |
1.1 |
|
144 |
|
|
$prev = $arc; |
145 |
|
|
$more = undef; |
146 |
|
|
} elsif (/^arch (\S+)$/i) { |
147 |
|
|
push @{ $arc{arch} }, $parse_block->(_name => $1); |
148 |
|
|
} elsif (/^\s*($|#)/) { |
149 |
|
|
# |
150 |
|
|
} else { |
151 |
|
|
warn "$path: unparseable top-level line '$_'"; |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
undef $parse_block; # work around bug in perl not freeing $fh etc. |
156 |
|
|
|
157 |
|
|
Storable::nstore \%arc, "$path.pst" |
158 |
|
|
if $cache; |
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 |
root |
1.10 |
my $map; |
197 |
root |
1.1 |
|
198 |
|
|
for (@{ $mapa->{arch} }) { |
199 |
root |
1.10 |
my ($x, $y) = ($_->{x}, $_->{y}); |
200 |
|
|
|
201 |
root |
1.1 |
if ($_->{_name} eq "map") { |
202 |
|
|
$meta{info} = $_; |
203 |
|
|
|
204 |
root |
1.10 |
$mapx = $_->{width} || $x; |
205 |
|
|
$mapy = $_->{height} || $y; |
206 |
root |
1.1 |
} else { |
207 |
root |
1.10 |
push @{ $map->[$x][$y] }, $_; |
208 |
root |
1.1 |
|
209 |
|
|
# arch map is unreliable w.r.t. width and height |
210 |
root |
1.10 |
$mapx = $x + 1 if $mapx <= $x; |
211 |
|
|
$mapy = $y + 1 if $mapy <= $y; |
212 |
root |
1.1 |
#$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 |
root |
1.10 |
my %map_info; |
222 |
root |
1.1 |
|
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 |
|
|
for my $layer (0 .. $#$as) { |
230 |
|
|
my $a = $as->[$layer]; |
231 |
|
|
|
232 |
root |
1.3 |
my $o = $arch->{$a->{_name}} |
233 |
root |
1.1 |
or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
234 |
|
|
|
235 |
root |
1.5 |
my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; |
236 |
|
|
my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; |
237 |
|
|
my $level = $smoothlevel ? $smoothlevel |
238 |
root |
1.11 |
: $is_floor ? $layer - 1000 |
239 |
|
|
: $layer + 1000; |
240 |
root |
1.1 |
|
241 |
root |
1.5 |
while ($o) { |
242 |
|
|
my $face = $a->{face} || $o->{face}; |
243 |
root |
1.1 |
|
244 |
root |
1.5 |
my $pb = tile $face |
245 |
|
|
or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
246 |
root |
1.1 |
|
247 |
|
|
my $mx = $x + $o->{x}; |
248 |
|
|
my $my = $y + $o->{y}; |
249 |
|
|
|
250 |
root |
1.2 |
last if 0 > $mx || $mx >= $mapx |
251 |
|
|
|| 0 > $my || $my >= $mapy; |
252 |
root |
1.1 |
|
253 |
|
|
# this is very ugly (some tiles are 32x33 or worse) |
254 |
|
|
my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
255 |
|
|
|
256 |
root |
1.10 |
my $dx = $bigface ? $o->{x} : 0; |
257 |
|
|
my $dy = $bigface ? $o->{y} : 0; |
258 |
|
|
|
259 |
|
|
push @{ $map_info{$level}{$mx, $my} }, $a; |
260 |
|
|
|
261 |
|
|
$draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
262 |
|
|
|
263 |
root |
1.5 |
if (my $sface = $smooth->{$face}) { |
264 |
|
|
$bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
265 |
root |
1.1 |
|
266 |
|
|
# full tile |
267 |
root |
1.3 |
$draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
268 |
root |
1.1 |
|
269 |
|
|
# borders |
270 |
root |
1.3 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; |
271 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; |
272 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; |
273 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; |
274 |
root |
1.1 |
|
275 |
|
|
# corners |
276 |
root |
1.3 |
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; |
277 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
278 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
279 |
|
|
$draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
280 |
root |
1.1 |
} |
281 |
|
|
|
282 |
|
|
$o = $o->{more}; |
283 |
|
|
} |
284 |
|
|
} |
285 |
|
|
} |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
289 |
|
|
or die; |
290 |
root |
1.7 |
$map_pb->fill (0xffffff00); |
291 |
root |
1.1 |
|
292 |
root |
1.10 |
# second pass, render the map |
293 |
root |
1.1 |
for my $level (sort { $a <=> $b } keys %draw_info) { |
294 |
|
|
my $v = $draw_info{$level}; |
295 |
|
|
while (my ($sface, $info) = each %$v) { |
296 |
|
|
my $pb = tile $sface |
297 |
|
|
or die "no smooth face $sface\n"; |
298 |
|
|
|
299 |
|
|
while (my ($xy, $bits) = each %$info) { |
300 |
|
|
my ($x, $y) = split $;, $xy; |
301 |
|
|
|
302 |
|
|
next if $x < 0 || $x >= $mapx |
303 |
|
|
|| $y < 0 || $y >= $mapy; |
304 |
|
|
|
305 |
root |
1.13 |
# bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb |
306 |
|
|
# f full tile draw with x|y bigface displacement |
307 |
|
|
# n do not draw borders&corners |
308 |
|
|
# c draw these corners, but... |
309 |
|
|
# C ... not these |
310 |
root |
1.1 |
# b draw these borders |
311 |
|
|
|
312 |
|
|
if ($bits & 0x2000) { |
313 |
|
|
my $dx = (($bits >> 24) & 0xff) - 128; |
314 |
|
|
my $dy = (($bits >> 16) & 0xff) - 128; |
315 |
|
|
|
316 |
|
|
$pb->composite ($map_pb, |
317 |
|
|
$x * T, $y * T, |
318 |
|
|
T, T, |
319 |
|
|
($x - $dx) * T, ($y - $dy) * T, 1, 1, |
320 |
|
|
"nearest", |
321 |
|
|
255 |
322 |
|
|
); |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
unless ($bits & 0x1000) { |
326 |
|
|
my $border = $bits & 0xf; |
327 |
|
|
my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; |
328 |
|
|
|
329 |
|
|
$pb->composite ($map_pb, |
330 |
|
|
$x * T, $y * T, |
331 |
|
|
T, T, |
332 |
|
|
($x - $border) * T, $y * T, 1, 1, |
333 |
|
|
"nearest", |
334 |
|
|
255 |
335 |
|
|
) if $border; |
336 |
|
|
|
337 |
|
|
$pb->composite ($map_pb, |
338 |
|
|
$x * T, $y * T, |
339 |
|
|
T, T, |
340 |
|
|
($x - $corner) * T, ($y - 1) * T, 1, 1, |
341 |
|
|
"nearest", |
342 |
|
|
255 |
343 |
|
|
) if $corner; |
344 |
|
|
} |
345 |
|
|
} |
346 |
|
|
} |
347 |
|
|
} |
348 |
|
|
|
349 |
root |
1.10 |
# third pass, gather meta info |
350 |
|
|
for my $level (sort { $a <=> $b } keys %map_info) { |
351 |
|
|
my $info = $map_info{$level}; |
352 |
|
|
|
353 |
|
|
while (my ($xy, $as) = each %$info) { |
354 |
|
|
my ($x, $y) = split $;, $xy; |
355 |
|
|
|
356 |
|
|
next if $x < 0 || $x >= $mapx |
357 |
|
|
|| $y < 0 || $y >= $mapy; |
358 |
|
|
|
359 |
|
|
push @{ $meta{map}[$x][$y] }, $_ for @$as; |
360 |
|
|
} |
361 |
|
|
} |
362 |
|
|
|
363 |
root |
1.1 |
($map_pb, \%meta) |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
for my $file (@ARGV) { |
367 |
|
|
my $mapa = read_arch $file; |
368 |
|
|
my ($pb, $meta) = cfmap_render $mapa, $file; |
369 |
root |
1.3 |
$pb->save ("$file.png~", "png"); |
370 |
|
|
system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; |
371 |
root |
1.6 |
#system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency |
372 |
root |
1.14 |
# system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; |
373 |
|
|
system "pngnq <\Q$file.png~\E >\Q$file.png\E"; |
374 |
root |
1.3 |
unlink "$file.png~"; |
375 |
root |
1.1 |
Storable::nstore $meta, "$file.pst"; |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|