… | |
… | |
10 | |
10 | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
|
|
15 | use Carp (); |
|
|
16 | use File::Spec; |
|
|
17 | use List::Util qw(min max); |
15 | use Storable; |
18 | use Storable; |
16 | |
19 | |
17 | our @EXPORT = qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs arch2pickmap arch_extends); |
20 | our @EXPORT = qw( |
|
|
21 | read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
|
|
22 | ); |
18 | |
23 | |
19 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
24 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
20 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
25 | |
|
|
26 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
|
|
27 | |
|
|
28 | mkdir $VARDIR, 0777; |
21 | |
29 | |
22 | sub TILESIZE (){ 32 } |
30 | sub TILESIZE (){ 32 } |
23 | |
31 | |
24 | our $ARCH; |
32 | our %ARCH; |
|
|
33 | our %FACE; |
|
|
34 | our $TILE; |
25 | |
35 | |
26 | sub read_pak($;$) { |
36 | our %FIELD_MULTILINE = ( |
|
|
37 | msg => "endmsg", |
|
|
38 | lore => "endlore", |
|
|
39 | maplore => "endmaplore", |
|
|
40 | ); |
|
|
41 | |
|
|
42 | # not used yet, maybe alphabetical is ok |
|
|
43 | our @FIELD_ORDER = (qw(name name_pl)); |
|
|
44 | |
|
|
45 | sub MOVE_WALK (){ 0x01 } |
|
|
46 | sub MOVE_FLY_LOW (){ 0x02 } |
|
|
47 | sub MOVE_FLY_HIGH (){ 0x04 } |
|
|
48 | sub MOVE_FLYING (){ 0x06 } |
|
|
49 | sub MOVE_SWIM (){ 0x08 } |
|
|
50 | sub MOVE_BOAT (){ 0x10 } |
|
|
51 | sub MOVE_ALL (){ 0xff } |
|
|
52 | |
|
|
53 | sub load_ref($) { |
27 | my ($path, $cache) = @_; |
54 | my ($path) = @_; |
28 | |
55 | |
29 | eval { |
|
|
30 | defined $cache |
|
|
31 | && -M $cache < -M $path |
|
|
32 | && Storable::retrieve $cache |
|
|
33 | } or do { |
|
|
34 | my %pak; |
|
|
35 | |
|
|
36 | open my $fh, "<:raw", $path |
56 | open my $fh, "<", $path |
37 | or die "$_[0]: $!"; |
|
|
38 | while (<$fh>) { |
|
|
39 | my ($type, $id, $len, $path) = split; |
|
|
40 | $path =~ s/.*\///; |
|
|
41 | read $fh, $pak{$path}, $len; |
|
|
42 | } |
|
|
43 | |
|
|
44 | Storable::nstore \%pak, $cache |
|
|
45 | if defined $cache; |
|
|
46 | |
|
|
47 | \%pak |
|
|
48 | } |
|
|
49 | } |
|
|
50 | |
|
|
51 | sub read_arch($;$) { |
|
|
52 | my ($path, $cache) = @_; |
|
|
53 | |
|
|
54 | eval { |
|
|
55 | defined $cache |
|
|
56 | && -M $cache < -M $path |
|
|
57 | && Storable::retrieve $cache |
|
|
58 | } or do { |
|
|
59 | my %arc; |
|
|
60 | my ($more, $prev); |
|
|
61 | |
|
|
62 | open my $fh, "<:raw", $path |
|
|
63 | or die "$path: $!"; |
57 | or die "$path: $!"; |
|
|
58 | binmode $fh; |
|
|
59 | local $/; |
64 | |
60 | |
65 | my $parse_block; $parse_block = sub { |
61 | Storable::thaw <$fh> |
66 | my %arc = @_; |
62 | } |
67 | |
63 | |
68 | while (<$fh>) { |
64 | sub save_ref($$) { |
69 | s/\s+$//; |
65 | my ($ref, $path) = @_; |
70 | if (/^end$/i) { |
66 | |
71 | last; |
67 | open my $fh, ">", "$path~" |
72 | } elsif (/^arch (\S+)$/) { |
68 | or die "$path~: $!"; |
73 | push @{ $arc{inventory} }, $parse_block->(_name => $1); |
69 | binmode $fh; |
74 | } elsif (/^lore$/) { |
70 | print $fh Storable::freeze $ref; |
75 | while (<$fh>) { |
71 | close $fh; |
76 | last if /^endlore\s*$/i; |
72 | rename "$path~", $path |
77 | $arc{lore} .= $_; |
73 | or die "$path: $!"; |
78 | } |
74 | } |
79 | } elsif (/^msg$/) { |
75 | |
80 | while (<$fh>) { |
76 | sub normalize_arch($) { |
81 | last if /^endmsg\s*$/i; |
77 | my ($ob) = @_; |
82 | $arc{msg} .= $_; |
78 | |
83 | } |
79 | my $arch = $ARCH{$ob->{_name}} |
84 | } elsif (/^(\S+)\s*(.*)$/) { |
80 | or (warn "$ob->{_name}: no such archetype", return $ob); |
85 | $arc{lc $1} = $2; |
81 | |
86 | } elsif (/^\s*($|#)/) { |
82 | delete $ob->{$_} for qw( |
87 | # |
83 | can_knockback can_parry can_impale can_cut can_dam_armour |
88 | } else { |
84 | can_apply pass_thru can_pass_thru |
89 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
85 | ); |
90 | } |
86 | |
|
|
87 | if ($arch->{type} == 22) { # map |
|
|
88 | my %normalize = ( |
|
|
89 | "enter_x" => "hp", |
|
|
90 | "enter_y" => "sp", |
|
|
91 | "width" => "x", |
|
|
92 | "height" => "y", |
|
|
93 | "reset_timeout" => "weight", |
|
|
94 | "swap_time" => "value", |
|
|
95 | "difficulty" => "level", |
|
|
96 | "darkness" => "invisible", |
|
|
97 | "fixed_resettime" => "stand_still", |
|
|
98 | ); |
|
|
99 | |
|
|
100 | while (my ($k2, $k1) = each %normalize) { |
|
|
101 | if (defined (my $v = delete $ob->{$k1})) { |
|
|
102 | $ob->{$k2} = $v; |
91 | } |
103 | } |
92 | |
|
|
93 | \%arc |
|
|
94 | }; |
104 | } |
|
|
105 | } |
|
|
106 | |
|
|
107 | for my $attr (qw(move_type move_block move_allow move_on move_off move_slow)) { |
|
|
108 | next unless exists $ob->{$attr}; |
|
|
109 | next if $ob->{$attr} =~ /^\d+$/; |
|
|
110 | |
|
|
111 | my $flags = 0; |
|
|
112 | |
|
|
113 | # assume list |
|
|
114 | for my $flag (map lc, split /\s+/, $ob->{$attr}) { |
|
|
115 | $flags |= MOVE_WALK if $flag eq "walk"; |
|
|
116 | $flags |= MOVE_FLY_LOW if $flag eq "fly_low"; |
|
|
117 | $flags |= MOVE_FLY_HIGH if $flag eq "fly_high"; |
|
|
118 | $flags |= MOVE_FLYING if $flag eq "flying"; |
|
|
119 | $flags |= MOVE_SWIM if $flag eq "swim"; |
|
|
120 | $flags |= MOVE_BOAT if $flag eq "boat"; |
|
|
121 | $flags |= MOVE_ALL if $flag eq "all"; |
|
|
122 | |
|
|
123 | $flags &= ~MOVE_WALK if $flag eq "-walk"; |
|
|
124 | $flags &= ~MOVE_FLY_LOW if $flag eq "-fly_low"; |
|
|
125 | $flags &= ~MOVE_FLY_HIGH if $flag eq "-fly_high"; |
|
|
126 | $flags &= ~MOVE_FLYING if $flag eq "-flying"; |
|
|
127 | $flags &= ~MOVE_SWIM if $flag eq "-swim"; |
|
|
128 | $flags &= ~MOVE_BOAT if $flag eq "-boat"; |
|
|
129 | $flags &= ~MOVE_ALL if $flag eq "-all"; |
|
|
130 | } |
|
|
131 | |
|
|
132 | $ob->{$attr} = $flags; |
|
|
133 | } |
|
|
134 | |
|
|
135 | if (defined (my $v = delete $ob->{no_pass})) { |
|
|
136 | $ob->{move_block} = $v ? MOVE_ALL : 0; |
|
|
137 | } |
|
|
138 | if (defined (my $v = delete $ob->{slow_move})) { |
|
|
139 | $ob->{move_slow} |= MOVE_WALK; |
|
|
140 | $ob->{move_slow_penalty} = $v; |
|
|
141 | } |
|
|
142 | if (defined (my $v = delete $ob->{walk_on})) { |
|
|
143 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
|
|
144 | : $ob->{move_on} & ~MOVE_WALK; |
|
|
145 | } |
|
|
146 | if (defined (my $v = delete $ob->{walk_off})) { |
|
|
147 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
|
|
148 | : $ob->{move_off} & ~MOVE_WALK; |
|
|
149 | } |
|
|
150 | if (defined (my $v = delete $ob->{fly_on})) { |
|
|
151 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
|
|
152 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
|
|
153 | } |
|
|
154 | if (defined (my $v = delete $ob->{fly_off})) { |
|
|
155 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
|
|
156 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
|
|
157 | } |
|
|
158 | if (defined (my $v = delete $ob->{flying})) { |
|
|
159 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
|
|
160 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
|
|
161 | } |
|
|
162 | |
|
|
163 | # if value matches archetype default, delete |
|
|
164 | while (my ($k, $v) = each %$ob) { |
|
|
165 | if (exists $arch->{$k} and $arch->{$k} eq $v) { |
|
|
166 | next if $k eq "_name"; |
|
|
167 | delete $ob->{$k}; |
|
|
168 | } |
|
|
169 | } |
|
|
170 | |
|
|
171 | $ob |
|
|
172 | } |
|
|
173 | |
|
|
174 | sub read_pak($) { |
|
|
175 | my ($path) = @_; |
|
|
176 | |
|
|
177 | my %pak; |
|
|
178 | |
|
|
179 | open my $fh, "<", $path |
|
|
180 | or Carp::croak "$_[0]: $!"; |
|
|
181 | binmode $fh; |
|
|
182 | while (<$fh>) { |
|
|
183 | my ($type, $id, $len, $path) = split; |
|
|
184 | $path =~ s/.*\///; |
|
|
185 | read $fh, $pak{$path}, $len; |
|
|
186 | } |
|
|
187 | |
|
|
188 | \%pak |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub read_arch($) { |
|
|
192 | my ($path) = @_; |
|
|
193 | |
|
|
194 | my %arc; |
|
|
195 | my ($more, $prev); |
|
|
196 | |
|
|
197 | open my $fh, "<", $path |
|
|
198 | or Carp::croak "$path: $!"; |
|
|
199 | |
|
|
200 | binmode $fh; |
|
|
201 | |
|
|
202 | my $parse_block; $parse_block = sub { |
|
|
203 | my %arc = @_; |
95 | |
204 | |
96 | while (<$fh>) { |
205 | while (<$fh>) { |
97 | s/\s+$//; |
206 | s/\s+$//; |
98 | if (/^more$/i) { |
207 | if (/^end$/i) { |
99 | $more = $prev; |
208 | last; |
100 | } elsif (/^object (\S+)$/i) { |
209 | } elsif (/^arch (\S+)$/) { |
101 | my $name = $1; |
210 | push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
102 | my $arc = $parse_block->(_name => $name); |
211 | } elsif (/^lore$/) { |
103 | |
212 | while (<$fh>) { |
104 | if ($more) { |
213 | last if /^endlore\s*$/i; |
105 | $more->{more} = $arc; |
|
|
106 | } else { |
|
|
107 | $arc{$name} = $arc; |
214 | $arc{lore} .= $_; |
108 | } |
215 | } |
109 | $prev = $arc; |
216 | } elsif (/^msg$/) { |
110 | $more = undef; |
217 | while (<$fh>) { |
|
|
218 | last if /^endmsg\s*$/i; |
|
|
219 | $arc{msg} .= $_; |
|
|
220 | } |
111 | } elsif (/^arch (\S+)$/i) { |
221 | } elsif (/^(\S+)\s*(.*)$/) { |
112 | push @{ $arc{arch} }, $parse_block->(_name => $1); |
222 | $arc{lc $1} = $2; |
113 | } elsif (/^\s*($|#)/) { |
223 | } elsif (/^\s*($|#)/) { |
114 | # |
224 | # |
115 | } else { |
225 | } else { |
116 | warn "$path: unparseable top-level line '$_'"; |
226 | warn "$path: unparsable line '$_' in arch $arc{_name}"; |
117 | } |
227 | } |
118 | } |
228 | } |
119 | |
229 | |
120 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
|
|
121 | |
|
|
122 | Storable::nstore \%arc, $cache |
|
|
123 | if defined $cache; |
|
|
124 | |
|
|
125 | \%arc |
230 | \%arc |
126 | } |
231 | }; |
|
|
232 | |
|
|
233 | while (<$fh>) { |
|
|
234 | s/\s+$//; |
|
|
235 | if (/^more$/i) { |
|
|
236 | $more = $prev; |
|
|
237 | } elsif (/^object (\S+)$/i) { |
|
|
238 | my $name = $1; |
|
|
239 | my $arc = $parse_block->(_name => $name); |
|
|
240 | |
|
|
241 | if ($more) { |
|
|
242 | $more->{more} = $arc; |
|
|
243 | } else { |
|
|
244 | $arc{$name} = $arc; |
|
|
245 | } |
|
|
246 | $prev = $arc; |
|
|
247 | $more = undef; |
|
|
248 | } elsif (/^arch (\S+)$/i) { |
|
|
249 | push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1); |
|
|
250 | } elsif (/^\s*($|#)/) { |
|
|
251 | # |
|
|
252 | } else { |
|
|
253 | warn "$path: unparseable top-level line '$_'"; |
|
|
254 | } |
|
|
255 | } |
|
|
256 | |
|
|
257 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
|
|
258 | |
|
|
259 | \%arc |
127 | } |
260 | } |
128 | |
261 | |
129 | # put all archs into a hash with editor_face as it's key |
262 | # put all archs into a hash with editor_face as it's key |
130 | # NOTE: the arrays in the hash values are references to |
263 | # NOTE: the arrays in the hash values are references to |
131 | # the archs from $ARCH |
264 | # the archs from $ARCH |
132 | sub editor_archs { |
265 | sub editor_archs { |
133 | my %paths; |
266 | my %paths; |
134 | |
267 | |
135 | for (keys %$ARCH) { |
268 | for (keys %ARCH) { |
136 | my $arch = $ARCH->{$_}; |
269 | my $arch = $ARCH{$_}; |
137 | push @{$paths{$arch->{editor_folder}}}, \$arch; |
270 | push @{$paths{$arch->{editor_folder}}}, \$arch; |
138 | } |
271 | } |
139 | |
272 | |
140 | return \%paths; |
273 | \%paths |
141 | } |
274 | } |
142 | |
275 | |
143 | # arch_extends determines how the arch looks like on the map, |
276 | =item ($minx, $miny, $maxx, $maxy) = arch_extents $arch |
144 | # bigfaces, linked faces and single faces are handled here |
277 | |
145 | # it returns (<xoffset>, <yoffset>, <width>, <height>) |
278 | arch_extents determines the extents of the given arch's face(s), linked |
146 | # NOTE: non rectangular linked faces are not considered |
279 | faces and single faces are handled here it returns (minx, miny, maxx, |
|
|
280 | maxy) |
|
|
281 | |
|
|
282 | =cut |
|
|
283 | |
147 | sub arch_extends { |
284 | sub arch_extents { |
148 | my ($a) = @_; |
285 | my ($a) = @_; |
149 | |
286 | |
150 | my $TC = \%Crossfire::Tilecache::TILECACHE; |
287 | my $o = $ARCH{$a->{_name}} |
151 | |
|
|
152 | my $facename = |
|
|
153 | $a->{face} || $ARCH->{$a->{_name}}->{face} |
|
|
154 | or return (); |
288 | or return; |
155 | |
289 | |
156 | my $tile = $TC->{$facename} |
290 | my $face = $FACE{$a->{face} || $o->{face} || "blank.111"} |
157 | or (warn "no gfx found for arch '$facename' in arch_size ()"), return; |
291 | or (warn "no face data found for arch '$a->{_name}'"), return; |
158 | |
292 | |
159 | if ($tile->{w} > 1 || $tile->{h} > 1) { |
293 | if ($face->{w} > 1 || $face->{h} > 1) { |
160 | # bigfaces |
294 | # bigface |
161 | return (0, 0, $tile->{w}, $tile->{h}); |
295 | return (0, 0, $face->{w} - 1, $face->{h} - 1); |
162 | |
296 | |
163 | } elsif ($a->{more}) { |
297 | } elsif ($o->{more}) { |
164 | # linked faces |
298 | # linked face |
165 | my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0); |
299 | my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2; |
166 | do { |
|
|
167 | $miw > (0 + $a->{x}) and $miw = $a->{x}; |
|
|
168 | $mih > (0 + $a->{y}) and $mih = $a->{y}; |
|
|
169 | $maw < (0 + $a->{x}) and $maw = $a->{x}; |
|
|
170 | $mah < (0 + $a->{y}) and $mah = $a->{y}; |
|
|
171 | } while $a = $a->{more}; |
|
|
172 | |
300 | |
173 | return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1) |
301 | for (; $o; $o = $o->{more}) { |
|
|
302 | $minx = min $minx, $o->{x}; |
|
|
303 | $miny = min $miny, $o->{y}; |
|
|
304 | $maxx = max $maxx, $o->{x}; |
|
|
305 | $maxy = max $maxy, $o->{y}; |
|
|
306 | } |
|
|
307 | |
|
|
308 | return ($minx, $miny, $maxx, $maxy); |
174 | |
309 | |
175 | } else { |
310 | } else { |
176 | # single face |
311 | # single face |
177 | return (0, 0, 1, 1); |
312 | return (0, 0, 0, 0); |
|
|
313 | } |
|
|
314 | } |
|
|
315 | |
|
|
316 | =item $type = arch_attr $arch |
|
|
317 | |
|
|
318 | Returns a hashref describing the object and its attributes. It can contain |
|
|
319 | the following keys: |
|
|
320 | |
|
|
321 | name the name, suitable for display purposes |
|
|
322 | ignore |
|
|
323 | attr |
|
|
324 | desc |
|
|
325 | use |
|
|
326 | section => [name => \%attr, name => \%attr] |
|
|
327 | import |
|
|
328 | |
|
|
329 | =cut |
|
|
330 | |
|
|
331 | sub arch_attr($) { |
|
|
332 | my ($obj) = @_; |
|
|
333 | |
|
|
334 | require Crossfire::Data; |
|
|
335 | |
|
|
336 | my $root; |
|
|
337 | my $attr = { }; |
|
|
338 | |
|
|
339 | my $arch = $ARCH{ $obj->{_name} }; |
|
|
340 | my $type = $obj->{type} || $arch->{type}; |
|
|
341 | |
|
|
342 | if ($type > 0) { |
|
|
343 | $root = $Crossfire::Data::ATTR{$type}; |
|
|
344 | } else { |
|
|
345 | $root = $Crossfire::Data::TYPE{Misc}; |
|
|
346 | |
|
|
347 | type: |
|
|
348 | for (@Crossfire::Data::ATTR0) { |
|
|
349 | my $req = $_->{required} |
|
|
350 | or die "internal error: ATTR0 without 'required'"; |
|
|
351 | |
|
|
352 | keys %$req; |
|
|
353 | while (my ($k, $v) = each %$req) { |
|
|
354 | next type |
|
|
355 | unless $obj->{$k} == $v || $arch->{$k} == $v; |
|
|
356 | } |
|
|
357 | |
|
|
358 | $root = $_; |
|
|
359 | } |
|
|
360 | } |
|
|
361 | |
|
|
362 | my @import = ($root); |
|
|
363 | |
|
|
364 | unshift @import, \%Crossfire::Data::DEFAULT_ATTR |
|
|
365 | unless $type == 116; |
|
|
366 | |
|
|
367 | my (%ignore); |
|
|
368 | my (@section_order, %section, @attr_order); |
|
|
369 | |
|
|
370 | while (my $type = shift @import) { |
|
|
371 | push @import, @{$type->{import} || []}; |
|
|
372 | |
|
|
373 | $attr->{$_} ||= $type->{$_} |
|
|
374 | for qw(name desc use); |
|
|
375 | |
|
|
376 | for (@{$type->{ignore} || []}) { |
|
|
377 | $ignore{$_}++ for ref $_ ? @$_ : $_; |
|
|
378 | } |
|
|
379 | |
|
|
380 | for ([general => ($type->{attr} || [])], @{$type->{section} || []}) { |
|
|
381 | my ($name, $attr) = @$_; |
|
|
382 | push @section_order, $name; |
|
|
383 | for (@$attr) { |
|
|
384 | my ($k, $v) = @$_; |
|
|
385 | push @attr_order, $k; |
|
|
386 | $section{$name}{$k} ||= $v; |
|
|
387 | } |
|
|
388 | } |
|
|
389 | } |
|
|
390 | |
|
|
391 | $attr->{section} = [ |
|
|
392 | map !exists $section{$_} ? () : do { |
|
|
393 | my $attr = delete $section{$_}; |
|
|
394 | |
|
|
395 | [ |
|
|
396 | $_, |
|
|
397 | map exists $attr->{$_} && !$ignore{$_} |
|
|
398 | ? [$_ => delete $attr->{$_}] : (), |
|
|
399 | @attr_order |
|
|
400 | ] |
|
|
401 | }, |
|
|
402 | |
|
|
403 | exists $section{$_} ? [$_ => delete $section{$_}] : (), |
|
|
404 | @section_order |
|
|
405 | ]; |
|
|
406 | |
|
|
407 | $attr |
|
|
408 | } |
|
|
409 | |
|
|
410 | sub arch_edit_sections { |
|
|
411 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
|
|
412 | # edit_type = 0; |
|
|
413 | # else if (edit_type != 0) { |
|
|
414 | # // all flags from 'check_type' must be unset in this arch because they get recalculated now |
|
|
415 | # edit_type &= ~check_type; |
|
|
416 | # } |
|
|
417 | # |
|
|
418 | # } |
|
|
419 | # if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 && |
|
|
420 | # getAttributeValue("alive", defarch) == 1 && |
|
|
421 | # (getAttributeValue("monster", defarch) == 1 || |
|
|
422 | # getAttributeValue("generator", defarch) == 1)) { |
|
|
423 | # // Monster: monsters/npcs/generators |
|
|
424 | # edit_type |= IGUIConstants.TILE_EDIT_MONSTER; |
|
|
425 | # } |
|
|
426 | # if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 && |
|
|
427 | # arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) { |
|
|
428 | # // Walls |
|
|
429 | # edit_type |= IGUIConstants.TILE_EDIT_WALL; |
|
|
430 | # } |
|
|
431 | # if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 && |
|
|
432 | # getAttributeValue("connected", defarch) != 0) { |
|
|
433 | # // Connected Objects |
|
|
434 | # edit_type |= IGUIConstants.TILE_EDIT_CONNECTED; |
|
|
435 | # } |
|
|
436 | # if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 && |
|
|
437 | # arch_type == 66 || arch_type == 41 || arch_type == 95) { |
|
|
438 | # // Exit: teleporter/exit/trapdoors |
|
|
439 | # edit_type |= IGUIConstants.TILE_EDIT_EXIT; |
|
|
440 | # } |
|
|
441 | # if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 && |
|
|
442 | # getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 || |
|
|
443 | # arch_type == 5 || arch_type == 36 || arch_type == 60 || |
|
|
444 | # arch_type == 85 || arch_type == 111 || arch_type == 123 || |
|
|
445 | # arch_type == 124 || arch_type == 130)) { |
|
|
446 | # // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls |
|
|
447 | # edit_type |= IGUIConstants.TILE_EDIT_TREASURE; |
|
|
448 | # } |
|
|
449 | # if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 && |
|
|
450 | # arch_type == 20 || arch_type == 23 || arch_type == 26 || |
|
|
451 | # arch_type == 91 || arch_type == 21 || arch_type == 24) { |
|
|
452 | # // Door: door/special door/gates + keys |
|
|
453 | # edit_type |= IGUIConstants.TILE_EDIT_DOOR; |
|
|
454 | # } |
|
|
455 | # if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 && |
|
|
456 | # getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 && |
|
|
457 | # arch_type <= 16) || arch_type == 33 || arch_type == 34 || |
|
|
458 | # arch_type == 35 || arch_type == 39 || arch_type == 70 || |
|
|
459 | # arch_type == 87 || arch_type == 99 || arch_type == 100 || |
|
|
460 | # arch_type == 104 || arch_type == 109 || arch_type == 113 || |
|
|
461 | # arch_type == 122 || arch_type == 3)) { |
|
|
462 | # // Equipment: weapons/armour/wands/rods |
|
|
463 | # edit_type |= IGUIConstants.TILE_EDIT_EQUIP; |
|
|
464 | # } |
|
|
465 | # |
|
|
466 | # return(edit_type); |
|
|
467 | # |
|
|
468 | # |
|
|
469 | } |
|
|
470 | |
|
|
471 | sub cache_file($$&&) { |
|
|
472 | my ($src, $cache, $load, $create) = @_; |
|
|
473 | |
|
|
474 | warn "<@_>\n";#d# |
|
|
475 | |
|
|
476 | my ($size, $mtime) = (stat $src)[7,9] |
|
|
477 | or Carp::croak "$src: $!"; |
|
|
478 | |
|
|
479 | if (-e $cache) { |
|
|
480 | my $ref = eval { load_ref $cache }; |
|
|
481 | |
|
|
482 | if ($ref->{version} == 1 |
|
|
483 | && $ref->{size} == $size |
|
|
484 | && $ref->{mtime} == $mtime |
|
|
485 | && eval { $load->($ref->{data}); 1 }) { |
|
|
486 | return; |
|
|
487 | } |
|
|
488 | } |
|
|
489 | |
|
|
490 | my $ref = { |
|
|
491 | version => 1, |
|
|
492 | size => $size, |
|
|
493 | mtime => $mtime, |
|
|
494 | data => $create->(), |
178 | } |
495 | }; |
179 | } |
|
|
180 | |
496 | |
181 | # arch2pickmap forms a list of archs to a pickmap |
497 | $load->($ref->{data}); |
182 | sub arch2pickmap { |
|
|
183 | my ($archs, $w) = @_; |
|
|
184 | |
498 | |
185 | # sort archs alphabetiacally |
499 | save_ref $ref, $cache; |
186 | my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ]; |
500 | } |
187 | |
501 | |
188 | $w ||= 10; # default width |
502 | =item set_libdir $path |
189 | my $num = @$archs; |
503 | |
190 | my $map = { }; |
504 | Sets the library directory to the given path |
191 | # overall placement coords |
505 | (default: $ENV{CROSSFIRE_LIBDIR}). |
|
|
506 | |
|
|
507 | You have to (re-)load the archetypes and tilecache manually after steting |
|
|
508 | the library path. |
|
|
509 | |
|
|
510 | =cut |
|
|
511 | |
|
|
512 | sub set_libdir($) { |
|
|
513 | $LIB = $_[0]; |
|
|
514 | } |
|
|
515 | |
|
|
516 | =item load_archetypes |
|
|
517 | |
|
|
518 | (Re-)Load archetypes into %ARCH. |
|
|
519 | |
|
|
520 | =cut |
|
|
521 | |
|
|
522 | sub load_archetypes() { |
|
|
523 | cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub { |
|
|
524 | *ARCH = $_[0]; |
|
|
525 | }, sub { |
|
|
526 | read_arch "$LIB/archetypes" |
|
|
527 | }; |
|
|
528 | } |
|
|
529 | |
|
|
530 | =item load_tilecache |
|
|
531 | |
|
|
532 | (Re-)Load %TILE and %FACE. |
|
|
533 | |
|
|
534 | =cut |
|
|
535 | |
|
|
536 | sub load_tilecache() { |
|
|
537 | require Gtk2; |
|
|
538 | |
|
|
539 | cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub { |
|
|
540 | $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png" |
|
|
541 | or die "$VARDIR/tilecache.png: $!"; |
|
|
542 | *FACE = $_[0]; |
|
|
543 | }, sub { |
|
|
544 | require File::Temp; |
|
|
545 | |
|
|
546 | my $tile = read_pak "$LIB/crossfire.0"; |
|
|
547 | |
|
|
548 | my %cache; |
|
|
549 | |
192 | my $x = 0; |
550 | my $idx = 0; |
193 | my $y = 0; |
|
|
194 | |
551 | |
195 | my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later |
552 | for my $name (sort keys %$tile) { |
196 | my $drawn_archs = 1; # line-break counter |
553 | my ($fh, $filename) = File::Temp::tempfile (); |
197 | my $max_line_height = 1; |
554 | print $fh $tile->{$name}; |
|
|
555 | close $fh; |
|
|
556 | my $pb = new_from_file Gtk2::Gdk::Pixbuf $filename; |
|
|
557 | unlink $filename; |
198 | |
558 | |
199 | for (my $i = 0; $i < $num; $i++) { |
559 | my $tile = $cache{$name} = { |
200 | |
560 | pb => $pb, |
201 | defined ${$archs->[$i]}->{face} or next; |
561 | idx => $idx, |
202 | |
562 | w => int $pb->get_width / TILESIZE, |
203 | # check whether this tile was already written (see below at (b)) |
563 | h => int $pb->get_height / TILESIZE, |
204 | unless (defined $map->{map}[$x][$y]) { |
564 | }; |
205 | |
|
|
206 | my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]}); |
|
|
207 | |
|
|
208 | # these are special placement coords, for chained faces which |
|
|
209 | # have a special placement offset |
|
|
210 | my ($place_x, $place_y) = ($x, $y); |
|
|
211 | $xoffs < 0 and |
|
|
212 | $place_x += -$xoffs; |
|
|
213 | $yoffs < 0 and |
|
|
214 | $place_y += -$yoffs; |
|
|
215 | |
|
|
216 | # iterate over the tiles this arch takes |
|
|
217 | # NOTE: Chained archs are maybe not a rectangle, but i don't care |
|
|
218 | # much for that on pickmaps |
|
|
219 | |
|
|
220 | for (my $xi = 0; $xi < $arch_w; $xi++) { |
|
|
221 | for (my $yi = 0; $yi < $arch_h; $yi++) { |
|
|
222 | |
|
|
223 | my ($lx, $ly) = ($x + $xi, $y + $yi); |
|
|
224 | |
|
|
225 | if ($lx == $place_x and $ly == $place_y) { |
|
|
226 | push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]}; |
|
|
227 | |
|
|
228 | } else { |
|
|
229 | |
|
|
230 | # (b): here we set occupied tiles, but without the arch |
|
|
231 | $map->{map}[$lx][$ly] = []; |
|
|
232 | } |
565 | |
|
|
566 | |
|
|
567 | $idx += $tile->{w} * $tile->{h}; |
|
|
568 | } |
|
|
569 | |
|
|
570 | my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64; |
|
|
571 | |
|
|
572 | while (my ($name, $tile) = each %cache) { |
|
|
573 | my $tpb = delete $tile->{pb}; |
|
|
574 | my $ofs = $tile->{idx}; |
|
|
575 | |
|
|
576 | for my $x (0 .. $tile->{w} - 1) { |
|
|
577 | for my $y (0 .. $tile->{h} - 1) { |
|
|
578 | my $idx = $ofs + $x + $y * $tile->{w}; |
|
|
579 | $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE, |
|
|
580 | $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64); |
233 | } |
581 | } |
234 | } |
582 | } |
235 | $drawn_archs++; |
|
|
236 | |
|
|
237 | $x += $arch_w - 1; |
|
|
238 | $max_line_height < $arch_h |
|
|
239 | and $max_line_height = $arch_h; |
|
|
240 | |
|
|
241 | } else { |
|
|
242 | $i--; |
|
|
243 | } |
583 | } |
244 | |
584 | |
|
|
585 | $pb->save ("$VARDIR/tilecache.png", "png"); |
245 | |
586 | |
246 | $x++; |
587 | \%cache |
247 | |
|
|
248 | if ($x > $w) { |
|
|
249 | |
|
|
250 | $y += $max_line_height; |
|
|
251 | $max_line_height = 1; |
|
|
252 | $x = 0; |
|
|
253 | } |
|
|
254 | |
|
|
255 | $maxw < ($x + 1) and $maxw = $x + 1; |
|
|
256 | $maxh < ($y + 1) and $maxh = $y + 1; |
|
|
257 | } |
588 | }; |
258 | |
|
|
259 | $map->{height} = $maxh; |
|
|
260 | $map->{width} = $maxw; |
|
|
261 | |
|
|
262 | return $map; |
|
|
263 | } |
|
|
264 | |
|
|
265 | sub arch2map($;$) { |
|
|
266 | my ($mapa) = @_; |
|
|
267 | |
|
|
268 | my %meta; |
|
|
269 | |
|
|
270 | my ($mapx, $mapy); |
|
|
271 | |
|
|
272 | my $map; |
|
|
273 | |
|
|
274 | for (@{ $mapa->{arch} }) { |
|
|
275 | my ($x, $y) = (delete $_->{x}, delete $_->{y}); |
|
|
276 | |
|
|
277 | if ($_->{_name} eq "map") { |
|
|
278 | $meta{info} = $_; |
|
|
279 | |
|
|
280 | $mapx = $_->{width} || $x; |
|
|
281 | $mapy = $_->{height} || $y; |
|
|
282 | } else { |
|
|
283 | push @{ $map->[$x][$y] }, $_; |
|
|
284 | |
|
|
285 | # arch map is unreliable w.r.t. width and height |
|
|
286 | $mapx = $x + 1 if $mapx <= $x; |
|
|
287 | $mapy = $y + 1 if $mapy <= $y; |
|
|
288 | #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; |
|
|
289 | #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; |
|
|
290 | } |
|
|
291 | } |
|
|
292 | |
|
|
293 | $meta{width} = $mapx; |
|
|
294 | $meta{height} = $mapy; |
|
|
295 | $meta{map} = $map; |
|
|
296 | |
|
|
297 | \%meta |
|
|
298 | } |
|
|
299 | |
|
|
300 | sub init($) { |
|
|
301 | my ($cachedir) = @_; |
|
|
302 | |
|
|
303 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
304 | } |
589 | } |
305 | |
590 | |
306 | =head1 AUTHOR |
591 | =head1 AUTHOR |
307 | |
592 | |
308 | Marc Lehmann <schmorp@schmorp.de> |
593 | Marc Lehmann <schmorp@schmorp.de> |