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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.3 by root, Fri Nov 18 06:57:15 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
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{inventory} }, $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;
223 my %map_info;
195 224
196 # first pass, gather face stacking order, border and corner info 225 # first pass, gather face stacking order, border and corner info
197 for my $x (0 .. $mapx - 1) { 226 for my $x (0 .. $mapx - 1) {
198 my $col = $map->[$x]; 227 my $col = $map->[$x];
199 for my $y (0 .. $mapy - 1) { 228 for my $y (0 .. $mapy - 1) {
200 my $as = $col->[$y] || []; 229 my $as = $col->[$y] || [];
201 230
231 my $minsmooth = 0;
232
202 for my $layer (0 .. $#$as) { 233 for my $layer (0 .. $#$as) {
203 my $a = $as->[$layer]; 234 my $a = $as->[$layer];
204 235
205 my $o = $arch->{$a->{_name}} 236 my $o = $arch->{$a->{_name}}
206 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;
207 238
208 my $A = { %$o, %$a }; 239 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
209 240
210 my $smoothlevel = $A->{smoothlevel}; 241 # hack to ensure somewhat correct ordering in case of conflicting
211 my $level = $smoothlevel ? $smoothlevel 242 # smoothlevel/stacking order
212 : $A->{is_floor} ? 0 243 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
213 : 256 + $layer; 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";
214 250
215 while ($o) { 251 while ($o) {
252 my $face = $a->{face} || $o->{face};
253
216 my $pb = tile $A->{face} 254 my $pb = tile $face
217 or (warn "$mapname: face '$A->{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;
218 256
219 my $mx = $x + $o->{x}; 257 my $mx = $x + $o->{x};
220 my $my = $y + $o->{y}; 258 my $my = $y + $o->{y};
221 259
222 last if 0 > $mx || $mx >= $mapx 260 last if 0 > $mx || $mx >= $mapx
223 || 0 > $my || $my >= $mapy; 261 || 0 > $my || $my >= $mapy;
224 262
225 # this is very ugly (some tiles are 32x33 or worse) 263 # this is very ugly (some tiles are 32x33 or worse)
226 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;
227 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
228 if (my $sface = $smooth->{$A->{face}}) { 273 if (my $sface = $smooth->{$face}) {
229 $bigface and die "can't handle bigfaces with smoothing ($A->{face})\n"; 274 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
230 275
231 # full tile 276 # full tile
232 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 277 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
233 278
234 # borders 279 # borders
242 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 287 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
243 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 288 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
244 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 289 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
245 } 290 }
246 291
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}; 292 $o = $o->{more};
293 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
253 } 294 }
254 } 295 }
255 } 296 }
256 } 297 }
257 298
258 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
259 or die; 300 or die;
260 $map_pb->fill (0x00000000); 301 $map_pb->fill (0xffffff00);
261 302
262 # second pass, render all the stuff 303 # second pass, render the map
263 for my $level (sort { $a <=> $b } keys %draw_info) { 304 for my $level (sort { $a <=> $b } keys %draw_info) {
264 my $v = $draw_info{$level}; 305 my $v = $draw_info{$level};
265 while (my ($sface, $info) = each %$v) { 306 while (my ($sface, $info) = each %$v) {
266 my $pb = tile $sface 307 my $pb = tile $sface
267 or die "no smooth face $sface\n"; 308 or die "no smooth face $sface\n";
270 my ($x, $y) = split $;, $xy; 311 my ($x, $y) = split $;, $xy;
271 312
272 next if $x < 0 || $x >= $mapx 313 next if $x < 0 || $x >= $mapx
273 || $y < 0 || $y >= $mapy; 314 || $y < 0 || $y >= $mapy;
274 315
275 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 316 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
276 # X don't draw
277 # F full tile draw with x|y bigface displacement 317 # f full tile draw with x|y bigface displacement
278 # c maybe draw these corners
279 # C do not draw these corners 318 # n do not draw borders&corners
319 # c draw these corners, but...
320 # C ... not these
280 # b draw these borders 321 # b draw these borders
281 322
282 if ($bits & 0x2000) { 323 if ($bits & 0x2000) {
283 my $dx = (($bits >> 24) & 0xff) - 128; 324 my $dx = (($bits >> 24) & 0xff) - 128;
284 my $dy = (($bits >> 16) & 0xff) - 128; 325 my $dy = (($bits >> 16) & 0xff) - 128;
314 } 355 }
315 } 356 }
316 } 357 }
317 } 358 }
318 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
319 ($map_pb, \%meta) 374 ($map_pb, \%meta)
320} 375}
321 376
322for my $file (@ARGV) { 377for my $file (@ARGV) {
323 my $mapa = read_arch $file; 378 my $mapa = read_arch $file;
324 my ($pb, $meta) = cfmap_render $mapa, $file; 379 my ($pb, $meta) = cfmap_render $mapa, $file;
325 $pb->save ("$file.png~", "png"); 380 $pb->save ("$file.png~~", "png");
326 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; 381 system "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
327 system "mogrify", "-colors" => 65536, "$file.png~"; 382 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
328 system "pngcrush", "-q", "-m" => 7, "-rem" => "alla", "-reduce", "$file.png~", "$file.png"; 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";
329 unlink "$file.png~"; 385 unlink "$file.png~~";
330 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";
331} 390}
332 391
333 392
334 393
335 394

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines