Map.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 205 CC 8 SCAR 19.7
CC
8
Cov
0%
setOccupied
20.0
setVacant
6.0
getObjectLocation
6.0
CRAP 7.2
1  
2 package Games::EternalLands::Map;
3  
4 use strict;
5 use Carp;
6 use Data::Dumper;
7 use Games::EternalLands::MapHelper ':all';
8 use vars qw(@ISA);
9  
10 our $VERSION = '0.04';
11  
12 sub setOccupied
13 {
14 my $self = shift;
15 my ($actor) = @_;
16  
17 return 0;
18  
19 if (!defined($actor)) {
20 carp("Actor is undefined");
21 return 0;
22 }
23 if (!defined($actor->{'xpos'}) || !defined($actor->{'ypos'})) {
24 carp("Actor has undefined location");
25 return 0;
26 }
27 my $loc = $actor->{'xpos'}.",".$actor->{'ypos'};
28 if ($self->{'actorsByLoc'}->{$loc} ne $actor) {
29 carp("Map says another actor is at ($loc) !\n");
30 return 0;
31 }
32 $self->{'actorsByLoc'}->{$loc} = $actor;
33 return 1;
34 }
35  
36 sub setVacant
37 {
38 my $self = shift;
39 my ($actor) = @_;
40  
41 my $loc = $self->{'xpos'}.",".$self->{'ypos'};
42 if ($self->{'actorsByLoc'}->{$loc} != $actor) {
43 carp("Map says actor is not at ($loc) !\n");
44 return 0;
45 }
46 delete $self->{'actorsByLoc'}->{$loc};
47 return 1;
48 }
49  
50 sub distance($$$$$)
51 {
52 my $self = shift;
53  
54 my ($x1,$y1,$x2,$y2) = @_;
55  
56 my $x = ($x1-$x2);
57 my $y = ($y1-$y2);
58  
59 return sqrt($x*$x+$y*$y);
60 }
61  
62 sub getObjectLocation
63 {
64 my $self = shift;
65 my ($id) = @_;
66  
67 my $obj = $self->{'3dByID'}->{$id};
68  
69 my ($x,$y) = (undef,undef);
70 if (defined($obj)) {
71 $x = int($obj->{'x_pos'}*2);
72 $y = int($obj->{'y_pos'}*2);
73 }
74 return ($x,$y);
75 }
76  
77 sub objects
78 {
79 my $self = shift;
80  
81 my @objs = keys(%{$self->{'3dByID'}});
82  
83 return wantarray ? @objs : \@objs;
84 }
85  
86 sub parseMapHeader($)
87 {
88 my ($buf) = @_;
89  
90 my %mapHdr;
91  
92 $mapHdr{'file_sig'} = substr($buf,0,4);
93 $mapHdr{'tile_map_x_len'} = unpack('V',substr($buf,4,4));
94 $mapHdr{'tile_map_y_len'} = unpack('V',substr($buf,8,4));
95 $mapHdr{'tile_map_offset'} = unpack('V',substr($buf,12,4));
96 $mapHdr{'height_map_offset'} = unpack('V',substr($buf,16,4));
97 $mapHdr{'obj_3d_struct_len'} = unpack('V',substr($buf,20,4));
98 $mapHdr{'obj_3d_no'} = unpack('V',substr($buf,24,4));
99 $mapHdr{'obj_3d_offset'} = unpack('V',substr($buf,28,4));
100 $mapHdr{'obj_2d_struct_len'} = unpack('V',substr($buf,32,4));
101 $mapHdr{'obj_2d_no'} = unpack('V',substr($buf,36,4));
102 $mapHdr{'obj_2d_offset'} = unpack('V',substr($buf,40,4));
103 $mapHdr{'lights_struct_len'} = unpack('V',substr($buf,44,4));
104 $mapHdr{'lights_no'} = unpack('V',substr($buf,48,4));
105 $mapHdr{'lights_offset'} = unpack('V',substr($buf,52,4));
106 $mapHdr{'dungeon'} = ord(substr($buf,56,1));
107 $mapHdr{'res_2'} = ord(substr($buf,57,1));
108 $mapHdr{'res_3'} = ord(substr($buf,58,1));
109 $mapHdr{'res_4'} = ord(substr($buf,59,1));
110 $mapHdr{'ambient_r'} = unpack('f',substr($buf,60,4));
111 $mapHdr{'ambient_g'} = unpack('f',substr($buf,64,4));
112 $mapHdr{'ambient_b'} = unpack('f',substr($buf,68,4));
113 $mapHdr{'particles_struct_len'} = unpack('V',substr($buf,72,4));
114 $mapHdr{'particles_no'} = unpack('V',substr($buf,76,4));
115 $mapHdr{'particles_offset'} = unpack('V',substr($buf,80,4));
116 $mapHdr{'reserved_8'} = unpack('V',substr($buf,84,4));
117 $mapHdr{'reserved_9'} = unpack('V',substr($buf,88,4));
118 $mapHdr{'reserved_10'} = unpack('V',substr($buf,92,4));
119 $mapHdr{'reserved_11'} = unpack('V',substr($buf,96,4));
120 $mapHdr{'reserved_12'} = unpack('V',substr($buf,100,4));
121 $mapHdr{'reserved_13'} = unpack('V',substr($buf,104,4));
122 $mapHdr{'reserved_14'} = unpack('V',substr($buf,108,4));
123 $mapHdr{'reserved_15'} = unpack('V',substr($buf,112,4));
124 $mapHdr{'reserved_16'} = unpack('V',substr($buf,116,4));
125 $mapHdr{'reserved_17'} = unpack('V',substr($buf,120,4));
126  
127 return \%mapHdr;
128 }
129  
130 sub parse3Dobj
131 {
132 my ($buf) = @_;
133  
134 my %obj;
135  
136 $obj{'file_name'} = unpack('Z*',substr($buf,0,80));
137 $obj{'x_pos'} = unpack('f',substr($buf,80,4));
138 $obj{'y_pos'} = unpack('f',substr($buf,84,4));
139 $obj{'z_pos'} = unpack('f',substr($buf,88,4));
140 $obj{'x_rot'} = unpack('f',substr($buf,92,4));
141 $obj{'y_rot'} = unpack('f',substr($buf,96,4));
142 $obj{'z_rot'} = unpack('f',substr($buf,100,4));
143 $obj{'self_list'} = ord(substr($buf,104,1));
144 $obj{'blended'} = ord(substr($buf,105,1));
145 $obj{'r'} = unpack('f',substr($buf,106,4));
146 $obj{'g'} = unpack('f',substr($buf,110,4));
147 $obj{'b'} = unpack('f',substr($buf,114,4));
148 $obj{'reserved'} = unpack('C*',substr($buf,118,24));
149  
150 return \%obj;
151 }
152  
153 sub new
154 {
155 my $class = shift;
156 my $self = {};
157 bless($self, $class);
158  
159 my ($fname,$elDir) = @_;
160 my ($mapHdrBuf,$tileMapBuf,$hghtMapBuf);
161  
162 if (!defined($elDir)) {
163 print STDERR "elDir not defined, so no maps\n";
164 return undef;
165 }
166  
167 open(FP,"$elDir/$fname") || confess "Could not open map file $elDir/$fname";
168  
169 (read(FP,$mapHdrBuf,124) == 124) || confess "Could not read map header: $!\n";
170  
171 my $mapHdr = parseMapHeader($mapHdrBuf);
172 my $wdth = $mapHdr->{'tile_map_x_len'};
173 my $hght = $mapHdr->{'tile_map_y_len'};
174  
175 (read(FP,$tileMapBuf,$wdth*$hght) == $wdth*$hght) || die "Could not read tileMap for '$fname'";
176 undef $tileMapBuf;
177  
178 $wdth *= 6;
179 $hght *= 6;
180 $self->{'width'} = $wdth;
181 $self->{'height'} = $hght;
182 my $hMapSize = $wdth*$hght;
183 my $hMapBuf;
184 (read(FP,$hMapBuf,$hMapSize) == $hMapSize) ||
185 confess "Could not read Height Map for '$fname'";
186 $self->{'hMap'} = $hMapBuf;
187  
188 my $rMapSize = $hMapSize;
189 $self->{'rMap'} = sprintf("%".$rMapSize."s","");
190 findRegions($self->{'hMap'},$self->{'rMap'},$wdth,$hght);
191  
192 my $obj3dBuf;
193 for(my $i=0; $i<$mapHdr->{'obj_3d_no'}; $i++) {
194 my $obj3dSize = $mapHdr->{'obj_3d_struct_len'};
195 (read(FP,$obj3dBuf,$obj3dSize) == $obj3dSize) || confess "Could not read 3D object";
196 $self->{'3dByID'}->{$i} = parse3Dobj($obj3dBuf);
197 }
198  
199 $self->{'wdth'} = $wdth;
200 $self->{'hght'} = $hght;
201 $self->{'actorsByLoc'} = {};
202 $fname =~ s%^.*/([^/]+)\.elm%$1%;
203 $self->{'name'} = $fname;
204  
205 return $self;
206 }
207  
208 return 1;