|
|
1 | =head1 NAME |
|
|
2 | |
|
|
3 | Crossfire - Crossfire maphandling |
|
|
4 | |
|
|
5 | =cut |
|
|
6 | |
1 | package Crossfire; |
7 | package Crossfire; |
2 | =head1 NAME |
|
|
3 | |
8 | |
4 | Crossfire - Crossfire maphandling |
|
|
5 | |
|
|
6 | =cut |
|
|
7 | |
|
|
8 | our $VERSION = '1.21'; |
9 | our $VERSION = '0.1'; |
9 | |
10 | |
10 | use strict; |
11 | use strict; |
11 | |
12 | |
|
|
13 | use base 'Exporter'; |
|
|
14 | |
|
|
15 | use Carp (); |
12 | use Storable; |
16 | use Storable; |
13 | use 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; |
20 | our @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 | |
19 | my $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 | our $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 | |
22 | my $VARDIR = "$ENV{HOME}/.gcfedit"; |
|
|
23 | mkdir $VARDIR; |
|
|
24 | |
|
|
25 | sub T (){ 32 } |
29 | sub TILESIZE (){ 32 } |
26 | |
30 | |
|
|
31 | our $ARCH; |
|
|
32 | |
|
|
33 | our %FIELD_MULTILINE = ( |
|
|
34 | msg => "endmsg", |
|
|
35 | ); |
|
|
36 | |
|
|
37 | # not used yet, maybe alphabetical is ok |
|
|
38 | our @FIELD_ORDER = (qw(name name_pl)); |
|
|
39 | |
|
|
40 | # not used yet, AND NOT CHECKED, should also be BY TYPE |
|
|
41 | our %FIELD_NORMALIZE = ( |
|
|
42 | "enter_x" => "hp", |
|
|
43 | "enter_y" => "sp", |
|
|
44 | ); |
|
|
45 | |
27 | sub read_pak($) { |
46 | sub 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 | |
50 | sub read_arch($;$) { |
71 | sub read_arch($;$) { |
51 | my ($path, $cache) = @_; |
72 | my ($path, $cache) = @_; |
52 | |
73 | |
53 | eval { |
74 | eval { |
54 | $cache |
75 | defined $cache |
55 | && -M "$VARDIR/$path.pst" < -M $path |
76 | && -M $cache < -M $path |
56 | && Storable::retrieve "$VARDIR/$path.pst" |
77 | && Storable::retrieve $cache |
57 | } or do { |
78 | } or do { |
58 | my %arc; |
79 | my %arc; |
59 | my ($more, $prev); |
80 | my ($more, $prev); |
60 | |
81 | |
61 | open my $fh, "<:raw", $path |
82 | open my $fh, "<:raw", $path |
62 | or die "$path: $!"; |
83 | or Carp::croak "$path: $!"; |
63 | |
84 | |
64 | my $parse_block; $parse_block = sub { |
85 | my $parse_block; $parse_block = sub { |
65 | my %arc = @_; |
86 | my %arc = @_; |
66 | |
87 | |
67 | while (<$fh>) { |
88 | while (<$fh>) { |
… | |
… | |
116 | } |
137 | } |
117 | } |
138 | } |
118 | |
139 | |
119 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
140 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
120 | |
141 | |
121 | if ($cache) { |
|
|
122 | Storable::nstore \%arc, "$path.pst"; |
142 | Storable::nstore \%arc, $cache |
123 | utime +(stat $path)[8,9], "$path.pst"; |
143 | if defined $cache; |
124 | } |
|
|
125 | |
144 | |
126 | \%arc |
145 | \%arc |
127 | } |
146 | } |
128 | } |
147 | } |
129 | |
148 | |
130 | sub cfmap_meta($;$) { |
149 | # returns the arch/object stack from a tile on a map |
131 | my ($self, $mapa, $mapname) = @_; |
150 | sub map_get_tile_stack { |
132 | |
|
|
133 | my $arch = $self->{arch}; |
|
|
134 | |
|
|
135 | my %meta; |
|
|
136 | |
|
|
137 | my ($mapx, $mapy); |
151 | my ($map, $x, $y) = @_; |
138 | |
|
|
139 | my $map; |
152 | my $as; |
140 | |
153 | |
141 | for (@{ $mapa->{arch} }) { |
154 | if ($x > 0 || $x < $map->{width} |
142 | my ($x, $y) = ($_->{x}, $_->{y}); |
155 | || $y > 0 || $y < $map->{height}) { |
143 | |
156 | |
144 | if ($_->{_name} eq "map") { |
157 | $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 | } |
158 | } |
159 | |
159 | |
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 | |
|
|
218 | sub 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 | |
|
|
226 | sub 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; |
160 | return $as; |
|
|
161 | } |
|
|
162 | |
|
|
163 | # pop the topmost arch/object from the stack of a tile on a map |
|
|
164 | sub 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 |
|
|
175 | sub 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 |
|
|
189 | sub 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 |
|
|
204 | sub 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 | |
|
|
238 | sub init($) { |
|
|
239 | my ($cachedir) = @_; |
|
|
240 | |
|
|
241 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
232 | } |
242 | } |
233 | |
243 | |
234 | =head1 AUTHOR |
244 | =head1 AUTHOR |
235 | |
245 | |
236 | Marc Lehmann <schmorp@schmorp.de> |
246 | Marc Lehmann <schmorp@schmorp.de> |
… | |
… | |
238 | |
248 | |
239 | Robin Redeker <elmex@ta-sa.org> |
249 | Robin Redeker <elmex@ta-sa.org> |
240 | http://www.ta-sa.org/ |
250 | http://www.ta-sa.org/ |
241 | |
251 | |
242 | =cut |
252 | =cut |
243 | 1; |
253 | |
|
|
254 | 1 |