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.1 by elmex, Sat Feb 4 23:24:03 2006 UTC vs.
Revision 1.49 by root, Thu Mar 16 22:10:25 2006 UTC

1=head1 NAME
2
3Crossfire - Crossfire maphandling
4
5=cut
6
1package Crossfire; 7package Crossfire;
2=head1 NAME
3 8
4Crossfire - Crossfire maphandling
5
6=cut
7
8our $VERSION = '1.21'; 9our $VERSION = '0.1';
9 10
10use strict; 11use strict;
11 12
13use base 'Exporter';
14
15use Carp ();
16use File::Spec;
17use List::Util qw(min max);
12use Storable; 18use Storable;
13use List::Util qw(max);
14 19
15#use Gtk2; 20our @EXPORT =
21 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
16 22
17#init Gtk2::Gdk;
18
19my $LIB = $ENV{CROSSFIRE_LIBDIR} 23our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 24 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
21 25
22my $VARDIR = "$ENV{HOME}/.gcfedit";
23mkdir $VARDIR;
24
25sub T (){ 32 } 26sub TILESIZE (){ 32 }
26 27
27sub read_pak($) { 28our $VARDIR;
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($) {
28 my ($path) = @_; 49 my ($path) = @_;
29 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}
133
134sub read_pak($;$) {
135 my ($path, $cache) = @_;
136
30 eval { 137 eval {
31 -M "$VARDIR/crossfire.pak.pst" < -M $path 138 defined $cache
32 && Storable::retrieve "$VARDIR/crossfire.pak.pst" 139 && -M $cache < -M $path
140 && load_ref $cache
33 } or do { 141 } or do {
34 my %pak; 142 my %pak;
35 143
36 open my $fh, "<:raw", $path 144 open my $fh, "<", $path
37 or die "$_[0]: $!"; 145 or Carp::croak "$_[0]: $!";
146 binmode $fh;
38 while (<$fh>) { 147 while (<$fh>) {
39 my ($type, $id, $len, $path) = split; 148 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 149 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 150 read $fh, $pak{$path}, $len;
42 } 151 }
43 152
44 Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; 153 save_ref \%pak, $cache
154 if defined $cache;
45 155
46 \%pak 156 \%pak
47 } 157 }
48} 158}
49 159
50sub read_arch($;$) { 160sub read_arch($;$) {
51 my ($path, $cache) = @_; 161 my ($path, $cache) = @_;
52 162
53 eval { 163 eval {
54 $cache 164 defined $cache
55 && -M "$VARDIR/$path.pst" < -M $path 165 && -M $cache < -M $path
56 && Storable::retrieve "$VARDIR/$path.pst" 166 && load_ref $cache
57 } or do { 167 } or do {
58 my %arc; 168 my %arc;
59 my ($more, $prev); 169 my ($more, $prev);
60 170
61 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
62 or die "$path: $!"; 172 or Carp::croak "$path: $!";
173
174 binmode $fh;
63 175
64 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
65 my %arc = @_; 177 my %arc = @_;
66 178
67 while (<$fh>) { 179 while (<$fh>) {
68 s/\s+$//; 180 s/\s+$//;
69 if (/^end$/i) { 181 if (/^end$/i) {
70 last; 182 last;
71 } elsif (/^arch (\S+)$/) { 183 } elsif (/^arch (\S+)$/) {
72 push @{ $arc{inventory} }, $parse_block->(_name => $1); 184 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
73 } elsif (/^lore$/) { 185 } elsif (/^lore$/) {
74 while (<$fh>) { 186 while (<$fh>) {
75 last if /^endlore\s*$/i; 187 last if /^endlore\s*$/i;
76 $arc{lore} .= $_; 188 $arc{lore} .= $_;
77 } 189 }
106 $arc{$name} = $arc; 218 $arc{$name} = $arc;
107 } 219 }
108 $prev = $arc; 220 $prev = $arc;
109 $more = undef; 221 $more = undef;
110 } elsif (/^arch (\S+)$/i) { 222 } elsif (/^arch (\S+)$/i) {
111 push @{ $arc{arch} }, $parse_block->(_name => $1); 223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
112 } elsif (/^\s*($|#)/) { 224 } elsif (/^\s*($|#)/) {
113 # 225 #
114 } else { 226 } else {
115 warn "$path: unparseable top-level line '$_'"; 227 warn "$path: unparseable top-level line '$_'";
116 } 228 }
117 } 229 }
118 230
119 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.
120 232
121 if ($cache) { 233 save_ref \%arc, $cache
122 Storable::nstore \%arc, "$path.pst"; 234 if defined $cache;
123 utime +(stat $path)[8,9], "$path.pst";
124 }
125 235
126 \%arc 236 \%arc
127 } 237 }
128} 238}
129 239
130sub cfmap_meta($;$) { 240# put all archs into a hash with editor_face as it's key
131 my ($self, $mapa, $mapname) = @_; 241# NOTE: the arrays in the hash values are references to
242# the archs from $ARCH
243sub editor_archs {
244 my %paths;
132 245
133 my $arch = $self->{arch}; 246 for (keys %ARCH) {
247 my $arch = $ARCH{$_};
248 push @{$paths{$arch->{editor_folder}}}, \$arch;
249 }
134 250
135 my %meta; 251 \%paths
252}
136 253
137 my ($mapx, $mapy); 254=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
138 255
139 my $map; 256arch_extents determines the extents of the given arch's face(s), linked
257faces and single faces are handled here it returns (minx, miny, maxx,
258maxy)
140 259
141 for (@{ $mapa->{arch} }) { 260=cut
142 my ($x, $y) = ($_->{x}, $_->{y});
143 261
144 if ($_->{_name} eq "map") { 262sub arch_extents {
145 $meta{info} = $_; 263 my ($a) = @_;
146 264
147 $mapx = $_->{width} || $x; 265 my $o = $ARCH{$a->{_name}}
148 $mapy = $_->{height} || $y; 266 or return;
267
268 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
269 or (warn "no face data found for arch '$a->{_name}'"), return;
270
271 if ($face->{w} > 1 || $face->{h} > 1) {
272 # bigface
273 return (0, 0, $face->{w} - 1, $face->{h} - 1);
274
275 } elsif ($o->{more}) {
276 # linked face
277 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
278
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);
287
149 } else { 288 } else {
150 push @{ $map->[$x][$y] }, $_; 289 # single face
290 return (0, 0, 0, 0);
291 }
292}
151 293
152 # arch map is unreliable w.r.t. width and height 294=item $type = arch_attr $arch
153 $mapx = $x + 1 if $mapx <= $x; 295
154 $mapy = $y + 1 if $mapy <= $y; 296Returns a hashref describing the object and its attributes. It can contain
155 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; 297the following keys:
156 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; 298
157 } 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 ($obj) = @_;
311
312 require Crossfire::Data;
313
314 my $root;
315 my $attr = { };
158 } 316
317 my $arch = $ARCH{ $obj->{_name} };
318 my $type = $obj->{type} || $arch->{type};
159 319
160 $meta{width} = $mapx; 320 if ($type > 0) {
161 $meta{height} = $mapy; 321 $root = $Crossfire::Data::ATTR{$type};
322 } else {
323 $root = $Crossfire::Data::TYPE{Misc};
162 324
163 my %draw_info; 325 type:
164 my %map_info; 326 for (@Crossfire::Data::ATTR0) {
327 my $req = $_->{required}
328 or die "internal error: ATTR0 without 'required'";
165 329
166 # first pass, gather face stacking order, border and corner info 330 keys %$req;
167 for my $x (0 .. $mapx - 1) { 331 while (my ($k, $v) = each %$req) {
168 my $col = $map->[$x]; 332 next type
169 for my $y (0 .. $mapy - 1) { 333 unless $obj->{$k} == $v || $arch->{$k} == $v;
170 my $as = $col->[$y] || [];
171
172 for my $layer (0 .. $#$as) {
173 my $a = $as->[$layer];
174
175 my $o = $arch->{$a->{_name}}
176 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
177
178 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
179 my $level = $layer * 256;
180
181 $level -= 100 * 256 if $o->{_name} eq "blocked";
182
183 while ($o) {
184 my $face = $a->{face} || $o->{face};
185
186 my $mx = $x + $o->{x};
187 my $my = $y + $o->{y};
188
189 last if 0 > $mx || $mx >= $mapx
190 || 0 > $my || $my >= $mapy;
191
192 push @{ $map_info{$level}{$mx, $my} }, $a;
193
194 $o = $o->{more};
195 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
196 }
197 } 334 }
335
336 $root = $_;
198 } 337 }
338 }
339
340 my @import = ($root);
199 } 341
342 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
343 unless $type == 116;
200 344
201 # third pass, gather meta info 345 my (%ignore);
202 for my $level (sort { $a <=> $b } keys %map_info) { 346 my (@section_order, %section, @attr_order);
203 my $info = $map_info{$level};
204 347
205 while (my ($xy, $as) = each %$info) { 348 while (my $type = shift @import) {
206 my ($x, $y) = split $;, $xy; 349 push @import, @{$type->{import} || []};
207 350
208 next if $x < 0 || $x >= $mapx 351 $attr->{$_} ||= $type->{$_}
209 || $y < 0 || $y >= $mapy; 352 for qw(name desc use);
210 353
211 push @{ $meta{map}[$x][$y] }, $_ for @$as; 354 for (@{$type->{ignore} || []}) {
355 $ignore{$_}++ for ref $_ ? @$_ : $_;
356 }
357
358 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
359 my ($name, $attr) = @$_;
360 push @section_order, $name;
361 for (@$attr) {
362 my ($k, $v) = @$_;
363 push @attr_order, $k;
364 $section{$name}{$k} ||= $v;
212 } 365 }
366 }
213 } 367 }
214 368
215 \%meta 369 $attr->{section} = [
216} 370 map !exists $section{$_} ? () : do {
371 my $attr = delete $section{$_};
217 372
218sub new { 373 [
219 my $class = shift; 374 $_,
220 my $self = bless { }, $class; 375 map exists $attr->{$_} && !$ignore{$_}
221 $self->{arch} = read_arch "$LIB/archetypes"; 376 ? [$_ => delete $attr->{$_}] : (),
222 $self->{tile} = read_pak "$LIB/crossfire.0"; 377 @attr_order
223 $self 378 ]
224} 379 },
380
381 exists $section{$_} ? [$_ => delete $section{$_}] : (),
382 @section_order
383 ];
225 384
226sub read { 385 $attr
227 my ($self, $file) = @_;
228 my $mapa = read_arch $file;
229 my $map = $self->cfmap_meta ($mapa, $file);
230 print "READ: ".join(',', %{$map->{info}})."\n";
231 return $map;
232} 386}
387
388sub arch_edit_sections {
389# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
390# edit_type = 0;
391# else if (edit_type != 0) {
392# // all flags from 'check_type' must be unset in this arch because they get recalculated now
393# edit_type &= ~check_type;
394# }
395#
396# }
397# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
398# getAttributeValue("alive", defarch) == 1 &&
399# (getAttributeValue("monster", defarch) == 1 ||
400# getAttributeValue("generator", defarch) == 1)) {
401# // Monster: monsters/npcs/generators
402# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
403# }
404# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
405# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
406# // Walls
407# edit_type |= IGUIConstants.TILE_EDIT_WALL;
408# }
409# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
410# getAttributeValue("connected", defarch) != 0) {
411# // Connected Objects
412# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
413# }
414# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
415# arch_type == 66 || arch_type == 41 || arch_type == 95) {
416# // Exit: teleporter/exit/trapdoors
417# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
418# }
419# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
420# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
421# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
422# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
423# arch_type == 124 || arch_type == 130)) {
424# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
425# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
426# }
427# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
428# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
429# arch_type == 91 || arch_type == 21 || arch_type == 24) {
430# // Door: door/special door/gates + keys
431# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
432# }
433# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
434# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
435# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
436# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
437# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
438# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
439# arch_type == 122 || arch_type == 3)) {
440# // Equipment: weapons/armour/wands/rods
441# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
442# }
443#
444# return(edit_type);
445#
446#
447}
448
449sub init($) {
450 my ($cachedir) = @_;
451
452 return if %ARCH;
453
454 mkdir $cachedir, 0777;
455 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
456}
457
458$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
459
460init $VARDIR;
233 461
234=head1 AUTHOR 462=head1 AUTHOR
235 463
236 Marc Lehmann <schmorp@schmorp.de> 464 Marc Lehmann <schmorp@schmorp.de>
237 http://home.schmorp.de/ 465 http://home.schmorp.de/
238 466
239 Robin Redeker <elmex@ta-sa.org> 467 Robin Redeker <elmex@ta-sa.org>
240 http://www.ta-sa.org/ 468 http://www.ta-sa.org/
241 469
242=cut 470=cut
2431; 471
4721

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines