ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
Revision: 1.3
Committed: Fri Nov 18 06:57:15 2005 UTC (18 years, 7 months ago) by root
Branch: MAIN
Changes since 1.2: +25 -33 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/bin/perl
2
3 # bugs: http://cfmaps.schmorp.de/brest/apartments/brest_town_house.html <- walls
4 # whaling... icecaves... water is red?
5 # tower of stars: missing craters?
6 # http://cfmaps.schmorp.de/pup_land/raffle/raffle3_u2.html <-> yellow "doors" show as grey stone as surrounding
7
8 use strict;
9
10 use Storable;
11 use List::Util qw(max);
12
13 use Gtk2;
14
15 #init Gtk2::Gdk;
16
17 my $LIB = $ENV{CROSSFIRE_LIBDIR}
18 or die "\$CROSSFIRE_LIBDIR must be set\n";
19
20 sub T (){ 32 }
21
22 sub read_pak($) {
23 my ($path) = @_;
24
25 eval {
26 -M "$path.pst" < -M $path
27 && Storable::retrieve "$path.pst"
28 } or do {
29 my %pak;
30
31 open my $fh, "<:raw", $path
32 or die "$_[0]: $!";
33 while (<$fh>) {
34 my ($type, $id, $len, $path) = split;
35 $path =~ s/.*\///;
36 read $fh, $pak{$path}, $len;
37 }
38
39 Storable::nstore \%pak, "$path.pst";
40
41 \%pak
42 }
43 }
44
45 sub read_smooth($) {
46 my ($path) = @_;
47
48 eval {
49 -M "$path.pst" < -M $path
50 && Storable::retrieve "$path.pst"
51 } or do {
52 my %smooth;
53
54 open my $fh, "<:raw", $path
55 or die "$path: $!";
56 while (<$fh>) {
57 next if /^\s*($|#)/;
58
59 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
60 }
61
62 Storable::nstore \%smooth, "$path.pst";
63
64 \%smooth
65 }
66 }
67
68 sub read_arch($;$) {
69 my ($path, $cache) = @_;
70
71 eval {
72 $cache
73 && -M "$path.pst" < -M $path
74 && Storable::retrieve "$path.pst"
75 } or do {
76 my %arc;
77 my ($more, $prev);
78
79 open my $fh, "<:raw", $path
80 or die "$path: $!";
81
82 my $parse_block; $parse_block = sub {
83 my %arc = @_;
84
85 while (<$fh>) {
86 s/\s+$//;
87 if (/^end$/i) {
88 last;
89 } elsif (/^arch (\S+)$/) {
90 push @{ $arc{inventory} }, $parse_block->(_name => $1);
91 } elsif (/^msg$/) {
92 while (<$fh>) {
93 last if /^endmsg\s*$/i;
94 $arc{msg} .= $_;
95 }
96 } elsif (/^(\S+)\s*(.*)$/) {
97 $arc{lc $1} = $2;
98 } elsif (/^\s*($|#)/) {
99 #
100 } else {
101 warn "$path: unparsable line '$_' in arch $arc{_name}";
102 }
103 }
104
105 \%arc
106 };
107
108 while (<$fh>) {
109 s/\s+$//;
110 if (/^more$/i) {
111 $more = $prev;
112 } elsif (/^object (\S+)$/i) {
113 my $name = $1;
114 my $arc = $parse_block->(_name => $name);
115
116 $arc{$name} = $arc;
117 $more->{more} = $arc if $more;
118
119 $prev = $arc;
120 $more = undef;
121 } elsif (/^arch (\S+)$/i) {
122 push @{ $arc{arch} }, $parse_block->(_name => $1);
123 } elsif (/^\s*($|#)/) {
124 #
125 } else {
126 warn "$path: unparseable top-level line '$_'";
127 }
128 }
129
130 undef $parse_block; # work around bug in perl not freeing $fh etc.
131
132 Storable::nstore \%arc, "$path.pst"
133 if $cache;
134
135 \%arc
136 }
137 }
138
139 my $arch = read_arch "$LIB/archetypes", 1;
140 my $tile = read_pak "$LIB/crossfire.0";
141 my $smooth = read_smooth "$LIB/smooth";
142
143 sub tile($) {
144 my $name = $_[0];
145
146 my $pb = $tile->{$name}
147 or return;
148
149 unless (ref $pb) {
150 my $file = "/tmp/map2png.$$~";
151
152 open my $fh, ">:raw", $file
153 or die "$file: $!";
154 print $fh $pb;
155 close $fh;
156
157 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
158 unlink $file;
159 }
160
161 $pb
162 }
163
164 sub cfmap_render($;$) {
165 my ($mapa, $mapname) = @_;
166
167 my %meta;
168
169 my ($mapx, $mapy);
170
171 my $map = $meta{map} = [];
172
173 for (@{ $mapa->{arch} }) {
174 if ($_->{_name} eq "map") {
175 $meta{info} = $_;
176
177 $mapx = $_->{width} || $_->{x};
178 $mapy = $_->{height} || $_->{y};
179 } else {
180 push @{ $map->[$_->{x}][$_->{y}] }, $_;
181
182 # arch map is unreliable w.r.t. width and height
183 $mapx = $_->{x} + 1 if $mapx <= $_->{x};
184 $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};
186 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
187 }
188 }
189
190 $meta{width} = $mapx;
191 $meta{height} = $mapy;
192
193 my %map_face;
194 my %draw_info;
195
196 # first pass, gather face stacking order, border and corner info
197 for my $x (0 .. $mapx - 1) {
198 my $col = $map->[$x];
199 for my $y (0 .. $mapy - 1) {
200 my $as = $col->[$y] || [];
201
202 for my $layer (0 .. $#$as) {
203 my $a = $as->[$layer];
204
205 my $o = $arch->{$a->{_name}}
206 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
207
208 my $A = { %$o, %$a };
209
210 my $smoothlevel = $A->{smoothlevel};
211 my $level = $smoothlevel ? $smoothlevel
212 : $A->{is_floor} ? 0
213 : 256 + $layer;
214
215 while ($o) {
216 my $pb = tile $A->{face}
217 or (warn "$mapname: face '$A->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
218
219 my $mx = $x + $o->{x};
220 my $my = $y + $o->{y};
221
222 last if 0 > $mx || $mx >= $mapx
223 || 0 > $my || $my >= $mapy;
224
225 # this is very ugly (some tiles are 32x33 or worse)
226 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
227
228 if (my $sface = $smooth->{$A->{face}}) {
229 $bigface and die "can't handle bigfaces with smoothing ($A->{face})\n";
230
231 # full tile
232 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
233
234 # borders
235 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031;
236 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
237 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064;
238 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8;
239
240 # corners
241 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100;
242 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
243 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
244 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
245 }
246
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};
253 }
254 }
255 }
256 }
257
258 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
259 or die;
260 $map_pb->fill (0x00000000);
261
262 # second pass, render all the stuff
263 for my $level (sort { $a <=> $b } keys %draw_info) {
264 my $v = $draw_info{$level};
265 while (my ($sface, $info) = each %$v) {
266 my $pb = tile $sface
267 or die "no smooth face $sface\n";
268
269 while (my ($xy, $bits) = each %$info) {
270 my ($x, $y) = split $;, $xy;
271
272 next if $x < 0 || $x >= $mapx
273 || $y < 0 || $y >= $mapy;
274
275 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB
276 # X don't draw
277 # F full tile draw with x|y bigface displacement
278 # c maybe draw these corners
279 # C do not draw these corners
280 # b draw these borders
281
282 if ($bits & 0x2000) {
283 my $dx = (($bits >> 24) & 0xff) - 128;
284 my $dy = (($bits >> 16) & 0xff) - 128;
285
286 $pb->composite ($map_pb,
287 $x * T, $y * T,
288 T, T,
289 ($x - $dx) * T, ($y - $dy) * T, 1, 1,
290 "nearest",
291 255
292 );
293 }
294
295 unless ($bits & 0x1000) {
296 my $border = $bits & 0xf;
297 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf;
298
299 $pb->composite ($map_pb,
300 $x * T, $y * T,
301 T, T,
302 ($x - $border) * T, $y * T, 1, 1,
303 "nearest",
304 255
305 ) if $border;
306
307 $pb->composite ($map_pb,
308 $x * T, $y * T,
309 T, T,
310 ($x - $corner) * T, ($y - 1) * T, 1, 1,
311 "nearest",
312 255
313 ) if $corner;
314 }
315 }
316 }
317 }
318
319 ($map_pb, \%meta)
320 }
321
322 for my $file (@ARGV) {
323 my $mapa = read_arch $file;
324 my ($pb, $meta) = cfmap_render $mapa, $file;
325 $pb->save ("$file.png~", "png");
326 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
327 system "mogrify", "-colors" => 65536, "$file.png~";
328 system "pngcrush", "-q", "-m" => 7, "-rem" => "alla", "-reduce", "$file.png~", "$file.png";
329 unlink "$file.png~";
330 Storable::nstore $meta, "$file.pst";
331 }
332
333
334
335
336
337