ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
(Generate patch)

Comparing cfmaps/cfmap2png (file contents):
Revision 1.1 by root, Thu Nov 17 11:51:08 2005 UTC vs.
Revision 1.16 by root, Mon Dec 12 01:37:11 2005 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# bugs: http://cfmaps.schmorp.de/brest/apartments/brest_town_house.html <- walls 3# cfarch2png - convert crossfire maps to png+metadata
4# whaling... icecaves... water is red? 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
5# tower of stars: missing craters? 20# tower of stars: missing craters?
6# http://cfmaps.schmorp.de/pup_land/raffle/raffle3_u2.html <-> yellow "doors" show as grey stone as surrounding 21
22our $VERSION = '1.2';
7 23
8use strict; 24use strict;
9 25
10use Storable; 26use Storable;
11use List::Util qw(max); 27use List::Util qw(max);
58 74
59 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; 75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
60 } 76 }
61 77
62 Storable::nstore \%smooth, "$path.pst"; 78 Storable::nstore \%smooth, "$path.pst";
79 utime +(stat $path)[8,9], "$path.pst";
63 80
64 \%smooth 81 \%smooth
65 } 82 }
66} 83}
67 84
85 while (<$fh>) { 102 while (<$fh>) {
86 s/\s+$//; 103 s/\s+$//;
87 if (/^end$/i) { 104 if (/^end$/i) {
88 last; 105 last;
89 } elsif (/^arch (\S+)$/) { 106 } elsif (/^arch (\S+)$/) {
90 push @{ $arc{subarch} }, $parse_block->(_name => $1); 107 push @{ $arc{inventory} }, $parse_block->(_name => $1);
108 } elsif (/^lore$/) {
109 while (<$fh>) {
110 last if /^endlore\s*$/i;
111 $arc{lore} .= $_;
112 }
91 } elsif (/^msg$/) { 113 } elsif (/^msg$/) {
92 while (<$fh>) { 114 while (<$fh>) {
93 last if /^endmsg\s*$/i; 115 last if /^endmsg\s*$/i;
94 $arc{msg} .= $_; 116 $arc{msg} .= $_;
95 } 117 }
111 $more = $prev; 133 $more = $prev;
112 } elsif (/^object (\S+)$/i) { 134 } elsif (/^object (\S+)$/i) {
113 my $name = $1; 135 my $name = $1;
114 my $arc = $parse_block->(_name => $name); 136 my $arc = $parse_block->(_name => $name);
115 137
138 if ($more) {
139 $more->{more} = $arc;
140 } else {
116 $arc{$name} = $arc; 141 $arc{$name} = $arc;
117 $more->{more} = $arc if $more; 142 }
118 143
119 $prev = $arc; 144 $prev = $arc;
120 $more = undef; 145 $more = undef;
121 } elsif (/^arch (\S+)$/i) { 146 } elsif (/^arch (\S+)$/i) {
122 push @{ $arc{arch} }, $parse_block->(_name => $1); 147 push @{ $arc{arch} }, $parse_block->(_name => $1);
127 } 152 }
128 } 153 }
129 154
130 undef $parse_block; # work around bug in perl not freeing $fh etc. 155 undef $parse_block; # work around bug in perl not freeing $fh etc.
131 156
157 if ($cache) {
132 Storable::nstore \%arc, "$path.pst" 158 Storable::nstore \%arc, "$path.pst";
133 if $cache; 159 utime +(stat $path)[8,9], "$path.pst";
160 }
134 161
135 \%arc 162 \%arc
136 } 163 }
137} 164}
138 165
166 193
167 my %meta; 194 my %meta;
168 195
169 my ($mapx, $mapy); 196 my ($mapx, $mapy);
170 197
171 my $map = $meta{map} = []; 198 my $map;
172 199
173 for (@{ $mapa->{arch} }) { 200 for (@{ $mapa->{arch} }) {
201 my ($x, $y) = ($_->{x}, $_->{y});
202
174 if ($_->{_name} eq "map") { 203 if ($_->{_name} eq "map") {
175 $meta{info} = $_; 204 $meta{info} = $_;
176 205
177 $mapx = $_->{width} || $_->{x}; 206 $mapx = $_->{width} || $x;
178 $mapy = $_->{height} || $_->{y}; 207 $mapy = $_->{height} || $y;
179 } else { 208 } else {
180 push @{ $map->[$_->{x}][$_->{y}] }, $_; 209 push @{ $map->[$x][$y] }, $_;
181 210
182 # arch map is unreliable w.r.t. width and height 211 # arch map is unreliable w.r.t. width and height
183 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 212 $mapx = $x + 1 if $mapx <= $x;
184 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 213 $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}; 214 #$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}; 215 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
187 } 216 }
188 } 217 }
189 218
190 $meta{width} = $mapx; 219 $meta{width} = $mapx;
191 $meta{height} = $mapy; 220 $meta{height} = $mapy;
192 221
193 my %map_face;
194 my %draw_info; 222 my %draw_info;
195 223 my %map_info;
196 my $OBJ_LEVEL = 2**32; # higher than any valid (or in-use) smooth_level
197 224
198 # first pass, gather face stacking order, border and corner info 225 # first pass, gather face stacking order, border and corner info
199 for my $x (0 .. $mapx - 1) { 226 for my $x (0 .. $mapx - 1) {
200 my $col = $map->[$x]; 227 my $col = $map->[$x];
201 for my $y (0 .. $mapy - 1) { 228 for my $y (0 .. $mapy - 1) {
202 my $as = $col->[$y] || []; 229 my $as = $col->[$y] || [];
203 230
231 my $minsmooth = 0;
232
204 for my $layer (0 .. $#$as) { 233 for my $layer (0 .. $#$as) {
205 my $a = $as->[$layer]; 234 my $a = $as->[$layer];
206 235
207 my $o = $arch->{$a->{_name}} 236 my $o = $arch->{$a->{_name}}
208 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 237 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
209 238
210 $o = $arch->{$o->{other_arch}} while $o->{other_arch} && !$o->{face}; 239 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
211 240
212 $o or die "arch $a->{_name} undefined at ($x|$y)\n"; 241 # hack to ensure somewhat correct ordering in case of conflicting
213 242 # smoothlevel/stacking order
243 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
214 my $smooth_level = $o->{smoothlevel}; 244 $minsmooth = $smoothlevel;
215 my $level = $smooth_level ? $smooth_level 245
216 : $o->{is_floor} ? 0 246 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
217 : $OBJ_LEVEL + $layer; 247 my $level = $smoothlevel + $layer * 256;
248
249 $level -= 100 * 256 if $o->{_name} eq "blocked";
218 250
219 while ($o) { 251 while ($o) {
252 my $face = $a->{face} || $o->{face};
253
220 my $pb = tile $o->{face} 254 my $pb = tile $face
221 or (warn "$mapname: face '$o->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; 255 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
222 256
223 my $mx = $x + $o->{x}; 257 my $mx = $x + $o->{x};
224 my $my = $y + $o->{y}; 258 my $my = $y + $o->{y};
225 259
226 last if $mx >= $mapx 260 last if 0 > $mx || $mx >= $mapx
227 || $my >= $mapy; 261 || 0 > $my || $my >= $mapy;
228 262
229 # this is very ugly (some tiles are 32x33 or worse) 263 # this is very ugly (some tiles are 32x33 or worse)
230 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; 264 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
231 265
232 if (my $sface = $smooth->{$o->{face}}) {
233 $bigface and die "can't handle big faces with smoothing ($o->{face})\n";
234
235 # full tile
236 $draw_info{$smooth_level}{$sface}{$mx , $my } |= 0x1000;
237
238 # borders
239 $draw_info{$smooth_level}{$sface}{$mx + 1, $my } |= 0x0031;
240 $draw_info{$smooth_level}{$sface}{$mx , $my + 1} |= 0x0092;
241 $draw_info{$smooth_level}{$sface}{$mx - 1, $my } |= 0x0064;
242 $draw_info{$smooth_level}{$sface}{$mx , $my - 1} |= 0x00c8;
243
244 # corners
245 $draw_info{$smooth_level}{$sface}{$mx + 1, $my + 1} |= 0x0100;
246 $draw_info{$smooth_level}{$sface}{$mx - 1, $my + 1} |= 0x0200;
247 $draw_info{$smooth_level}{$sface}{$mx - 1, $my - 1} |= 0x0400;
248 $draw_info{$smooth_level}{$sface}{$mx + 1, $my - 1} |= 0x0800;
249 } else {
250 $smooth_level = 0;
251 }
252
253 my $dx = $bigface ? $o->{x} : 0; 266 my $dx = $bigface ? $o->{x} : 0;
254 my $dy = $bigface ? $o->{y} : 0; 267 my $dy = $bigface ? $o->{y} : 0;
255 268
269 push @{ $map_info{$level}{$mx, $my} }, $a;
270
256 $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 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 }
257 291
258 $o = $o->{more}; 292 $o = $o->{more};
293 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
259 } 294 }
260 } 295 }
261 } 296 }
262 } 297 }
263 298
264 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 299 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
265 or die; 300 or die;
266 $map_pb->fill (0x00000000); 301 $map_pb->fill (0xffffff00);
267 302
268 # second pass, render all the stuff 303 # second pass, render the map
269 for my $level (sort { $a <=> $b } keys %draw_info) { 304 for my $level (sort { $a <=> $b } keys %draw_info) {
270 my $v = $draw_info{$level}; 305 my $v = $draw_info{$level};
271 while (my ($sface, $info) = each %$v) { 306 while (my ($sface, $info) = each %$v) {
272 my $pb = tile $sface 307 my $pb = tile $sface
273 or die "no smooth face $sface\n"; 308 or die "no smooth face $sface\n";
276 my ($x, $y) = split $;, $xy; 311 my ($x, $y) = split $;, $xy;
277 312
278 next if $x < 0 || $x >= $mapx 313 next if $x < 0 || $x >= $mapx
279 || $y < 0 || $y >= $mapy; 314 || $y < 0 || $y >= $mapy;
280 315
281 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 316 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
282 # X don't draw
283 # F full tile draw with x|y bigface displacement 317 # f full tile draw with x|y bigface displacement
284 # c maybe draw these corners
285 # C do not draw these corners 318 # n do not draw borders&corners
319 # c draw these corners, but...
320 # C ... not these
286 # b draw these borders 321 # b draw these borders
287 322
288 if ($bits & 0x2000) { 323 if ($bits & 0x2000) {
289 my $dx = (($bits >> 24) & 0xff) - 128; 324 my $dx = (($bits >> 24) & 0xff) - 128;
290 my $dy = (($bits >> 16) & 0xff) - 128; 325 my $dy = (($bits >> 16) & 0xff) - 128;
320 } 355 }
321 } 356 }
322 } 357 }
323 } 358 }
324 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
325 ($map_pb, \%meta) 374 ($map_pb, \%meta)
326} 375}
327 376
328for my $file (@ARGV) { 377for my $file (@ARGV) {
329 my $mapa = read_arch $file; 378 my $mapa = read_arch $file;
330 my ($pb, $meta) = cfmap_render $mapa, $file; 379 my ($pb, $meta) = cfmap_render $mapa, $file;
331 $pb->save ("$file.png", "png"); 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~~";
332 Storable::nstore $meta, "$file.pst"; 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";
333} 390}
334 391
335 392
336 393
337 394

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines