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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines