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.12 by root, Thu Feb 9 19:59:29 2006 UTC vs.
Revision 1.15 by root, Sun Feb 12 04:50:24 2006 UTC

10 10
11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp ();
15use Storable; 16use Storable;
17use List::Util qw(min max);
16 18
17#XXX: The map_* procedures scream for a map-object 19#XXX: The map_* procedures scream for a map-object
18 20
19our @EXPORT = 21our @EXPORT =
20 qw(read_pak read_arch $ARCH TILESIZE editor_archs 22 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
21 arch_extends
22 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
23 );
24 23
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR}
26 or die "\$CROSSFIRE_LIBDIR must be set\n"; 25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
27 26
28sub TILESIZE (){ 32 } 27sub TILESIZE (){ 32 }
29 28
29our $CACHEDIR;
30our $ARCH; 30our %ARCH;
31our %FACE;
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}
31 111
32sub read_pak($;$) { 112sub read_pak($;$) {
33 my ($path, $cache) = @_; 113 my ($path, $cache) = @_;
34 114
35 eval { 115 eval {
38 && Storable::retrieve $cache 118 && Storable::retrieve $cache
39 } or do { 119 } or do {
40 my %pak; 120 my %pak;
41 121
42 open my $fh, "<:raw", $path 122 open my $fh, "<:raw", $path
43 or die "$_[0]: $!"; 123 or Carp::croak "$_[0]: $!";
44 while (<$fh>) { 124 while (<$fh>) {
45 my ($type, $id, $len, $path) = split; 125 my ($type, $id, $len, $path) = split;
46 $path =~ s/.*\///; 126 $path =~ s/.*\///;
47 read $fh, $pak{$path}, $len; 127 read $fh, $pak{$path}, $len;
48 } 128 }
64 } or do { 144 } or do {
65 my %arc; 145 my %arc;
66 my ($more, $prev); 146 my ($more, $prev);
67 147
68 open my $fh, "<:raw", $path 148 open my $fh, "<:raw", $path
69 or die "$path: $!"; 149 or Carp::croak "$path: $!";
70 150
71 my $parse_block; $parse_block = sub { 151 my $parse_block; $parse_block = sub {
72 my %arc = @_; 152 my %arc = @_;
73 153
74 while (<$fh>) { 154 while (<$fh>) {
75 s/\s+$//; 155 s/\s+$//;
76 if (/^end$/i) { 156 if (/^end$/i) {
77 last; 157 last;
78 } elsif (/^arch (\S+)$/) { 158 } elsif (/^arch (\S+)$/) {
79 push @{ $arc{inventory} }, $parse_block->(_name => $1); 159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
80 } elsif (/^lore$/) { 160 } elsif (/^lore$/) {
81 while (<$fh>) { 161 while (<$fh>) {
82 last if /^endlore\s*$/i; 162 last if /^endlore\s*$/i;
83 $arc{lore} .= $_; 163 $arc{lore} .= $_;
84 } 164 }
113 $arc{$name} = $arc; 193 $arc{$name} = $arc;
114 } 194 }
115 $prev = $arc; 195 $prev = $arc;
116 $more = undef; 196 $more = undef;
117 } elsif (/^arch (\S+)$/i) { 197 } elsif (/^arch (\S+)$/i) {
118 push @{ $arc{arch} }, $parse_block->(_name => $1); 198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
119 } elsif (/^\s*($|#)/) { 199 } elsif (/^\s*($|#)/) {
120 # 200 #
121 } else { 201 } else {
122 warn "$path: unparseable top-level line '$_'"; 202 warn "$path: unparseable top-level line '$_'";
123 } 203 }
130 210
131 \%arc 211 \%arc
132 } 212 }
133} 213}
134 214
135# returns the arch/object stack from a tile on a map
136sub map_get_tile_stack {
137 my ($map, $x, $y) = @_;
138 my $as;
139
140 if ($x > 0 || $x < $map->{width}
141 || $y > 0 || $y < $map->{height}) {
142
143 $as = $map->{map}{map}[$x][$y] || [];
144 }
145
146 return $as;
147}
148
149# pop the topmost arch/object from the stack of a tile on a map
150sub map_pop_tile_stack {
151 my ($map, $x, $y) = @_;
152
153 if ($x > 0 || $x < $map->{width}
154 || $y > 0 || $y < $map->{height}) {
155
156 pop @{$map->{map}{map}[$x][$y]};
157 }
158}
159
160# pushes the arch/object on the stack of a tile on a map
161sub map_push_tile_stack {
162 my ($map, $x, $y, $arch) = @_;
163
164 if ($x > 0 || $x < $map->{width}
165 || $y > 0 || $y < $map->{height}) {
166
167 push @{$map->{map}{map}[$x][$y]}, $arch;
168 }
169}
170
171
172# 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
173# NOTE: the arrays in the hash values are references to 216# NOTE: the arrays in the hash values are references to
174# the archs from $ARCH 217# the archs from $ARCH
175sub editor_archs { 218sub editor_archs {
176 my %paths; 219 my %paths;
177 220
178 for (keys %$ARCH) { 221 for (keys %ARCH) {
179 my $arch = $ARCH->{$_}; 222 my $arch = $ARCH{$_};
180 push @{$paths{$arch->{editor_folder}}}, \$arch; 223 push @{$paths{$arch->{editor_folder}}}, \$arch;
181 } 224 }
182 225
183 return \%paths; 226 \%paths
184} 227}
185 228
186# arch_extends determines how the arch looks like on the map, 229# arch_extents determines the extents of a given arch
187# bigfaces, linked faces and single faces are handled here 230# bigfaces, linked faces and single faces are handled here
188# it returns (<xoffset>, <yoffset>, <width>, <height>) 231# it returns (minx, miny, maxx, maxy)
189# NOTE: non rectangular linked faces are not considered
190sub arch_extends { 232sub arch_extents {
191 my ($a) = @_; 233 my ($a) = @_;
192 234
193 my $TC = \%Crossfire::Tilecache::TILECACHE; 235 my $o = $ARCH{$a->{_name}}
194
195 my $facename =
196 $a->{face} || $ARCH->{$a->{_name}}->{face}
197 or return (); 236 or return;
198 237
199 my $tile = $TC->{$facename} 238 my $face = $FACE{$a->{face} || $o->{face}}
200 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 239 or (warn "no face data found for arch '$a->{_name}'"), return;
201 240
202 if ($tile->{w} > 1 || $tile->{h} > 1) { 241 if ($face->{w} > 1 || $face->{h} > 1) {
203 # bigfaces 242 # bigface
204 return (0, 0, $tile->{w}, $tile->{h}); 243 return (0, 0, $face->{w} - 1, $face->{h} - 1);
205 244
206 } elsif ($a->{more}) { 245 } elsif ($o->{more}) {
207 # linked faces 246 # linked face
208 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 247 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
209 do {
210 $miw > (0 + $a->{x}) and $miw = $a->{x};
211 $mih > (0 + $a->{y}) and $mih = $a->{y};
212 $maw < (0 + $a->{x}) and $maw = $a->{x};
213 $mah < (0 + $a->{y}) and $mah = $a->{y};
214 } while $a = $a->{more};
215 248
216 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);
217 257
218 } else { 258 } else {
219 # single face 259 # single face
220 return (0, 0, 1, 1); 260 return (0, 0, 0, 0);
221 } 261 }
222} 262}
223 263
224sub init($) { 264sub init($) {
225 my ($cachedir) = @_; 265 my ($cachedir) = @_;
226 266
267 return if %ARCH;
268
227 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 269 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
228} 270}
271
272$CACHEDIR ||= "$ENV{HOME}/.crossfire";
273
274init $CACHEDIR;
229 275
230=head1 AUTHOR 276=head1 AUTHOR
231 277
232 Marc Lehmann <schmorp@schmorp.de> 278 Marc Lehmann <schmorp@schmorp.de>
233 http://home.schmorp.de/ 279 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines