ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
Revision: 1.16
Committed: Mon Dec 12 01:37:11 2005 UTC (18 years, 6 months ago) by root
Branch: MAIN
Changes since 1.15: +27 -13 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 # tower of stars: missing craters?
21
22 our $VERSION = '1.2';
23
24 use strict;
25
26 use Storable;
27 use List::Util qw(max);
28
29 use Gtk2;
30
31 #init Gtk2::Gdk;
32
33 my $LIB = $ENV{CROSSFIRE_LIBDIR}
34 or die "\$CROSSFIRE_LIBDIR must be set\n";
35
36 sub T (){ 32 }
37
38 sub read_pak($) {
39 my ($path) = @_;
40
41 eval {
42 -M "$path.pst" < -M $path
43 && Storable::retrieve "$path.pst"
44 } or do {
45 my %pak;
46
47 open my $fh, "<:raw", $path
48 or die "$_[0]: $!";
49 while (<$fh>) {
50 my ($type, $id, $len, $path) = split;
51 $path =~ s/.*\///;
52 read $fh, $pak{$path}, $len;
53 }
54
55 Storable::nstore \%pak, "$path.pst";
56
57 \%pak
58 }
59 }
60
61 sub read_smooth($) {
62 my ($path) = @_;
63
64 eval {
65 -M "$path.pst" < -M $path
66 && Storable::retrieve "$path.pst"
67 } or do {
68 my %smooth;
69
70 open my $fh, "<:raw", $path
71 or die "$path: $!";
72 while (<$fh>) {
73 next if /^\s*($|#)/;
74
75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
76 }
77
78 Storable::nstore \%smooth, "$path.pst";
79 utime +(stat $path)[8,9], "$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 push @{ $arc{inventory} }, $parse_block->(_name => $1);
108 } elsif (/^lore$/) {
109 while (<$fh>) {
110 last if /^endlore\s*$/i;
111 $arc{lore} .= $_;
112 }
113 } 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 if ($more) {
139 $more->{more} = $arc;
140 } else {
141 $arc{$name} = $arc;
142 }
143
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 if ($cache) {
158 Storable::nstore \%arc, "$path.pst";
159 utime +(stat $path)[8,9], "$path.pst";
160 }
161
162 \%arc
163 }
164 }
165
166 my $arch = read_arch "$LIB/archetypes", 1;
167 my $tile = read_pak "$LIB/crossfire.0";
168 my $smooth = read_smooth "$LIB/smooth";
169
170 sub tile($) {
171 my $name = $_[0];
172
173 my $pb = $tile->{$name}
174 or return;
175
176 unless (ref $pb) {
177 my $file = "/tmp/map2png.$$~";
178
179 open my $fh, ">:raw", $file
180 or die "$file: $!";
181 print $fh $pb;
182 close $fh;
183
184 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
185 unlink $file;
186 }
187
188 $pb
189 }
190
191 sub cfmap_render($;$) {
192 my ($mapa, $mapname) = @_;
193
194 my %meta;
195
196 my ($mapx, $mapy);
197
198 my $map;
199
200 for (@{ $mapa->{arch} }) {
201 my ($x, $y) = ($_->{x}, $_->{y});
202
203 if ($_->{_name} eq "map") {
204 $meta{info} = $_;
205
206 $mapx = $_->{width} || $x;
207 $mapy = $_->{height} || $y;
208 } else {
209 push @{ $map->[$x][$y] }, $_;
210
211 # arch map is unreliable w.r.t. width and height
212 $mapx = $x + 1 if $mapx <= $x;
213 $mapy = $y + 1 if $mapy <= $y;
214 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
215 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
216 }
217 }
218
219 $meta{width} = $mapx;
220 $meta{height} = $mapy;
221
222 my %draw_info;
223 my %map_info;
224
225 # first pass, gather face stacking order, border and corner info
226 for my $x (0 .. $mapx - 1) {
227 my $col = $map->[$x];
228 for my $y (0 .. $mapy - 1) {
229 my $as = $col->[$y] || [];
230
231 my $minsmooth = 0;
232
233 for my $layer (0 .. $#$as) {
234 my $a = $as->[$layer];
235
236 my $o = $arch->{$a->{_name}}
237 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
238
239 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
240
241 # hack to ensure somewhat correct ordering in case of conflicting
242 # smoothlevel/stacking order
243 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
244 $minsmooth = $smoothlevel;
245
246 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
247 my $level = $smoothlevel + $layer * 256;
248
249 $level -= 100 * 256 if $o->{_name} eq "blocked";
250
251 while ($o) {
252 my $face = $a->{face} || $o->{face};
253
254 my $pb = tile $face
255 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
256
257 my $mx = $x + $o->{x};
258 my $my = $y + $o->{y};
259
260 last if 0 > $mx || $mx >= $mapx
261 || 0 > $my || $my >= $mapy;
262
263 # this is very ugly (some tiles are 32x33 or worse)
264 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
265
266 my $dx = $bigface ? $o->{x} : 0;
267 my $dy = $bigface ? $o->{y} : 0;
268
269 push @{ $map_info{$level}{$mx, $my} }, $a;
270
271 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
272
273 if (my $sface = $smooth->{$face}) {
274 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
275
276 # full tile
277 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
278
279 # borders
280 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031;
281 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
282 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064;
283 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8;
284
285 # corners
286 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100;
287 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
288 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
289 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
290 }
291
292 $o = $o->{more};
293 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
294 }
295 }
296 }
297 }
298
299 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
300 or die;
301 $map_pb->fill (0xffffff00);
302
303 # second pass, render the map
304 for my $level (sort { $a <=> $b } keys %draw_info) {
305 my $v = $draw_info{$level};
306 while (my ($sface, $info) = each %$v) {
307 my $pb = tile $sface
308 or die "no smooth face $sface\n";
309
310 while (my ($xy, $bits) = each %$info) {
311 my ($x, $y) = split $;, $xy;
312
313 next if $x < 0 || $x >= $mapx
314 || $y < 0 || $y >= $mapy;
315
316 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
317 # f full tile draw with x|y bigface displacement
318 # n do not draw borders&corners
319 # c draw these corners, but...
320 # C ... not these
321 # b draw these borders
322
323 if ($bits & 0x2000) {
324 my $dx = (($bits >> 24) & 0xff) - 128;
325 my $dy = (($bits >> 16) & 0xff) - 128;
326
327 $pb->composite ($map_pb,
328 $x * T, $y * T,
329 T, T,
330 ($x - $dx) * T, ($y - $dy) * T, 1, 1,
331 "nearest",
332 255
333 );
334 }
335
336 unless ($bits & 0x1000) {
337 my $border = $bits & 0xf;
338 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf;
339
340 $pb->composite ($map_pb,
341 $x * T, $y * T,
342 T, T,
343 ($x - $border) * T, $y * T, 1, 1,
344 "nearest",
345 255
346 ) if $border;
347
348 $pb->composite ($map_pb,
349 $x * T, $y * T,
350 T, T,
351 ($x - $corner) * T, ($y - 1) * T, 1, 1,
352 "nearest",
353 255
354 ) if $corner;
355 }
356 }
357 }
358 }
359
360 # third pass, gather meta info
361 for my $level (sort { $a <=> $b } keys %map_info) {
362 my $info = $map_info{$level};
363
364 while (my ($xy, $as) = each %$info) {
365 my ($x, $y) = split $;, $xy;
366
367 next if $x < 0 || $x >= $mapx
368 || $y < 0 || $y >= $mapy;
369
370 push @{ $meta{map}[$x][$y] }, $_ for @$as;
371 }
372 }
373
374 ($map_pb, \%meta)
375 }
376
377 for my $file (@ARGV) {
378 my $mapa = read_arch $file;
379 my ($pb, $meta) = cfmap_render $mapa, $file;
380 $pb->save ("$file.png~~", "png");
381 system "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
382 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
383 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~";
384 # system "pngnq <\Q$file.png~\E >\Q$file.png\E";
385 unlink "$file.png~~";
386 Storable::nstore $meta, "$file.pst";
387 utime +(stat $file)[8,9], "$file.pst";
388 utime +(stat $file)[8,9], "$file.png~";
389 rename "$file.png~", "$file.png";
390 }
391
392
393
394
395
396