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

Comparing cfmaps/cfmap2png (file contents):
Revision 1.1 by root, Thu Nov 17 11:51:08 2005 UTC vs.
Revision 1.15 by root, Wed Nov 30 08:05:59 2005 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# cfarch2png - convert crossfire maps to png+metadata
4# whaling... icecaves... water is red? 4# Copyright (C) 2005 Marc Lehmann <gvpe@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
5# tower of stars: missing craters? 20# tower of stars: missing craters?
6# http://cfmaps.schmorp.de/pup_land/raffle/raffle3_u2.html <-> yellow "doors" show as grey stone as surrounding 21# world_108_123 (8|18), hole below grass but shouldn't?
22
23our $VERSION = '1.11';
7 24
8use strict; 25use strict;
9 26
10use Storable; 27use Storable;
11use List::Util qw(max); 28use List::Util qw(max);
85 while (<$fh>) { 102 while (<$fh>) {
86 s/\s+$//; 103 s/\s+$//;
87 if (/^end$/i) { 104 if (/^end$/i) {
88 last; 105 last;
89 } elsif (/^arch (\S+)$/) { 106 } elsif (/^arch (\S+)$/) {
90 push @{ $arc{subarch} }, $parse_block->(_name => $1); 107 push @{ $arc{inventory} }, $parse_block->(_name => $1);
108 } elsif (/^lore$/) {
109 while (<$fh>) {
110 last if /^endlore\s*$/i;
111 $arc{lore} .= $_;
112 }
91 } elsif (/^msg$/) { 113 } elsif (/^msg$/) {
92 while (<$fh>) { 114 while (<$fh>) {
93 last if /^endmsg\s*$/i; 115 last if /^endmsg\s*$/i;
94 $arc{msg} .= $_; 116 $arc{msg} .= $_;
95 } 117 }
111 $more = $prev; 133 $more = $prev;
112 } elsif (/^object (\S+)$/i) { 134 } elsif (/^object (\S+)$/i) {
113 my $name = $1; 135 my $name = $1;
114 my $arc = $parse_block->(_name => $name); 136 my $arc = $parse_block->(_name => $name);
115 137
138 if ($more) {
139 $more->{more} = $arc;
140 } else {
116 $arc{$name} = $arc; 141 $arc{$name} = $arc;
117 $more->{more} = $arc if $more; 142 }
118 143
119 $prev = $arc; 144 $prev = $arc;
120 $more = undef; 145 $more = undef;
121 } elsif (/^arch (\S+)$/i) { 146 } elsif (/^arch (\S+)$/i) {
122 push @{ $arc{arch} }, $parse_block->(_name => $1); 147 push @{ $arc{arch} }, $parse_block->(_name => $1);
166 191
167 my %meta; 192 my %meta;
168 193
169 my ($mapx, $mapy); 194 my ($mapx, $mapy);
170 195
171 my $map = $meta{map} = []; 196 my $map;
172 197
173 for (@{ $mapa->{arch} }) { 198 for (@{ $mapa->{arch} }) {
199 my ($x, $y) = ($_->{x}, $_->{y});
200
174 if ($_->{_name} eq "map") { 201 if ($_->{_name} eq "map") {
175 $meta{info} = $_; 202 $meta{info} = $_;
176 203
177 $mapx = $_->{width} || $_->{x}; 204 $mapx = $_->{width} || $x;
178 $mapy = $_->{height} || $_->{y}; 205 $mapy = $_->{height} || $y;
179 } else { 206 } else {
180 push @{ $map->[$_->{x}][$_->{y}] }, $_; 207 push @{ $map->[$x][$y] }, $_;
181 208
182 # arch map is unreliable w.r.t. width and height 209 # arch map is unreliable w.r.t. width and height
183 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 210 $mapx = $x + 1 if $mapx <= $x;
184 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 211 $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}; 212 #$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}; 213 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
187 } 214 }
188 } 215 }
189 216
190 $meta{width} = $mapx; 217 $meta{width} = $mapx;
191 $meta{height} = $mapy; 218 $meta{height} = $mapy;
192 219
193 my %map_face;
194 my %draw_info; 220 my %draw_info;
195 221 my %map_info;
196 my $OBJ_LEVEL = 2**32; # higher than any valid (or in-use) smooth_level
197 222
198 # first pass, gather face stacking order, border and corner info 223 # first pass, gather face stacking order, border and corner info
199 for my $x (0 .. $mapx - 1) { 224 for my $x (0 .. $mapx - 1) {
200 my $col = $map->[$x]; 225 my $col = $map->[$x];
201 for my $y (0 .. $mapy - 1) { 226 for my $y (0 .. $mapy - 1) {
205 my $a = $as->[$layer]; 230 my $a = $as->[$layer];
206 231
207 my $o = $arch->{$a->{_name}} 232 my $o = $arch->{$a->{_name}}
208 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 233 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
209 234
210 $o = $arch->{$o->{other_arch}} while $o->{other_arch} && !$o->{face}; 235 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
211 236 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
212 $o or die "arch $a->{_name} undefined at ($x|$y)\n";
213
214 my $smooth_level = $o->{smoothlevel};
215 my $level = $smooth_level ? $smooth_level 237 my $level = $smoothlevel ? $smoothlevel
216 : $o->{is_floor} ? 0 238 : $is_floor ? $layer - 1000
217 : $OBJ_LEVEL + $layer; 239 : $layer + 1000;
218 240
219 while ($o) { 241 while ($o) {
242 my $face = $a->{face} || $o->{face};
243
220 my $pb = tile $o->{face} 244 my $pb = tile $face
221 or (warn "$mapname: face '$o->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; 245 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
222 246
223 my $mx = $x + $o->{x}; 247 my $mx = $x + $o->{x};
224 my $my = $y + $o->{y}; 248 my $my = $y + $o->{y};
225 249
226 last if $mx >= $mapx 250 last if 0 > $mx || $mx >= $mapx
227 || $my >= $mapy; 251 || 0 > $my || $my >= $mapy;
228 252
229 # this is very ugly (some tiles are 32x33 or worse) 253 # this is very ugly (some tiles are 32x33 or worse)
230 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; 254 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
231 255
232 if (my $sface = $smooth->{$o->{face}}) {
233 $bigface and die "can't handle big faces with smoothing ($o->{face})\n";
234
235 # full tile
236 $draw_info{$smooth_level}{$sface}{$mx , $my } |= 0x1000;
237
238 # borders
239 $draw_info{$smooth_level}{$sface}{$mx + 1, $my } |= 0x0031;
240 $draw_info{$smooth_level}{$sface}{$mx , $my + 1} |= 0x0092;
241 $draw_info{$smooth_level}{$sface}{$mx - 1, $my } |= 0x0064;
242 $draw_info{$smooth_level}{$sface}{$mx , $my - 1} |= 0x00c8;
243
244 # corners
245 $draw_info{$smooth_level}{$sface}{$mx + 1, $my + 1} |= 0x0100;
246 $draw_info{$smooth_level}{$sface}{$mx - 1, $my + 1} |= 0x0200;
247 $draw_info{$smooth_level}{$sface}{$mx - 1, $my - 1} |= 0x0400;
248 $draw_info{$smooth_level}{$sface}{$mx + 1, $my - 1} |= 0x0800;
249 } else {
250 $smooth_level = 0;
251 }
252
253 my $dx = $bigface ? $o->{x} : 0; 256 my $dx = $bigface ? $o->{x} : 0;
254 my $dy = $bigface ? $o->{y} : 0; 257 my $dy = $bigface ? $o->{y} : 0;
255 258
259 push @{ $map_info{$level}{$mx, $my} }, $a;
260
256 $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 261 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
262
263 if (my $sface = $smooth->{$face}) {
264 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
265
266 # full tile
267 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
268
269 # borders
270 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031;
271 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
272 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064;
273 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8;
274
275 # corners
276 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100;
277 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
278 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
279 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
280 }
257 281
258 $o = $o->{more}; 282 $o = $o->{more};
259 } 283 }
260 } 284 }
261 } 285 }
262 } 286 }
263 287
264 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 288 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
265 or die; 289 or die;
266 $map_pb->fill (0x00000000); 290 $map_pb->fill (0xffffff00);
267 291
268 # second pass, render all the stuff 292 # second pass, render the map
269 for my $level (sort { $a <=> $b } keys %draw_info) { 293 for my $level (sort { $a <=> $b } keys %draw_info) {
270 my $v = $draw_info{$level}; 294 my $v = $draw_info{$level};
271 while (my ($sface, $info) = each %$v) { 295 while (my ($sface, $info) = each %$v) {
272 my $pb = tile $sface 296 my $pb = tile $sface
273 or die "no smooth face $sface\n"; 297 or die "no smooth face $sface\n";
276 my ($x, $y) = split $;, $xy; 300 my ($x, $y) = split $;, $xy;
277 301
278 next if $x < 0 || $x >= $mapx 302 next if $x < 0 || $x >= $mapx
279 || $y < 0 || $y >= $mapy; 303 || $y < 0 || $y >= $mapy;
280 304
281 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 305 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
282 # X don't draw
283 # F full tile draw with x|y bigface displacement 306 # f full tile draw with x|y bigface displacement
284 # c maybe draw these corners
285 # C do not draw these corners 307 # n do not draw borders&corners
308 # c draw these corners, but...
309 # C ... not these
286 # b draw these borders 310 # b draw these borders
287 311
288 if ($bits & 0x2000) { 312 if ($bits & 0x2000) {
289 my $dx = (($bits >> 24) & 0xff) - 128; 313 my $dx = (($bits >> 24) & 0xff) - 128;
290 my $dy = (($bits >> 16) & 0xff) - 128; 314 my $dy = (($bits >> 16) & 0xff) - 128;
320 } 344 }
321 } 345 }
322 } 346 }
323 } 347 }
324 348
349 # third pass, gather meta info
350 for my $level (sort { $a <=> $b } keys %map_info) {
351 my $info = $map_info{$level};
352
353 while (my ($xy, $as) = each %$info) {
354 my ($x, $y) = split $;, $xy;
355
356 next if $x < 0 || $x >= $mapx
357 || $y < 0 || $y >= $mapy;
358
359 push @{ $meta{map}[$x][$y] }, $_ for @$as;
360 }
361 }
362
325 ($map_pb, \%meta) 363 ($map_pb, \%meta)
326} 364}
327 365
328for my $file (@ARGV) { 366for my $file (@ARGV) {
329 my $mapa = read_arch $file; 367 my $mapa = read_arch $file;
330 my ($pb, $meta) = cfmap_render $mapa, $file; 368 my ($pb, $meta) = cfmap_render $mapa, $file;
331 $pb->save ("$file.png", "png"); 369 $pb->save ("$file.png~", "png");
370 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
371 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
372 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png";
373# system "pngnq <\Q$file.png~\E >\Q$file.png\E";
374 unlink "$file.png~";
332 Storable::nstore $meta, "$file.pst"; 375 Storable::nstore $meta, "$file.pst";
333} 376}
334 377
335 378
336 379

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines