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.4 by root, Sun Feb 5 00:18:26 2006 UTC vs.
Revision 1.13 by root, Thu Feb 9 20:54:42 2006 UTC

8 8
9our $VERSION = '0.1'; 9our $VERSION = '0.1';
10 10
11use strict; 11use strict;
12 12
13use base 'Exporter';
14
15use Carp ();
13use Storable; 16use Storable;
14use List::Util qw(max); 17
18#XXX: The map_* procedures scream for a map-object
19
20our @EXPORT =
21 qw(read_pak read_arch $ARCH TILESIZE editor_archs
22 arch_extends
23 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
24 );
15 25
16our $LIB = $ENV{CROSSFIRE_LIBDIR} 26our $LIB = $ENV{CROSSFIRE_LIBDIR}
17 or die "\$CROSSFIRE_LIBDIR must be set\n"; 27 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
18 28
19sub T (){ 32 } 29sub TILESIZE (){ 32 }
20 30
21our $ARCH; 31our $ARCH;
22our $TILE; 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);
23 45
24sub read_pak($;$) { 46sub read_pak($;$) {
25 my ($path, $cache) = @_; 47 my ($path, $cache) = @_;
26 48
27 eval { 49 eval {
30 && Storable::retrieve $cache 52 && Storable::retrieve $cache
31 } or do { 53 } or do {
32 my %pak; 54 my %pak;
33 55
34 open my $fh, "<:raw", $path 56 open my $fh, "<:raw", $path
35 or die "$_[0]: $!"; 57 or Carp::croak "$_[0]: $!";
36 while (<$fh>) { 58 while (<$fh>) {
37 my ($type, $id, $len, $path) = split; 59 my ($type, $id, $len, $path) = split;
38 $path =~ s/.*\///; 60 $path =~ s/.*\///;
39 read $fh, $pak{$path}, $len; 61 read $fh, $pak{$path}, $len;
40 } 62 }
56 } or do { 78 } or do {
57 my %arc; 79 my %arc;
58 my ($more, $prev); 80 my ($more, $prev);
59 81
60 open my $fh, "<:raw", $path 82 open my $fh, "<:raw", $path
61 or die "$path: $!"; 83 or Carp::croak "$path: $!";
62 84
63 my $parse_block; $parse_block = sub { 85 my $parse_block; $parse_block = sub {
64 my %arc = @_; 86 my %arc = @_;
65 87
66 while (<$fh>) { 88 while (<$fh>) {
122 144
123 \%arc 145 \%arc
124 } 146 }
125} 147}
126 148
127sub arch2map($;$) { 149# returns the arch/object stack from a tile on a map
150sub map_get_tile_stack {
151 my ($map, $x, $y) = @_;
152 my $as;
153
154 if ($x > 0 || $x < $map->{width}
155 || $y > 0 || $y < $map->{height}) {
156
157 $as = $map->{map}{map}[$x][$y] || [];
158 }
159
160 return $as;
161}
162
163# pop the topmost arch/object from the stack of a tile on a map
164sub map_pop_tile_stack {
165 my ($map, $x, $y) = @_;
166
167 if ($x > 0 || $x < $map->{width}
168 || $y > 0 || $y < $map->{height}) {
169
170 pop @{$map->{map}{map}[$x][$y]};
171 }
172}
173
174# pushes the arch/object on the stack of a tile on a map
175sub map_push_tile_stack {
176 my ($map, $x, $y, $arch) = @_;
177
178 if ($x > 0 || $x < $map->{width}
179 || $y > 0 || $y < $map->{height}) {
180
181 push @{$map->{map}{map}[$x][$y]}, $arch;
182 }
183}
184
185
186# put all archs into a hash with editor_face as it's key
187# NOTE: the arrays in the hash values are references to
188# the archs from $ARCH
189sub editor_archs {
190 my %paths;
191
192 for (keys %$ARCH) {
193 my $arch = $ARCH->{$_};
194 push @{$paths{$arch->{editor_folder}}}, \$arch;
195 }
196
197 return \%paths;
198}
199
200# arch_extends determines how the arch looks like on the map,
201# bigfaces, linked faces and single faces are handled here
202# it returns (<xoffset>, <yoffset>, <width>, <height>)
203# NOTE: non rectangular linked faces are not considered
204sub arch_extends {
128 my ($mapa) = @_; 205 my ($a) = @_;
129 206
130 my %meta; 207 my $TC = \%Crossfire::Tilecache::TILECACHE;
131 208
132 my ($mapx, $mapy); 209 my $facename =
210 $a->{face} || $ARCH->{$a->{_name}}->{face}
211 or return ();
133 212
134 my $map; 213 my $tile = $TC->{$facename}
214 or (warn "no gfx found for arch '$facename' in arch_size ()"), return;
135 215
136 for (@{ $mapa->{arch} }) { 216 if ($tile->{w} > 1 || $tile->{h} > 1) {
137 my ($x, $y) = ($_->{x}, $_->{y}); 217 # bigfaces
218 return (0, 0, $tile->{w}, $tile->{h});
138 219
139 if ($_->{_name} eq "map") { 220 } elsif ($a->{more}) {
140 $meta{info} = $_; 221 # linked faces
222 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0);
223 do {
224 $miw > (0 + $a->{x}) and $miw = $a->{x};
225 $mih > (0 + $a->{y}) and $mih = $a->{y};
226 $maw < (0 + $a->{x}) and $maw = $a->{x};
227 $mah < (0 + $a->{y}) and $mah = $a->{y};
228 } while $a = $a->{more};
141 229
142 $mapx = $_->{width} || $x; 230 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1)
143 $mapy = $_->{height} || $y; 231
144 } else { 232 } else {
145 push @{ $map->[$x][$y] }, $_; 233 # single face
146 234 return (0, 0, 1, 1);
147 # arch map is unreliable w.r.t. width and height
148 $mapx = $x + 1 if $mapx <= $x;
149 $mapy = $y + 1 if $mapy <= $y;
150 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
151 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
152 }
153 } 235 }
154
155 $meta{width} = $mapx;
156 $meta{height} = $mapy;
157
158 \%meta
159} 236}
160 237
161sub init($) { 238sub init($) {
162 my ($cachedir) = @_; 239 my ($cachedir) = @_;
163 240
164 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 241 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
165 $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst";
166} 242}
167 243
168=head1 AUTHOR 244=head1 AUTHOR
169 245
170 Marc Lehmann <schmorp@schmorp.de> 246 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines