1 |
=head1 NAME |
2 |
|
3 |
Crossfire - Crossfire maphandling |
4 |
|
5 |
=cut |
6 |
|
7 |
package Crossfire; |
8 |
|
9 |
our $VERSION = '0.1'; |
10 |
|
11 |
use strict; |
12 |
|
13 |
use base 'Exporter'; |
14 |
|
15 |
use Carp (); |
16 |
use Storable; |
17 |
use File::Spec; |
18 |
use List::Util qw(min max); |
19 |
|
20 |
#XXX: The map_* procedures scream for a map-object |
21 |
|
22 |
our @EXPORT = |
23 |
qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); |
24 |
|
25 |
our $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 |
or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
27 |
|
28 |
sub TILESIZE (){ 32 } |
29 |
|
30 |
our $VARDIR; |
31 |
our %ARCH; |
32 |
our %FACE; |
33 |
our $TILE; |
34 |
|
35 |
our %FIELD_MULTILINE = ( |
36 |
msg => "endmsg", |
37 |
lore => "endlore", |
38 |
); |
39 |
|
40 |
# not used yet, maybe alphabetical is ok |
41 |
our @FIELD_ORDER = (qw(name name_pl)); |
42 |
|
43 |
sub MOVE_WALK (){ 0x1 } |
44 |
sub MOVE_FLY_LOW (){ 0x2 } |
45 |
sub MOVE_FLY_HIGH (){ 0x4 } |
46 |
sub MOVE_FLYING (){ 0x6 } |
47 |
sub MOVE_SWIM (){ 0x8 } |
48 |
sub MOVE_ALL (){ 0xf } |
49 |
|
50 |
sub load_ref($) { |
51 |
my ($path) = @_; |
52 |
|
53 |
open my $fh, ">:raw", "$path~" |
54 |
or die "$path~: $!"; |
55 |
local $/; |
56 |
Storable::thaw <$fh> |
57 |
} |
58 |
|
59 |
sub save_ref($$) { |
60 |
my ($ref, $path) = @_; |
61 |
|
62 |
open my $fh, ">:raw", "$path~" |
63 |
or die "$path~: $!"; |
64 |
print $fh Storable::nfreeze $ref; |
65 |
close $fh; |
66 |
rename "$path~", $path |
67 |
or die "$path: $!"; |
68 |
} |
69 |
|
70 |
sub normalize_arch($) { |
71 |
my ($ob) = @_; |
72 |
|
73 |
my $arch = $ARCH{$ob->{_name}} |
74 |
or (warn "$ob->{_name}: no such archetype", return $ob); |
75 |
|
76 |
delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply); |
77 |
|
78 |
if ($arch->{type} == 22) { # map |
79 |
my %normalize = ( |
80 |
"enter_x" => "hp", |
81 |
"enter_y" => "sp", |
82 |
"width" => "x", |
83 |
"height" => "y", |
84 |
"reset_timeout" => "weight", |
85 |
"swap_time" => "value", |
86 |
"difficulty" => "level", |
87 |
"darkness" => "invisible", |
88 |
"fixed_resettime" => "stand_still", |
89 |
); |
90 |
|
91 |
while (my ($k2, $k1) = each %normalize) { |
92 |
if (defined (my $v = delete $ob->{$k1})) { |
93 |
$ob->{$k2} = $v; |
94 |
} |
95 |
} |
96 |
} |
97 |
|
98 |
if (defined (my $v = delete $ob->{no_pass})) { |
99 |
$ob->{move_block} = $v ? MOVE_ALL : 0; |
100 |
} |
101 |
if (defined (my $v = delete $ob->{walk_on})) { |
102 |
$ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
103 |
: $ob->{move_on} & ~MOVE_WALK; |
104 |
} |
105 |
if (defined (my $v = delete $ob->{walk_off})) { |
106 |
$ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
107 |
: $ob->{move_off} & ~MOVE_WALK; |
108 |
} |
109 |
if (defined (my $v = delete $ob->{fly_on})) { |
110 |
$ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
111 |
: $ob->{move_on} & ~MOVE_FLY_LOW; |
112 |
} |
113 |
if (defined (my $v = delete $ob->{fly_off})) { |
114 |
$ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
115 |
: $ob->{move_off} & ~MOVE_FLY_LOW; |
116 |
} |
117 |
if (defined (my $v = delete $ob->{flying})) { |
118 |
$ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
119 |
: $ob->{move_type} & ~MOVE_FLY_LOW; |
120 |
} |
121 |
|
122 |
# if value matches archetype default, delete |
123 |
while (my ($k, $v) = each %$ob) { |
124 |
if (exists $arch->{$k} and $arch->{$k} eq $v) { |
125 |
next if $k eq "_name"; |
126 |
delete $ob->{$k}; |
127 |
} |
128 |
} |
129 |
|
130 |
$ob |
131 |
} |
132 |
|
133 |
sub read_pak($;$) { |
134 |
my ($path, $cache) = @_; |
135 |
|
136 |
eval { |
137 |
defined $cache |
138 |
&& -M $cache < -M $path |
139 |
&& load_ref $cache |
140 |
} or do { |
141 |
my %pak; |
142 |
|
143 |
open my $fh, "<:raw", $path |
144 |
or Carp::croak "$_[0]: $!"; |
145 |
while (<$fh>) { |
146 |
my ($type, $id, $len, $path) = split; |
147 |
$path =~ s/.*\///; |
148 |
read $fh, $pak{$path}, $len; |
149 |
} |
150 |
|
151 |
save_ref \%pak, $cache |
152 |
if defined $cache; |
153 |
|
154 |
\%pak |
155 |
} |
156 |
} |
157 |
|
158 |
sub read_arch($;$) { |
159 |
my ($path, $cache) = @_; |
160 |
|
161 |
eval { |
162 |
defined $cache |
163 |
&& -M $cache < -M $path |
164 |
&& load_ref $cache |
165 |
} or do { |
166 |
my %arc; |
167 |
my ($more, $prev); |
168 |
|
169 |
open my $fh, "<:raw", $path |
170 |
or Carp::croak "$path: $!"; |
171 |
|
172 |
my $parse_block; $parse_block = sub { |
173 |
my %arc = @_; |
174 |
|
175 |
while (<$fh>) { |
176 |
s/\s+$//; |
177 |
if (/^end$/i) { |
178 |
last; |
179 |
} elsif (/^arch (\S+)$/) { |
180 |
push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
181 |
} elsif (/^lore$/) { |
182 |
while (<$fh>) { |
183 |
last if /^endlore\s*$/i; |
184 |
$arc{lore} .= $_; |
185 |
} |
186 |
} elsif (/^msg$/) { |
187 |
while (<$fh>) { |
188 |
last if /^endmsg\s*$/i; |
189 |
$arc{msg} .= $_; |
190 |
} |
191 |
} elsif (/^(\S+)\s*(.*)$/) { |
192 |
$arc{lc $1} = $2; |
193 |
} elsif (/^\s*($|#)/) { |
194 |
# |
195 |
} else { |
196 |
warn "$path: unparsable line '$_' in arch $arc{_name}"; |
197 |
} |
198 |
} |
199 |
|
200 |
\%arc |
201 |
}; |
202 |
|
203 |
while (<$fh>) { |
204 |
s/\s+$//; |
205 |
if (/^more$/i) { |
206 |
$more = $prev; |
207 |
} elsif (/^object (\S+)$/i) { |
208 |
my $name = $1; |
209 |
my $arc = $parse_block->(_name => $name); |
210 |
|
211 |
if ($more) { |
212 |
$more->{more} = $arc; |
213 |
} else { |
214 |
$arc{$name} = $arc; |
215 |
} |
216 |
$prev = $arc; |
217 |
$more = undef; |
218 |
} elsif (/^arch (\S+)$/i) { |
219 |
push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); |
220 |
} elsif (/^\s*($|#)/) { |
221 |
# |
222 |
} else { |
223 |
warn "$path: unparseable top-level line '$_'"; |
224 |
} |
225 |
} |
226 |
|
227 |
undef $parse_block; # work around bug in perl not freeing $fh etc. |
228 |
|
229 |
save_ref \%arc, $cache |
230 |
if defined $cache; |
231 |
|
232 |
\%arc |
233 |
} |
234 |
} |
235 |
|
236 |
# put all archs into a hash with editor_face as it's key |
237 |
# NOTE: the arrays in the hash values are references to |
238 |
# the archs from $ARCH |
239 |
sub editor_archs { |
240 |
my %paths; |
241 |
|
242 |
for (keys %ARCH) { |
243 |
my $arch = $ARCH{$_}; |
244 |
push @{$paths{$arch->{editor_folder}}}, \$arch; |
245 |
} |
246 |
|
247 |
\%paths |
248 |
} |
249 |
|
250 |
=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch |
251 |
|
252 |
arch_extents determines the extents of the given arch's face(s), linked |
253 |
faces and single faces are handled here it returns (minx, miny, maxx, |
254 |
maxy) |
255 |
|
256 |
=cut |
257 |
|
258 |
sub arch_extents { |
259 |
my ($a) = @_; |
260 |
|
261 |
my $o = $ARCH{$a->{_name}} |
262 |
or return; |
263 |
|
264 |
my $face = $FACE{$a->{face} || $o->{face}} |
265 |
or (warn "no face data found for arch '$a->{_name}'"), return; |
266 |
|
267 |
if ($face->{w} > 1 || $face->{h} > 1) { |
268 |
# bigface |
269 |
return (0, 0, $face->{w} - 1, $face->{h} - 1); |
270 |
|
271 |
} elsif ($o->{more}) { |
272 |
# linked face |
273 |
my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2; |
274 |
|
275 |
for (; $o; $o = $o->{more}) { |
276 |
$minx = min $minx, $o->{x}; |
277 |
$miny = min $miny, $o->{y}; |
278 |
$maxx = max $maxx, $o->{x}; |
279 |
$maxy = max $maxy, $o->{y}; |
280 |
} |
281 |
|
282 |
return ($minx, $miny, $maxx, $maxy); |
283 |
|
284 |
} else { |
285 |
# single face |
286 |
return (0, 0, 0, 0); |
287 |
} |
288 |
} |
289 |
|
290 |
=item $type = arch_attr $arch |
291 |
|
292 |
Returns a hashref describing the object and its attributes. It can contain |
293 |
the following keys: |
294 |
|
295 |
name the name, suitable for display purposes |
296 |
ignore |
297 |
attr |
298 |
desc |
299 |
use |
300 |
section => [name => \%attr, name => \%attr] |
301 |
import |
302 |
|
303 |
=cut |
304 |
|
305 |
sub arch_attr($) { |
306 |
my ($arch) = @_; |
307 |
|
308 |
require Crossfire::Data; |
309 |
|
310 |
my $attr; |
311 |
|
312 |
if ($arch->{type} > 0) { |
313 |
$attr = $Crossfire::Data::ATTR{$arch->{type}+0}; |
314 |
} else { |
315 |
$attr = $Crossfire::Data::TYPE{Misc}; |
316 |
|
317 |
type: |
318 |
for (@Crossfire::Data::ATTR0) { |
319 |
my $req = $_->{required} |
320 |
or die "internal error: ATTR0 without 'required'"; |
321 |
|
322 |
while (my ($k, $v) = each %$req) { |
323 |
next type |
324 |
unless $arch->{$k} == $v; |
325 |
} |
326 |
|
327 |
$attr = $_; |
328 |
} |
329 |
} |
330 |
|
331 |
$attr || \%Crossfire::Data::DEFAULT_ATTR; |
332 |
} |
333 |
|
334 |
sub arch_edit_sections { |
335 |
# if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
336 |
# edit_type = 0; |
337 |
# else if (edit_type != 0) { |
338 |
# // all flags from 'check_type' must be unset in this arch because they get recalculated now |
339 |
# edit_type &= ~check_type; |
340 |
# } |
341 |
# |
342 |
# } |
343 |
# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 && |
344 |
# getAttributeValue("alive", defarch) == 1 && |
345 |
# (getAttributeValue("monster", defarch) == 1 || |
346 |
# getAttributeValue("generator", defarch) == 1)) { |
347 |
# // Monster: monsters/npcs/generators |
348 |
# edit_type |= IGUIConstants.TILE_EDIT_MONSTER; |
349 |
# } |
350 |
# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 && |
351 |
# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) { |
352 |
# // Walls |
353 |
# edit_type |= IGUIConstants.TILE_EDIT_WALL; |
354 |
# } |
355 |
# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 && |
356 |
# getAttributeValue("connected", defarch) != 0) { |
357 |
# // Connected Objects |
358 |
# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED; |
359 |
# } |
360 |
# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 && |
361 |
# arch_type == 66 || arch_type == 41 || arch_type == 95) { |
362 |
# // Exit: teleporter/exit/trapdoors |
363 |
# edit_type |= IGUIConstants.TILE_EDIT_EXIT; |
364 |
# } |
365 |
# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 && |
366 |
# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 || |
367 |
# arch_type == 5 || arch_type == 36 || arch_type == 60 || |
368 |
# arch_type == 85 || arch_type == 111 || arch_type == 123 || |
369 |
# arch_type == 124 || arch_type == 130)) { |
370 |
# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls |
371 |
# edit_type |= IGUIConstants.TILE_EDIT_TREASURE; |
372 |
# } |
373 |
# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 && |
374 |
# arch_type == 20 || arch_type == 23 || arch_type == 26 || |
375 |
# arch_type == 91 || arch_type == 21 || arch_type == 24) { |
376 |
# // Door: door/special door/gates + keys |
377 |
# edit_type |= IGUIConstants.TILE_EDIT_DOOR; |
378 |
# } |
379 |
# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 && |
380 |
# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 && |
381 |
# arch_type <= 16) || arch_type == 33 || arch_type == 34 || |
382 |
# arch_type == 35 || arch_type == 39 || arch_type == 70 || |
383 |
# arch_type == 87 || arch_type == 99 || arch_type == 100 || |
384 |
# arch_type == 104 || arch_type == 109 || arch_type == 113 || |
385 |
# arch_type == 122 || arch_type == 3)) { |
386 |
# // Equipment: weapons/armour/wands/rods |
387 |
# edit_type |= IGUIConstants.TILE_EDIT_EQUIP; |
388 |
# } |
389 |
# |
390 |
# return(edit_type); |
391 |
# |
392 |
# |
393 |
} |
394 |
|
395 |
sub init($) { |
396 |
my ($cachedir) = @_; |
397 |
|
398 |
return if %ARCH; |
399 |
|
400 |
mkdir $cachedir, 0777; |
401 |
*ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
402 |
} |
403 |
|
404 |
$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
405 |
|
406 |
init $VARDIR; |
407 |
|
408 |
=head1 AUTHOR |
409 |
|
410 |
Marc Lehmann <schmorp@schmorp.de> |
411 |
http://home.schmorp.de/ |
412 |
|
413 |
Robin Redeker <elmex@ta-sa.org> |
414 |
http://www.ta-sa.org/ |
415 |
|
416 |
=cut |
417 |
|
418 |
1 |