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.10 by elmex, Mon Feb 6 23:01: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
15use Storable; 15use Carp ();
16use File::Spec;
17use List::Util qw(min max);
18use Storable qw(freeze thaw);
16 19
17our @EXPORT = qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs arch2pickmap arch_extends); 20our @EXPORT = qw(
21 read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents
22);
18 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}
35
19our $LIB = $ENV{CROSSFIRE_LIBDIR} 36our $LIB = $ENV{CROSSFIRE_LIBDIR};
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 37
38our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
39
40mkdir $VARDIR, 0777;
21 41
22sub TILESIZE (){ 32 } 42sub TILESIZE (){ 32 }
23 43
24our $ARCH; 44our %ARCH;
45our %FACE;
46our $TILE;
25 47
48our %FIELD_MULTILINE = (
49 msg => "endmsg",
50 lore => "endlore",
51 maplore => "endmaplore",
52);
53
54# movement bit type, PITA
55our %FIELD_MOVEMENT = map +($_ => undef),
56 qw(move_type move_block move_allow move_on move_off move_slow);
57
58# same as in server save routine, to (hopefully) be compatible
59# to the other editors.
60our @FIELD_ORDER_MAP = (qw(
61 name attach swap_time reset_timeout fixed_resettime difficulty region
62 shopitems shopgreed shopmin shopmax shoprace
63 darkness width height enter_x enter_y msg maplore
64 unique template
65 outdoor temp pressure humid windspeed winddir sky nosmooth
66 tile_path_1 tile_path_2 tile_path_3 tile_path_4
67));
68
69our @FIELD_ORDER = (qw(
70 elevation
71
72 name name_pl custom_name attach title race
73 slaying skill msg lore other_arch face
74 #todo-events
75 animation is_animated
76 str dex con wis pow cha int
77 hp maxhp sp maxsp grace maxgrace
78 exp perm_exp expmul
79 food dam luck wc ac x y speed speed_left move_state attack_movement
80 nrof level direction type subtype attacktype
81
82 resist_physical resist_magic resist_fire resist_electricity
83 resist_cold resist_confusion resist_acid resist_drain
84 resist_weaponmagic resist_ghosthit resist_poison resist_slow
85 resist_paralyze resist_turn_undead resist_fear resist_cancellation
86 resist_deplete resist_death resist_chaos resist_counterspell
87 resist_godpower resist_holyword resist_blind resist_internal
88 resist_life_stealing resist_disease
89
90 path_attuned path_repelled path_denied material materialname
91 value carrying weight invisible state magic
92 last_heal last_sp last_grace last_eat
93 connected glow_radius randomitems npx_status npc_program
94 run_away pick_up container will_apply smoothlevel
95 current_weapon_script weapontype tooltype elevation client_type
96 item_power duration range
97 range_modifier duration_modifier dam_modifier gen_sp_armour
98 move_type move_block move_allow move_on move_off move_on move_slow move_slow_penalty
99
100 alive wiz was_wiz applied unpaid can_use_shield no_pick is_animated monster
101 friendly generator is_thrown auto_apply treasure player sold see_invisible
102 can_roll overlay_floor is_turnable is_used_up identified reflecting changing
103 splitting hitback startequip blocksview undead scared unaggressive
104 reflect_missile reflect_spell no_magic no_fix_player is_lightable tear_down
105 run_away pick_up unique no_drop can_cast_spell can_use_scroll can_use_range
106 can_use_bow can_use_armour can_use_weapon can_use_ring has_ready_range
107 has_ready_bow xrays is_floor lifesave no_strength sleep stand_still
108 random_move only_attack confused stealth cursed damned see_anywhere
109 known_magical known_cursed can_use_skill been_applied has_ready_scroll
110 can_use_rod can_use_horn make_invisible inv_locked is_wooded is_hilly
111 has_ready_skill has_ready_weapon no_skill_ident is_blind can_see_in_dark
112 is_cauldron is_dust no_steal one_hit berserk neutral no_attack no_damage
113 activate_on_push activate_on_release is_water use_content_on_gen is_buildable
114
115 body_range body_arm body_torso body_head body_neck body_skill
116 body_finger body_shoulder body_foot body_hand body_wrist body_waist
117));
118
119our %EVENT_TYPE = (
120 apply => 1,
121 attack => 2,
122 death => 3,
123 drop => 4,
124 pickup => 5,
125 say => 6,
126 stop => 7,
127 time => 8,
128 throw => 9,
129 trigger => 10,
130 close => 11,
131 timer => 12,
132);
133
134sub MOVE_WALK (){ 0x01 }
135sub MOVE_FLY_LOW (){ 0x02 }
136sub MOVE_FLY_HIGH (){ 0x04 }
137sub MOVE_FLYING (){ 0x06 }
138sub MOVE_SWIM (){ 0x08 }
139sub MOVE_BOAT (){ 0x10 }
140sub MOVE_KNOWN (){ 0x1f } # all of above
141sub MOVE_ALLBIT (){ 0x10000 }
142sub MOVE_ALL (){ 0x1001f } # very special value, more PITA
143
144sub load_ref($) {
145 my ($path) = @_;
146
147 open my $fh, "<:raw:perlio", $path
148 or die "$path: $!";
149 local $/;
150
151 thaw <$fh>
152}
153
154sub save_ref($$) {
155 my ($ref, $path) = @_;
156
157 open my $fh, ">:raw:perlio", "$path~"
158 or die "$path~: $!";
159 print $fh freeze $ref;
160 close $fh;
161 rename "$path~", $path
162 or die "$path: $!";
163}
164
165# object as in "Object xxx", i.e. archetypes
166sub normalize_object($) {
167 my ($ob) = @_;
168
169 # nuke outdated or never supported fields
170 delete $ob->{$_} for qw(
171 can_knockback can_parry can_impale can_cut can_dam_armour
172 can_apply pass_thru can_pass_thru
173 );
174
175 # convert movement strings to bitsets
176 for my $attr (keys %FIELD_MOVEMENT) {
177 next unless exists $ob->{$attr};
178
179 $ob->{$attr} = MOVE_ALL if $ob->{$attr} == 255; #d# compatibility
180
181 next if $ob->{$attr} =~ /^\d+$/;
182
183 my $flags = 0;
184
185 # assume list
186 for my $flag (map lc, split /\s+/, $ob->{$attr}) {
187 $flags |= MOVE_WALK if $flag eq "walk";
188 $flags |= MOVE_FLY_LOW if $flag eq "fly_low";
189 $flags |= MOVE_FLY_HIGH if $flag eq "fly_high";
190 $flags |= MOVE_FLYING if $flag eq "flying";
191 $flags |= MOVE_SWIM if $flag eq "swim";
192 $flags |= MOVE_BOAT if $flag eq "boat";
193 $flags |= MOVE_ALL if $flag eq "all";
194
195 $flags &= ~MOVE_WALK if $flag eq "-walk";
196 $flags &= ~MOVE_FLY_LOW if $flag eq "-fly_low";
197 $flags &= ~MOVE_FLY_HIGH if $flag eq "-fly_high";
198 $flags &= ~MOVE_FLYING if $flag eq "-flying";
199 $flags &= ~MOVE_SWIM if $flag eq "-swim";
200 $flags &= ~MOVE_BOAT if $flag eq "-boat";
201 $flags &= ~MOVE_ALL if $flag eq "-all";
202 }
203
204 $ob->{$attr} = $flags;
205 }
206
207 # convert outdated movement flags to new movement sets
208 if (defined (my $v = delete $ob->{no_pass})) {
209 $ob->{move_block} = $v ? MOVE_ALL : 0;
210 }
211 if (defined (my $v = delete $ob->{slow_move})) {
212 $ob->{move_slow} |= MOVE_WALK;
213 $ob->{move_slow_penalty} = $v;
214 }
215 if (defined (my $v = delete $ob->{walk_on})) {
216 $ob->{move_on} = MOVE_ALL unless exists $ob->{move_on};
217 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
218 : $ob->{move_on} & ~MOVE_WALK;
219 }
220 if (defined (my $v = delete $ob->{walk_off})) {
221 $ob->{move_off} = MOVE_ALL unless exists $ob->{move_off};
222 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
223 : $ob->{move_off} & ~MOVE_WALK;
224 }
225 if (defined (my $v = delete $ob->{fly_on})) {
226 $ob->{move_on} = MOVE_ALL unless exists $ob->{move_on};
227 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
228 : $ob->{move_on} & ~MOVE_FLY_LOW;
229 }
230 if (defined (my $v = delete $ob->{fly_off})) {
231 $ob->{move_off} = MOVE_ALL unless exists $ob->{move_off};
232 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
233 : $ob->{move_off} & ~MOVE_FLY_LOW;
234 }
235 if (defined (my $v = delete $ob->{flying})) {
236 $ob->{move_type} = MOVE_ALL unless exists $ob->{move_type};
237 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
238 : $ob->{move_type} & ~MOVE_FLY_LOW;
239 }
240
241 # convert idiotic event_xxx things into objects
242 while (my ($event, $subtype) = each %EVENT_TYPE) {
243 if (exists $ob->{"event_${event}_plugin"}) {
244 push @{$ob->{inventory}}, {
245 _name => "event_$event",
246 title => delete $ob->{"event_${event}_plugin"},
247 slaying => delete $ob->{"event_${event}"},
248 name => delete $ob->{"event_${event}_options"},
249 };
250 }
251 }
252
253 $ob
254}
255
256# arch as in "arch xxx", ie.. objects
257sub normalize_arch($) {
258 my ($ob) = @_;
259
260 normalize_object $ob;
261
262 my $arch = $ARCH{$ob->{_name}}
263 or (warn "$ob->{_name}: no such archetype", return $ob);
264
265 if ($arch->{type} == 22) { # map
266 my %normalize = (
267 "enter_x" => "hp",
268 "enter_y" => "sp",
269 "width" => "x",
270 "height" => "y",
271 "reset_timeout" => "weight",
272 "swap_time" => "value",
273 "difficulty" => "level",
274 "darkness" => "invisible",
275 "fixed_resettime" => "stand_still",
276 );
277
278 while (my ($k2, $k1) = each %normalize) {
279 if (defined (my $v = delete $ob->{$k1})) {
280 $ob->{$k2} = $v;
281 }
282 }
283 } else {
284 # if value matches archetype default, delete
285 while (my ($k, $v) = each %$ob) {
286 if (exists $arch->{$k} and $arch->{$k} eq $v) {
287 next if $k eq "_name";
288 delete $ob->{$k};
289 }
290 }
291 }
292
293 # a speciality for the editor
294 if (exists $ob->{attack_movement}) {
295 my $am = delete $ob->{attack_movement};
296 $ob->{attack_movement_bits_0_3} = $am & 15;
297 $ob->{attack_movement_bits_4_7} = $am & 240;
298 }
299
300 $ob
301}
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
26sub read_pak($;$) { 321sub read_pak($) {
27 my ($path, $cache) = @_; 322 my ($path) = @_;
28 323
29 eval {
30 defined $cache
31 && -M $cache < -M $path
32 && Storable::retrieve $cache
33 } or do {
34 my %pak; 324 my %pak;
35 325
36 open my $fh, "<:raw", $path 326 open my $fh, "<:raw:perlio", $path
37 or die "$_[0]: $!"; 327 or Carp::croak "$_[0]: $!";
328 binmode $fh;
38 while (<$fh>) { 329 while (<$fh>) {
39 my ($type, $id, $len, $path) = split; 330 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 331 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 332 read $fh, $pak{$path}, $len;
42 } 333 }
43 334
44 Storable::nstore \%pak, $cache
45 if defined $cache;
46
47 \%pak 335 \%pak
48 }
49} 336}
50 337
51sub read_arch($;$) { 338sub read_arch($;$) {
52 my ($path, $cache) = @_; 339 my ($path, $toplevel) = @_;
53 340
54 eval {
55 defined $cache
56 && -M $cache < -M $path
57 && Storable::retrieve $cache
58 } or do {
59 my %arc; 341 my %arc;
60 my ($more, $prev); 342 my ($more, $prev);
61 343
62 open my $fh, "<:raw", $path 344 open my $fh, "<:raw:perlio:utf8", $path
63 or die "$path: $!"; 345 or Carp::croak "$path: $!";
64 346
347# binmode $fh;
348
65 my $parse_block; $parse_block = sub { 349 my $parse_block; $parse_block = sub {
66 my %arc = @_; 350 my %arc = @_;
67
68 while (<$fh>) {
69 s/\s+$//;
70 if (/^end$/i) {
71 last;
72 } elsif (/^arch (\S+)$/) {
73 push @{ $arc{inventory} }, $parse_block->(_name => $1);
74 } elsif (/^lore$/) {
75 while (<$fh>) {
76 last if /^endlore\s*$/i;
77 $arc{lore} .= $_;
78 }
79 } elsif (/^msg$/) {
80 while (<$fh>) {
81 last if /^endmsg\s*$/i;
82 $arc{msg} .= $_;
83 }
84 } elsif (/^(\S+)\s*(.*)$/) {
85 $arc{lc $1} = $2;
86 } elsif (/^\s*($|#)/) {
87 #
88 } else {
89 warn "$path: unparsable line '$_' in arch $arc{_name}";
90 }
91 }
92
93 \%arc
94 };
95 351
96 while (<$fh>) { 352 while (<$fh>) {
97 s/\s+$//; 353 s/\s+$//;
98 if (/^more$/i) { 354 if (/^end$/i) {
99 $more = $prev; 355 last;
100 } elsif (/^object (\S+)$/i) { 356 } elsif (/^arch (\S+)$/i) {
101 my $name = $1; 357 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
102 my $arc = $parse_block->(_name => $name); 358 } elsif (/^lore$/i) {
103 359 while (<$fh>) {
104 if ($more) { 360 last if /^endlore\s*$/i;
105 $more->{more} = $arc;
106 } else {
107 $arc{$name} = $arc; 361 $arc{lore} .= $_;
108 } 362 }
109 $prev = $arc; 363 } elsif (/^msg$/i) {
110 $more = undef; 364 while (<$fh>) {
365 last if /^endmsg\s*$/i;
366 $arc{msg} .= $_;
367 }
368 } elsif (/^anim$/i) {
369 while (<$fh>) {
370 last if /^mina\s*$/i;
371 chomp;
372 push @{ $arc{anim} }, $_;
373 }
111 } elsif (/^arch (\S+)$/i) { 374 } elsif (/^(\S+)\s*(.*)$/) {
112 push @{ $arc{arch} }, $parse_block->(_name => $1); 375 $arc{lc $1} = $2;
113 } elsif (/^\s*($|#)/) { 376 } elsif (/^\s*($|#)/) {
114 # 377 #
115 } else { 378 } else {
116 warn "$path: unparseable top-level line '$_'"; 379 warn "$path: unparsable line '$_' in arch $arc{_name}";
117 } 380 }
118 } 381 }
119 382
120 undef $parse_block; # work around bug in perl not freeing $fh etc.
121
122 Storable::nstore \%arc, $cache
123 if defined $cache;
124
125 \%arc 383 \%arc
126 } 384 };
385
386 while (<$fh>) {
387 s/\s+$//;
388 if (/^more$/i) {
389 $more = $prev;
390 } elsif (/^object (\S+)$/i) {
391 my $name = $1;
392 my $arc = attr_thaw normalize_object $parse_block->(_name => $name);
393 $arc->{_atype} = 'object';
394
395 if ($more) {
396 $more->{more} = $arc;
397 } else {
398 $arc{$name} = $arc;
399 }
400 $prev = $arc;
401 $more = undef;
402 } elsif (/^arch (\S+)$/i) {
403 my $name = $1;
404 my $arc = attr_thaw normalize_arch $parse_block->(_name => $name);
405 $arc->{_atype} = 'arch';
406
407 if ($more) {
408 $more->{more} = $arc;
409 } else {
410 push @{ $arc{arch} }, $arc;
411 }
412 $prev = $arc;
413 $more = undef;
414 } elsif ($toplevel && /^(\S+)\s+(.*)$/) {
415 if ($1 eq "lev_array") {
416 while (<$fh>) {
417 last if /^endplst\s*$/;
418 push @{$toplevel->{lev_array}}, $_+0;
419 }
420 } else {
421 $toplevel->{$1} = $2;
422 }
423 } elsif (/^\s*($|#)/) {
424 #
425 } else {
426 die "$path: unparseable top-level line '$_'";
427 }
428 }
429
430 undef $parse_block; # work around bug in perl not freeing $fh etc.
431
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
127} 531}
128 532
129# 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
130# NOTE: the arrays in the hash values are references to 534# NOTE: the arrays in the hash values are references to
131# the archs from $ARCH 535# the archs from $ARCH
132sub editor_archs { 536sub editor_archs {
133 my %paths; 537 my %paths;
134 538
135 for (keys %$ARCH) { 539 for (keys %ARCH) {
136 my $arch = $ARCH->{$_}; 540 my $arch = $ARCH{$_};
137 push @{$paths{$arch->{editor_folder}}}, \$arch; 541 push @{$paths{$arch->{editor_folder}}}, $arch;
138 } 542 }
139 543
140 return \%paths; 544 \%paths
141} 545}
142 546
143# arch_extends determines how the arch looks like on the map, 547=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
144# bigfaces, linked faces and single faces are handled here 548
145# it returns (<xoffset>, <yoffset>, <width>, <height>) 549arch_extents determines the extents of the given arch's face(s), linked
146# NOTE: non rectangular linked faces are not considered 550faces and single faces are handled here it returns (minx, miny, maxx,
551maxy)
552
553=cut
554
147sub arch_extends { 555sub arch_extents {
148 my ($a) = @_; 556 my ($a) = @_;
149 557
150 my $TC = \%Crossfire::Tilecache::TILECACHE; 558 my $o = $ARCH{$a->{_name}}
151
152 my $facename =
153 $a->{face} || $ARCH->{$a->{_name}}->{face}
154 or return (); 559 or return;
155 560
156 my $tile = $TC->{$facename} 561 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
157 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 562 or (warn "no face data found for arch '$a->{_name}'"), return;
158 563
159 if ($tile->{w} > 1 || $tile->{h} > 1) { 564 if ($face->{w} > 1 || $face->{h} > 1) {
160 # bigfaces 565 # bigface
161 return (0, 0, $tile->{w}, $tile->{h}); 566 return (0, 0, $face->{w} - 1, $face->{h} - 1);
162 567
163 } elsif ($a->{more}) { 568 } elsif ($o->{more}) {
164 # linked faces 569 # linked face
165 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 570 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
166 do {
167 $miw > (0 + $a->{x}) and $miw = $a->{x};
168 $mih > (0 + $a->{y}) and $mih = $a->{y};
169 $maw < (0 + $a->{x}) and $maw = $a->{x};
170 $mah < (0 + $a->{y}) and $mah = $a->{y};
171 } while $a = $a->{more};
172 571
173 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) 572 for (; $o; $o = $o->{more}) {
573 $minx = min $minx, $o->{x};
574 $miny = min $miny, $o->{y};
575 $maxx = max $maxx, $o->{x};
576 $maxy = max $maxy, $o->{y};
577 }
578
579 return ($minx, $miny, $maxx, $maxy);
174 580
175 } else { 581 } else {
176 # single face 582 # single face
177 return (0, 0, 1, 1); 583 return (0, 0, 0, 0);
584 }
585}
586
587=item $type = arch_attr $arch
588
589Returns a hashref describing the object and its attributes. It can contain
590the following keys:
591
592 name the name, suitable for display purposes
593 ignore
594 attr
595 desc
596 use
597 section => [name => \%attr, name => \%attr]
598 import
599
600=cut
601
602sub arch_attr($) {
603 my ($obj) = @_;
604
605 require Crossfire::Data;
606
607 my $root;
608 my $attr = { };
609
610 my $arch = $ARCH{ $obj->{_name} };
611 my $type = $obj->{type} || $arch->{type};
612
613 if ($type > 0) {
614 $root = $Crossfire::Data::ATTR{$type};
615 } else {
616 my %a = (%$arch, %$obj);
617
618 if ($a{is_floor} && !$a{alive}) {
619 $root = $Crossfire::Data::TYPE{Floor};
620 } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) {
621 $root = $Crossfire::Data::TYPE{"Monster & NPC"};
622 } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) {
623 $root = $Crossfire::Data::TYPE{Wall};
624 } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) {
625 $root = $Crossfire::Data::TYPE{"Weak Wall"};
626 } else {
627 $root = $Crossfire::Data::TYPE{Misc};
628 }
629 }
630
631 my @import = ($root);
632
633 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
634 unless $type == 116;
635
636 my (%ignore);
637 my (@section_order, %section, @attr_order);
638
639 while (my $type = shift @import) {
640 push @import, @{$type->{import} || []};
641
642 $attr->{$_} ||= $type->{$_}
643 for qw(name desc use);
644
645 for (@{$type->{ignore} || []}) {
646 $ignore{$_}++ for ref $_ ? @$_ : $_;
647 }
648
649 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
650 my ($name, $attr) = @$_;
651 push @section_order, $name;
652 for (@$attr) {
653 my ($k, $v) = @$_;
654 push @attr_order, $k;
655 $section{$name}{$k} ||= $v;
656 }
657 }
658 }
659
660 $attr->{section} = [
661 map !exists $section{$_} ? () : do {
662 my $attr = delete $section{$_};
663
664 [
665 $_,
666 map exists $attr->{$_} && !$ignore{$_}
667 ? [$_ => delete $attr->{$_}] : (),
668 @attr_order
669 ]
670 },
671
672 exists $section{$_} ? [$_ => delete $section{$_}] : (),
673 @section_order
674 ];
675
676 $attr
677}
678
679sub arch_edit_sections {
680# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
681# edit_type = 0;
682# else if (edit_type != 0) {
683# // all flags from 'check_type' must be unset in this arch because they get recalculated now
684# edit_type &= ~check_type;
685# }
686#
687# }
688# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
689# getAttributeValue("alive", defarch) == 1 &&
690# (getAttributeValue("monster", defarch) == 1 ||
691# getAttributeValue("generator", defarch) == 1)) {
692# // Monster: monsters/npcs/generators
693# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
694# }
695# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
696# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
697# // Walls
698# edit_type |= IGUIConstants.TILE_EDIT_WALL;
699# }
700# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
701# getAttributeValue("connected", defarch) != 0) {
702# // Connected Objects
703# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
704# }
705# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
706# arch_type == 66 || arch_type == 41 || arch_type == 95) {
707# // Exit: teleporter/exit/trapdoors
708# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
709# }
710# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
711# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
712# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
713# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
714# arch_type == 124 || arch_type == 130)) {
715# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
716# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
717# }
718# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
719# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
720# arch_type == 91 || arch_type == 21 || arch_type == 24) {
721# // Door: door/special door/gates + keys
722# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
723# }
724# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
725# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
726# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
727# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
728# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
729# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
730# arch_type == 122 || arch_type == 3)) {
731# // Equipment: weapons/armour/wands/rods
732# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
733# }
734#
735# return(edit_type);
736#
737#
738}
739
740sub cache_file($$&&) {
741 my ($src, $cache, $load, $create) = @_;
742
743 my ($size, $mtime) = (stat $src)[7,9]
744 or Carp::croak "$src: $!";
745
746 if (-e $cache) {
747 my $ref = eval { load_ref $cache };
748
749 if ($ref->{version} == 1
750 && $ref->{size} == $size
751 && $ref->{mtime} == $mtime
752 && eval { $load->($ref->{data}); 1 }) {
753 return;
754 }
755 }
756
757 my $ref = {
758 version => 1,
759 size => $size,
760 mtime => $mtime,
761 data => $create->(),
178 } 762 };
179}
180 763
181# arch2pickmap forms a list of archs to a pickmap 764 $load->($ref->{data});
182sub arch2pickmap {
183 my ($archs, $w) = @_;
184 765
185 # sort archs alphabetiacally 766 save_ref $ref, $cache;
186 my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ]; 767}
187 768
188 $w ||= 10; # default width 769=item set_libdir $path
189 my $num = @$archs; 770
190 my $map = { }; 771Sets the library directory to the given path
191 # overall placement coords 772(default: $ENV{CROSSFIRE_LIBDIR}).
773
774You have to (re-)load the archetypes and tilecache manually after steting
775the library path.
776
777=cut
778
779sub set_libdir($) {
780 $LIB = $_[0];
781}
782
783=item load_archetypes
784
785(Re-)Load archetypes into %ARCH.
786
787=cut
788
789sub load_archetypes() {
790 cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub {
791 *ARCH = $_[0];
792 }, sub {
793 read_arch "$LIB/archetypes"
794 };
795}
796
797=item load_tilecache
798
799(Re-)Load %TILE and %FACE.
800
801=cut
802
803sub load_tilecache() {
804 require Gtk2;
805
806 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub {
807 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
808 or die "$VARDIR/tilecache.png: $!";
809 *FACE = $_[0];
810 }, sub {
811 my $tile = read_pak "$LIB/crossfire.0";
812
813 my %cache;
814
192 my $x = 0; 815 my $idx = 0;
193 my $y = 0;
194 816
195 my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later 817 for my $name (sort keys %$tile) {
196 my $drawn_archs = 1; # line-break counter 818 my $pb = new Gtk2::Gdk::PixbufLoader;
197 my $max_line_height = 1; 819 $pb->write ($tile->{$name});
820 $pb->close;
821 my $pb = $pb->get_pixbuf;
198 822
199 for (my $i = 0; $i < $num; $i++) { 823 my $tile = $cache{$name} = {
200 824 pb => $pb,
201 defined ${$archs->[$i]}->{face} or next; 825 idx => $idx,
202 826 w => int $pb->get_width / TILESIZE,
203 # check whether this tile was already written (see below at (b)) 827 h => int $pb->get_height / TILESIZE,
204 unless (defined $map->{map}[$x][$y]) { 828 };
205
206 my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
207
208 # these are special placement coords, for chained faces which
209 # have a special placement offset
210 my ($place_x, $place_y) = ($x, $y);
211 $xoffs < 0 and
212 $place_x += -$xoffs;
213 $yoffs < 0 and
214 $place_y += -$yoffs;
215
216 # iterate over the tiles this arch takes
217 # NOTE: Chained archs are maybe not a rectangle, but i don't care
218 # much for that on pickmaps
219
220 for (my $xi = 0; $xi < $arch_w; $xi++) {
221 for (my $yi = 0; $yi < $arch_h; $yi++) {
222
223 my ($lx, $ly) = ($x + $xi, $y + $yi);
224
225 if ($lx == $place_x and $ly == $place_y) {
226 push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
227
228 } else {
229
230 # (b): here we set occupied tiles, but without the arch
231 $map->{map}[$lx][$ly] = [];
232 } 829
830
831 $idx += $tile->{w} * $tile->{h};
832 }
833
834 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
835
836 while (my ($name, $tile) = each %cache) {
837 my $tpb = delete $tile->{pb};
838 my $ofs = $tile->{idx};
839
840 for my $x (0 .. $tile->{w} - 1) {
841 for my $y (0 .. $tile->{h} - 1) {
842 my $idx = $ofs + $x + $y * $tile->{w};
843 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
844 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
233 } 845 }
234 } 846 }
235 $drawn_archs++;
236
237 $x += $arch_w - 1;
238 $max_line_height < $arch_h
239 and $max_line_height = $arch_h;
240
241 } else {
242 $i--;
243 } 847 }
244 848
849 $pb->save ("$VARDIR/tilecache.png", "png", compression => 1);
245 850
246 $x++; 851 \%cache
247
248 if ($x > $w) {
249
250 $y += $max_line_height;
251 $max_line_height = 1;
252 $x = 0;
253 }
254
255 $maxw < ($x + 1) and $maxw = $x + 1;
256 $maxh < ($y + 1) and $maxh = $y + 1;
257 } 852 };
258
259 $map->{height} = $maxh;
260 $map->{width} = $maxw;
261
262 return $map;
263}
264
265sub arch2map($;$) {
266 my ($mapa) = @_;
267
268 my %meta;
269
270 my ($mapx, $mapy);
271
272 my $map;
273
274 for (@{ $mapa->{arch} }) {
275 my ($x, $y) = (delete $_->{x}, delete $_->{y});
276
277 if ($_->{_name} eq "map") {
278 $meta{info} = $_;
279
280 $mapx = $_->{width} || $x;
281 $mapy = $_->{height} || $y;
282 } else {
283 push @{ $map->[$x][$y] }, $_;
284
285 # arch map is unreliable w.r.t. width and height
286 $mapx = $x + 1 if $mapx <= $x;
287 $mapy = $y + 1 if $mapy <= $y;
288 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
289 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
290 }
291 }
292
293 $meta{width} = $mapx;
294 $meta{height} = $mapy;
295 $meta{map} = $map;
296
297 \%meta
298}
299
300sub init($) {
301 my ($cachedir) = @_;
302
303 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
304} 853}
305 854
306=head1 AUTHOR 855=head1 AUTHOR
307 856
308 Marc Lehmann <schmorp@schmorp.de> 857 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines