… | |
… | |
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 | our @EXPORT = qw(read_pak read_arch arch2map $ARCH TILESIZE); |
18 | #XXX: The map_* procedures scream for a map-object |
|
|
19 | |
|
|
20 | our @EXPORT = |
|
|
21 | qw(read_pak read_arch $ARCH TILESIZE editor_archs |
|
|
22 | arch_extends |
|
|
23 | map_get_tile_stack map_push_tile_stack map_pop_tile_stack |
|
|
24 | ); |
18 | |
25 | |
19 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
20 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
27 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
21 | |
28 | |
22 | sub TILESIZE (){ 32 } |
29 | sub TILESIZE (){ 32 } |
23 | |
30 | |
24 | our $ARCH; |
31 | our $ARCH; |
|
|
32 | |
|
|
33 | our %FIELD_MULTILINE = ( |
|
|
34 | msg => "endmsg", |
|
|
35 | ); |
|
|
36 | |
|
|
37 | # not used yet, maybe alphabetical is ok |
|
|
38 | our @FIELD_ORDER = (qw(name name_pl)); |
|
|
39 | |
|
|
40 | # not used yet, AND NOT CHECKED, should also be BY TYPE |
|
|
41 | our %FIELD_NORMALIZE = ( |
|
|
42 | "enter_x" => "hp", |
|
|
43 | "enter_y" => "sp", |
|
|
44 | ); |
25 | |
45 | |
26 | sub read_pak($;$) { |
46 | sub read_pak($;$) { |
27 | my ($path, $cache) = @_; |
47 | my ($path, $cache) = @_; |
28 | |
48 | |
29 | eval { |
49 | eval { |
… | |
… | |
32 | && Storable::retrieve $cache |
52 | && Storable::retrieve $cache |
33 | } or do { |
53 | } or do { |
34 | my %pak; |
54 | my %pak; |
35 | |
55 | |
36 | open my $fh, "<:raw", $path |
56 | open my $fh, "<:raw", $path |
37 | or die "$_[0]: $!"; |
57 | or Carp::croak "$_[0]: $!"; |
38 | while (<$fh>) { |
58 | while (<$fh>) { |
39 | my ($type, $id, $len, $path) = split; |
59 | my ($type, $id, $len, $path) = split; |
40 | $path =~ s/.*\///; |
60 | $path =~ s/.*\///; |
41 | read $fh, $pak{$path}, $len; |
61 | read $fh, $pak{$path}, $len; |
42 | } |
62 | } |
… | |
… | |
58 | } or do { |
78 | } or do { |
59 | my %arc; |
79 | my %arc; |
60 | my ($more, $prev); |
80 | my ($more, $prev); |
61 | |
81 | |
62 | open my $fh, "<:raw", $path |
82 | open my $fh, "<:raw", $path |
63 | or die "$path: $!"; |
83 | or Carp::croak "$path: $!"; |
64 | |
84 | |
65 | my $parse_block; $parse_block = sub { |
85 | my $parse_block; $parse_block = sub { |
66 | my %arc = @_; |
86 | my %arc = @_; |
67 | |
87 | |
68 | while (<$fh>) { |
88 | while (<$fh>) { |
… | |
… | |
124 | |
144 | |
125 | \%arc |
145 | \%arc |
126 | } |
146 | } |
127 | } |
147 | } |
128 | |
148 | |
129 | sub arch2map($;$) { |
149 | # returns the arch/object stack from a tile on a map |
|
|
150 | sub map_get_tile_stack { |
|
|
151 | my ($map, $x, $y) = @_; |
|
|
152 | my $as; |
|
|
153 | |
|
|
154 | if ($x > 0 || $x < $map->{width} |
|
|
155 | || $y > 0 || $y < $map->{height}) { |
|
|
156 | |
|
|
157 | $as = $map->{map}{map}[$x][$y] || []; |
|
|
158 | } |
|
|
159 | |
|
|
160 | return $as; |
|
|
161 | } |
|
|
162 | |
|
|
163 | # pop the topmost arch/object from the stack of a tile on a map |
|
|
164 | sub map_pop_tile_stack { |
|
|
165 | my ($map, $x, $y) = @_; |
|
|
166 | |
|
|
167 | if ($x > 0 || $x < $map->{width} |
|
|
168 | || $y > 0 || $y < $map->{height}) { |
|
|
169 | |
|
|
170 | pop @{$map->{map}{map}[$x][$y]}; |
|
|
171 | } |
|
|
172 | } |
|
|
173 | |
|
|
174 | # pushes the arch/object on the stack of a tile on a map |
|
|
175 | sub map_push_tile_stack { |
|
|
176 | my ($map, $x, $y, $arch) = @_; |
|
|
177 | |
|
|
178 | if ($x > 0 || $x < $map->{width} |
|
|
179 | || $y > 0 || $y < $map->{height}) { |
|
|
180 | |
|
|
181 | push @{$map->{map}{map}[$x][$y]}, $arch; |
|
|
182 | } |
|
|
183 | } |
|
|
184 | |
|
|
185 | |
|
|
186 | # put all archs into a hash with editor_face as it's key |
|
|
187 | # NOTE: the arrays in the hash values are references to |
|
|
188 | # the archs from $ARCH |
|
|
189 | sub editor_archs { |
|
|
190 | my %paths; |
|
|
191 | |
|
|
192 | for (keys %$ARCH) { |
|
|
193 | my $arch = $ARCH->{$_}; |
|
|
194 | push @{$paths{$arch->{editor_folder}}}, \$arch; |
|
|
195 | } |
|
|
196 | |
|
|
197 | return \%paths; |
|
|
198 | } |
|
|
199 | |
|
|
200 | # arch_extends determines how the arch looks like on the map, |
|
|
201 | # bigfaces, linked faces and single faces are handled here |
|
|
202 | # it returns (<xoffset>, <yoffset>, <width>, <height>) |
|
|
203 | # NOTE: non rectangular linked faces are not considered |
|
|
204 | sub arch_extends { |
130 | my ($mapa) = @_; |
205 | my ($a) = @_; |
131 | |
206 | |
132 | my %meta; |
207 | my $TC = \%Crossfire::Tilecache::TILECACHE; |
133 | |
208 | |
134 | my ($mapx, $mapy); |
209 | my $facename = |
|
|
210 | $a->{face} || $ARCH->{$a->{_name}}->{face} |
|
|
211 | or return (); |
135 | |
212 | |
136 | my $map; |
213 | my $tile = $TC->{$facename} |
|
|
214 | or (warn "no gfx found for arch '$facename' in arch_size ()"), return; |
137 | |
215 | |
138 | for (@{ $mapa->{arch} }) { |
216 | if ($tile->{w} > 1 || $tile->{h} > 1) { |
139 | my ($x, $y) = ($_->{x}, $_->{y}); |
217 | # bigfaces |
|
|
218 | return (0, 0, $tile->{w}, $tile->{h}); |
140 | |
219 | |
141 | if ($_->{_name} eq "map") { |
220 | } elsif ($a->{more}) { |
142 | $meta{info} = $_; |
221 | # linked faces |
|
|
222 | my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); |
|
|
223 | do { |
|
|
224 | $miw > (0 + $a->{x}) and $miw = $a->{x}; |
|
|
225 | $mih > (0 + $a->{y}) and $mih = $a->{y}; |
|
|
226 | $maw < (0 + $a->{x}) and $maw = $a->{x}; |
|
|
227 | $mah < (0 + $a->{y}) and $mah = $a->{y}; |
|
|
228 | } while $a = $a->{more}; |
143 | |
229 | |
144 | $mapx = $_->{width} || $x; |
230 | return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) |
145 | $mapy = $_->{height} || $y; |
231 | |
146 | } else { |
232 | } else { |
147 | push @{ $map->[$x][$y] }, $_; |
233 | # single face |
148 | |
234 | return (0, 0, 1, 1); |
149 | # arch map is unreliable w.r.t. width and height |
|
|
150 | $mapx = $x + 1 if $mapx <= $x; |
|
|
151 | $mapy = $y + 1 if $mapy <= $y; |
|
|
152 | #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
|
|
153 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
|
|
154 | } |
|
|
155 | } |
235 | } |
156 | |
|
|
157 | $meta{width} = $mapx; |
|
|
158 | $meta{height} = $mapy; |
|
|
159 | $meta{map} = $map; |
|
|
160 | |
|
|
161 | \%meta |
|
|
162 | } |
236 | } |
163 | |
237 | |
164 | sub init($) { |
238 | sub init($) { |
165 | my ($cachedir) = @_; |
239 | my ($cachedir) = @_; |
166 | |
240 | |