… | |
… | |
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 | glacium => 260, |
|
|
231 | blood => 544, |
|
|
232 | ; |
201 | |
233 | |
202 | # object as in "Object xxx", i.e. archetypes |
234 | # object as in "Object xxx", i.e. archetypes |
203 | sub normalize_object($) { |
235 | sub normalize_object($) { |
204 | my ($ob) = @_; |
236 | my ($ob) = @_; |
|
|
237 | |
|
|
238 | # convert material bitset to materialname, if possible |
|
|
239 | if (exists $ob->{material}) { |
|
|
240 | if (!$ob->{material}) { |
|
|
241 | delete $ob->{material}; |
|
|
242 | } elsif (exists $ob->{materialname}) { |
|
|
243 | if ($MATERIAL{$ob->{material}} eq $ob->{materialname}) { |
|
|
244 | delete $ob->{material}; |
|
|
245 | } else { |
|
|
246 | warn "object $ob->{_name} has both materialname ($ob->{materialname}) and material ($ob->{material}) set.\n"; |
|
|
247 | delete $ob->{material}; # assume materilname is more specific and nuke material |
|
|
248 | } |
|
|
249 | } elsif (my $name = $MATERIAL{$ob->{material}}) { |
|
|
250 | delete $ob->{material}; |
|
|
251 | $ob->{materialname} = $name; |
|
|
252 | } else { |
|
|
253 | warn "object $ob->{_name} has unknown material ($ob->{material}) set.\n"; |
|
|
254 | } |
|
|
255 | } |
205 | |
256 | |
206 | # nuke outdated or never supported fields |
257 | # nuke outdated or never supported fields |
207 | delete @$ob{qw( |
258 | delete @$ob{qw( |
208 | can_knockback can_parry can_impale can_cut can_dam_armour |
259 | can_knockback can_parry can_impale can_cut can_dam_armour |
209 | can_apply pass_thru can_pass_thru |
260 | can_apply pass_thru can_pass_thru |
… | |
… | |
288 | slaying => delete $ob->{"event_${event}"}, |
339 | slaying => delete $ob->{"event_${event}"}, |
289 | name => delete $ob->{"event_${event}_options"}, |
340 | name => delete $ob->{"event_${event}_options"}, |
290 | }; |
341 | }; |
291 | } |
342 | } |
292 | } |
343 | } |
|
|
344 | |
|
|
345 | # some archetypes had "+3" instead of the canonical "3", so fix |
|
|
346 | $ob->{dam} *= 1 if exists $ob->{dam}; |
293 | |
347 | |
294 | $ob |
348 | $ob |
295 | } |
349 | } |
296 | |
350 | |
297 | # arch as in "arch xxx", ie.. objects |
351 | # arch as in "arch xxx", ie.. objects |
… | |
… | |
379 | sub read_arch($;$) { |
433 | sub read_arch($;$) { |
380 | my ($path, $toplevel) = @_; |
434 | my ($path, $toplevel) = @_; |
381 | |
435 | |
382 | my %arc; |
436 | my %arc; |
383 | my ($more, $prev); |
437 | my ($more, $prev); |
|
|
438 | my $comment; |
384 | |
439 | |
385 | open my $fh, "<:raw:perlio:utf8", $path |
440 | open my $fh, "<:raw:perlio:utf8", $path |
386 | or Carp::croak "$path: $!"; |
441 | or Carp::croak "$path: $!"; |
387 | |
442 | |
388 | # binmode $fh; |
443 | # binmode $fh; |
… | |
… | |
392 | |
447 | |
393 | while (<$fh>) { |
448 | while (<$fh>) { |
394 | s/\s+$//; |
449 | s/\s+$//; |
395 | if (/^end$/i) { |
450 | if (/^end$/i) { |
396 | last; |
451 | last; |
|
|
452 | |
397 | } elsif (/^arch (\S+)$/i) { |
453 | } elsif (/^arch (\S+)$/i) { |
398 | push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1); |
454 | push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1); |
|
|
455 | |
399 | } elsif (/^lore$/i) { |
456 | } elsif (/^lore$/i) { |
400 | while (<$fh>) { |
457 | while (<$fh>) { |
401 | last if /^endlore\s*$/i; |
458 | last if /^endlore\s*$/i; |
402 | $arc{lore} .= $_; |
459 | $arc{lore} .= $_; |
403 | } |
460 | } |
… | |
… | |
412 | chomp; |
469 | chomp; |
413 | push @{ $arc{anim} }, $_; |
470 | push @{ $arc{anim} }, $_; |
414 | } |
471 | } |
415 | } elsif (/^(\S+)\s*(.*)$/) { |
472 | } elsif (/^(\S+)\s*(.*)$/) { |
416 | $arc{lc $1} = $2; |
473 | $arc{lc $1} = $2; |
417 | } elsif (/^\s*($|#)/) { |
474 | } elsif (/^\s*#/) { |
|
|
475 | $arc{_comment} .= "$_\n"; |
|
|
476 | |
|
|
477 | } elsif (/^\s*$/) { |
418 | # |
478 | # |
419 | } else { |
479 | } else { |
420 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
480 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
421 | } |
481 | } |
422 | } |
482 | } |
… | |
… | |
428 | s/\s+$//; |
488 | s/\s+$//; |
429 | if (/^more$/i) { |
489 | if (/^more$/i) { |
430 | $more = $prev; |
490 | $more = $prev; |
431 | } elsif (/^object (\S+)$/i) { |
491 | } elsif (/^object (\S+)$/i) { |
432 | my $name = $1; |
492 | my $name = $1; |
433 | my $arc = attr_thaw normalize_object $parse_block->(_name => $name); |
493 | my $arc = attr_thaw normalize_object $parse_block->(_name => $name, _comment => $comment); |
|
|
494 | undef $comment; |
|
|
495 | delete $arc{_comment} unless length $arc{_comment}; |
434 | $arc->{_atype} = 'object'; |
496 | $arc->{_atype} = 'object'; |
435 | |
497 | |
436 | if ($more) { |
498 | if ($more) { |
437 | $more->{more} = $arc; |
499 | $more->{more} = $arc; |
438 | } else { |
500 | } else { |
… | |
… | |
440 | } |
502 | } |
441 | $prev = $arc; |
503 | $prev = $arc; |
442 | $more = undef; |
504 | $more = undef; |
443 | } elsif (/^arch (\S+)$/i) { |
505 | } elsif (/^arch (\S+)$/i) { |
444 | my $name = $1; |
506 | my $name = $1; |
445 | my $arc = attr_thaw normalize_arch $parse_block->(_name => $name); |
507 | my $arc = attr_thaw normalize_arch $parse_block->(_name => $name, _comment => $comment); |
|
|
508 | undef $comment; |
|
|
509 | delete $arc{_comment} unless length $arc{_comment}; |
446 | $arc->{_atype} = 'arch'; |
510 | $arc->{_atype} = 'arch'; |
447 | |
511 | |
448 | if ($more) { |
512 | if ($more) { |
449 | $more->{more} = $arc; |
513 | $more->{more} = $arc; |
450 | } else { |
514 | } else { |
… | |
… | |
459 | push @{$toplevel->{lev_array}}, $_+0; |
523 | push @{$toplevel->{lev_array}}, $_+0; |
460 | } |
524 | } |
461 | } else { |
525 | } else { |
462 | $toplevel->{$1} = $2; |
526 | $toplevel->{$1} = $2; |
463 | } |
527 | } |
|
|
528 | } elsif (/^\s*#/) { |
|
|
529 | $comment .= "$_\n"; |
464 | } elsif (/^\s*($|#)/) { |
530 | } elsif (/^\s*($|#)/) { |
465 | # |
531 | # |
466 | } else { |
532 | } else { |
467 | die "$path: unparseable top-level line '$_'"; |
533 | die "$path: unparseable top-level line '$_'"; |
468 | } |
534 | } |
… | |
… | |
488 | if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) { |
554 | 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}) |
555 | $a{attack_movement} = (delete $a{attack_movement_bits_0_3}) |
490 | | (delete $a{attack_movement_bits_4_7}); |
556 | | (delete $a{attack_movement_bits_4_7}); |
491 | } |
557 | } |
492 | |
558 | |
|
|
559 | if (my $comment = delete $a{_comment}) { |
|
|
560 | if ($comment =~ /[^\n\s#]/) { |
|
|
561 | $str .= $comment; |
|
|
562 | } |
|
|
563 | } |
|
|
564 | |
493 | $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n"; |
565 | $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n"; |
494 | |
566 | |
495 | my $inv = delete $a{inventory}; |
567 | my $inv = delete $a{inventory}; |
496 | my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some |
568 | my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some |
497 | my $anim = delete $a{anim}; |
569 | my $anim = delete $a{anim}; |
498 | |
570 | |
499 | my @kv; |
571 | my @kv; |
500 | |
572 | |
… | |
… | |
556 | if $anim; |
628 | if $anim; |
557 | } |
629 | } |
558 | |
630 | |
559 | $str .= "end\n"; |
631 | $str .= "end\n"; |
560 | |
632 | |
561 | if (($a{_atype} eq 'object') && $more) { |
633 | if ($a{_atype} eq 'object') { |
|
|
634 | if ($more) { |
562 | $str .= "\nmore\n"; |
635 | $str .= "more\n"; |
563 | $append->($more) if $more; |
636 | $append->($more) if $more; |
|
|
637 | } else { |
|
|
638 | $str .= "\n"; |
|
|
639 | } |
564 | } |
640 | } |
565 | }; |
641 | }; |
566 | |
642 | |
567 | for (@$arch) { |
643 | for (@$arch) { |
568 | $append->($_); |
644 | $append->($_); |
… | |
… | |
597 | my ($a) = @_; |
673 | my ($a) = @_; |
598 | |
674 | |
599 | my $o = $ARCH{$a->{_name}} |
675 | my $o = $ARCH{$a->{_name}} |
600 | or return; |
676 | or return; |
601 | |
677 | |
602 | my $face = $FACE{$a->{face} || $o->{face} || "blank.111"} |
678 | my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}; |
|
|
679 | unless ($face) { |
|
|
680 | $face = $FACE{"blank.x11"} |
603 | or (warn "no face data found for arch '$a->{_name}'"), return; |
681 | or (warn "no face data found for arch '$a->{_name}'"), return; |
|
|
682 | } |
604 | |
683 | |
605 | if ($face->{w} > 1 || $face->{h} > 1) { |
684 | if ($face->{w} > 1 || $face->{h} > 1) { |
606 | # bigface |
685 | # bigface |
607 | return (0, 0, $face->{w} - 1, $face->{h} - 1); |
686 | return (0, 0, $face->{w} - 1, $face->{h} - 1); |
608 | |
687 | |