ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
Revision: 1.25
Committed: Sun Jan 6 21:12:00 2008 UTC (16 years, 4 months ago) by root
Branch: MAIN
Changes since 1.24: +6 -6 lines
Log Message:
deliantrify

File Contents

# Content
1 #!/opt/bin/perl
2
3 # cfarch2png - convert deliantra maps to png+metadata
4 # Copyright (C) 2005,2007,2008 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.002';
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 (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
100
101 my $idx = $FACE{$face}{idx};
102
103 my $mx = $x + $o->{x};
104 my $my = $y + $o->{y};
105
106 last if 0 > $mx || $mx >= $mapx
107 || 0 > $my || $my >= $mapy;
108
109 push @{ $map_info{$level}{$mx, $my} }, $a;
110
111 $draw_info{$level}{$idx}{$mx, $my} |= 0x2000;
112
113 if (0) { # disable smoothing for the time being
114 if (my $sface = $FACEDATA{faceinfo}{$face}{smooth}) {
115 $FACE{$sface}
116 or (warn "$mapname: smoothface '$sface' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
117 my $idx = $FACE{$sface}{idx};
118 my $level = $layer * 256 + $FACEDATA{faceinfo}{$face}{smoothlevel};
119
120 # full tile
121 $draw_info{$level}{$idx}{$mx , $my } |= 0x1000;
122
123 # borders
124 $draw_info{$level}{$idx}{$mx + 1, $my } |= 0x0091;
125 $draw_info{$level}{$idx}{$mx , $my + 1} |= 0x0032;
126 $draw_info{$level}{$idx}{$mx - 1, $my } |= 0x0064;
127 $draw_info{$level}{$idx}{$mx , $my - 1} |= 0x00c8;
128
129 # corners
130 $draw_info{$level}{$idx}{$mx + 1, $my + 1} |= 0x0100;
131 $draw_info{$level}{$idx}{$mx - 1, $my + 1} |= 0x0200;
132 $draw_info{$level}{$idx}{$mx - 1, $my - 1} |= 0x0400;
133 $draw_info{$level}{$idx}{$mx + 1, $my - 1} |= 0x0800;
134 }
135 }
136
137 $o = $o->{more};
138 }
139 }
140 }
141 }
142
143 my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
144 or die;
145 $map_pb->fill (0xffffff00);
146
147 # second pass, render the map
148 for my $level (sort { $a <=> $b } keys %draw_info) {
149 my $v = $draw_info{$level};
150 while (my ($face, $info) = each %$v) {
151 while (my ($xy, $bits) = each %$info) {
152 my ($x, $y) = split $;, $xy;
153
154 next if $x < 0 || $x >= $mapx
155 || $y < 0 || $y >= $mapy;
156
157 # bits is __fn cccc CCCC bbbb
158 # f full tile draw with x|y bigface displacement
159 # n do not draw borders&corners
160 # c draw these corners, but...
161 # C ... not these
162 # b draw these borders
163
164 if ($bits & 0x2000) {
165 my $ix = $face % CACHESTRIDE;
166 my $iy = int $face / CACHESTRIDE;
167
168 $TILE->composite ($map_pb,
169 $x * T, $y * T,
170 T, T,
171 ($x - $ix) * T, ($y - $iy) * T, 1, 1,
172 "nearest",
173 255
174 );
175 }
176
177 unless ($bits & 0x1000) {
178 my $border = $face + ($bits & 0xf);
179 my $bx = $border % CACHESTRIDE;
180 my $by = int $border / CACHESTRIDE;
181 $TILE->composite ($map_pb,
182 $x * T, $y * T,
183 T, T,
184 ($x - $bx) * T, ($y - $by) * T, 1, 1,
185 "nearest",
186 255
187 ) if $border;
188
189 my $corner = $face + (($bits >> 8) & ~($bits >> 4) & 0xf);
190 my $cx = $corner % CACHESTRIDE;
191 my $cy = int $corner / CACHESTRIDE;
192 $TILE->composite ($map_pb,
193 $x * T, $y * T,
194 T, T,
195 ($x - $cx) * T, ($y - $cy) * T, 1, 1,
196 "nearest",
197 255
198 ) if $corner;
199 }
200 }
201 }
202 }
203
204 # third pass, gather meta info
205 for my $level (sort { $a <=> $b } keys %map_info) {
206 my $info = $map_info{$level};
207
208 while (my ($xy, $as) = each %$info) {
209 my ($x, $y) = split $;, $xy;
210
211 next if $x < 0 || $x >= $mapx
212 || $y < 0 || $y >= $mapy;
213
214 push @{ $meta{map}[$x][$y] }, $_ for @$as;
215 }
216 }
217
218 ($map_pb, \%meta)
219 }
220
221 for my $file (@ARGV) {
222 my $mapa = read_arch $file;
223 $file =~ s/\.map$//;
224 my ($pb, $meta) = cfmap_render $mapa, $file;
225 $pb->save ("$file.png~~", "png");
226 system "gm", "convert", "$file.png~~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
227 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
228 system "optipng", "-q", "-out", "$file.png~", "$file.png~~";
229 # system "pngnq <\Q$file.png~\E >\Q$file.png\E";
230 unlink "$file.png~~";
231 utime +(stat $file)[8,9], "$file.jpg";
232 utime +(stat $file)[8,9], "$file.png~";
233 rename "$file.png~", "$file.png";
234 }
235
236
237
238
239
240