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.35 by root, Sun Mar 12 16:23:56 2006 UTC vs.
Revision 1.51 by root, Mon Mar 20 01:15:10 2006 UTC

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;
19 19
20#XXX: The map_* procedures scream for a map-object
21
22our @EXPORT = 20our @EXPORT = qw(
23 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);
24 23
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR};
26 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; 25
26our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
27
28mkdir $VARDIR, 0777;
27 29
28sub TILESIZE (){ 32 } 30sub TILESIZE (){ 32 }
29 31
30our $VARDIR;
31our %ARCH; 32our %ARCH;
32our %FACE; 33our %FACE;
33our $TILE; 34our $TILE;
34 35
35our %FIELD_MULTILINE = ( 36our %FIELD_MULTILINE = (
36 msg => "endmsg", 37 msg => "endmsg",
37 lore => "endlore", 38 lore => "endlore",
39 maplore => "endmaplore",
38); 40);
39 41
40# not used yet, maybe alphabetical is ok 42# not used yet, maybe alphabetical is ok
41our @FIELD_ORDER = (qw(name name_pl)); 43our @FIELD_ORDER = (qw(name name_pl));
42 44
43sub MOVE_WALK (){ 0x1 } 45sub MOVE_WALK (){ 0x01 }
44sub MOVE_FLY_LOW (){ 0x2 } 46sub MOVE_FLY_LOW (){ 0x02 }
45sub MOVE_FLY_HIGH (){ 0x4 } 47sub MOVE_FLY_HIGH (){ 0x04 }
46sub MOVE_FLYING (){ 0x6 } 48sub MOVE_FLYING (){ 0x06 }
47sub MOVE_SWIM (){ 0x8 } 49sub MOVE_SWIM (){ 0x08 }
50sub MOVE_BOAT (){ 0x10 }
48sub MOVE_ALL (){ 0xf } 51sub MOVE_ALL (){ 0xff }
49 52
50sub load_ref($) { 53sub load_ref($) {
51 my ($path) = @_; 54 my ($path) = @_;
52 55
53 open my $fh, "<", $path 56 open my $fh, "<", $path
74 my ($ob) = @_; 77 my ($ob) = @_;
75 78
76 my $arch = $ARCH{$ob->{_name}} 79 my $arch = $ARCH{$ob->{_name}}
77 or (warn "$ob->{_name}: no such archetype", return $ob); 80 or (warn "$ob->{_name}: no such archetype", return $ob);
78 81
82 delete $ob->{$_} for qw(
79 delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply); 83 can_knockback can_parry can_impale can_cut can_dam_armour
84 can_apply pass_thru can_pass_thru
85 );
80 86
81 if ($arch->{type} == 22) { # map 87 if ($arch->{type} == 22) { # map
82 my %normalize = ( 88 my %normalize = (
83 "enter_x" => "hp", 89 "enter_x" => "hp",
84 "enter_y" => "sp", 90 "enter_y" => "sp",
96 $ob->{$k2} = $v; 102 $ob->{$k2} = $v;
97 } 103 }
98 } 104 }
99 } 105 }
100 106
107 for my $attr (qw(move_type move_block move_allow move_on move_off move_slow)) {
108 next unless exists $ob->{$attr};
109 next if $ob->{$attr} =~ /^\d+$/;
110
111 my $flags = 0;
112
113 # assume list
114 for my $flag (map lc, split /\s+/, $ob->{$attr}) {
115 $flags |= MOVE_WALK if $flag eq "walk";
116 $flags |= MOVE_FLY_LOW if $flag eq "fly_low";
117 $flags |= MOVE_FLY_HIGH if $flag eq "fly_high";
118 $flags |= MOVE_FLYING if $flag eq "flying";
119 $flags |= MOVE_SWIM if $flag eq "swim";
120 $flags |= MOVE_BOAT if $flag eq "boat";
121 $flags |= MOVE_ALL if $flag eq "all";
122
123 $flags &= ~MOVE_WALK if $flag eq "-walk";
124 $flags &= ~MOVE_FLY_LOW if $flag eq "-fly_low";
125 $flags &= ~MOVE_FLY_HIGH if $flag eq "-fly_high";
126 $flags &= ~MOVE_FLYING if $flag eq "-flying";
127 $flags &= ~MOVE_SWIM if $flag eq "-swim";
128 $flags &= ~MOVE_BOAT if $flag eq "-boat";
129 $flags &= ~MOVE_ALL if $flag eq "-all";
130 }
131
132 $ob->{$attr} = $flags;
133 }
134
101 if (defined (my $v = delete $ob->{no_pass})) { 135 if (defined (my $v = delete $ob->{no_pass})) {
102 $ob->{move_block} = $v ? MOVE_ALL : 0; 136 $ob->{move_block} = $v ? MOVE_ALL : 0;
137 }
138 if (defined (my $v = delete $ob->{slow_move})) {
139 $ob->{move_slow} |= MOVE_WALK;
140 $ob->{move_slow_penalty} = $v;
103 } 141 }
104 if (defined (my $v = delete $ob->{walk_on})) { 142 if (defined (my $v = delete $ob->{walk_on})) {
105 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK 143 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
106 : $ob->{move_on} & ~MOVE_WALK; 144 : $ob->{move_on} & ~MOVE_WALK;
107 } 145 }
131 } 169 }
132 170
133 $ob 171 $ob
134} 172}
135 173
136sub read_pak($;$) { 174sub read_pak($) {
137 my ($path, $cache) = @_; 175 my ($path) = @_;
138 176
139 eval {
140 defined $cache
141 && -M $cache < -M $path
142 && load_ref $cache
143 } or do {
144 my %pak; 177 my %pak;
145 178
146 open my $fh, "<", $path 179 open my $fh, "<", $path
147 or Carp::croak "$_[0]: $!"; 180 or Carp::croak "$_[0]: $!";
148 binmode $fh; 181 binmode $fh;
149 while (<$fh>) { 182 while (<$fh>) {
150 my ($type, $id, $len, $path) = split; 183 my ($type, $id, $len, $path) = split;
151 $path =~ s/.*\///; 184 $path =~ s/.*\///;
152 read $fh, $pak{$path}, $len; 185 read $fh, $pak{$path}, $len;
153 } 186 }
154 187
155 save_ref \%pak, $cache
156 if defined $cache;
157
158 \%pak 188 \%pak
159 }
160} 189}
161 190
162sub read_arch($;$) { 191sub read_arch($) {
163 my ($path, $cache) = @_; 192 my ($path) = @_;
164 193
165 eval {
166 defined $cache
167 && -M $cache < -M $path
168 && load_ref $cache
169 } or do {
170 my %arc; 194 my %arc;
171 my ($more, $prev); 195 my ($more, $prev);
172 196
173 open my $fh, "<", $path 197 open my $fh, "<", $path
174 or Carp::croak "$path: $!"; 198 or Carp::croak "$path: $!";
175 199
176 binmode $fh; 200 binmode $fh;
177 201
178 my $parse_block; $parse_block = sub { 202 my $parse_block; $parse_block = sub {
179 my %arc = @_; 203 my %arc = @_;
180
181 while (<$fh>) {
182 s/\s+$//;
183 if (/^end$/i) {
184 last;
185 } elsif (/^arch (\S+)$/) {
186 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
187 } elsif (/^lore$/) {
188 while (<$fh>) {
189 last if /^endlore\s*$/i;
190 $arc{lore} .= $_;
191 }
192 } elsif (/^msg$/) {
193 while (<$fh>) {
194 last if /^endmsg\s*$/i;
195 $arc{msg} .= $_;
196 }
197 } elsif (/^(\S+)\s*(.*)$/) {
198 $arc{lc $1} = $2;
199 } elsif (/^\s*($|#)/) {
200 #
201 } else {
202 warn "$path: unparsable line '$_' in arch $arc{_name}";
203 }
204 }
205
206 \%arc
207 };
208 204
209 while (<$fh>) { 205 while (<$fh>) {
210 s/\s+$//; 206 s/\s+$//;
211 if (/^more$/i) { 207 if (/^end$/i) {
212 $more = $prev; 208 last;
213 } elsif (/^object (\S+)$/i) { 209 } elsif (/^arch (\S+)$/) {
214 my $name = $1; 210 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
215 my $arc = $parse_block->(_name => $name); 211 } elsif (/^lore$/) {
216 212 while (<$fh>) {
217 if ($more) { 213 last if /^endlore\s*$/i;
218 $more->{more} = $arc;
219 } else {
220 $arc{$name} = $arc; 214 $arc{lore} .= $_;
221 } 215 }
222 $prev = $arc; 216 } elsif (/^msg$/) {
223 $more = undef; 217 while (<$fh>) {
218 last if /^endmsg\s*$/i;
219 $arc{msg} .= $_;
220 }
224 } elsif (/^arch (\S+)$/i) { 221 } elsif (/^(\S+)\s*(.*)$/) {
225 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); 222 $arc{lc $1} = $2;
226 } elsif (/^\s*($|#)/) { 223 } elsif (/^\s*($|#)/) {
227 # 224 #
228 } else { 225 } else {
229 warn "$path: unparseable top-level line '$_'"; 226 warn "$path: unparsable line '$_' in arch $arc{_name}";
230 } 227 }
231 } 228 }
232 229
233 undef $parse_block; # work around bug in perl not freeing $fh etc.
234
235 save_ref \%arc, $cache
236 if defined $cache;
237
238 \%arc 230 \%arc
239 } 231 };
232
233 while (<$fh>) {
234 s/\s+$//;
235 if (/^more$/i) {
236 $more = $prev;
237 } elsif (/^object (\S+)$/i) {
238 my $name = $1;
239 my $arc = $parse_block->(_name => $name);
240
241 if ($more) {
242 $more->{more} = $arc;
243 } else {
244 $arc{$name} = $arc;
245 }
246 $prev = $arc;
247 $more = undef;
248 } elsif (/^arch (\S+)$/i) {
249 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
250 } elsif (/^\s*($|#)/) {
251 #
252 } else {
253 warn "$path: unparseable top-level line '$_'";
254 }
255 }
256
257 undef $parse_block; # work around bug in perl not freeing $fh etc.
258
259 \%arc
240} 260}
241 261
242# put all archs into a hash with editor_face as it's key 262# put all archs into a hash with editor_face as it's key
243# NOTE: the arrays in the hash values are references to 263# NOTE: the arrays in the hash values are references to
244# the archs from $ARCH 264# the archs from $ARCH
265 my ($a) = @_; 285 my ($a) = @_;
266 286
267 my $o = $ARCH{$a->{_name}} 287 my $o = $ARCH{$a->{_name}}
268 or return; 288 or return;
269 289
270 my $face = $FACE{$a->{face} || $o->{face}} 290 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
271 or (warn "no face data found for arch '$a->{_name}'"), return; 291 or (warn "no face data found for arch '$a->{_name}'"), return;
272 292
273 if ($face->{w} > 1 || $face->{h} > 1) { 293 if ($face->{w} > 1 || $face->{h} > 1) {
274 # bigface 294 # bigface
275 return (0, 0, $face->{w} - 1, $face->{h} - 1); 295 return (0, 0, $face->{w} - 1, $face->{h} - 1);
307 import 327 import
308 328
309=cut 329=cut
310 330
311sub arch_attr($) { 331sub arch_attr($) {
312 my ($arch) = @_; 332 my ($obj) = @_;
313 333
314 require Crossfire::Data; 334 require Crossfire::Data;
315 335
336 my $root;
316 my $attr; 337 my $attr = { };
338
339 my $arch = $ARCH{ $obj->{_name} };
340 my $type = $obj->{type} || $arch->{type};
317 341
318 if ($arch->{type} > 0) { 342 if ($type > 0) {
319 $attr = $Crossfire::Data::ATTR{$arch->{type}+0}; 343 $root = $Crossfire::Data::ATTR{$type};
320 } else { 344 } else {
321 $attr = $Crossfire::Data::TYPE{Misc}; 345 $root = $Crossfire::Data::TYPE{Misc};
322 346
323 type: 347 type:
324 for (@Crossfire::Data::ATTR0) { 348 for (@Crossfire::Data::ATTR0) {
325 my $req = $_->{required} 349 my $req = $_->{required}
326 or die "internal error: ATTR0 without 'required'"; 350 or die "internal error: ATTR0 without 'required'";
327 351
328 keys %$req; 352 keys %$req;
329 while (my ($k, $v) = each %$req) { 353 while (my ($k, $v) = each %$req) {
330 next type 354 next type
331 unless $arch->{$k} == $v; 355 unless $obj->{$k} == $v || $arch->{$k} == $v;
332 } 356 }
333 357
334 $attr = $_; 358 $root = $_;
335 } 359 }
360 }
361
362 my @import = ($root);
336 } 363
337
338 $attr || \%Crossfire::Data::DEFAULT_ATTR; 364 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
365 unless $type == 116;
366
367 my (%ignore);
368 my (@section_order, %section, @attr_order);
369
370 while (my $type = shift @import) {
371 push @import, @{$type->{import} || []};
372
373 $attr->{$_} ||= $type->{$_}
374 for qw(name desc use);
375
376 for (@{$type->{ignore} || []}) {
377 $ignore{$_}++ for ref $_ ? @$_ : $_;
378 }
379
380 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
381 my ($name, $attr) = @$_;
382 push @section_order, $name;
383 for (@$attr) {
384 my ($k, $v) = @$_;
385 push @attr_order, $k;
386 $section{$name}{$k} ||= $v;
387 }
388 }
389 }
390
391 $attr->{section} = [
392 map !exists $section{$_} ? () : do {
393 my $attr = delete $section{$_};
394
395 [
396 $_,
397 map exists $attr->{$_} && !$ignore{$_}
398 ? [$_ => delete $attr->{$_}] : (),
399 @attr_order
400 ]
401 },
402
403 exists $section{$_} ? [$_ => delete $section{$_}] : (),
404 @section_order
405 ];
406
407 $attr
339} 408}
340 409
341sub arch_edit_sections { 410sub arch_edit_sections {
342# if (edit_type == IGUIConstants.TILE_EDIT_NONE) 411# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
343# edit_type = 0; 412# edit_type = 0;
397# return(edit_type); 466# return(edit_type);
398# 467#
399# 468#
400} 469}
401 470
402sub init($) { 471sub cache_file($$&&) {
403 my ($cachedir) = @_; 472 my ($src, $cache, $load, $create) = @_;
404 473
405 return if %ARCH; 474 my ($size, $mtime) = (stat $src)[7,9]
475 or Carp::croak "$src: $!";
406 476
407 mkdir $cachedir, 0777; 477 if (-e $cache) {
408 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 478 my $ref = eval { load_ref $cache };
409}
410 479
411$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; 480 if ($ref->{version} == 1
481 && $ref->{size} == $size
482 && $ref->{mtime} == $mtime
483 && eval { $load->($ref->{data}); 1 }) {
484 return;
485 }
486 }
412 487
413init $VARDIR; 488 my $ref = {
489 version => 1,
490 size => $size,
491 mtime => $mtime,
492 data => $create->(),
493 };
494
495 $load->($ref->{data});
496
497 save_ref $ref, $cache;
498}
499
500=item set_libdir $path
501
502Sets the library directory to the given path
503(default: $ENV{CROSSFIRE_LIBDIR}).
504
505You have to (re-)load the archetypes and tilecache manually after steting
506the library path.
507
508=cut
509
510sub set_libdir($) {
511 $LIB = $_[0];
512}
513
514=item load_archetypes
515
516(Re-)Load archetypes into %ARCH.
517
518=cut
519
520sub load_archetypes() {
521 cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub {
522 *ARCH = $_[0];
523 }, sub {
524 read_arch "$LIB/archetypes"
525 };
526}
527
528=item load_tilecache
529
530(Re-)Load %TILE and %FACE.
531
532=cut
533
534sub load_tilecache() {
535 require Gtk2;
536
537 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub {
538 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
539 or die "$VARDIR/tilecache.png: $!";
540 *FACE = $_[0];
541 }, sub {
542 require File::Temp;
543
544 my $tile = read_pak "$LIB/crossfire.0";
545
546 my %cache;
547
548 my $idx = 0;
549
550 for my $name (sort keys %$tile) {
551 my ($fh, $filename) = File::Temp::tempfile ();
552 print $fh $tile->{$name};
553 close $fh;
554 my $pb = new_from_file Gtk2::Gdk::Pixbuf $filename;
555 unlink $filename;
556
557 my $tile = $cache{$name} = {
558 pb => $pb,
559 idx => $idx,
560 w => int $pb->get_width / TILESIZE,
561 h => int $pb->get_height / TILESIZE,
562 };
563
564
565 $idx += $tile->{w} * $tile->{h};
566 }
567
568 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
569
570 while (my ($name, $tile) = each %cache) {
571 my $tpb = delete $tile->{pb};
572 my $ofs = $tile->{idx};
573
574 for my $x (0 .. $tile->{w} - 1) {
575 for my $y (0 .. $tile->{h} - 1) {
576 my $idx = $ofs + $x + $y * $tile->{w};
577 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
578 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
579 }
580 }
581 }
582
583 $pb->save ("$VARDIR/tilecache.png", "png");
584
585 \%cache
586 };
587}
414 588
415=head1 AUTHOR 589=head1 AUTHOR
416 590
417 Marc Lehmann <schmorp@schmorp.de> 591 Marc Lehmann <schmorp@schmorp.de>
418 http://home.schmorp.de/ 592 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines