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.49 by root, Thu Mar 16 22:10:25 2006 UTC vs.
Revision 1.50 by root, Mon Mar 20 01:12:23 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
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
23our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR};
24 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;
25 29
26sub TILESIZE (){ 32 } 30sub TILESIZE (){ 32 }
27 31
28our $VARDIR;
29our %ARCH; 32our %ARCH;
30our %FACE; 33our %FACE;
31our $TILE; 34our $TILE;
32 35
33our %FIELD_MULTILINE = ( 36our %FIELD_MULTILINE = (
34 msg => "endmsg", 37 msg => "endmsg",
35 lore => "endlore", 38 lore => "endlore",
39 maplore => "endmaplore",
36); 40);
37 41
38# not used yet, maybe alphabetical is ok 42# not used yet, maybe alphabetical is ok
39our @FIELD_ORDER = (qw(name name_pl)); 43our @FIELD_ORDER = (qw(name name_pl));
40 44
41sub MOVE_WALK (){ 0x1 } 45sub MOVE_WALK (){ 0x01 }
42sub MOVE_FLY_LOW (){ 0x2 } 46sub MOVE_FLY_LOW (){ 0x02 }
43sub MOVE_FLY_HIGH (){ 0x4 } 47sub MOVE_FLY_HIGH (){ 0x04 }
44sub MOVE_FLYING (){ 0x6 } 48sub MOVE_FLYING (){ 0x06 }
45sub MOVE_SWIM (){ 0x8 } 49sub MOVE_SWIM (){ 0x08 }
50sub MOVE_BOAT (){ 0x10 }
46sub MOVE_ALL (){ 0xf } 51sub MOVE_ALL (){ 0xff }
47 52
48sub load_ref($) { 53sub load_ref($) {
49 my ($path) = @_; 54 my ($path) = @_;
50 55
51 open my $fh, "<", $path 56 open my $fh, "<", $path
72 my ($ob) = @_; 77 my ($ob) = @_;
73 78
74 my $arch = $ARCH{$ob->{_name}} 79 my $arch = $ARCH{$ob->{_name}}
75 or (warn "$ob->{_name}: no such archetype", return $ob); 80 or (warn "$ob->{_name}: no such archetype", return $ob);
76 81
82 delete $ob->{$_} for qw(
77 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 );
78 86
79 if ($arch->{type} == 22) { # map 87 if ($arch->{type} == 22) { # map
80 my %normalize = ( 88 my %normalize = (
81 "enter_x" => "hp", 89 "enter_x" => "hp",
82 "enter_y" => "sp", 90 "enter_y" => "sp",
94 $ob->{$k2} = $v; 102 $ob->{$k2} = $v;
95 } 103 }
96 } 104 }
97 } 105 }
98 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
99 if (defined (my $v = delete $ob->{no_pass})) { 135 if (defined (my $v = delete $ob->{no_pass})) {
100 $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;
101 } 141 }
102 if (defined (my $v = delete $ob->{walk_on})) { 142 if (defined (my $v = delete $ob->{walk_on})) {
103 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK 143 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
104 : $ob->{move_on} & ~MOVE_WALK; 144 : $ob->{move_on} & ~MOVE_WALK;
105 } 145 }
129 } 169 }
130 170
131 $ob 171 $ob
132} 172}
133 173
134sub read_pak($;$) { 174sub read_pak($) {
135 my ($path, $cache) = @_; 175 my ($path) = @_;
136 176
137 eval {
138 defined $cache
139 && -M $cache < -M $path
140 && load_ref $cache
141 } or do {
142 my %pak; 177 my %pak;
143 178
144 open my $fh, "<", $path 179 open my $fh, "<", $path
145 or Carp::croak "$_[0]: $!"; 180 or Carp::croak "$_[0]: $!";
146 binmode $fh; 181 binmode $fh;
147 while (<$fh>) { 182 while (<$fh>) {
148 my ($type, $id, $len, $path) = split; 183 my ($type, $id, $len, $path) = split;
149 $path =~ s/.*\///; 184 $path =~ s/.*\///;
150 read $fh, $pak{$path}, $len; 185 read $fh, $pak{$path}, $len;
151 } 186 }
152 187
153 save_ref \%pak, $cache
154 if defined $cache;
155
156 \%pak 188 \%pak
157 }
158} 189}
159 190
160sub read_arch($;$) { 191sub read_arch($) {
161 my ($path, $cache) = @_; 192 my ($path) = @_;
162 193
163 eval {
164 defined $cache
165 && -M $cache < -M $path
166 && load_ref $cache
167 } or do {
168 my %arc; 194 my %arc;
169 my ($more, $prev); 195 my ($more, $prev);
170 196
171 open my $fh, "<", $path 197 open my $fh, "<", $path
172 or Carp::croak "$path: $!"; 198 or Carp::croak "$path: $!";
173 199
174 binmode $fh; 200 binmode $fh;
175 201
176 my $parse_block; $parse_block = sub { 202 my $parse_block; $parse_block = sub {
177 my %arc = @_; 203 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 204
207 while (<$fh>) { 205 while (<$fh>) {
208 s/\s+$//; 206 s/\s+$//;
209 if (/^more$/i) { 207 if (/^end$/i) {
210 $more = $prev; 208 last;
211 } elsif (/^object (\S+)$/i) { 209 } elsif (/^arch (\S+)$/) {
212 my $name = $1; 210 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
213 my $arc = $parse_block->(_name => $name); 211 } elsif (/^lore$/) {
214 212 while (<$fh>) {
215 if ($more) { 213 last if /^endlore\s*$/i;
216 $more->{more} = $arc;
217 } else {
218 $arc{$name} = $arc; 214 $arc{lore} .= $_;
219 } 215 }
220 $prev = $arc; 216 } elsif (/^msg$/) {
221 $more = undef; 217 while (<$fh>) {
218 last if /^endmsg\s*$/i;
219 $arc{msg} .= $_;
220 }
222 } elsif (/^arch (\S+)$/i) { 221 } elsif (/^(\S+)\s*(.*)$/) {
223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); 222 $arc{lc $1} = $2;
224 } elsif (/^\s*($|#)/) { 223 } elsif (/^\s*($|#)/) {
225 # 224 #
226 } else { 225 } else {
227 warn "$path: unparseable top-level line '$_'"; 226 warn "$path: unparsable line '$_' in arch $arc{_name}";
228 } 227 }
229 } 228 }
230 229
231 undef $parse_block; # work around bug in perl not freeing $fh etc.
232
233 save_ref \%arc, $cache
234 if defined $cache;
235
236 \%arc 230 \%arc
237 } 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
238} 260}
239 261
240# 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
241# NOTE: the arrays in the hash values are references to 263# NOTE: the arrays in the hash values are references to
242# the archs from $ARCH 264# the archs from $ARCH
444# return(edit_type); 466# return(edit_type);
445# 467#
446# 468#
447} 469}
448 470
449sub init($) { 471sub cache_file($$&&) {
450 my ($cachedir) = @_; 472 my ($src, $cache, $load, $create) = @_;
451 473
452 return if %ARCH; 474 warn "<@_>\n";#d#
453 475
454 mkdir $cachedir, 0777; 476 my ($size, $mtime) = (stat $src)[7,9]
455 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 477 or Carp::croak "$src: $!";
456}
457 478
458$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; 479 if (-e $cache) {
480 my $ref = eval { load_ref $cache };
459 481
460init $VARDIR; 482 if ($ref->{version} == 1
483 && $ref->{size} == $size
484 && $ref->{mtime} == $mtime
485 && eval { $load->($ref->{data}); 1 }) {
486 return;
487 }
488 }
489
490 my $ref = {
491 version => 1,
492 size => $size,
493 mtime => $mtime,
494 data => $create->(),
495 };
496
497 $load->($ref->{data});
498
499 save_ref $ref, $cache;
500}
501
502=item set_libdir $path
503
504Sets the library directory to the given path
505(default: $ENV{CROSSFIRE_LIBDIR}).
506
507You have to (re-)load the archetypes and tilecache manually after steting
508the library path.
509
510=cut
511
512sub set_libdir($) {
513 $LIB = $_[0];
514}
515
516=item load_archetypes
517
518(Re-)Load archetypes into %ARCH.
519
520=cut
521
522sub load_archetypes() {
523 cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub {
524 *ARCH = $_[0];
525 }, sub {
526 read_arch "$LIB/archetypes"
527 };
528}
529
530=item load_tilecache
531
532(Re-)Load %TILE and %FACE.
533
534=cut
535
536sub load_tilecache() {
537 require Gtk2;
538
539 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub {
540 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
541 or die "$VARDIR/tilecache.png: $!";
542 *FACE = $_[0];
543 }, sub {
544 require File::Temp;
545
546 my $tile = read_pak "$LIB/crossfire.0";
547
548 my %cache;
549
550 my $idx = 0;
551
552 for my $name (sort keys %$tile) {
553 my ($fh, $filename) = File::Temp::tempfile ();
554 print $fh $tile->{$name};
555 close $fh;
556 my $pb = new_from_file Gtk2::Gdk::Pixbuf $filename;
557 unlink $filename;
558
559 my $tile = $cache{$name} = {
560 pb => $pb,
561 idx => $idx,
562 w => int $pb->get_width / TILESIZE,
563 h => int $pb->get_height / TILESIZE,
564 };
565
566
567 $idx += $tile->{w} * $tile->{h};
568 }
569
570 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
571
572 while (my ($name, $tile) = each %cache) {
573 my $tpb = delete $tile->{pb};
574 my $ofs = $tile->{idx};
575
576 for my $x (0 .. $tile->{w} - 1) {
577 for my $y (0 .. $tile->{h} - 1) {
578 my $idx = $ofs + $x + $y * $tile->{w};
579 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
580 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
581 }
582 }
583 }
584
585 $pb->save ("$VARDIR/tilecache.png", "png");
586
587 \%cache
588 };
589}
461 590
462=head1 AUTHOR 591=head1 AUTHOR
463 592
464 Marc Lehmann <schmorp@schmorp.de> 593 Marc Lehmann <schmorp@schmorp.de>
465 http://home.schmorp.de/ 594 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines