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.10 by elmex, Mon Feb 6 23:01:31 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
17our @EXPORT = qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs arch2pickmap arch_extends); 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);
18 23
19our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
21 26
22sub TILESIZE (){ 32 } 27sub TILESIZE (){ 32 }
23 28
29our $CACHEDIR;
24our $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}
25 111
26sub read_pak($;$) { 112sub read_pak($;$) {
27 my ($path, $cache) = @_; 113 my ($path, $cache) = @_;
28 114
29 eval { 115 eval {
32 && Storable::retrieve $cache 118 && Storable::retrieve $cache
33 } or do { 119 } or do {
34 my %pak; 120 my %pak;
35 121
36 open my $fh, "<:raw", $path 122 open my $fh, "<:raw", $path
37 or die "$_[0]: $!"; 123 or Carp::croak "$_[0]: $!";
38 while (<$fh>) { 124 while (<$fh>) {
39 my ($type, $id, $len, $path) = split; 125 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 126 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 127 read $fh, $pak{$path}, $len;
42 } 128 }
58 } or do { 144 } or do {
59 my %arc; 145 my %arc;
60 my ($more, $prev); 146 my ($more, $prev);
61 147
62 open my $fh, "<:raw", $path 148 open my $fh, "<:raw", $path
63 or die "$path: $!"; 149 or Carp::croak "$path: $!";
64 150
65 my $parse_block; $parse_block = sub { 151 my $parse_block; $parse_block = sub {
66 my %arc = @_; 152 my %arc = @_;
67 153
68 while (<$fh>) { 154 while (<$fh>) {
69 s/\s+$//; 155 s/\s+$//;
70 if (/^end$/i) { 156 if (/^end$/i) {
71 last; 157 last;
72 } elsif (/^arch (\S+)$/) { 158 } elsif (/^arch (\S+)$/) {
73 push @{ $arc{inventory} }, $parse_block->(_name => $1); 159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
74 } elsif (/^lore$/) { 160 } elsif (/^lore$/) {
75 while (<$fh>) { 161 while (<$fh>) {
76 last if /^endlore\s*$/i; 162 last if /^endlore\s*$/i;
77 $arc{lore} .= $_; 163 $arc{lore} .= $_;
78 } 164 }
107 $arc{$name} = $arc; 193 $arc{$name} = $arc;
108 } 194 }
109 $prev = $arc; 195 $prev = $arc;
110 $more = undef; 196 $more = undef;
111 } elsif (/^arch (\S+)$/i) { 197 } elsif (/^arch (\S+)$/i) {
112 push @{ $arc{arch} }, $parse_block->(_name => $1); 198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
113 } elsif (/^\s*($|#)/) { 199 } elsif (/^\s*($|#)/) {
114 # 200 #
115 } else { 201 } else {
116 warn "$path: unparseable top-level line '$_'"; 202 warn "$path: unparseable top-level line '$_'";
117 } 203 }
130# NOTE: the arrays in the hash values are references to 216# NOTE: the arrays in the hash values are references to
131# the archs from $ARCH 217# the archs from $ARCH
132sub editor_archs { 218sub editor_archs {
133 my %paths; 219 my %paths;
134 220
135 for (keys %$ARCH) { 221 for (keys %ARCH) {
136 my $arch = $ARCH->{$_}; 222 my $arch = $ARCH{$_};
137 push @{$paths{$arch->{editor_folder}}}, \$arch; 223 push @{$paths{$arch->{editor_folder}}}, \$arch;
138 } 224 }
139 225
140 return \%paths; 226 \%paths
141} 227}
142 228
143# arch_extends determines how the arch looks like on the map, 229# arch_extents determines the extents of a given arch
144# bigfaces, linked faces and single faces are handled here 230# bigfaces, linked faces and single faces are handled here
145# it returns (<xoffset>, <yoffset>, <width>, <height>) 231# it returns (minx, miny, maxx, maxy)
146# NOTE: non rectangular linked faces are not considered
147sub arch_extends { 232sub arch_extents {
148 my ($a) = @_; 233 my ($a) = @_;
149 234
150 my $TC = \%Crossfire::Tilecache::TILECACHE; 235 my $o = $ARCH{$a->{_name}}
151
152 my $facename =
153 $a->{face} || $ARCH->{$a->{_name}}->{face}
154 or return (); 236 or return;
155 237
156 my $tile = $TC->{$facename} 238 my $face = $FACE{$a->{face} || $o->{face}}
157 or (warn "no gfx found for arch '$facename' in arch_size ()"), return; 239 or (warn "no face data found for arch '$a->{_name}'"), return;
158 240
159 if ($tile->{w} > 1 || $tile->{h} > 1) { 241 if ($face->{w} > 1 || $face->{h} > 1) {
160 # bigfaces 242 # bigface
161 return (0, 0, $tile->{w}, $tile->{h}); 243 return (0, 0, $face->{w} - 1, $face->{h} - 1);
162 244
163 } elsif ($a->{more}) { 245 } elsif ($o->{more}) {
164 # linked faces 246 # linked face
165 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); 247 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
166 do {
167 $miw > (0 + $a->{x}) and $miw = $a->{x};
168 $mih > (0 + $a->{y}) and $mih = $a->{y};
169 $maw < (0 + $a->{x}) and $maw = $a->{x};
170 $mah < (0 + $a->{y}) and $mah = $a->{y};
171 } while $a = $a->{more};
172 248
173 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);
174 257
175 } else { 258 } else {
176 # single face 259 # single face
177 return (0, 0, 1, 1); 260 return (0, 0, 0, 0);
178 } 261 }
179}
180
181# arch2pickmap forms a list of archs to a pickmap
182sub arch2pickmap {
183 my ($archs, $w) = @_;
184
185 # sort archs alphabetiacally
186 my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ];
187
188 $w ||= 10; # default width
189 my $num = @$archs;
190 my $map = { };
191 # overall placement coords
192 my $x = 0;
193 my $y = 0;
194
195 my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later
196 my $drawn_archs = 1; # line-break counter
197 my $max_line_height = 1;
198
199 for (my $i = 0; $i < $num; $i++) {
200
201 defined ${$archs->[$i]}->{face} or next;
202
203 # check whether this tile was already written (see below at (b))
204 unless (defined $map->{map}[$x][$y]) {
205
206 my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
207
208 # these are special placement coords, for chained faces which
209 # have a special placement offset
210 my ($place_x, $place_y) = ($x, $y);
211 $xoffs < 0 and
212 $place_x += -$xoffs;
213 $yoffs < 0 and
214 $place_y += -$yoffs;
215
216 # iterate over the tiles this arch takes
217 # NOTE: Chained archs are maybe not a rectangle, but i don't care
218 # much for that on pickmaps
219
220 for (my $xi = 0; $xi < $arch_w; $xi++) {
221 for (my $yi = 0; $yi < $arch_h; $yi++) {
222
223 my ($lx, $ly) = ($x + $xi, $y + $yi);
224
225 if ($lx == $place_x and $ly == $place_y) {
226 push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
227
228 } else {
229
230 # (b): here we set occupied tiles, but without the arch
231 $map->{map}[$lx][$ly] = [];
232 }
233 }
234 }
235 $drawn_archs++;
236
237 $x += $arch_w - 1;
238 $max_line_height < $arch_h
239 and $max_line_height = $arch_h;
240
241 } else {
242 $i--;
243 }
244
245
246 $x++;
247
248 if ($x > $w) {
249
250 $y += $max_line_height;
251 $max_line_height = 1;
252 $x = 0;
253 }
254
255 $maxw < ($x + 1) and $maxw = $x + 1;
256 $maxh < ($y + 1) and $maxh = $y + 1;
257 }
258
259 $map->{height} = $maxh;
260 $map->{width} = $maxw;
261
262 return $map;
263}
264
265sub arch2map($;$) {
266 my ($mapa) = @_;
267
268 my %meta;
269
270 my ($mapx, $mapy);
271
272 my $map;
273
274 for (@{ $mapa->{arch} }) {
275 my ($x, $y) = (delete $_->{x}, delete $_->{y});
276
277 if ($_->{_name} eq "map") {
278 $meta{info} = $_;
279
280 $mapx = $_->{width} || $x;
281 $mapy = $_->{height} || $y;
282 } else {
283 push @{ $map->[$x][$y] }, $_;
284
285 # arch map is unreliable w.r.t. width and height
286 $mapx = $x + 1 if $mapx <= $x;
287 $mapy = $y + 1 if $mapy <= $y;
288 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
289 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
290 }
291 }
292
293 $meta{width} = $mapx;
294 $meta{height} = $mapy;
295 $meta{map} = $map;
296
297 \%meta
298} 262}
299 263
300sub init($) { 264sub init($) {
301 my ($cachedir) = @_; 265 my ($cachedir) = @_;
302 266
267 return if %ARCH;
268
303 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 269 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
304} 270}
271
272$CACHEDIR ||= "$ENV{HOME}/.crossfire";
273
274init $CACHEDIR;
305 275
306=head1 AUTHOR 276=head1 AUTHOR
307 277
308 Marc Lehmann <schmorp@schmorp.de> 278 Marc Lehmann <schmorp@schmorp.de>
309 http://home.schmorp.de/ 279 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines