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.13 by root, Thu Feb 9 20:54:42 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
15use Carp ();
12use Storable; 16use Storable;
13use List::Util qw(max);
14 17
15#use Gtk2; 18#XXX: The map_* procedures scream for a map-object
16 19
17#init Gtk2::Gdk; 20our @EXPORT =
21 qw(read_pak read_arch $ARCH TILESIZE editor_archs
22 arch_extends
23 map_get_tile_stack map_push_tile_stack map_pop_tile_stack
24 );
18 25
19my $LIB = $ENV{CROSSFIRE_LIBDIR} 26our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 27 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
21 28
22my $VARDIR = "$ENV{HOME}/.gcfedit";
23mkdir $VARDIR;
24
25sub T (){ 32 } 29sub TILESIZE (){ 32 }
26 30
31our $ARCH;
32
33our %FIELD_MULTILINE = (
34 msg => "endmsg",
35);
36
37# not used yet, maybe alphabetical is ok
38our @FIELD_ORDER = (qw(name name_pl));
39
40# not used yet, AND NOT CHECKED, should also be BY TYPE
41our %FIELD_NORMALIZE = (
42 "enter_x" => "hp",
43 "enter_y" => "sp",
44);
45
27sub read_pak($) { 46sub read_pak($;$) {
28 my ($path) = @_; 47 my ($path, $cache) = @_;
29 48
30 eval { 49 eval {
31 -M "$VARDIR/crossfire.pak.pst" < -M $path 50 defined $cache
32 && Storable::retrieve "$VARDIR/crossfire.pak.pst" 51 && -M $cache < -M $path
52 && Storable::retrieve $cache
33 } or do { 53 } or do {
34 my %pak; 54 my %pak;
35 55
36 open my $fh, "<:raw", $path 56 open my $fh, "<:raw", $path
37 or die "$_[0]: $!"; 57 or Carp::croak "$_[0]: $!";
38 while (<$fh>) { 58 while (<$fh>) {
39 my ($type, $id, $len, $path) = split; 59 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 60 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 61 read $fh, $pak{$path}, $len;
42 } 62 }
43 63
44 Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; 64 Storable::nstore \%pak, $cache
65 if defined $cache;
45 66
46 \%pak 67 \%pak
47 } 68 }
48} 69}
49 70
50sub read_arch($;$) { 71sub read_arch($;$) {
51 my ($path) = @_; 72 my ($path, $cache) = @_;
52 73
74 eval {
75 defined $cache
76 && -M $cache < -M $path
77 && Storable::retrieve $cache
78 } or do {
53 my %arc; 79 my %arc;
54 my ($more, $prev); 80 my ($more, $prev);
55 81
56 open my $fh, "<:raw", $path 82 open my $fh, "<:raw", $path
57 or die "$path: $!"; 83 or Carp::croak "$path: $!";
58 84
59 my $parse_block; $parse_block = sub { 85 my $parse_block; $parse_block = sub {
60 my %arc = @_; 86 my %arc = @_;
87
88 while (<$fh>) {
89 s/\s+$//;
90 if (/^end$/i) {
91 last;
92 } elsif (/^arch (\S+)$/) {
93 push @{ $arc{inventory} }, $parse_block->(_name => $1);
94 } elsif (/^lore$/) {
95 while (<$fh>) {
96 last if /^endlore\s*$/i;
97 $arc{lore} .= $_;
98 }
99 } elsif (/^msg$/) {
100 while (<$fh>) {
101 last if /^endmsg\s*$/i;
102 $arc{msg} .= $_;
103 }
104 } elsif (/^(\S+)\s*(.*)$/) {
105 $arc{lc $1} = $2;
106 } elsif (/^\s*($|#)/) {
107 #
108 } else {
109 warn "$path: unparsable line '$_' in arch $arc{_name}";
110 }
111 }
112
113 \%arc
114 };
61 115
62 while (<$fh>) { 116 while (<$fh>) {
63 s/\s+$//; 117 s/\s+$//;
64 if (/^end$/i) { 118 if (/^more$/i) {
65 last; 119 $more = $prev;
66 } elsif (/^arch (\S+)$/) { 120 } elsif (/^object (\S+)$/i) {
121 my $name = $1;
67 push @{ $arc{inventory} }, $parse_block->(_name => $1); 122 my $arc = $parse_block->(_name => $name);
68 } elsif (/^lore$/) { 123
69 while (<$fh>) { 124 if ($more) {
70 last if /^endlore\s*$/i; 125 $more->{more} = $arc;
126 } else {
71 $arc{lore} .= $_; 127 $arc{$name} = $arc;
72 } 128 }
73 } elsif (/^msg$/) { 129 $prev = $arc;
74 while (<$fh>) { 130 $more = undef;
75 last if /^endmsg\s*$/i;
76 $arc{msg} .= $_;
77 }
78 } elsif (/^(\S+)\s*(.*)$/) { 131 } elsif (/^arch (\S+)$/i) {
79 $arc{lc $1} = $2; 132 push @{ $arc{arch} }, $parse_block->(_name => $1);
80 } elsif (/^\s*($|#)/) { 133 } elsif (/^\s*($|#)/) {
81 # 134 #
82 } else { 135 } else {
83 warn "$path: unparsable line '$_' in arch $arc{_name}"; 136 warn "$path: unparseable top-level line '$_'";
84 } 137 }
85 } 138 }
86 139
140 undef $parse_block; # work around bug in perl not freeing $fh etc.
141
142 Storable::nstore \%arc, $cache
143 if defined $cache;
144
87 \%arc 145 \%arc
88 }; 146 }
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} 147}
118 148
119sub cfmap_meta($;$) { 149# returns the arch/object stack from a tile on a map
120 my ($self, $mapa, $mapname) = @_; 150sub map_get_tile_stack {
121
122 my $arch = $self->{arch};
123
124 my %meta;
125
126 my ($mapx, $mapy); 151 my ($map, $x, $y) = @_;
127
128 my $map; 152 my $as;
129 153
130 for (@{ $mapa->{arch} }) { 154 if ($x > 0 || $x < $map->{width}
131 my ($x, $y) = ($_->{x}, $_->{y}); 155 || $y > 0 || $y < $map->{height}) {
132 156
133 if ($_->{_name} eq "map") { 157 $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 } 158 }
148 159
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; 160 return $as;
161}
162
163# pop the topmost arch/object from the stack of a tile on a map
164sub map_pop_tile_stack {
165 my ($map, $x, $y) = @_;
166
167 if ($x > 0 || $x < $map->{width}
168 || $y > 0 || $y < $map->{height}) {
169
170 pop @{$map->{map}{map}[$x][$y]};
171 }
172}
173
174# pushes the arch/object on the stack of a tile on a map
175sub map_push_tile_stack {
176 my ($map, $x, $y, $arch) = @_;
177
178 if ($x > 0 || $x < $map->{width}
179 || $y > 0 || $y < $map->{height}) {
180
181 push @{$map->{map}{map}[$x][$y]}, $arch;
182 }
183}
184
185
186# put all archs into a hash with editor_face as it's key
187# NOTE: the arrays in the hash values are references to
188# the archs from $ARCH
189sub editor_archs {
190 my %paths;
191
192 for (keys %$ARCH) {
193 my $arch = $ARCH->{$_};
194 push @{$paths{$arch->{editor_folder}}}, \$arch;
195 }
196
197 return \%paths;
198}
199
200# arch_extends determines how the arch looks like on the map,
201# bigfaces, linked faces and single faces are handled here
202# it returns (<xoffset>, <yoffset>, <width>, <height>)
203# NOTE: non rectangular linked faces are not considered
204sub arch_extends {
205 my ($a) = @_;
206
207 my $TC = \%Crossfire::Tilecache::TILECACHE;
208
209 my $facename =
210 $a->{face} || $ARCH->{$a->{_name}}->{face}
211 or return ();
212
213 my $tile = $TC->{$facename}
214 or (warn "no gfx found for arch '$facename' in arch_size ()"), return;
215
216 if ($tile->{w} > 1 || $tile->{h} > 1) {
217 # bigfaces
218 return (0, 0, $tile->{w}, $tile->{h});
219
220 } elsif ($a->{more}) {
221 # linked faces
222 my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0);
223 do {
224 $miw > (0 + $a->{x}) and $miw = $a->{x};
225 $mih > (0 + $a->{y}) and $mih = $a->{y};
226 $maw < (0 + $a->{x}) and $maw = $a->{x};
227 $mah < (0 + $a->{y}) and $mah = $a->{y};
228 } while $a = $a->{more};
229
230 return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1)
231
232 } else {
233 # single face
234 return (0, 0, 1, 1);
235 }
236}
237
238sub init($) {
239 my ($cachedir) = @_;
240
241 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
221} 242}
222 243
223=head1 AUTHOR 244=head1 AUTHOR
224 245
225 Marc Lehmann <schmorp@schmorp.de> 246 Marc Lehmann <schmorp@schmorp.de>
227 248
228 Robin Redeker <elmex@ta-sa.org> 249 Robin Redeker <elmex@ta-sa.org>
229 http://www.ta-sa.org/ 250 http://www.ta-sa.org/
230 251
231=cut 252=cut
2321; 253
2541

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines