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.12 by root, Thu Feb 9 19:59:29 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 $ARCH TILESIZE editor_archs
21 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, $cache) = @_; 58 my ($path, $cache) = @_;
52 59
53 eval { 60 eval {
54 $cache 61 defined $cache
55 && -M "$VARDIR/$path.pst" < -M $path 62 && -M $cache < -M $path
56 && Storable::retrieve "$VARDIR/$path.pst" 63 && Storable::retrieve $cache
57 } or do { 64 } or do {
58 my %arc; 65 my %arc;
59 my ($more, $prev); 66 my ($more, $prev);
60 67
61 open my $fh, "<:raw", $path 68 open my $fh, "<:raw", $path
116 } 123 }
117 } 124 }
118 125
119 undef $parse_block; # work around bug in perl not freeing $fh etc. 126 undef $parse_block; # work around bug in perl not freeing $fh etc.
120 127
121 if ($cache) {
122 Storable::nstore \%arc, "$path.pst"; 128 Storable::nstore \%arc, $cache
123 utime +(stat $path)[8,9], "$path.pst"; 129 if defined $cache;
124 }
125 130
126 \%arc 131 \%arc
127 } 132 }
128} 133}
129 134
130sub cfmap_meta($;$) { 135# returns the arch/object stack from a tile on a map
131 my ($self, $mapa, $mapname) = @_; 136sub map_get_tile_stack {
132
133 my $arch = $self->{arch};
134
135 my %meta;
136
137 my ($mapx, $mapy); 137 my ($map, $x, $y) = @_;
138
139 my $map; 138 my $as;
140 139
141 for (@{ $mapa->{arch} }) { 140 if ($x > 0 || $x < $map->{width}
142 my ($x, $y) = ($_->{x}, $_->{y}); 141 || $y > 0 || $y < $map->{height}) {
143 142
144 if ($_->{_name} eq "map") { 143 $as = $map->{map}{map}[$x][$y] || [];
145 $meta{info} = $_;
146
147 $mapx = $_->{width} || $x;
148 $mapy = $_->{height} || $y;
149 } else {
150 push @{ $map->[$x][$y] }, $_;
151
152 # arch map is unreliable w.r.t. width and height
153 $mapx = $x + 1 if $mapx <= $x;
154 $mapy = $y + 1 if $mapy <= $y;
155 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
156 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
157 }
158 } 144 }
159 145
160 $meta{width} = $mapx;
161 $meta{height} = $mapy;
162
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
216}
217
218sub new {
219 my $class = shift;
220 my $self = bless { }, $class;
221 $self->{arch} = read_arch "$LIB/archetypes";
222 $self->{tile} = read_pak "$LIB/crossfire.0";
223 $self
224}
225
226sub read {
227 my ($self, $file) = @_;
228 my $mapa = read_arch $file;
229 my $map = $self->cfmap_meta ($mapa, $file);
230 print "READ: ".join(',', %{$map->{info}})."\n";
231 return $map; 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
224sub init($) {
225 my ($cachedir) = @_;
226
227 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
232} 228}
233 229
234=head1 AUTHOR 230=head1 AUTHOR
235 231
236 Marc Lehmann <schmorp@schmorp.de> 232 Marc Lehmann <schmorp@schmorp.de>
238 234
239 Robin Redeker <elmex@ta-sa.org> 235 Robin Redeker <elmex@ta-sa.org>
240 http://www.ta-sa.org/ 236 http://www.ta-sa.org/
241 237
242=cut 238=cut
2431; 239
2401

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines