ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/Map.pm
Revision: 1.1
Committed: Thu Feb 9 19:59:29 2006 UTC (18 years, 3 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Crossfire::Map - represent a crossfire map
4    
5     =cut
6    
7     package Crossfire::Map;
8    
9     our $VERSION = '0.1';
10    
11     use strict;
12    
13     use Crossfire;
14    
15     use base 'Exporter';
16    
17     sub new {
18     my ($class, $width, $height) = @_;
19    
20     bless { width => $width, height => $height }, $class
21     }
22    
23     sub new_from_file {
24     new_from_archlist {$_[0]} read_arch $_[1]
25     }
26    
27     sub new_from_archlist {
28     my ($class, $mapa) = @_;
29    
30     my %meta;
31    
32     my ($mapx, $mapy);
33    
34     my $map;
35    
36     for (@{ $mapa->{arch} }) {
37     my ($x, $y) = (delete $_->{x}, delete $_->{y});
38    
39     if ($_->{_name} eq "map") {
40     $meta{info} = $_;
41    
42     $mapx = $_->{width} || $x;
43     $mapy = $_->{height} || $y;
44     } else {
45     push @{ $map->[$x][$y] }, $_;
46    
47     # arch map is unreliable w.r.t. width and height
48     $mapx = $x + 1 if $mapx <= $x;
49     $mapy = $y + 1 if $mapy <= $y;
50     #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
51     #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
52     }
53     }
54    
55     $meta{width} = $mapx;
56     $meta{height} = $mapy;
57     $meta{map} = $map;
58    
59     bless \%meta, $class
60     }
61    
62     sub new_pickmap {
63     my ($class, $archs, $width) = @_;
64    
65     # sort archs alphabetically
66     my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ];
67    
68     $width ||= 10; # default width
69    
70     my $num = @$archs;
71     my $map = { };
72     # overall placement coords
73     my $x = 0;
74     my $y = 0;
75    
76     my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later
77     my $drawn_archs = 1; # line-break counter
78     my $max_line_height = 1;
79    
80     for (my $i = 0; $i < $num; $i++) {
81    
82     defined ${$archs->[$i]}->{face} or next;
83    
84     # check whether this tile was already written (see below at (b))
85     unless (defined $map->{map}[$x][$y]) {
86     my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
87    
88     if ($x + $arch_w > $width) {
89     $y += $max_line_height;
90     $max_line_height = 1;
91     $x = 0;
92     }
93    
94     # these are special placement coords, for chained faces which
95     # have a special placement offset
96     my ($place_x, $place_y) = ($x, $y);
97     $xoffs < 0 and
98     $place_x += -$xoffs;
99     $yoffs < 0 and
100     $place_y += -$yoffs;
101    
102     # iterate over the tiles this arch takes
103     # NOTE: Chained archs are maybe not a rectangle, but i don't care
104     # much for that on pickmaps
105    
106     for (my $xi = 0; $xi < $arch_w; $xi++) {
107     for (my $yi = 0; $yi < $arch_h; $yi++) {
108    
109     my ($lx, $ly) = ($x + $xi, $y + $yi);
110    
111     if ($lx == $place_x and $ly == $place_y) {
112     push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
113    
114     } else {
115    
116     # (b): here we set occupied tiles, but without the arch
117     $map->{map}[$lx][$ly] = [];
118     }
119     }
120     }
121     $drawn_archs++;
122    
123     $x += $arch_w;
124    
125     $max_line_height < $arch_h
126     and $max_line_height = $arch_h;
127    
128     } else {
129     $i--;
130     }
131    
132     $maxw = List::Util::max $maxw, $x;
133     $maxh = List::Util::max $maxh, $y;
134     }
135    
136     $map->{height} = $maxh;
137     $map->{width} = $maxw;
138    
139     return $map;
140     }
141    
142     =head1 AUTHOR
143    
144     Marc Lehmann <schmorp@schmorp.de>
145     http://home.schmorp.de/
146    
147     Robin Redeker <elmex@ta-sa.org>
148     http://www.ta-sa.org/
149    
150     =cut
151    
152     1