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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.4 by root, Fri Nov 18 12:04:12 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# cfarch2png - convert crossfire maps to png+metadata
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
3# tower of stars: missing craters? 20# tower of stars: missing craters?
21
22our $VERSION = '1.2';
4 23
5use strict; 24use strict;
6 25
7use Storable; 26use Storable;
8use List::Util qw(max); 27use List::Util qw(max);
9 28
10use Gtk2; 29use Gtk2;
11
12our $VERSION = '1.0';
13 30
14#init Gtk2::Gdk; 31#init Gtk2::Gdk;
15 32
16my $LIB = $ENV{CROSSFIRE_LIBDIR} 33my $LIB = $ENV{CROSSFIRE_LIBDIR}
17 or die "\$CROSSFIRE_LIBDIR must be set\n"; 34 or die "\$CROSSFIRE_LIBDIR must be set\n";
57 74
58 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; 75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
59 } 76 }
60 77
61 Storable::nstore \%smooth, "$path.pst"; 78 Storable::nstore \%smooth, "$path.pst";
79 utime +(stat $path)[8,9], "$path.pst";
62 80
63 \%smooth 81 \%smooth
64 } 82 }
65} 83}
66 84
134 } 152 }
135 } 153 }
136 154
137 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.
138 156
157 if ($cache) {
139 Storable::nstore \%arc, "$path.pst" 158 Storable::nstore \%arc, "$path.pst";
140 if $cache; 159 utime +(stat $path)[8,9], "$path.pst";
160 }
141 161
142 \%arc 162 \%arc
143 } 163 }
144} 164}
145 165
173 193
174 my %meta; 194 my %meta;
175 195
176 my ($mapx, $mapy); 196 my ($mapx, $mapy);
177 197
178 my $map = $meta{map} = []; 198 my $map;
179 199
180 for (@{ $mapa->{arch} }) { 200 for (@{ $mapa->{arch} }) {
201 my ($x, $y) = ($_->{x}, $_->{y});
202
181 if ($_->{_name} eq "map") { 203 if ($_->{_name} eq "map") {
182 $meta{info} = $_; 204 $meta{info} = $_;
183 205
184 $mapx = $_->{width} || $_->{x}; 206 $mapx = $_->{width} || $x;
185 $mapy = $_->{height} || $_->{y}; 207 $mapy = $_->{height} || $y;
186 } else { 208 } else {
187 push @{ $map->[$_->{x}][$_->{y}] }, $_; 209 push @{ $map->[$x][$y] }, $_;
188 210
189 # arch map is unreliable w.r.t. width and height 211 # arch map is unreliable w.r.t. width and height
190 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 212 $mapx = $x + 1 if $mapx <= $x;
191 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 213 $mapy = $y + 1 if $mapy <= $y;
192 #$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};
193 #$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};
194 } 216 }
195 } 217 }
196 218
197 $meta{width} = $mapx; 219 $meta{width} = $mapx;
198 $meta{height} = $mapy; 220 $meta{height} = $mapy;
199 221
200 my %map_face;
201 my %draw_info; 222 my %draw_info;
223 my %map_info;
202 224
203 # first pass, gather face stacking order, border and corner info 225 # first pass, gather face stacking order, border and corner info
204 for my $x (0 .. $mapx - 1) { 226 for my $x (0 .. $mapx - 1) {
205 my $col = $map->[$x]; 227 my $col = $map->[$x];
206 for my $y (0 .. $mapy - 1) { 228 for my $y (0 .. $mapy - 1) {
207 my $as = $col->[$y] || []; 229 my $as = $col->[$y] || [];
208 230
231 my $minsmooth = 0;
232
209 for my $layer (0 .. $#$as) { 233 for my $layer (0 .. $#$as) {
210 my $a = $as->[$layer]; 234 my $a = $as->[$layer];
211 235
212 my $o = $arch->{$a->{_name}} 236 my $o = $arch->{$a->{_name}}
213 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;
214 238
215 my $A = { %$o, %$a }; 239 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
216 240
217 my $smoothlevel = $A->{smoothlevel}; 241 # hack to ensure somewhat correct ordering in case of conflicting
218 my $level = $smoothlevel ? $smoothlevel 242 # smoothlevel/stacking order
219 : $A->{is_floor} ? 0 243 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
220 : 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";
221 250
222 while ($o) { 251 while ($o) {
252 my $face = $a->{face} || $o->{face};
253
223 my $pb = tile $A->{face} 254 my $pb = tile $face
224 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;
225 256
226 my $mx = $x + $o->{x}; 257 my $mx = $x + $o->{x};
227 my $my = $y + $o->{y}; 258 my $my = $y + $o->{y};
228 259
229 last if 0 > $mx || $mx >= $mapx 260 last if 0 > $mx || $mx >= $mapx
230 || 0 > $my || $my >= $mapy; 261 || 0 > $my || $my >= $mapy;
231 262
232 # this is very ugly (some tiles are 32x33 or worse) 263 # this is very ugly (some tiles are 32x33 or worse)
233 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;
234 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
235 if (my $sface = $smooth->{$A->{face}}) { 273 if (my $sface = $smooth->{$face}) {
236 $bigface and die "can't handle bigfaces with smoothing ($A->{face})\n"; 274 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
237 275
238 # full tile 276 # full tile
239 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 277 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
240 278
241 # borders 279 # borders
249 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 287 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
250 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 288 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
251 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 289 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
252 } 290 }
253 291
254 my $dx = $bigface ? $o->{x} : 0;
255 my $dy = $bigface ? $o->{y} : 0;
256
257 $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
258
259 $o = $o->{more}; 292 $o = $o->{more};
293 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
260 } 294 }
261 } 295 }
262 } 296 }
263 } 297 }
264 298
265 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
266 or die; 300 or die;
267 $map_pb->fill (0x00000000); 301 $map_pb->fill (0xffffff00);
268 302
269 # second pass, render all the stuff 303 # second pass, render the map
270 for my $level (sort { $a <=> $b } keys %draw_info) { 304 for my $level (sort { $a <=> $b } keys %draw_info) {
271 my $v = $draw_info{$level}; 305 my $v = $draw_info{$level};
272 while (my ($sface, $info) = each %$v) { 306 while (my ($sface, $info) = each %$v) {
273 my $pb = tile $sface 307 my $pb = tile $sface
274 or die "no smooth face $sface\n"; 308 or die "no smooth face $sface\n";
277 my ($x, $y) = split $;, $xy; 311 my ($x, $y) = split $;, $xy;
278 312
279 next if $x < 0 || $x >= $mapx 313 next if $x < 0 || $x >= $mapx
280 || $y < 0 || $y >= $mapy; 314 || $y < 0 || $y >= $mapy;
281 315
282 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 316 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
283 # X don't draw
284 # F full tile draw with x|y bigface displacement 317 # f full tile draw with x|y bigface displacement
285 # c maybe draw these corners
286 # C do not draw these corners 318 # n do not draw borders&corners
319 # c draw these corners, but...
320 # C ... not these
287 # b draw these borders 321 # b draw these borders
288 322
289 if ($bits & 0x2000) { 323 if ($bits & 0x2000) {
290 my $dx = (($bits >> 24) & 0xff) - 128; 324 my $dx = (($bits >> 24) & 0xff) - 128;
291 my $dy = (($bits >> 16) & 0xff) - 128; 325 my $dy = (($bits >> 16) & 0xff) - 128;
321 } 355 }
322 } 356 }
323 } 357 }
324 } 358 }
325 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
326 ($map_pb, \%meta) 374 ($map_pb, \%meta)
327} 375}
328 376
329for my $file (@ARGV) { 377for my $file (@ARGV) {
330 my $mapa = read_arch $file; 378 my $mapa = read_arch $file;
331 my ($pb, $meta) = cfmap_render $mapa, $file; 379 my ($pb, $meta) = cfmap_render $mapa, $file;
332 $pb->save ("$file.png~", "png"); 380 $pb->save ("$file.png~~", "png");
333 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";
334 system "mogrify", "-colors" => 65536, "$file.png~"; 382 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
335 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";
336 unlink "$file.png~"; 385 unlink "$file.png~~";
337 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";
338} 390}
339 391
340 392
341 393
342 394

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines