… | |
… | |
4 | |
4 | |
5 | =cut |
5 | =cut |
6 | |
6 | |
7 | package Crossfire; |
7 | package Crossfire; |
8 | |
8 | |
9 | our $VERSION = '0.9'; |
9 | our $VERSION = '0.96'; |
10 | |
10 | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
… | |
… | |
33 | JSON::Syck::Dump $_[0] |
33 | JSON::Syck::Dump $_[0] |
34 | } |
34 | } |
35 | |
35 | |
36 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
36 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
37 | |
37 | |
38 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
38 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" |
|
|
39 | : $ENV{AppData} ? "$ENV{APPDATA}/crossfire" |
|
|
40 | : File::Spec->tmpdir . "/crossfire"; |
39 | |
41 | |
40 | mkdir $VARDIR, 0777; |
42 | mkdir $VARDIR, 0777; |
41 | |
43 | |
42 | sub TILESIZE (){ 32 } |
44 | sub TILESIZE (){ 32 } |
43 | |
45 | |
… | |
… | |
193 | |
195 | |
194 | sub _add_resist($$$) { |
196 | sub _add_resist($$$) { |
195 | my ($ob, $mask, $value) = @_; |
197 | my ($ob, $mask, $value) = @_; |
196 | |
198 | |
197 | while (my ($k, $v) = each %attack_mask) { |
199 | while (my ($k, $v) = each %attack_mask) { |
198 | $ob->{"resist_$k"} += $value if $mask & $v; |
200 | $ob->{"resist_$k"} = min 100, max -100, $ob->{"resist_$k"} + $value if $mask & $v; |
199 | } |
201 | } |
200 | } |
202 | } |
|
|
203 | |
|
|
204 | my %MATERIAL = reverse |
|
|
205 | paper => 1, |
|
|
206 | iron => 2, |
|
|
207 | glass => 4, |
|
|
208 | leather => 8, |
|
|
209 | wood => 16, |
|
|
210 | organic => 32, |
|
|
211 | stone => 64, |
|
|
212 | cloth => 128, |
|
|
213 | adamant => 256, |
|
|
214 | liquid => 512, |
|
|
215 | tin => 1024, |
|
|
216 | bone => 2048, |
|
|
217 | ice => 4096, |
|
|
218 | |
|
|
219 | # guesses |
|
|
220 | runestone => 12, |
|
|
221 | bronze => 18, |
|
|
222 | "ancient wood" => 20, |
|
|
223 | glass => 36, |
|
|
224 | marble => 66, |
|
|
225 | ice => 68, |
|
|
226 | stone => 70, |
|
|
227 | stone => 80, |
|
|
228 | cloth => 136, |
|
|
229 | ironwood => 144, |
|
|
230 | blood => 544, |
|
|
231 | ; |
201 | |
232 | |
202 | # object as in "Object xxx", i.e. archetypes |
233 | # object as in "Object xxx", i.e. archetypes |
203 | sub normalize_object($) { |
234 | sub normalize_object($) { |
204 | my ($ob) = @_; |
235 | my ($ob) = @_; |
|
|
236 | |
|
|
237 | # convert material bitset to materialname, if possible |
|
|
238 | if (exists $ob->{material}) { |
|
|
239 | if (!$ob->{material}) { |
|
|
240 | delete $ob->{material}; |
|
|
241 | } elsif (exists $ob->{materialname}) { |
|
|
242 | if ($MATERIAL{$ob->{material}} eq $ob->{materialname}) { |
|
|
243 | delete $ob->{material}; |
|
|
244 | } else { |
|
|
245 | warn "object $ob->{_name} has both materialname ($ob->{materialname}) and material ($ob->{material}) set.\n"; |
|
|
246 | delete $ob->{material}; # assume materilname is more specific and nuke material |
|
|
247 | } |
|
|
248 | } elsif (my $name = $MATERIAL{$ob->{material}}) { |
|
|
249 | delete $ob->{material}; |
|
|
250 | $ob->{materialname} = $name; |
|
|
251 | } else { |
|
|
252 | warn "object $ob->{_name} has unknown material ($ob->{material}) set.\n"; |
|
|
253 | } |
|
|
254 | } |
205 | |
255 | |
206 | # nuke outdated or never supported fields |
256 | # nuke outdated or never supported fields |
207 | delete @$ob{qw( |
257 | delete @$ob{qw( |
208 | can_knockback can_parry can_impale can_cut can_dam_armour |
258 | can_knockback can_parry can_impale can_cut can_dam_armour |
209 | can_apply pass_thru can_pass_thru |
259 | can_apply pass_thru can_pass_thru |
… | |
… | |
288 | slaying => delete $ob->{"event_${event}"}, |
338 | slaying => delete $ob->{"event_${event}"}, |
289 | name => delete $ob->{"event_${event}_options"}, |
339 | name => delete $ob->{"event_${event}_options"}, |
290 | }; |
340 | }; |
291 | } |
341 | } |
292 | } |
342 | } |
|
|
343 | |
|
|
344 | # some archetypes had "+3" instead of the canonical "3", so fix |
|
|
345 | $ob->{dam} *= 1 if exists $ob->{dam}; |
293 | |
346 | |
294 | $ob |
347 | $ob |
295 | } |
348 | } |
296 | |
349 | |
297 | # arch as in "arch xxx", ie.. objects |
350 | # arch as in "arch xxx", ie.. objects |
… | |
… | |
379 | sub read_arch($;$) { |
432 | sub read_arch($;$) { |
380 | my ($path, $toplevel) = @_; |
433 | my ($path, $toplevel) = @_; |
381 | |
434 | |
382 | my %arc; |
435 | my %arc; |
383 | my ($more, $prev); |
436 | my ($more, $prev); |
|
|
437 | my $comment; |
384 | |
438 | |
385 | open my $fh, "<:raw:perlio:utf8", $path |
439 | open my $fh, "<:raw:perlio:utf8", $path |
386 | or Carp::croak "$path: $!"; |
440 | or Carp::croak "$path: $!"; |
387 | |
441 | |
388 | # binmode $fh; |
442 | # binmode $fh; |
… | |
… | |
392 | |
446 | |
393 | while (<$fh>) { |
447 | while (<$fh>) { |
394 | s/\s+$//; |
448 | s/\s+$//; |
395 | if (/^end$/i) { |
449 | if (/^end$/i) { |
396 | last; |
450 | last; |
|
|
451 | |
397 | } elsif (/^arch (\S+)$/i) { |
452 | } elsif (/^arch (\S+)$/i) { |
398 | push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1); |
453 | push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1); |
|
|
454 | |
399 | } elsif (/^lore$/i) { |
455 | } elsif (/^lore$/i) { |
400 | while (<$fh>) { |
456 | while (<$fh>) { |
401 | last if /^endlore\s*$/i; |
457 | last if /^endlore\s*$/i; |
402 | $arc{lore} .= $_; |
458 | $arc{lore} .= $_; |
403 | } |
459 | } |
… | |
… | |
412 | chomp; |
468 | chomp; |
413 | push @{ $arc{anim} }, $_; |
469 | push @{ $arc{anim} }, $_; |
414 | } |
470 | } |
415 | } elsif (/^(\S+)\s*(.*)$/) { |
471 | } elsif (/^(\S+)\s*(.*)$/) { |
416 | $arc{lc $1} = $2; |
472 | $arc{lc $1} = $2; |
417 | } elsif (/^\s*($|#)/) { |
473 | } elsif (/^\s*#/) { |
|
|
474 | $arc{_comment} .= "$_\n"; |
|
|
475 | |
|
|
476 | } elsif (/^\s*$/) { |
418 | # |
477 | # |
419 | } else { |
478 | } else { |
420 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
479 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
421 | } |
480 | } |
422 | } |
481 | } |
… | |
… | |
428 | s/\s+$//; |
487 | s/\s+$//; |
429 | if (/^more$/i) { |
488 | if (/^more$/i) { |
430 | $more = $prev; |
489 | $more = $prev; |
431 | } elsif (/^object (\S+)$/i) { |
490 | } elsif (/^object (\S+)$/i) { |
432 | my $name = $1; |
491 | my $name = $1; |
433 | my $arc = attr_thaw normalize_object $parse_block->(_name => $name); |
492 | my $arc = attr_thaw normalize_object $parse_block->(_name => $name, _comment => $comment); |
|
|
493 | undef $comment; |
|
|
494 | delete $arc{_comment} unless length $arc{_comment}; |
434 | $arc->{_atype} = 'object'; |
495 | $arc->{_atype} = 'object'; |
435 | |
496 | |
436 | if ($more) { |
497 | if ($more) { |
437 | $more->{more} = $arc; |
498 | $more->{more} = $arc; |
438 | } else { |
499 | } else { |
… | |
… | |
440 | } |
501 | } |
441 | $prev = $arc; |
502 | $prev = $arc; |
442 | $more = undef; |
503 | $more = undef; |
443 | } elsif (/^arch (\S+)$/i) { |
504 | } elsif (/^arch (\S+)$/i) { |
444 | my $name = $1; |
505 | my $name = $1; |
445 | my $arc = attr_thaw normalize_arch $parse_block->(_name => $name); |
506 | my $arc = attr_thaw normalize_arch $parse_block->(_name => $name, _comment => $comment); |
|
|
507 | undef $comment; |
|
|
508 | delete $arc{_comment} unless length $arc{_comment}; |
446 | $arc->{_atype} = 'arch'; |
509 | $arc->{_atype} = 'arch'; |
447 | |
510 | |
448 | if ($more) { |
511 | if ($more) { |
449 | $more->{more} = $arc; |
512 | $more->{more} = $arc; |
450 | } else { |
513 | } else { |
… | |
… | |
459 | push @{$toplevel->{lev_array}}, $_+0; |
522 | push @{$toplevel->{lev_array}}, $_+0; |
460 | } |
523 | } |
461 | } else { |
524 | } else { |
462 | $toplevel->{$1} = $2; |
525 | $toplevel->{$1} = $2; |
463 | } |
526 | } |
|
|
527 | } elsif (/^\s*#/) { |
|
|
528 | $comment .= "$_\n"; |
464 | } elsif (/^\s*($|#)/) { |
529 | } elsif (/^\s*($|#)/) { |
465 | # |
530 | # |
466 | } else { |
531 | } else { |
467 | die "$path: unparseable top-level line '$_'"; |
532 | die "$path: unparseable top-level line '$_'"; |
468 | } |
533 | } |
… | |
… | |
488 | if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) { |
553 | if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) { |
489 | $a{attack_movement} = (delete $a{attack_movement_bits_0_3}) |
554 | $a{attack_movement} = (delete $a{attack_movement_bits_0_3}) |
490 | | (delete $a{attack_movement_bits_4_7}); |
555 | | (delete $a{attack_movement_bits_4_7}); |
491 | } |
556 | } |
492 | |
557 | |
|
|
558 | if (my $comment = delete $a{_comment}) { |
|
|
559 | if ($comment =~ /[^\n\s#]/) { |
|
|
560 | $str .= $comment; |
|
|
561 | } |
|
|
562 | } |
|
|
563 | |
493 | $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n"; |
564 | $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n"; |
494 | |
565 | |
495 | my $inv = delete $a{inventory}; |
566 | my $inv = delete $a{inventory}; |
496 | my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some |
567 | my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some |
497 | my $anim = delete $a{anim}; |
568 | my $anim = delete $a{anim}; |
498 | |
569 | |
499 | my @kv; |
570 | my @kv; |
500 | |
571 | |
… | |
… | |
556 | if $anim; |
627 | if $anim; |
557 | } |
628 | } |
558 | |
629 | |
559 | $str .= "end\n"; |
630 | $str .= "end\n"; |
560 | |
631 | |
561 | if (($a{_atype} eq 'object') && $more) { |
632 | if ($a{_atype} eq 'object') { |
|
|
633 | if ($more) { |
562 | $str .= "\nmore\n"; |
634 | $str .= "more\n"; |
563 | $append->($more) if $more; |
635 | $append->($more) if $more; |
|
|
636 | } else { |
|
|
637 | $str .= "\n"; |
|
|
638 | } |
564 | } |
639 | } |
565 | }; |
640 | }; |
566 | |
641 | |
567 | for (@$arch) { |
642 | for (@$arch) { |
568 | $append->($_); |
643 | $append->($_); |
… | |
… | |
597 | my ($a) = @_; |
672 | my ($a) = @_; |
598 | |
673 | |
599 | my $o = $ARCH{$a->{_name}} |
674 | my $o = $ARCH{$a->{_name}} |
600 | or return; |
675 | or return; |
601 | |
676 | |
602 | my $face = $FACE{$a->{face} || $o->{face} || "blank.111"} |
677 | my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}; |
|
|
678 | unless ($face) { |
|
|
679 | $face = $FACE{"blank.x11"} |
603 | or (warn "no face data found for arch '$a->{_name}'"), return; |
680 | or (warn "no face data found for arch '$a->{_name}'"), return; |
|
|
681 | } |
604 | |
682 | |
605 | if ($face->{w} > 1 || $face->{h} > 1) { |
683 | if ($face->{w} > 1 || $face->{h} > 1) { |
606 | # bigface |
684 | # bigface |
607 | return (0, 0, $face->{w} - 1, $face->{h} - 1); |
685 | return (0, 0, $face->{w} - 1, $face->{h} - 1); |
608 | |
686 | |