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.17 by root, Mon Dec 12 01:37:18 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines