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.11 by elmex, Tue Feb 7 14:30:08 2006 UTC vs.
Revision 1.13 by root, Thu Feb 9 20:54:42 2006 UTC

10 10
11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp ();
15use Storable; 16use Storable;
16 17
17#XXX: The map_* procedures scream for a map-object 18#XXX: The map_* procedures scream for a map-object
18 19
19our @EXPORT = 20our @EXPORT =
20 qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs 21 qw(read_pak read_arch $ARCH TILESIZE editor_archs
21 arch2pickmap arch_extends 22 arch_extends
22 map_get_tile_stack map_push_tile_stack map_pop_tile_stack 23 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
23 ); 24 );
24 25
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 26our $LIB = $ENV{CROSSFIRE_LIBDIR}
26 or die "\$CROSSFIRE_LIBDIR must be set\n"; 27 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
27 28
28sub TILESIZE (){ 32 } 29sub TILESIZE (){ 32 }
29 30
30our $ARCH; 31our $ARCH;
32
33our %FIELD_MULTILINE = (
34 msg => "endmsg",
35);
36
37# not used yet, maybe alphabetical is ok
38our @FIELD_ORDER = (qw(name name_pl));
39
40# not used yet, AND NOT CHECKED, should also be BY TYPE
41our %FIELD_NORMALIZE = (
42 "enter_x" => "hp",
43 "enter_y" => "sp",
44);
31 45
32sub read_pak($;$) { 46sub read_pak($;$) {
33 my ($path, $cache) = @_; 47 my ($path, $cache) = @_;
34 48
35 eval { 49 eval {
38 && Storable::retrieve $cache 52 && Storable::retrieve $cache
39 } or do { 53 } or do {
40 my %pak; 54 my %pak;
41 55
42 open my $fh, "<:raw", $path 56 open my $fh, "<:raw", $path
43 or die "$_[0]: $!"; 57 or Carp::croak "$_[0]: $!";
44 while (<$fh>) { 58 while (<$fh>) {
45 my ($type, $id, $len, $path) = split; 59 my ($type, $id, $len, $path) = split;
46 $path =~ s/.*\///; 60 $path =~ s/.*\///;
47 read $fh, $pak{$path}, $len; 61 read $fh, $pak{$path}, $len;
48 } 62 }
64 } or do { 78 } or do {
65 my %arc; 79 my %arc;
66 my ($more, $prev); 80 my ($more, $prev);
67 81
68 open my $fh, "<:raw", $path 82 open my $fh, "<:raw", $path
69 or die "$path: $!"; 83 or Carp::croak "$path: $!";
70 84
71 my $parse_block; $parse_block = sub { 85 my $parse_block; $parse_block = sub {
72 my %arc = @_; 86 my %arc = @_;
73 87
74 while (<$fh>) { 88 while (<$fh>) {
219 # single face 233 # single face
220 return (0, 0, 1, 1); 234 return (0, 0, 1, 1);
221 } 235 }
222} 236}
223 237
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
308sub arch2map($;$) {
309 my ($mapa) = @_;
310
311 my %meta;
312
313 my ($mapx, $mapy);
314
315 my $map;
316
317 for (@{ $mapa->{arch} }) {
318 my ($x, $y) = (delete $_->{x}, delete $_->{y});
319
320 if ($_->{_name} eq "map") {
321 $meta{info} = $_;
322
323 $mapx = $_->{width} || $x;
324 $mapy = $_->{height} || $y;
325 } else {
326 push @{ $map->[$x][$y] }, $_;
327
328 # arch map is unreliable w.r.t. width and height
329 $mapx = $x + 1 if $mapx <= $x;
330 $mapy = $y + 1 if $mapy <= $y;
331 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
332 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
333 }
334 }
335
336 $meta{width} = $mapx;
337 $meta{height} = $mapy;
338 $meta{map} = $map;
339
340 \%meta
341}
342
343sub init($) { 238sub init($) {
344 my ($cachedir) = @_; 239 my ($cachedir) = @_;
345 240
346 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 241 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
347} 242}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines