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 | |
|
|
22 | our $VERSION = '1.1'; |
4 | |
23 | |
5 | use strict; |
24 | use strict; |
6 | |
25 | |
7 | use Storable; |
26 | use Storable; |
8 | use List::Util qw(max); |
27 | use List::Util qw(max); |
9 | |
28 | |
10 | use Gtk2; |
29 | use Gtk2; |
11 | |
|
|
12 | our $VERSION = '1.0'; |
|
|
13 | |
30 | |
14 | #init Gtk2::Gdk; |
31 | #init Gtk2::Gdk; |
15 | |
32 | |
16 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
33 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
17 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
34 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
… | |
… | |
173 | |
190 | |
174 | my %meta; |
191 | my %meta; |
175 | |
192 | |
176 | my ($mapx, $mapy); |
193 | my ($mapx, $mapy); |
177 | |
194 | |
178 | my $map = $meta{map} = []; |
195 | my $map; |
179 | |
196 | |
180 | for (@{ $mapa->{arch} }) { |
197 | for (@{ $mapa->{arch} }) { |
|
|
198 | my ($x, $y) = ($_->{x}, $_->{y}); |
|
|
199 | |
181 | if ($_->{_name} eq "map") { |
200 | if ($_->{_name} eq "map") { |
182 | $meta{info} = $_; |
201 | $meta{info} = $_; |
183 | |
202 | |
184 | $mapx = $_->{width} || $_->{x}; |
203 | $mapx = $_->{width} || $x; |
185 | $mapy = $_->{height} || $_->{y}; |
204 | $mapy = $_->{height} || $y; |
186 | } else { |
205 | } else { |
187 | push @{ $map->[$_->{x}][$_->{y}] }, $_; |
206 | push @{ $map->[$x][$y] }, $_; |
188 | |
207 | |
189 | # arch map is unreliable w.r.t. width and height |
208 | # arch map is unreliable w.r.t. width and height |
190 | $mapx = $_->{x} + 1 if $mapx <= $_->{x}; |
209 | $mapx = $x + 1 if $mapx <= $x; |
191 | $mapy = $_->{y} + 1 if $mapy <= $_->{y}; |
210 | $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}; |
211 | #$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}; |
212 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
194 | } |
213 | } |
195 | } |
214 | } |
196 | |
215 | |
197 | $meta{width} = $mapx; |
216 | $meta{width} = $mapx; |
198 | $meta{height} = $mapy; |
217 | $meta{height} = $mapy; |
199 | |
218 | |
200 | my %draw_info; |
219 | my %draw_info; |
|
|
220 | my %map_info; |
201 | |
221 | |
202 | # first pass, gather face stacking order, border and corner info |
222 | # first pass, gather face stacking order, border and corner info |
203 | for my $x (0 .. $mapx - 1) { |
223 | for my $x (0 .. $mapx - 1) { |
204 | my $col = $map->[$x]; |
224 | my $col = $map->[$x]; |
205 | for my $y (0 .. $mapy - 1) { |
225 | for my $y (0 .. $mapy - 1) { |
… | |
… | |
230 | || 0 > $my || $my >= $mapy; |
250 | || 0 > $my || $my >= $mapy; |
231 | |
251 | |
232 | # this is very ugly (some tiles are 32x33 or worse) |
252 | # this is very ugly (some tiles are 32x33 or worse) |
233 | my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
253 | my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2; |
234 | |
254 | |
|
|
255 | my $dx = $bigface ? $o->{x} : 0; |
|
|
256 | my $dy = $bigface ? $o->{y} : 0; |
|
|
257 | |
|
|
258 | push @{ $map_info{$level}{$mx, $my} }, $a; |
|
|
259 | |
|
|
260 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
|
|
261 | |
235 | if (my $sface = $smooth->{$face}) { |
262 | if (my $sface = $smooth->{$face}) { |
236 | $bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
263 | $bigface and die "can't handle bigfaces with smoothing ($face)\n"; |
237 | |
264 | |
238 | # full tile |
265 | # full tile |
239 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
266 | $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000; |
… | |
… | |
249 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
276 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200; |
250 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
277 | $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400; |
251 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
278 | $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800; |
252 | } |
279 | } |
253 | |
280 | |
254 | my $dx = $bigface ? $o->{x} : 0; |
|
|
255 | my $dy = $bigface ? $o->{y} : 0; |
|
|
256 | |
|
|
257 | $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16); |
|
|
258 | |
|
|
259 | $o = $o->{more}; |
281 | $o = $o->{more}; |
260 | } |
282 | } |
261 | } |
283 | } |
262 | } |
284 | } |
263 | } |
285 | } |
264 | |
286 | |
265 | my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
287 | my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
266 | or die; |
288 | or die; |
267 | $map_pb->fill (0x00000000); |
289 | $map_pb->fill (0xffffff00); |
268 | |
290 | |
269 | # second pass, render all the stuff |
291 | # second pass, render the map |
270 | for my $level (sort { $a <=> $b } keys %draw_info) { |
292 | for my $level (sort { $a <=> $b } keys %draw_info) { |
271 | my $v = $draw_info{$level}; |
293 | my $v = $draw_info{$level}; |
272 | while (my ($sface, $info) = each %$v) { |
294 | while (my ($sface, $info) = each %$v) { |
273 | my $pb = tile $sface |
295 | my $pb = tile $sface |
274 | or die "no smooth face $sface\n"; |
296 | or die "no smooth face $sface\n"; |
… | |
… | |
319 | 255 |
341 | 255 |
320 | ) if $corner; |
342 | ) if $corner; |
321 | } |
343 | } |
322 | } |
344 | } |
323 | } |
345 | } |
|
|
346 | } |
|
|
347 | |
|
|
348 | # third pass, gather meta info |
|
|
349 | for my $level (sort { $a <=> $b } keys %map_info) { |
|
|
350 | my $info = $map_info{$level}; |
|
|
351 | |
|
|
352 | while (my ($xy, $as) = each %$info) { |
|
|
353 | my ($x, $y) = split $;, $xy; |
|
|
354 | |
|
|
355 | next if $x < 0 || $x >= $mapx |
|
|
356 | || $y < 0 || $y >= $mapy; |
|
|
357 | |
|
|
358 | push @{ $meta{map}[$x][$y] }, $_ for @$as; |
|
|
359 | } |
324 | } |
360 | } |
325 | |
361 | |
326 | ($map_pb, \%meta) |
362 | ($map_pb, \%meta) |
327 | } |
363 | } |
328 | |
364 | |