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.15 by root, Sun Feb 12 04:50:24 2006 UTC vs.
Revision 1.101 by root, Tue Apr 10 09:37:03 2007 UTC

4 4
5=cut 5=cut
6 6
7package Crossfire; 7package Crossfire;
8 8
9our $VERSION = '0.1'; 9our $VERSION = '0.98';
10 10
11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp (); 15use Carp ();
16use Storable; 16use File::Spec;
17use List::Util qw(min max); 17use List::Util qw(min max);
18use Storable qw(freeze thaw);
18 19
19#XXX: The map_* procedures scream for a map-object
20
21our @EXPORT = 20our @EXPORT = qw(
22 qw(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);
23 23
24use JSON::XS qw(from_json to_json);
25
24our $LIB = $ENV{CROSSFIRE_LIBDIR} 26our $LIB = $ENV{CROSSFIRE_LIBDIR};
25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; 27
28our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire"
29 : $ENV{AppData} ? "$ENV{APPDATA}/crossfire"
30 : File::Spec->tmpdir . "/crossfire";
31
32mkdir $VARDIR, 0777;
26 33
27sub TILESIZE (){ 32 } 34sub TILESIZE (){ 32 }
28 35
29our $CACHEDIR;
30our %ARCH; 36our %ARCH;
31our %FACE; 37our %FACE;
32our $TILE; 38our $TILE;
33 39
34our %FIELD_MULTILINE = ( 40our %FIELD_MULTILINE = (
35 msg => "endmsg", 41 msg => "endmsg",
36 lore => "endlore", 42 lore => "endlore",
43 maplore => "endmaplore",
37); 44);
38 45
39# not used yet, maybe alphabetical is ok 46# movement bit type, PITA
47our %FIELD_MOVEMENT = map +($_ => undef),
48 qw(move_type move_block move_allow move_on move_off move_slow);
49
50# same as in server save routine, to (hopefully) be compatible
51# to the other editors.
52our @FIELD_ORDER_MAP = (qw(
53 file_format_version
54 name attach swap_time reset_timeout fixed_resettime difficulty region
55 shopitems shopgreed shopmin shopmax shoprace
56 darkness width height enter_x enter_y msg maplore
57 unique template
58 outdoor temp pressure humid windspeed winddir sky nosmooth
59 tile_path_1 tile_path_2 tile_path_3 tile_path_4
60));
61
40our @FIELD_ORDER = (qw(name name_pl)); 62our @FIELD_ORDER = (qw(
63 elevation
41 64
65 name name_pl custom_name attach title race
66 slaying skill msg lore other_arch
67 is_animated animation face
68 magicmap smoothlevel smoothface
69 str dex con wis pow cha int
70 hp maxhp sp maxsp grace maxgrace
71 exp perm_exp expmul
72 food dam luck wc ac x y speed speed_left move_state attack_movement
73 nrof level direction type subtype attacktype
74
75 resist_physical resist_magic resist_fire resist_electricity
76 resist_cold resist_confusion resist_acid resist_drain
77 resist_weaponmagic resist_ghosthit resist_poison resist_slow
78 resist_paralyze resist_turn_undead resist_fear resist_cancellation
79 resist_deplete resist_death resist_chaos resist_counterspell
80 resist_godpower resist_holyword resist_blind resist_internal
81 resist_life_stealing resist_disease
82
83 path_attuned path_repelled path_denied material materialname
84 value carrying weight invisible state magic
85 last_heal last_sp last_grace last_eat
86 connected glow_radius randomitems npx_status npc_program
87 run_away pick_up container will_apply smoothlevel
88 current_weapon_script weapontype tooltype elevation client_type
89 item_power duration range
90 range_modifier duration_modifier dam_modifier gen_sp_armour
91 move_type move_block move_allow move_on move_off move_on move_slow move_slow_penalty
92
93 alive wiz was_wiz applied unpaid can_use_shield no_pick is_animated monster
94 friendly generator is_thrown auto_apply treasure player sold see_invisible
95 can_roll overlay_floor is_turnable is_used_up identified reflecting changing
96 splitting hitback startequip blocksview undead scared unaggressive
97 reflect_missile reflect_spell no_magic no_fix_player is_lightable tear_down
98 run_away pick_up unique no_drop can_cast_spell can_use_scroll can_use_range
99 can_use_bow can_use_armour can_use_weapon can_use_ring has_ready_range
100 has_ready_bow xrays is_floor lifesave no_strength sleep stand_still
101 random_move only_attack confused stealth cursed damned see_anywhere
102 known_magical known_cursed can_use_skill been_applied has_ready_scroll
103 can_use_rod can_use_horn make_invisible inv_locked is_wooded is_hilly
104 has_ready_skill has_ready_weapon no_skill_ident is_blind can_see_in_dark
105 is_cauldron is_dust no_steal one_hit berserk neutral no_attack no_damage
106 activate_on_push activate_on_release is_water use_content_on_gen is_buildable
107
108 body_range body_arm body_torso body_head body_neck body_skill
109 body_finger body_shoulder body_foot body_hand body_wrist body_waist
110));
111
112our %EVENT_TYPE = (
113 apply => 1,
114 attack => 2,
115 death => 3,
116 drop => 4,
117 pickup => 5,
118 say => 6,
119 stop => 7,
120 time => 8,
121 throw => 9,
122 trigger => 10,
123 close => 11,
124 timer => 12,
125);
126
42sub MOVE_WALK (){ 0x1 } 127sub MOVE_WALK (){ 0x01 }
43sub MOVE_FLY_LOW (){ 0x2 } 128sub MOVE_FLY_LOW (){ 0x02 }
44sub MOVE_FLY_HIGH (){ 0x4 } 129sub MOVE_FLY_HIGH (){ 0x04 }
45sub MOVE_FLYING (){ 0x6 } 130sub MOVE_FLYING (){ 0x06 }
46sub MOVE_SWIM (){ 0x8 } 131sub MOVE_SWIM (){ 0x08 }
47sub MOVE_ALL (){ 0xf } 132sub MOVE_BOAT (){ 0x10 }
133sub MOVE_KNOWN (){ 0x1f } # all of above
134sub MOVE_ALL (){ 0x10000 } # very special value
48 135
136our %MOVE_TYPE = (
137 walk => MOVE_WALK,
138 fly_low => MOVE_FLY_LOW,
139 fly_high => MOVE_FLY_HIGH,
140 flying => MOVE_FLYING,
141 swim => MOVE_SWIM,
142 boat => MOVE_BOAT,
143 all => MOVE_ALL,
144);
145
146our @MOVE_TYPE = qw(all walk flying fly_low fly_high swim boat);
147
148{
149 package Crossfire::MoveType;
150
151 use overload
152 '=' => sub { bless [@{$_[0]}], ref $_[0] },
153 '""' => \&as_string,
154 '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef },
155 '+=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] |= $MOVE_TYPE{$_[1]}; &normalise },
156 '-=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] &= ~$MOVE_TYPE{$_[1]}; &normalise },
157 '/=' => sub { $_[0][0] &= ~$MOVE_TYPE{$_[1]}; &normalise },
158 'x=' => sub {
159 my $cur = $_[0] >= $_[1];
160 if (!defined $cur) {
161 if ($_[0] >= "all") {
162 $_[0] -= $_[1];
163 } else {
164 $_[0] += $_[1];
165 }
166 } elsif ($cur) {
167 $_[0] -= $_[1];
168 } else {
169 $_[0] /= $_[1];
170 }
171
172 $_[0]
173 },
174 'eq' => sub { "$_[0]" eq "$_[1]" },
175 'ne' => sub { "$_[0]" ne "$_[1]" },
176 ;
177}
178
179sub Crossfire::MoveType::new {
180 my ($class, $string) = @_;
181
182 my $mask;
183 my $value;
184
185 if ($string =~ /^\s*\d+\s*$/) {
186 $mask = MOVE_ALL;
187 $value = $string+0;
188 } else {
189 for (split /\s+/, lc $string) {
190 if (s/^-//) {
191 $mask |= $MOVE_TYPE{$_};
192 $value &= ~$MOVE_TYPE{$_};
193 } else {
194 $mask |= $MOVE_TYPE{$_};
195 $value |= $MOVE_TYPE{$_};
196 }
197 }
198 }
199
200 (bless [$mask, $value], $class)->normalise
201}
202
203sub Crossfire::MoveType::normalise {
204 my ($self) = @_;
205
206 if ($self->[0] & MOVE_ALL) {
207 my $mask = ~(($self->[1] & MOVE_ALL ? $self->[1] : ~$self->[1]) & $self->[0] & ~MOVE_ALL);
208 $self->[0] &= $mask;
209 $self->[1] &= $mask;
210 }
211
212 $self->[1] &= $self->[0];
213
214 $self
215}
216
217sub Crossfire::MoveType::as_string {
218 my ($self) = @_;
219
220 my @res;
221
222 my ($mask, $value) = @$self;
223
224 for (@Crossfire::MOVE_TYPE) {
225 my $bit = $Crossfire::MOVE_TYPE{$_};
226 if (($mask & $bit) == $bit && (($value & $bit) == $bit || ($value & $bit) == 0)) {
227 $mask &= ~$bit;
228 push @res, $value & $bit ? $_ : "-$_";
229 }
230 }
231
232 join " ", @res
233}
234
235sub load_ref($) {
236 my ($path) = @_;
237
238 open my $fh, "<:raw:perlio", $path
239 or die "$path: $!";
240 local $/;
241
242 thaw <$fh>
243}
244
245sub save_ref($$) {
246 my ($ref, $path) = @_;
247
248 open my $fh, ">:raw:perlio", "$path~"
249 or die "$path~: $!";
250 print $fh freeze $ref;
251 close $fh;
252 rename "$path~", $path
253 or die "$path: $!";
254}
255
256my %attack_mask = (
257 physical => 0x00000001,
258 magic => 0x00000002,
259 fire => 0x00000004,
260 electricity => 0x00000008,
261 cold => 0x00000010,
262 confusion => 0x00000020,
263 acid => 0x00000040,
264 drain => 0x00000080,
265 weaponmagic => 0x00000100,
266 ghosthit => 0x00000200,
267 poison => 0x00000400,
268 slow => 0x00000800,
269 paralyze => 0x00001000,
270 turn_undead => 0x00002000,
271 fear => 0x00004000,
272 cancellation => 0x00008000,
273 deplete => 0x00010000,
274 death => 0x00020000,
275 chaos => 0x00040000,
276 counterspell => 0x00080000,
277 godpower => 0x00100000,
278 holyword => 0x00200000,
279 blind => 0x00400000,
280 internal => 0x00800000,
281 life_stealing => 0x01000000,
282 disease => 0x02000000,
283);
284
285sub _add_resist($$$) {
286 my ($ob, $mask, $value) = @_;
287
288 while (my ($k, $v) = each %attack_mask) {
289 $ob->{"resist_$k"} = min 100, max -100, $ob->{"resist_$k"} + $value if $mask & $v;
290 }
291}
292
293my %MATERIAL = reverse
294 paper => 1,
295 iron => 2,
296 glass => 4,
297 leather => 8,
298 wood => 16,
299 organic => 32,
300 stone => 64,
301 cloth => 128,
302 adamant => 256,
303 liquid => 512,
304 tin => 1024,
305 bone => 2048,
306 ice => 4096,
307
308 # guesses
309 runestone => 12,
310 bronze => 18,
311 "ancient wood" => 20,
312 glass => 36,
313 marble => 66,
314 ice => 68,
315 stone => 70,
316 stone => 80,
317 cloth => 136,
318 ironwood => 144,
319 adamantium => 258,
320 glacium => 260,
321 blood => 544,
322;
323
324# object as in "Object xxx", i.e. archetypes
325sub normalize_object($) {
326 my ($ob) = @_;
327
328 # convert material bitset to materialname, if possible
329 if (exists $ob->{material}) {
330 if (!$ob->{material}) {
331 delete $ob->{material};
332 } elsif (exists $ob->{materialname}) {
333 if ($MATERIAL{$ob->{material}} eq $ob->{materialname}) {
334 delete $ob->{material};
335 } else {
336 warn "object $ob->{_name} has both materialname ($ob->{materialname}) and material ($ob->{material}) set.\n";
337 delete $ob->{material}; # assume materilname is more specific and nuke material
338 }
339 } elsif (my $name = $MATERIAL{$ob->{material}}) {
340 delete $ob->{material};
341 $ob->{materialname} = $name;
342 } else {
343 warn "object $ob->{_name} has unknown material ($ob->{material}) set.\n";
344 }
345 }
346
347 # color_fg is used as default for magicmap if magicmap does not exist
348 $ob->{magicmap} ||= delete $ob->{color_fg} if exists $ob->{color_fg};
349
350 # nuke outdated or never supported fields
351 delete @$ob{qw(
352 can_knockback can_parry can_impale can_cut can_dam_armour
353 can_apply pass_thru can_pass_thru color_bg color_fg
354 )};
355
356 if (my $mask = delete $ob->{immune} ) { _add_resist $ob, $mask, 100; }
357 if (my $mask = delete $ob->{protected} ) { _add_resist $ob, $mask, 30; }
358 if (my $mask = delete $ob->{vulnerable}) { _add_resist $ob, $mask, -100; }
359
360 # convert movement strings to bitsets
361 for my $attr (keys %FIELD_MOVEMENT) {
362 next unless exists $ob->{$attr};
363
364 $ob->{$attr} = new Crossfire::MoveType $ob->{$attr};
365 }
366
367 # convert outdated movement flags to new movement sets
368 if (defined (my $v = delete $ob->{no_pass})) {
369 $ob->{move_block} = new Crossfire::MoveType $v ? "all" : "";
370 }
371 if (defined (my $v = delete $ob->{slow_move})) {
372 $ob->{move_slow} += "walk";
373 $ob->{move_slow_penalty} = $v;
374 }
375 if (defined (my $v = delete $ob->{walk_on})) {
376 $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" }
377 }
378 if (defined (my $v = delete $ob->{walk_off})) {
379 $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" }
380 }
381 if (defined (my $v = delete $ob->{fly_on})) {
382 $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" }
383 }
384 if (defined (my $v = delete $ob->{fly_off})) {
385 $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" }
386 }
387 if (defined (my $v = delete $ob->{flying})) {
388 $ob->{move_type} ||= new Crossfire::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" }
389 }
390
391 # convert idiotic event_xxx things into objects
392 while (my ($event, $subtype) = each %EVENT_TYPE) {
393 if (exists $ob->{"event_${event}_plugin"}) {
394 push @{$ob->{inventory}}, {
395 _name => "event_$event",
396 title => delete $ob->{"event_${event}_plugin"},
397 slaying => delete $ob->{"event_${event}"},
398 name => delete $ob->{"event_${event}_options"},
399 };
400 }
401 }
402
403 # some archetypes had "+3" instead of the canonical "3", so fix
404 $ob->{dam} *= 1 if exists $ob->{dam};
405
406 $ob
407}
408
409# arch as in "arch xxx", ie.. objects
49sub normalize_arch($) { 410sub normalize_arch($) {
50 my ($ob) = @_; 411 my ($ob) = @_;
51 412
413 normalize_object $ob;
414
52 my $arch = $ARCH{$ob->{_name}} 415 my $arch = $ARCH{$ob->{_name}}
53 or (warn "$ob->{_name}: no such archetype", return $ob); 416 or (warn "$ob->{_name}: no such archetype", return $ob);
54
55 delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply);
56 417
57 if ($arch->{type} == 22) { # map 418 if ($arch->{type} == 22) { # map
58 my %normalize = ( 419 my %normalize = (
59 "enter_x" => "hp", 420 "enter_x" => "hp",
60 "enter_y" => "sp", 421 "enter_y" => "sp",
70 while (my ($k2, $k1) = each %normalize) { 431 while (my ($k2, $k1) = each %normalize) {
71 if (defined (my $v = delete $ob->{$k1})) { 432 if (defined (my $v = delete $ob->{$k1})) {
72 $ob->{$k2} = $v; 433 $ob->{$k2} = $v;
73 } 434 }
74 } 435 }
75 } 436 } else {
76
77 if (defined (my $v = delete $ob->{no_pass})) {
78 $ob->{move_block} = $v ? MOVE_ALL : 0;
79 }
80 if (defined (my $v = delete $ob->{walk_on})) {
81 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
82 : $ob->{move_on} & ~MOVE_WALK;
83 }
84 if (defined (my $v = delete $ob->{walk_off})) {
85 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
86 : $ob->{move_off} & ~MOVE_WALK;
87 }
88 if (defined (my $v = delete $ob->{fly_on})) {
89 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
90 : $ob->{move_on} & ~MOVE_FLY_LOW;
91 }
92 if (defined (my $v = delete $ob->{fly_off})) {
93 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
94 : $ob->{move_off} & ~MOVE_FLY_LOW;
95 }
96 if (defined (my $v = delete $ob->{flying})) {
97 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
98 : $ob->{move_type} & ~MOVE_FLY_LOW;
99 }
100
101 # if value matches archetype default, delete 437 # if value matches archetype default, delete
102 while (my ($k, $v) = each %$ob) { 438 while (my ($k, $v) = each %$ob) {
103 if (exists $arch->{$k} and $arch->{$k} eq $v) { 439 if (exists $arch->{$k} and $arch->{$k} eq $v) {
104 next if $k eq "_name"; 440 next if $k eq "_name";
105 delete $ob->{$k}; 441 delete $ob->{$k};
106 } 442 }
443 }
444 }
445
446 # a speciality for the editor
447 if (exists $ob->{attack_movement}) {
448 my $am = delete $ob->{attack_movement};
449 $ob->{attack_movement_bits_0_3} = $am & 15;
450 $ob->{attack_movement_bits_4_7} = $am & 240;
107 } 451 }
108 452
109 $ob 453 $ob
110} 454}
111 455
456sub attr_thaw($) {
457 my ($ob) = @_;
458
459 $ob->{attach} = from_json $ob->{attach}
460 if exists $ob->{attach};
461
462 $ob
463}
464
465sub attr_freeze($) {
466 my ($ob) = @_;
467
468 $ob->{attach} = Crossfire::to_json $ob->{attach}
469 if exists $ob->{attach};
470
471 $ob
472}
473
112sub read_pak($;$) { 474sub read_pak($) {
113 my ($path, $cache) = @_; 475 my ($path) = @_;
114 476
115 eval {
116 defined $cache
117 && -M $cache < -M $path
118 && Storable::retrieve $cache
119 } or do {
120 my %pak; 477 my %pak;
121 478
122 open my $fh, "<:raw", $path 479 open my $fh, "<:raw:perlio", $path
123 or Carp::croak "$_[0]: $!"; 480 or Carp::croak "$_[0]: $!";
481 binmode $fh;
124 while (<$fh>) { 482 while (<$fh>) {
125 my ($type, $id, $len, $path) = split; 483 my ($type, $id, $len, $path) = split;
126 $path =~ s/.*\///; 484 $path =~ s/.*\///;
127 read $fh, $pak{$path}, $len; 485 read $fh, $pak{$path}, $len;
128 } 486 }
129 487
130 Storable::nstore \%pak, $cache
131 if defined $cache;
132
133 \%pak 488 \%pak
134 }
135} 489}
136 490
137sub read_arch($;$) { 491sub read_arch($;$) {
138 my ($path, $cache) = @_; 492 my ($path, $toplevel) = @_;
139 493
140 eval {
141 defined $cache
142 && -M $cache < -M $path
143 && Storable::retrieve $cache
144 } or do {
145 my %arc; 494 my %arc;
146 my ($more, $prev); 495 my ($more, $prev);
496 my $comment;
147 497
148 open my $fh, "<:raw", $path 498 open my $fh, "<:raw:perlio:utf8", $path
149 or Carp::croak "$path: $!"; 499 or Carp::croak "$path: $!";
150 500
501# binmode $fh;
502
151 my $parse_block; $parse_block = sub { 503 my $parse_block; $parse_block = sub {
152 my %arc = @_; 504 my %arc = @_;
153
154 while (<$fh>) {
155 s/\s+$//;
156 if (/^end$/i) {
157 last;
158 } elsif (/^arch (\S+)$/) {
159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
160 } elsif (/^lore$/) {
161 while (<$fh>) {
162 last if /^endlore\s*$/i;
163 $arc{lore} .= $_;
164 }
165 } elsif (/^msg$/) {
166 while (<$fh>) {
167 last if /^endmsg\s*$/i;
168 $arc{msg} .= $_;
169 }
170 } elsif (/^(\S+)\s*(.*)$/) {
171 $arc{lc $1} = $2;
172 } elsif (/^\s*($|#)/) {
173 #
174 } else {
175 warn "$path: unparsable line '$_' in arch $arc{_name}";
176 }
177 }
178
179 \%arc
180 };
181 505
182 while (<$fh>) { 506 while (<$fh>) {
183 s/\s+$//; 507 s/\s+$//;
184 if (/^more$/i) { 508 if (/^end$/i) {
185 $more = $prev; 509 last;
510
186 } elsif (/^object (\S+)$/i) { 511 } elsif (/^arch (\S+)$/i) {
187 my $name = $1; 512 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
188 my $arc = $parse_block->(_name => $name);
189 513
190 if ($more) { 514 } elsif (/^lore$/i) {
191 $more->{more} = $arc; 515 while (<$fh>) {
192 } else { 516 last if /^endlore\s*$/i;
193 $arc{$name} = $arc; 517 $arc{lore} .= $_;
194 } 518 }
195 $prev = $arc; 519 } elsif (/^msg$/i) {
196 $more = undef; 520 while (<$fh>) {
521 last if /^endmsg\s*$/i;
522 $arc{msg} .= $_;
523 }
524 } elsif (/^anim$/i) {
525 while (<$fh>) {
526 last if /^mina\s*$/i;
527 chomp;
528 push @{ $arc{anim} }, $_;
529 }
197 } elsif (/^arch (\S+)$/i) { 530 } elsif (/^(\S+)\s*(.*)$/) {
198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); 531 $arc{lc $1} = $2;
199 } elsif (/^\s*($|#)/) { 532 } elsif (/^\s*#/) {
533 $arc{_comment} .= "$_\n";
534
535 } elsif (/^\s*$/) {
200 # 536 #
201 } else { 537 } else {
202 warn "$path: unparseable top-level line '$_'"; 538 warn "$path: unparsable line '$_' in arch $arc{_name}";
203 }
204 } 539 }
205 540 }
206 undef $parse_block; # work around bug in perl not freeing $fh etc.
207
208 Storable::nstore \%arc, $cache
209 if defined $cache;
210 541
211 \%arc 542 \%arc
212 } 543 };
544
545 while (<$fh>) {
546 s/\s+$//;
547 if (/^more$/i) {
548 $more = $prev;
549 } elsif (/^object (\S+)$/i) {
550 my $name = $1;
551 my $arc = attr_thaw normalize_object $parse_block->(_name => $name, _comment => $comment);
552 undef $comment;
553 delete $arc{_comment} unless length $arc{_comment};
554 $arc->{_atype} = 'object';
555
556 if ($more) {
557 $more->{more} = $arc;
558 } else {
559 $arc{$name} = $arc;
560 }
561 $prev = $arc;
562 $more = undef;
563 } elsif (/^arch (\S+)$/i) {
564 my $name = $1;
565 my $arc = attr_thaw normalize_arch $parse_block->(_name => $name, _comment => $comment);
566 undef $comment;
567 delete $arc{_comment} unless length $arc{_comment};
568 $arc->{_atype} = 'arch';
569
570 if ($more) {
571 $more->{more} = $arc;
572 } else {
573 push @{ $arc{arch} }, $arc;
574 }
575 $prev = $arc;
576 $more = undef;
577 } elsif ($toplevel && /^(\S+)\s+(.*)$/) {
578 if ($1 eq "lev_array") {
579 while (<$fh>) {
580 last if /^endplst\s*$/;
581 push @{$toplevel->{lev_array}}, $_+0;
582 }
583 } else {
584 $toplevel->{$1} = $2;
585 }
586 } elsif (/^\s*#/) {
587 $comment .= "$_\n";
588 } elsif (/^\s*($|#)/) {
589 #
590 } else {
591 die "$path: unparseable top-level line '$_'";
592 }
593 }
594
595 undef $parse_block; # work around bug in perl not freeing $fh etc.
596
597 \%arc
598}
599
600sub archlist_to_string {
601 my ($arch) = @_;
602
603 my $str;
604
605 my $append; $append = sub {
606 my %a = %{$_[0]};
607
608 Crossfire::attr_freeze \%a;
609 Crossfire::normalize_arch \%a;
610
611 # undo the bit-split we did before
612 if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) {
613 $a{attack_movement} = (delete $a{attack_movement_bits_0_3})
614 | (delete $a{attack_movement_bits_4_7});
615 }
616
617 if (my $comment = delete $a{_comment}) {
618 if ($comment =~ /[^\n\s#]/) {
619 $str .= $comment;
620 }
621 }
622
623 $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n";
624
625 my $inv = delete $a{inventory};
626 my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some
627 my $anim = delete $a{anim};
628
629 if ($a{_atype} eq 'object') {
630 $str .= join "\n", "anim", @$anim, "mina\n"
631 if $anim;
632 }
633
634 my @kv;
635
636 for ($a{_name} eq "map"
637 ? @Crossfire::FIELD_ORDER_MAP
638 : @Crossfire::FIELD_ORDER) {
639 push @kv, [$_, delete $a{$_}]
640 if exists $a{$_};
641 }
642
643 for (sort keys %a) {
644 next if /^_/; # ignore our _-keys
645 push @kv, [$_, delete $a{$_}];
646 }
647
648 for (@kv) {
649 my ($k, $v) = @$_;
650
651 if (my $end = $Crossfire::FIELD_MULTILINE{$k}) {
652 $v =~ s/\n$//;
653 $str .= "$k\n$v\n$end\n";
654 } else {
655 $str .= "$k $v\n";
656 }
657 }
658
659 if ($inv) {
660 $append->($_) for @$inv;
661 }
662
663 $str .= "end\n";
664
665 if ($a{_atype} eq 'object') {
666 if ($more) {
667 $str .= "more\n";
668 $append->($more) if $more;
669 } else {
670 $str .= "\n";
671 }
672 }
673 };
674
675 for (@$arch) {
676 $append->($_);
677 }
678
679 $str
213} 680}
214 681
215# put all archs into a hash with editor_face as it's key 682# put all archs into a hash with editor_face as it's key
216# NOTE: the arrays in the hash values are references to 683# NOTE: the arrays in the hash values are references to
217# the archs from $ARCH 684# the archs from $ARCH
218sub editor_archs { 685sub editor_archs {
219 my %paths; 686 my %paths;
220 687
221 for (keys %ARCH) { 688 for (keys %ARCH) {
222 my $arch = $ARCH{$_}; 689 my $arch = $ARCH{$_};
223 push @{$paths{$arch->{editor_folder}}}, \$arch; 690 push @{$paths{$arch->{editor_folder}}}, $arch;
224 } 691 }
225 692
226 \%paths 693 \%paths
227} 694}
228 695
696=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
697
229# arch_extents determines the extents of a given arch 698arch_extents determines the extents of the given arch's face(s), linked
230# bigfaces, linked faces and single faces are handled here 699faces and single faces are handled here it returns (minx, miny, maxx,
231# it returns (minx, miny, maxx, maxy) 700maxy)
701
702=cut
703
232sub arch_extents { 704sub arch_extents {
233 my ($a) = @_; 705 my ($a) = @_;
234 706
235 my $o = $ARCH{$a->{_name}} 707 my $o = $ARCH{$a->{_name}}
236 or return; 708 or return;
237 709
238 my $face = $FACE{$a->{face} || $o->{face}} 710 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
711 unless ($face) {
712 $face = $FACE{"blank.x11"}
239 or (warn "no face data found for arch '$a->{_name}'"), return; 713 or (warn "no face data found for arch '$a->{_name}'"), return;
714 }
240 715
241 if ($face->{w} > 1 || $face->{h} > 1) { 716 if ($face->{w} > 1 || $face->{h} > 1) {
242 # bigface 717 # bigface
243 return (0, 0, $face->{w} - 1, $face->{h} - 1); 718 return (0, 0, $face->{w} - 1, $face->{h} - 1);
244 719
259 # single face 734 # single face
260 return (0, 0, 0, 0); 735 return (0, 0, 0, 0);
261 } 736 }
262} 737}
263 738
264sub init($) { 739=item $type = arch_attr $arch
265 my ($cachedir) = @_;
266 740
267 return if %ARCH; 741Returns a hashref describing the object and its attributes. It can contain
742the following keys:
268 743
269 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 744 name the name, suitable for display purposes
270} 745 ignore
746 attr
747 desc
748 use
749 section => [name => \%attr, name => \%attr]
750 import
271 751
272$CACHEDIR ||= "$ENV{HOME}/.crossfire"; 752=cut
273 753
274init $CACHEDIR; 754sub arch_attr($) {
755 my ($obj) = @_;
756
757 require Crossfire::Data;
758
759 my $root;
760 my $attr = { };
761
762 my $arch = $ARCH{ $obj->{_name} };
763 my $type = $obj->{type} || $arch->{type};
764
765 if ($type > 0) {
766 $root = $Crossfire::Data::ATTR{$type};
767 } else {
768 my %a = (%$arch, %$obj);
769
770 if ($a{is_floor} && !$a{alive}) {
771 $root = $Crossfire::Data::TYPE{Floor};
772 } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) {
773 $root = $Crossfire::Data::TYPE{"Monster & NPC"};
774 } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) {
775 $root = $Crossfire::Data::TYPE{Wall};
776 } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) {
777 $root = $Crossfire::Data::TYPE{"Weak Wall"};
778 } else {
779 $root = $Crossfire::Data::TYPE{Misc};
780 }
781 }
782
783 my @import = ($root);
784
785 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
786 unless $type == 116;
787
788 my (%ignore);
789 my (@section_order, %section, @attr_order);
790
791 while (my $type = shift @import) {
792 push @import, @{$type->{import} || []};
793
794 $attr->{$_} ||= $type->{$_}
795 for qw(name desc use);
796
797 for (@{$type->{ignore} || []}) {
798 $ignore{$_}++ for ref $_ ? @$_ : $_;
799 }
800
801 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
802 my ($name, $attr) = @$_;
803 push @section_order, $name;
804 for (@$attr) {
805 my ($k, $v) = @$_;
806 push @attr_order, $k;
807 $section{$name}{$k} ||= $v;
808 }
809 }
810 }
811
812 $attr->{section} = [
813 map !exists $section{$_} ? () : do {
814 my $attr = delete $section{$_};
815
816 [
817 $_,
818 map exists $attr->{$_} && !$ignore{$_}
819 ? [$_ => delete $attr->{$_}] : (),
820 @attr_order
821 ]
822 },
823
824 exists $section{$_} ? [$_ => delete $section{$_}] : (),
825 @section_order
826 ];
827
828 $attr
829}
830
831sub cache_file($$&&) {
832 my ($src, $cache, $load, $create) = @_;
833
834 my ($size, $mtime) = (stat $src)[7,9]
835 or Carp::croak "$src: $!";
836
837 if (-e $cache) {
838 my $ref = eval { load_ref $cache };
839
840 if ($ref->{version} == 1
841 && $ref->{size} == $size
842 && $ref->{mtime} == $mtime
843 && eval { $load->($ref->{data}); 1 }) {
844 return;
845 }
846 }
847
848 my $ref = {
849 version => 1,
850 size => $size,
851 mtime => $mtime,
852 data => $create->(),
853 };
854
855 $load->($ref->{data});
856
857 save_ref $ref, $cache;
858}
859
860=item set_libdir $path
861
862Sets the library directory to the given path
863(default: $ENV{CROSSFIRE_LIBDIR}).
864
865You have to (re-)load the archetypes and tilecache manually after steting
866the library path.
867
868=cut
869
870sub set_libdir($) {
871 $LIB = $_[0];
872}
873
874=item load_archetypes
875
876(Re-)Load archetypes into %ARCH.
877
878=cut
879
880sub load_archetypes() {
881 cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub {
882 *ARCH = $_[0];
883 }, sub {
884 read_arch "$LIB/archetypes"
885 };
886}
887
888=item load_tilecache
889
890(Re-)Load %TILE and %FACE.
891
892=cut
893
894sub load_tilecache() {
895 require Gtk2;
896
897 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub {
898 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
899 or die "$VARDIR/tilecache.png: $!";
900 *FACE = $_[0];
901 }, sub {
902 my $tile = read_pak "$LIB/crossfire.0";
903
904 my %cache;
905
906 my $idx = 0;
907
908 for my $name (sort keys %$tile) {
909 my $pb = new Gtk2::Gdk::PixbufLoader;
910 $pb->write ($tile->{$name});
911 $pb->close;
912 my $pb = $pb->get_pixbuf;
913
914 my $tile = $cache{$name} = {
915 pb => $pb,
916 idx => $idx,
917 w => int $pb->get_width / TILESIZE,
918 h => int $pb->get_height / TILESIZE,
919 };
920
921
922 $idx += $tile->{w} * $tile->{h};
923 }
924
925 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
926
927 while (my ($name, $tile) = each %cache) {
928 my $tpb = delete $tile->{pb};
929 my $ofs = $tile->{idx};
930
931 for my $x (0 .. $tile->{w} - 1) {
932 for my $y (0 .. $tile->{h} - 1) {
933 my $idx = $ofs + $x + $y * $tile->{w};
934 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
935 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
936 }
937 }
938 }
939
940 $pb->save ("$VARDIR/tilecache.png", "png", compression => 1);
941
942 \%cache
943 };
944}
275 945
276=head1 AUTHOR 946=head1 AUTHOR
277 947
278 Marc Lehmann <schmorp@schmorp.de> 948 Marc Lehmann <schmorp@schmorp.de>
279 http://home.schmorp.de/ 949 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines