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.1 by elmex, Sat Feb 4 23:24:03 2006 UTC vs.
Revision 1.4 by root, Sun Feb 5 00:18:26 2006 UTC

1package Crossfire;
2=head1 NAME 1=head1 NAME
3 2
4Crossfire - Crossfire maphandling 3Crossfire - Crossfire maphandling
5 4
6=cut 5=cut
7 6
7package Crossfire;
8
8our $VERSION = '1.21'; 9our $VERSION = '0.1';
9 10
10use strict; 11use strict;
11 12
12use Storable; 13use Storable;
13use List::Util qw(max); 14use List::Util qw(max);
14 15
15#use Gtk2;
16
17#init Gtk2::Gdk;
18
19my $LIB = $ENV{CROSSFIRE_LIBDIR} 16our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 17 or die "\$CROSSFIRE_LIBDIR must be set\n";
21
22my $VARDIR = "$ENV{HOME}/.gcfedit";
23mkdir $VARDIR;
24 18
25sub T (){ 32 } 19sub T (){ 32 }
26 20
21our $ARCH;
22our $TILE;
23
27sub read_pak($) { 24sub read_pak($;$) {
28 my ($path) = @_; 25 my ($path, $cache) = @_;
29 26
30 eval { 27 eval {
31 -M "$VARDIR/crossfire.pak.pst" < -M $path 28 defined $cache
32 && Storable::retrieve "$VARDIR/crossfire.pak.pst" 29 && -M $cache < -M $path
30 && Storable::retrieve $cache
33 } or do { 31 } or do {
34 my %pak; 32 my %pak;
35 33
36 open my $fh, "<:raw", $path 34 open my $fh, "<:raw", $path
37 or die "$_[0]: $!"; 35 or die "$_[0]: $!";
39 my ($type, $id, $len, $path) = split; 37 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 38 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 39 read $fh, $pak{$path}, $len;
42 } 40 }
43 41
44 Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; 42 Storable::nstore \%pak, $cache
43 if defined $cache;
45 44
46 \%pak 45 \%pak
47 } 46 }
48} 47}
49 48
50sub read_arch($;$) { 49sub read_arch($;$) {
51 my ($path, $cache) = @_; 50 my ($path, $cache) = @_;
52 51
53 eval { 52 eval {
54 $cache 53 defined $cache
55 && -M "$VARDIR/$path.pst" < -M $path 54 && -M $cache < -M $path
56 && Storable::retrieve "$VARDIR/$path.pst" 55 && Storable::retrieve $cache
57 } or do { 56 } or do {
58 my %arc; 57 my %arc;
59 my ($more, $prev); 58 my ($more, $prev);
60 59
61 open my $fh, "<:raw", $path 60 open my $fh, "<:raw", $path
116 } 115 }
117 } 116 }
118 117
119 undef $parse_block; # work around bug in perl not freeing $fh etc. 118 undef $parse_block; # work around bug in perl not freeing $fh etc.
120 119
121 if ($cache) {
122 Storable::nstore \%arc, "$path.pst"; 120 Storable::nstore \%arc, $cache
123 utime +(stat $path)[8,9], "$path.pst"; 121 if defined $cache;
124 }
125 122
126 \%arc 123 \%arc
127 } 124 }
128} 125}
129 126
130sub cfmap_meta($;$) { 127sub arch2map($;$) {
131 my ($self, $mapa, $mapname) = @_; 128 my ($mapa) = @_;
132
133 my $arch = $self->{arch};
134 129
135 my %meta; 130 my %meta;
136 131
137 my ($mapx, $mapy); 132 my ($mapx, $mapy);
138 133
158 } 153 }
159 154
160 $meta{width} = $mapx; 155 $meta{width} = $mapx;
161 $meta{height} = $mapy; 156 $meta{height} = $mapy;
162 157
163 my %draw_info;
164 my %map_info;
165
166 # first pass, gather face stacking order, border and corner info
167 for my $x (0 .. $mapx - 1) {
168 my $col = $map->[$x];
169 for my $y (0 .. $mapy - 1) {
170 my $as = $col->[$y] || [];
171
172 for my $layer (0 .. $#$as) {
173 my $a = $as->[$layer];
174
175 my $o = $arch->{$a->{_name}}
176 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
177
178 #my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
179 my $level = $layer * 256;
180
181 $level -= 100 * 256 if $o->{_name} eq "blocked";
182
183 while ($o) {
184 my $face = $a->{face} || $o->{face};
185
186 my $mx = $x + $o->{x};
187 my $my = $y + $o->{y};
188
189 last if 0 > $mx || $mx >= $mapx
190 || 0 > $my || $my >= $mapy;
191
192 push @{ $map_info{$level}{$mx, $my} }, $a;
193
194 $o = $o->{more};
195 $level = ($layer + 1000) * 2; # put "big things" on top, no matter what
196 }
197 }
198 }
199 }
200
201 # third pass, gather meta info
202 for my $level (sort { $a <=> $b } keys %map_info) {
203 my $info = $map_info{$level};
204
205 while (my ($xy, $as) = each %$info) {
206 my ($x, $y) = split $;, $xy;
207
208 next if $x < 0 || $x >= $mapx
209 || $y < 0 || $y >= $mapy;
210
211 push @{ $meta{map}[$x][$y] }, $_ for @$as;
212 }
213 }
214
215 \%meta 158 \%meta
216} 159}
217 160
218sub new { 161sub init($) {
219 my $class = shift; 162 my ($cachedir) = @_;
220 my $self = bless { }, $class;
221 $self->{arch} = read_arch "$LIB/archetypes";
222 $self->{tile} = read_pak "$LIB/crossfire.0";
223 $self
224}
225 163
226sub read { 164 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
227 my ($self, $file) = @_; 165 $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst";
228 my $mapa = read_arch $file;
229 my $map = $self->cfmap_meta ($mapa, $file);
230 print "READ: ".join(',', %{$map->{info}})."\n";
231 return $map;
232} 166}
233 167
234=head1 AUTHOR 168=head1 AUTHOR
235 169
236 Marc Lehmann <schmorp@schmorp.de> 170 Marc Lehmann <schmorp@schmorp.de>
238 172
239 Robin Redeker <elmex@ta-sa.org> 173 Robin Redeker <elmex@ta-sa.org>
240 http://www.ta-sa.org/ 174 http://www.ta-sa.org/
241 175
242=cut 176=cut
2431; 177
1781

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines