… | |
… | |
4 | |
4 | |
5 | =cut |
5 | =cut |
6 | |
6 | |
7 | package Crossfire; |
7 | package Crossfire; |
8 | |
8 | |
9 | our $VERSION = '0.1'; |
9 | our $VERSION = '0.9'; |
10 | |
10 | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
… | |
… | |
18 | use Storable qw(freeze thaw); |
18 | use Storable qw(freeze thaw); |
19 | |
19 | |
20 | our @EXPORT = qw( |
20 | our @EXPORT = qw( |
21 | read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
21 | read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
22 | ); |
22 | ); |
|
|
23 | |
|
|
24 | use JSON::Syck (); #TODO#d# replace by JSON::PC when it becomes available == working |
|
|
25 | |
|
|
26 | sub from_json($) { |
|
|
27 | $JSON::Syck::ImplicitUnicode = 1; |
|
|
28 | JSON::Syck::Load $_[0] |
|
|
29 | } |
|
|
30 | |
|
|
31 | sub to_json($) { |
|
|
32 | $JSON::Syck::ImplicitUnicode = 0; |
|
|
33 | JSON::Syck::Dump $_[0] |
|
|
34 | } |
23 | |
35 | |
24 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
36 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
25 | |
37 | |
26 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
38 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
27 | |
39 | |
… | |
… | |
44 | qw(move_type move_block move_allow move_on move_off move_slow); |
56 | qw(move_type move_block move_allow move_on move_off move_slow); |
45 | |
57 | |
46 | # same as in server save routine, to (hopefully) be compatible |
58 | # same as in server save routine, to (hopefully) be compatible |
47 | # to the other editors. |
59 | # to the other editors. |
48 | our @FIELD_ORDER_MAP = (qw( |
60 | our @FIELD_ORDER_MAP = (qw( |
49 | name swap_time reset_timeout fixed_resettime difficulty region |
61 | name attach swap_time reset_timeout fixed_resettime difficulty region |
50 | shopitems shopgreed shopmin shopmax shoprace |
62 | shopitems shopgreed shopmin shopmax shoprace |
51 | darkness width height enter_x enter_y msg maplore |
63 | darkness width height enter_x enter_y msg maplore |
52 | unique template |
64 | unique template |
53 | outdoor temp pressure humid windspeed winddir sky nosmooth |
65 | outdoor temp pressure humid windspeed winddir sky nosmooth |
54 | tile_path_1 tile_path_2 tile_path_3 tile_path_4 |
66 | tile_path_1 tile_path_2 tile_path_3 tile_path_4 |
55 | )); |
67 | )); |
56 | |
68 | |
57 | our @FIELD_ORDER = (qw( |
69 | our @FIELD_ORDER = (qw( |
58 | elevation |
70 | elevation |
59 | |
71 | |
60 | name name_pl custom_name title race |
72 | name name_pl custom_name attach title race |
61 | slaying skill msg lore other_arch face |
73 | slaying skill msg lore other_arch face |
62 | #todo-events |
74 | #todo-events |
63 | animation is_animated |
75 | animation is_animated |
64 | str dex con wis pow cha int |
76 | str dex con wis pow cha int |
65 | hp maxhp sp maxsp grace maxgrace |
77 | hp maxhp sp maxsp grace maxgrace |
… | |
… | |
130 | sub MOVE_ALL (){ 0x1001f } # very special value, more PITA |
142 | sub MOVE_ALL (){ 0x1001f } # very special value, more PITA |
131 | |
143 | |
132 | sub load_ref($) { |
144 | sub load_ref($) { |
133 | my ($path) = @_; |
145 | my ($path) = @_; |
134 | |
146 | |
135 | open my $fh, "<", $path |
147 | open my $fh, "<:raw:perlio", $path |
136 | or die "$path: $!"; |
148 | or die "$path: $!"; |
137 | binmode $fh; |
|
|
138 | local $/; |
149 | local $/; |
139 | |
150 | |
140 | thaw <$fh> |
151 | thaw <$fh> |
141 | } |
152 | } |
142 | |
153 | |
143 | sub save_ref($$) { |
154 | sub save_ref($$) { |
144 | my ($ref, $path) = @_; |
155 | my ($ref, $path) = @_; |
145 | |
156 | |
146 | open my $fh, ">", "$path~" |
157 | open my $fh, ">:raw:perlio", "$path~" |
147 | or die "$path~: $!"; |
158 | or die "$path~: $!"; |
148 | binmode $fh; |
|
|
149 | print $fh freeze $ref; |
159 | print $fh freeze $ref; |
150 | close $fh; |
160 | close $fh; |
151 | rename "$path~", $path |
161 | rename "$path~", $path |
152 | or die "$path: $!"; |
162 | or die "$path: $!"; |
153 | } |
163 | } |
… | |
… | |
201 | if (defined (my $v = delete $ob->{slow_move})) { |
211 | if (defined (my $v = delete $ob->{slow_move})) { |
202 | $ob->{move_slow} |= MOVE_WALK; |
212 | $ob->{move_slow} |= MOVE_WALK; |
203 | $ob->{move_slow_penalty} = $v; |
213 | $ob->{move_slow_penalty} = $v; |
204 | } |
214 | } |
205 | if (defined (my $v = delete $ob->{walk_on})) { |
215 | if (defined (my $v = delete $ob->{walk_on})) { |
|
|
216 | $ob->{move_on} = MOVE_ALL unless exists $ob->{move_on}; |
206 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
217 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
207 | : $ob->{move_on} & ~MOVE_WALK; |
218 | : $ob->{move_on} & ~MOVE_WALK; |
208 | } |
219 | } |
209 | if (defined (my $v = delete $ob->{walk_off})) { |
220 | if (defined (my $v = delete $ob->{walk_off})) { |
|
|
221 | $ob->{move_off} = MOVE_ALL unless exists $ob->{move_off}; |
210 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
222 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
211 | : $ob->{move_off} & ~MOVE_WALK; |
223 | : $ob->{move_off} & ~MOVE_WALK; |
212 | } |
224 | } |
213 | if (defined (my $v = delete $ob->{fly_on})) { |
225 | if (defined (my $v = delete $ob->{fly_on})) { |
|
|
226 | $ob->{move_on} = MOVE_ALL unless exists $ob->{move_on}; |
214 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
227 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
215 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
228 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
216 | } |
229 | } |
217 | if (defined (my $v = delete $ob->{fly_off})) { |
230 | if (defined (my $v = delete $ob->{fly_off})) { |
|
|
231 | $ob->{move_off} = MOVE_ALL unless exists $ob->{move_off}; |
218 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
232 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
219 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
233 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
220 | } |
234 | } |
221 | if (defined (my $v = delete $ob->{flying})) { |
235 | if (defined (my $v = delete $ob->{flying})) { |
|
|
236 | $ob->{move_type} = MOVE_ALL unless exists $ob->{move_type}; |
222 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
237 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
223 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
238 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
224 | } |
239 | } |
225 | |
240 | |
226 | # convert idiotic event_xxx things into objects |
241 | # convert idiotic event_xxx things into objects |
… | |
… | |
283 | } |
298 | } |
284 | |
299 | |
285 | $ob |
300 | $ob |
286 | } |
301 | } |
287 | |
302 | |
|
|
303 | sub attr_thaw($) { |
|
|
304 | my ($ob) = @_; |
|
|
305 | |
|
|
306 | $ob->{attach} = from_json $ob->{attach} |
|
|
307 | if exists $ob->{attach}; |
|
|
308 | |
|
|
309 | $ob |
|
|
310 | } |
|
|
311 | |
|
|
312 | sub attr_freeze($) { |
|
|
313 | my ($ob) = @_; |
|
|
314 | |
|
|
315 | $ob->{attach} = Crossfire::to_json $ob->{attach} |
|
|
316 | if exists $ob->{attach}; |
|
|
317 | |
|
|
318 | $ob |
|
|
319 | } |
|
|
320 | |
288 | sub read_pak($) { |
321 | sub read_pak($) { |
289 | my ($path) = @_; |
322 | my ($path) = @_; |
290 | |
323 | |
291 | my %pak; |
324 | my %pak; |
292 | |
325 | |
293 | open my $fh, "<", $path |
326 | open my $fh, "<:raw:perlio", $path |
294 | or Carp::croak "$_[0]: $!"; |
327 | or Carp::croak "$_[0]: $!"; |
295 | binmode $fh; |
328 | binmode $fh; |
296 | while (<$fh>) { |
329 | while (<$fh>) { |
297 | my ($type, $id, $len, $path) = split; |
330 | my ($type, $id, $len, $path) = split; |
298 | $path =~ s/.*\///; |
331 | $path =~ s/.*\///; |
… | |
… | |
300 | } |
333 | } |
301 | |
334 | |
302 | \%pak |
335 | \%pak |
303 | } |
336 | } |
304 | |
337 | |
305 | sub read_arch($) { |
338 | sub read_arch($;$) { |
306 | my ($path) = @_; |
339 | my ($path, $toplevel) = @_; |
307 | |
340 | |
308 | my %arc; |
341 | my %arc; |
309 | my ($more, $prev); |
342 | my ($more, $prev); |
310 | |
343 | |
311 | open my $fh, "<", $path |
344 | open my $fh, "<:raw:perlio:utf8", $path |
312 | or Carp::croak "$path: $!"; |
345 | or Carp::croak "$path: $!"; |
313 | |
346 | |
314 | binmode $fh; |
347 | # binmode $fh; |
315 | |
348 | |
316 | my $parse_block; $parse_block = sub { |
349 | my $parse_block; $parse_block = sub { |
317 | my %arc = @_; |
350 | my %arc = @_; |
318 | |
351 | |
319 | while (<$fh>) { |
352 | while (<$fh>) { |
320 | s/\s+$//; |
353 | s/\s+$//; |
321 | if (/^end$/i) { |
354 | if (/^end$/i) { |
322 | last; |
355 | last; |
323 | } elsif (/^arch (\S+)$/i) { |
356 | } elsif (/^arch (\S+)$/i) { |
324 | push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
357 | push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1); |
325 | } elsif (/^lore$/i) { |
358 | } elsif (/^lore$/i) { |
326 | while (<$fh>) { |
359 | while (<$fh>) { |
327 | last if /^endlore\s*$/i; |
360 | last if /^endlore\s*$/i; |
328 | $arc{lore} .= $_; |
361 | $arc{lore} .= $_; |
329 | } |
362 | } |
330 | } elsif (/^msg$/i) { |
363 | } elsif (/^msg$/i) { |
331 | while (<$fh>) { |
364 | while (<$fh>) { |
332 | last if /^endmsg\s*$/i; |
365 | last if /^endmsg\s*$/i; |
333 | $arc{msg} .= $_; |
366 | $arc{msg} .= $_; |
334 | } |
367 | } |
|
|
368 | } elsif (/^anim$/i) { |
|
|
369 | while (<$fh>) { |
|
|
370 | last if /^mina\s*$/i; |
|
|
371 | chomp; |
|
|
372 | push @{ $arc{anim} }, $_; |
|
|
373 | } |
335 | } elsif (/^(\S+)\s*(.*)$/) { |
374 | } elsif (/^(\S+)\s*(.*)$/) { |
336 | $arc{lc $1} = $2; |
375 | $arc{lc $1} = $2; |
337 | } elsif (/^\s*($|#)/) { |
376 | } elsif (/^\s*($|#)/) { |
338 | # |
377 | # |
339 | } else { |
378 | } else { |
… | |
… | |
348 | s/\s+$//; |
387 | s/\s+$//; |
349 | if (/^more$/i) { |
388 | if (/^more$/i) { |
350 | $more = $prev; |
389 | $more = $prev; |
351 | } elsif (/^object (\S+)$/i) { |
390 | } elsif (/^object (\S+)$/i) { |
352 | my $name = $1; |
391 | my $name = $1; |
353 | my $arc = normalize_object $parse_block->(_name => $name); |
392 | my $arc = attr_thaw normalize_object $parse_block->(_name => $name); |
354 | |
393 | |
355 | if ($more) { |
394 | if ($more) { |
356 | $more->{more} = $arc; |
395 | $more->{more} = $arc; |
357 | } else { |
396 | } else { |
358 | $arc{$name} = $arc; |
397 | $arc{$name} = $arc; |
359 | } |
398 | } |
360 | $prev = $arc; |
399 | $prev = $arc; |
361 | $more = undef; |
400 | $more = undef; |
362 | } elsif (/^arch (\S+)$/i) { |
401 | } elsif (/^arch (\S+)$/i) { |
363 | my $name = $1; |
402 | my $name = $1; |
364 | my $arc = normalize_arch $parse_block->(_name => $name); |
403 | my $arc = attr_thaw normalize_arch $parse_block->(_name => $name); |
365 | |
404 | |
366 | if ($more) { |
405 | if ($more) { |
367 | $more->{more} = $arc; |
406 | $more->{more} = $arc; |
368 | } else { |
407 | } else { |
369 | push @{ $arc{arch} }, $arc; |
408 | push @{ $arc{arch} }, $arc; |
370 | } |
409 | } |
371 | $prev = $arc; |
410 | $prev = $arc; |
372 | $more = undef; |
411 | $more = undef; |
|
|
412 | } elsif ($toplevel && /^(\S+)\s+(.*)$/) { |
|
|
413 | if ($1 eq "lev_array") { |
|
|
414 | while (<$fh>) { |
|
|
415 | last if /^endplst\s*$/; |
|
|
416 | push @{$toplevel->{lev_array}}, $_+0; |
|
|
417 | } |
|
|
418 | } else { |
|
|
419 | $toplevel->{$1} = $2; |
|
|
420 | } |
373 | } elsif (/^\s*($|#)/) { |
421 | } elsif (/^\s*($|#)/) { |
374 | # |
422 | # |
375 | } else { |
423 | } else { |
376 | warn "$path: unparseable top-level line '$_'"; |
424 | die "$path: unparseable top-level line '$_'"; |
377 | } |
425 | } |
378 | } |
426 | } |
379 | |
427 | |
380 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
428 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
381 | |
429 | |