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.11 by elmex, Tue Feb 7 14:30:08 2006 UTC vs.
Revision 1.43 by root, Mon Mar 13 03:30:37 2006 UTC

10 10
11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp ();
16use File::Spec;
17use List::Util qw(min max);
15use Storable; 18use Storable;
16 19
17#XXX: The map_* procedures scream for a map-object
18
19our @EXPORT = 20our @EXPORT =
20 qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs 21 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
21 arch2pickmap arch_extends
22 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
23 );
24 22
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 23our $LIB = $ENV{CROSSFIRE_LIBDIR}
26 or die "\$CROSSFIRE_LIBDIR must be set\n"; 24 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
27 25
28sub TILESIZE (){ 32 } 26sub TILESIZE (){ 32 }
29 27
28our $VARDIR;
30our $ARCH; 29our %ARCH;
30our %FACE;
31our $TILE;
32
33our %FIELD_MULTILINE = (
34 msg => "endmsg",
35 lore => "endlore",
36);
37
38# not used yet, maybe alphabetical is ok
39our @FIELD_ORDER = (qw(name name_pl));
40
41sub MOVE_WALK (){ 0x1 }
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 = (
81 "enter_x" => "hp",
82 "enter_y" => "sp",
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}
31 133
32sub read_pak($;$) { 134sub read_pak($;$) {
33 my ($path, $cache) = @_; 135 my ($path, $cache) = @_;
34 136
35 eval { 137 eval {
36 defined $cache 138 defined $cache
37 && -M $cache < -M $path 139 && -M $cache < -M $path
38 && Storable::retrieve $cache 140 && load_ref $cache
39 } or do { 141 } or do {
40 my %pak; 142 my %pak;
41 143
42 open my $fh, "<:raw", $path 144 open my $fh, "<", $path
43 or die "$_[0]: $!"; 145 or Carp::croak "$_[0]: $!";
146 binmode $fh;
44 while (<$fh>) { 147 while (<$fh>) {
45 my ($type, $id, $len, $path) = split; 148 my ($type, $id, $len, $path) = split;
46 $path =~ s/.*\///; 149 $path =~ s/.*\///;
47 read $fh, $pak{$path}, $len; 150 read $fh, $pak{$path}, $len;
48 } 151 }
49 152
50 Storable::nstore \%pak, $cache 153 save_ref \%pak, $cache
51 if defined $cache; 154 if defined $cache;
52 155
53 \%pak 156 \%pak
54 } 157 }
55} 158}
58 my ($path, $cache) = @_; 161 my ($path, $cache) = @_;
59 162
60 eval { 163 eval {
61 defined $cache 164 defined $cache
62 && -M $cache < -M $path 165 && -M $cache < -M $path
63 && Storable::retrieve $cache 166 && load_ref $cache
64 } or do { 167 } or do {
65 my %arc; 168 my %arc;
66 my ($more, $prev); 169 my ($more, $prev);
67 170
68 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
69 or die "$path: $!"; 172 or Carp::croak "$path: $!";
173
174 binmode $fh;
70 175
71 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
72 my %arc = @_; 177 my %arc = @_;
73 178
74 while (<$fh>) { 179 while (<$fh>) {
75 s/\s+$//; 180 s/\s+$//;
76 if (/^end$/i) { 181 if (/^end$/i) {
77 last; 182 last;
78 } elsif (/^arch (\S+)$/) { 183 } elsif (/^arch (\S+)$/) {
79 push @{ $arc{inventory} }, $parse_block->(_name => $1); 184 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
80 } elsif (/^lore$/) { 185 } elsif (/^lore$/) {
81 while (<$fh>) { 186 while (<$fh>) {
82 last if /^endlore\s*$/i; 187 last if /^endlore\s*$/i;
83 $arc{lore} .= $_; 188 $arc{lore} .= $_;
84 } 189 }
113 $arc{$name} = $arc; 218 $arc{$name} = $arc;
114 } 219 }
115 $prev = $arc; 220 $prev = $arc;
116 $more = undef; 221 $more = undef;
117 } elsif (/^arch (\S+)$/i) { 222 } elsif (/^arch (\S+)$/i) {
118 push @{ $arc{arch} }, $parse_block->(_name => $1); 223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
119 } elsif (/^\s*($|#)/) { 224 } elsif (/^\s*($|#)/) {
120 # 225 #
121 } else { 226 } else {
122 warn "$path: unparseable top-level line '$_'"; 227 warn "$path: unparseable top-level line '$_'";
123 } 228 }
124 } 229 }
125 230
126 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.
127 232
128 Storable::nstore \%arc, $cache 233 save_ref \%arc, $cache
129 if defined $cache; 234 if defined $cache;
130 235
131 \%arc 236 \%arc
132 } 237 }
133} 238}
134
135# returns the arch/object stack from a tile on a map
136sub map_get_tile_stack {
137 my ($map, $x, $y) = @_;
138 my $as;
139
140 if ($x > 0 || $x < $map->{width}
141 || $y > 0 || $y < $map->{height}) {
142
143 $as = $map->{map}{map}[$x][$y] || [];
144 }
145
146 return $as;
147}
148
149# pop the topmost arch/object from the stack of a tile on a map
150sub map_pop_tile_stack {
151 my ($map, $x, $y) = @_;
152
153 if ($x > 0 || $x < $map->{width}
154 || $y > 0 || $y < $map->{height}) {
155
156 pop @{$map->{map}{map}[$x][$y]};
157 }
158}
159
160# pushes the arch/object on the stack of a tile on a map
161sub map_push_tile_stack {
162 my ($map, $x, $y, $arch) = @_;
163
164 if ($x > 0 || $x < $map->{width}
165 || $y > 0 || $y < $map->{height}) {
166
167 push @{$map->{map}{map}[$x][$y]}, $arch;
168 }
169}
170
171 239
172# 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
173# NOTE: the arrays in the hash values are references to 241# NOTE: the arrays in the hash values are references to
174# the archs from $ARCH 242# the archs from $ARCH
175sub editor_archs { 243sub editor_archs {
176 my %paths; 244 my %paths;
177 245
178 for (keys %$ARCH) { 246 for (keys %ARCH) {
179 my $arch = $ARCH->{$_}; 247 my $arch = $ARCH{$_};
180 push @{$paths{$arch->{editor_folder}}}, \$arch; 248 push @{$paths{$arch->{editor_folder}}}, \$arch;
181 } 249 }
182 250
183 return \%paths; 251 \%paths
184} 252}
185 253
186# arch_extends determines how the arch looks like on the map, 254=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
187# bigfaces, linked faces and single faces are handled here 255
188# it returns (<xoffset>, <yoffset>, <width>, <height>) 256arch_extents determines the extents of the given arch's face(s), linked
189# 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
190sub arch_extends { 262sub arch_extents {
191 my ($a) = @_; 263 my ($a) = @_;
192 264
193 my $TC = \%Crossfire::Tilecache::TILECACHE; 265 my $o = $ARCH{$a->{_name}}
194
195 my $facename =
196 $a->{face} || $ARCH->{$a->{_name}}->{face}
197 or return (); 266 or return;
198 267
199 my $tile = $TC->{$facename} 268 my $face = $FACE{$a->{face} || $o->{face}}
200 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 269 or (warn "no face data found for arch '$a->{_name}'"), return;
201 270
202 if ($tile->{w} > 1 || $tile->{h} > 1) { 271 if ($face->{w} > 1 || $face->{h} > 1) {
203 # bigfaces 272 # bigface
204 return (0, 0, $tile->{w}, $tile->{h}); 273 return (0, 0, $face->{w} - 1, $face->{h} - 1);
205 274
206 } elsif ($a->{more}) { 275 } elsif ($o->{more}) {
207 # linked faces 276 # linked face
208 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 277 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
209 do {
210 $miw > (0 + $a->{x}) and $miw = $a->{x};
211 $mih > (0 + $a->{y}) and $mih = $a->{y};
212 $maw < (0 + $a->{x}) and $maw = $a->{x};
213 $mah < (0 + $a->{y}) and $mah = $a->{y};
214 } while $a = $a->{more};
215 278
216 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);
217 287
218 } else { 288 } else {
219 # single face 289 # single face
220 return (0, 0, 1, 1); 290 return (0, 0, 0, 0);
221 } 291 }
222} 292}
223 293
224# arch2pickmap forms a list of archs to a pickmap 294=item $type = arch_attr $arch
225sub arch2pickmap { 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($) {
226 my ($archs, $w) = @_; 310 my ($arch) = @_;
227 311
228 # sort archs alphabetiacally 312 require Crossfire::Data;
229 my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ];
230 313
231 $w ||= 10; # default width 314 my $root;
232 my $num = @$archs;
233 my $map = { };
234 # overall placement coords
235 my $x = 0;
236 my $y = 0;
237 315
238 my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later 316 if ($arch->{type} > 0) {
239 my $drawn_archs = 1; # line-break counter 317 $root = $Crossfire::Data::ATTR{$arch->{type}+0};
240 my $max_line_height = 1; 318 } else {
319 $root = $Crossfire::Data::TYPE{Misc};
241 320
242 for (my $i = 0; $i < $num; $i++) { 321 type:
322 for (@Crossfire::Data::ATTR0) {
323 my $req = $_->{required}
324 or die "internal error: ATTR0 without 'required'";
243 325
244 defined ${$archs->[$i]}->{face} or next; 326 keys %$req;
245 327 while (my ($k, $v) = each %$req) {
246 # check whether this tile was already written (see below at (b)) 328 next type
247 unless (defined $map->{map}[$x][$y]) { 329 unless $arch->{$k} == $v;
248
249 my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
250
251 # these are special placement coords, for chained faces which
252 # have a special placement offset
253 my ($place_x, $place_y) = ($x, $y);
254 $xoffs < 0 and
255 $place_x += -$xoffs;
256 $yoffs < 0 and
257 $place_y += -$yoffs;
258
259 # iterate over the tiles this arch takes
260 # NOTE: Chained archs are maybe not a rectangle, but i don't care
261 # much for that on pickmaps
262
263 for (my $xi = 0; $xi < $arch_w; $xi++) {
264 for (my $yi = 0; $yi < $arch_h; $yi++) {
265
266 my ($lx, $ly) = ($x + $xi, $y + $yi);
267
268 if ($lx == $place_x and $ly == $place_y) {
269 push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
270
271 } else {
272
273 # (b): here we set occupied tiles, but without the arch
274 $map->{map}[$lx][$ly] = [];
275 }
276 }
277 } 330 }
278 $drawn_archs++;
279 331
280 $x += $arch_w - 1; 332 $root = $_;
281 $max_line_height < $arch_h 333 }
282 and $max_line_height = $arch_h; 334 }
283 335
284 } else { 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 warn "$name, $attr\n";#d#
356 for (@$attr) {
357 my ($k, $v) = @$_;
358 push @attr_order, $k;
359 $section{$name}{$k} ||= $v;
360 }
361 }
362 }
363
364 $attr->{section} = [
365 map !exists $section{$_} ? () : do {
366 my $attr = delete $section{$_};
367
368 [
369 $_,
370 map exists $attr->{$_} && !$ignore{$_}
371 ? [$_ => delete $attr->{$_}] : (),
372 @attr_order
373 ]
374 },
285 $i--; 375
376 exists $section{$_} ? [$_ => delete $section{$_}] : (),
377 @section_order
378 ];
379
380 $attr
381}
382
383sub arch_edit_sections {
384# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
385# edit_type = 0;
386# else if (edit_type != 0) {
387# // all flags from 'check_type' must be unset in this arch because they get recalculated now
388# edit_type &= ~check_type;
286 } 389# }
287 390#
288
289 $x++;
290
291 if ($x > $w) {
292
293 $y += $max_line_height;
294 $max_line_height = 1;
295 $x = 0;
296 } 391# }
297 392# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
298 $maxw < ($x + 1) and $maxw = $x + 1; 393# getAttributeValue("alive", defarch) == 1 &&
299 $maxh < ($y + 1) and $maxh = $y + 1; 394# (getAttributeValue("monster", defarch) == 1 ||
300 } 395# getAttributeValue("generator", defarch) == 1)) {
301 396# // Monster: monsters/npcs/generators
302 $map->{height} = $maxh; 397# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
303 $map->{width} = $maxw;
304
305 return $map;
306}
307
308sub arch2map($;$) {
309 my ($mapa) = @_;
310
311 my %meta;
312
313 my ($mapx, $mapy);
314
315 my $map;
316
317 for (@{ $mapa->{arch} }) {
318 my ($x, $y) = (delete $_->{x}, delete $_->{y});
319
320 if ($_->{_name} eq "map") {
321 $meta{info} = $_;
322
323 $mapx = $_->{width} || $x;
324 $mapy = $_->{height} || $y;
325 } else {
326 push @{ $map->[$x][$y] }, $_;
327
328 # arch map is unreliable w.r.t. width and height
329 $mapx = $x + 1 if $mapx <= $x;
330 $mapy = $y + 1 if $mapy <= $y;
331 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
332 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
333 } 398# }
334 } 399# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
335 400# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
336 $meta{width} = $mapx; 401# // Walls
337 $meta{height} = $mapy; 402# edit_type |= IGUIConstants.TILE_EDIT_WALL;
338 $meta{map} = $map; 403# }
339 404# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
340 \%meta 405# getAttributeValue("connected", defarch) != 0) {
406# // Connected Objects
407# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
408# }
409# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
410# arch_type == 66 || arch_type == 41 || arch_type == 95) {
411# // Exit: teleporter/exit/trapdoors
412# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
413# }
414# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
415# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
416# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
417# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
418# arch_type == 124 || arch_type == 130)) {
419# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
420# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
421# }
422# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
423# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
424# arch_type == 91 || arch_type == 21 || arch_type == 24) {
425# // Door: door/special door/gates + keys
426# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
427# }
428# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
429# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
430# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
431# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
432# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
433# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
434# arch_type == 122 || arch_type == 3)) {
435# // Equipment: weapons/armour/wands/rods
436# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
437# }
438#
439# return(edit_type);
440#
441#
341} 442}
342 443
343sub init($) { 444sub init($) {
344 my ($cachedir) = @_; 445 my ($cachedir) = @_;
345 446
447 return if %ARCH;
448
449 mkdir $cachedir, 0777;
346 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 450 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
347} 451}
452
453$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
454
455init $VARDIR;
348 456
349=head1 AUTHOR 457=head1 AUTHOR
350 458
351 Marc Lehmann <schmorp@schmorp.de> 459 Marc Lehmann <schmorp@schmorp.de>
352 http://home.schmorp.de/ 460 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines