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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.13 by root, Wed Nov 23 18:48:17 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# world_108_123 (8|18), hole below grass but shouldn't? 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 :(
22 32
23our $VERSION = '1.11'; 33our $VERSION = '2.002';
24 34
25use strict; 35use strict;
26 36
27use Storable; 37use Deliantra;
28use List::Util qw(max); 38use List::Util qw(max);
29 39
30use Gtk2; 40use Gtk2;
31 41
32#init Gtk2::Gdk;
33
34my $LIB = $ENV{CROSSFIRE_LIBDIR}
35 or die "\$CROSSFIRE_LIBDIR must be set\n";
36
37sub T (){ 32 } 42sub T (){ 32 }
38 43
39sub read_pak($) { 44Deliantra::load_archetypes;
40 my ($path) = @_; 45Deliantra::load_tilecache;
41
42 eval {
43 -M "$path.pst" < -M $path
44 && Storable::retrieve "$path.pst"
45 } or do {
46 my %pak;
47
48 open my $fh, "<:raw", $path
49 or die "$_[0]: $!";
50 while (<$fh>) {
51 my ($type, $id, $len, $path) = split;
52 $path =~ s/.*\///;
53 read $fh, $pak{$path}, $len;
54 }
55
56 Storable::nstore \%pak, "$path.pst";
57
58 \%pak
59 }
60}
61
62sub read_smooth($) {
63 my ($path) = @_;
64
65 eval {
66 -M "$path.pst" < -M $path
67 && Storable::retrieve "$path.pst"
68 } or do {
69 my %smooth;
70
71 open my $fh, "<:raw", $path
72 or die "$path: $!";
73 while (<$fh>) {
74 next if /^\s*($|#)/;
75
76 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
77 }
78
79 Storable::nstore \%smooth, "$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 Storable::nstore \%arc, "$path.pst"
158 if $cache;
159
160 \%arc
161 }
162}
163
164my $arch = read_arch "$LIB/archetypes", 1;
165my $tile = read_pak "$LIB/crossfire.0";
166my $smooth = read_smooth "$LIB/smooth";
167
168sub tile($) {
169 my $name = $_[0];
170
171 my $pb = $tile->{$name}
172 or return;
173
174 unless (ref $pb) {
175 my $file = "/tmp/map2png.$$~";
176
177 open my $fh, ">:raw", $file
178 or die "$file: $!";
179 print $fh $pb;
180 close $fh;
181
182 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
183 unlink $file;
184 }
185
186 $pb
187}
188 46
189sub cfmap_render($;$) { 47sub cfmap_render($;$) {
190 my ($mapa, $mapname) = @_; 48 my ($mapa, $mapname) = @_;
191 49
192 my %meta; 50 my %meta;
223 # first pass, gather face stacking order, border and corner info 81 # first pass, gather face stacking order, border and corner info
224 for my $x (0 .. $mapx - 1) { 82 for my $x (0 .. $mapx - 1) {
225 my $col = $map->[$x]; 83 my $col = $map->[$x];
226 for my $y (0 .. $mapy - 1) { 84 for my $y (0 .. $mapy - 1) {
227 my $as = $col->[$y] || []; 85 my $as = $col->[$y] || [];
228 86
229 for my $layer (0 .. $#$as) { 87 for my $layer (0 .. $#$as) {
230 my $a = $as->[$layer]; 88 my $a = $as->[$layer];
231 89
232 my $o = $arch->{$a->{_name}} 90 my $o = $ARCH{$a->{_name}}
233 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;
234 92
235 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; 93 my $level = $layer * 256;
236 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
237 my $level = $smoothlevel ? $smoothlevel
238 : $is_floor ? $layer - 1000
239 : $layer + 1000;
240 94
241 while ($o) { 95 while ($o) {
242 my $face = $a->{face} || $o->{face}; 96 my $face = $a->{face} || $o->{face};
243 97
244 my $pb = tile $face 98 $FACE{$face}
245 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};
246 102
247 my $mx = $x + $o->{x}; 103 my $mx = $x + $o->{x};
248 my $my = $y + $o->{y}; 104 my $my = $y + $o->{y};
249 105
250 last if 0 > $mx || $mx >= $mapx 106 last if 0 > $mx || $mx >= $mapx
251 || 0 > $my || $my >= $mapy; 107 || 0 > $my || $my >= $mapy;
252 108
253 # this is very ugly (some tiles are 32x33 or worse)
254 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
255
256 my $dx = $bigface ? $o->{x} : 0;
257 my $dy = $bigface ? $o->{y} : 0;
258
259 push @{ $map_info{$level}{$mx, $my} }, $a; 109 push @{ $map_info{$level}{$mx, $my} }, $a;
260 110
261 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 111 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
262 112
263 if (my $sface = $smooth->{$face}) { 113 if (0) { # disable smoothing for the time being
264 $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};
265 119
266 # full tile 120 # full tile
267 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 121 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
268 122
269 # borders 123 # borders
270 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; 124 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
271 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; 125 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
272 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; 126 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
273 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; 127 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
274 128
275 # corners 129 # corners
276 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; 130 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
277 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 131 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
278 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 132 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
279 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 133 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
134 }
280 } 135 }
281 136
282 $o = $o->{more}; 137 $o = $o->{more};
283 } 138 }
284 } 139 }
285 } 140 }
286 } 141 }
287 142
288 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
289 or die; 144 or die;
290 $map_pb->fill (0xffffff00); 145 $map_pb->fill (0xffffff00);
291 146
292 # second pass, render the map 147 # second pass, render the map
293 for my $level (sort { $a <=> $b } keys %draw_info) { 148 for my $level (sort { $a <=> $b } keys %draw_info) {
294 my $v = $draw_info{$level}; 149 my $v = $draw_info{$level};
295 while (my ($sface, $info) = each %$v) { 150 while (my ($face, $info) = each %$v) {
296 my $pb = tile $sface
297 or die "no smooth face $sface\n";
298
299 while (my ($xy, $bits) = each %$info) { 151 while (my ($xy, $bits) = each %$info) {
300 my ($x, $y) = split $;, $xy; 152 my ($x, $y) = split $;, $xy;
301 153
302 next if $x < 0 || $x >= $mapx 154 next if $x < 0 || $x >= $mapx
303 || $y < 0 || $y >= $mapy; 155 || $y < 0 || $y >= $mapy;
304 156
305 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb 157 # bits is __fn cccc CCCC bbbb
306 # f full tile draw with x|y bigface displacement 158 # f full tile draw with x|y bigface displacement
307 # n do not draw borders&corners 159 # n do not draw borders&corners
308 # c draw these corners, but... 160 # c draw these corners, but...
309 # C ... not these 161 # C ... not these
310 # b draw these borders 162 # b draw these borders
311 163
312 if ($bits & 0x2000) { 164 if ($bits & 0x2000) {
313 my $dx = (($bits >> 24) & 0xff) - 128; 165 my $ix = $face % CACHESTRIDE;
314 my $dy = (($bits >> 16) & 0xff) - 128; 166 my $iy = int $face / CACHESTRIDE;
315 167
316 $pb->composite ($map_pb, 168 $TILE->composite ($map_pb,
317 $x * T, $y * T, 169 $x * T, $y * T,
318 T, T, 170 T, T,
319 ($x - $dx) * T, ($y - $dy) * T, 1, 1, 171 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
320 "nearest", 172 "nearest",
321 255 173 255
322 ); 174 );
323 } 175 }
324 176
325 unless ($bits & 0x1000) { 177 unless ($bits & 0x1000) {
326 my $border = $bits & 0xf; 178 my $border = $face + ($bits & 0xf);
327 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; 179 my $bx = $border % CACHESTRIDE;
328 180 my $by = int $border / CACHESTRIDE;
329 $pb->composite ($map_pb, 181 $TILE->composite ($map_pb,
330 $x * T, $y * T, 182 $x * T, $y * T,
331 T, T, 183 T, T,
332 ($x - $border) * T, $y * T, 1, 1, 184 ($x - $bx) * T, ($y - $by) * T, 1, 1,
333 "nearest", 185 "nearest",
334 255 186 255
335 ) if $border; 187 ) if $border;
336 188
189 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
190 my $cx = $corner % CACHESTRIDE;
191 my $cy = int $corner / CACHESTRIDE;
337 $pb->composite ($map_pb, 192 $TILE->composite ($map_pb,
338 $x * T, $y * T, 193 $x * T, $y * T,
339 T, T, 194 T, T,
340 ($x - $corner) * T, ($y - 1) * T, 1, 1, 195 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
341 "nearest", 196 "nearest",
342 255 197 255
343 ) if $corner; 198 ) if $corner;
344 } 199 }
345 } 200 }
363 ($map_pb, \%meta) 218 ($map_pb, \%meta)
364} 219}
365 220
366for my $file (@ARGV) { 221for my $file (@ARGV) {
367 my $mapa = read_arch $file; 222 my $mapa = read_arch $file;
223 $file =~ s/\.map$//;
368 my ($pb, $meta) = cfmap_render $mapa, $file; 224 my ($pb, $meta) = cfmap_render $mapa, $file;
369 $pb->save ("$file.png~", "png"); 225 $pb->save ("$file.png~~", "png");
370 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";
371 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 227 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
372 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png"; 228 system "optipng", "-q", "-out", "$file.png~", "$file.png~~";
229# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
373 unlink "$file.png~"; 230 unlink "$file.png~~";
374 Storable::nstore $meta, "$file.pst"; 231 utime +(stat $file)[8,9], "$file.jpg";
232 utime +(stat $file)[8,9], "$file.png~";
233 rename "$file.png~", "$file.png";
375} 234}
376 235
377 236
378 237
379 238

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines