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.36 by root, Sun Mar 12 23:21:11 2006 UTC vs.
Revision 1.104 by root, Mon Apr 16 12:32:30 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 File::Spec; 16use File::Spec;
17use List::Util qw(min max); 17use List::Util qw(min max);
18use Storable; 18use Storable qw(freeze thaw);
19 19
20our @EXPORT = 20our @EXPORT = qw(
21 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);
22 23
24use JSON::XS qw(from_json to_json);
25
23our $LIB = $ENV{CROSSFIRE_LIBDIR} 26our $LIB = $ENV{CROSSFIRE_LIBDIR};
24 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;
25 33
26sub TILESIZE (){ 32 } 34sub TILESIZE (){ 32 }
27 35
28our $VARDIR;
29our %ARCH; 36our %ARCH;
30our %FACE; 37our %FACE;
31our $TILE; 38our $TILE;
32 39
33our %FIELD_MULTILINE = ( 40our %FIELD_MULTILINE = (
34 msg => "endmsg", 41 msg => "endmsg",
35 lore => "endlore", 42 lore => "endlore",
43 maplore => "endmaplore",
36); 44);
37 45
38# 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
39our @FIELD_ORDER = (qw(name name_pl)); 62our @FIELD_ORDER = (qw(
63 elevation
40 64
65 name name_pl custom_name attach title race
66 slaying skill msg lore other_arch
67 face animation is_animated
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
41sub MOVE_WALK (){ 0x1 } 127sub MOVE_WALK (){ 0x01 }
42sub MOVE_FLY_LOW (){ 0x2 } 128sub MOVE_FLY_LOW (){ 0x02 }
43sub MOVE_FLY_HIGH (){ 0x4 } 129sub MOVE_FLY_HIGH (){ 0x04 }
44sub MOVE_FLYING (){ 0x6 } 130sub MOVE_FLYING (){ 0x06 }
45sub MOVE_SWIM (){ 0x8 } 131sub MOVE_SWIM (){ 0x08 }
46sub MOVE_ALL (){ 0xf } 132sub MOVE_BOAT (){ 0x10 }
133sub MOVE_KNOWN (){ 0x1f } # all of above
134sub MOVE_ALL (){ 0x10000 } # very special value
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}
47 234
48sub load_ref($) { 235sub load_ref($) {
49 my ($path) = @_; 236 my ($path) = @_;
50 237
51 open my $fh, "<", $path 238 open my $fh, "<:raw:perlio", $path
52 or die "$path: $!"; 239 or die "$path: $!";
53 binmode $fh;
54 local $/; 240 local $/;
55 241
56 Storable::thaw <$fh> 242 thaw <$fh>
57} 243}
58 244
59sub save_ref($$) { 245sub save_ref($$) {
60 my ($ref, $path) = @_; 246 my ($ref, $path) = @_;
61 247
62 open my $fh, ">", "$path~" 248 open my $fh, ">:raw:perlio", "$path~"
63 or die "$path~: $!"; 249 or die "$path~: $!";
64 binmode $fh;
65 print $fh Storable::freeze $ref; 250 print $fh freeze $ref;
66 close $fh; 251 close $fh;
67 rename "$path~", $path 252 rename "$path~", $path
68 or die "$path: $!"; 253 or die "$path: $!";
69} 254}
70 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
71sub normalize_arch($) { 410sub normalize_arch($) {
72 my ($ob) = @_; 411 my ($ob) = @_;
73 412
413 normalize_object $ob;
414
74 my $arch = $ARCH{$ob->{_name}} 415 my $arch = $ARCH{$ob->{_name}}
75 or (warn "$ob->{_name}: no such archetype", return $ob); 416 or (warn "$ob->{_name}: no such archetype", return $ob);
76
77 delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply);
78 417
79 if ($arch->{type} == 22) { # map 418 if ($arch->{type} == 22) { # map
80 my %normalize = ( 419 my %normalize = (
81 "enter_x" => "hp", 420 "enter_x" => "hp",
82 "enter_y" => "sp", 421 "enter_y" => "sp",
92 while (my ($k2, $k1) = each %normalize) { 431 while (my ($k2, $k1) = each %normalize) {
93 if (defined (my $v = delete $ob->{$k1})) { 432 if (defined (my $v = delete $ob->{$k1})) {
94 $ob->{$k2} = $v; 433 $ob->{$k2} = $v;
95 } 434 }
96 } 435 }
97 } 436 } else {
98
99 if (defined (my $v = delete $ob->{no_pass})) {
100 $ob->{move_block} = $v ? MOVE_ALL : 0;
101 }
102 if (defined (my $v = delete $ob->{walk_on})) {
103 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
104 : $ob->{move_on} & ~MOVE_WALK;
105 }
106 if (defined (my $v = delete $ob->{walk_off})) {
107 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
108 : $ob->{move_off} & ~MOVE_WALK;
109 }
110 if (defined (my $v = delete $ob->{fly_on})) {
111 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
112 : $ob->{move_on} & ~MOVE_FLY_LOW;
113 }
114 if (defined (my $v = delete $ob->{fly_off})) {
115 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
116 : $ob->{move_off} & ~MOVE_FLY_LOW;
117 }
118 if (defined (my $v = delete $ob->{flying})) {
119 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
120 : $ob->{move_type} & ~MOVE_FLY_LOW;
121 }
122
123 # if value matches archetype default, delete 437 # if value matches archetype default, delete
124 while (my ($k, $v) = each %$ob) { 438 while (my ($k, $v) = each %$ob) {
125 if (exists $arch->{$k} and $arch->{$k} eq $v) { 439 if (exists $arch->{$k} and $arch->{$k} eq $v) {
126 next if $k eq "_name"; 440 next if $k eq "_name";
127 delete $ob->{$k}; 441 delete $ob->{$k};
128 } 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;
129 } 451 }
130 452
131 $ob 453 $ob
132} 454}
133 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
134sub read_pak($;$) { 474sub read_pak($) {
135 my ($path, $cache) = @_; 475 my ($path) = @_;
136 476
137 eval {
138 defined $cache
139 && -M $cache < -M $path
140 && load_ref $cache
141 } or do {
142 my %pak; 477 my %pak;
143 478
144 open my $fh, "<", $path 479 open my $fh, "<:raw:perlio", $path
145 or Carp::croak "$_[0]: $!"; 480 or Carp::croak "$_[0]: $!";
146 binmode $fh; 481 binmode $fh;
147 while (<$fh>) { 482 while (<$fh>) {
148 my ($type, $id, $len, $path) = split; 483 my ($type, $id, $len, $path) = split;
149 $path =~ s/.*\///; 484 $path =~ s/.*\///;
150 read $fh, $pak{$path}, $len; 485 read $fh, $pak{$path}, $len;
151 } 486 }
152 487
153 save_ref \%pak, $cache
154 if defined $cache;
155
156 \%pak 488 \%pak
157 }
158} 489}
159 490
160sub read_arch($;$) { 491sub read_arch($;$) {
161 my ($path, $cache) = @_; 492 my ($path, $toplevel) = @_;
162 493
163 eval {
164 defined $cache
165 && -M $cache < -M $path
166 && load_ref $cache
167 } or do {
168 my %arc; 494 my %arc;
169 my ($more, $prev); 495 my ($more, $prev);
496 my $comment;
170 497
171 open my $fh, "<", $path 498 open my $fh, "<:raw:perlio:utf8", $path
172 or Carp::croak "$path: $!"; 499 or Carp::croak "$path: $!";
173 500
174 binmode $fh; 501# binmode $fh;
175 502
176 my $parse_block; $parse_block = sub { 503 my $parse_block; $parse_block = sub {
177 my %arc = @_; 504 my %arc = @_;
178
179 while (<$fh>) {
180 s/\s+$//;
181 if (/^end$/i) {
182 last;
183 } elsif (/^arch (\S+)$/) {
184 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
185 } elsif (/^lore$/) {
186 while (<$fh>) {
187 last if /^endlore\s*$/i;
188 $arc{lore} .= $_;
189 }
190 } elsif (/^msg$/) {
191 while (<$fh>) {
192 last if /^endmsg\s*$/i;
193 $arc{msg} .= $_;
194 }
195 } elsif (/^(\S+)\s*(.*)$/) {
196 $arc{lc $1} = $2;
197 } elsif (/^\s*($|#)/) {
198 #
199 } else {
200 warn "$path: unparsable line '$_' in arch $arc{_name}";
201 }
202 }
203
204 \%arc
205 };
206 505
207 while (<$fh>) { 506 while (<$fh>) {
208 s/\s+$//; 507 s/\s+$//;
209 if (/^more$/i) { 508 if (/^end$/i) {
210 $more = $prev; 509 last;
510
211 } elsif (/^object (\S+)$/i) { 511 } elsif (/^arch (\S+)$/i) {
212 my $name = $1; 512 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
213 my $arc = $parse_block->(_name => $name);
214 513
215 if ($more) { 514 } elsif (/^lore$/i) {
216 $more->{more} = $arc; 515 while (<$fh>) {
217 } else { 516 last if /^endlore\s*$/i;
218 $arc{$name} = $arc; 517 $arc{lore} .= $_;
219 } 518 }
220 $prev = $arc; 519 } elsif (/^msg$/i) {
221 $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 }
222 } elsif (/^arch (\S+)$/i) { 530 } elsif (/^(\S+)\s*(.*)$/) {
223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); 531 $arc{lc $1} = $2;
224 } elsif (/^\s*($|#)/) { 532 } elsif (/^\s*#/) {
533 $arc{_comment} .= "$_\n";
534
535 } elsif (/^\s*$/) {
225 # 536 #
226 } else { 537 } else {
227 warn "$path: unparseable top-level line '$_'"; 538 warn "$path: unparsable line '$_' in arch $arc{_name}";
228 }
229 } 539 }
230 540 }
231 undef $parse_block; # work around bug in perl not freeing $fh etc.
232
233 save_ref \%arc, $cache
234 if defined $cache;
235 541
236 \%arc 542 \%arc
237 } 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
238} 680}
239 681
240# 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
241# NOTE: the arrays in the hash values are references to 683# NOTE: the arrays in the hash values are references to
242# the archs from $ARCH 684# the archs from $ARCH
243sub editor_archs { 685sub editor_archs {
244 my %paths; 686 my %paths;
245 687
246 for (keys %ARCH) { 688 for (keys %ARCH) {
247 my $arch = $ARCH{$_}; 689 my $arch = $ARCH{$_};
248 push @{$paths{$arch->{editor_folder}}}, \$arch; 690 push @{$paths{$arch->{editor_folder}}}, $arch;
249 } 691 }
250 692
251 \%paths 693 \%paths
252} 694}
253 695
263 my ($a) = @_; 705 my ($a) = @_;
264 706
265 my $o = $ARCH{$a->{_name}} 707 my $o = $ARCH{$a->{_name}}
266 or return; 708 or return;
267 709
268 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"}
269 or (warn "no face data found for arch '$a->{_name}'"), return; 713 or (warn "no face data found for arch '$a->{_name}'"), return;
714 }
270 715
271 if ($face->{w} > 1 || $face->{h} > 1) { 716 if ($face->{w} > 1 || $face->{h} > 1) {
272 # bigface 717 # bigface
273 return (0, 0, $face->{w} - 1, $face->{h} - 1); 718 return (0, 0, $face->{w} - 1, $face->{h} - 1);
274 719
305 import 750 import
306 751
307=cut 752=cut
308 753
309sub arch_attr($) { 754sub arch_attr($) {
310 my ($arch) = @_; 755 my ($obj) = @_;
311 756
312 require Crossfire::Data; 757 require Crossfire::Data;
313 758
314 my $root; 759 my $root;
760 my $attr = { };
761
762 my $arch = $ARCH{ $obj->{_name} };
763 my $type = $obj->{type} || $arch->{type};
315 764
316 if ($arch->{type} > 0) { 765 if ($type > 0) {
317 $root = $Crossfire::Data::ATTR{$arch->{type}+0}; 766 $root = $Crossfire::Data::ATTR{$type};
318 } else { 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 {
319 $root = $Crossfire::Data::TYPE{Misc}; 779 $root = $Crossfire::Data::TYPE{Misc};
320
321 type:
322 for (@Crossfire::Data::ATTR0) {
323 my $req = $_->{required}
324 or die "internal error: ATTR0 without 'required'";
325
326 keys %$req;
327 while (my ($k, $v) = each %$req) {
328 next type
329 unless $arch->{$k} == $v;
330 } 780 }
781 }
331 782
332 $root = $_; 783 my @import = ($root);
333 }
334 } 784
335
336 my $attr = { };
337
338 my @import = $root || \%Crossfire::Data::DEFAULT_ATTR; 785 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
786 unless $type == 116;
787
339 my (%ignore); 788 my (%ignore);
340 my (@section_order, %section, @attr_order); 789 my (@section_order, %section, @attr_order);
341 790
342 while (my $type = shift @import) { 791 while (my $type = shift @import) {
343 push @import, @{$type->{import} || []}; 792 push @import, @{$type->{import} || []};
347 796
348 for (@{$type->{ignore} || []}) { 797 for (@{$type->{ignore} || []}) {
349 $ignore{$_}++ for ref $_ ? @$_ : $_; 798 $ignore{$_}++ for ref $_ ? @$_ : $_;
350 } 799 }
351 800
352 for ([general => ($type->{attr} || {})], @{$type->{section} || []}) { 801 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
353 my ($name, $attr) = @$_; 802 my ($name, $attr) = @$_;
354 push @section_order, $name; 803 push @section_order, $name;
355 for (@$attr) { 804 for (@$attr) {
356 my ($k, $v) = @$_; 805 my ($k, $v) = @$_;
357 push @attr_order, $k; 806 push @attr_order, $k;
364 map !exists $section{$_} ? () : do { 813 map !exists $section{$_} ? () : do {
365 my $attr = delete $section{$_}; 814 my $attr = delete $section{$_};
366 815
367 [ 816 [
368 $_, 817 $_,
818 map exists $attr->{$_} && !$ignore{$_}
369 map exists $attr->{$_} ? [$_ => delete $attr->{$_}] : (), 819 ? [$_ => delete $attr->{$_}] : (),
370 @attr_order 820 @attr_order
371 ] 821 ]
372 }, 822 },
373 823
374 exists $section{$_} ? [$_ => delete $section{$_}] : (), 824 exists $section{$_} ? [$_ => delete $section{$_}] : (),
375 @section_order 825 @section_order
376 ]; 826 ];
377 827
378 use PApp::Util;
379 warn PApp::Util::dumpval $attr;
380
381 $attr 828 $attr
382} 829}
383 830
384sub arch_edit_sections { 831sub cache_file($$&&) {
385# if (edit_type == IGUIConstants.TILE_EDIT_NONE) 832 my ($src, $cache, $load, $create) = @_;
386# edit_type = 0; 833
387# else if (edit_type != 0) { 834 my ($size, $mtime) = (stat $src)[7,9]
388# // all flags from 'check_type' must be unset in this arch because they get recalculated now 835 or Carp::croak "$src: $!";
389# edit_type &= ~check_type; 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;
390# } 845 }
391# 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
888sub construct_tilecache_pb {
889 my ($idx, $cache) = @_;
890
891 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
892
893 while (my ($name, $tile) = each %$cache) {
894 my $tpb = delete $tile->{pb};
895 my $ofs = $tile->{idx};
896
897 for my $x (0 .. $tile->{w} - 1) {
898 for my $y (0 .. $tile->{h} - 1) {
899 my $idx = $ofs + $x + $y * $tile->{w};
900 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
901 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
902 }
392# } 903 }
393# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 && 904 }
394# getAttributeValue("alive", defarch) == 1 &&
395# (getAttributeValue("monster", defarch) == 1 ||
396# getAttributeValue("generator", defarch) == 1)) {
397# // Monster: monsters/npcs/generators
398# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
399# }
400# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
401# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
402# // Walls
403# edit_type |= IGUIConstants.TILE_EDIT_WALL;
404# }
405# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
406# getAttributeValue("connected", defarch) != 0) {
407# // Connected Objects
408# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
409# }
410# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
411# arch_type == 66 || arch_type == 41 || arch_type == 95) {
412# // Exit: teleporter/exit/trapdoors
413# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
414# }
415# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
416# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
417# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
418# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
419# arch_type == 124 || arch_type == 130)) {
420# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
421# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
422# }
423# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
424# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
425# arch_type == 91 || arch_type == 21 || arch_type == 24) {
426# // Door: door/special door/gates + keys
427# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
428# }
429# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
430# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
431# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
432# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
433# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
434# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
435# arch_type == 122 || arch_type == 3)) {
436# // Equipment: weapons/armour/wands/rods
437# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
438# }
439#
440# return(edit_type);
441#
442#
443}
444 905
445sub init($) { 906 $pb->save ("$VARDIR/tilecache.png", "png", compression => 1);
907
908 $cache
909}
910
911sub use_tilecache {
446 my ($cachedir) = @_; 912 my ($face) = @_;
447 913 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
448 return if %ARCH; 914 or die "$VARDIR/tilecache.png: $!";
449 915 *FACE = $_[0];
450 mkdir $cachedir, 0777;
451 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
452} 916}
453 917
454$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; 918=item load_tilecache
455 919
456init $VARDIR; 920(Re-)Load %TILE and %FACE.
921
922=cut
923
924sub load_tilecache() {
925 require Gtk2;
926
927 if (-e "$LIB/crossfire.0") { # Crossfire1 version
928 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", \&use_tilecache,
929 sub {
930 my $tile = read_pak "$LIB/crossfire.0";
931
932 my %cache;
933
934 my $idx = 0;
935
936 for my $name (sort keys %$tile) {
937 my $pb = new Gtk2::Gdk::PixbufLoader;
938 $pb->write ($tile->{$name});
939 $pb->close;
940 my $pb = $pb->get_pixbuf;
941
942 my $tile = $cache{$name} = {
943 pb => $pb,
944 idx => $idx,
945 w => int $pb->get_width / TILESIZE,
946 h => int $pb->get_height / TILESIZE,
947 };
948
949 $idx += $tile->{w} * $tile->{h};
950 }
951
952 construct_tilecache_pb $idx, \%cache;
953
954 \%cache
955 };
956
957 } else { # Crossfire+ version
958 cache_file "$LIB/facedata", "$VARDIR/tilecache.pst", \&use_tilecache,
959 sub {
960 my %cache;
961 my $facedata = Storable::retrieve "$LIB/facedata";
962
963 $facedata->{version} == 2
964 or die "$LIB/facedata: version mismatch, cannot proceed.";
965
966 my $faces = $facedata->{faceinfo};
967 my $idx = 0;
968
969 for (sort keys %$faces) {
970 my ($face, $info) = ($_, $faces->{$_});
971
972 my $pb = new Gtk2::Gdk::PixbufLoader;
973 $pb->write ($info->{data32});
974 $pb->close;
975 my $pb = $pb->get_pixbuf;
976
977 my $tile = $cache{$face} = {
978 pb => $pb,
979 idx => $idx,
980 w => int $pb->get_width / TILESIZE,
981 h => int $pb->get_height / TILESIZE,
982 };
983
984 $idx += $tile->{w} * $tile->{h};
985 }
986
987 construct_tilecache_pb $idx, \%cache;
988
989 \%cache
990 };
991 }
992}
457 993
458=head1 AUTHOR 994=head1 AUTHOR
459 995
460 Marc Lehmann <schmorp@schmorp.de> 996 Marc Lehmann <schmorp@schmorp.de>
461 http://home.schmorp.de/ 997 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines