ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.54
Committed: Thu Mar 23 06:45:23 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.53: +49 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Crossfire - Crossfire maphandling
4
5 =cut
6
7 package Crossfire;
8
9 our $VERSION = '0.1';
10
11 use strict;
12
13 use base 'Exporter';
14
15 use Carp ();
16 use File::Spec;
17 use List::Util qw(min max);
18 use Storable qw(freeze thaw);
19
20 our @EXPORT = qw(
21 read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents
22 );
23
24 our $LIB = $ENV{CROSSFIRE_LIBDIR};
25
26 our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
27
28 mkdir $VARDIR, 0777;
29
30 sub TILESIZE (){ 32 }
31
32 our %ARCH;
33 our %FACE;
34 our $TILE;
35
36 our %FIELD_MULTILINE = (
37 msg => "endmsg",
38 lore => "endlore",
39 maplore => "endmaplore",
40 );
41
42 # same as in server save routine, to (hopefully) be compatible
43 # to the other editors.
44 our @FIELD_ORDER = (qw(
45 name name_pl custom_name title race
46 slaying skill msg lore other_arch face
47 #events
48 animation is_animated
49 Str Dex Con Wis Pow Cha Int
50 hp maxhp sp maxsp grace maxgrace
51 exp perm_exp expmul
52 food dam luck wc ac x y speed speed_left move_state attack_movement
53 nrof level direction type subtype
54
55 resist_physical resist_magic resist_fire resist_electricity
56 resist_cold resist_confusion resist_acid resist_drain
57 resist_weaponmagic resist_ghosthit resist_poison resist_slow
58 resist_paralyze resist_turn_undead resist_fear resist_cancellation
59 resist_deplete resist_death resist_chaos resist_counterspell
60 resist_godpower resist_holyword resist_blind resist_internal
61 resist_life_stealing resist_disease
62
63 path_attuned path_repelled path_denied material materialname
64 value carrying weight invisible state magic
65 last_heal last_sp last_grace last_eat
66 connected glow_radius randomitems npx_status npc_program
67 run_away pick_up container will_apply smoothlevel
68 current_weapon_script weapontype tooltype elevation client_type
69 item_power duration range
70 range_modifier duration_modifier dam_modifier gen_sp_armour
71 move_type move_block move_allow move_on move_off move_on move_slow move_slow_penalty
72
73 alive wiz was_wiz applied unpaid can_use_shield no_pick is_animated monster
74 friendly generator is_thrown auto_apply treasure player sold see_invisible
75 can_roll overlay_floor is_turnable is_used_up identified reflecting changing
76 splitting hitback startequip blocksview undead scared unaggressive
77 reflect_missile reflect_spell no_magic no_fix_player is_lightable tear_down
78 run_away pick_up unique no_drop can_cast_spell can_use_scroll can_use_range
79 can_use_bow can_use_armour can_use_weapon can_use_ring has_ready_range
80 has_ready_bow xrays is_floor lifesave no_strength sleep stand_still
81 random_move only_attack confused stealth cursed damned see_anywhere
82 known_magical known_cursed can_use_skill been_applied has_ready_scroll
83 can_use_rod can_use_horn make_invisible inv_locked is_wooded is_hilly
84 has_ready_skill has_ready_weapon no_skill_ident is_blind can_see_in_dark
85 is_cauldron is_dust no_steal one_hit berserk neutral no_attack no_damage
86 activate_on_push activate_on_release is_water use_content_on_gen is_buildable
87
88 body_range body_arm body_torso body_head body_neck body_skill
89 body_finger body_shoulder body_foot body_hand body_wrist body_waist
90 ));
91
92 sub MOVE_WALK (){ 0x01 }
93 sub MOVE_FLY_LOW (){ 0x02 }
94 sub MOVE_FLY_HIGH (){ 0x04 }
95 sub MOVE_FLYING (){ 0x06 }
96 sub MOVE_SWIM (){ 0x08 }
97 sub MOVE_BOAT (){ 0x10 }
98 sub MOVE_ALL (){ 0xff }
99
100 sub load_ref($) {
101 my ($path) = @_;
102
103 open my $fh, "<", $path
104 or die "$path: $!";
105 binmode $fh;
106 local $/;
107
108 thaw <$fh>
109 }
110
111 sub save_ref($$) {
112 my ($ref, $path) = @_;
113
114 open my $fh, ">", "$path~"
115 or die "$path~: $!";
116 binmode $fh;
117 print $fh freeze $ref;
118 close $fh;
119 rename "$path~", $path
120 or die "$path: $!";
121 }
122
123 sub normalize_object($) {
124 my ($ob) = @_;
125
126 delete $ob->{$_} for qw(
127 can_knockback can_parry can_impale can_cut can_dam_armour
128 can_apply pass_thru can_pass_thru
129 );
130
131 for my $attr (qw(move_type move_block move_allow move_on move_off move_slow)) {
132 next unless exists $ob->{$attr};
133 next if $ob->{$attr} =~ /^\d+$/;
134
135 my $flags = 0;
136
137 # assume list
138 for my $flag (map lc, split /\s+/, $ob->{$attr}) {
139 $flags |= MOVE_WALK if $flag eq "walk";
140 $flags |= MOVE_FLY_LOW if $flag eq "fly_low";
141 $flags |= MOVE_FLY_HIGH if $flag eq "fly_high";
142 $flags |= MOVE_FLYING if $flag eq "flying";
143 $flags |= MOVE_SWIM if $flag eq "swim";
144 $flags |= MOVE_BOAT if $flag eq "boat";
145 $flags |= MOVE_ALL if $flag eq "all";
146
147 $flags &= ~MOVE_WALK if $flag eq "-walk";
148 $flags &= ~MOVE_FLY_LOW if $flag eq "-fly_low";
149 $flags &= ~MOVE_FLY_HIGH if $flag eq "-fly_high";
150 $flags &= ~MOVE_FLYING if $flag eq "-flying";
151 $flags &= ~MOVE_SWIM if $flag eq "-swim";
152 $flags &= ~MOVE_BOAT if $flag eq "-boat";
153 $flags &= ~MOVE_ALL if $flag eq "-all";
154 }
155
156 $ob->{$attr} = $flags;
157 }
158
159 if (defined (my $v = delete $ob->{no_pass})) {
160 $ob->{move_block} = $v ? MOVE_ALL : 0;
161 }
162 if (defined (my $v = delete $ob->{slow_move})) {
163 $ob->{move_slow} |= MOVE_WALK;
164 $ob->{move_slow_penalty} = $v;
165 }
166 if (defined (my $v = delete $ob->{walk_on})) {
167 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
168 : $ob->{move_on} & ~MOVE_WALK;
169 }
170 if (defined (my $v = delete $ob->{walk_off})) {
171 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
172 : $ob->{move_off} & ~MOVE_WALK;
173 }
174 if (defined (my $v = delete $ob->{fly_on})) {
175 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
176 : $ob->{move_on} & ~MOVE_FLY_LOW;
177 }
178 if (defined (my $v = delete $ob->{fly_off})) {
179 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
180 : $ob->{move_off} & ~MOVE_FLY_LOW;
181 }
182 if (defined (my $v = delete $ob->{flying})) {
183 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
184 : $ob->{move_type} & ~MOVE_FLY_LOW;
185 }
186
187 $ob
188 }
189
190 sub normalize_arch($) {
191 my ($ob) = @_;
192
193 normalize_object $ob;
194
195 my $arch = $ARCH{$ob->{_name}}
196 or (warn "$ob->{_name}: no such archetype", return $ob);
197
198 if ($arch->{type} == 22) { # map
199 my %normalize = (
200 "enter_x" => "hp",
201 "enter_y" => "sp",
202 "width" => "x",
203 "height" => "y",
204 "reset_timeout" => "weight",
205 "swap_time" => "value",
206 "difficulty" => "level",
207 "darkness" => "invisible",
208 "fixed_resettime" => "stand_still",
209 );
210
211 while (my ($k2, $k1) = each %normalize) {
212 if (defined (my $v = delete $ob->{$k1})) {
213 $ob->{$k2} = $v;
214 }
215 }
216 } else {
217 # if value matches archetype default, delete
218 while (my ($k, $v) = each %$ob) {
219 if (exists $arch->{$k} and $arch->{$k} eq $v) {
220 next if $k eq "_name";
221 delete $ob->{$k};
222 }
223 }
224 }
225
226 $ob
227 }
228
229 sub read_pak($) {
230 my ($path) = @_;
231
232 my %pak;
233
234 open my $fh, "<", $path
235 or Carp::croak "$_[0]: $!";
236 binmode $fh;
237 while (<$fh>) {
238 my ($type, $id, $len, $path) = split;
239 $path =~ s/.*\///;
240 read $fh, $pak{$path}, $len;
241 }
242
243 \%pak
244 }
245
246 sub read_arch($) {
247 my ($path) = @_;
248
249 my %arc;
250 my ($more, $prev);
251
252 open my $fh, "<", $path
253 or Carp::croak "$path: $!";
254
255 binmode $fh;
256
257 my $parse_block; $parse_block = sub {
258 my %arc = @_;
259
260 while (<$fh>) {
261 s/\s+$//;
262 if (/^end$/i) {
263 last;
264 } elsif (/^arch (\S+)$/) {
265 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
266 } elsif (/^lore$/) {
267 while (<$fh>) {
268 last if /^endlore\s*$/i;
269 $arc{lore} .= $_;
270 }
271 } elsif (/^msg$/) {
272 while (<$fh>) {
273 last if /^endmsg\s*$/i;
274 $arc{msg} .= $_;
275 }
276 } elsif (/^(\S+)\s*(.*)$/) {
277 $arc{lc $1} = $2;
278 } elsif (/^\s*($|#)/) {
279 #
280 } else {
281 warn "$path: unparsable line '$_' in arch $arc{_name}";
282 }
283 }
284
285 \%arc
286 };
287
288 while (<$fh>) {
289 s/\s+$//;
290 if (/^more$/i) {
291 $more = $prev;
292 } elsif (/^object (\S+)$/i) {
293 my $name = $1;
294 my $arc = normalize_object $parse_block->(_name => $name);
295
296 if ($more) {
297 $more->{more} = $arc;
298 } else {
299 $arc{$name} = $arc;
300 }
301 $prev = $arc;
302 $more = undef;
303 } elsif (/^arch (\S+)$/i) {
304 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
305 } elsif (/^\s*($|#)/) {
306 #
307 } else {
308 warn "$path: unparseable top-level line '$_'";
309 }
310 }
311
312 undef $parse_block; # work around bug in perl not freeing $fh etc.
313
314 \%arc
315 }
316
317 # put all archs into a hash with editor_face as it's key
318 # NOTE: the arrays in the hash values are references to
319 # the archs from $ARCH
320 sub editor_archs {
321 my %paths;
322
323 for (keys %ARCH) {
324 my $arch = $ARCH{$_};
325 push @{$paths{$arch->{editor_folder}}}, \$arch;
326 }
327
328 \%paths
329 }
330
331 =item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
332
333 arch_extents determines the extents of the given arch's face(s), linked
334 faces and single faces are handled here it returns (minx, miny, maxx,
335 maxy)
336
337 =cut
338
339 sub arch_extents {
340 my ($a) = @_;
341
342 my $o = $ARCH{$a->{_name}}
343 or return;
344
345 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
346 or (warn "no face data found for arch '$a->{_name}'"), return;
347
348 if ($face->{w} > 1 || $face->{h} > 1) {
349 # bigface
350 return (0, 0, $face->{w} - 1, $face->{h} - 1);
351
352 } elsif ($o->{more}) {
353 # linked face
354 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
355
356 for (; $o; $o = $o->{more}) {
357 $minx = min $minx, $o->{x};
358 $miny = min $miny, $o->{y};
359 $maxx = max $maxx, $o->{x};
360 $maxy = max $maxy, $o->{y};
361 }
362
363 return ($minx, $miny, $maxx, $maxy);
364
365 } else {
366 # single face
367 return (0, 0, 0, 0);
368 }
369 }
370
371 =item $type = arch_attr $arch
372
373 Returns a hashref describing the object and its attributes. It can contain
374 the following keys:
375
376 name the name, suitable for display purposes
377 ignore
378 attr
379 desc
380 use
381 section => [name => \%attr, name => \%attr]
382 import
383
384 =cut
385
386 sub arch_attr($) {
387 my ($obj) = @_;
388
389 require Crossfire::Data;
390
391 my $root;
392 my $attr = { };
393
394 my $arch = $ARCH{ $obj->{_name} };
395 my $type = $obj->{type} || $arch->{type};
396
397 if ($type > 0) {
398 $root = $Crossfire::Data::ATTR{$type};
399 } else {
400 $root = $Crossfire::Data::TYPE{Misc};
401
402 type:
403 for (@Crossfire::Data::ATTR0) {
404 my $req = $_->{required}
405 or die "internal error: ATTR0 without 'required'";
406
407 keys %$req;
408 while (my ($k, $v) = each %$req) {
409 next type
410 unless $obj->{$k} == $v || $arch->{$k} == $v;
411 }
412
413 $root = $_;
414 }
415 }
416
417 my @import = ($root);
418
419 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
420 unless $type == 116;
421
422 my (%ignore);
423 my (@section_order, %section, @attr_order);
424
425 while (my $type = shift @import) {
426 push @import, @{$type->{import} || []};
427
428 $attr->{$_} ||= $type->{$_}
429 for qw(name desc use);
430
431 for (@{$type->{ignore} || []}) {
432 $ignore{$_}++ for ref $_ ? @$_ : $_;
433 }
434
435 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
436 my ($name, $attr) = @$_;
437 push @section_order, $name;
438 for (@$attr) {
439 my ($k, $v) = @$_;
440 push @attr_order, $k;
441 $section{$name}{$k} ||= $v;
442 }
443 }
444 }
445
446 $attr->{section} = [
447 map !exists $section{$_} ? () : do {
448 my $attr = delete $section{$_};
449
450 [
451 $_,
452 map exists $attr->{$_} && !$ignore{$_}
453 ? [$_ => delete $attr->{$_}] : (),
454 @attr_order
455 ]
456 },
457
458 exists $section{$_} ? [$_ => delete $section{$_}] : (),
459 @section_order
460 ];
461
462 $attr
463 }
464
465 sub arch_edit_sections {
466 # if (edit_type == IGUIConstants.TILE_EDIT_NONE)
467 # edit_type = 0;
468 # else if (edit_type != 0) {
469 # // all flags from 'check_type' must be unset in this arch because they get recalculated now
470 # edit_type &= ~check_type;
471 # }
472 #
473 # }
474 # if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
475 # getAttributeValue("alive", defarch) == 1 &&
476 # (getAttributeValue("monster", defarch) == 1 ||
477 # getAttributeValue("generator", defarch) == 1)) {
478 # // Monster: monsters/npcs/generators
479 # edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
480 # }
481 # if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
482 # arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
483 # // Walls
484 # edit_type |= IGUIConstants.TILE_EDIT_WALL;
485 # }
486 # if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
487 # getAttributeValue("connected", defarch) != 0) {
488 # // Connected Objects
489 # edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
490 # }
491 # if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
492 # arch_type == 66 || arch_type == 41 || arch_type == 95) {
493 # // Exit: teleporter/exit/trapdoors
494 # edit_type |= IGUIConstants.TILE_EDIT_EXIT;
495 # }
496 # if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
497 # getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
498 # arch_type == 5 || arch_type == 36 || arch_type == 60 ||
499 # arch_type == 85 || arch_type == 111 || arch_type == 123 ||
500 # arch_type == 124 || arch_type == 130)) {
501 # // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
502 # edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
503 # }
504 # if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
505 # arch_type == 20 || arch_type == 23 || arch_type == 26 ||
506 # arch_type == 91 || arch_type == 21 || arch_type == 24) {
507 # // Door: door/special door/gates + keys
508 # edit_type |= IGUIConstants.TILE_EDIT_DOOR;
509 # }
510 # if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
511 # getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
512 # arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
513 # arch_type == 35 || arch_type == 39 || arch_type == 70 ||
514 # arch_type == 87 || arch_type == 99 || arch_type == 100 ||
515 # arch_type == 104 || arch_type == 109 || arch_type == 113 ||
516 # arch_type == 122 || arch_type == 3)) {
517 # // Equipment: weapons/armour/wands/rods
518 # edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
519 # }
520 #
521 # return(edit_type);
522 #
523 #
524 }
525
526 sub cache_file($$&&) {
527 my ($src, $cache, $load, $create) = @_;
528
529 my ($size, $mtime) = (stat $src)[7,9]
530 or Carp::croak "$src: $!";
531
532 if (-e $cache) {
533 my $ref = eval { load_ref $cache };
534
535 if ($ref->{version} == 1
536 && $ref->{size} == $size
537 && $ref->{mtime} == $mtime
538 && eval { $load->($ref->{data}); 1 }) {
539 return;
540 }
541 }
542
543 my $ref = {
544 version => 1,
545 size => $size,
546 mtime => $mtime,
547 data => $create->(),
548 };
549
550 $load->($ref->{data});
551
552 save_ref $ref, $cache;
553 }
554
555 =item set_libdir $path
556
557 Sets the library directory to the given path
558 (default: $ENV{CROSSFIRE_LIBDIR}).
559
560 You have to (re-)load the archetypes and tilecache manually after steting
561 the library path.
562
563 =cut
564
565 sub set_libdir($) {
566 $LIB = $_[0];
567 }
568
569 =item load_archetypes
570
571 (Re-)Load archetypes into %ARCH.
572
573 =cut
574
575 sub load_archetypes() {
576 cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub {
577 *ARCH = $_[0];
578 }, sub {
579 read_arch "$LIB/archetypes"
580 };
581 }
582
583 =item load_tilecache
584
585 (Re-)Load %TILE and %FACE.
586
587 =cut
588
589 sub load_tilecache() {
590 require Gtk2;
591
592 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub {
593 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
594 or die "$VARDIR/tilecache.png: $!";
595 *FACE = $_[0];
596 }, sub {
597 require File::Temp;
598
599 my $tile = read_pak "$LIB/crossfire.0";
600
601 my %cache;
602
603 my $idx = 0;
604
605 for my $name (sort keys %$tile) {
606 my ($fh, $filename) = File::Temp::tempfile ();
607 print $fh $tile->{$name};
608 close $fh;
609 my $pb = new_from_file Gtk2::Gdk::Pixbuf $filename;
610 unlink $filename;
611
612 my $tile = $cache{$name} = {
613 pb => $pb,
614 idx => $idx,
615 w => int $pb->get_width / TILESIZE,
616 h => int $pb->get_height / TILESIZE,
617 };
618
619
620 $idx += $tile->{w} * $tile->{h};
621 }
622
623 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
624
625 while (my ($name, $tile) = each %cache) {
626 my $tpb = delete $tile->{pb};
627 my $ofs = $tile->{idx};
628
629 for my $x (0 .. $tile->{w} - 1) {
630 for my $y (0 .. $tile->{h} - 1) {
631 my $idx = $ofs + $x + $y * $tile->{w};
632 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
633 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
634 }
635 }
636 }
637
638 $pb->save ("$VARDIR/tilecache.png", "png");
639
640 \%cache
641 };
642 }
643
644 =head1 AUTHOR
645
646 Marc Lehmann <schmorp@schmorp.de>
647 http://home.schmorp.de/
648
649 Robin Redeker <elmex@ta-sa.org>
650 http://www.ta-sa.org/
651
652 =cut
653
654 1