ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
(Generate patch)

Comparing deliantra/Deliantra/Deliantra.pm (file contents):
Revision 1.65 by root, Wed May 3 19:34:31 2006 UTC vs.
Revision 1.71 by elmex, Thu Aug 31 21:09:32 2006 UTC

4 4
5=cut 5=cut
6 6
7package Crossfire; 7package Crossfire;
8 8
9our $VERSION = '0.1'; 9our $VERSION = '0.9';
10 10
11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
18use Storable qw(freeze thaw); 18use Storable qw(freeze thaw);
19 19
20our @EXPORT = qw( 20our @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
24use JSON::Syck (); #TODO#d# replace by JSON::PC when it becomes available == working
25
26sub from_json($) {
27 $JSON::Syck::ImplicitUnicode = 1;
28 JSON::Syck::Load $_[0]
29}
30
31sub to_json($) {
32 $JSON::Syck::ImplicitUnicode = 0;
33 JSON::Syck::Dump $_[0]
34}
23 35
24our $LIB = $ENV{CROSSFIRE_LIBDIR}; 36our $LIB = $ENV{CROSSFIRE_LIBDIR};
25 37
26our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; 38our $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.
48our @FIELD_ORDER_MAP = (qw( 60our @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
57our @FIELD_ORDER = (qw( 69our @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
199 if (defined (my $v = delete $ob->{slow_move})) { 211 if (defined (my $v = delete $ob->{slow_move})) {
200 $ob->{move_slow} |= MOVE_WALK; 212 $ob->{move_slow} |= MOVE_WALK;
201 $ob->{move_slow_penalty} = $v; 213 $ob->{move_slow_penalty} = $v;
202 } 214 }
203 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};
204 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK 217 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
205 : $ob->{move_on} & ~MOVE_WALK; 218 : $ob->{move_on} & ~MOVE_WALK;
206 } 219 }
207 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};
208 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK 222 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
209 : $ob->{move_off} & ~MOVE_WALK; 223 : $ob->{move_off} & ~MOVE_WALK;
210 } 224 }
211 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};
212 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW 227 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
213 : $ob->{move_on} & ~MOVE_FLY_LOW; 228 : $ob->{move_on} & ~MOVE_FLY_LOW;
214 } 229 }
215 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};
216 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW 232 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
217 : $ob->{move_off} & ~MOVE_FLY_LOW; 233 : $ob->{move_off} & ~MOVE_FLY_LOW;
218 } 234 }
219 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};
220 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW 237 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
221 : $ob->{move_type} & ~MOVE_FLY_LOW; 238 : $ob->{move_type} & ~MOVE_FLY_LOW;
222 } 239 }
223 240
224 # convert idiotic event_xxx things into objects 241 # convert idiotic event_xxx things into objects
281 } 298 }
282 299
283 $ob 300 $ob
284} 301}
285 302
303sub attr_thaw($) {
304 my ($ob) = @_;
305
306 $ob->{attach} = from_json $ob->{attach}
307 if exists $ob->{attach};
308
309 $ob
310}
311
312sub attr_freeze($) {
313 my ($ob) = @_;
314
315 $ob->{attach} = Crossfire::to_json $ob->{attach}
316 if exists $ob->{attach};
317
318 $ob
319}
320
286sub read_pak($) { 321sub read_pak($) {
287 my ($path) = @_; 322 my ($path) = @_;
288 323
289 my %pak; 324 my %pak;
290 325
307 my ($more, $prev); 342 my ($more, $prev);
308 343
309 open my $fh, "<:raw:perlio:utf8", $path 344 open my $fh, "<:raw:perlio:utf8", $path
310 or Carp::croak "$path: $!"; 345 or Carp::croak "$path: $!";
311 346
312 binmode $fh; 347# binmode $fh;
313 348
314 my $parse_block; $parse_block = sub { 349 my $parse_block; $parse_block = sub {
315 my %arc = @_; 350 my %arc = @_;
316 351
317 while (<$fh>) { 352 while (<$fh>) {
318 s/\s+$//; 353 s/\s+$//;
319 if (/^end$/i) { 354 if (/^end$/i) {
320 last; 355 last;
321 } elsif (/^arch (\S+)$/i) { 356 } elsif (/^arch (\S+)$/i) {
322 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); 357 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
323 } elsif (/^lore$/i) { 358 } elsif (/^lore$/i) {
324 while (<$fh>) { 359 while (<$fh>) {
325 last if /^endlore\s*$/i; 360 last if /^endlore\s*$/i;
326 $arc{lore} .= $_; 361 $arc{lore} .= $_;
327 } 362 }
352 s/\s+$//; 387 s/\s+$//;
353 if (/^more$/i) { 388 if (/^more$/i) {
354 $more = $prev; 389 $more = $prev;
355 } elsif (/^object (\S+)$/i) { 390 } elsif (/^object (\S+)$/i) {
356 my $name = $1; 391 my $name = $1;
357 my $arc = normalize_object $parse_block->(_name => $name); 392 my $arc = attr_thaw normalize_object $parse_block->(_name => $name);
393 $arc->{_atype} = 'object';
358 394
359 if ($more) { 395 if ($more) {
360 $more->{more} = $arc; 396 $more->{more} = $arc;
361 } else { 397 } else {
362 $arc{$name} = $arc; 398 $arc{$name} = $arc;
363 } 399 }
364 $prev = $arc; 400 $prev = $arc;
365 $more = undef; 401 $more = undef;
366 } elsif (/^arch (\S+)$/i) { 402 } elsif (/^arch (\S+)$/i) {
367 my $name = $1; 403 my $name = $1;
368 my $arc = normalize_arch $parse_block->(_name => $name); 404 my $arc = attr_thaw normalize_arch $parse_block->(_name => $name);
405 $arc->{_atype} = 'arch';
369 406
370 if ($more) { 407 if ($more) {
371 $more->{more} = $arc; 408 $more->{more} = $arc;
372 } else { 409 } else {
373 push @{ $arc{arch} }, $arc; 410 push @{ $arc{arch} }, $arc;
391 } 428 }
392 429
393 undef $parse_block; # work around bug in perl not freeing $fh etc. 430 undef $parse_block; # work around bug in perl not freeing $fh etc.
394 431
395 \%arc 432 \%arc
433}
434
435sub archlist_to_string {
436 my ($arch) = @_;
437
438 my $str;
439
440 my $append; $append = sub {
441 my %a = %{$_[0]};
442
443 Crossfire::attr_freeze \%a;
444 Crossfire::normalize_arch \%a;
445
446 # undo the bit-split we did before
447 if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) {
448 $a{attack_movement} = (delete $a{attack_movement_bits_0_3})
449 | (delete $a{attack_movement_bits_4_7});
450 }
451
452 $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n";
453
454 my $inv = delete $a{inventory};
455 my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some
456 my $anim = delete $a{anim};
457
458 my @kv;
459
460 for ($a{_name} eq "map"
461 ? @Crossfire::FIELD_ORDER_MAP
462 : @Crossfire::FIELD_ORDER) {
463 push @kv, [$_, delete $a{$_}]
464 if exists $a{$_};
465 }
466
467 for (sort keys %a) {
468 next if /^_/; # ignore our _-keys
469 push @kv, [$_, delete $a{$_}];
470 }
471
472 for (@kv) {
473 my ($k, $v) = @$_;
474
475 if (my $end = $Crossfire::FIELD_MULTILINE{$k}) {
476 $v =~ s/\n$//;
477 $str .= "$k\n$v\n$end\n";
478 } elsif (exists $Crossfire::FIELD_MOVEMENT{$k}) {
479 if ($v & ~Crossfire::MOVE_ALL or !$v) {
480 $str .= "$k $v\n";
481
482 } elsif ($v & Crossfire::MOVE_ALLBIT) {
483 $str .= "$k all";
484
485 $str .= " -walk" unless $v & Crossfire::MOVE_WALK;
486 $str .= " -fly_low" unless $v & Crossfire::MOVE_FLY_LOW;
487 $str .= " -fly_high" unless $v & Crossfire::MOVE_FLY_HIGH;
488 $str .= " -swim" unless $v & Crossfire::MOVE_SWIM;
489 $str .= " -boat" unless $v & Crossfire::MOVE_BOAT;
490
491 $str .= "\n";
492
493 } else {
494 $str .= $k;
495
496 $str .= " walk" if $v & Crossfire::MOVE_WALK;
497 $str .= " fly_low" if $v & Crossfire::MOVE_FLY_LOW;
498 $str .= " fly_high" if $v & Crossfire::MOVE_FLY_HIGH;
499 $str .= " swim" if $v & Crossfire::MOVE_SWIM;
500 $str .= " boat" if $v & Crossfire::MOVE_BOAT;
501
502 $str .= "\n";
503 }
504 } else {
505 $str .= "$k $v\n";
506 }
507 }
508
509 if ($inv) {
510 $append->($_) for @$inv;
511 }
512
513 if ($a{_atype} eq 'object') {
514 $str .= join "\n", "anim", @$anim, "mina\n"
515 if $anim;
516 }
517
518 $str .= "end\n";
519
520 if (($a{_atype} eq 'object') && $more) {
521 $str .= "\nmore\n";
522 $append->($more) if $more;
523 }
524 };
525
526 for (@$arch) {
527 $append->($_);
528 }
529
530 $str
396} 531}
397 532
398# put all archs into a hash with editor_face as it's key 533# put all archs into a hash with editor_face as it's key
399# NOTE: the arrays in the hash values are references to 534# NOTE: the arrays in the hash values are references to
400# the archs from $ARCH 535# the archs from $ARCH

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines