… | |
… | |
10 | |
10 | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
|
|
15 | use Carp (); |
15 | use Storable; |
16 | use Storable; |
16 | |
17 | |
17 | #XXX: The map_* procedures scream for a map-object |
18 | #XXX: The map_* procedures scream for a map-object |
18 | |
19 | |
19 | our @EXPORT = |
20 | our @EXPORT = |
… | |
… | |
21 | arch_extends |
22 | arch_extends |
22 | map_get_tile_stack map_push_tile_stack map_pop_tile_stack |
23 | map_get_tile_stack map_push_tile_stack map_pop_tile_stack |
23 | ); |
24 | ); |
24 | |
25 | |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
27 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
27 | |
28 | |
28 | sub TILESIZE (){ 32 } |
29 | sub TILESIZE (){ 32 } |
29 | |
30 | |
30 | our $ARCH; |
31 | our $ARCH; |
|
|
32 | |
|
|
33 | our %FIELD_MULTILINE = ( |
|
|
34 | msg => "endmsg", |
|
|
35 | lore => "endlore", |
|
|
36 | ); |
|
|
37 | |
|
|
38 | # not used yet, maybe alphabetical is ok |
|
|
39 | our @FIELD_ORDER = (qw(name name_pl)); |
|
|
40 | |
|
|
41 | sub MOVE_WALK (){ 0x1 } |
|
|
42 | sub MOVE_FLY_LOW (){ 0x2 } |
|
|
43 | sub MOVE_FLY_HIGH (){ 0x4 } |
|
|
44 | sub MOVE_FLYING (){ 0x6 } |
|
|
45 | sub MOVE_SWIM (){ 0x8 } |
|
|
46 | sub MOVE_ALL (){ 0xf } |
|
|
47 | |
|
|
48 | sub normalize_arch($) { |
|
|
49 | my ($ob) = @_; |
|
|
50 | |
|
|
51 | my $arch = $ARCH->{$ob->{_name}} |
|
|
52 | or (warn "$ob->{_name}: no such archetype", return $ob); |
|
|
53 | |
|
|
54 | delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply); |
|
|
55 | |
|
|
56 | if ($arch->{type} == 22) { # map |
|
|
57 | my %normalize = ( |
|
|
58 | "enter_x" => "hp", |
|
|
59 | "enter_y" => "sp", |
|
|
60 | "width" => "x", |
|
|
61 | "height" => "y", |
|
|
62 | "reset_timeout" => "weight", |
|
|
63 | "swap_time" => "value", |
|
|
64 | "difficulty" => "level", |
|
|
65 | "darkness" => "invisible", |
|
|
66 | "fixed_resettime" => "stand_still", |
|
|
67 | ); |
|
|
68 | |
|
|
69 | while (my ($k2, $k1) = each %normalize) { |
|
|
70 | if (defined (my $v = delete $ob->{$k1})) { |
|
|
71 | $ob->{$k2} = $v; |
|
|
72 | } |
|
|
73 | } |
|
|
74 | } |
|
|
75 | |
|
|
76 | if (defined (my $v = delete $ob->{no_pass})) { |
|
|
77 | $ob->{move_block} = $v ? MOVE_ALL : 0; |
|
|
78 | } |
|
|
79 | if (defined (my $v = delete $ob->{walk_on})) { |
|
|
80 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
|
|
81 | : $ob->{move_on} & ~MOVE_WALK; |
|
|
82 | } |
|
|
83 | if (defined (my $v = delete $ob->{walk_off})) { |
|
|
84 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
|
|
85 | : $ob->{move_off} & ~MOVE_WALK; |
|
|
86 | } |
|
|
87 | if (defined (my $v = delete $ob->{fly_on})) { |
|
|
88 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
|
|
89 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
|
|
90 | } |
|
|
91 | if (defined (my $v = delete $ob->{fly_off})) { |
|
|
92 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
|
|
93 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
|
|
94 | } |
|
|
95 | if (defined (my $v = delete $ob->{flying})) { |
|
|
96 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
|
|
97 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
|
|
98 | } |
|
|
99 | |
|
|
100 | # if value matches archetype default, delete |
|
|
101 | while (my ($k, $v) = each %$ob) { |
|
|
102 | if (exists $arch->{$k} and $arch->{$k} eq $v) { |
|
|
103 | delete $ob->{$k}; |
|
|
104 | } |
|
|
105 | } |
|
|
106 | |
|
|
107 | $ob |
|
|
108 | } |
31 | |
109 | |
32 | sub read_pak($;$) { |
110 | sub read_pak($;$) { |
33 | my ($path, $cache) = @_; |
111 | my ($path, $cache) = @_; |
34 | |
112 | |
35 | eval { |
113 | eval { |
… | |
… | |
38 | && Storable::retrieve $cache |
116 | && Storable::retrieve $cache |
39 | } or do { |
117 | } or do { |
40 | my %pak; |
118 | my %pak; |
41 | |
119 | |
42 | open my $fh, "<:raw", $path |
120 | open my $fh, "<:raw", $path |
43 | or die "$_[0]: $!"; |
121 | or Carp::croak "$_[0]: $!"; |
44 | while (<$fh>) { |
122 | while (<$fh>) { |
45 | my ($type, $id, $len, $path) = split; |
123 | my ($type, $id, $len, $path) = split; |
46 | $path =~ s/.*\///; |
124 | $path =~ s/.*\///; |
47 | read $fh, $pak{$path}, $len; |
125 | read $fh, $pak{$path}, $len; |
48 | } |
126 | } |
… | |
… | |
64 | } or do { |
142 | } or do { |
65 | my %arc; |
143 | my %arc; |
66 | my ($more, $prev); |
144 | my ($more, $prev); |
67 | |
145 | |
68 | open my $fh, "<:raw", $path |
146 | open my $fh, "<:raw", $path |
69 | or die "$path: $!"; |
147 | or Carp::croak "$path: $!"; |
70 | |
148 | |
71 | my $parse_block; $parse_block = sub { |
149 | my $parse_block; $parse_block = sub { |
72 | my %arc = @_; |
150 | my %arc = @_; |
73 | |
151 | |
74 | while (<$fh>) { |
152 | while (<$fh>) { |
75 | s/\s+$//; |
153 | s/\s+$//; |
76 | if (/^end$/i) { |
154 | if (/^end$/i) { |
77 | last; |
155 | last; |
78 | } elsif (/^arch (\S+)$/) { |
156 | } elsif (/^arch (\S+)$/) { |
79 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
157 | push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
80 | } elsif (/^lore$/) { |
158 | } elsif (/^lore$/) { |
81 | while (<$fh>) { |
159 | while (<$fh>) { |
82 | last if /^endlore\s*$/i; |
160 | last if /^endlore\s*$/i; |
83 | $arc{lore} .= $_; |
161 | $arc{lore} .= $_; |
84 | } |
162 | } |
… | |
… | |
113 | $arc{$name} = $arc; |
191 | $arc{$name} = $arc; |
114 | } |
192 | } |
115 | $prev = $arc; |
193 | $prev = $arc; |
116 | $more = undef; |
194 | $more = undef; |
117 | } elsif (/^arch (\S+)$/i) { |
195 | } elsif (/^arch (\S+)$/i) { |
118 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
196 | push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); |
119 | } elsif (/^\s*($|#)/) { |
197 | } elsif (/^\s*($|#)/) { |
120 | # |
198 | # |
121 | } else { |
199 | } else { |
122 | warn "$path: unparseable top-level line '$_'"; |
200 | warn "$path: unparseable top-level line '$_'"; |
123 | } |
201 | } |