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.13 by root, Thu Feb 9 20:54:42 2006 UTC vs.
Revision 1.40 by root, Sun Mar 12 23:35:56 2006 UTC

11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp (); 15use Carp ();
16use File::Spec;
17use List::Util qw(min max);
16use Storable; 18use Storable;
17 19
18#XXX: The map_* procedures scream for a map-object
19
20our @EXPORT = 20our @EXPORT =
21 qw(read_pak read_arch $ARCH TILESIZE editor_archs 21 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
22 arch_extends
23 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
24 );
25 22
26our $LIB = $ENV{CROSSFIRE_LIBDIR} 23our $LIB = $ENV{CROSSFIRE_LIBDIR}
27 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; 24 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
28 25
29sub TILESIZE (){ 32 } 26sub TILESIZE (){ 32 }
30 27
28our $VARDIR;
31our $ARCH; 29our %ARCH;
30our %FACE;
31our $TILE;
32 32
33our %FIELD_MULTILINE = ( 33our %FIELD_MULTILINE = (
34 msg => "endmsg", 34 msg => "endmsg",
35 lore => "endlore",
35); 36);
36 37
37# not used yet, maybe alphabetical is ok 38# not used yet, maybe alphabetical is ok
38our @FIELD_ORDER = (qw(name name_pl)); 39our @FIELD_ORDER = (qw(name name_pl));
39 40
40# not used yet, AND NOT CHECKED, should also be BY TYPE 41sub MOVE_WALK (){ 0x1 }
41our %FIELD_NORMALIZE = ( 42sub MOVE_FLY_LOW (){ 0x2 }
43sub MOVE_FLY_HIGH (){ 0x4 }
44sub MOVE_FLYING (){ 0x6 }
45sub MOVE_SWIM (){ 0x8 }
46sub MOVE_ALL (){ 0xf }
47
48sub load_ref($) {
49 my ($path) = @_;
50
51 open my $fh, "<", $path
52 or die "$path: $!";
53 binmode $fh;
54 local $/;
55
56 Storable::thaw <$fh>
57}
58
59sub save_ref($$) {
60 my ($ref, $path) = @_;
61
62 open my $fh, ">", "$path~"
63 or die "$path~: $!";
64 binmode $fh;
65 print $fh Storable::freeze $ref;
66 close $fh;
67 rename "$path~", $path
68 or die "$path: $!";
69}
70
71sub normalize_arch($) {
72 my ($ob) = @_;
73
74 my $arch = $ARCH{$ob->{_name}}
75 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
79 if ($arch->{type} == 22) { # map
80 my %normalize = (
42 "enter_x" => "hp", 81 "enter_x" => "hp",
43 "enter_y" => "sp", 82 "enter_y" => "sp",
44); 83 "width" => "x",
84 "height" => "y",
85 "reset_timeout" => "weight",
86 "swap_time" => "value",
87 "difficulty" => "level",
88 "darkness" => "invisible",
89 "fixed_resettime" => "stand_still",
90 );
91
92 while (my ($k2, $k1) = each %normalize) {
93 if (defined (my $v = delete $ob->{$k1})) {
94 $ob->{$k2} = $v;
95 }
96 }
97 }
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
124 while (my ($k, $v) = each %$ob) {
125 if (exists $arch->{$k} and $arch->{$k} eq $v) {
126 next if $k eq "_name";
127 delete $ob->{$k};
128 }
129 }
130
131 $ob
132}
45 133
46sub read_pak($;$) { 134sub read_pak($;$) {
47 my ($path, $cache) = @_; 135 my ($path, $cache) = @_;
48 136
49 eval { 137 eval {
50 defined $cache 138 defined $cache
51 && -M $cache < -M $path 139 && -M $cache < -M $path
52 && Storable::retrieve $cache 140 && load_ref $cache
53 } or do { 141 } or do {
54 my %pak; 142 my %pak;
55 143
56 open my $fh, "<:raw", $path 144 open my $fh, "<", $path
57 or Carp::croak "$_[0]: $!"; 145 or Carp::croak "$_[0]: $!";
146 binmode $fh;
58 while (<$fh>) { 147 while (<$fh>) {
59 my ($type, $id, $len, $path) = split; 148 my ($type, $id, $len, $path) = split;
60 $path =~ s/.*\///; 149 $path =~ s/.*\///;
61 read $fh, $pak{$path}, $len; 150 read $fh, $pak{$path}, $len;
62 } 151 }
63 152
64 Storable::nstore \%pak, $cache 153 save_ref \%pak, $cache
65 if defined $cache; 154 if defined $cache;
66 155
67 \%pak 156 \%pak
68 } 157 }
69} 158}
72 my ($path, $cache) = @_; 161 my ($path, $cache) = @_;
73 162
74 eval { 163 eval {
75 defined $cache 164 defined $cache
76 && -M $cache < -M $path 165 && -M $cache < -M $path
77 && Storable::retrieve $cache 166 && load_ref $cache
78 } or do { 167 } or do {
79 my %arc; 168 my %arc;
80 my ($more, $prev); 169 my ($more, $prev);
81 170
82 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
83 or Carp::croak "$path: $!"; 172 or Carp::croak "$path: $!";
173
174 binmode $fh;
84 175
85 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
86 my %arc = @_; 177 my %arc = @_;
87 178
88 while (<$fh>) { 179 while (<$fh>) {
89 s/\s+$//; 180 s/\s+$//;
90 if (/^end$/i) { 181 if (/^end$/i) {
91 last; 182 last;
92 } elsif (/^arch (\S+)$/) { 183 } elsif (/^arch (\S+)$/) {
93 push @{ $arc{inventory} }, $parse_block->(_name => $1); 184 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
94 } elsif (/^lore$/) { 185 } elsif (/^lore$/) {
95 while (<$fh>) { 186 while (<$fh>) {
96 last if /^endlore\s*$/i; 187 last if /^endlore\s*$/i;
97 $arc{lore} .= $_; 188 $arc{lore} .= $_;
98 } 189 }
127 $arc{$name} = $arc; 218 $arc{$name} = $arc;
128 } 219 }
129 $prev = $arc; 220 $prev = $arc;
130 $more = undef; 221 $more = undef;
131 } elsif (/^arch (\S+)$/i) { 222 } elsif (/^arch (\S+)$/i) {
132 push @{ $arc{arch} }, $parse_block->(_name => $1); 223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
133 } elsif (/^\s*($|#)/) { 224 } elsif (/^\s*($|#)/) {
134 # 225 #
135 } else { 226 } else {
136 warn "$path: unparseable top-level line '$_'"; 227 warn "$path: unparseable top-level line '$_'";
137 } 228 }
138 } 229 }
139 230
140 undef $parse_block; # work around bug in perl not freeing $fh etc. 231 undef $parse_block; # work around bug in perl not freeing $fh etc.
141 232
142 Storable::nstore \%arc, $cache 233 save_ref \%arc, $cache
143 if defined $cache; 234 if defined $cache;
144 235
145 \%arc 236 \%arc
146 } 237 }
147} 238}
148
149# returns the arch/object stack from a tile on a map
150sub map_get_tile_stack {
151 my ($map, $x, $y) = @_;
152 my $as;
153
154 if ($x > 0 || $x < $map->{width}
155 || $y > 0 || $y < $map->{height}) {
156
157 $as = $map->{map}{map}[$x][$y] || [];
158 }
159
160 return $as;
161}
162
163# pop the topmost arch/object from the stack of a tile on a map
164sub map_pop_tile_stack {
165 my ($map, $x, $y) = @_;
166
167 if ($x > 0 || $x < $map->{width}
168 || $y > 0 || $y < $map->{height}) {
169
170 pop @{$map->{map}{map}[$x][$y]};
171 }
172}
173
174# pushes the arch/object on the stack of a tile on a map
175sub map_push_tile_stack {
176 my ($map, $x, $y, $arch) = @_;
177
178 if ($x > 0 || $x < $map->{width}
179 || $y > 0 || $y < $map->{height}) {
180
181 push @{$map->{map}{map}[$x][$y]}, $arch;
182 }
183}
184
185 239
186# put all archs into a hash with editor_face as it's key 240# put all archs into a hash with editor_face as it's key
187# NOTE: the arrays in the hash values are references to 241# NOTE: the arrays in the hash values are references to
188# the archs from $ARCH 242# the archs from $ARCH
189sub editor_archs { 243sub editor_archs {
190 my %paths; 244 my %paths;
191 245
192 for (keys %$ARCH) { 246 for (keys %ARCH) {
193 my $arch = $ARCH->{$_}; 247 my $arch = $ARCH{$_};
194 push @{$paths{$arch->{editor_folder}}}, \$arch; 248 push @{$paths{$arch->{editor_folder}}}, \$arch;
195 } 249 }
196 250
197 return \%paths; 251 \%paths
198} 252}
199 253
200# arch_extends determines how the arch looks like on the map, 254=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
201# bigfaces, linked faces and single faces are handled here 255
202# it returns (<xoffset>, <yoffset>, <width>, <height>) 256arch_extents determines the extents of the given arch's face(s), linked
203# NOTE: non rectangular linked faces are not considered 257faces and single faces are handled here it returns (minx, miny, maxx,
258maxy)
259
260=cut
261
204sub arch_extends { 262sub arch_extents {
205 my ($a) = @_; 263 my ($a) = @_;
206 264
207 my $TC = \%Crossfire::Tilecache::TILECACHE; 265 my $o = $ARCH{$a->{_name}}
208
209 my $facename =
210 $a->{face} || $ARCH->{$a->{_name}}->{face}
211 or return (); 266 or return;
212 267
213 my $tile = $TC->{$facename} 268 my $face = $FACE{$a->{face} || $o->{face}}
214 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 269 or (warn "no face data found for arch '$a->{_name}'"), return;
215 270
216 if ($tile->{w} > 1 || $tile->{h} > 1) { 271 if ($face->{w} > 1 || $face->{h} > 1) {
217 # bigfaces 272 # bigface
218 return (0, 0, $tile->{w}, $tile->{h}); 273 return (0, 0, $face->{w} - 1, $face->{h} - 1);
219 274
220 } elsif ($a->{more}) { 275 } elsif ($o->{more}) {
221 # linked faces 276 # linked face
222 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 277 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
223 do {
224 $miw > (0 + $a->{x}) and $miw = $a->{x};
225 $mih > (0 + $a->{y}) and $mih = $a->{y};
226 $maw < (0 + $a->{x}) and $maw = $a->{x};
227 $mah < (0 + $a->{y}) and $mah = $a->{y};
228 } while $a = $a->{more};
229 278
230 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) 279 for (; $o; $o = $o->{more}) {
280 $minx = min $minx, $o->{x};
281 $miny = min $miny, $o->{y};
282 $maxx = max $maxx, $o->{x};
283 $maxy = max $maxy, $o->{y};
284 }
285
286 return ($minx, $miny, $maxx, $maxy);
231 287
232 } else { 288 } else {
233 # single face 289 # single face
234 return (0, 0, 1, 1); 290 return (0, 0, 0, 0);
235 } 291 }
292}
293
294=item $type = arch_attr $arch
295
296Returns a hashref describing the object and its attributes. It can contain
297the following keys:
298
299 name the name, suitable for display purposes
300 ignore
301 attr
302 desc
303 use
304 section => [name => \%attr, name => \%attr]
305 import
306
307=cut
308
309sub arch_attr($) {
310 my ($arch) = @_;
311
312 require Crossfire::Data;
313
314 my $root;
315
316 if ($arch->{type} > 0) {
317 $root = $Crossfire::Data::ATTR{$arch->{type}+0};
318 } else {
319 $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 }
331
332 $root = $_;
333 }
334 }
335
336 my $attr = { };
337
338 my @import = (\%Crossfire::Data::DEFAULT_ATTR, $root);
339 my (%ignore);
340 my (@section_order, %section, @attr_order);
341
342 while (my $type = shift @import) {
343 push @import, @{$type->{import} || []};
344
345 $attr->{$_} ||= $type->{$_}
346 for qw(name desc use);
347
348 for (@{$type->{ignore} || []}) {
349 $ignore{$_}++ for ref $_ ? @$_ : $_;
350 }
351
352 for ([general => ($type->{attr} || {})], @{$type->{section} || []}) {
353 my ($name, $attr) = @$_;
354 push @section_order, $name;
355 for (@$attr) {
356 my ($k, $v) = @$_;
357 push @attr_order, $k;
358 $section{$name}{$k} ||= $v;
359 }
360 }
361 }
362
363 $attr->{section} = [
364 map !exists $section{$_} ? () : do {
365 my $attr = delete $section{$_};
366
367 [
368 $_,
369 map exists $attr->{$_} && !$ignore{$_}
370 ? [$_ => delete $attr->{$_}] : (),
371 @attr_order
372 ]
373 },
374
375 exists $section{$_} ? [$_ => delete $section{$_}] : (),
376 @section_order
377 ];
378
379 use PApp::Util;
380 warn PApp::Util::dumpval $attr;
381
382 $attr
383}
384
385sub arch_edit_sections {
386# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
387# edit_type = 0;
388# else if (edit_type != 0) {
389# // all flags from 'check_type' must be unset in this arch because they get recalculated now
390# edit_type &= ~check_type;
391# }
392#
393# }
394# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
395# getAttributeValue("alive", defarch) == 1 &&
396# (getAttributeValue("monster", defarch) == 1 ||
397# getAttributeValue("generator", defarch) == 1)) {
398# // Monster: monsters/npcs/generators
399# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
400# }
401# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
402# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
403# // Walls
404# edit_type |= IGUIConstants.TILE_EDIT_WALL;
405# }
406# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
407# getAttributeValue("connected", defarch) != 0) {
408# // Connected Objects
409# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
410# }
411# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
412# arch_type == 66 || arch_type == 41 || arch_type == 95) {
413# // Exit: teleporter/exit/trapdoors
414# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
415# }
416# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
417# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
418# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
419# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
420# arch_type == 124 || arch_type == 130)) {
421# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
422# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
423# }
424# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
425# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
426# arch_type == 91 || arch_type == 21 || arch_type == 24) {
427# // Door: door/special door/gates + keys
428# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
429# }
430# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
431# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
432# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
433# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
434# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
435# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
436# arch_type == 122 || arch_type == 3)) {
437# // Equipment: weapons/armour/wands/rods
438# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
439# }
440#
441# return(edit_type);
442#
443#
236} 444}
237 445
238sub init($) { 446sub init($) {
239 my ($cachedir) = @_; 447 my ($cachedir) = @_;
240 448
449 return if %ARCH;
450
451 mkdir $cachedir, 0777;
241 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 452 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
242} 453}
454
455$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
456
457init $VARDIR;
243 458
244=head1 AUTHOR 459=head1 AUTHOR
245 460
246 Marc Lehmann <schmorp@schmorp.de> 461 Marc Lehmann <schmorp@schmorp.de>
247 http://home.schmorp.de/ 462 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines