ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.16
Committed: Tue Feb 21 21:37:01 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.15: +61 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Crossfire - Crossfire maphandling
4
5 =cut
6
7 package Crossfire;
8
9 our $VERSION = '0.1';
10
11 use strict;
12
13 use base 'Exporter';
14
15 use Carp ();
16 use Storable;
17 use List::Util qw(min max);
18
19 #XXX: The map_* procedures scream for a map-object
20
21 our @EXPORT =
22 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
23
24 our $LIB = $ENV{CROSSFIRE_LIBDIR}
25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
26
27 sub TILESIZE (){ 32 }
28
29 our $CACHEDIR;
30 our %ARCH;
31 our %FACE;
32 our $TILE;
33
34 our %FIELD_MULTILINE = (
35 msg => "endmsg",
36 lore => "endlore",
37 );
38
39 # not used yet, maybe alphabetical is ok
40 our @FIELD_ORDER = (qw(name name_pl));
41
42 sub MOVE_WALK (){ 0x1 }
43 sub MOVE_FLY_LOW (){ 0x2 }
44 sub MOVE_FLY_HIGH (){ 0x4 }
45 sub MOVE_FLYING (){ 0x6 }
46 sub MOVE_SWIM (){ 0x8 }
47 sub MOVE_ALL (){ 0xf }
48
49 sub normalize_arch($) {
50 my ($ob) = @_;
51
52 my $arch = $ARCH{$ob->{_name}}
53 or (warn "$ob->{_name}: no such archetype", return $ob);
54
55 delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply);
56
57 if ($arch->{type} == 22) { # map
58 my %normalize = (
59 "enter_x" => "hp",
60 "enter_y" => "sp",
61 "width" => "x",
62 "height" => "y",
63 "reset_timeout" => "weight",
64 "swap_time" => "value",
65 "difficulty" => "level",
66 "darkness" => "invisible",
67 "fixed_resettime" => "stand_still",
68 );
69
70 while (my ($k2, $k1) = each %normalize) {
71 if (defined (my $v = delete $ob->{$k1})) {
72 $ob->{$k2} = $v;
73 }
74 }
75 }
76
77 if (defined (my $v = delete $ob->{no_pass})) {
78 $ob->{move_block} = $v ? MOVE_ALL : 0;
79 }
80 if (defined (my $v = delete $ob->{walk_on})) {
81 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
82 : $ob->{move_on} & ~MOVE_WALK;
83 }
84 if (defined (my $v = delete $ob->{walk_off})) {
85 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
86 : $ob->{move_off} & ~MOVE_WALK;
87 }
88 if (defined (my $v = delete $ob->{fly_on})) {
89 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
90 : $ob->{move_on} & ~MOVE_FLY_LOW;
91 }
92 if (defined (my $v = delete $ob->{fly_off})) {
93 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
94 : $ob->{move_off} & ~MOVE_FLY_LOW;
95 }
96 if (defined (my $v = delete $ob->{flying})) {
97 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
98 : $ob->{move_type} & ~MOVE_FLY_LOW;
99 }
100
101 # if value matches archetype default, delete
102 while (my ($k, $v) = each %$ob) {
103 if (exists $arch->{$k} and $arch->{$k} eq $v) {
104 next if $k eq "_name";
105 delete $ob->{$k};
106 }
107 }
108
109 $ob
110 }
111
112 sub read_pak($;$) {
113 my ($path, $cache) = @_;
114
115 eval {
116 defined $cache
117 && -M $cache < -M $path
118 && Storable::retrieve $cache
119 } or do {
120 my %pak;
121
122 open my $fh, "<:raw", $path
123 or Carp::croak "$_[0]: $!";
124 while (<$fh>) {
125 my ($type, $id, $len, $path) = split;
126 $path =~ s/.*\///;
127 read $fh, $pak{$path}, $len;
128 }
129
130 Storable::nstore \%pak, $cache
131 if defined $cache;
132
133 \%pak
134 }
135 }
136
137 sub read_arch($;$) {
138 my ($path, $cache) = @_;
139
140 eval {
141 defined $cache
142 && -M $cache < -M $path
143 && Storable::retrieve $cache
144 } or do {
145 my %arc;
146 my ($more, $prev);
147
148 open my $fh, "<:raw", $path
149 or Carp::croak "$path: $!";
150
151 my $parse_block; $parse_block = sub {
152 my %arc = @_;
153
154 while (<$fh>) {
155 s/\s+$//;
156 if (/^end$/i) {
157 last;
158 } elsif (/^arch (\S+)$/) {
159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
160 } elsif (/^lore$/) {
161 while (<$fh>) {
162 last if /^endlore\s*$/i;
163 $arc{lore} .= $_;
164 }
165 } elsif (/^msg$/) {
166 while (<$fh>) {
167 last if /^endmsg\s*$/i;
168 $arc{msg} .= $_;
169 }
170 } elsif (/^(\S+)\s*(.*)$/) {
171 $arc{lc $1} = $2;
172 } elsif (/^\s*($|#)/) {
173 #
174 } else {
175 warn "$path: unparsable line '$_' in arch $arc{_name}";
176 }
177 }
178
179 \%arc
180 };
181
182 while (<$fh>) {
183 s/\s+$//;
184 if (/^more$/i) {
185 $more = $prev;
186 } elsif (/^object (\S+)$/i) {
187 my $name = $1;
188 my $arc = $parse_block->(_name => $name);
189
190 if ($more) {
191 $more->{more} = $arc;
192 } else {
193 $arc{$name} = $arc;
194 }
195 $prev = $arc;
196 $more = undef;
197 } elsif (/^arch (\S+)$/i) {
198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
199 } elsif (/^\s*($|#)/) {
200 #
201 } else {
202 warn "$path: unparseable top-level line '$_'";
203 }
204 }
205
206 undef $parse_block; # work around bug in perl not freeing $fh etc.
207
208 Storable::nstore \%arc, $cache
209 if defined $cache;
210
211 \%arc
212 }
213 }
214
215 # put all archs into a hash with editor_face as it's key
216 # NOTE: the arrays in the hash values are references to
217 # the archs from $ARCH
218 sub editor_archs {
219 my %paths;
220
221 for (keys %ARCH) {
222 my $arch = $ARCH{$_};
223 push @{$paths{$arch->{editor_folder}}}, \$arch;
224 }
225
226 \%paths
227 }
228
229 # arch_extents determines the extents of a given arch
230 # bigfaces, linked faces and single faces are handled here
231 # it returns (minx, miny, maxx, maxy)
232 sub arch_extents {
233 my ($a) = @_;
234
235 my $o = $ARCH{$a->{_name}}
236 or return;
237
238 my $face = $FACE{$a->{face} || $o->{face}}
239 or (warn "no face data found for arch '$a->{_name}'"), return;
240
241 if ($face->{w} > 1 || $face->{h} > 1) {
242 # bigface
243 return (0, 0, $face->{w} - 1, $face->{h} - 1);
244
245 } elsif ($o->{more}) {
246 # linked face
247 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
248
249 for (; $o; $o = $o->{more}) {
250 $minx = min $minx, $o->{x};
251 $miny = min $miny, $o->{y};
252 $maxx = max $maxx, $o->{x};
253 $maxy = max $maxy, $o->{y};
254 }
255
256 return ($minx, $miny, $maxx, $maxy);
257
258 } else {
259 # single face
260 return (0, 0, 0, 0);
261 }
262 }
263
264 sub init($) {
265 my ($cachedir) = @_;
266
267 return if %ARCH;
268
269 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
270 }
271
272 sub arch_edit_sections {
273 # if (edit_type == IGUIConstants.TILE_EDIT_NONE)
274 # edit_type = 0;
275 # else if (edit_type != 0) {
276 # // all flags from 'check_type' must be unset in this arch because they get recalculated now
277 # edit_type &= ~check_type;
278 # }
279 #
280 # }
281 # if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
282 # getAttributeValue("alive", defarch) == 1 &&
283 # (getAttributeValue("monster", defarch) == 1 ||
284 # getAttributeValue("generator", defarch) == 1)) {
285 # // Monster: monsters/npcs/generators
286 # edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
287 # }
288 # if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
289 # arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
290 # // Walls
291 # edit_type |= IGUIConstants.TILE_EDIT_WALL;
292 # }
293 # if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
294 # getAttributeValue("connected", defarch) != 0) {
295 # // Connected Objects
296 # edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
297 # }
298 # if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
299 # arch_type == 66 || arch_type == 41 || arch_type == 95) {
300 # // Exit: teleporter/exit/trapdoors
301 # edit_type |= IGUIConstants.TILE_EDIT_EXIT;
302 # }
303 # if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
304 # getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
305 # arch_type == 5 || arch_type == 36 || arch_type == 60 ||
306 # arch_type == 85 || arch_type == 111 || arch_type == 123 ||
307 # arch_type == 124 || arch_type == 130)) {
308 # // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
309 # edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
310 # }
311 # if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
312 # arch_type == 20 || arch_type == 23 || arch_type == 26 ||
313 # arch_type == 91 || arch_type == 21 || arch_type == 24) {
314 # // Door: door/special door/gates + keys
315 # edit_type |= IGUIConstants.TILE_EDIT_DOOR;
316 # }
317 # if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
318 # getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
319 # arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
320 # arch_type == 35 || arch_type == 39 || arch_type == 70 ||
321 # arch_type == 87 || arch_type == 99 || arch_type == 100 ||
322 # arch_type == 104 || arch_type == 109 || arch_type == 113 ||
323 # arch_type == 122 || arch_type == 3)) {
324 # // Equipment: weapons/armour/wands/rods
325 # edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
326 # }
327 #
328 # return(edit_type);
329 #
330 #
331 }
332
333 $CACHEDIR ||= "$ENV{HOME}/.crossfire";
334
335 init $CACHEDIR;
336
337 =head1 AUTHOR
338
339 Marc Lehmann <schmorp@schmorp.de>
340 http://home.schmorp.de/
341
342 Robin Redeker <elmex@ta-sa.org>
343 http://www.ta-sa.org/
344
345 =cut
346
347 1