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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.9 by root, Sun Nov 20 01:05:34 2005 UTC vs.
Revision 1.18 by root, Mon Dec 12 01:41:50 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines