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.6 by elmex, Sun Feb 5 00:28:09 2006 UTC vs.
Revision 1.16 by root, Tue Feb 21 21:37:01 2006 UTC

5=cut 5=cut
6 6
7package Crossfire; 7package Crossfire;
8 8
9our $VERSION = '0.1'; 9our $VERSION = '0.1';
10
11use strict;
12
10use base 'Exporter'; 13use base 'Exporter';
11our @EXPORT = qw(read_pak read_arch arch2map);
12 14
13use strict; 15use Carp ();
14
15use Storable; 16use Storable;
17use List::Util qw(min max);
18
19#XXX: The map_* procedures scream for a map-object
20
21our @EXPORT =
22 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
16 23
17our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR}
18 or die "\$CROSSFIRE_LIBDIR must be set\n"; 25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
19 26
20sub T (){ 32 } 27sub TILESIZE (){ 32 }
21 28
29our $CACHEDIR;
22our $ARCH; 30our %ARCH;
31our %FACE;
23our $TILE; 32our $TILE;
33
34our %FIELD_MULTILINE = (
35 msg => "endmsg",
36 lore => "endlore",
37);
38
39# not used yet, maybe alphabetical is ok
40our @FIELD_ORDER = (qw(name name_pl));
41
42sub MOVE_WALK (){ 0x1 }
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 = (
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}
24 111
25sub read_pak($;$) { 112sub read_pak($;$) {
26 my ($path, $cache) = @_; 113 my ($path, $cache) = @_;
27 114
28 eval { 115 eval {
31 && Storable::retrieve $cache 118 && Storable::retrieve $cache
32 } or do { 119 } or do {
33 my %pak; 120 my %pak;
34 121
35 open my $fh, "<:raw", $path 122 open my $fh, "<:raw", $path
36 or die "$_[0]: $!"; 123 or Carp::croak "$_[0]: $!";
37 while (<$fh>) { 124 while (<$fh>) {
38 my ($type, $id, $len, $path) = split; 125 my ($type, $id, $len, $path) = split;
39 $path =~ s/.*\///; 126 $path =~ s/.*\///;
40 read $fh, $pak{$path}, $len; 127 read $fh, $pak{$path}, $len;
41 } 128 }
57 } or do { 144 } or do {
58 my %arc; 145 my %arc;
59 my ($more, $prev); 146 my ($more, $prev);
60 147
61 open my $fh, "<:raw", $path 148 open my $fh, "<:raw", $path
62 or die "$path: $!"; 149 or Carp::croak "$path: $!";
63 150
64 my $parse_block; $parse_block = sub { 151 my $parse_block; $parse_block = sub {
65 my %arc = @_; 152 my %arc = @_;
66 153
67 while (<$fh>) { 154 while (<$fh>) {
68 s/\s+$//; 155 s/\s+$//;
69 if (/^end$/i) { 156 if (/^end$/i) {
70 last; 157 last;
71 } elsif (/^arch (\S+)$/) { 158 } elsif (/^arch (\S+)$/) {
72 push @{ $arc{inventory} }, $parse_block->(_name => $1); 159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
73 } elsif (/^lore$/) { 160 } elsif (/^lore$/) {
74 while (<$fh>) { 161 while (<$fh>) {
75 last if /^endlore\s*$/i; 162 last if /^endlore\s*$/i;
76 $arc{lore} .= $_; 163 $arc{lore} .= $_;
77 } 164 }
106 $arc{$name} = $arc; 193 $arc{$name} = $arc;
107 } 194 }
108 $prev = $arc; 195 $prev = $arc;
109 $more = undef; 196 $more = undef;
110 } elsif (/^arch (\S+)$/i) { 197 } elsif (/^arch (\S+)$/i) {
111 push @{ $arc{arch} }, $parse_block->(_name => $1); 198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
112 } elsif (/^\s*($|#)/) { 199 } elsif (/^\s*($|#)/) {
113 # 200 #
114 } else { 201 } else {
115 warn "$path: unparseable top-level line '$_'"; 202 warn "$path: unparseable top-level line '$_'";
116 } 203 }
123 210
124 \%arc 211 \%arc
125 } 212 }
126} 213}
127 214
128sub arch2map($;$) { 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
218sub 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)
232sub arch_extents {
129 my ($mapa) = @_; 233 my ($a) = @_;
130 234
131 my %meta; 235 my $o = $ARCH{$a->{_name}}
236 or return;
132 237
133 my ($mapx, $mapy); 238 my $face = $FACE{$a->{face} || $o->{face}}
239 or (warn "no face data found for arch '$a->{_name}'"), return;
134 240
135 my $map; 241 if ($face->{w} > 1 || $face->{h} > 1) {
242 # bigface
243 return (0, 0, $face->{w} - 1, $face->{h} - 1);
136 244
137 for (@{ $mapa->{arch} }) { 245 } elsif ($o->{more}) {
138 my ($x, $y) = ($_->{x}, $_->{y}); 246 # linked face
247 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
139 248
140 if ($_->{_name} eq "map") { 249 for (; $o; $o = $o->{more}) {
141 $meta{info} = $_; 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 }
142 255
143 $mapx = $_->{width} || $x; 256 return ($minx, $miny, $maxx, $maxy);
144 $mapy = $_->{height} || $y; 257
145 } else { 258 } else {
146 push @{ $map->[$x][$y] }, $_; 259 # single face
147 260 return (0, 0, 0, 0);
148 # arch map is unreliable w.r.t. width and height
149 $mapx = $x + 1 if $mapx <= $x;
150 $mapy = $y + 1 if $mapy <= $y;
151 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
152 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
153 }
154 } 261 }
155
156 $meta{width} = $mapx;
157 $meta{height} = $mapy;
158
159 \%meta
160} 262}
161 263
162sub init($) { 264sub init($) {
163 my ($cachedir) = @_; 265 my ($cachedir) = @_;
164 266
267 return if %ARCH;
268
165 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 269 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
166 $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst";
167} 270}
271
272sub 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
335init $CACHEDIR;
168 336
169=head1 AUTHOR 337=head1 AUTHOR
170 338
171 Marc Lehmann <schmorp@schmorp.de> 339 Marc Lehmann <schmorp@schmorp.de>
172 http://home.schmorp.de/ 340 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines