|
|
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 | lore => "endlore", |
|
|
36 | ); |
|
|
37 | |
|
|
38 | # not used yet, maybe alphabetical is ok |
|
|
39 | our @FIELD_ORDER = (qw(name name_pl)); |
|
|
40 | |
|
|
41 | sub MOVE_WALK (){ 0x1 } |
|
|
42 | sub MOVE_FLY_LOW (){ 0x2 } |
|
|
43 | sub MOVE_FLY_HIGH (){ 0x4 } |
|
|
44 | sub MOVE_FLYING (){ 0x6 } |
|
|
45 | sub MOVE_SWIM (){ 0x8 } |
|
|
46 | sub MOVE_ALL (){ 0xf } |
|
|
47 | |
|
|
48 | sub normalize_arch($) { |
|
|
49 | my ($ob) = @_; |
|
|
50 | |
|
|
51 | my $arch = $ARCH->{$ob->{_name}} |
|
|
52 | or (warn "$ob->{_name}: no such archetype", return $ob); |
|
|
53 | |
|
|
54 | delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply); |
|
|
55 | |
|
|
56 | if ($arch->{type} == 22) { # map |
|
|
57 | my %normalize = ( |
|
|
58 | "enter_x" => "hp", |
|
|
59 | "enter_y" => "sp", |
|
|
60 | "width" => "x", |
|
|
61 | "height" => "y", |
|
|
62 | "reset_timeout" => "weight", |
|
|
63 | "swap_time" => "value", |
|
|
64 | "difficulty" => "level", |
|
|
65 | "darkness" => "invisible", |
|
|
66 | "fixed_resettime" => "stand_still", |
|
|
67 | ); |
|
|
68 | |
|
|
69 | while (my ($k2, $k1) = each %normalize) { |
|
|
70 | if (defined (my $v = delete $ob->{$k1})) { |
|
|
71 | $ob->{$k2} = $v; |
|
|
72 | } |
|
|
73 | } |
|
|
74 | } |
|
|
75 | |
|
|
76 | if (defined (my $v = delete $ob->{no_pass})) { |
|
|
77 | $ob->{move_block} = $v ? MOVE_ALL : 0; |
|
|
78 | } |
|
|
79 | if (defined (my $v = delete $ob->{walk_on})) { |
|
|
80 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
|
|
81 | : $ob->{move_on} & ~MOVE_WALK; |
|
|
82 | } |
|
|
83 | if (defined (my $v = delete $ob->{walk_off})) { |
|
|
84 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
|
|
85 | : $ob->{move_off} & ~MOVE_WALK; |
|
|
86 | } |
|
|
87 | if (defined (my $v = delete $ob->{fly_on})) { |
|
|
88 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
|
|
89 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
|
|
90 | } |
|
|
91 | if (defined (my $v = delete $ob->{fly_off})) { |
|
|
92 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
|
|
93 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
|
|
94 | } |
|
|
95 | if (defined (my $v = delete $ob->{flying})) { |
|
|
96 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
|
|
97 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
|
|
98 | } |
|
|
99 | |
|
|
100 | # if value matches archetype default, delete |
|
|
101 | while (my ($k, $v) = each %$ob) { |
|
|
102 | if (exists $arch->{$k} and $arch->{$k} eq $v) { |
|
|
103 | delete $ob->{$k}; |
|
|
104 | } |
|
|
105 | } |
|
|
106 | |
|
|
107 | $ob |
|
|
108 | } |
|
|
109 | |
27 | sub read_pak($) { |
110 | sub read_pak($;$) { |
28 | my ($path) = @_; |
111 | my ($path, $cache) = @_; |
29 | |
112 | |
30 | eval { |
113 | eval { |
31 | -M "$VARDIR/crossfire.pak.pst" < -M $path |
114 | defined $cache |
32 | && Storable::retrieve "$VARDIR/crossfire.pak.pst" |
115 | && -M $cache < -M $path |
|
|
116 | && Storable::retrieve $cache |
33 | } or do { |
117 | } or do { |
34 | my %pak; |
118 | my %pak; |
35 | |
119 | |
36 | open my $fh, "<:raw", $path |
120 | open my $fh, "<:raw", $path |
37 | or die "$_[0]: $!"; |
121 | or Carp::croak "$_[0]: $!"; |
38 | while (<$fh>) { |
122 | while (<$fh>) { |
39 | my ($type, $id, $len, $path) = split; |
123 | my ($type, $id, $len, $path) = split; |
40 | $path =~ s/.*\///; |
124 | $path =~ s/.*\///; |
41 | read $fh, $pak{$path}, $len; |
125 | read $fh, $pak{$path}, $len; |
42 | } |
126 | } |
43 | |
127 | |
44 | Storable::nstore \%pak, "$VARDIR/crossfire.pak.pst"; |
128 | Storable::nstore \%pak, $cache |
|
|
129 | if defined $cache; |
45 | |
130 | |
46 | \%pak |
131 | \%pak |
47 | } |
132 | } |
48 | } |
133 | } |
49 | |
134 | |
50 | sub read_arch($;$) { |
135 | sub read_arch($;$) { |
51 | my ($path) = @_; |
136 | my ($path, $cache) = @_; |
52 | |
137 | |
|
|
138 | eval { |
|
|
139 | defined $cache |
|
|
140 | && -M $cache < -M $path |
|
|
141 | && Storable::retrieve $cache |
|
|
142 | } or do { |
53 | my %arc; |
143 | my %arc; |
54 | my ($more, $prev); |
144 | my ($more, $prev); |
55 | |
145 | |
56 | open my $fh, "<:raw", $path |
146 | open my $fh, "<:raw", $path |
57 | or die "$path: $!"; |
147 | or Carp::croak "$path: $!"; |
58 | |
148 | |
59 | my $parse_block; $parse_block = sub { |
149 | my $parse_block; $parse_block = sub { |
60 | my %arc = @_; |
150 | my %arc = @_; |
|
|
151 | |
|
|
152 | while (<$fh>) { |
|
|
153 | s/\s+$//; |
|
|
154 | if (/^end$/i) { |
|
|
155 | last; |
|
|
156 | } elsif (/^arch (\S+)$/) { |
|
|
157 | push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
|
|
158 | } elsif (/^lore$/) { |
|
|
159 | while (<$fh>) { |
|
|
160 | last if /^endlore\s*$/i; |
|
|
161 | $arc{lore} .= $_; |
|
|
162 | } |
|
|
163 | } elsif (/^msg$/) { |
|
|
164 | while (<$fh>) { |
|
|
165 | last if /^endmsg\s*$/i; |
|
|
166 | $arc{msg} .= $_; |
|
|
167 | } |
|
|
168 | } elsif (/^(\S+)\s*(.*)$/) { |
|
|
169 | $arc{lc $1} = $2; |
|
|
170 | } elsif (/^\s*($|#)/) { |
|
|
171 | # |
|
|
172 | } else { |
|
|
173 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | \%arc |
|
|
178 | }; |
61 | |
179 | |
62 | while (<$fh>) { |
180 | while (<$fh>) { |
63 | s/\s+$//; |
181 | s/\s+$//; |
64 | if (/^end$/i) { |
182 | if (/^more$/i) { |
65 | last; |
183 | $more = $prev; |
66 | } elsif (/^arch (\S+)$/) { |
184 | } elsif (/^object (\S+)$/i) { |
|
|
185 | my $name = $1; |
67 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
186 | my $arc = $parse_block->(_name => $name); |
68 | } elsif (/^lore$/) { |
187 | |
69 | while (<$fh>) { |
188 | if ($more) { |
70 | last if /^endlore\s*$/i; |
189 | $more->{more} = $arc; |
|
|
190 | } else { |
71 | $arc{lore} .= $_; |
191 | $arc{$name} = $arc; |
72 | } |
192 | } |
73 | } elsif (/^msg$/) { |
193 | $prev = $arc; |
74 | while (<$fh>) { |
194 | $more = undef; |
75 | last if /^endmsg\s*$/i; |
|
|
76 | $arc{msg} .= $_; |
|
|
77 | } |
|
|
78 | } elsif (/^(\S+)\s*(.*)$/) { |
195 | } elsif (/^arch (\S+)$/i) { |
79 | $arc{lc $1} = $2; |
196 | push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); |
80 | } elsif (/^\s*($|#)/) { |
197 | } elsif (/^\s*($|#)/) { |
81 | # |
198 | # |
82 | } else { |
199 | } else { |
83 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
200 | warn "$path: unparseable top-level line '$_'"; |
84 | } |
201 | } |
85 | } |
202 | } |
86 | |
203 | |
|
|
204 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
|
|
205 | |
|
|
206 | Storable::nstore \%arc, $cache |
|
|
207 | if defined $cache; |
|
|
208 | |
87 | \%arc |
209 | \%arc |
88 | }; |
210 | } |
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 | } |
211 | } |
118 | |
212 | |
119 | sub cfmap_meta($;$) { |
213 | # returns the arch/object stack from a tile on a map |
120 | my ($self, $mapa, $mapname) = @_; |
214 | sub map_get_tile_stack { |
121 | |
|
|
122 | my $arch = $self->{arch}; |
|
|
123 | |
|
|
124 | my %meta; |
|
|
125 | |
|
|
126 | my ($mapx, $mapy); |
215 | my ($map, $x, $y) = @_; |
127 | |
|
|
128 | my $map; |
216 | my $as; |
129 | |
217 | |
130 | for (@{ $mapa->{arch} }) { |
218 | if ($x > 0 || $x < $map->{width} |
131 | my ($x, $y) = ($_->{x}, $_->{y}); |
219 | || $y > 0 || $y < $map->{height}) { |
132 | |
220 | |
133 | if ($_->{_name} eq "map") { |
221 | $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 | } |
222 | } |
148 | |
223 | |
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 | |
|
|
207 | sub 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 | |
|
|
215 | sub 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; |
224 | return $as; |
|
|
225 | } |
|
|
226 | |
|
|
227 | # pop the topmost arch/object from the stack of a tile on a map |
|
|
228 | sub map_pop_tile_stack { |
|
|
229 | my ($map, $x, $y) = @_; |
|
|
230 | |
|
|
231 | if ($x > 0 || $x < $map->{width} |
|
|
232 | || $y > 0 || $y < $map->{height}) { |
|
|
233 | |
|
|
234 | pop @{$map->{map}{map}[$x][$y]}; |
|
|
235 | } |
|
|
236 | } |
|
|
237 | |
|
|
238 | # pushes the arch/object on the stack of a tile on a map |
|
|
239 | sub map_push_tile_stack { |
|
|
240 | my ($map, $x, $y, $arch) = @_; |
|
|
241 | |
|
|
242 | if ($x > 0 || $x < $map->{width} |
|
|
243 | || $y > 0 || $y < $map->{height}) { |
|
|
244 | |
|
|
245 | push @{$map->{map}{map}[$x][$y]}, $arch; |
|
|
246 | } |
|
|
247 | } |
|
|
248 | |
|
|
249 | |
|
|
250 | # put all archs into a hash with editor_face as it's key |
|
|
251 | # NOTE: the arrays in the hash values are references to |
|
|
252 | # the archs from $ARCH |
|
|
253 | sub editor_archs { |
|
|
254 | my %paths; |
|
|
255 | |
|
|
256 | for (keys %$ARCH) { |
|
|
257 | my $arch = $ARCH->{$_}; |
|
|
258 | push @{$paths{$arch->{editor_folder}}}, \$arch; |
|
|
259 | } |
|
|
260 | |
|
|
261 | return \%paths; |
|
|
262 | } |
|
|
263 | |
|
|
264 | # arch_extends determines how the arch looks like on the map, |
|
|
265 | # bigfaces, linked faces and single faces are handled here |
|
|
266 | # it returns (<xoffset>, <yoffset>, <width>, <height>) |
|
|
267 | # NOTE: non rectangular linked faces are not considered |
|
|
268 | sub arch_extends { |
|
|
269 | my ($a) = @_; |
|
|
270 | |
|
|
271 | my $TC = \%Crossfire::Tilecache::TILECACHE; |
|
|
272 | |
|
|
273 | my $facename = |
|
|
274 | $a->{face} || $ARCH->{$a->{_name}}->{face} |
|
|
275 | or return (); |
|
|
276 | |
|
|
277 | my $tile = $TC->{$facename} |
|
|
278 | or (warn "no gfx found for arch '$facename' in arch_size ()"), return; |
|
|
279 | |
|
|
280 | if ($tile->{w} > 1 || $tile->{h} > 1) { |
|
|
281 | # bigfaces |
|
|
282 | return (0, 0, $tile->{w}, $tile->{h}); |
|
|
283 | |
|
|
284 | } elsif ($a->{more}) { |
|
|
285 | # linked faces |
|
|
286 | my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); |
|
|
287 | do { |
|
|
288 | $miw > (0 + $a->{x}) and $miw = $a->{x}; |
|
|
289 | $mih > (0 + $a->{y}) and $mih = $a->{y}; |
|
|
290 | $maw < (0 + $a->{x}) and $maw = $a->{x}; |
|
|
291 | $mah < (0 + $a->{y}) and $mah = $a->{y}; |
|
|
292 | } while $a = $a->{more}; |
|
|
293 | |
|
|
294 | return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) |
|
|
295 | |
|
|
296 | } else { |
|
|
297 | # single face |
|
|
298 | return (0, 0, 1, 1); |
|
|
299 | } |
|
|
300 | } |
|
|
301 | |
|
|
302 | sub init($) { |
|
|
303 | my ($cachedir) = @_; |
|
|
304 | |
|
|
305 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
221 | } |
306 | } |
222 | |
307 | |
223 | =head1 AUTHOR |
308 | =head1 AUTHOR |
224 | |
309 | |
225 | Marc Lehmann <schmorp@schmorp.de> |
310 | Marc Lehmann <schmorp@schmorp.de> |
… | |
… | |
227 | |
312 | |
228 | Robin Redeker <elmex@ta-sa.org> |
313 | Robin Redeker <elmex@ta-sa.org> |
229 | http://www.ta-sa.org/ |
314 | http://www.ta-sa.org/ |
230 | |
315 | |
231 | =cut |
316 | =cut |
232 | 1; |
317 | |
|
|
318 | 1 |