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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.22 by root, Fri Apr 13 18:09:23 2007 UTC vs.
Revision 1.23 by root, Thu Jun 21 00:09:39 2007 UTC

25# middle face: Object with highest visibility (of monster/player on a space). If 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, 26# no monster/player, then object with second highest visibility, or if all equal,
27# second top object relative to map stacking. 27# second top object relative to map stacking.
28# Bottom object: the highest object that is a floor type object. 28# Bottom object: the highest object that is a floor type object.
29# 29#
30# ... i believe that anytime, but it still doesn't mention the smoothlevel 30# ... I believe that anytime, but it still doesn't mention the smoothlevel
31# interaction :( 31# interaction :(
32 32
33our $VERSION = '1.212'; 33our $VERSION = '2.001';
34 34
35use strict; 35use strict;
36 36
37use Storable; 37use Crossfire 1.0;
38use List::Util qw(max); 38use List::Util qw(max);
39 39
40use Gtk2; 40use Gtk2;
41 41
42#init Gtk2::Gdk;
43
44my $LIB = $ENV{CROSSFIRE_LIBDIR}
45 or die "\$CROSSFIRE_LIBDIR must be set\n";
46
47sub T (){ 32 } 42sub T (){ 32 }
48 43
49sub read_pak($) { 44Crossfire::load_archetypes;
50 my ($path) = @_; 45Crossfire::load_tilecache;
51
52 eval {
53 -M "$path.pst" < -M $path
54 && Storable::retrieve "$path.pst"
55 } or do {
56 my %pak;
57
58 open my $fh, "<:raw:perlio", $path
59 or die "$_[0]: $!";
60 while (<$fh>) {
61 my ($type, $id, $len, $path) = split;
62 $path =~ s/.*\///;
63 read $fh, $pak{$path}, $len;
64 }
65
66 Storable::nstore \%pak, "$path.pst";
67
68 \%pak
69 }
70}
71
72sub read_smooth($) {
73 my ($path) = @_;
74
75 eval {
76 -M "$path.pst" < -M $path
77 && Storable::retrieve "$path.pst"
78 } or do {
79 my %smooth;
80
81 open my $fh, "<:raw:perlio", $path
82 or die "$path: $!";
83 while (<$fh>) {
84 next if /^\s*($|#)/;
85
86 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
87 }
88
89 Storable::nstore \%smooth, "$path.pst";
90 utime +(stat $path)[8,9], "$path.pst";
91
92 \%smooth
93 }
94}
95
96sub read_arch($;$) {
97 my ($path, $cache) = @_;
98
99 eval {
100 $cache
101 && -M "$path.pst" < -M $path
102 && Storable::retrieve "$path.pst"
103 } or do {
104 my %arc;
105 my ($more, $prev);
106
107 open my $fh, "<:utf8", $path
108 or die "$path: $!";
109
110 my $parse_block; $parse_block = sub {
111 my %arc = @_;
112
113 while (<$fh>) {
114 s/\s+$//;
115 if (/^end$/i) {
116 last;
117 } elsif (/^arch (\S+)$/) {
118 push @{ $arc{inventory} }, $parse_block->(_name => $1);
119 } elsif (/^lore$/) {
120 while (<$fh>) {
121 last if /^endlore\s*$/i;
122 $arc{lore} .= $_;
123 }
124 } elsif (/^msg$/) {
125 while (<$fh>) {
126 last if /^endmsg\s*$/i;
127 $arc{msg} .= $_;
128 }
129 } elsif (/^(\S+)\s*(.*)$/) {
130 $arc{lc $1} = $2;
131 } elsif (/^\s*($|#)/) {
132 #
133 } else {
134 warn "$path: unparsable line '$_' in arch $arc{_name}";
135 }
136 }
137
138 \%arc
139 };
140
141 while (<$fh>) {
142 s/\s+$//;
143 if (/^more$/i) {
144 $more = $prev;
145 } elsif (/^object (\S+)$/i) {
146 my $name = $1;
147 my $arc = $parse_block->(_name => $name);
148
149 if ($more) {
150 $more->{more} = $arc;
151 } else {
152 $arc{$name} = $arc;
153 }
154
155 $prev = $arc;
156 $more = undef;
157 } elsif (/^arch (\S+)$/i) {
158 push @{ $arc{arch} }, $parse_block->(_name => $1);
159 } elsif (/^\s*($|#)/) {
160 #
161 } else {
162 warn "$path: unparseable top-level line '$_'";
163 }
164 }
165
166 undef $parse_block; # work around bug in perl not freeing $fh etc.
167
168 if ($cache) {
169 Storable::nstore \%arc, "$path.pst";
170 utime +(stat $path)[8,9], "$path.pst";
171 }
172
173 \%arc
174 }
175}
176
177my $arch = read_arch "$LIB/archetypes", 1;
178my $tile = read_pak "$LIB/crossfire.0";
179my $smooth = read_smooth "$LIB/smooth";
180
181sub tile($) {
182 my $name = $_[0];
183
184 my $pb = $tile->{$name}
185 or return;
186
187 unless (ref $pb) {
188 my $file = "/tmp/map2png.$$~";
189
190 open my $fh, ">:raw", $file
191 or die "$file: $!";
192 print $fh $pb;
193 close $fh;
194
195 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
196 unlink $file;
197 }
198
199 $pb
200}
201 46
202sub cfmap_render($;$) { 47sub cfmap_render($;$) {
203 my ($mapa, $mapname) = @_; 48 my ($mapa, $mapname) = @_;
204 49
205 my %meta; 50 my %meta;
237 for my $x (0 .. $mapx - 1) { 82 for my $x (0 .. $mapx - 1) {
238 my $col = $map->[$x]; 83 my $col = $map->[$x];
239 for my $y (0 .. $mapy - 1) { 84 for my $y (0 .. $mapy - 1) {
240 my $as = $col->[$y] || []; 85 my $as = $col->[$y] || [];
241 86
242 my $minsmooth = 0;
243
244 for my $layer (0 .. $#$as) { 87 for my $layer (0 .. $#$as) {
245 my $a = $as->[$layer]; 88 my $a = $as->[$layer];
246 89
247 my $o = $arch->{$a->{_name}} 90 my $o = $ARCH{$a->{_name}}
248 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;
249 92
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
257 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
258 my $level = $smoothlevel + $layer * 256; 93 my $level = $layer * 256;
259
260 $level -= 100 * 256 if $o->{_name} eq "blocked";
261 94
262 while ($o) { 95 while ($o) {
263 my $face = $a->{face} || $o->{face}; 96 my $face = $a->{face} || $o->{face};
264 97
265 my $pb = tile $face 98 $FACE{$face}
266 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};
267 102
268 my $mx = $x + $o->{x}; 103 my $mx = $x + $o->{x};
269 my $my = $y + $o->{y}; 104 my $my = $y + $o->{y};
270 105
271 last if 0 > $mx || $mx >= $mapx 106 last if 0 > $mx || $mx >= $mapx
272 || 0 > $my || $my >= $mapy; 107 || 0 > $my || $my >= $mapy;
273 108
274 # this is very ugly (some tiles are 32x33 or worse)
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; 109 push @{ $map_info{$level}{$mx, $my} }, $a;
281 110
282 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 111 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
283 112
284 if (my $sface = $smooth->{$face}) { 113 if (0) { # disable smoothing for the time being
285 $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};
286 119
287 # full tile 120 # full tile
288 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 121 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
289 122
290 # borders 123 # borders
291 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0091; 124 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
292 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0032; 125 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
293 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; 126 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
294 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; 127 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
295 128
296 # corners 129 # corners
297 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; 130 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
298 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 131 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
299 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 132 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
300 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 133 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
301 } 134 }
135 }
302 136
303 $o = $o->{more}; 137 $o = $o->{more};
304 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
305 } 138 }
306 } 139 }
307 } 140 }
308 } 141 }
309 142
310 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
311 or die; 144 or die;
312 $map_pb->fill (0xffffff00); 145 $map_pb->fill (0xffffff00);
313 146
314 # second pass, render the map 147 # second pass, render the map
315 for my $level (sort { $a <=> $b } keys %draw_info) { 148 for my $level (sort { $a <=> $b } keys %draw_info) {
316 my $v = $draw_info{$level}; 149 my $v = $draw_info{$level};
317 while (my ($sface, $info) = each %$v) { 150 while (my ($face, $info) = each %$v) {
318 my $pb = tile $sface
319 or die "no smooth face $sface\n";
320
321 while (my ($xy, $bits) = each %$info) { 151 while (my ($xy, $bits) = each %$info) {
322 my ($x, $y) = split $;, $xy; 152 my ($x, $y) = split $;, $xy;
323 153
324 next if $x < 0 || $x >= $mapx 154 next if $x < 0 || $x >= $mapx
325 || $y < 0 || $y >= $mapy; 155 || $y < 0 || $y >= $mapy;
326 156
327 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb 157 # bits is __fn cccc CCCC bbbb
328 # f full tile draw with x|y bigface displacement 158 # f full tile draw with x|y bigface displacement
329 # n do not draw borders&corners 159 # n do not draw borders&corners
330 # c draw these corners, but... 160 # c draw these corners, but...
331 # C ... not these 161 # C ... not these
332 # b draw these borders 162 # b draw these borders
333 163
334 if ($bits & 0x2000) { 164 if ($bits & 0x2000) {
335 my $dx = (($bits >> 24) & 0xff) - 128; 165 my $ix = $face % CACHESTRIDE;
336 my $dy = (($bits >> 16) & 0xff) - 128; 166 my $iy = int $face / CACHESTRIDE;
337 167
338 $pb->composite ($map_pb, 168 $TILE->composite ($map_pb,
339 $x * T, $y * T, 169 $x * T, $y * T,
340 T, T, 170 T, T,
341 ($x - $dx) * T, ($y - $dy) * T, 1, 1, 171 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
342 "nearest", 172 "nearest",
343 255 173 255
344 ); 174 );
345 } 175 }
346 176
347 unless ($bits & 0x1000) { 177 unless ($bits & 0x1000) {
348 my $border = $bits & 0xf; 178 my $border = $face + ($bits & 0xf);
349 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; 179 my $bx = $border % CACHESTRIDE;
350 180 my $by = int $border / CACHESTRIDE;
351 $pb->composite ($map_pb, 181 $TILE->composite ($map_pb,
352 $x * T, $y * T, 182 $x * T, $y * T,
353 T, T, 183 T, T,
354 ($x - $border) * T, $y * T, 1, 1, 184 ($x - $bx) * T, ($y - $by) * T, 1, 1,
355 "nearest", 185 "nearest",
356 255 186 255
357 ) if $border; 187 ) if $border;
358 188
189 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
190 my $cx = $corner % CACHESTRIDE;
191 my $cy = int $corner / CACHESTRIDE;
359 $pb->composite ($map_pb, 192 $TILE->composite ($map_pb,
360 $x * T, $y * T, 193 $x * T, $y * T,
361 T, T, 194 T, T,
362 ($x - $corner) * T, ($y - 1) * T, 1, 1, 195 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
363 "nearest", 196 "nearest",
364 255 197 255
365 ) if $corner; 198 ) if $corner;
366 } 199 }
367 } 200 }
390 $file =~ s/\.map$//; 223 $file =~ s/\.map$//;
391 my ($pb, $meta) = cfmap_render $mapa, $file; 224 my ($pb, $meta) = cfmap_render $mapa, $file;
392 $pb->save ("$file.png~~", "png"); 225 $pb->save ("$file.png~~", "png");
393 system "gm", "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";
394 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 227 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
395 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~~", "$file.png~"; 228 system "optipng", "-q", "-out", "$file.png~", "$file.png~~";
396# system "pngnq <\Q$file.png~\E >\Q$file.png\E"; 229# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
397 unlink "$file.png~~"; 230 unlink "$file.png~~";
398 Storable::nstore $meta, "$file.pst"; 231 Storable::nstore $meta, "$file.pst";
399 utime +(stat $file)[8,9], "$file.pst"; 232 utime +(stat $file)[8,9], "$file.pst";
400 utime +(stat $file)[8,9], "$file.jpg"; 233 utime +(stat $file)[8,9], "$file.jpg";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines