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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines