… | |
… | |
4 | |
4 | |
5 | =cut |
5 | =cut |
6 | |
6 | |
7 | package Crossfire; |
7 | package Crossfire; |
8 | |
8 | |
9 | our $VERSION = '0.1'; |
9 | our $VERSION = '0.9'; |
10 | |
10 | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
… | |
… | |
18 | use Storable qw(freeze thaw); |
18 | use Storable qw(freeze thaw); |
19 | |
19 | |
20 | our @EXPORT = qw( |
20 | our @EXPORT = qw( |
21 | read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
21 | read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
22 | ); |
22 | ); |
|
|
23 | |
|
|
24 | use JSON::Syck (); #TODO#d# replace by JSON::PC when it becomes available == working |
|
|
25 | |
|
|
26 | sub from_json($) { |
|
|
27 | $JSON::Syck::ImplicitUnicode = 1; |
|
|
28 | JSON::Syck::Load $_[0] |
|
|
29 | } |
|
|
30 | |
|
|
31 | sub to_json($) { |
|
|
32 | $JSON::Syck::ImplicitUnicode = 0; |
|
|
33 | JSON::Syck::Dump $_[0] |
|
|
34 | } |
23 | |
35 | |
24 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
36 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
25 | |
37 | |
26 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
38 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
27 | |
39 | |
… | |
… | |
44 | qw(move_type move_block move_allow move_on move_off move_slow); |
56 | qw(move_type move_block move_allow move_on move_off move_slow); |
45 | |
57 | |
46 | # same as in server save routine, to (hopefully) be compatible |
58 | # same as in server save routine, to (hopefully) be compatible |
47 | # to the other editors. |
59 | # to the other editors. |
48 | our @FIELD_ORDER_MAP = (qw( |
60 | our @FIELD_ORDER_MAP = (qw( |
49 | name swap_time reset_timeout fixed_resettime difficulty region |
61 | name attach swap_time reset_timeout fixed_resettime difficulty region |
50 | shopitems shopgreed shopmin shopmax shoprace |
62 | shopitems shopgreed shopmin shopmax shoprace |
51 | darkness width height enter_x enter_y msg maplore |
63 | darkness width height enter_x enter_y msg maplore |
52 | unique template |
64 | unique template |
53 | outdoor temp pressure humid windspeed winddir sky nosmooth |
65 | outdoor temp pressure humid windspeed winddir sky nosmooth |
54 | tile_path_1 tile_path_2 tile_path_3 tile_path_4 |
66 | tile_path_1 tile_path_2 tile_path_3 tile_path_4 |
55 | )); |
67 | )); |
56 | |
68 | |
57 | our @FIELD_ORDER = (qw( |
69 | our @FIELD_ORDER = (qw( |
58 | elevation |
70 | elevation |
59 | |
71 | |
60 | name name_pl custom_name title race |
72 | name name_pl custom_name attach title race |
61 | slaying skill msg lore other_arch face |
73 | slaying skill msg lore other_arch face |
62 | #todo-events |
74 | #todo-events |
63 | animation is_animated |
75 | animation is_animated |
64 | str dex con wis pow cha int |
76 | str dex con wis pow cha int |
65 | hp maxhp sp maxsp grace maxgrace |
77 | hp maxhp sp maxsp grace maxgrace |
… | |
… | |
102 | |
114 | |
103 | body_range body_arm body_torso body_head body_neck body_skill |
115 | body_range body_arm body_torso body_head body_neck body_skill |
104 | body_finger body_shoulder body_foot body_hand body_wrist body_waist |
116 | body_finger body_shoulder body_foot body_hand body_wrist body_waist |
105 | )); |
117 | )); |
106 | |
118 | |
|
|
119 | our %EVENT_TYPE = ( |
|
|
120 | apply => 1, |
|
|
121 | attack => 2, |
|
|
122 | death => 3, |
|
|
123 | drop => 4, |
|
|
124 | pickup => 5, |
|
|
125 | say => 6, |
|
|
126 | stop => 7, |
|
|
127 | time => 8, |
|
|
128 | throw => 9, |
|
|
129 | trigger => 10, |
|
|
130 | close => 11, |
|
|
131 | timer => 12, |
|
|
132 | ); |
|
|
133 | |
107 | sub MOVE_WALK (){ 0x01 } |
134 | sub MOVE_WALK (){ 0x01 } |
108 | sub MOVE_FLY_LOW (){ 0x02 } |
135 | sub MOVE_FLY_LOW (){ 0x02 } |
109 | sub MOVE_FLY_HIGH (){ 0x04 } |
136 | sub MOVE_FLY_HIGH (){ 0x04 } |
110 | sub MOVE_FLYING (){ 0x06 } |
137 | sub MOVE_FLYING (){ 0x06 } |
111 | sub MOVE_SWIM (){ 0x08 } |
138 | sub MOVE_SWIM (){ 0x08 } |
… | |
… | |
115 | sub MOVE_ALL (){ 0x1001f } # very special value, more PITA |
142 | sub MOVE_ALL (){ 0x1001f } # very special value, more PITA |
116 | |
143 | |
117 | sub load_ref($) { |
144 | sub load_ref($) { |
118 | my ($path) = @_; |
145 | my ($path) = @_; |
119 | |
146 | |
120 | open my $fh, "<", $path |
147 | open my $fh, "<:raw:perlio", $path |
121 | or die "$path: $!"; |
148 | or die "$path: $!"; |
122 | binmode $fh; |
|
|
123 | local $/; |
149 | local $/; |
124 | |
150 | |
125 | thaw <$fh> |
151 | thaw <$fh> |
126 | } |
152 | } |
127 | |
153 | |
128 | sub save_ref($$) { |
154 | sub save_ref($$) { |
129 | my ($ref, $path) = @_; |
155 | my ($ref, $path) = @_; |
130 | |
156 | |
131 | open my $fh, ">", "$path~" |
157 | open my $fh, ">:raw:perlio", "$path~" |
132 | or die "$path~: $!"; |
158 | or die "$path~: $!"; |
133 | binmode $fh; |
|
|
134 | print $fh freeze $ref; |
159 | print $fh freeze $ref; |
135 | close $fh; |
160 | close $fh; |
136 | rename "$path~", $path |
161 | rename "$path~", $path |
137 | or die "$path: $!"; |
162 | or die "$path: $!"; |
138 | } |
163 | } |
139 | |
164 | |
|
|
165 | # object as in "Object xxx", i.e. archetypes |
140 | sub normalize_object($) { |
166 | sub normalize_object($) { |
141 | my ($ob) = @_; |
167 | my ($ob) = @_; |
142 | |
168 | |
|
|
169 | # nuke outdated or never supported fields |
143 | delete $ob->{$_} for qw( |
170 | delete $ob->{$_} for qw( |
144 | can_knockback can_parry can_impale can_cut can_dam_armour |
171 | can_knockback can_parry can_impale can_cut can_dam_armour |
145 | can_apply pass_thru can_pass_thru |
172 | can_apply pass_thru can_pass_thru |
146 | ); |
173 | ); |
147 | |
174 | |
|
|
175 | # convert movement strings to bitsets |
148 | for my $attr (keys %FIELD_MOVEMENT) { |
176 | for my $attr (keys %FIELD_MOVEMENT) { |
149 | next unless exists $ob->{$attr}; |
177 | next unless exists $ob->{$attr}; |
150 | |
178 | |
151 | $ob->{$attr} = MOVE_ALL if $ob->{$attr} == 255; #d# compatibility |
179 | $ob->{$attr} = MOVE_ALL if $ob->{$attr} == 255; #d# compatibility |
152 | |
180 | |
… | |
… | |
174 | } |
202 | } |
175 | |
203 | |
176 | $ob->{$attr} = $flags; |
204 | $ob->{$attr} = $flags; |
177 | } |
205 | } |
178 | |
206 | |
|
|
207 | # convert outdated movement flags to new movement sets |
179 | if (defined (my $v = delete $ob->{no_pass})) { |
208 | if (defined (my $v = delete $ob->{no_pass})) { |
180 | $ob->{move_block} = $v ? MOVE_ALL : 0; |
209 | $ob->{move_block} = $v ? MOVE_ALL : 0; |
181 | } |
210 | } |
182 | if (defined (my $v = delete $ob->{slow_move})) { |
211 | if (defined (my $v = delete $ob->{slow_move})) { |
183 | $ob->{move_slow} |= MOVE_WALK; |
212 | $ob->{move_slow} |= MOVE_WALK; |
184 | $ob->{move_slow_penalty} = $v; |
213 | $ob->{move_slow_penalty} = $v; |
185 | } |
214 | } |
186 | if (defined (my $v = delete $ob->{walk_on})) { |
215 | if (defined (my $v = delete $ob->{walk_on})) { |
|
|
216 | $ob->{move_on} = MOVE_ALL unless exists $ob->{move_on}; |
187 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
217 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK |
188 | : $ob->{move_on} & ~MOVE_WALK; |
218 | : $ob->{move_on} & ~MOVE_WALK; |
189 | } |
219 | } |
190 | if (defined (my $v = delete $ob->{walk_off})) { |
220 | if (defined (my $v = delete $ob->{walk_off})) { |
|
|
221 | $ob->{move_off} = MOVE_ALL unless exists $ob->{move_off}; |
191 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
222 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK |
192 | : $ob->{move_off} & ~MOVE_WALK; |
223 | : $ob->{move_off} & ~MOVE_WALK; |
193 | } |
224 | } |
194 | if (defined (my $v = delete $ob->{fly_on})) { |
225 | if (defined (my $v = delete $ob->{fly_on})) { |
|
|
226 | $ob->{move_on} = MOVE_ALL unless exists $ob->{move_on}; |
195 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
227 | $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW |
196 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
228 | : $ob->{move_on} & ~MOVE_FLY_LOW; |
197 | } |
229 | } |
198 | if (defined (my $v = delete $ob->{fly_off})) { |
230 | if (defined (my $v = delete $ob->{fly_off})) { |
|
|
231 | $ob->{move_off} = MOVE_ALL unless exists $ob->{move_off}; |
199 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
232 | $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW |
200 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
233 | : $ob->{move_off} & ~MOVE_FLY_LOW; |
201 | } |
234 | } |
202 | if (defined (my $v = delete $ob->{flying})) { |
235 | if (defined (my $v = delete $ob->{flying})) { |
|
|
236 | $ob->{move_type} = MOVE_ALL unless exists $ob->{move_type}; |
203 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
237 | $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW |
204 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
238 | : $ob->{move_type} & ~MOVE_FLY_LOW; |
205 | } |
239 | } |
206 | |
240 | |
|
|
241 | # convert idiotic event_xxx things into objects |
|
|
242 | while (my ($event, $subtype) = each %EVENT_TYPE) { |
|
|
243 | if (exists $ob->{"event_${event}_plugin"}) { |
|
|
244 | push @{$ob->{inventory}}, { |
|
|
245 | _name => "event_$event", |
|
|
246 | title => delete $ob->{"event_${event}_plugin"}, |
|
|
247 | slaying => delete $ob->{"event_${event}"}, |
|
|
248 | name => delete $ob->{"event_${event}_options"}, |
|
|
249 | }; |
|
|
250 | } |
|
|
251 | } |
|
|
252 | |
207 | $ob |
253 | $ob |
208 | } |
254 | } |
209 | |
255 | |
|
|
256 | # arch as in "arch xxx", ie.. objects |
210 | sub normalize_arch($) { |
257 | sub normalize_arch($) { |
211 | my ($ob) = @_; |
258 | my ($ob) = @_; |
212 | |
259 | |
213 | normalize_object $ob; |
260 | normalize_object $ob; |
214 | |
261 | |
… | |
… | |
251 | } |
298 | } |
252 | |
299 | |
253 | $ob |
300 | $ob |
254 | } |
301 | } |
255 | |
302 | |
|
|
303 | sub attr_thaw($) { |
|
|
304 | my ($ob) = @_; |
|
|
305 | |
|
|
306 | $ob->{attach} = from_json $ob->{attach} |
|
|
307 | if exists $ob->{attach}; |
|
|
308 | |
|
|
309 | $ob |
|
|
310 | } |
|
|
311 | |
|
|
312 | sub attr_freeze($) { |
|
|
313 | my ($ob) = @_; |
|
|
314 | |
|
|
315 | $ob->{attach} = Crossfire::to_json $ob->{attach} |
|
|
316 | if exists $ob->{attach}; |
|
|
317 | |
|
|
318 | $ob |
|
|
319 | } |
|
|
320 | |
256 | sub read_pak($) { |
321 | sub read_pak($) { |
257 | my ($path) = @_; |
322 | my ($path) = @_; |
258 | |
323 | |
259 | my %pak; |
324 | my %pak; |
260 | |
325 | |
261 | open my $fh, "<", $path |
326 | open my $fh, "<:raw:perlio", $path |
262 | or Carp::croak "$_[0]: $!"; |
327 | or Carp::croak "$_[0]: $!"; |
263 | binmode $fh; |
328 | binmode $fh; |
264 | while (<$fh>) { |
329 | while (<$fh>) { |
265 | my ($type, $id, $len, $path) = split; |
330 | my ($type, $id, $len, $path) = split; |
266 | $path =~ s/.*\///; |
331 | $path =~ s/.*\///; |
… | |
… | |
268 | } |
333 | } |
269 | |
334 | |
270 | \%pak |
335 | \%pak |
271 | } |
336 | } |
272 | |
337 | |
273 | sub read_arch($) { |
338 | sub read_arch($;$) { |
274 | my ($path) = @_; |
339 | my ($path, $toplevel) = @_; |
275 | |
340 | |
276 | my %arc; |
341 | my %arc; |
277 | my ($more, $prev); |
342 | my ($more, $prev); |
278 | |
343 | |
279 | open my $fh, "<", $path |
344 | open my $fh, "<:raw:perlio:utf8", $path |
280 | or Carp::croak "$path: $!"; |
345 | or Carp::croak "$path: $!"; |
281 | |
346 | |
282 | binmode $fh; |
347 | # binmode $fh; |
283 | |
348 | |
284 | my $parse_block; $parse_block = sub { |
349 | my $parse_block; $parse_block = sub { |
285 | my %arc = @_; |
350 | my %arc = @_; |
286 | |
351 | |
287 | while (<$fh>) { |
352 | while (<$fh>) { |
288 | s/\s+$//; |
353 | s/\s+$//; |
289 | if (/^end$/i) { |
354 | if (/^end$/i) { |
290 | last; |
355 | last; |
291 | } elsif (/^arch (\S+)$/i) { |
356 | } elsif (/^arch (\S+)$/i) { |
292 | push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1); |
357 | push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1); |
293 | } elsif (/^lore$/i) { |
358 | } elsif (/^lore$/i) { |
294 | while (<$fh>) { |
359 | while (<$fh>) { |
295 | last if /^endlore\s*$/i; |
360 | last if /^endlore\s*$/i; |
296 | $arc{lore} .= $_; |
361 | $arc{lore} .= $_; |
297 | } |
362 | } |
298 | } elsif (/^msg$/i) { |
363 | } elsif (/^msg$/i) { |
299 | while (<$fh>) { |
364 | while (<$fh>) { |
300 | last if /^endmsg\s*$/i; |
365 | last if /^endmsg\s*$/i; |
301 | $arc{msg} .= $_; |
366 | $arc{msg} .= $_; |
302 | } |
367 | } |
|
|
368 | } elsif (/^anim$/i) { |
|
|
369 | while (<$fh>) { |
|
|
370 | last if /^mina\s*$/i; |
|
|
371 | chomp; |
|
|
372 | push @{ $arc{anim} }, $_; |
|
|
373 | } |
303 | } elsif (/^(\S+)\s*(.*)$/) { |
374 | } elsif (/^(\S+)\s*(.*)$/) { |
304 | $arc{lc $1} = $2; |
375 | $arc{lc $1} = $2; |
305 | } elsif (/^\s*($|#)/) { |
376 | } elsif (/^\s*($|#)/) { |
306 | # |
377 | # |
307 | } else { |
378 | } else { |
… | |
… | |
316 | s/\s+$//; |
387 | s/\s+$//; |
317 | if (/^more$/i) { |
388 | if (/^more$/i) { |
318 | $more = $prev; |
389 | $more = $prev; |
319 | } elsif (/^object (\S+)$/i) { |
390 | } elsif (/^object (\S+)$/i) { |
320 | my $name = $1; |
391 | my $name = $1; |
321 | my $arc = normalize_object $parse_block->(_name => $name); |
392 | my $arc = attr_thaw normalize_object $parse_block->(_name => $name); |
322 | |
393 | |
323 | if ($more) { |
394 | if ($more) { |
324 | $more->{more} = $arc; |
395 | $more->{more} = $arc; |
325 | } else { |
396 | } else { |
326 | $arc{$name} = $arc; |
397 | $arc{$name} = $arc; |
327 | } |
398 | } |
328 | $prev = $arc; |
399 | $prev = $arc; |
329 | $more = undef; |
400 | $more = undef; |
330 | } elsif (/^arch (\S+)$/i) { |
401 | } elsif (/^arch (\S+)$/i) { |
331 | my $name = $1; |
402 | my $name = $1; |
332 | my $arc = normalize_arch $parse_block->(_name => $name); |
403 | my $arc = attr_thaw normalize_arch $parse_block->(_name => $name); |
333 | |
404 | |
334 | if ($more) { |
405 | if ($more) { |
335 | $more->{more} = $arc; |
406 | $more->{more} = $arc; |
336 | } else { |
407 | } else { |
337 | push @{ $arc{arch} }, $arc; |
408 | push @{ $arc{arch} }, $arc; |
338 | } |
409 | } |
339 | $prev = $arc; |
410 | $prev = $arc; |
340 | $more = undef; |
411 | $more = undef; |
|
|
412 | } elsif ($toplevel && /^(\S+)\s+(.*)$/) { |
|
|
413 | if ($1 eq "lev_array") { |
|
|
414 | while (<$fh>) { |
|
|
415 | last if /^endplst\s*$/; |
|
|
416 | push @{$toplevel->{lev_array}}, $_+0; |
|
|
417 | } |
|
|
418 | } else { |
|
|
419 | $toplevel->{$1} = $2; |
|
|
420 | } |
341 | } elsif (/^\s*($|#)/) { |
421 | } elsif (/^\s*($|#)/) { |
342 | # |
422 | # |
343 | } else { |
423 | } else { |
344 | warn "$path: unparseable top-level line '$_'"; |
424 | die "$path: unparseable top-level line '$_'"; |
345 | } |
425 | } |
346 | } |
426 | } |
347 | |
427 | |
348 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
428 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
349 | |
429 | |
… | |
… | |
356 | sub editor_archs { |
436 | sub editor_archs { |
357 | my %paths; |
437 | my %paths; |
358 | |
438 | |
359 | for (keys %ARCH) { |
439 | for (keys %ARCH) { |
360 | my $arch = $ARCH{$_}; |
440 | my $arch = $ARCH{$_}; |
361 | push @{$paths{$arch->{editor_folder}}}, \$arch; |
441 | push @{$paths{$arch->{editor_folder}}}, $arch; |
362 | } |
442 | } |
363 | |
443 | |
364 | \%paths |
444 | \%paths |
365 | } |
445 | } |
366 | |
446 | |