ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
(Generate patch)

Comparing deliantra/Deliantra/Deliantra.pm (file contents):
Revision 1.5 by root, Sun Feb 5 00:25:33 2006 UTC vs.
Revision 1.11 by elmex, Tue Feb 7 14:30:08 2006 UTC

8 8
9our $VERSION = '0.1'; 9our $VERSION = '0.1';
10 10
11use strict; 11use strict;
12 12
13use base 'Exporter';
14
13use Storable; 15use Storable;
16
17#XXX: The map_* procedures scream for a map-object
18
19our @EXPORT =
20 qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs
21 arch2pickmap arch_extends
22 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
23 );
14 24
15our $LIB = $ENV{CROSSFIRE_LIBDIR} 25our $LIB = $ENV{CROSSFIRE_LIBDIR}
16 or die "\$CROSSFIRE_LIBDIR must be set\n"; 26 or die "\$CROSSFIRE_LIBDIR must be set\n";
17 27
18sub T (){ 32 } 28sub TILESIZE (){ 32 }
19 29
20our $ARCH; 30our $ARCH;
21our $TILE;
22 31
23sub read_pak($;$) { 32sub read_pak($;$) {
24 my ($path, $cache) = @_; 33 my ($path, $cache) = @_;
25 34
26 eval { 35 eval {
121 130
122 \%arc 131 \%arc
123 } 132 }
124} 133}
125 134
135# returns the arch/object stack from a tile on a map
136sub map_get_tile_stack {
137 my ($map, $x, $y) = @_;
138 my $as;
139
140 if ($x > 0 || $x < $map->{width}
141 || $y > 0 || $y < $map->{height}) {
142
143 $as = $map->{map}{map}[$x][$y] || [];
144 }
145
146 return $as;
147}
148
149# pop the topmost arch/object from the stack of a tile on a map
150sub map_pop_tile_stack {
151 my ($map, $x, $y) = @_;
152
153 if ($x > 0 || $x < $map->{width}
154 || $y > 0 || $y < $map->{height}) {
155
156 pop @{$map->{map}{map}[$x][$y]};
157 }
158}
159
160# pushes the arch/object on the stack of a tile on a map
161sub map_push_tile_stack {
162 my ($map, $x, $y, $arch) = @_;
163
164 if ($x > 0 || $x < $map->{width}
165 || $y > 0 || $y < $map->{height}) {
166
167 push @{$map->{map}{map}[$x][$y]}, $arch;
168 }
169}
170
171
172# put all archs into a hash with editor_face as it's key
173# NOTE: the arrays in the hash values are references to
174# the archs from $ARCH
175sub editor_archs {
176 my %paths;
177
178 for (keys %$ARCH) {
179 my $arch = $ARCH->{$_};
180 push @{$paths{$arch->{editor_folder}}}, \$arch;
181 }
182
183 return \%paths;
184}
185
186# arch_extends determines how the arch looks like on the map,
187# bigfaces, linked faces and single faces are handled here
188# it returns (<xoffset>, <yoffset>, <width>, <height>)
189# NOTE: non rectangular linked faces are not considered
190sub arch_extends {
191 my ($a) = @_;
192
193 my $TC = \%Crossfire::Tilecache::TILECACHE;
194
195 my $facename =
196 $a->{face} || $ARCH->{$a->{_name}}->{face}
197 or return ();
198
199 my $tile = $TC->{$facename}
200 or (warn "no gfx found for arch '$facename' in arch_size ()"), return;
201
202 if ($tile->{w} > 1 || $tile->{h} > 1) {
203 # bigfaces
204 return (0, 0, $tile->{w}, $tile->{h});
205
206 } elsif ($a->{more}) {
207 # linked faces
208 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0);
209 do {
210 $miw > (0 + $a->{x}) and $miw = $a->{x};
211 $mih > (0 + $a->{y}) and $mih = $a->{y};
212 $maw < (0 + $a->{x}) and $maw = $a->{x};
213 $mah < (0 + $a->{y}) and $mah = $a->{y};
214 } while $a = $a->{more};
215
216 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1)
217
218 } else {
219 # single face
220 return (0, 0, 1, 1);
221 }
222}
223
224# arch2pickmap forms a list of archs to a pickmap
225sub arch2pickmap {
226 my ($archs, $w) = @_;
227
228 # sort archs alphabetiacally
229 my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ];
230
231 $w ||= 10; # default width
232 my $num = @$archs;
233 my $map = { };
234 # overall placement coords
235 my $x = 0;
236 my $y = 0;
237
238 my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later
239 my $drawn_archs = 1; # line-break counter
240 my $max_line_height = 1;
241
242 for (my $i = 0; $i < $num; $i++) {
243
244 defined ${$archs->[$i]}->{face} or next;
245
246 # check whether this tile was already written (see below at (b))
247 unless (defined $map->{map}[$x][$y]) {
248
249 my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
250
251 # these are special placement coords, for chained faces which
252 # have a special placement offset
253 my ($place_x, $place_y) = ($x, $y);
254 $xoffs < 0 and
255 $place_x += -$xoffs;
256 $yoffs < 0 and
257 $place_y += -$yoffs;
258
259 # iterate over the tiles this arch takes
260 # NOTE: Chained archs are maybe not a rectangle, but i don't care
261 # much for that on pickmaps
262
263 for (my $xi = 0; $xi < $arch_w; $xi++) {
264 for (my $yi = 0; $yi < $arch_h; $yi++) {
265
266 my ($lx, $ly) = ($x + $xi, $y + $yi);
267
268 if ($lx == $place_x and $ly == $place_y) {
269 push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
270
271 } else {
272
273 # (b): here we set occupied tiles, but without the arch
274 $map->{map}[$lx][$ly] = [];
275 }
276 }
277 }
278 $drawn_archs++;
279
280 $x += $arch_w - 1;
281 $max_line_height < $arch_h
282 and $max_line_height = $arch_h;
283
284 } else {
285 $i--;
286 }
287
288
289 $x++;
290
291 if ($x > $w) {
292
293 $y += $max_line_height;
294 $max_line_height = 1;
295 $x = 0;
296 }
297
298 $maxw < ($x + 1) and $maxw = $x + 1;
299 $maxh < ($y + 1) and $maxh = $y + 1;
300 }
301
302 $map->{height} = $maxh;
303 $map->{width} = $maxw;
304
305 return $map;
306}
307
126sub arch2map($;$) { 308sub arch2map($;$) {
127 my ($mapa) = @_; 309 my ($mapa) = @_;
128 310
129 my %meta; 311 my %meta;
130 312
131 my ($mapx, $mapy); 313 my ($mapx, $mapy);
132 314
133 my $map; 315 my $map;
134 316
135 for (@{ $mapa->{arch} }) { 317 for (@{ $mapa->{arch} }) {
136 my ($x, $y) = ($_->{x}, $_->{y}); 318 my ($x, $y) = (delete $_->{x}, delete $_->{y});
137 319
138 if ($_->{_name} eq "map") { 320 if ($_->{_name} eq "map") {
139 $meta{info} = $_; 321 $meta{info} = $_;
140 322
141 $mapx = $_->{width} || $x; 323 $mapx = $_->{width} || $x;
151 } 333 }
152 } 334 }
153 335
154 $meta{width} = $mapx; 336 $meta{width} = $mapx;
155 $meta{height} = $mapy; 337 $meta{height} = $mapy;
338 $meta{map} = $map;
156 339
157 \%meta 340 \%meta
158} 341}
159 342
160sub init($) { 343sub init($) {
161 my ($cachedir) = @_; 344 my ($cachedir) = @_;
162 345
163 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 346 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
164 $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst";
165} 347}
166 348
167=head1 AUTHOR 349=head1 AUTHOR
168 350
169 Marc Lehmann <schmorp@schmorp.de> 351 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines