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.2 by elmex, Sat Feb 4 23:54:05 2006 UTC vs.
Revision 1.11 by elmex, Tue Feb 7 14:30:08 2006 UTC

1=head1 NAME
2
3Crossfire - Crossfire maphandling
4
5=cut
6
1package Crossfire; 7package Crossfire;
2=head1 NAME
3 8
4Crossfire - Crossfire maphandling
5
6=cut
7
8our $VERSION = '1.21'; 9our $VERSION = '0.1';
9 10
10use strict; 11use strict;
11 12
13use base 'Exporter';
14
12use Storable; 15use Storable;
13use List::Util qw(max);
14 16
15#use Gtk2; 17#XXX: The map_* procedures scream for a map-object
16 18
17#init Gtk2::Gdk; 19our @EXPORT =
20 qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs
21 arch2pickmap arch_extends
22 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
23 );
18 24
19my $LIB = $ENV{CROSSFIRE_LIBDIR} 25our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 26 or die "\$CROSSFIRE_LIBDIR must be set\n";
21 27
22my $VARDIR = "$ENV{HOME}/.gcfedit";
23mkdir $VARDIR;
24
25sub T (){ 32 } 28sub TILESIZE (){ 32 }
26 29
30our $ARCH;
31
27sub read_pak($) { 32sub read_pak($;$) {
28 my ($path) = @_; 33 my ($path, $cache) = @_;
29 34
30 eval { 35 eval {
31 -M "$VARDIR/crossfire.pak.pst" < -M $path 36 defined $cache
32 && Storable::retrieve "$VARDIR/crossfire.pak.pst" 37 && -M $cache < -M $path
38 && Storable::retrieve $cache
33 } or do { 39 } or do {
34 my %pak; 40 my %pak;
35 41
36 open my $fh, "<:raw", $path 42 open my $fh, "<:raw", $path
37 or die "$_[0]: $!"; 43 or die "$_[0]: $!";
39 my ($type, $id, $len, $path) = split; 45 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 46 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 47 read $fh, $pak{$path}, $len;
42 } 48 }
43 49
44 Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; 50 Storable::nstore \%pak, $cache
51 if defined $cache;
45 52
46 \%pak 53 \%pak
47 } 54 }
48} 55}
49 56
50sub read_arch($;$) { 57sub read_arch($;$) {
51 my ($path) = @_; 58 my ($path, $cache) = @_;
52 59
60 eval {
61 defined $cache
62 && -M $cache < -M $path
63 && Storable::retrieve $cache
64 } or do {
53 my %arc; 65 my %arc;
54 my ($more, $prev); 66 my ($more, $prev);
55 67
56 open my $fh, "<:raw", $path 68 open my $fh, "<:raw", $path
57 or die "$path: $!"; 69 or die "$path: $!";
58 70
59 my $parse_block; $parse_block = sub { 71 my $parse_block; $parse_block = sub {
60 my %arc = @_; 72 my %arc = @_;
73
74 while (<$fh>) {
75 s/\s+$//;
76 if (/^end$/i) {
77 last;
78 } elsif (/^arch (\S+)$/) {
79 push @{ $arc{inventory} }, $parse_block->(_name => $1);
80 } elsif (/^lore$/) {
81 while (<$fh>) {
82 last if /^endlore\s*$/i;
83 $arc{lore} .= $_;
84 }
85 } elsif (/^msg$/) {
86 while (<$fh>) {
87 last if /^endmsg\s*$/i;
88 $arc{msg} .= $_;
89 }
90 } elsif (/^(\S+)\s*(.*)$/) {
91 $arc{lc $1} = $2;
92 } elsif (/^\s*($|#)/) {
93 #
94 } else {
95 warn "$path: unparsable line '$_' in arch $arc{_name}";
96 }
97 }
98
99 \%arc
100 };
61 101
62 while (<$fh>) { 102 while (<$fh>) {
63 s/\s+$//; 103 s/\s+$//;
64 if (/^end$/i) { 104 if (/^more$/i) {
65 last; 105 $more = $prev;
66 } elsif (/^arch (\S+)$/) { 106 } elsif (/^object (\S+)$/i) {
107 my $name = $1;
67 push @{ $arc{inventory} }, $parse_block->(_name => $1); 108 my $arc = $parse_block->(_name => $name);
68 } elsif (/^lore$/) { 109
69 while (<$fh>) { 110 if ($more) {
70 last if /^endlore\s*$/i; 111 $more->{more} = $arc;
112 } else {
71 $arc{lore} .= $_; 113 $arc{$name} = $arc;
72 } 114 }
73 } elsif (/^msg$/) { 115 $prev = $arc;
74 while (<$fh>) { 116 $more = undef;
75 last if /^endmsg\s*$/i;
76 $arc{msg} .= $_;
77 }
78 } elsif (/^(\S+)\s*(.*)$/) { 117 } elsif (/^arch (\S+)$/i) {
79 $arc{lc $1} = $2; 118 push @{ $arc{arch} }, $parse_block->(_name => $1);
80 } elsif (/^\s*($|#)/) { 119 } elsif (/^\s*($|#)/) {
81 # 120 #
82 } else { 121 } else {
83 warn "$path: unparsable line '$_' in arch $arc{_name}"; 122 warn "$path: unparseable top-level line '$_'";
84 } 123 }
85 } 124 }
86 125
126 undef $parse_block; # work around bug in perl not freeing $fh etc.
127
128 Storable::nstore \%arc, $cache
129 if defined $cache;
130
87 \%arc 131 \%arc
88 }; 132 }
133}
89 134
90 while (<$fh>) { 135# returns the arch/object stack from a tile on a map
91 s/\s+$//; 136sub map_get_tile_stack {
92 if (/^more$/i) { 137 my ($map, $x, $y) = @_;
93 $more = $prev; 138 my $as;
94 } elsif (/^object (\S+)$/i) {
95 my $name = $1;
96 my $arc = $parse_block->(_name => $name);
97 139
98 if ($more) { 140 if ($x > 0 || $x < $map->{width}
99 $more->{more} = $arc; 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
173# NOTE: the arrays in the hash values are references to
174# the archs from $ARCH
175sub editor_archs {
176 my %paths;
177
178 for (keys %$ARCH) {
179 my $arch = $ARCH->{$_};
180 push @{$paths{$arch->{editor_folder}}}, \$arch;
181 }
182
183 return \%paths;
184}
185
186# arch_extends determines how the arch looks like on the map,
187# bigfaces, linked faces and single faces are handled here
188# it returns (<xoffset>, <yoffset>, <width>, <height>)
189# NOTE: non rectangular linked faces are not considered
190sub arch_extends {
191 my ($a) = @_;
192
193 my $TC = \%Crossfire::Tilecache::TILECACHE;
194
195 my $facename =
196 $a->{face} || $ARCH->{$a->{_name}}->{face}
197 or return ();
198
199 my $tile = $TC->{$facename}
200 or (warn "no gfx found for arch '$facename' in arch_size ()"), return;
201
202 if ($tile->{w} > 1 || $tile->{h} > 1) {
203 # bigfaces
204 return (0, 0, $tile->{w}, $tile->{h});
205
206 } elsif ($a->{more}) {
207 # linked faces
208 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0);
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
216 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1)
217
218 } else {
219 # single face
220 return (0, 0, 1, 1);
221 }
222}
223
224# arch2pickmap forms a list of archs to a pickmap
225sub arch2pickmap {
226 my ($archs, $w) = @_;
227
228 # sort archs alphabetiacally
229 my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ];
230
231 $w ||= 10; # default width
232 my $num = @$archs;
233 my $map = { };
234 # overall placement coords
235 my $x = 0;
236 my $y = 0;
237
238 my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later
239 my $drawn_archs = 1; # line-break counter
240 my $max_line_height = 1;
241
242 for (my $i = 0; $i < $num; $i++) {
243
244 defined ${$archs->[$i]}->{face} or next;
245
246 # check whether this tile was already written (see below at (b))
247 unless (defined $map->{map}[$x][$y]) {
248
249 my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
250
251 # these are special placement coords, for chained faces which
252 # have a special placement offset
253 my ($place_x, $place_y) = ($x, $y);
254 $xoffs < 0 and
255 $place_x += -$xoffs;
256 $yoffs < 0 and
257 $place_y += -$yoffs;
258
259 # iterate over the tiles this arch takes
260 # NOTE: Chained archs are maybe not a rectangle, but i don't care
261 # much for that on pickmaps
262
263 for (my $xi = 0; $xi < $arch_w; $xi++) {
264 for (my $yi = 0; $yi < $arch_h; $yi++) {
265
266 my ($lx, $ly) = ($x + $xi, $y + $yi);
267
268 if ($lx == $place_x and $ly == $place_y) {
269 push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
270
100 } else { 271 } else {
101 $arc{$name} = $arc; 272
273 # (b): here we set occupied tiles, but without the arch
274 $map->{map}[$lx][$ly] = [];
275 }
276 }
102 } 277 }
103 $prev = $arc; 278 $drawn_archs++;
104 $more = undef; 279
105 } elsif (/^arch (\S+)$/i) { 280 $x += $arch_w - 1;
106 push @{ $arc{arch} }, $parse_block->(_name => $1); 281 $max_line_height < $arch_h
107 } elsif (/^\s*($|#)/) { 282 and $max_line_height = $arch_h;
108 # 283
109 } else { 284 } else {
110 warn "$path: unparseable top-level line '$_'"; 285 $i--;
111 } 286 }
112 }
113 287
114 undef $parse_block; # work around bug in perl not freeing $fh etc.
115 288
116 \%arc 289 $x++;
117}
118 290
291 if ($x > $w) {
292
293 $y += $max_line_height;
294 $max_line_height = 1;
295 $x = 0;
296 }
297
298 $maxw < ($x + 1) and $maxw = $x + 1;
299 $maxh < ($y + 1) and $maxh = $y + 1;
300 }
301
302 $map->{height} = $maxh;
303 $map->{width} = $maxw;
304
305 return $map;
306}
307
119sub cfmap_meta($;$) { 308sub arch2map($;$) {
120 my ($self, $mapa, $mapname) = @_; 309 my ($mapa) = @_;
121
122 my $arch = $self->{arch};
123 310
124 my %meta; 311 my %meta;
125 312
126 my ($mapx, $mapy); 313 my ($mapx, $mapy);
127 314
128 my $map; 315 my $map;
129 316
130 for (@{ $mapa->{arch} }) { 317 for (@{ $mapa->{arch} }) {
131 my ($x, $y) = ($_->{x}, $_->{y}); 318 my ($x, $y) = (delete $_->{x}, delete $_->{y});
132 319
133 if ($_->{_name} eq "map") { 320 if ($_->{_name} eq "map") {
134 $meta{info} = $_; 321 $meta{info} = $_;
135 322
136 $mapx = $_->{width} || $x; 323 $mapx = $_->{width} || $x;
146 } 333 }
147 } 334 }
148 335
149 $meta{width} = $mapx; 336 $meta{width} = $mapx;
150 $meta{height} = $mapy; 337 $meta{height} = $mapy;
151 338 $meta{map} = $map;
152 my %draw_info;
153 my %map_info;
154
155 # first pass, gather face stacking order, border and corner info
156 for my $x (0 .. $mapx - 1) {
157 my $col = $map->[$x];
158 for my $y (0 .. $mapy - 1) {
159 my $as = $col->[$y] || [];
160
161 for my $layer (0 .. $#$as) {
162 my $a = $as->[$layer];
163
164 my $o = $arch->{$a->{_name}}
165 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
166
167 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
168 my $level = $layer * 256;
169
170 $level -= 100 * 256 if $o->{_name} eq "blocked";
171
172 while ($o) {
173 my $face = $a->{face} || $o->{face};
174
175 my $mx = $x + $o->{x};
176 my $my = $y + $o->{y};
177
178 last if 0 > $mx || $mx >= $mapx
179 || 0 > $my || $my >= $mapy;
180
181 push @{ $map_info{$level}{$mx, $my} }, $a;
182
183 $o = $o->{more};
184 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
185 }
186 }
187 }
188 }
189
190 # third pass, gather meta info
191 for my $level (sort { $a <=> $b } keys %map_info) {
192 my $info = $map_info{$level};
193
194 while (my ($xy, $as) = each %$info) {
195 my ($x, $y) = split $;, $xy;
196
197 next if $x < 0 || $x >= $mapx
198 || $y < 0 || $y >= $mapy;
199
200 push @{ $meta{map}[$x][$y] }, $_ for @$as;
201 }
202 }
203 339
204 \%meta 340 \%meta
205} 341}
206 342
207sub new { 343sub init($) {
208 my $class = shift; 344 my ($cachedir) = @_;
209 my $self = bless { }, $class;
210 $self->{arch} = read_arch "$LIB/archetypes";
211 $self->{tile} = read_pak "$LIB/crossfire.0";
212 $self
213}
214 345
215sub read { 346 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
216 my ($self, $file) = @_;
217 my $mapa = read_arch $file;
218 my $map = $self->cfmap_meta ($mapa, $file);
219 print "READ: ".join(',', %{$map->{info}})."\n";
220 return $map;
221} 347}
222 348
223=head1 AUTHOR 349=head1 AUTHOR
224 350
225 Marc Lehmann <schmorp@schmorp.de> 351 Marc Lehmann <schmorp@schmorp.de>
227 353
228 Robin Redeker <elmex@ta-sa.org> 354 Robin Redeker <elmex@ta-sa.org>
229 http://www.ta-sa.org/ 355 http://www.ta-sa.org/
230 356
231=cut 357=cut
2321; 358
3591

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines