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.2 by elmex, Sat Feb 4 23:54:05 2006 UTC vs.
Revision 1.45 by root, Thu Mar 16 01:13:02 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) = @_; 161 my ($path, $cache) = @_;
52 162
163 eval {
164 defined $cache
165 && -M $cache < -M $path
166 && load_ref $cache
167 } or do {
53 my %arc; 168 my %arc;
54 my ($more, $prev); 169 my ($more, $prev);
55 170
56 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
57 or die "$path: $!"; 172 or Carp::croak "$path: $!";
58 173
174 binmode $fh;
175
59 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
60 my %arc = @_; 177 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 };
61 206
62 while (<$fh>) { 207 while (<$fh>) {
63 s/\s+$//; 208 s/\s+$//;
64 if (/^end$/i) { 209 if (/^more$/i) {
65 last; 210 $more = $prev;
66 } elsif (/^arch (\S+)$/) { 211 } elsif (/^object (\S+)$/i) {
212 my $name = $1;
67 push @{ $arc{inventory} }, $parse_block->(_name => $1); 213 my $arc = $parse_block->(_name => $name);
68 } elsif (/^lore$/) { 214
69 while (<$fh>) { 215 if ($more) {
70 last if /^endlore\s*$/i; 216 $more->{more} = $arc;
217 } else {
71 $arc{lore} .= $_; 218 $arc{$name} = $arc;
72 } 219 }
73 } elsif (/^msg$/) { 220 $prev = $arc;
74 while (<$fh>) { 221 $more = undef;
75 last if /^endmsg\s*$/i;
76 $arc{msg} .= $_;
77 }
78 } elsif (/^(\S+)\s*(.*)$/) { 222 } elsif (/^arch (\S+)$/i) {
79 $arc{lc $1} = $2; 223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
80 } elsif (/^\s*($|#)/) { 224 } elsif (/^\s*($|#)/) {
81 # 225 #
82 } else { 226 } else {
83 warn "$path: unparsable line '$_' in arch $arc{_name}"; 227 warn "$path: unparseable top-level line '$_'";
84 } 228 }
85 } 229 }
86 230
231 undef $parse_block; # work around bug in perl not freeing $fh etc.
232
233 save_ref \%arc, $cache
234 if defined $cache;
235
87 \%arc 236 \%arc
88 }; 237 }
238}
89 239
90 while (<$fh>) { 240# put all archs into a hash with editor_face as it's key
91 s/\s+$//; 241# NOTE: the arrays in the hash values are references to
92 if (/^more$/i) { 242# the archs from $ARCH
93 $more = $prev; 243sub editor_archs {
94 } elsif (/^object (\S+)$/i) { 244 my %paths;
95 my $name = $1;
96 my $arc = $parse_block->(_name => $name);
97 245
98 if ($more) { 246 for (keys %ARCH) {
99 $more->{more} = $arc; 247 my $arch = $ARCH{$_};
248 push @{$paths{$arch->{editor_folder}}}, \$arch;
249 }
250
251 \%paths
252}
253
254=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
255
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)
259
260=cut
261
262sub arch_extents {
263 my ($a) = @_;
264
265 my $o = $ARCH{$a->{_name}}
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
100 } else { 288 } else {
101 $arc{$name} = $arc; 289 # single face
290 return (0, 0, 0, 0);
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;
102 } 330 }
331
103 $prev = $arc; 332 $root = $_;
104 $more = undef;
105 } elsif (/^arch (\S+)$/i) {
106 push @{ $arc{arch} }, $parse_block->(_name => $1);
107 } elsif (/^\s*($|#)/) {
108 #
109 } else {
110 warn "$path: unparseable top-level line '$_'";
111 } 333 }
112 } 334 }
113 335
114 undef $parse_block; # work around bug in perl not freeing $fh etc. 336 my $attr = { };
115 337
116 \%arc 338 my @import = (\%Crossfire::Data::DEFAULT_ATTR, $root);
117} 339 my (%ignore);
340 my (@section_order, %section, @attr_order);
118 341
119sub cfmap_meta($;$) { 342 while (my $type = shift @import) {
120 my ($self, $mapa, $mapname) = @_; 343 push @import, @{$type->{import} || []};
121 344
122 my $arch = $self->{arch}; 345 $attr->{$_} ||= $type->{$_}
346 for qw(name desc use);
123 347
124 my %meta; 348 for (@{$type->{ignore} || []}) {
125 349 $ignore{$_}++ for ref $_ ? @$_ : $_;
126 my ($mapx, $mapy);
127
128 my $map;
129
130 for (@{ $mapa->{arch} }) {
131 my ($x, $y) = ($_->{x}, $_->{y});
132
133 if ($_->{_name} eq "map") {
134 $meta{info} = $_;
135
136 $mapx = $_->{width} || $x;
137 $mapy = $_->{height} || $y;
138 } else {
139 push @{ $map->[$x][$y] }, $_;
140
141 # arch map is unreliable w.r.t. width and height
142 $mapx = $x + 1 if $mapx <= $x;
143 $mapy = $y + 1 if $mapy <= $y;
144 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
145 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
146 } 350 }
147 }
148 351
149 $meta{width} = $mapx; 352 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
150 $meta{height} = $mapy; 353 my ($name, $attr) = @$_;
151 354 push @section_order, $name;
152 my %draw_info; 355 for (@$attr) {
153 my %map_info; 356 my ($k, $v) = @$_;
154 357 push @attr_order, $k;
155 # first pass, gather face stacking order, border and corner info 358 $section{$name}{$k} ||= $v;
156 for my $x (0 .. $mapx - 1) {
157 my $col = $map->[$x];
158 for my $y (0 .. $mapy - 1) {
159 my $as = $col->[$y] || [];
160
161 for my $layer (0 .. $#$as) {
162 my $a = $as->[$layer];
163
164 my $o = $arch->{$a->{_name}}
165 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
166
167 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
168 my $level = $layer * 256;
169
170 $level -= 100 * 256 if $o->{_name} eq "blocked";
171
172 while ($o) {
173 my $face = $a->{face} || $o->{face};
174
175 my $mx = $x + $o->{x};
176 my $my = $y + $o->{y};
177
178 last if 0 > $mx || $mx >= $mapx
179 || 0 > $my || $my >= $mapy;
180
181 push @{ $map_info{$level}{$mx, $my} }, $a;
182
183 $o = $o->{more};
184 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
185 }
186 } 359 }
187 } 360 }
188 } 361 }
189 362
190 # third pass, gather meta info 363 $attr->{section} = [
191 for my $level (sort { $a <=> $b } keys %map_info) { 364 map !exists $section{$_} ? () : do {
192 my $info = $map_info{$level}; 365 my $attr = delete $section{$_};
193 366
194 while (my ($xy, $as) = each %$info) { 367 [
195 my ($x, $y) = split $;, $xy; 368 $_,
196 369 map exists $attr->{$_} && !$ignore{$_}
197 next if $x < 0 || $x >= $mapx 370 ? [$_ => delete $attr->{$_}] : (),
198 || $y < 0 || $y >= $mapy; 371 @attr_order
199 372 ]
200 push @{ $meta{map}[$x][$y] }, $_ for @$as;
201 } 373 },
202 } 374
375 exists $section{$_} ? [$_ => delete $section{$_}] : (),
376 @section_order
377 ];
203 378
204 \%meta 379 $attr
205} 380}
206 381
207sub new { 382sub arch_edit_sections {
208 my $class = shift; 383# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
209 my $self = bless { }, $class; 384# edit_type = 0;
210 $self->{arch} = read_arch "$LIB/archetypes"; 385# else if (edit_type != 0) {
211 $self->{tile} = read_pak "$LIB/crossfire.0"; 386# // all flags from 'check_type' must be unset in this arch because they get recalculated now
212 $self 387# edit_type &= ~check_type;
388# }
389#
390# }
391# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
392# getAttributeValue("alive", defarch) == 1 &&
393# (getAttributeValue("monster", defarch) == 1 ||
394# getAttributeValue("generator", defarch) == 1)) {
395# // Monster: monsters/npcs/generators
396# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
397# }
398# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
399# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
400# // Walls
401# edit_type |= IGUIConstants.TILE_EDIT_WALL;
402# }
403# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
404# getAttributeValue("connected", defarch) != 0) {
405# // Connected Objects
406# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
407# }
408# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
409# arch_type == 66 || arch_type == 41 || arch_type == 95) {
410# // Exit: teleporter/exit/trapdoors
411# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
412# }
413# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
414# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
415# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
416# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
417# arch_type == 124 || arch_type == 130)) {
418# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
419# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
420# }
421# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
422# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
423# arch_type == 91 || arch_type == 21 || arch_type == 24) {
424# // Door: door/special door/gates + keys
425# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
426# }
427# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
428# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
429# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
430# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
431# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
432# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
433# arch_type == 122 || arch_type == 3)) {
434# // Equipment: weapons/armour/wands/rods
435# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
436# }
437#
438# return(edit_type);
439#
440#
213} 441}
214 442
215sub read { 443sub init($) {
216 my ($self, $file) = @_; 444 my ($cachedir) = @_;
217 my $mapa = read_arch $file; 445
218 my $map = $self->cfmap_meta ($mapa, $file); 446 return if %ARCH;
219 print "READ: ".join(',', %{$map->{info}})."\n"; 447
220 return $map; 448 mkdir $cachedir, 0777;
449 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
221} 450}
451
452$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
453
454init $VARDIR;
222 455
223=head1 AUTHOR 456=head1 AUTHOR
224 457
225 Marc Lehmann <schmorp@schmorp.de> 458 Marc Lehmann <schmorp@schmorp.de>
226 http://home.schmorp.de/ 459 http://home.schmorp.de/
227 460
228 Robin Redeker <elmex@ta-sa.org> 461 Robin Redeker <elmex@ta-sa.org>
229 http://www.ta-sa.org/ 462 http://www.ta-sa.org/
230 463
231=cut 464=cut
2321; 465
4661

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines