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.15 by root, Sun Feb 12 04:50:24 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# arch_extents determines the extents of a given arch
201# bigfaces, linked faces and single faces are handled here 230# bigfaces, linked faces and single faces are handled here
202# it returns (<xoffset>, <yoffset>, <width>, <height>) 231# it returns (minx, miny, maxx, maxy)
203# NOTE: non rectangular linked faces are not considered
204sub arch_extends { 232sub arch_extents {
205 my ($a) = @_; 233 my ($a) = @_;
206 234
207 my $TC = \%Crossfire::Tilecache::TILECACHE; 235 my $o = $ARCH{$a->{_name}}
208
209 my $facename =
210 $a->{face} || $ARCH->{$a->{_name}}->{face}
211 or return (); 236 or return;
212 237
213 my $tile = $TC->{$facename} 238 my $face = $FACE{$a->{face} || $o->{face}}
214 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 239 or (warn "no face data found for arch '$a->{_name}'"), return;
215 240
216 if ($tile->{w} > 1 || $tile->{h} > 1) { 241 if ($face->{w} > 1 || $face->{h} > 1) {
217 # bigfaces 242 # bigface
218 return (0, 0, $tile->{w}, $tile->{h}); 243 return (0, 0, $face->{w} - 1, $face->{h} - 1);
219 244
220 } elsif ($a->{more}) { 245 } elsif ($o->{more}) {
221 # linked faces 246 # linked face
222 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 247 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 248
230 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) 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);
231 257
232 } else { 258 } else {
233 # single face 259 # single face
234 return (0, 0, 1, 1); 260 return (0, 0, 0, 0);
235 } 261 }
236} 262}
237 263
238sub init($) { 264sub init($) {
239 my ($cachedir) = @_; 265 my ($cachedir) = @_;
240 266
267 return if %ARCH;
268
241 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 269 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
242} 270}
271
272$CACHEDIR ||= "$ENV{HOME}/.crossfire";
273
274init $CACHEDIR;
243 275
244=head1 AUTHOR 276=head1 AUTHOR
245 277
246 Marc Lehmann <schmorp@schmorp.de> 278 Marc Lehmann <schmorp@schmorp.de>
247 http://home.schmorp.de/ 279 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines