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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.16 by root, Mon Dec 12 01:37:11 2005 UTC vs.
Revision 1.25 by root, Sun Jan 6 21:12:00 2008 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# cfarch2png - convert crossfire maps to png+metadata 3# cfarch2png - convert deliantra maps to png+metadata
4# Copyright (C) 2005 Marc Lehmann <gvpe@schmorp.de> 4# Copyright (C) 2005,2007,2008 Marc Lehmann <cfmaps@schmorp.de>
5# 5#
6# CFARCH2PNG is free software; you can redistribute it and/or modify 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 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 8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version. 9# (at your option) any later version.
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 :(
21 32
22our $VERSION = '1.2'; 33our $VERSION = '2.002';
23 34
24use strict; 35use strict;
25 36
26use Storable; 37use Deliantra;
27use List::Util qw(max); 38use List::Util qw(max);
28 39
29use Gtk2; 40use Gtk2;
30 41
31#init Gtk2::Gdk;
32
33my $LIB = $ENV{CROSSFIRE_LIBDIR}
34 or die "\$CROSSFIRE_LIBDIR must be set\n";
35
36sub T (){ 32 } 42sub T (){ 32 }
37 43
38sub read_pak($) { 44Deliantra::load_archetypes;
39 my ($path) = @_; 45Deliantra::load_tilecache;
40
41 eval {
42 -M "$path.pst" < -M $path
43 && Storable::retrieve "$path.pst"
44 } or do {
45 my %pak;
46
47 open my $fh, "<:raw", $path
48 or die "$_[0]: $!";
49 while (<$fh>) {
50 my ($type, $id, $len, $path) = split;
51 $path =~ s/.*\///;
52 read $fh, $pak{$path}, $len;
53 }
54
55 Storable::nstore \%pak, "$path.pst";
56
57 \%pak
58 }
59}
60
61sub read_smooth($) {
62 my ($path) = @_;
63
64 eval {
65 -M "$path.pst" < -M $path
66 && Storable::retrieve "$path.pst"
67 } or do {
68 my %smooth;
69
70 open my $fh, "<:raw", $path
71 or die "$path: $!";
72 while (<$fh>) {
73 next if /^\s*($|#)/;
74
75 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
76 }
77
78 Storable::nstore \%smooth, "$path.pst";
79 utime +(stat $path)[8,9], "$path.pst";
80
81 \%smooth
82 }
83}
84
85sub read_arch($;$) {
86 my ($path, $cache) = @_;
87
88 eval {
89 $cache
90 && -M "$path.pst" < -M $path
91 && Storable::retrieve "$path.pst"
92 } or do {
93 my %arc;
94 my ($more, $prev);
95
96 open my $fh, "<:raw", $path
97 or die "$path: $!";
98
99 my $parse_block; $parse_block = sub {
100 my %arc = @_;
101
102 while (<$fh>) {
103 s/\s+$//;
104 if (/^end$/i) {
105 last;
106 } elsif (/^arch (\S+)$/) {
107 push @{ $arc{inventory} }, $parse_block->(_name => $1);
108 } elsif (/^lore$/) {
109 while (<$fh>) {
110 last if /^endlore\s*$/i;
111 $arc{lore} .= $_;
112 }
113 } elsif (/^msg$/) {
114 while (<$fh>) {
115 last if /^endmsg\s*$/i;
116 $arc{msg} .= $_;
117 }
118 } elsif (/^(\S+)\s*(.*)$/) {
119 $arc{lc $1} = $2;
120 } elsif (/^\s*($|#)/) {
121 #
122 } else {
123 warn "$path: unparsable line '$_' in arch $arc{_name}";
124 }
125 }
126
127 \%arc
128 };
129
130 while (<$fh>) {
131 s/\s+$//;
132 if (/^more$/i) {
133 $more = $prev;
134 } elsif (/^object (\S+)$/i) {
135 my $name = $1;
136 my $arc = $parse_block->(_name => $name);
137
138 if ($more) {
139 $more->{more} = $arc;
140 } else {
141 $arc{$name} = $arc;
142 }
143
144 $prev = $arc;
145 $more = undef;
146 } elsif (/^arch (\S+)$/i) {
147 push @{ $arc{arch} }, $parse_block->(_name => $1);
148 } elsif (/^\s*($|#)/) {
149 #
150 } else {
151 warn "$path: unparseable top-level line '$_'";
152 }
153 }
154
155 undef $parse_block; # work around bug in perl not freeing $fh etc.
156
157 if ($cache) {
158 Storable::nstore \%arc, "$path.pst";
159 utime +(stat $path)[8,9], "$path.pst";
160 }
161
162 \%arc
163 }
164}
165
166my $arch = read_arch "$LIB/archetypes", 1;
167my $tile = read_pak "$LIB/crossfire.0";
168my $smooth = read_smooth "$LIB/smooth";
169
170sub tile($) {
171 my $name = $_[0];
172
173 my $pb = $tile->{$name}
174 or return;
175
176 unless (ref $pb) {
177 my $file = "/tmp/map2png.$$~";
178
179 open my $fh, ">:raw", $file
180 or die "$file: $!";
181 print $fh $pb;
182 close $fh;
183
184 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
185 unlink $file;
186 }
187
188 $pb
189}
190 46
191sub cfmap_render($;$) { 47sub cfmap_render($;$) {
192 my ($mapa, $mapname) = @_; 48 my ($mapa, $mapname) = @_;
193 49
194 my %meta; 50 my %meta;
226 for my $x (0 .. $mapx - 1) { 82 for my $x (0 .. $mapx - 1) {
227 my $col = $map->[$x]; 83 my $col = $map->[$x];
228 for my $y (0 .. $mapy - 1) { 84 for my $y (0 .. $mapy - 1) {
229 my $as = $col->[$y] || []; 85 my $as = $col->[$y] || [];
230 86
231 my $minsmooth = 0;
232
233 for my $layer (0 .. $#$as) { 87 for my $layer (0 .. $#$as) {
234 my $a = $as->[$layer]; 88 my $a = $as->[$layer];
235 89
236 my $o = $arch->{$a->{_name}} 90 my $o = $ARCH{$a->{_name}}
237 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 91 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
238 92
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
246 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
247 my $level = $smoothlevel + $layer * 256; 93 my $level = $layer * 256;
248
249 $level -= 100 * 256 if $o->{_name} eq "blocked";
250 94
251 while ($o) { 95 while ($o) {
252 my $face = $a->{face} || $o->{face}; 96 my $face = $a->{face} || $o->{face};
253 97
254 my $pb = tile $face 98 $FACE{$face}
255 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; 99 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
100
101 my $idx = $FACE{$face}{idx};
256 102
257 my $mx = $x + $o->{x}; 103 my $mx = $x + $o->{x};
258 my $my = $y + $o->{y}; 104 my $my = $y + $o->{y};
259 105
260 last if 0 > $mx || $mx >= $mapx 106 last if 0 > $mx || $mx >= $mapx
261 || 0 > $my || $my >= $mapy; 107 || 0 > $my || $my >= $mapy;
262 108
263 # this is very ugly (some tiles are 32x33 or worse)
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; 109 push @{ $map_info{$level}{$mx, $my} }, $a;
270 110
271 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 111 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
272 112
273 if (my $sface = $smooth->{$face}) { 113 if (0) { # disable smoothing for the time being
274 $bigface and die "can't handle bigfaces with smoothing ($face)\n"; 114 if (my $sface = $FACEDATA{faceinfo}{$face}{smooth}) {
115 $FACE{$sface}
116 or (warn "$mapname: smoothface '$sface' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
117 my $idx = $FACE{$sface}{idx};
118 my $level = $layer * 256 + $FACEDATA{faceinfo}{$face}{smoothlevel};
275 119
276 # full tile 120 # full tile
277 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 121 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
278 122
279 # borders 123 # borders
280 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; 124 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
281 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; 125 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
282 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; 126 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
283 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; 127 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
284 128
285 # corners 129 # corners
286 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; 130 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
287 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 131 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
288 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 132 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
289 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 133 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
290 } 134 }
135 }
291 136
292 $o = $o->{more}; 137 $o = $o->{more};
293 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
294 } 138 }
295 } 139 }
296 } 140 }
297 } 141 }
298 142
299 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 143 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
300 or die; 144 or die;
301 $map_pb->fill (0xffffff00); 145 $map_pb->fill (0xffffff00);
302 146
303 # second pass, render the map 147 # second pass, render the map
304 for my $level (sort { $a <=> $b } keys %draw_info) { 148 for my $level (sort { $a <=> $b } keys %draw_info) {
305 my $v = $draw_info{$level}; 149 my $v = $draw_info{$level};
306 while (my ($sface, $info) = each %$v) { 150 while (my ($face, $info) = each %$v) {
307 my $pb = tile $sface
308 or die "no smooth face $sface\n";
309
310 while (my ($xy, $bits) = each %$info) { 151 while (my ($xy, $bits) = each %$info) {
311 my ($x, $y) = split $;, $xy; 152 my ($x, $y) = split $;, $xy;
312 153
313 next if $x < 0 || $x >= $mapx 154 next if $x < 0 || $x >= $mapx
314 || $y < 0 || $y >= $mapy; 155 || $y < 0 || $y >= $mapy;
315 156
316 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb 157 # bits is __fn cccc CCCC bbbb
317 # f full tile draw with x|y bigface displacement 158 # f full tile draw with x|y bigface displacement
318 # n do not draw borders&corners 159 # n do not draw borders&corners
319 # c draw these corners, but... 160 # c draw these corners, but...
320 # C ... not these 161 # C ... not these
321 # b draw these borders 162 # b draw these borders
322 163
323 if ($bits & 0x2000) { 164 if ($bits & 0x2000) {
324 my $dx = (($bits >> 24) & 0xff) - 128; 165 my $ix = $face % CACHESTRIDE;
325 my $dy = (($bits >> 16) & 0xff) - 128; 166 my $iy = int $face / CACHESTRIDE;
326 167
327 $pb->composite ($map_pb, 168 $TILE->composite ($map_pb,
328 $x * T, $y * T, 169 $x * T, $y * T,
329 T, T, 170 T, T,
330 ($x - $dx) * T, ($y - $dy) * T, 1, 1, 171 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
331 "nearest", 172 "nearest",
332 255 173 255
333 ); 174 );
334 } 175 }
335 176
336 unless ($bits & 0x1000) { 177 unless ($bits & 0x1000) {
337 my $border = $bits & 0xf; 178 my $border = $face + ($bits & 0xf);
338 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; 179 my $bx = $border % CACHESTRIDE;
339 180 my $by = int $border / CACHESTRIDE;
340 $pb->composite ($map_pb, 181 $TILE->composite ($map_pb,
341 $x * T, $y * T, 182 $x * T, $y * T,
342 T, T, 183 T, T,
343 ($x - $border) * T, $y * T, 1, 1, 184 ($x - $bx) * T, ($y - $by) * T, 1, 1,
344 "nearest", 185 "nearest",
345 255 186 255
346 ) if $border; 187 ) if $border;
347 188
189 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
190 my $cx = $corner % CACHESTRIDE;
191 my $cy = int $corner / CACHESTRIDE;
348 $pb->composite ($map_pb, 192 $TILE->composite ($map_pb,
349 $x * T, $y * T, 193 $x * T, $y * T,
350 T, T, 194 T, T,
351 ($x - $corner) * T, ($y - 1) * T, 1, 1, 195 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
352 "nearest", 196 "nearest",
353 255 197 255
354 ) if $corner; 198 ) if $corner;
355 } 199 }
356 } 200 }
374 ($map_pb, \%meta) 218 ($map_pb, \%meta)
375} 219}
376 220
377for my $file (@ARGV) { 221for my $file (@ARGV) {
378 my $mapa = read_arch $file; 222 my $mapa = read_arch $file;
223 $file =~ s/\.map$//;
379 my ($pb, $meta) = cfmap_render $mapa, $file; 224 my ($pb, $meta) = cfmap_render $mapa, $file;
380 $pb->save ("$file.png~~", "png"); 225 $pb->save ("$file.png~~", "png");
381 system "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; 226 system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
382 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 227 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
383 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~"; 228 system "optipng", "-q", "-out", "$file.png~", "$file.png~~";
384# system "pngnq <\Q$file.png~\E >\Q$file.png\E"; 229# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
385 unlink "$file.png~~"; 230 unlink "$file.png~~";
386 Storable::nstore $meta, "$file.pst";
387 utime +(stat $file)[8,9], "$file.pst"; 231 utime +(stat $file)[8,9], "$file.jpg";
388 utime +(stat $file)[8,9], "$file.png~"; 232 utime +(stat $file)[8,9], "$file.png~";
389 rename "$file.png~", "$file.png"; 233 rename "$file.png~", "$file.png";
390} 234}
391 235
392 236

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines