ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
Revision: 1.21
Committed: Wed Feb 14 02:33:47 2007 UTC (17 years, 3 months ago) by root
Branch: MAIN
Changes since 1.20: +3 -2 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,2007 Marc Lehmann <cfmaps@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 # 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.211';
34
35 use strict;
36
37 use Storable;
38 use List::Util qw(max);
39
40 use Gtk2;
41
42 #init Gtk2::Gdk;
43
44 my $LIB = $ENV{CROSSFIRE_LIBDIR}
45 or die "\$CROSSFIRE_LIBDIR must be set\n";
46
47 sub T (){ 32 }
48
49 sub read_pak($) {
50 my ($path) = @_;
51
52 eval {
53 -M "$path.pst" < -M $path
54 && Storable::retrieve "$path.pst"
55 } or do {
56 my %pak;
57
58 open my $fh, "<:raw:perlio", $path
59 or die "$_[0]: $!";
60 while (<$fh>) {
61 my ($type, $id, $len, $path) = split;
62 $path =~ s/.*\///;
63 read $fh, $pak{$path}, $len;
64 }
65
66 Storable::nstore \%pak, "$path.pst";
67
68 \%pak
69 }
70 }
71
72 sub read_smooth($) {
73 my ($path) = @_;
74
75 eval {
76 -M "$path.pst" < -M $path
77 && Storable::retrieve "$path.pst"
78 } or do {
79 my %smooth;
80
81 open my $fh, "<:raw:perlio", $path
82 or die "$path: $!";
83 while (<$fh>) {
84 next if /^\s*($|#)/;
85
86 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
87 }
88
89 Storable::nstore \%smooth, "$path.pst";
90 utime +(stat $path)[8,9], "$path.pst";
91
92 \%smooth
93 }
94 }
95
96 sub read_arch($;$) {
97 my ($path, $cache) = @_;
98
99 eval {
100 $cache
101 && -M "$path.pst" < -M $path
102 && Storable::retrieve "$path.pst"
103 } or do {
104 my %arc;
105 my ($more, $prev);
106
107 open my $fh, "<:utf8", $path
108 or die "$path: $!";
109
110 my $parse_block; $parse_block = sub {
111 my %arc = @_;
112
113 while (<$fh>) {
114 s/\s+$//;
115 if (/^end$/i) {
116 last;
117 } elsif (/^arch (\S+)$/) {
118 push @{ $arc{inventory} }, $parse_block->(_name => $1);
119 } elsif (/^lore$/) {
120 while (<$fh>) {
121 last if /^endlore\s*$/i;
122 $arc{lore} .= $_;
123 }
124 } elsif (/^msg$/) {
125 while (<$fh>) {
126 last if /^endmsg\s*$/i;
127 $arc{msg} .= $_;
128 }
129 } elsif (/^(\S+)\s*(.*)$/) {
130 $arc{lc $1} = $2;
131 } elsif (/^\s*($|#)/) {
132 #
133 } else {
134 warn "$path: unparsable line '$_' in arch $arc{_name}";
135 }
136 }
137
138 \%arc
139 };
140
141 while (<$fh>) {
142 s/\s+$//;
143 if (/^more$/i) {
144 $more = $prev;
145 } elsif (/^object (\S+)$/i) {
146 my $name = $1;
147 my $arc = $parse_block->(_name => $name);
148
149 if ($more) {
150 $more->{more} = $arc;
151 } else {
152 $arc{$name} = $arc;
153 }
154
155 $prev = $arc;
156 $more = undef;
157 } elsif (/^arch (\S+)$/i) {
158 push @{ $arc{arch} }, $parse_block->(_name => $1);
159 } elsif (/^\s*($|#)/) {
160 #
161 } else {
162 warn "$path: unparseable top-level line '$_'";
163 }
164 }
165
166 undef $parse_block; # work around bug in perl not freeing $fh etc.
167
168 if ($cache) {
169 Storable::nstore \%arc, "$path.pst";
170 utime +(stat $path)[8,9], "$path.pst";
171 }
172
173 \%arc
174 }
175 }
176
177 my $arch = read_arch "$LIB/archetypes", 1;
178 my $tile = read_pak "$LIB/crossfire.0";
179 my $smooth = read_smooth "$LIB/smooth";
180
181 sub tile($) {
182 my $name = $_[0];
183
184 my $pb = $tile->{$name}
185 or return;
186
187 unless (ref $pb) {
188 my $file = "/tmp/map2png.$$~";
189
190 open my $fh, ">:raw", $file
191 or die "$file: $!";
192 print $fh $pb;
193 close $fh;
194
195 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
196 unlink $file;
197 }
198
199 $pb
200 }
201
202 sub cfmap_render($;$) {
203 my ($mapa, $mapname) = @_;
204
205 my %meta;
206
207 my ($mapx, $mapy);
208
209 my $map;
210
211 for (@{ $mapa->{arch} }) {
212 my ($x, $y) = ($_->{x}, $_->{y});
213
214 if ($_->{_name} eq "map") {
215 $meta{info} = $_;
216
217 $mapx = $_->{width} || $x;
218 $mapy = $_->{height} || $y;
219 } else {
220 push @{ $map->[$x][$y] }, $_;
221
222 # arch map is unreliable w.r.t. width and height
223 $mapx = $x + 1 if $mapx <= $x;
224 $mapy = $y + 1 if $mapy <= $y;
225 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
226 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
227 }
228 }
229
230 $meta{width} = $mapx;
231 $meta{height} = $mapy;
232
233 my %draw_info;
234 my %map_info;
235
236 # first pass, gather face stacking order, border and corner info
237 for my $x (0 .. $mapx - 1) {
238 my $col = $map->[$x];
239 for my $y (0 .. $mapy - 1) {
240 my $as = $col->[$y] || [];
241
242 my $minsmooth = 0;
243
244 for my $layer (0 .. $#$as) {
245 my $a = $as->[$layer];
246
247 my $o = $arch->{$a->{_name}}
248 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
249
250 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
251
252 # hack to ensure somewhat correct ordering in case of conflicting
253 # smoothlevel/stacking order
254 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
255 $minsmooth = $smoothlevel;
256
257 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
258 my $level = $smoothlevel + $layer * 256;
259
260 $level -= 100 * 256 if $o->{_name} eq "blocked";
261
262 while ($o) {
263 my $face = $a->{face} || $o->{face};
264
265 my $pb = tile $face
266 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
267
268 my $mx = $x + $o->{x};
269 my $my = $y + $o->{y};
270
271 last if 0 > $mx || $mx >= $mapx
272 || 0 > $my || $my >= $mapy;
273
274 # this is very ugly (some tiles are 32x33 or worse)
275 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
276
277 my $dx = $bigface ? $o->{x} : 0;
278 my $dy = $bigface ? $o->{y} : 0;
279
280 push @{ $map_info{$level}{$mx, $my} }, $a;
281
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 } |= 0x0031;
292 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
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 }
302
303 $o = $o->{more};
304 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
305 }
306 }
307 }
308 }
309
310 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
311 or die;
312 $map_pb->fill (0xffffff00);
313
314 # second pass, render the map
315 for my $level (sort { $a <=> $b } keys %draw_info) {
316 my $v = $draw_info{$level};
317 while (my ($sface, $info) = each %$v) {
318 my $pb = tile $sface
319 or die "no smooth face $sface\n";
320
321 while (my ($xy, $bits) = each %$info) {
322 my ($x, $y) = split $;, $xy;
323
324 next if $x < 0 || $x >= $mapx
325 || $y < 0 || $y >= $mapy;
326
327 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
328 # f full tile draw with x|y bigface displacement
329 # n do not draw borders&corners
330 # c draw these corners, but...
331 # C ... not these
332 # b draw these borders
333
334 if ($bits & 0x2000) {
335 my $dx = (($bits >> 24) & 0xff) - 128;
336 my $dy = (($bits >> 16) & 0xff) - 128;
337
338 $pb->composite ($map_pb,
339 $x * T, $y * T,
340 T, T,
341 ($x - $dx) * T, ($y - $dy) * T, 1, 1,
342 "nearest",
343 255
344 );
345 }
346
347 unless ($bits & 0x1000) {
348 my $border = $bits & 0xf;
349 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf;
350
351 $pb->composite ($map_pb,
352 $x * T, $y * T,
353 T, T,
354 ($x - $border) * T, $y * T, 1, 1,
355 "nearest",
356 255
357 ) if $border;
358
359 $pb->composite ($map_pb,
360 $x * T, $y * T,
361 T, T,
362 ($x - $corner) * T, ($y - 1) * T, 1, 1,
363 "nearest",
364 255
365 ) if $corner;
366 }
367 }
368 }
369 }
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
385 ($map_pb, \%meta)
386 }
387
388 for my $file (@ARGV) {
389 my $mapa = read_arch $file;
390 $file =~ s/\.map$//;
391 my ($pb, $meta) = cfmap_render $mapa, $file;
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~~";
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";
403 }
404
405
406
407
408
409