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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.8 by root, Sun Nov 20 00:58:57 2005 UTC vs.
Revision 1.16 by root, Mon Dec 12 01:37:11 2005 UTC

17# along with gvpe; if not, write to the Free Software 17# along with gvpe; if not, write to the Free Software
18# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 19
20# tower of stars: missing craters? 20# tower of stars: missing craters?
21 21
22our $VERSION = '1.2';
23
22use strict; 24use strict;
23 25
24use Storable; 26use Storable;
25use List::Util qw(max); 27use List::Util qw(max);
26 28
27use Gtk2; 29use Gtk2;
28
29our $VERSION = '1.0';
30 30
31#init Gtk2::Gdk; 31#init Gtk2::Gdk;
32 32
33my $LIB = $ENV{CROSSFIRE_LIBDIR} 33my $LIB = $ENV{CROSSFIRE_LIBDIR}
34 or die "\$CROSSFIRE_LIBDIR must be set\n"; 34 or die "\$CROSSFIRE_LIBDIR must be set\n";
74 74
75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; 75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
76 } 76 }
77 77
78 Storable::nstore \%smooth, "$path.pst"; 78 Storable::nstore \%smooth, "$path.pst";
79 utime +(stat $path)[8,9], "$path.pst";
79 80
80 \%smooth 81 \%smooth
81 } 82 }
82} 83}
83 84
151 } 152 }
152 } 153 }
153 154
154 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.
155 156
157 if ($cache) {
156 Storable::nstore \%arc, "$path.pst" 158 Storable::nstore \%arc, "$path.pst";
157 if $cache; 159 utime +(stat $path)[8,9], "$path.pst";
160 }
158 161
159 \%arc 162 \%arc
160 } 163 }
161} 164}
162 165
190 193
191 my %meta; 194 my %meta;
192 195
193 my ($mapx, $mapy); 196 my ($mapx, $mapy);
194 197
195 my $map = $meta{map} = []; 198 my $map;
196 199
197 for (@{ $mapa->{arch} }) { 200 for (@{ $mapa->{arch} }) {
201 my ($x, $y) = ($_->{x}, $_->{y});
202
198 if ($_->{_name} eq "map") { 203 if ($_->{_name} eq "map") {
199 $meta{info} = $_; 204 $meta{info} = $_;
200 205
201 $mapx = $_->{width} || $_->{x}; 206 $mapx = $_->{width} || $x;
202 $mapy = $_->{height} || $_->{y}; 207 $mapy = $_->{height} || $y;
203 } else { 208 } else {
204 push @{ $map->[$_->{x}][$_->{y}] }, $_; 209 push @{ $map->[$x][$y] }, $_;
205 210
206 # arch map is unreliable w.r.t. width and height 211 # arch map is unreliable w.r.t. width and height
207 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 212 $mapx = $x + 1 if $mapx <= $x;
208 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 213 $mapy = $y + 1 if $mapy <= $y;
209 #$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};
210 #$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};
211 } 216 }
212 } 217 }
213 218
214 $meta{width} = $mapx; 219 $meta{width} = $mapx;
215 $meta{height} = $mapy; 220 $meta{height} = $mapy;
216 221
217 my %draw_info; 222 my %draw_info;
223 my %map_info;
218 224
219 # first pass, gather face stacking order, border and corner info 225 # first pass, gather face stacking order, border and corner info
220 for my $x (0 .. $mapx - 1) { 226 for my $x (0 .. $mapx - 1) {
221 my $col = $map->[$x]; 227 my $col = $map->[$x];
222 for my $y (0 .. $mapy - 1) { 228 for my $y (0 .. $mapy - 1) {
223 my $as = $col->[$y] || []; 229 my $as = $col->[$y] || [];
224 230
231 my $minsmooth = 0;
232
225 for my $layer (0 .. $#$as) { 233 for my $layer (0 .. $#$as) {
226 my $a = $as->[$layer]; 234 my $a = $as->[$layer];
227 235
228 my $o = $arch->{$a->{_name}} 236 my $o = $arch->{$a->{_name}}
229 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;
230 238
231 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; 239 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
240
241 # hack to ensure somewhat correct ordering in case of conflicting
242 # smoothlevel/stacking order
243 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
244 $minsmooth = $smoothlevel;
245
232 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; 246 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
233 my $level = $smoothlevel ? $smoothlevel 247 my $level = $smoothlevel + $layer * 256;
234 : $is_floor ? 0 248
235 : 256 + $layer; 249 $level -= 100 * 256 if $o->{_name} eq "blocked";
236 250
237 while ($o) { 251 while ($o) {
238 my $face = $a->{face} || $o->{face}; 252 my $face = $a->{face} || $o->{face};
239 253
240 my $pb = tile $face 254 my $pb = tile $face
246 last if 0 > $mx || $mx >= $mapx 260 last if 0 > $mx || $mx >= $mapx
247 || 0 > $my || $my >= $mapy; 261 || 0 > $my || $my >= $mapy;
248 262
249 # this is very ugly (some tiles are 32x33 or worse) 263 # this is very ugly (some tiles are 32x33 or worse)
250 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;
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);
251 272
252 if (my $sface = $smooth->{$face}) { 273 if (my $sface = $smooth->{$face}) {
253 $bigface and die "can't handle bigfaces with smoothing ($face)\n"; 274 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
254 275
255 # full tile 276 # full tile
266 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 287 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
267 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 288 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
268 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 289 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
269 } 290 }
270 291
271 my $dx = $bigface ? $o->{x} : 0;
272 my $dy = $bigface ? $o->{y} : 0;
273
274 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
275
276 $o = $o->{more}; 292 $o = $o->{more};
293 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
277 } 294 }
278 } 295 }
279 } 296 }
280 } 297 }
281 298
282 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
283 or die; 300 or die;
284 $map_pb->fill (0xffffff00); 301 $map_pb->fill (0xffffff00);
285 302
286 # second pass, render all the stuff 303 # second pass, render the map
287 for my $level (sort { $a <=> $b } keys %draw_info) { 304 for my $level (sort { $a <=> $b } keys %draw_info) {
288 my $v = $draw_info{$level}; 305 my $v = $draw_info{$level};
289 while (my ($sface, $info) = each %$v) { 306 while (my ($sface, $info) = each %$v) {
290 my $pb = tile $sface 307 my $pb = tile $sface
291 or die "no smooth face $sface\n"; 308 or die "no smooth face $sface\n";
294 my ($x, $y) = split $;, $xy; 311 my ($x, $y) = split $;, $xy;
295 312
296 next if $x < 0 || $x >= $mapx 313 next if $x < 0 || $x >= $mapx
297 || $y < 0 || $y >= $mapy; 314 || $y < 0 || $y >= $mapy;
298 315
299 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 316 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
300 # X don't draw
301 # F full tile draw with x|y bigface displacement 317 # f full tile draw with x|y bigface displacement
302 # c maybe draw these corners
303 # C do not draw these corners 318 # n do not draw borders&corners
319 # c draw these corners, but...
320 # C ... not these
304 # b draw these borders 321 # b draw these borders
305 322
306 if ($bits & 0x2000) { 323 if ($bits & 0x2000) {
307 my $dx = (($bits >> 24) & 0xff) - 128; 324 my $dx = (($bits >> 24) & 0xff) - 128;
308 my $dy = (($bits >> 16) & 0xff) - 128; 325 my $dy = (($bits >> 16) & 0xff) - 128;
338 } 355 }
339 } 356 }
340 } 357 }
341 } 358 }
342 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
343 ($map_pb, \%meta) 374 ($map_pb, \%meta)
344} 375}
345 376
346for my $file (@ARGV) { 377for my $file (@ARGV) {
347 my $mapa = read_arch $file; 378 my $mapa = read_arch $file;
348 my ($pb, $meta) = cfmap_render $mapa, $file; 379 my ($pb, $meta) = cfmap_render $mapa, $file;
349 $pb->save ("$file.png~", "png"); 380 $pb->save ("$file.png~~", "png");
350 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";
351 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 382 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
352 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-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";
353 unlink "$file.png~"; 385 unlink "$file.png~~";
354 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";
355} 390}
356 391
357 392
358 393
359 394

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines