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.20 by root, Sat Sep 16 18:39:10 2006 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? 20# Quoth The master himself:
21#
22# Object ordering is basically like this:
23# top face: players or monsters. If none on the space, object with highest
24# visibility value - if equal, then top object in terms of object stacking on the map.
25# middle face: Object with highest visibility (of monster/player on a space). If
26# no monster/player, then object with second highest visibility, or if all equal,
27# second top object relative to map stacking.
28# Bottom object: the highest object that is a floor type object.
29#
30# ... i believe that anytime, but it still doesn't mention the smoothlevel
31# interaction :(
32
33our $VERSION = '1.21';
21 34
22use strict; 35use strict;
23 36
24use Storable; 37use Storable;
25use List::Util qw(max); 38use List::Util qw(max);
26 39
27use Gtk2; 40use Gtk2;
28
29our $VERSION = '1.0';
30 41
31#init Gtk2::Gdk; 42#init Gtk2::Gdk;
32 43
33my $LIB = $ENV{CROSSFIRE_LIBDIR} 44my $LIB = $ENV{CROSSFIRE_LIBDIR}
34 or die "\$CROSSFIRE_LIBDIR must be set\n"; 45 or die "\$CROSSFIRE_LIBDIR must be set\n";
42 -M "$path.pst" < -M $path 53 -M "$path.pst" < -M $path
43 && Storable::retrieve "$path.pst" 54 && Storable::retrieve "$path.pst"
44 } or do { 55 } or do {
45 my %pak; 56 my %pak;
46 57
47 open my $fh, "<:raw", $path 58 open my $fh, "<:raw:perlio", $path
48 or die "$_[0]: $!"; 59 or die "$_[0]: $!";
49 while (<$fh>) { 60 while (<$fh>) {
50 my ($type, $id, $len, $path) = split; 61 my ($type, $id, $len, $path) = split;
51 $path =~ s/.*\///; 62 $path =~ s/.*\///;
52 read $fh, $pak{$path}, $len; 63 read $fh, $pak{$path}, $len;
65 -M "$path.pst" < -M $path 76 -M "$path.pst" < -M $path
66 && Storable::retrieve "$path.pst" 77 && Storable::retrieve "$path.pst"
67 } or do { 78 } or do {
68 my %smooth; 79 my %smooth;
69 80
70 open my $fh, "<:raw", $path 81 open my $fh, "<:raw:perlio", $path
71 or die "$path: $!"; 82 or die "$path: $!";
72 while (<$fh>) { 83 while (<$fh>) {
73 next if /^\s*($|#)/; 84 next if /^\s*($|#)/;
74 85
75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/; 86 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
76 } 87 }
77 88
78 Storable::nstore \%smooth, "$path.pst"; 89 Storable::nstore \%smooth, "$path.pst";
90 utime +(stat $path)[8,9], "$path.pst";
79 91
80 \%smooth 92 \%smooth
81 } 93 }
82} 94}
83 95
90 && Storable::retrieve "$path.pst" 102 && Storable::retrieve "$path.pst"
91 } or do { 103 } or do {
92 my %arc; 104 my %arc;
93 my ($more, $prev); 105 my ($more, $prev);
94 106
95 open my $fh, "<:raw", $path 107 open my $fh, "<:utf8", $path
96 or die "$path: $!"; 108 or die "$path: $!";
97 109
98 my $parse_block; $parse_block = sub { 110 my $parse_block; $parse_block = sub {
99 my %arc = @_; 111 my %arc = @_;
100 112
151 } 163 }
152 } 164 }
153 165
154 undef $parse_block; # work around bug in perl not freeing $fh etc. 166 undef $parse_block; # work around bug in perl not freeing $fh etc.
155 167
168 if ($cache) {
156 Storable::nstore \%arc, "$path.pst" 169 Storable::nstore \%arc, "$path.pst";
157 if $cache; 170 utime +(stat $path)[8,9], "$path.pst";
171 }
158 172
159 \%arc 173 \%arc
160 } 174 }
161} 175}
162 176
190 204
191 my %meta; 205 my %meta;
192 206
193 my ($mapx, $mapy); 207 my ($mapx, $mapy);
194 208
195 my $map = $meta{map} = []; 209 my $map;
196 210
197 for (@{ $mapa->{arch} }) { 211 for (@{ $mapa->{arch} }) {
212 my ($x, $y) = ($_->{x}, $_->{y});
213
198 if ($_->{_name} eq "map") { 214 if ($_->{_name} eq "map") {
199 $meta{info} = $_; 215 $meta{info} = $_;
200 216
201 $mapx = $_->{width} || $_->{x}; 217 $mapx = $_->{width} || $x;
202 $mapy = $_->{height} || $_->{y}; 218 $mapy = $_->{height} || $y;
203 } else { 219 } else {
204 push @{ $map->[$_->{x}][$_->{y}] }, $_; 220 push @{ $map->[$x][$y] }, $_;
205 221
206 # arch map is unreliable w.r.t. width and height 222 # arch map is unreliable w.r.t. width and height
207 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 223 $mapx = $x + 1 if $mapx <= $x;
208 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 224 $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}; 225 #$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}; 226 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
211 } 227 }
212 } 228 }
213 229
214 $meta{width} = $mapx; 230 $meta{width} = $mapx;
215 $meta{height} = $mapy; 231 $meta{height} = $mapy;
216 232
217 my %draw_info; 233 my %draw_info;
234 my %map_info;
218 235
219 # first pass, gather face stacking order, border and corner info 236 # first pass, gather face stacking order, border and corner info
220 for my $x (0 .. $mapx - 1) { 237 for my $x (0 .. $mapx - 1) {
221 my $col = $map->[$x]; 238 my $col = $map->[$x];
222 for my $y (0 .. $mapy - 1) { 239 for my $y (0 .. $mapy - 1) {
223 my $as = $col->[$y] || []; 240 my $as = $col->[$y] || [];
224 241
242 my $minsmooth = 0;
243
225 for my $layer (0 .. $#$as) { 244 for my $layer (0 .. $#$as) {
226 my $a = $as->[$layer]; 245 my $a = $as->[$layer];
227 246
228 my $o = $arch->{$a->{_name}} 247 my $o = $arch->{$a->{_name}}
229 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 248 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
230 249
231 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; 250 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
251
252 # hack to ensure somewhat correct ordering in case of conflicting
253 # smoothlevel/stacking order
254 $smoothlevel = $minsmooth + 0.01 if $minsmooth >= $smoothlevel;
255 $minsmooth = $smoothlevel;
256
232 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor}; 257 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
233 my $level = $smoothlevel ? $smoothlevel 258 my $level = $smoothlevel + $layer * 256;
234 : $is_floor ? 0 259
235 : 256 + $layer; 260 $level -= 100 * 256 if $o->{_name} eq "blocked";
236 261
237 while ($o) { 262 while ($o) {
238 my $face = $a->{face} || $o->{face}; 263 my $face = $a->{face} || $o->{face};
239 264
240 my $pb = tile $face 265 my $pb = tile $face
246 last if 0 > $mx || $mx >= $mapx 271 last if 0 > $mx || $mx >= $mapx
247 || 0 > $my || $my >= $mapy; 272 || 0 > $my || $my >= $mapy;
248 273
249 # this is very ugly (some tiles are 32x33 or worse) 274 # this is very ugly (some tiles are 32x33 or worse)
250 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; 275 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
276
277 my $dx = $bigface ? $o->{x} : 0;
278 my $dy = $bigface ? $o->{y} : 0;
279
280 push @{ $map_info{$level}{$mx, $my} }, $a;
281
282 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
251 283
252 if (my $sface = $smooth->{$face}) { 284 if (my $sface = $smooth->{$face}) {
253 $bigface and die "can't handle bigfaces with smoothing ($face)\n"; 285 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
254 286
255 # full tile 287 # full tile
266 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 298 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
267 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 299 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
268 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 300 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
269 } 301 }
270 302
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}; 303 $o = $o->{more};
304 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
277 } 305 }
278 } 306 }
279 } 307 }
280 } 308 }
281 309
282 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 310 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
283 or die; 311 or die;
284 $map_pb->fill (0xffffff00); 312 $map_pb->fill (0xffffff00);
285 313
286 # second pass, render all the stuff 314 # second pass, render the map
287 for my $level (sort { $a <=> $b } keys %draw_info) { 315 for my $level (sort { $a <=> $b } keys %draw_info) {
288 my $v = $draw_info{$level}; 316 my $v = $draw_info{$level};
289 while (my ($sface, $info) = each %$v) { 317 while (my ($sface, $info) = each %$v) {
290 my $pb = tile $sface 318 my $pb = tile $sface
291 or die "no smooth face $sface\n"; 319 or die "no smooth face $sface\n";
294 my ($x, $y) = split $;, $xy; 322 my ($x, $y) = split $;, $xy;
295 323
296 next if $x < 0 || $x >= $mapx 324 next if $x < 0 || $x >= $mapx
297 || $y < 0 || $y >= $mapy; 325 || $y < 0 || $y >= $mapy;
298 326
299 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 327 # 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 328 # f full tile draw with x|y bigface displacement
302 # c maybe draw these corners
303 # C do not draw these corners 329 # n do not draw borders&corners
330 # c draw these corners, but...
331 # C ... not these
304 # b draw these borders 332 # b draw these borders
305 333
306 if ($bits & 0x2000) { 334 if ($bits & 0x2000) {
307 my $dx = (($bits >> 24) & 0xff) - 128; 335 my $dx = (($bits >> 24) & 0xff) - 128;
308 my $dy = (($bits >> 16) & 0xff) - 128; 336 my $dy = (($bits >> 16) & 0xff) - 128;
338 } 366 }
339 } 367 }
340 } 368 }
341 } 369 }
342 370
371 # third pass, gather meta info
372 for my $level (sort { $a <=> $b } keys %map_info) {
373 my $info = $map_info{$level};
374
375 while (my ($xy, $as) = each %$info) {
376 my ($x, $y) = split $;, $xy;
377
378 next if $x < 0 || $x >= $mapx
379 || $y < 0 || $y >= $mapy;
380
381 push @{ $meta{map}[$x][$y] }, $_ for @$as;
382 }
383 }
384
343 ($map_pb, \%meta) 385 ($map_pb, \%meta)
344} 386}
345 387
346for my $file (@ARGV) { 388for my $file (@ARGV) {
347 my $mapa = read_arch $file; 389 my $mapa = read_arch $file;
348 my ($pb, $meta) = cfmap_render $mapa, $file; 390 my ($pb, $meta) = cfmap_render $mapa, $file;
349 $pb->save ("$file.png~", "png"); 391 $pb->save ("$file.png~~", "png");
350 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; 392 system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
351 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 393 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
352 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; 394 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~";
395# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
353 unlink "$file.png~"; 396 unlink "$file.png~~";
354 Storable::nstore $meta, "$file.pst"; 397 Storable::nstore $meta, "$file.pst";
398 utime +(stat $file)[8,9], "$file.pst";
399 utime +(stat $file)[8,9], "$file.jpg";
400 utime +(stat $file)[8,9], "$file.png~";
401 rename "$file.png~", "$file.png";
355} 402}
356 403
357 404
358 405
359 406

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines