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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.9 by root, Sun Nov 20 01:05:34 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.01'; 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
80 \%smooth
81 }
82}
83
84sub read_arch($;$) {
85 my ($path, $cache) = @_;
86
87 eval {
88 $cache
89 && -M "$path.pst" < -M $path
90 && Storable::retrieve "$path.pst"
91 } or do {
92 my %arc;
93 my ($more, $prev);
94
95 open my $fh, "<:raw", $path
96 or die "$path: $!";
97
98 my $parse_block; $parse_block = sub {
99 my %arc = @_;
100
101 while (<$fh>) {
102 s/\s+$//;
103 if (/^end$/i) {
104 last;
105 } elsif (/^arch (\S+)$/) {
106 push @{ $arc{inventory} }, $parse_block->(_name => $1);
107 } elsif (/^lore$/) {
108 while (<$fh>) {
109 last if /^endlore\s*$/i;
110 $arc{lore} .= $_;
111 }
112 } elsif (/^msg$/) {
113 while (<$fh>) {
114 last if /^endmsg\s*$/i;
115 $arc{msg} .= $_;
116 }
117 } elsif (/^(\S+)\s*(.*)$/) {
118 $arc{lc $1} = $2;
119 } elsif (/^\s*($|#)/) {
120 #
121 } else {
122 warn "$path: unparsable line '$_' in arch $arc{_name}";
123 }
124 }
125
126 \%arc
127 };
128
129 while (<$fh>) {
130 s/\s+$//;
131 if (/^more$/i) {
132 $more = $prev;
133 } elsif (/^object (\S+)$/i) {
134 my $name = $1;
135 my $arc = $parse_block->(_name => $name);
136
137 if ($more) {
138 $more->{more} = $arc;
139 } else {
140 $arc{$name} = $arc;
141 }
142
143 $prev = $arc;
144 $more = undef;
145 } elsif (/^arch (\S+)$/i) {
146 push @{ $arc{arch} }, $parse_block->(_name => $1);
147 } elsif (/^\s*($|#)/) {
148 #
149 } else {
150 warn "$path: unparseable top-level line '$_'";
151 }
152 }
153
154 undef $parse_block; # work around bug in perl not freeing $fh etc.
155
156 Storable::nstore \%arc, "$path.pst"
157 if $cache;
158
159 \%arc
160 }
161}
162
163my $arch = read_arch "$LIB/archetypes", 1;
164my $tile = read_pak "$LIB/crossfire.0";
165my $smooth = read_smooth "$LIB/smooth";
166
167sub tile($) {
168 my $name = $_[0];
169
170 my $pb = $tile->{$name}
171 or return;
172
173 unless (ref $pb) {
174 my $file = "/tmp/map2png.$$~";
175
176 open my $fh, ">:raw", $file
177 or die "$file: $!";
178 print $fh $pb;
179 close $fh;
180
181 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
182 unlink $file;
183 }
184
185 $pb
186}
187 46
188sub cfmap_render($;$) { 47sub cfmap_render($;$) {
189 my ($mapa, $mapname) = @_; 48 my ($mapa, $mapname) = @_;
190 49
191 my %meta; 50 my %meta;
192 51
193 my ($mapx, $mapy); 52 my ($mapx, $mapy);
194 53
195 my $map = $meta{map} = []; 54 my $map;
196 55
197 for (@{ $mapa->{arch} }) { 56 for (@{ $mapa->{arch} }) {
57 my ($x, $y) = ($_->{x}, $_->{y});
58
198 if ($_->{_name} eq "map") { 59 if ($_->{_name} eq "map") {
199 $meta{info} = $_; 60 $meta{info} = $_;
200 61
201 $mapx = $_->{width} || $_->{x}; 62 $mapx = $_->{width} || $x;
202 $mapy = $_->{height} || $_->{y}; 63 $mapy = $_->{height} || $y;
203 } else { 64 } else {
204 push @{ $map->[$_->{x}][$_->{y}] }, $_; 65 push @{ $map->[$x][$y] }, $_;
205 66
206 # arch map is unreliable w.r.t. width and height 67 # arch map is unreliable w.r.t. width and height
207 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 68 $mapx = $x + 1 if $mapx <= $x;
208 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 69 $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}; 70 #$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}; 71 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
211 } 72 }
212 } 73 }
213 74
214 $meta{width} = $mapx; 75 $meta{width} = $mapx;
215 $meta{height} = $mapy; 76 $meta{height} = $mapy;
216 77
217 my %draw_info; 78 my %draw_info;
79 my %map_info;
218 80
219 # first pass, gather face stacking order, border and corner info 81 # first pass, gather face stacking order, border and corner info
220 for my $x (0 .. $mapx - 1) { 82 for my $x (0 .. $mapx - 1) {
221 my $col = $map->[$x]; 83 my $col = $map->[$x];
222 for my $y (0 .. $mapy - 1) { 84 for my $y (0 .. $mapy - 1) {
223 my $as = $col->[$y] || []; 85 my $as = $col->[$y] || [];
224 86
225 for my $layer (0 .. $#$as) { 87 for my $layer (0 .. $#$as) {
226 my $a = $as->[$layer]; 88 my $a = $as->[$layer];
227 89
228 my $o = $arch->{$a->{_name}} 90 my $o = $ARCH{$a->{_name}}
229 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;
230 92
231 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel}; 93 my $level = $layer * 256;
232 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
233 my $level = $smoothlevel ? $smoothlevel
234 : $is_floor ? 0
235 : 256 + $layer;
236 94
237 while ($o) { 95 while ($o) {
238 my $face = $a->{face} || $o->{face}; 96 my $face = $a->{face} || $o->{face};
239 97
240 my $pb = tile $face 98 $FACE{$face}
241 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};
242 102
243 my $mx = $x + $o->{x}; 103 my $mx = $x + $o->{x};
244 my $my = $y + $o->{y}; 104 my $my = $y + $o->{y};
245 105
246 last if 0 > $mx || $mx >= $mapx 106 last if 0 > $mx || $mx >= $mapx
247 || 0 > $my || $my >= $mapy; 107 || 0 > $my || $my >= $mapy;
248 108
249 # this is very ugly (some tiles are 32x33 or worse) 109 push @{ $map_info{$level}{$mx, $my} }, $a;
250 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
251 110
252 if (my $sface = $smooth->{$face}) { 111 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
253 $bigface and die "can't handle bigfaces with smoothing ($face)\n"; 112
113 if (0) { # disable smoothing for the time being
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};
254 119
255 # full tile 120 # full tile
256 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 121 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
257 122
258 # borders 123 # borders
259 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; 124 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
260 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; 125 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
261 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; 126 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
262 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; 127 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
263 128
264 # corners 129 # corners
265 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; 130 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
266 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 131 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
267 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 132 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
268 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 133 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
269 } 134 }
270 135 }
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 136
276 $o = $o->{more}; 137 $o = $o->{more};
277 } 138 }
278 } 139 }
279 } 140 }
280 } 141 }
281 142
282 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
283 or die; 144 or die;
284 $map_pb->fill (0xffffff00); 145 $map_pb->fill (0xffffff00);
285 146
286 # second pass, render all the stuff 147 # second pass, render the map
287 for my $level (sort { $a <=> $b } keys %draw_info) { 148 for my $level (sort { $a <=> $b } keys %draw_info) {
288 my $v = $draw_info{$level}; 149 my $v = $draw_info{$level};
289 while (my ($sface, $info) = each %$v) { 150 while (my ($face, $info) = each %$v) {
290 my $pb = tile $sface
291 or die "no smooth face $sface\n";
292
293 while (my ($xy, $bits) = each %$info) { 151 while (my ($xy, $bits) = each %$info) {
294 my ($x, $y) = split $;, $xy; 152 my ($x, $y) = split $;, $xy;
295 153
296 next if $x < 0 || $x >= $mapx 154 next if $x < 0 || $x >= $mapx
297 || $y < 0 || $y >= $mapy; 155 || $y < 0 || $y >= $mapy;
298 156
299 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 157 # bits is __fn cccc CCCC bbbb
300 # X don't draw
301 # F full tile draw with x|y bigface displacement 158 # f full tile draw with x|y bigface displacement
302 # c maybe draw these corners
303 # C do not draw these corners 159 # n do not draw borders&corners
160 # c draw these corners, but...
161 # C ... not these
304 # b draw these borders 162 # b draw these borders
305 163
306 if ($bits & 0x2000) { 164 if ($bits & 0x2000) {
307 my $dx = (($bits >> 24) & 0xff) - 128; 165 my $ix = $face % CACHESTRIDE;
308 my $dy = (($bits >> 16) & 0xff) - 128; 166 my $iy = int $face / CACHESTRIDE;
309 167
310 $pb->composite ($map_pb, 168 $TILE->composite ($map_pb,
311 $x * T, $y * T, 169 $x * T, $y * T,
312 T, T, 170 T, T,
313 ($x - $dx) * T, ($y - $dy) * T, 1, 1, 171 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
314 "nearest", 172 "nearest",
315 255 173 255
316 ); 174 );
317 } 175 }
318 176
319 unless ($bits & 0x1000) { 177 unless ($bits & 0x1000) {
320 my $border = $bits & 0xf; 178 my $border = $face + ($bits & 0xf);
321 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; 179 my $bx = $border % CACHESTRIDE;
322 180 my $by = int $border / CACHESTRIDE;
323 $pb->composite ($map_pb, 181 $TILE->composite ($map_pb,
324 $x * T, $y * T, 182 $x * T, $y * T,
325 T, T, 183 T, T,
326 ($x - $border) * T, $y * T, 1, 1, 184 ($x - $bx) * T, ($y - $by) * T, 1, 1,
327 "nearest", 185 "nearest",
328 255 186 255
329 ) if $border; 187 ) if $border;
330 188
189 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
190 my $cx = $corner % CACHESTRIDE;
191 my $cy = int $corner / CACHESTRIDE;
331 $pb->composite ($map_pb, 192 $TILE->composite ($map_pb,
332 $x * T, $y * T, 193 $x * T, $y * T,
333 T, T, 194 T, T,
334 ($x - $corner) * T, ($y - 1) * T, 1, 1, 195 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
335 "nearest", 196 "nearest",
336 255 197 255
337 ) if $corner; 198 ) if $corner;
338 } 199 }
339 } 200 }
340 } 201 }
341 } 202 }
342 203
204 # third pass, gather meta info
205 for my $level (sort { $a <=> $b } keys %map_info) {
206 my $info = $map_info{$level};
207
208 while (my ($xy, $as) = each %$info) {
209 my ($x, $y) = split $;, $xy;
210
211 next if $x < 0 || $x >= $mapx
212 || $y < 0 || $y >= $mapy;
213
214 push @{ $meta{map}[$x][$y] }, $_ for @$as;
215 }
216 }
217
343 ($map_pb, \%meta) 218 ($map_pb, \%meta)
344} 219}
345 220
346for my $file (@ARGV) { 221for my $file (@ARGV) {
347 my $mapa = read_arch $file; 222 my $mapa = read_arch $file;
223 $file =~ s/\.map$//;
348 my ($pb, $meta) = cfmap_render $mapa, $file; 224 my ($pb, $meta) = cfmap_render $mapa, $file;
349 $pb->save ("$file.png~", "png"); 225 $pb->save ("$file.png~~", "png");
350 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";
351 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency 227 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
352 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";
353 unlink "$file.png~"; 230 unlink "$file.png~~";
354 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";
355} 234}
356 235
357 236
358 237
359 238

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines