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.19 by root, Wed Feb 22 23:02:59 2006 UTC

12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp (); 15use Carp ();
16use Storable; 16use Storable;
17use List::Util qw(min max);
17 18
18#XXX: The map_* procedures scream for a map-object 19#XXX: The map_* procedures scream for a map-object
19 20
20our @EXPORT = 21our @EXPORT =
21 qw(read_pak read_arch $ARCH TILESIZE editor_archs 22 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 23
26our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR}
27 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; 25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
28 26
29sub TILESIZE (){ 32 } 27sub TILESIZE (){ 32 }
30 28
29our $CACHEDIR;
31our $ARCH; 30our %ARCH;
31our %FACE;
32our $TILE;
32 33
33our %FIELD_MULTILINE = ( 34our %FIELD_MULTILINE = (
34 msg => "endmsg", 35 msg => "endmsg",
36 lore => "endlore",
35); 37);
36 38
37# not used yet, maybe alphabetical is ok 39# not used yet, maybe alphabetical is ok
38our @FIELD_ORDER = (qw(name name_pl)); 40our @FIELD_ORDER = (qw(name name_pl));
39 41
40# not used yet, AND NOT CHECKED, should also be BY TYPE 42sub MOVE_WALK (){ 0x1 }
41our %FIELD_NORMALIZE = ( 43sub MOVE_FLY_LOW (){ 0x2 }
44sub MOVE_FLY_HIGH (){ 0x4 }
45sub MOVE_FLYING (){ 0x6 }
46sub MOVE_SWIM (){ 0x8 }
47sub MOVE_ALL (){ 0xf }
48
49sub 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 = (
42 "enter_x" => "hp", 59 "enter_x" => "hp",
43 "enter_y" => "sp", 60 "enter_y" => "sp",
44); 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}
45 111
46sub read_pak($;$) { 112sub read_pak($;$) {
47 my ($path, $cache) = @_; 113 my ($path, $cache) = @_;
48 114
49 eval { 115 eval {
88 while (<$fh>) { 154 while (<$fh>) {
89 s/\s+$//; 155 s/\s+$//;
90 if (/^end$/i) { 156 if (/^end$/i) {
91 last; 157 last;
92 } elsif (/^arch (\S+)$/) { 158 } elsif (/^arch (\S+)$/) {
93 push @{ $arc{inventory} }, $parse_block->(_name => $1); 159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
94 } elsif (/^lore$/) { 160 } elsif (/^lore$/) {
95 while (<$fh>) { 161 while (<$fh>) {
96 last if /^endlore\s*$/i; 162 last if /^endlore\s*$/i;
97 $arc{lore} .= $_; 163 $arc{lore} .= $_;
98 } 164 }
127 $arc{$name} = $arc; 193 $arc{$name} = $arc;
128 } 194 }
129 $prev = $arc; 195 $prev = $arc;
130 $more = undef; 196 $more = undef;
131 } elsif (/^arch (\S+)$/i) { 197 } elsif (/^arch (\S+)$/i) {
132 push @{ $arc{arch} }, $parse_block->(_name => $1); 198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
133 } elsif (/^\s*($|#)/) { 199 } elsif (/^\s*($|#)/) {
134 # 200 #
135 } else { 201 } else {
136 warn "$path: unparseable top-level line '$_'"; 202 warn "$path: unparseable top-level line '$_'";
137 } 203 }
144 210
145 \%arc 211 \%arc
146 } 212 }
147} 213}
148 214
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
186# put all archs into a hash with editor_face as it's key 215# put all archs into a hash with editor_face as it's key
187# NOTE: the arrays in the hash values are references to 216# NOTE: the arrays in the hash values are references to
188# the archs from $ARCH 217# the archs from $ARCH
189sub editor_archs { 218sub editor_archs {
190 my %paths; 219 my %paths;
191 220
192 for (keys %$ARCH) { 221 for (keys %ARCH) {
193 my $arch = $ARCH->{$_}; 222 my $arch = $ARCH{$_};
194 push @{$paths{$arch->{editor_folder}}}, \$arch; 223 push @{$paths{$arch->{editor_folder}}}, \$arch;
195 } 224 }
196 225
197 return \%paths; 226 \%paths
198} 227}
199 228
200# arch_extends determines how the arch looks like on the map, 229=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
201# bigfaces, linked faces and single faces are handled here 230
202# it returns (<xoffset>, <yoffset>, <width>, <height>) 231arch_extents determines the extents of the given arch's face(s), linked
203# NOTE: non rectangular linked faces are not considered 232faces and single faces are handled here it returns (minx, miny, maxx,
233maxy)
234
235=cut
236
204sub arch_extends { 237sub arch_extents {
205 my ($a) = @_; 238 my ($a) = @_;
206 239
207 my $TC = \%Crossfire::Tilecache::TILECACHE; 240 my $o = $ARCH{$a->{_name}}
208
209 my $facename =
210 $a->{face} || $ARCH->{$a->{_name}}->{face}
211 or return (); 241 or return;
212 242
213 my $tile = $TC->{$facename} 243 my $face = $FACE{$a->{face} || $o->{face}}
214 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 244 or (warn "no face data found for arch '$a->{_name}'"), return;
215 245
216 if ($tile->{w} > 1 || $tile->{h} > 1) { 246 if ($face->{w} > 1 || $face->{h} > 1) {
217 # bigfaces 247 # bigface
218 return (0, 0, $tile->{w}, $tile->{h}); 248 return (0, 0, $face->{w} - 1, $face->{h} - 1);
219 249
220 } elsif ($a->{more}) { 250 } elsif ($o->{more}) {
221 # linked faces 251 # linked face
222 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 252 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 253
230 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) 254 for (; $o; $o = $o->{more}) {
255 $minx = min $minx, $o->{x};
256 $miny = min $miny, $o->{y};
257 $maxx = max $maxx, $o->{x};
258 $maxy = max $maxy, $o->{y};
259 }
260
261 return ($minx, $miny, $maxx, $maxy);
231 262
232 } else { 263 } else {
233 # single face 264 # single face
234 return (0, 0, 1, 1); 265 return (0, 0, 0, 0);
235 } 266 }
236} 267}
237 268
238sub init($) { 269sub init($) {
239 my ($cachedir) = @_; 270 my ($cachedir) = @_;
240 271
272 return if %ARCH;
273
241 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 274 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
242} 275}
276
277=item $type = arch_attr $arch
278
279Returns a hashref describing the object and its attributes. It can contain
280the following keys:
281
282 name the name, suitable for display purposes
283 ignore
284 attr
285 desc
286 use
287 section => [name => \%attr, name => \%attr]
288 import
289
290=cut
291
292sub arch_attr($) {
293 my ($arch) = @_;
294
295 require Crossfire::Data;
296
297 my $attr;
298
299 if ($arch->{type} > 0) {
300 $attr = $Crossfire::Data::ATTR{$arch->{type}+0};
301 } else {
302 $attr = $Crossfire::Data::TYPE{Misc};
303
304 type:
305 for (@Crossfire::Data::ATTR0) {
306 my $req = $_->{required}
307 or die "internal error: ATTR0 without 'required'";
308
309 while (my ($k, $v) = each %$req) {
310 next type
311 unless $arch->{$k} == $v;
312 }
313
314 $attr = $_;
315 }
316 }
317
318 use PApp::Util;
319 warn PApp::Util::dumpval $attr;
320
321 $attr || \%Crossfire::Data::DEFAULT_ATTR;
322}
323
324sub arch_edit_sections {
325# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
326# edit_type = 0;
327# else if (edit_type != 0) {
328# // all flags from 'check_type' must be unset in this arch because they get recalculated now
329# edit_type &= ~check_type;
330# }
331#
332# }
333# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
334# getAttributeValue("alive", defarch) == 1 &&
335# (getAttributeValue("monster", defarch) == 1 ||
336# getAttributeValue("generator", defarch) == 1)) {
337# // Monster: monsters/npcs/generators
338# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
339# }
340# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
341# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
342# // Walls
343# edit_type |= IGUIConstants.TILE_EDIT_WALL;
344# }
345# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
346# getAttributeValue("connected", defarch) != 0) {
347# // Connected Objects
348# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
349# }
350# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
351# arch_type == 66 || arch_type == 41 || arch_type == 95) {
352# // Exit: teleporter/exit/trapdoors
353# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
354# }
355# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
356# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
357# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
358# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
359# arch_type == 124 || arch_type == 130)) {
360# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
361# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
362# }
363# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
364# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
365# arch_type == 91 || arch_type == 21 || arch_type == 24) {
366# // Door: door/special door/gates + keys
367# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
368# }
369# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
370# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
371# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
372# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
373# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
374# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
375# arch_type == 122 || arch_type == 3)) {
376# // Equipment: weapons/armour/wands/rods
377# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
378# }
379#
380# return(edit_type);
381#
382#
383}
384
385$CACHEDIR ||= "$ENV{HOME}/.crossfire";
386
387init $CACHEDIR;
243 388
244=head1 AUTHOR 389=head1 AUTHOR
245 390
246 Marc Lehmann <schmorp@schmorp.de> 391 Marc Lehmann <schmorp@schmorp.de>
247 http://home.schmorp.de/ 392 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines