1 |
#!/opt/bin/perl |
2 |
|
3 |
# cfmap2png - convert deliantra maps to png+metadata |
4 |
# Copyright (C) 2005,2007,2008,2009 Marc Lehmann <cfmaps@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 |
|
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 |
|
33 |
our $VERSION = '2.012'; |
34 |
|
35 |
use strict; |
36 |
|
37 |
use Deliantra; |
38 |
use List::Util qw(max); |
39 |
|
40 |
use Gtk2; |
41 |
|
42 |
sub T (){ 32 } |
43 |
|
44 |
Deliantra::load_archetypes; |
45 |
Deliantra::load_tilecache; |
46 |
|
47 |
sub cfmap_render($;$) { |
48 |
my ($mapa, $mapname) = @_; |
49 |
|
50 |
my %meta; |
51 |
|
52 |
my ($mapx, $mapy); |
53 |
|
54 |
my $map; |
55 |
|
56 |
for (@{ $mapa->{arch} }) { |
57 |
my ($x, $y) = ($_->{x}, $_->{y}); |
58 |
|
59 |
if ($_->{_name} eq "map") { |
60 |
$meta{info} = $_; |
61 |
|
62 |
$mapx = $_->{width} || $x; |
63 |
$mapy = $_->{height} || $y; |
64 |
} else { |
65 |
push @{ $map->[$x][$y] }, $_; |
66 |
|
67 |
# arch map is unreliable w.r.t. width and height |
68 |
$mapx = $x + 1 if $mapx <= $x; |
69 |
$mapy = $y + 1 if $mapy <= $y; |
70 |
#$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
71 |
#$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
72 |
} |
73 |
} |
74 |
|
75 |
$meta{width} = $mapx; |
76 |
$meta{height} = $mapy; |
77 |
|
78 |
my %draw_info; |
79 |
my %map_info; |
80 |
|
81 |
# first pass, gather face stacking order, border and corner info |
82 |
for my $x (0 .. $mapx - 1) { |
83 |
my $col = $map->[$x]; |
84 |
for my $y (0 .. $mapy - 1) { |
85 |
my $as = $col->[$y] || []; |
86 |
|
87 |
for my $layer (0 .. $#$as) { |
88 |
my $a = $as->[$layer]; |
89 |
|
90 |
my $o = $ARCH{$a->{_name}} |
91 |
or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next; |
92 |
|
93 |
my $level = $layer * 256; |
94 |
|
95 |
while ($o) { |
96 |
my $face = $a->{face} || $o->{face}; |
97 |
|
98 |
$FACE{$face} |
99 |
or $face =~ s/\.1(\d\d)$/.x$1/; |
100 |
|
101 |
$FACE{$face} |
102 |
or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last; |
103 |
|
104 |
my $idx = $FACE{$face}{idx}; |
105 |
|
106 |
my $mx = $x + $o->{x}; |
107 |
my $my = $y + $o->{y}; |
108 |
|
109 |
last if 0 > $mx || $mx >= $mapx |
110 |
|| 0 > $my || $my >= $mapy; |
111 |
|
112 |
push @{ $map_info{$level}{$mx, $my} }, $a; |
113 |
|
114 |
$draw_info{$level}{$idx}{$mx, $my} |= 0x2000; |
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}; |
122 |
|
123 |
# full tile |
124 |
$draw_info{$level}{$idx}{$mx , $my } |= 0x1000; |
125 |
|
126 |
# borders |
127 |
$draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091; |
128 |
$draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032; |
129 |
$draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064; |
130 |
$draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8; |
131 |
|
132 |
# corners |
133 |
$draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100; |
134 |
$draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200; |
135 |
$draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400; |
136 |
$draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800; |
137 |
} |
138 |
} |
139 |
|
140 |
$o = $o->{more}; |
141 |
} |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T |
147 |
or die; |
148 |
$map_pb->fill (0xffffff00); |
149 |
|
150 |
# second pass, render the map |
151 |
for my $level (sort { $a <=> $b } keys %draw_info) { |
152 |
my $v = $draw_info{$level}; |
153 |
while (my ($face, $info) = each %$v) { |
154 |
while (my ($xy, $bits) = each %$info) { |
155 |
my ($x, $y) = split $;, $xy; |
156 |
|
157 |
next if $x < 0 || $x >= $mapx |
158 |
|| $y < 0 || $y >= $mapy; |
159 |
|
160 |
# bits is __fn cccc CCCC bbbb |
161 |
# f full tile draw with x|y bigface displacement |
162 |
# n do not draw borders&corners |
163 |
# c draw these corners, but... |
164 |
# C ... not these |
165 |
# b draw these borders |
166 |
|
167 |
if ($bits & 0x2000) { |
168 |
my $ix = $face % CACHESTRIDE; |
169 |
my $iy = int $face / CACHESTRIDE; |
170 |
|
171 |
$TILE->composite ($map_pb, |
172 |
$x * T, $y * T, |
173 |
T, T, |
174 |
($x - $ix) * T, ($y - $iy) * T, 1, 1, |
175 |
"nearest", |
176 |
255 |
177 |
); |
178 |
} |
179 |
|
180 |
unless ($bits & 0x1000) { |
181 |
my $border = $face + ($bits & 0xf); |
182 |
my $bx = $border % CACHESTRIDE; |
183 |
my $by = int $border / CACHESTRIDE; |
184 |
$TILE->composite ($map_pb, |
185 |
$x * T, $y * T, |
186 |
T, T, |
187 |
($x - $bx) * T, ($y - $by) * T, 1, 1, |
188 |
"nearest", |
189 |
255 |
190 |
) if $border; |
191 |
|
192 |
my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf); |
193 |
my $cx = $corner % CACHESTRIDE; |
194 |
my $cy = int $corner / CACHESTRIDE; |
195 |
$TILE->composite ($map_pb, |
196 |
$x * T, $y * T, |
197 |
T, T, |
198 |
($x - $cx) * T, ($y - $cy) * T, 1, 1, |
199 |
"nearest", |
200 |
255 |
201 |
) if $corner; |
202 |
} |
203 |
} |
204 |
} |
205 |
} |
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 |
|
221 |
($map_pb, \%meta) |
222 |
} |
223 |
|
224 |
for 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 { |
234 |
my $mapa = read_arch "$file.map"; |
235 |
my ($pb, $meta) = cfmap_render $mapa, $file; |
236 |
$pb->save ("$file.png~~", "png"); |
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 $stderr .= $@; |
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 |
} |
257 |
} |
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|