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.14 by root, Sat Feb 11 16:17:14 2006 UTC vs.
Revision 1.102 by elmex, Sun Apr 15 11:43: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);
18use Storable qw(freeze thaw);
17 19
18#XXX: The map_* procedures scream for a map-object
19
20our @EXPORT = 20our @EXPORT = qw(
21 qw(read_pak read_arch $ARCH TILESIZE editor_archs 21 read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents
22 arch_extends 22);
23 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
24 );
25 23
24use JSON::XS qw(from_json to_json);
25
26our $LIB = $ENV{CROSSFIRE_LIBDIR} 26our $LIB = $ENV{CROSSFIRE_LIBDIR};
27 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;
28 33
29sub TILESIZE (){ 32 } 34sub TILESIZE (){ 32 }
30 35
31our $ARCH; 36our %ARCH;
37our %FACE;
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 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
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
47 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
48sub normalize_arch($) { 410sub normalize_arch($) {
49 my ($ob) = @_; 411 my ($ob) = @_;
50 412
413 normalize_object $ob;
414
51 my $arch = $ARCH->{$ob->{_name}} 415 my $arch = $ARCH{$ob->{_name}}
52 or (warn "$ob->{_name}: no such archetype", return $ob); 416 or (warn "$ob->{_name}: no such archetype", return $ob);
53
54 delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply);
55 417
56 if ($arch->{type} == 22) { # map 418 if ($arch->{type} == 22) { # map
57 my %normalize = ( 419 my %normalize = (
58 "enter_x" => "hp", 420 "enter_x" => "hp",
59 "enter_y" => "sp", 421 "enter_y" => "sp",
69 while (my ($k2, $k1) = each %normalize) { 431 while (my ($k2, $k1) = each %normalize) {
70 if (defined (my $v = delete $ob->{$k1})) { 432 if (defined (my $v = delete $ob->{$k1})) {
71 $ob->{$k2} = $v; 433 $ob->{$k2} = $v;
72 } 434 }
73 } 435 }
74 } 436 } else {
75
76 if (defined (my $v = delete $ob->{no_pass})) {
77 $ob->{move_block} = $v ? MOVE_ALL : 0;
78 }
79 if (defined (my $v = delete $ob->{walk_on})) {
80 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
81 : $ob->{move_on} & ~MOVE_WALK;
82 }
83 if (defined (my $v = delete $ob->{walk_off})) {
84 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
85 : $ob->{move_off} & ~MOVE_WALK;
86 }
87 if (defined (my $v = delete $ob->{fly_on})) {
88 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
89 : $ob->{move_on} & ~MOVE_FLY_LOW;
90 }
91 if (defined (my $v = delete $ob->{fly_off})) {
92 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
93 : $ob->{move_off} & ~MOVE_FLY_LOW;
94 }
95 if (defined (my $v = delete $ob->{flying})) {
96 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
97 : $ob->{move_type} & ~MOVE_FLY_LOW;
98 }
99
100 # if value matches archetype default, delete 437 # if value matches archetype default, delete
101 while (my ($k, $v) = each %$ob) { 438 while (my ($k, $v) = each %$ob) {
102 if (exists $arch->{$k} and $arch->{$k} eq $v) { 439 if (exists $arch->{$k} and $arch->{$k} eq $v) {
440 next if $k eq "_name";
103 delete $ob->{$k}; 441 delete $ob->{$k};
104 } 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;
105 } 451 }
106 452
107 $ob 453 $ob
108} 454}
109 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
110sub read_pak($;$) { 474sub read_pak($) {
111 my ($path, $cache) = @_; 475 my ($path) = @_;
112 476
113 eval {
114 defined $cache
115 && -M $cache < -M $path
116 && Storable::retrieve $cache
117 } or do {
118 my %pak; 477 my %pak;
119 478
120 open my $fh, "<:raw", $path 479 open my $fh, "<:raw:perlio", $path
121 or Carp::croak "$_[0]: $!"; 480 or Carp::croak "$_[0]: $!";
481 binmode $fh;
122 while (<$fh>) { 482 while (<$fh>) {
123 my ($type, $id, $len, $path) = split; 483 my ($type, $id, $len, $path) = split;
124 $path =~ s/.*\///; 484 $path =~ s/.*\///;
125 read $fh, $pak{$path}, $len; 485 read $fh, $pak{$path}, $len;
126 } 486 }
127 487
128 Storable::nstore \%pak, $cache
129 if defined $cache;
130
131 \%pak 488 \%pak
132 }
133} 489}
134 490
135sub read_arch($;$) { 491sub read_arch($;$) {
136 my ($path, $cache) = @_; 492 my ($path, $toplevel) = @_;
137 493
138 eval {
139 defined $cache
140 && -M $cache < -M $path
141 && Storable::retrieve $cache
142 } or do {
143 my %arc; 494 my %arc;
144 my ($more, $prev); 495 my ($more, $prev);
496 my $comment;
145 497
146 open my $fh, "<:raw", $path 498 open my $fh, "<:raw:perlio:utf8", $path
147 or Carp::croak "$path: $!"; 499 or Carp::croak "$path: $!";
148 500
501# binmode $fh;
502
149 my $parse_block; $parse_block = sub { 503 my $parse_block; $parse_block = sub {
150 my %arc = @_; 504 my %arc = @_;
151
152 while (<$fh>) {
153 s/\s+$//;
154 if (/^end$/i) {
155 last;
156 } elsif (/^arch (\S+)$/) {
157 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
158 } elsif (/^lore$/) {
159 while (<$fh>) {
160 last if /^endlore\s*$/i;
161 $arc{lore} .= $_;
162 }
163 } elsif (/^msg$/) {
164 while (<$fh>) {
165 last if /^endmsg\s*$/i;
166 $arc{msg} .= $_;
167 }
168 } elsif (/^(\S+)\s*(.*)$/) {
169 $arc{lc $1} = $2;
170 } elsif (/^\s*($|#)/) {
171 #
172 } else {
173 warn "$path: unparsable line '$_' in arch $arc{_name}";
174 }
175 }
176
177 \%arc
178 };
179 505
180 while (<$fh>) { 506 while (<$fh>) {
181 s/\s+$//; 507 s/\s+$//;
182 if (/^more$/i) { 508 if (/^end$/i) {
183 $more = $prev; 509 last;
510
184 } elsif (/^object (\S+)$/i) { 511 } elsif (/^arch (\S+)$/i) {
185 my $name = $1; 512 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
186 my $arc = $parse_block->(_name => $name);
187 513
188 if ($more) { 514 } elsif (/^lore$/i) {
189 $more->{more} = $arc; 515 while (<$fh>) {
190 } else { 516 last if /^endlore\s*$/i;
191 $arc{$name} = $arc; 517 $arc{lore} .= $_;
192 } 518 }
193 $prev = $arc; 519 } elsif (/^msg$/i) {
194 $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 }
195 } elsif (/^arch (\S+)$/i) { 530 } elsif (/^(\S+)\s*(.*)$/) {
196 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); 531 $arc{lc $1} = $2;
197 } elsif (/^\s*($|#)/) { 532 } elsif (/^\s*#/) {
533 $arc{_comment} .= "$_\n";
534
535 } elsif (/^\s*$/) {
198 # 536 #
199 } else { 537 } else {
200 warn "$path: unparseable top-level line '$_'"; 538 warn "$path: unparsable line '$_' in arch $arc{_name}";
201 }
202 } 539 }
203 540 }
204 undef $parse_block; # work around bug in perl not freeing $fh etc.
205
206 Storable::nstore \%arc, $cache
207 if defined $cache;
208 541
209 \%arc 542 \%arc
210 } 543 };
211}
212 544
213# returns the arch/object stack from a tile on a map 545 while (<$fh>) {
214sub map_get_tile_stack { 546 s/\s+$//;
215 my ($map, $x, $y) = @_; 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
216 my $as; 603 my $str;
217 604
218 if ($x > 0 || $x < $map->{width} 605 my $append; $append = sub {
219 || $y > 0 || $y < $map->{height}) { 606 my %a = %{$_[0]};
220 607
221 $as = $map->{map}{map}[$x][$y] || []; 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 }
222 } 673 };
223 674
224 return $as; 675 for (@$arch) {
225} 676 $append->($_);
226
227# pop the topmost arch/object from the stack of a tile on a map
228sub map_pop_tile_stack {
229 my ($map, $x, $y) = @_;
230
231 if ($x > 0 || $x < $map->{width}
232 || $y > 0 || $y < $map->{height}) {
233
234 pop @{$map->{map}{map}[$x][$y]};
235 } 677 }
236}
237 678
238# pushes the arch/object on the stack of a tile on a map 679 $str
239sub map_push_tile_stack {
240 my ($map, $x, $y, $arch) = @_;
241
242 if ($x > 0 || $x < $map->{width}
243 || $y > 0 || $y < $map->{height}) {
244
245 push @{$map->{map}{map}[$x][$y]}, $arch;
246 }
247} 680}
248
249 681
250# 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
251# NOTE: the arrays in the hash values are references to 683# NOTE: the arrays in the hash values are references to
252# the archs from $ARCH 684# the archs from $ARCH
253sub editor_archs { 685sub editor_archs {
254 my %paths; 686 my %paths;
255 687
256 for (keys %$ARCH) { 688 for (keys %ARCH) {
257 my $arch = $ARCH->{$_}; 689 my $arch = $ARCH{$_};
258 push @{$paths{$arch->{editor_folder}}}, \$arch; 690 push @{$paths{$arch->{editor_folder}}}, $arch;
259 } 691 }
260 692
261 return \%paths; 693 \%paths
262} 694}
263 695
264# arch_extends determines how the arch looks like on the map, 696=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
265# bigfaces, linked faces and single faces are handled here 697
266# it returns (<xoffset>, <yoffset>, <width>, <height>) 698arch_extents determines the extents of the given arch's face(s), linked
267# NOTE: non rectangular linked faces are not considered 699faces and single faces are handled here it returns (minx, miny, maxx,
700maxy)
701
702=cut
703
268sub arch_extends { 704sub arch_extents {
269 my ($a) = @_; 705 my ($a) = @_;
270 706
271 my $TC = \%Crossfire::Tilecache::TILECACHE; 707 my $o = $ARCH{$a->{_name}}
272
273 my $facename =
274 $a->{face} || $ARCH->{$a->{_name}}->{face}
275 or return (); 708 or return;
276 709
277 my $tile = $TC->{$facename} 710 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
278 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 711 unless ($face) {
712 $face = $FACE{"blank.x11"}
713 or (warn "no face data found for arch '$a->{_name}'"), return;
714 }
279 715
280 if ($tile->{w} > 1 || $tile->{h} > 1) { 716 if ($face->{w} > 1 || $face->{h} > 1) {
281 # bigfaces 717 # bigface
282 return (0, 0, $tile->{w}, $tile->{h}); 718 return (0, 0, $face->{w} - 1, $face->{h} - 1);
283 719
284 } elsif ($a->{more}) { 720 } elsif ($o->{more}) {
285 # linked faces 721 # linked face
286 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 722 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
287 do {
288 $miw > (0 + $a->{x}) and $miw = $a->{x};
289 $mih > (0 + $a->{y}) and $mih = $a->{y};
290 $maw < (0 + $a->{x}) and $maw = $a->{x};
291 $mah < (0 + $a->{y}) and $mah = $a->{y};
292 } while $a = $a->{more};
293 723
294 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) 724 for (; $o; $o = $o->{more}) {
725 $minx = min $minx, $o->{x};
726 $miny = min $miny, $o->{y};
727 $maxx = max $maxx, $o->{x};
728 $maxy = max $maxy, $o->{y};
729 }
730
731 return ($minx, $miny, $maxx, $maxy);
295 732
296 } else { 733 } else {
297 # single face 734 # single face
298 return (0, 0, 1, 1); 735 return (0, 0, 0, 0);
736 }
737}
738
739=item $type = arch_attr $arch
740
741Returns a hashref describing the object and its attributes. It can contain
742the following keys:
743
744 name the name, suitable for display purposes
745 ignore
746 attr
747 desc
748 use
749 section => [name => \%attr, name => \%attr]
750 import
751
752=cut
753
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->(),
299 } 853 };
300}
301 854
302sub init($) { 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 }
903 }
904 }
905
906 $pb->save ("$VARDIR/tilecache.png", "png", compression => 1);
907
908 $cache
909}
910
911sub use_tilecache {
303 my ($cachedir) = @_; 912 my ($face) = @_;
913 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
914 or die "$VARDIR/tilecache.png: $!";
915 *FACE = $_[0];
916}
304 917
305 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 918=item load_tilecache
919
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 }
306} 992}
307 993
308=head1 AUTHOR 994=head1 AUTHOR
309 995
310 Marc Lehmann <schmorp@schmorp.de> 996 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines