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 File::Spec; |
17 |
use List::Util qw(min max); |
18 |
use Storable; |
19 |
|
20 |
our @EXPORT = qw( |
21 |
read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
22 |
); |
23 |
|
24 |
our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
25 |
|
26 |
our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
27 |
|
28 |
mkdir $VARDIR, 0777; |
29 |
|
30 |
sub TILESIZE (){ 32 } |
31 |
|
32 |
our %ARCH; |
33 |
our %FACE; |
34 |
our $TILE; |
35 |
|
36 |
our %FIELD_MULTILINE = ( |
37 |
msg => "endmsg", |
38 |
lore => "endlore", |
39 |
maplore => "endmaplore", |
40 |
); |
41 |
|
42 |
# not used yet, maybe alphabetical is ok |
43 |
our @FIELD_ORDER = (qw(name name_pl)); |
44 |
|
45 |
sub MOVE_WALK (){ 0x01 } |
46 |
sub MOVE_FLY_LOW (){ 0x02 } |
47 |
sub MOVE_FLY_HIGH (){ 0x04 } |
48 |
sub MOVE_FLYING (){ 0x06 } |
49 |
sub MOVE_SWIM (){ 0x08 } |
50 |
sub MOVE_BOAT (){ 0x10 } |
51 |
sub MOVE_ALL (){ 0xff } |
52 |
|
53 |
sub load_ref($) { |
54 |
my ($path) = @_; |
55 |
|
56 |
open my $fh, "<", $path |
57 |
or die "$path: $!"; |
58 |
binmode $fh; |
59 |
local $/; |
60 |
|
61 |
Storable::thaw <$fh> |
62 |
} |
63 |
|
64 |
sub save_ref($$) { |
65 |
my ($ref, $path) = @_; |
66 |
|
67 |
open my $fh, ">", "$path~" |
68 |
or die "$path~: $!"; |
69 |
binmode $fh; |
70 |
print $fh Storable::freeze $ref; |
71 |
close $fh; |
72 |
rename "$path~", $path |
73 |
or die "$path: $!"; |
74 |
} |
75 |
|
76 |
sub normalize_arch($) { |
77 |
my ($ob) = @_; |
78 |
|
79 |
my $arch = $ARCH{$ob->{_name}} |
80 |
or (warn "$ob->{_name}: no such archetype", return $ob); |
81 |
|
82 |
delete $ob->{$_} for qw( |
83 |
can_knockback can_parry can_impale can_cut can_dam_armour |
84 |
can_apply pass_thru can_pass_thru |
85 |
); |
86 |
|
87 |
if ($arch->{type} == 22) { # map |
88 |
my %normalize = ( |
89 |
"enter_x" => "hp", |
90 |
"enter_y" => "sp", |
91 |
"width" => "x", |
92 |
"height" => "y", |
93 |
"reset_timeout" => "weight", |
94 |
"swap_time" => "value", |
95 |
"difficulty" => "level", |
96 |
"darkness" => "invisible", |
97 |
"fixed_resettime" => "stand_still", |
98 |
); |
99 |
|
100 |
while (my ($k2, $k1) = each %normalize) { |
101 |
if (defined (my $v = delete $ob->{$k1})) { |
102 |
$ob->{$k2} = $v; |
103 |
} |
104 |
} |
105 |
} |
106 |
|
107 |
for my $attr (qw(move_type move_block move_allow move_on move_off move_slow)) { |
108 |
next unless exists $ob->{$attr}; |
109 |
next if $ob->{$attr} =~ /^\d+$/; |
110 |
|
111 |
my $flags = 0; |
112 |
|
113 |
# assume list |
114 |
for my $flag (map lc, split /\s+/, $ob->{$attr}) { |
115 |
$flags |= MOVE_WALK if $flag eq "walk"; |
116 |
$flags |= MOVE_FLY_LOW if $flag eq "fly_low"; |
117 |
$flags |= MOVE_FLY_HIGH if $flag eq "fly_high"; |
118 |
$flags |= MOVE_FLYING if $flag eq "flying"; |
119 |
$flags |= MOVE_SWIM if $flag eq "swim"; |
120 |
$flags |= MOVE_BOAT if $flag eq "boat"; |
121 |
$flags |= MOVE_ALL if $flag eq "all"; |
122 |
|
123 |
$flags &= ~MOVE_WALK if $flag eq "-walk"; |
124 |
$flags &= ~MOVE_FLY_LOW if $flag eq "-fly_low"; |
125 |
$flags &= ~MOVE_FLY_HIGH if $flag eq "-fly_high"; |
126 |
$flags &= ~MOVE_FLYING if $flag eq "-flying"; |
127 |
$flags &= ~MOVE_SWIM if $flag eq "-swim"; |
128 |
$flags &= ~MOVE_BOAT if $flag eq "-boat"; |
129 |
$flags &= ~MOVE_ALL if $flag eq "-all"; |
130 |
} |
131 |
|
132 |
$ob->{$attr} = $flags; |
133 |
} |
134 |
|
135 |
if (defined (my $v = delete $ob->{no_pass})) { |
136 |
$ob->{move_block} = $v ? MOVE_ALL : 0; |
137 |
} |
138 |
if (defined (my $v = delete $ob->{slow_move})) { |
139 |
$ob->{move_slow} |= MOVE_WALK; |
140 |
$ob->{move_slow_penalty} = $v; |
141 |
} |
142 |
if (defined (my $v = delete $ob->{walk_on})) { |
143 |
$ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
144 |
: $ob->{move_on} & ~MOVE_WALK; |
145 |
} |
146 |
if (defined (my $v = delete $ob->{walk_off})) { |
147 |
$ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
148 |
: $ob->{move_off} & ~MOVE_WALK; |
149 |
} |
150 |
if (defined (my $v = delete $ob->{fly_on})) { |
151 |
$ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
152 |
: $ob->{move_on} & ~MOVE_FLY_LOW; |
153 |
} |
154 |
if (defined (my $v = delete $ob->{fly_off})) { |
155 |
$ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
156 |
: $ob->{move_off} & ~MOVE_FLY_LOW; |
157 |
} |
158 |
if (defined (my $v = delete $ob->{flying})) { |
159 |
$ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
160 |
: $ob->{move_type} & ~MOVE_FLY_LOW; |
161 |
} |
162 |
|
163 |
# if value matches archetype default, delete |
164 |
while (my ($k, $v) = each %$ob) { |
165 |
if (exists $arch->{$k} and $arch->{$k} eq $v) { |
166 |
next if $k eq "_name"; |
167 |
delete $ob->{$k}; |
168 |
} |
169 |
} |
170 |
|
171 |
$ob |
172 |
} |
173 |
|
174 |
sub read_pak($) { |
175 |
my ($path) = @_; |
176 |
|
177 |
my %pak; |
178 |
|
179 |
open my $fh, "<", $path |
180 |
or Carp::croak "$_[0]: $!"; |
181 |
binmode $fh; |
182 |
while (<$fh>) { |
183 |
my ($type, $id, $len, $path) = split; |
184 |
$path =~ s/.*\///; |
185 |
read $fh, $pak{$path}, $len; |
186 |
} |
187 |
|
188 |
\%pak |
189 |
} |
190 |
|
191 |
sub read_arch($) { |
192 |
my ($path) = @_; |
193 |
|
194 |
my %arc; |
195 |
my ($more, $prev); |
196 |
|
197 |
open my $fh, "<", $path |
198 |
or Carp::croak "$path: $!"; |
199 |
|
200 |
binmode $fh; |
201 |
|
202 |
my $parse_block; $parse_block = sub { |
203 |
my %arc = @_; |
204 |
|
205 |
while (<$fh>) { |
206 |
s/\s+$//; |
207 |
if (/^end$/i) { |
208 |
last; |
209 |
} elsif (/^arch (\S+)$/) { |
210 |
push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
211 |
} elsif (/^lore$/) { |
212 |
while (<$fh>) { |
213 |
last if /^endlore\s*$/i; |
214 |
$arc{lore} .= $_; |
215 |
} |
216 |
} elsif (/^msg$/) { |
217 |
while (<$fh>) { |
218 |
last if /^endmsg\s*$/i; |
219 |
$arc{msg} .= $_; |
220 |
} |
221 |
} elsif (/^(\S+)\s*(.*)$/) { |
222 |
$arc{lc $1} = $2; |
223 |
} elsif (/^\s*($|#)/) { |
224 |
# |
225 |
} else { |
226 |
warn "$path: unparsable line '$_' in arch $arc{_name}"; |
227 |
} |
228 |
} |
229 |
|
230 |
\%arc |
231 |
}; |
232 |
|
233 |
while (<$fh>) { |
234 |
s/\s+$//; |
235 |
if (/^more$/i) { |
236 |
$more = $prev; |
237 |
} elsif (/^object (\S+)$/i) { |
238 |
my $name = $1; |
239 |
my $arc = $parse_block->(_name => $name); |
240 |
|
241 |
if ($more) { |
242 |
$more->{more} = $arc; |
243 |
} else { |
244 |
$arc{$name} = $arc; |
245 |
} |
246 |
$prev = $arc; |
247 |
$more = undef; |
248 |
} elsif (/^arch (\S+)$/i) { |
249 |
push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); |
250 |
} elsif (/^\s*($|#)/) { |
251 |
# |
252 |
} else { |
253 |
warn "$path: unparseable top-level line '$_'"; |
254 |
} |
255 |
} |
256 |
|
257 |
undef $parse_block; # work around bug in perl not freeing $fh etc. |
258 |
|
259 |
\%arc |
260 |
} |
261 |
|
262 |
# put all archs into a hash with editor_face as it's key |
263 |
# NOTE: the arrays in the hash values are references to |
264 |
# the archs from $ARCH |
265 |
sub editor_archs { |
266 |
my %paths; |
267 |
|
268 |
for (keys %ARCH) { |
269 |
my $arch = $ARCH{$_}; |
270 |
push @{$paths{$arch->{editor_folder}}}, \$arch; |
271 |
} |
272 |
|
273 |
\%paths |
274 |
} |
275 |
|
276 |
=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch |
277 |
|
278 |
arch_extents determines the extents of the given arch's face(s), linked |
279 |
faces and single faces are handled here it returns (minx, miny, maxx, |
280 |
maxy) |
281 |
|
282 |
=cut |
283 |
|
284 |
sub arch_extents { |
285 |
my ($a) = @_; |
286 |
|
287 |
my $o = $ARCH{$a->{_name}} |
288 |
or return; |
289 |
|
290 |
my $face = $FACE{$a->{face} || $o->{face} || "blank.111"} |
291 |
or (warn "no face data found for arch '$a->{_name}'"), return; |
292 |
|
293 |
if ($face->{w} > 1 || $face->{h} > 1) { |
294 |
# bigface |
295 |
return (0, 0, $face->{w} - 1, $face->{h} - 1); |
296 |
|
297 |
} elsif ($o->{more}) { |
298 |
# linked face |
299 |
my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2; |
300 |
|
301 |
for (; $o; $o = $o->{more}) { |
302 |
$minx = min $minx, $o->{x}; |
303 |
$miny = min $miny, $o->{y}; |
304 |
$maxx = max $maxx, $o->{x}; |
305 |
$maxy = max $maxy, $o->{y}; |
306 |
} |
307 |
|
308 |
return ($minx, $miny, $maxx, $maxy); |
309 |
|
310 |
} else { |
311 |
# single face |
312 |
return (0, 0, 0, 0); |
313 |
} |
314 |
} |
315 |
|
316 |
=item $type = arch_attr $arch |
317 |
|
318 |
Returns a hashref describing the object and its attributes. It can contain |
319 |
the following keys: |
320 |
|
321 |
name the name, suitable for display purposes |
322 |
ignore |
323 |
attr |
324 |
desc |
325 |
use |
326 |
section => [name => \%attr, name => \%attr] |
327 |
import |
328 |
|
329 |
=cut |
330 |
|
331 |
sub arch_attr($) { |
332 |
my ($obj) = @_; |
333 |
|
334 |
require Crossfire::Data; |
335 |
|
336 |
my $root; |
337 |
my $attr = { }; |
338 |
|
339 |
my $arch = $ARCH{ $obj->{_name} }; |
340 |
my $type = $obj->{type} || $arch->{type}; |
341 |
|
342 |
if ($type > 0) { |
343 |
$root = $Crossfire::Data::ATTR{$type}; |
344 |
} else { |
345 |
$root = $Crossfire::Data::TYPE{Misc}; |
346 |
|
347 |
type: |
348 |
for (@Crossfire::Data::ATTR0) { |
349 |
my $req = $_->{required} |
350 |
or die "internal error: ATTR0 without 'required'"; |
351 |
|
352 |
keys %$req; |
353 |
while (my ($k, $v) = each %$req) { |
354 |
next type |
355 |
unless $obj->{$k} == $v || $arch->{$k} == $v; |
356 |
} |
357 |
|
358 |
$root = $_; |
359 |
} |
360 |
} |
361 |
|
362 |
my @import = ($root); |
363 |
|
364 |
unshift @import, \%Crossfire::Data::DEFAULT_ATTR |
365 |
unless $type == 116; |
366 |
|
367 |
my (%ignore); |
368 |
my (@section_order, %section, @attr_order); |
369 |
|
370 |
while (my $type = shift @import) { |
371 |
push @import, @{$type->{import} || []}; |
372 |
|
373 |
$attr->{$_} ||= $type->{$_} |
374 |
for qw(name desc use); |
375 |
|
376 |
for (@{$type->{ignore} || []}) { |
377 |
$ignore{$_}++ for ref $_ ? @$_ : $_; |
378 |
} |
379 |
|
380 |
for ([general => ($type->{attr} || [])], @{$type->{section} || []}) { |
381 |
my ($name, $attr) = @$_; |
382 |
push @section_order, $name; |
383 |
for (@$attr) { |
384 |
my ($k, $v) = @$_; |
385 |
push @attr_order, $k; |
386 |
$section{$name}{$k} ||= $v; |
387 |
} |
388 |
} |
389 |
} |
390 |
|
391 |
$attr->{section} = [ |
392 |
map !exists $section{$_} ? () : do { |
393 |
my $attr = delete $section{$_}; |
394 |
|
395 |
[ |
396 |
$_, |
397 |
map exists $attr->{$_} && !$ignore{$_} |
398 |
? [$_ => delete $attr->{$_}] : (), |
399 |
@attr_order |
400 |
] |
401 |
}, |
402 |
|
403 |
exists $section{$_} ? [$_ => delete $section{$_}] : (), |
404 |
@section_order |
405 |
]; |
406 |
|
407 |
$attr |
408 |
} |
409 |
|
410 |
sub arch_edit_sections { |
411 |
# if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
412 |
# edit_type = 0; |
413 |
# else if (edit_type != 0) { |
414 |
# // all flags from 'check_type' must be unset in this arch because they get recalculated now |
415 |
# edit_type &= ~check_type; |
416 |
# } |
417 |
# |
418 |
# } |
419 |
# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 && |
420 |
# getAttributeValue("alive", defarch) == 1 && |
421 |
# (getAttributeValue("monster", defarch) == 1 || |
422 |
# getAttributeValue("generator", defarch) == 1)) { |
423 |
# // Monster: monsters/npcs/generators |
424 |
# edit_type |= IGUIConstants.TILE_EDIT_MONSTER; |
425 |
# } |
426 |
# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 && |
427 |
# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) { |
428 |
# // Walls |
429 |
# edit_type |= IGUIConstants.TILE_EDIT_WALL; |
430 |
# } |
431 |
# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 && |
432 |
# getAttributeValue("connected", defarch) != 0) { |
433 |
# // Connected Objects |
434 |
# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED; |
435 |
# } |
436 |
# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 && |
437 |
# arch_type == 66 || arch_type == 41 || arch_type == 95) { |
438 |
# // Exit: teleporter/exit/trapdoors |
439 |
# edit_type |= IGUIConstants.TILE_EDIT_EXIT; |
440 |
# } |
441 |
# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 && |
442 |
# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 || |
443 |
# arch_type == 5 || arch_type == 36 || arch_type == 60 || |
444 |
# arch_type == 85 || arch_type == 111 || arch_type == 123 || |
445 |
# arch_type == 124 || arch_type == 130)) { |
446 |
# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls |
447 |
# edit_type |= IGUIConstants.TILE_EDIT_TREASURE; |
448 |
# } |
449 |
# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 && |
450 |
# arch_type == 20 || arch_type == 23 || arch_type == 26 || |
451 |
# arch_type == 91 || arch_type == 21 || arch_type == 24) { |
452 |
# // Door: door/special door/gates + keys |
453 |
# edit_type |= IGUIConstants.TILE_EDIT_DOOR; |
454 |
# } |
455 |
# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 && |
456 |
# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 && |
457 |
# arch_type <= 16) || arch_type == 33 || arch_type == 34 || |
458 |
# arch_type == 35 || arch_type == 39 || arch_type == 70 || |
459 |
# arch_type == 87 || arch_type == 99 || arch_type == 100 || |
460 |
# arch_type == 104 || arch_type == 109 || arch_type == 113 || |
461 |
# arch_type == 122 || arch_type == 3)) { |
462 |
# // Equipment: weapons/armour/wands/rods |
463 |
# edit_type |= IGUIConstants.TILE_EDIT_EQUIP; |
464 |
# } |
465 |
# |
466 |
# return(edit_type); |
467 |
# |
468 |
# |
469 |
} |
470 |
|
471 |
sub cache_file($$&&) { |
472 |
my ($src, $cache, $load, $create) = @_; |
473 |
|
474 |
my ($size, $mtime) = (stat $src)[7,9] |
475 |
or Carp::croak "$src: $!"; |
476 |
|
477 |
if (-e $cache) { |
478 |
my $ref = eval { load_ref $cache }; |
479 |
|
480 |
if ($ref->{version} == 1 |
481 |
&& $ref->{size} == $size |
482 |
&& $ref->{mtime} == $mtime |
483 |
&& eval { $load->($ref->{data}); 1 }) { |
484 |
return; |
485 |
} |
486 |
} |
487 |
|
488 |
my $ref = { |
489 |
version => 1, |
490 |
size => $size, |
491 |
mtime => $mtime, |
492 |
data => $create->(), |
493 |
}; |
494 |
|
495 |
$load->($ref->{data}); |
496 |
|
497 |
save_ref $ref, $cache; |
498 |
} |
499 |
|
500 |
=item set_libdir $path |
501 |
|
502 |
Sets the library directory to the given path |
503 |
(default: $ENV{CROSSFIRE_LIBDIR}). |
504 |
|
505 |
You have to (re-)load the archetypes and tilecache manually after steting |
506 |
the library path. |
507 |
|
508 |
=cut |
509 |
|
510 |
sub set_libdir($) { |
511 |
$LIB = $_[0]; |
512 |
} |
513 |
|
514 |
=item load_archetypes |
515 |
|
516 |
(Re-)Load archetypes into %ARCH. |
517 |
|
518 |
=cut |
519 |
|
520 |
sub load_archetypes() { |
521 |
cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub { |
522 |
*ARCH = $_[0]; |
523 |
}, sub { |
524 |
read_arch "$LIB/archetypes" |
525 |
}; |
526 |
} |
527 |
|
528 |
=item load_tilecache |
529 |
|
530 |
(Re-)Load %TILE and %FACE. |
531 |
|
532 |
=cut |
533 |
|
534 |
sub load_tilecache() { |
535 |
require Gtk2; |
536 |
|
537 |
cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub { |
538 |
$TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png" |
539 |
or die "$VARDIR/tilecache.png: $!"; |
540 |
*FACE = $_[0]; |
541 |
}, sub { |
542 |
require File::Temp; |
543 |
|
544 |
my $tile = read_pak "$LIB/crossfire.0"; |
545 |
|
546 |
my %cache; |
547 |
|
548 |
my $idx = 0; |
549 |
|
550 |
for my $name (sort keys %$tile) { |
551 |
my ($fh, $filename) = File::Temp::tempfile (); |
552 |
print $fh $tile->{$name}; |
553 |
close $fh; |
554 |
my $pb = new_from_file Gtk2::Gdk::Pixbuf $filename; |
555 |
unlink $filename; |
556 |
|
557 |
my $tile = $cache{$name} = { |
558 |
pb => $pb, |
559 |
idx => $idx, |
560 |
w => int $pb->get_width / TILESIZE, |
561 |
h => int $pb->get_height / TILESIZE, |
562 |
}; |
563 |
|
564 |
|
565 |
$idx += $tile->{w} * $tile->{h}; |
566 |
} |
567 |
|
568 |
my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64; |
569 |
|
570 |
while (my ($name, $tile) = each %cache) { |
571 |
my $tpb = delete $tile->{pb}; |
572 |
my $ofs = $tile->{idx}; |
573 |
|
574 |
for my $x (0 .. $tile->{w} - 1) { |
575 |
for my $y (0 .. $tile->{h} - 1) { |
576 |
my $idx = $ofs + $x + $y * $tile->{w}; |
577 |
$tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE, |
578 |
$pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64); |
579 |
} |
580 |
} |
581 |
} |
582 |
|
583 |
$pb->save ("$VARDIR/tilecache.png", "png"); |
584 |
|
585 |
\%cache |
586 |
}; |
587 |
} |
588 |
|
589 |
=head1 AUTHOR |
590 |
|
591 |
Marc Lehmann <schmorp@schmorp.de> |
592 |
http://home.schmorp.de/ |
593 |
|
594 |
Robin Redeker <elmex@ta-sa.org> |
595 |
http://www.ta-sa.org/ |
596 |
|
597 |
=cut |
598 |
|
599 |
1 |