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.13 by root, Wed Nov 23 18:48:17 2005 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# cfarch2png - convert crossfire maps to png+metadata
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
3# tower of stars: missing craters? 20# tower of stars: missing craters?
21# world_108_123 (8|18), hole below grass but shouldn't?
22
23our $VERSION = '1.11';
4 24
5use strict; 25use strict;
6 26
7use Storable; 27use Storable;
8use List::Util qw(max); 28use List::Util qw(max);
9 29
10use Gtk2; 30use Gtk2;
11
12our $VERSION = '1.0';
13 31
14#init Gtk2::Gdk; 32#init Gtk2::Gdk;
15 33
16my $LIB = $ENV{CROSSFIRE_LIBDIR} 34my $LIB = $ENV{CROSSFIRE_LIBDIR}
17 or die "\$CROSSFIRE_LIBDIR must be set\n"; 35 or die "\$CROSSFIRE_LIBDIR must be set\n";
173 191
174 my %meta; 192 my %meta;
175 193
176 my ($mapx, $mapy); 194 my ($mapx, $mapy);
177 195
178 my $map = $meta{map} = []; 196 my $map;
179 197
180 for (@{ $mapa->{arch} }) { 198 for (@{ $mapa->{arch} }) {
199 my ($x, $y) = ($_->{x}, $_->{y});
200
181 if ($_->{_name} eq "map") { 201 if ($_->{_name} eq "map") {
182 $meta{info} = $_; 202 $meta{info} = $_;
183 203
184 $mapx = $_->{width} || $_->{x}; 204 $mapx = $_->{width} || $x;
185 $mapy = $_->{height} || $_->{y}; 205 $mapy = $_->{height} || $y;
186 } else { 206 } else {
187 push @{ $map->[$_->{x}][$_->{y}] }, $_; 207 push @{ $map->[$x][$y] }, $_;
188 208
189 # arch map is unreliable w.r.t. width and height 209 # arch map is unreliable w.r.t. width and height
190 $mapx = $_->{x} + 1 if $mapx <= $_->{x}; 210 $mapx = $x + 1 if $mapx <= $x;
191 $mapy = $_->{y} + 1 if $mapy <= $_->{y}; 211 $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}; 212 #$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}; 213 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
194 } 214 }
195 } 215 }
196 216
197 $meta{width} = $mapx; 217 $meta{width} = $mapx;
198 $meta{height} = $mapy; 218 $meta{height} = $mapy;
199 219
200 my %map_face;
201 my %draw_info; 220 my %draw_info;
221 my %map_info;
202 222
203 # first pass, gather face stacking order, border and corner info 223 # first pass, gather face stacking order, border and corner info
204 for my $x (0 .. $mapx - 1) { 224 for my $x (0 .. $mapx - 1) {
205 my $col = $map->[$x]; 225 my $col = $map->[$x];
206 for my $y (0 .. $mapy - 1) { 226 for my $y (0 .. $mapy - 1) {
210 my $a = $as->[$layer]; 230 my $a = $as->[$layer];
211 231
212 my $o = $arch->{$a->{_name}} 232 my $o = $arch->{$a->{_name}}
213 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;
214 234
215 my $A = { %$o, %$a }; 235 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
216 236 my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
217 my $smoothlevel = $A->{smoothlevel};
218 my $level = $smoothlevel ? $smoothlevel 237 my $level = $smoothlevel ? $smoothlevel
219 : $A->{is_floor} ? 0 238 : $is_floor ? $layer - 1000
220 : 256 + $layer; 239 : $layer + 1000;
221 240
222 while ($o) { 241 while ($o) {
242 my $face = $a->{face} || $o->{face};
243
223 my $pb = tile $A->{face} 244 my $pb = tile $face
224 or (warn "$mapname: face '$A->{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;
225 246
226 my $mx = $x + $o->{x}; 247 my $mx = $x + $o->{x};
227 my $my = $y + $o->{y}; 248 my $my = $y + $o->{y};
228 249
229 last if 0 > $mx || $mx >= $mapx 250 last if 0 > $mx || $mx >= $mapx
230 || 0 > $my || $my >= $mapy; 251 || 0 > $my || $my >= $mapy;
231 252
232 # this is very ugly (some tiles are 32x33 or worse) 253 # this is very ugly (some tiles are 32x33 or worse)
233 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;
234 255
256 my $dx = $bigface ? $o->{x} : 0;
257 my $dy = $bigface ? $o->{y} : 0;
258
259 push @{ $map_info{$level}{$mx, $my} }, $a;
260
261 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
262
235 if (my $sface = $smooth->{$A->{face}}) { 263 if (my $sface = $smooth->{$face}) {
236 $bigface and die "can't handle bigfaces with smoothing ($A->{face})\n"; 264 $bigface and die "can't handle bigfaces with smoothing ($face)\n";
237 265
238 # full tile 266 # full tile
239 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; 267 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
240 268
241 # borders 269 # borders
249 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; 277 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
250 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; 278 $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
251 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; 279 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
252 } 280 }
253 281
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
259 $o = $o->{more}; 282 $o = $o->{more};
260 } 283 }
261 } 284 }
262 } 285 }
263 } 286 }
264 287
265 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
266 or die; 289 or die;
267 $map_pb->fill (0x00000000); 290 $map_pb->fill (0xffffff00);
268 291
269 # second pass, render all the stuff 292 # second pass, render the map
270 for my $level (sort { $a <=> $b } keys %draw_info) { 293 for my $level (sort { $a <=> $b } keys %draw_info) {
271 my $v = $draw_info{$level}; 294 my $v = $draw_info{$level};
272 while (my ($sface, $info) = each %$v) { 295 while (my ($sface, $info) = each %$v) {
273 my $pb = tile $sface 296 my $pb = tile $sface
274 or die "no smooth face $sface\n"; 297 or die "no smooth face $sface\n";
277 my ($x, $y) = split $;, $xy; 300 my ($x, $y) = split $;, $xy;
278 301
279 next if $x < 0 || $x >= $mapx 302 next if $x < 0 || $x >= $mapx
280 || $y < 0 || $y >= $mapy; 303 || $y < 0 || $y >= $mapy;
281 304
282 # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB 305 # bits is xxxx xxxx yyyy yyyy __fn cccc CCCC bbbb
283 # X don't draw
284 # F full tile draw with x|y bigface displacement 306 # f full tile draw with x|y bigface displacement
285 # c maybe draw these corners
286 # C do not draw these corners 307 # n do not draw borders&corners
308 # c draw these corners, but...
309 # C ... not these
287 # b draw these borders 310 # b draw these borders
288 311
289 if ($bits & 0x2000) { 312 if ($bits & 0x2000) {
290 my $dx = (($bits >> 24) & 0xff) - 128; 313 my $dx = (($bits >> 24) & 0xff) - 128;
291 my $dy = (($bits >> 16) & 0xff) - 128; 314 my $dy = (($bits >> 16) & 0xff) - 128;
321 } 344 }
322 } 345 }
323 } 346 }
324 } 347 }
325 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
326 ($map_pb, \%meta) 363 ($map_pb, \%meta)
327} 364}
328 365
329for my $file (@ARGV) { 366for my $file (@ARGV) {
330 my $mapa = read_arch $file; 367 my $mapa = read_arch $file;
331 my ($pb, $meta) = cfmap_render $mapa, $file; 368 my ($pb, $meta) = cfmap_render $mapa, $file;
332 $pb->save ("$file.png~", "png"); 369 $pb->save ("$file.png~", "png");
333 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg"; 370 system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
334 system "mogrify", "-colors" => 65536, "$file.png~"; 371 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
335 system "pngcrush", "-q", "-m" => 7, "-rem" => "alla", "-reduce", "$file.png~", "$file.png"; 372 system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png";
336 unlink "$file.png~"; 373 unlink "$file.png~";
337 Storable::nstore $meta, "$file.pst"; 374 Storable::nstore $meta, "$file.pst";
338} 375}
339 376
340 377

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines