… | |
… | |
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 = |
20 | qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs |
21 | qw(read_pak read_arch $ARCH TILESIZE editor_archs |
21 | arch2pickmap 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 | } |
… | |
… | |
219 | # single face |
297 | # single face |
220 | return (0, 0, 1, 1); |
298 | return (0, 0, 1, 1); |
221 | } |
299 | } |
222 | } |
300 | } |
223 | |
301 | |
224 | # arch2pickmap forms a list of archs to a pickmap |
|
|
225 | sub arch2pickmap { |
|
|
226 | my ($archs, $w) = @_; |
|
|
227 | |
|
|
228 | # sort archs alphabetiacally |
|
|
229 | my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ]; |
|
|
230 | |
|
|
231 | $w ||= 10; # default width |
|
|
232 | my $num = @$archs; |
|
|
233 | my $map = { }; |
|
|
234 | # overall placement coords |
|
|
235 | my $x = 0; |
|
|
236 | my $y = 0; |
|
|
237 | |
|
|
238 | my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later |
|
|
239 | my $drawn_archs = 1; # line-break counter |
|
|
240 | my $max_line_height = 1; |
|
|
241 | |
|
|
242 | for (my $i = 0; $i < $num; $i++) { |
|
|
243 | |
|
|
244 | defined ${$archs->[$i]}->{face} or next; |
|
|
245 | |
|
|
246 | # check whether this tile was already written (see below at (b)) |
|
|
247 | unless (defined $map->{map}[$x][$y]) { |
|
|
248 | |
|
|
249 | my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]}); |
|
|
250 | |
|
|
251 | # these are special placement coords, for chained faces which |
|
|
252 | # have a special placement offset |
|
|
253 | my ($place_x, $place_y) = ($x, $y); |
|
|
254 | $xoffs < 0 and |
|
|
255 | $place_x += -$xoffs; |
|
|
256 | $yoffs < 0 and |
|
|
257 | $place_y += -$yoffs; |
|
|
258 | |
|
|
259 | # iterate over the tiles this arch takes |
|
|
260 | # NOTE: Chained archs are maybe not a rectangle, but i don't care |
|
|
261 | # much for that on pickmaps |
|
|
262 | |
|
|
263 | for (my $xi = 0; $xi < $arch_w; $xi++) { |
|
|
264 | for (my $yi = 0; $yi < $arch_h; $yi++) { |
|
|
265 | |
|
|
266 | my ($lx, $ly) = ($x + $xi, $y + $yi); |
|
|
267 | |
|
|
268 | if ($lx == $place_x and $ly == $place_y) { |
|
|
269 | push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]}; |
|
|
270 | |
|
|
271 | } else { |
|
|
272 | |
|
|
273 | # (b): here we set occupied tiles, but without the arch |
|
|
274 | $map->{map}[$lx][$ly] = []; |
|
|
275 | } |
|
|
276 | } |
|
|
277 | } |
|
|
278 | $drawn_archs++; |
|
|
279 | |
|
|
280 | $x += $arch_w - 1; |
|
|
281 | $max_line_height < $arch_h |
|
|
282 | and $max_line_height = $arch_h; |
|
|
283 | |
|
|
284 | } else { |
|
|
285 | $i--; |
|
|
286 | } |
|
|
287 | |
|
|
288 | |
|
|
289 | $x++; |
|
|
290 | |
|
|
291 | if ($x > $w) { |
|
|
292 | |
|
|
293 | $y += $max_line_height; |
|
|
294 | $max_line_height = 1; |
|
|
295 | $x = 0; |
|
|
296 | } |
|
|
297 | |
|
|
298 | $maxw < ($x + 1) and $maxw = $x + 1; |
|
|
299 | $maxh < ($y + 1) and $maxh = $y + 1; |
|
|
300 | } |
|
|
301 | |
|
|
302 | $map->{height} = $maxh; |
|
|
303 | $map->{width} = $maxw; |
|
|
304 | |
|
|
305 | return $map; |
|
|
306 | } |
|
|
307 | |
|
|
308 | sub arch2map($;$) { |
|
|
309 | my ($mapa) = @_; |
|
|
310 | |
|
|
311 | my %meta; |
|
|
312 | |
|
|
313 | my ($mapx, $mapy); |
|
|
314 | |
|
|
315 | my $map; |
|
|
316 | |
|
|
317 | for (@{ $mapa->{arch} }) { |
|
|
318 | my ($x, $y) = (delete $_->{x}, delete $_->{y}); |
|
|
319 | |
|
|
320 | if ($_->{_name} eq "map") { |
|
|
321 | $meta{info} = $_; |
|
|
322 | |
|
|
323 | $mapx = $_->{width} || $x; |
|
|
324 | $mapy = $_->{height} || $y; |
|
|
325 | } else { |
|
|
326 | push @{ $map->[$x][$y] }, $_; |
|
|
327 | |
|
|
328 | # arch map is unreliable w.r.t. width and height |
|
|
329 | $mapx = $x + 1 if $mapx <= $x; |
|
|
330 | $mapy = $y + 1 if $mapy <= $y; |
|
|
331 | #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
|
|
332 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
|
|
333 | } |
|
|
334 | } |
|
|
335 | |
|
|
336 | $meta{width} = $mapx; |
|
|
337 | $meta{height} = $mapy; |
|
|
338 | $meta{map} = $map; |
|
|
339 | |
|
|
340 | \%meta |
|
|
341 | } |
|
|
342 | |
|
|
343 | sub init($) { |
302 | sub init($) { |
344 | my ($cachedir) = @_; |
303 | my ($cachedir) = @_; |
345 | |
304 | |
346 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
305 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
347 | } |
306 | } |