1 |
elmex |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Crossfire - Crossfire maphandling |
4 |
|
|
|
5 |
|
|
=cut |
6 |
|
|
|
7 |
root |
1.4 |
package Crossfire; |
8 |
|
|
|
9 |
elmex |
1.3 |
our $VERSION = '0.1'; |
10 |
elmex |
1.1 |
|
11 |
|
|
use strict; |
12 |
|
|
|
13 |
root |
1.7 |
use base 'Exporter'; |
14 |
|
|
|
15 |
root |
1.13 |
use Carp (); |
16 |
elmex |
1.1 |
use Storable; |
17 |
root |
1.15 |
use List::Util qw(min max); |
18 |
elmex |
1.1 |
|
19 |
elmex |
1.11 |
#XXX: The map_* procedures scream for a map-object |
20 |
|
|
|
21 |
|
|
our @EXPORT = |
22 |
root |
1.15 |
qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); |
23 |
root |
1.7 |
|
24 |
root |
1.4 |
our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 |
root |
1.13 |
or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
26 |
elmex |
1.1 |
|
27 |
root |
1.7 |
sub TILESIZE (){ 32 } |
28 |
elmex |
1.1 |
|
29 |
root |
1.15 |
our $CACHEDIR; |
30 |
|
|
our %ARCH; |
31 |
|
|
our %FACE; |
32 |
|
|
our $TILE; |
33 |
elmex |
1.1 |
|
34 |
root |
1.13 |
our %FIELD_MULTILINE = ( |
35 |
root |
1.14 |
msg => "endmsg", |
36 |
|
|
lore => "endlore", |
37 |
root |
1.13 |
); |
38 |
|
|
|
39 |
|
|
# not used yet, maybe alphabetical is ok |
40 |
|
|
our @FIELD_ORDER = (qw(name name_pl)); |
41 |
|
|
|
42 |
root |
1.14 |
sub MOVE_WALK (){ 0x1 } |
43 |
|
|
sub MOVE_FLY_LOW (){ 0x2 } |
44 |
|
|
sub MOVE_FLY_HIGH (){ 0x4 } |
45 |
|
|
sub MOVE_FLYING (){ 0x6 } |
46 |
|
|
sub MOVE_SWIM (){ 0x8 } |
47 |
|
|
sub MOVE_ALL (){ 0xf } |
48 |
|
|
|
49 |
|
|
sub normalize_arch($) { |
50 |
|
|
my ($ob) = @_; |
51 |
|
|
|
52 |
root |
1.15 |
my $arch = $ARCH{$ob->{_name}} |
53 |
root |
1.14 |
or (warn "$ob->{_name}: no such archetype", return $ob); |
54 |
|
|
|
55 |
|
|
delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply); |
56 |
|
|
|
57 |
|
|
if ($arch->{type} == 22) { # map |
58 |
|
|
my %normalize = ( |
59 |
|
|
"enter_x" => "hp", |
60 |
|
|
"enter_y" => "sp", |
61 |
|
|
"width" => "x", |
62 |
|
|
"height" => "y", |
63 |
|
|
"reset_timeout" => "weight", |
64 |
|
|
"swap_time" => "value", |
65 |
|
|
"difficulty" => "level", |
66 |
|
|
"darkness" => "invisible", |
67 |
|
|
"fixed_resettime" => "stand_still", |
68 |
|
|
); |
69 |
|
|
|
70 |
|
|
while (my ($k2, $k1) = each %normalize) { |
71 |
|
|
if (defined (my $v = delete $ob->{$k1})) { |
72 |
|
|
$ob->{$k2} = $v; |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
if (defined (my $v = delete $ob->{no_pass})) { |
78 |
|
|
$ob->{move_block} = $v ? MOVE_ALL : 0; |
79 |
|
|
} |
80 |
|
|
if (defined (my $v = delete $ob->{walk_on})) { |
81 |
|
|
$ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
82 |
|
|
: $ob->{move_on} & ~MOVE_WALK; |
83 |
|
|
} |
84 |
|
|
if (defined (my $v = delete $ob->{walk_off})) { |
85 |
|
|
$ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
86 |
|
|
: $ob->{move_off} & ~MOVE_WALK; |
87 |
|
|
} |
88 |
|
|
if (defined (my $v = delete $ob->{fly_on})) { |
89 |
|
|
$ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
90 |
|
|
: $ob->{move_on} & ~MOVE_FLY_LOW; |
91 |
|
|
} |
92 |
|
|
if (defined (my $v = delete $ob->{fly_off})) { |
93 |
|
|
$ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
94 |
|
|
: $ob->{move_off} & ~MOVE_FLY_LOW; |
95 |
|
|
} |
96 |
|
|
if (defined (my $v = delete $ob->{flying})) { |
97 |
|
|
$ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
98 |
|
|
: $ob->{move_type} & ~MOVE_FLY_LOW; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
# if value matches archetype default, delete |
102 |
|
|
while (my ($k, $v) = each %$ob) { |
103 |
|
|
if (exists $arch->{$k} and $arch->{$k} eq $v) { |
104 |
root |
1.15 |
next if $k eq "_name"; |
105 |
root |
1.14 |
delete $ob->{$k}; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
$ob |
110 |
|
|
} |
111 |
root |
1.13 |
|
112 |
root |
1.4 |
sub read_pak($;$) { |
113 |
|
|
my ($path, $cache) = @_; |
114 |
elmex |
1.1 |
|
115 |
|
|
eval { |
116 |
root |
1.4 |
defined $cache |
117 |
|
|
&& -M $cache < -M $path |
118 |
|
|
&& Storable::retrieve $cache |
119 |
elmex |
1.1 |
} or do { |
120 |
|
|
my %pak; |
121 |
|
|
|
122 |
|
|
open my $fh, "<:raw", $path |
123 |
root |
1.13 |
or Carp::croak "$_[0]: $!"; |
124 |
elmex |
1.1 |
while (<$fh>) { |
125 |
|
|
my ($type, $id, $len, $path) = split; |
126 |
|
|
$path =~ s/.*\///; |
127 |
|
|
read $fh, $pak{$path}, $len; |
128 |
|
|
} |
129 |
|
|
|
130 |
root |
1.4 |
Storable::nstore \%pak, $cache |
131 |
|
|
if defined $cache; |
132 |
elmex |
1.1 |
|
133 |
|
|
\%pak |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
sub read_arch($;$) { |
138 |
root |
1.4 |
my ($path, $cache) = @_; |
139 |
|
|
|
140 |
|
|
eval { |
141 |
|
|
defined $cache |
142 |
|
|
&& -M $cache < -M $path |
143 |
|
|
&& Storable::retrieve $cache |
144 |
|
|
} or do { |
145 |
|
|
my %arc; |
146 |
|
|
my ($more, $prev); |
147 |
|
|
|
148 |
|
|
open my $fh, "<:raw", $path |
149 |
root |
1.13 |
or Carp::croak "$path: $!"; |
150 |
elmex |
1.1 |
|
151 |
root |
1.4 |
my $parse_block; $parse_block = sub { |
152 |
|
|
my %arc = @_; |
153 |
elmex |
1.2 |
|
154 |
root |
1.4 |
while (<$fh>) { |
155 |
|
|
s/\s+$//; |
156 |
|
|
if (/^end$/i) { |
157 |
|
|
last; |
158 |
|
|
} elsif (/^arch (\S+)$/) { |
159 |
root |
1.14 |
push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
160 |
root |
1.4 |
} elsif (/^lore$/) { |
161 |
|
|
while (<$fh>) { |
162 |
|
|
last if /^endlore\s*$/i; |
163 |
|
|
$arc{lore} .= $_; |
164 |
|
|
} |
165 |
|
|
} elsif (/^msg$/) { |
166 |
|
|
while (<$fh>) { |
167 |
|
|
last if /^endmsg\s*$/i; |
168 |
|
|
$arc{msg} .= $_; |
169 |
|
|
} |
170 |
|
|
} elsif (/^(\S+)\s*(.*)$/) { |
171 |
|
|
$arc{lc $1} = $2; |
172 |
|
|
} elsif (/^\s*($|#)/) { |
173 |
|
|
# |
174 |
|
|
} else { |
175 |
|
|
warn "$path: unparsable line '$_' in arch $arc{_name}"; |
176 |
|
|
} |
177 |
|
|
} |
178 |
elmex |
1.1 |
|
179 |
root |
1.4 |
\%arc |
180 |
|
|
}; |
181 |
elmex |
1.1 |
|
182 |
|
|
while (<$fh>) { |
183 |
|
|
s/\s+$//; |
184 |
root |
1.4 |
if (/^more$/i) { |
185 |
|
|
$more = $prev; |
186 |
|
|
} elsif (/^object (\S+)$/i) { |
187 |
|
|
my $name = $1; |
188 |
|
|
my $arc = $parse_block->(_name => $name); |
189 |
|
|
|
190 |
|
|
if ($more) { |
191 |
|
|
$more->{more} = $arc; |
192 |
|
|
} else { |
193 |
|
|
$arc{$name} = $arc; |
194 |
elmex |
1.1 |
} |
195 |
root |
1.4 |
$prev = $arc; |
196 |
|
|
$more = undef; |
197 |
|
|
} elsif (/^arch (\S+)$/i) { |
198 |
root |
1.14 |
push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); |
199 |
elmex |
1.1 |
} elsif (/^\s*($|#)/) { |
200 |
|
|
# |
201 |
|
|
} else { |
202 |
root |
1.4 |
warn "$path: unparseable top-level line '$_'"; |
203 |
elmex |
1.1 |
} |
204 |
|
|
} |
205 |
|
|
|
206 |
root |
1.4 |
undef $parse_block; # work around bug in perl not freeing $fh etc. |
207 |
elmex |
1.2 |
|
208 |
root |
1.4 |
Storable::nstore \%arc, $cache |
209 |
|
|
if defined $cache; |
210 |
elmex |
1.1 |
|
211 |
root |
1.4 |
\%arc |
212 |
elmex |
1.2 |
} |
213 |
elmex |
1.1 |
} |
214 |
|
|
|
215 |
elmex |
1.10 |
# put all archs into a hash with editor_face as it's key |
216 |
|
|
# NOTE: the arrays in the hash values are references to |
217 |
|
|
# the archs from $ARCH |
218 |
|
|
sub editor_archs { |
219 |
|
|
my %paths; |
220 |
|
|
|
221 |
root |
1.15 |
for (keys %ARCH) { |
222 |
|
|
my $arch = $ARCH{$_}; |
223 |
elmex |
1.10 |
push @{$paths{$arch->{editor_folder}}}, \$arch; |
224 |
|
|
} |
225 |
|
|
|
226 |
root |
1.15 |
\%paths |
227 |
elmex |
1.10 |
} |
228 |
|
|
|
229 |
root |
1.15 |
# arch_extents determines the extents of a given arch |
230 |
elmex |
1.10 |
# bigfaces, linked faces and single faces are handled here |
231 |
root |
1.15 |
# it returns (minx, miny, maxx, maxy) |
232 |
|
|
sub arch_extents { |
233 |
elmex |
1.10 |
my ($a) = @_; |
234 |
|
|
|
235 |
root |
1.15 |
my $o = $ARCH{$a->{_name}} |
236 |
|
|
or return; |
237 |
elmex |
1.10 |
|
238 |
root |
1.15 |
my $face = $FACE{$a->{face} || $o->{face}} |
239 |
|
|
or (warn "no face data found for arch '$a->{_name}'"), return; |
240 |
elmex |
1.10 |
|
241 |
root |
1.15 |
if ($face->{w} > 1 || $face->{h} > 1) { |
242 |
|
|
# bigface |
243 |
|
|
return (0, 0, $face->{w} - 1, $face->{h} - 1); |
244 |
|
|
|
245 |
|
|
} elsif ($o->{more}) { |
246 |
|
|
# linked face |
247 |
|
|
my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2; |
248 |
|
|
|
249 |
|
|
for (; $o; $o = $o->{more}) { |
250 |
|
|
$minx = min $minx, $o->{x}; |
251 |
|
|
$miny = min $miny, $o->{y}; |
252 |
|
|
$maxx = max $maxx, $o->{x}; |
253 |
|
|
$maxy = max $maxy, $o->{y}; |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
return ($minx, $miny, $maxx, $maxy); |
257 |
elmex |
1.10 |
|
258 |
|
|
} else { |
259 |
|
|
# single face |
260 |
root |
1.15 |
return (0, 0, 0, 0); |
261 |
elmex |
1.10 |
} |
262 |
|
|
} |
263 |
|
|
|
264 |
root |
1.4 |
sub init($) { |
265 |
|
|
my ($cachedir) = @_; |
266 |
elmex |
1.1 |
|
267 |
root |
1.15 |
return if %ARCH; |
268 |
|
|
|
269 |
|
|
*ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
270 |
elmex |
1.1 |
} |
271 |
|
|
|
272 |
root |
1.15 |
$CACHEDIR ||= "$ENV{HOME}/.crossfire"; |
273 |
|
|
|
274 |
|
|
init $CACHEDIR; |
275 |
|
|
|
276 |
elmex |
1.1 |
=head1 AUTHOR |
277 |
|
|
|
278 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
279 |
|
|
http://home.schmorp.de/ |
280 |
|
|
|
281 |
|
|
Robin Redeker <elmex@ta-sa.org> |
282 |
|
|
http://www.ta-sa.org/ |
283 |
|
|
|
284 |
|
|
=cut |
285 |
root |
1.4 |
|
286 |
|
|
1 |