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.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) = @_; 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 }
89
90 while (<$fh>) {
91 s/\s+$//;
92 if (/^more$/i) {
93 $more = $prev;
94 } elsif (/^object (\S+)$/i) {
95 my $name = $1;
96 my $arc = $parse_block->(_name => $name);
97
98 if ($more) {
99 $more->{more} = $arc;
100 } else {
101 $arc{$name} = $arc;
102 }
103 $prev = $arc;
104 $more = undef;
105 } elsif (/^arch (\S+)$/i) {
106 push @{ $arc{arch} }, $parse_block->(_name => $1);
107 } elsif (/^\s*($|#)/) {
108 #
109 } else {
110 warn "$path: unparseable top-level line '$_'";
111 }
112 }
113
114 undef $parse_block; # work around bug in perl not freeing $fh etc.
115
116 \%arc
117} 133}
118 134
119sub cfmap_meta($;$) { 135# returns the arch/object stack from a tile on a map
120 my ($self, $mapa, $mapname) = @_; 136sub map_get_tile_stack {
121
122 my $arch = $self->{arch};
123
124 my %meta;
125
126 my ($mapx, $mapy); 137 my ($map, $x, $y) = @_;
127
128 my $map; 138 my $as;
129 139
130 for (@{ $mapa->{arch} }) { 140 if ($x > 0 || $x < $map->{width}
131 my ($x, $y) = ($_->{x}, $_->{y}); 141 || $y > 0 || $y < $map->{height}) {
132 142
133 if ($_->{_name} eq "map") { 143 $as = $map->{map}{map}[$x][$y] || [];
134 $meta{info} = $_;
135
136 $mapx = $_->{width} || $x;
137 $mapy = $_->{height} || $y;
138 } else {
139 push @{ $map->[$x][$y] }, $_;
140
141 # arch map is unreliable w.r.t. width and height
142 $mapx = $x + 1 if $mapx <= $x;
143 $mapy = $y + 1 if $mapy <= $y;
144 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
145 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
146 }
147 } 144 }
148 145
149 $meta{width} = $mapx;
150 $meta{height} = $mapy;
151
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
204 \%meta
205}
206
207sub new {
208 my $class = shift;
209 my $self = bless { }, $class;
210 $self->{arch} = read_arch "$LIB/archetypes";
211 $self->{tile} = read_pak "$LIB/crossfire.0";
212 $self
213}
214
215sub read {
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; 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";
221} 228}
222 229
223=head1 AUTHOR 230=head1 AUTHOR
224 231
225 Marc Lehmann <schmorp@schmorp.de> 232 Marc Lehmann <schmorp@schmorp.de>
227 234
228 Robin Redeker <elmex@ta-sa.org> 235 Robin Redeker <elmex@ta-sa.org>
229 http://www.ta-sa.org/ 236 http://www.ta-sa.org/
230 237
231=cut 238=cut
2321; 239
2401

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines