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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines