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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.4 by root, Fri Nov 18 12:04:12 2005 UTC vs.
Revision 1.27 by root, Thu Oct 22 03:02:00 2009 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# tower of stars: missing craters? 3# cfmap2png - convert deliantra maps to png+metadata
4# Copyright (C) 2005,2007,2008,2009 Marc Lehmann <cfmaps@schmorp.de>
5#
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
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with gvpe; if not, write to the Free Software
18# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
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 = '2.012';
4 34
5use strict; 35use strict;
6 36
7use Storable; 37use Deliantra;
8use List::Util qw(max); 38use List::Util qw(max);
9 39
10use Gtk2; 40use Gtk2;
11 41
12our $VERSION = '1.0';
13
14#init Gtk2::Gdk;
15
16my $LIB = $ENV{CROSSFIRE_LIBDIR}
17 or die "\$CROSSFIRE_LIBDIR must be set\n";
18
19sub T (){ 32 } 42sub T (){ 32 }
20 43
21sub read_pak($) { 44Deliantra::load_archetypes;
22 my ($path) = @_; 45Deliantra::load_tilecache;
23
24 eval {
25 -M "$path.pst" < -M $path
26 && Storable::retrieve "$path.pst"
27 } or do {
28 my %pak;
29
30 open my $fh, "<:raw", $path
31 or die "$_[0]: $!";
32 while (<$fh>) {
33 my ($type, $id, $len, $path) = split;
34 $path =~ s/.*\///;
35 read $fh, $pak{$path}, $len;
36 }
37
38 Storable::nstore \%pak, "$path.pst";
39
40 \%pak
41 }
42}
43
44sub read_smooth($) {
45 my ($path) = @_;
46
47 eval {
48 -M "$path.pst" < -M $path
49 && Storable::retrieve "$path.pst"
50 } or do {
51 my %smooth;
52
53 open my $fh, "<:raw", $path
54 or die "$path: $!";
55 while (<$fh>) {
56 next if /^\s*($|#)/;
57
58 $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
59 }
60
61 Storable::nstore \%smooth, "$path.pst";
62
63 \%smooth
64 }
65}
66
67sub read_arch($;$) {
68 my ($path, $cache) = @_;
69
70 eval {
71 $cache
72 && -M "$path.pst" < -M $path
73 && Storable::retrieve "$path.pst"
74 } or do {
75 my %arc;
76 my ($more, $prev);
77
78 open my $fh, "<:raw", $path
79 or die "$path: $!";
80
81 my $parse_block; $parse_block = sub {
82 my %arc = @_;
83
84 while (<$fh>) {
85 s/\s+$//;
86 if (/^end$/i) {
87 last;
88 } elsif (/^arch (\S+)$/) {
89 push @{ $arc{inventory} }, $parse_block->(_name => $1);
90 } elsif (/^lore$/) {
91 while (<$fh>) {
92 last if /^endlore\s*$/i;
93 $arc{lore} .= $_;
94 }
95 } elsif (/^msg$/) {
96 while (<$fh>) {
97 last if /^endmsg\s*$/i;
98 $arc{msg} .= $_;
99 }
100 } elsif (/^(\S+)\s*(.*)$/) {
101 $arc{lc $1} = $2;
102 } elsif (/^\s*($|#)/) {
103 #
104 } else {
105 warn "$path: unparsable line '$_' in arch $arc{_name}";
106 }
107 }
108
109 \%arc
110 };
111
112 while (<$fh>) {
113 s/\s+$//;
114 if (/^more$/i) {
115 $more = $prev;
116 } elsif (/^object (\S+)$/i) {
117 my $name = $1;
118 my $arc = $parse_block->(_name => $name);
119
120 if ($more) {
121 $more->{more} = $arc;
122 } else {
123 $arc{$name} = $arc;
124 }
125
126 $prev = $arc;
127 $more = undef;
128 } elsif (/^arch (\S+)$/i) {
129 push @{ $arc{arch} }, $parse_block->(_name => $1);
130 } elsif (/^\s*($|#)/) {
131 #
132 } else {
133 warn "$path: unparseable top-level line '$_'";
134 }
135 }
136
137 undef $parse_block; # work around bug in perl not freeing $fh etc.
138
139 Storable::nstore \%arc, "$path.pst"
140 if $cache;
141
142 \%arc
143 }
144}
145
146my $arch = read_arch "$LIB/archetypes", 1;
147my $tile = read_pak "$LIB/crossfire.0";
148my $smooth = read_smooth "$LIB/smooth";
149
150sub tile($) {
151 my $name = $_[0];
152
153 my $pb = $tile->{$name}
154 or return;
155
156 unless (ref $pb) {
157 my $file = "/tmp/map2png.$$~";
158
159 open my $fh, ">:raw", $file
160 or die "$file: $!";
161 print $fh $pb;
162 close $fh;
163
164 $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
165 unlink $file;
166 }
167
168 $pb
169}
170 46
171sub cfmap_render($;$) { 47sub cfmap_render($;$) {
172 my ($mapa, $mapname) = @_; 48 my ($mapa, $mapname) = @_;
173 49
174 my %meta; 50 my %meta;
175 51
176 my ($mapx, $mapy); 52 my ($mapx, $mapy);
177 53
178 my $map = $meta{map} = []; 54 my $map;
179 55
180 for (@{ $mapa->{arch} }) { 56 for (@{ $mapa->{arch} }) {
57 my ($x, $y) = ($_->{x}, $_->{y});
58
181 if ($_->{_name} eq "map") { 59 if ($_->{_name} eq "map") {
182 $meta{info} = $_; 60 $meta{info} = $_;
183 61
184 $mapx = $_->{width} || $_->{x}; 62 $mapx = $_->{width} || $x;
185 $mapy = $_->{height} || $_->{y}; 63 $mapy = $_->{height} || $y;
186 } else { 64 } else {
187 push @{ $map->[$_->{x}][$_->{y}] }, $_; 65 push @{ $map->[$x][$y] }, $_;
188 66
189 # arch map is unreliable w.r.t. width and height 67 # arch map is unreliable w.r.t. width and height
190 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 68 $mapx = $x + 1 if $mapx <= $x;
191 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 69 $mapy = $y + 1 if $mapy <= $y;
192 #$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};
193 #$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};
194 } 72 }
195 } 73 }
196 74
197 $meta{width} = $mapx; 75 $meta{width} = $mapx;
198 $meta{height} = $mapy; 76 $meta{height} = $mapy;
199 77
200 my %map_face;
201 my %draw_info; 78 my %draw_info;
79 my %map_info;
202 80
203 # first pass, gather face stacking order, border and corner info 81 # first pass, gather face stacking order, border and corner info
204 for my $x (0 .. $mapx - 1) { 82 for my $x (0 .. $mapx - 1) {
205 my $col = $map->[$x]; 83 my $col = $map->[$x];
206 for my $y (0 .. $mapy - 1) { 84 for my $y (0 .. $mapy - 1) {
207 my $as = $col->[$y] || []; 85 my $as = $col->[$y] || [];
208 86
209 for my $layer (0 .. $#$as) { 87 for my $layer (0 .. $#$as) {
210 my $a = $as->[$layer]; 88 my $a = $as->[$layer];
211 89
212 my $o = $arch->{$a->{_name}} 90 my $o = $ARCH{$a->{_name}}
213 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;
214 92
215 my $A = { %$o, %$a }; 93 my $level = $layer * 256;
216
217 my $smoothlevel = $A->{smoothlevel};
218 my $level = $smoothlevel ? $smoothlevel
219 : $A->{is_floor} ? 0
220 : 256 + $layer;
221 94
222 while ($o) { 95 while ($o) {
223 my $pb = tile $A->{face} 96 my $face = $a->{face} || $o->{face};
97
98 $FACE{$face}
99 or $face =~ s/\.1(\d\d)$/.x$1/;
100
101 $FACE{$face}
224 or (warn "$mapname: face '$A->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; 102 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
103
104 my $idx = $FACE{$face}{idx};
225 105
226 my $mx = $x + $o->{x}; 106 my $mx = $x + $o->{x};
227 my $my = $y + $o->{y}; 107 my $my = $y + $o->{y};
228 108
229 last if 0 > $mx || $mx >= $mapx 109 last if 0 > $mx || $mx >= $mapx
230 || 0 > $my || $my >= $mapy; 110 || 0 > $my || $my >= $mapy;
231 111
232 # this is very ugly (some tiles are 32x33 or worse) 112 push @{ $map_info{$level}{$mx, $my} }, $a;
233 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
234 113
235 if (my $sface = $smooth->{$A->{face}}) { 114 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
236 $bigface and die "can't handle bigfaces with smoothing ($A->{face})\n"; 115
116 if (0) { # disable smoothing for the time being
117 if (my $sface = $FACEDATA{faceinfo}{$face}{smooth}) {
118 $FACE{$sface}
119 or (warn "$mapname: smoothface '$sface' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
120 my $idx = $FACE{$sface}{idx};
121 my $level = $layer * 256 + $FACEDATA{faceinfo}{$face}{smoothlevel};
237 122
238 # full tile 123 # full tile
239 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 124 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
240 125
241 # borders 126 # borders
242 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031; 127 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
243 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092; 128 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
244 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064; 129 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
245 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8; 130 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
246 131
247 # corners 132 # corners
248 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100; 133 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
249 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 134 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
250 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 135 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
251 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 136 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
252 } 137 }
253 138 }
254 my $dx = $bigface ? $o->{x} : 0;
255 my $dy = $bigface ? $o->{y} : 0;
256
257 $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
258 139
259 $o = $o->{more}; 140 $o = $o->{more};
260 } 141 }
261 } 142 }
262 } 143 }
263 } 144 }
264 145
265 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 146 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
266 or die; 147 or die;
267 $map_pb->fill (0x00000000); 148 $map_pb->fill (0xffffff00);
268 149
269 # second pass, render all the stuff 150 # second pass, render the map
270 for my $level (sort { $a <=> $b } keys %draw_info) { 151 for my $level (sort { $a <=> $b } keys %draw_info) {
271 my $v = $draw_info{$level}; 152 my $v = $draw_info{$level};
272 while (my ($sface, $info) = each %$v) { 153 while (my ($face, $info) = each %$v) {
273 my $pb = tile $sface
274 or die "no smooth face $sface\n";
275
276 while (my ($xy, $bits) = each %$info) { 154 while (my ($xy, $bits) = each %$info) {
277 my ($x, $y) = split $;, $xy; 155 my ($x, $y) = split $;, $xy;
278 156
279 next if $x < 0 || $x >= $mapx 157 next if $x < 0 || $x >= $mapx
280 || $y < 0 || $y >= $mapy; 158 || $y < 0 || $y >= $mapy;
281 159
282 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 160 # bits is __fn cccc CCCC bbbb
283 # X don't draw
284 # F full tile draw with x|y bigface displacement 161 # f full tile draw with x|y bigface displacement
285 # c maybe draw these corners
286 # C do not draw these corners 162 # n do not draw borders&corners
163 # c draw these corners, but...
164 # C ... not these
287 # b draw these borders 165 # b draw these borders
288 166
289 if ($bits & 0x2000) { 167 if ($bits & 0x2000) {
290 my $dx = (($bits >> 24) & 0xff) - 128; 168 my $ix = $face % CACHESTRIDE;
291 my $dy = (($bits >> 16) & 0xff) - 128; 169 my $iy = int $face / CACHESTRIDE;
292 170
293 $pb->composite ($map_pb, 171 $TILE->composite ($map_pb,
294 $x * T, $y * T, 172 $x * T, $y * T,
295 T, T, 173 T, T,
296 ($x - $dx) * T, ($y - $dy) * T, 1, 1, 174 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
297 "nearest", 175 "nearest",
298 255 176 255
299 ); 177 );
300 } 178 }
301 179
302 unless ($bits & 0x1000) { 180 unless ($bits & 0x1000) {
303 my $border = $bits & 0xf; 181 my $border = $face + ($bits & 0xf);
304 my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf; 182 my $bx = $border % CACHESTRIDE;
305 183 my $by = int $border / CACHESTRIDE;
306 $pb->composite ($map_pb, 184 $TILE->composite ($map_pb,
307 $x * T, $y * T, 185 $x * T, $y * T,
308 T, T, 186 T, T,
309 ($x - $border) * T, $y * T, 1, 1, 187 ($x - $bx) * T, ($y - $by) * T, 1, 1,
310 "nearest", 188 "nearest",
311 255 189 255
312 ) if $border; 190 ) if $border;
313 191
192 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
193 my $cx = $corner % CACHESTRIDE;
194 my $cy = int $corner / CACHESTRIDE;
314 $pb->composite ($map_pb, 195 $TILE->composite ($map_pb,
315 $x * T, $y * T, 196 $x * T, $y * T,
316 T, T, 197 T, T,
317 ($x - $corner) * T, ($y - 1) * T, 1, 1, 198 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
318 "nearest", 199 "nearest",
319 255 200 255
320 ) if $corner; 201 ) if $corner;
321 } 202 }
322 } 203 }
323 } 204 }
324 } 205 }
325 206
207 # third pass, gather meta info
208 for my $level (sort { $a <=> $b } keys %map_info) {
209 my $info = $map_info{$level};
210
211 while (my ($xy, $as) = each %$info) {
212 my ($x, $y) = split $;, $xy;
213
214 next if $x < 0 || $x >= $mapx
215 || $y < 0 || $y >= $mapy;
216
217 push @{ $meta{map}[$x][$y] }, $_ for @$as;
218 }
219 }
220
326 ($map_pb, \%meta) 221 ($map_pb, \%meta)
327} 222}
328 223
329for my $file (@ARGV) { 224for my $file (@ARGV) {
225 my $stderr;
226 $SIG{__WARN__} = sub {
227 $stderr .= $_[0];
228 print STDERR $_[0];
229 };
230
231 $file =~ s/\.map$//;
232
233 eval {
330 my $mapa = read_arch $file; 234 my $mapa = read_arch "$file.map";
331 my ($pb, $meta) = cfmap_render $mapa, $file; 235 my ($pb, $meta) = cfmap_render $mapa, $file;
332 $pb->save ("$file.png~", "png"); 236 $pb->save ("$file.png~~", "png");
333 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; 237 system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
334 system "mogrify", "-colors" => 65536, "$file.png~"; 238 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
335 system "pngcrush", "-q", "-m" => 7, "-rem" => "alla", "-reduce", "$file.png~", "$file.png"; 239 system "optipng", "-q", "-out", "$file.png~", "$file.png~~";
240# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
336 unlink "$file.png~"; 241 unlink "$file.png~~";
337 Storable::nstore $meta, "$file.pst"; 242 utime +(stat "$file.map")[8,9], "$file.jpg" or die "$file.jpg: $!";
243 utime +(stat "$file.map")[8,9], "$file.png~" or die "$file.png~: $!";
244 rename "$file.png~", "$file.png";
245 1
246 } or warn $@;
247
248 if (length $stderr) {
249 open my $fh, ">$file.png.err~"
250 or die "$file.png.err~: $!";
251 syswrite $fh, $stderr;
252 close $fh;
253 rename "$file.png.err~", "$file.png.err";
254 } else {
255 unlink "$file.png.err";
256 }
338} 257}
339 258
340 259
341 260
342 261

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines