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.8 by root, Sun Nov 20 00:58:57 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
7 21
8use strict; 22use strict;
9 23
10use Storable; 24use Storable;
11use List::Util qw(max); 25use List::Util qw(max);
12 26
13use Gtk2; 27use Gtk2;
28
29our $VERSION = '1.0';
14 30
15#init Gtk2::Gdk; 31#init Gtk2::Gdk;
16 32
17my $LIB = $ENV{CROSSFIRE_LIBDIR} 33my $LIB = $ENV{CROSSFIRE_LIBDIR}
18 or die "\$CROSSFIRE_LIBDIR must be set\n"; 34 or die "\$CROSSFIRE_LIBDIR must be set\n";
85 while (<$fh>) { 101 while (<$fh>) {
86 s/\s+$//; 102 s/\s+$//;
87 if (/^end$/i) { 103 if (/^end$/i) {
88 last; 104 last;
89 } elsif (/^arch (\S+)$/) { 105 } elsif (/^arch (\S+)$/) {
90 push @{ $arc{subarch} }, $parse_block->(_name => $1); 106 push @{ $arc{inventory} }, $parse_block->(_name => $1);
107 } elsif (/^lore$/) {
108 while (<$fh>) {
109 last if /^endlore\s*$/i;
110 $arc{lore} .= $_;
111 }
91 } elsif (/^msg$/) { 112 } elsif (/^msg$/) {
92 while (<$fh>) { 113 while (<$fh>) {
93 last if /^endmsg\s*$/i; 114 last if /^endmsg\s*$/i;
94 $arc{msg} .= $_; 115 $arc{msg} .= $_;
95 } 116 }
111 $more = $prev; 132 $more = $prev;
112 } elsif (/^object (\S+)$/i) { 133 } elsif (/^object (\S+)$/i) {
113 my $name = $1; 134 my $name = $1;
114 my $arc = $parse_block->(_name => $name); 135 my $arc = $parse_block->(_name => $name);
115 136
137 if ($more) {
138 $more->{more} = $arc;
139 } else {
116 $arc{$name} = $arc; 140 $arc{$name} = $arc;
117 $more->{more} = $arc if $more; 141 }
118 142
119 $prev = $arc; 143 $prev = $arc;
120 $more = undef; 144 $more = undef;
121 } elsif (/^arch (\S+)$/i) { 145 } elsif (/^arch (\S+)$/i) {
122 push @{ $arc{arch} }, $parse_block->(_name => $1); 146 push @{ $arc{arch} }, $parse_block->(_name => $1);
188 } 212 }
189 213
190 $meta{width} = $mapx; 214 $meta{width} = $mapx;
191 $meta{height} = $mapy; 215 $meta{height} = $mapy;
192 216
193 my %map_face;
194 my %draw_info; 217 my %draw_info;
195
196 my $OBJ_LEVEL = 2**32; # higher than any valid (or in-use) smooth_level
197 218
198 # first pass, gather face stacking order, border and corner info 219 # first pass, gather face stacking order, border and corner info
199 for my $x (0 .. $mapx - 1) { 220 for my $x (0 .. $mapx - 1) {
200 my $col = $map->[$x]; 221 my $col = $map->[$x];
201 for my $y (0 .. $mapy - 1) { 222 for my $y (0 .. $mapy - 1) {
205 my $a = $as->[$layer]; 226 my $a = $as->[$layer];
206 227
207 my $o = $arch->{$a->{_name}} 228 my $o = $arch->{$a->{_name}}
208 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; 229 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
209 230
210 $o = $arch->{$o->{other_arch}} while $o->{other_arch} && !$o->{face}; 231 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
211 232 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 233 my $level = $smoothlevel ? $smoothlevel
216 : $o->{is_floor} ? 0 234 : $is_floor ? 0
217 : $OBJ_LEVEL + $layer; 235 : 256 + $layer;
218 236
219 while ($o) { 237 while ($o) {
238 my $face = $a->{face} || $o->{face};
239
220 my $pb = tile $o->{face} 240 my $pb = tile $face
221 or (warn "$mapname: face '$o->{face}' not found for arch '$a->{_name}' at ($x|$y)\n"), last; 241 or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
222 242
223 my $mx = $x + $o->{x}; 243 my $mx = $x + $o->{x};
224 my $my = $y + $o->{y}; 244 my $my = $y + $o->{y};
225 245
226 last if $mx >= $mapx 246 last if 0 > $mx || $mx >= $mapx
227 || $my >= $mapy; 247 || 0 > $my || $my >= $mapy;
228 248
229 # this is very ugly (some tiles are 32x33 or worse) 249 # this is very ugly (some tiles are 32x33 or worse)
230 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; 250 my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
231 251
232 if (my $sface = $smooth->{$o->{face}}) { 252 if (my $sface = $smooth->{$face}) {
233 $bigface and die "can't handle big faces with smoothing ($o->{face})\n"; 253 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
234 254
235 # full tile 255 # full tile
236 $draw_info{$smooth_level}{$sface}{$mx , $my } |= 0x1000; 256 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
237 257
238 # borders 258 # borders
239 $draw_info{$smooth_level}{$sface}{$mx + 1, $my } |= 0x0031; 259 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031;
240 $draw_info{$smooth_level}{$sface}{$mx , $my + 1} |= 0x0092; 260 $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
241 $draw_info{$smooth_level}{$sface}{$mx - 1, $my } |= 0x0064; 261 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064;
242 $draw_info{$smooth_level}{$sface}{$mx , $my - 1} |= 0x00c8; 262 $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8;
243 263
244 # corners 264 # corners
245 $draw_info{$smooth_level}{$sface}{$mx + 1, $my + 1} |= 0x0100; 265 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100;
246 $draw_info{$smooth_level}{$sface}{$mx - 1, $my + 1} |= 0x0200; 266 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
247 $draw_info{$smooth_level}{$sface}{$mx - 1, $my - 1} |= 0x0400; 267 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
248 $draw_info{$smooth_level}{$sface}{$mx + 1, $my - 1} |= 0x0800; 268 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
249 } else {
250 $smooth_level = 0;
251 } 269 }
252 270
253 my $dx = $bigface ? $o->{x} : 0; 271 my $dx = $bigface ? $o->{x} : 0;
254 my $dy = $bigface ? $o->{y} : 0; 272 my $dy = $bigface ? $o->{y} : 0;
255 273
256 $draw_info{$level}{$o->{face}}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); 274 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
257 275
258 $o = $o->{more}; 276 $o = $o->{more};
259 } 277 }
260 } 278 }
261 } 279 }
262 } 280 }
263 281
264 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T 282 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
265 or die; 283 or die;
266 $map_pb->fill (0x00000000); 284 $map_pb->fill (0xffffff00);
267 285
268 # second pass, render all the stuff 286 # second pass, render all the stuff
269 for my $level (sort { $a <=> $b } keys %draw_info) { 287 for my $level (sort { $a <=> $b } keys %draw_info) {
270 my $v = $draw_info{$level}; 288 my $v = $draw_info{$level};
271 while (my ($sface, $info) = each %$v) { 289 while (my ($sface, $info) = each %$v) {
326} 344}
327 345
328for my $file (@ARGV) { 346for my $file (@ARGV) {
329 my $mapa = read_arch $file; 347 my $mapa = read_arch $file;
330 my ($pb, $meta) = cfmap_render $mapa, $file; 348 my ($pb, $meta) = cfmap_render $mapa, $file;
331 $pb->save ("$file.png", "png"); 349 $pb->save ("$file.png~", "png");
350 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
351 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
352 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png";
353 unlink "$file.png~";
332 Storable::nstore $meta, "$file.pst"; 354 Storable::nstore $meta, "$file.pst";
333} 355}
334 356
335 357
336 358

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines