… | |
… | |
11 | use Event; |
11 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
|
|
16 | _reload_1; |
|
|
17 | |
16 | our %COMMAND = (); |
18 | our %COMMAND = (); |
17 | our @EVENT; |
19 | our @EVENT; |
18 | our %PROP_TYPE; |
20 | our %PROP_TYPE; |
19 | our %PROP_IDX; |
21 | our %PROP_IDX; |
20 | our $LIBDIR = maps_directory "perl"; |
22 | our $LIBDIR = maps_directory "perl"; |
… | |
… | |
150 | package and register them. Only handlers for eevents supported by the |
152 | package and register them. Only handlers for eevents supported by the |
151 | object/class are recognised. |
153 | object/class are recognised. |
152 | |
154 | |
153 | =back |
155 | =back |
154 | |
156 | |
155 | =item cf::attach_to_type $object_type, ... |
157 | =item cf::attach_to_type $object_type, $subtype, ... |
156 | |
158 | |
157 | Attach handlers for a specific object type (e.g. TRANSPORT). |
159 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
160 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
161 | type. |
158 | |
162 | |
159 | =item cf::attach_to_objects ... |
163 | =item cf::attach_to_objects ... |
160 | |
164 | |
161 | Attach handlers to all objects. Do not use this except for debugging or |
165 | Attach handlers to all objects. Do not use this except for debugging or |
162 | very rare events, as handlers are (obviously) called for I<all> objects in |
166 | very rare events, as handlers are (obviously) called for I<all> objects in |
… | |
… | |
241 | |
245 | |
242 | \%undo |
246 | \%undo |
243 | } |
247 | } |
244 | |
248 | |
245 | sub _attach_attachment { |
249 | sub _attach_attachment { |
246 | my ($klass, $obj, $name, @args) = q_; |
250 | my ($obj, $name, @args) = @_; |
247 | |
251 | |
248 | my $res; |
252 | my $res; |
249 | |
253 | |
250 | if (my $attach = $attachment{$name}) { |
254 | if (my $attach = $attachment{$name}) { |
251 | my $registry = $obj->registry; |
255 | my $registry = $obj->registry; |
252 | |
256 | |
|
|
257 | for (@$attach) { |
|
|
258 | my ($klass, @attach) = @$_; |
253 | $res = _attach @$registry, $klass, @$attach; |
259 | $res = _attach @$registry, $klass, @attach; |
|
|
260 | } |
254 | |
261 | |
255 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
262 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
256 | for (@$cb) { |
263 | for (@$cb) { |
257 | eval { $_->[1]->($obj, @args); }; |
264 | eval { $_->[1]->($obj, @args); }; |
258 | if ($@) { |
265 | if ($@) { |
… | |
… | |
272 | } |
279 | } |
273 | |
280 | |
274 | sub cf::object::attach { |
281 | sub cf::object::attach { |
275 | my ($obj, $name, @args) = @_; |
282 | my ($obj, $name, @args) = @_; |
276 | |
283 | |
277 | _attach_attachment KLASS_OBJECT, $obj, $name, @args; |
284 | _attach_attachment $obj, $name, @args; |
278 | } |
285 | } |
279 | |
286 | |
280 | sub cf::player::attach { |
287 | sub cf::player::attach { |
281 | my ($obj, $name, @args) = @_; |
288 | my ($obj, $name, @args) = @_; |
282 | |
289 | |
… | |
… | |
293 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
294 | } |
301 | } |
295 | |
302 | |
296 | sub attach_to_type { |
303 | sub attach_to_type { |
297 | my $type = shift; |
304 | my $type = shift; |
|
|
305 | my $subtype = shift; |
298 | |
306 | |
299 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
307 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
300 | } |
308 | } |
301 | |
309 | |
302 | sub attach_to_objects { |
310 | sub attach_to_objects { |
303 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
311 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
304 | } |
312 | } |
… | |
… | |
312 | } |
320 | } |
313 | |
321 | |
314 | sub register_attachment { |
322 | sub register_attachment { |
315 | my $name = shift; |
323 | my $name = shift; |
316 | |
324 | |
317 | $attachment{$name} = [@_]; |
325 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
318 | } |
326 | } |
319 | |
327 | |
320 | our $override; |
328 | our $override; |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
329 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
322 | |
330 | |
… | |
… | |
362 | } |
370 | } |
363 | } |
371 | } |
364 | |
372 | |
365 | # basically do the same as instantiate, without calling instantiate |
373 | # basically do the same as instantiate, without calling instantiate |
366 | sub reattach { |
374 | sub reattach { |
367 | warn "reattach<@_>\n";#d# |
|
|
368 | my ($obj) = @_; |
375 | my ($obj) = @_; |
369 | my $registry = $obj->registry; |
376 | my $registry = $obj->registry; |
370 | |
377 | |
|
|
378 | @$registry = (); |
|
|
379 | |
371 | for my $name (@{ $obj->{_attachment} }) { |
380 | for my $name (@{ $obj->{_attachment} }) { |
372 | if (my $attach = $attachment{$name}) { |
381 | if (my $attach = $attachment{$name}) { |
|
|
382 | for (@$attach) { |
|
|
383 | my ($klass, @attach) = @$_; |
373 | _attach @$registry, KLASS_OBJECT, @$attach; |
384 | _attach @$registry, $klass, @attach; |
|
|
385 | } |
374 | } else { |
386 | } else { |
375 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
387 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
376 | } |
388 | } |
377 | } |
389 | } |
378 | |
|
|
379 | warn "reattach<@_, $_>\n"; |
|
|
380 | } |
390 | } |
381 | |
391 | |
382 | sub object_freezer_save { |
392 | sub object_freezer_save { |
383 | my ($filename, $objs) = @_; |
393 | my ($filename, $objs) = @_; |
384 | warn "freeze $filename\n";#d# |
|
|
385 | use Data::Dumper; print Dumper $objs; |
|
|
386 | |
394 | |
387 | $filename .= ".pst"; |
395 | $filename .= ".pst"; |
388 | |
396 | |
389 | if (@$objs) { |
397 | if (@$objs) { |
390 | open my $fh, ">:raw", "$filename~"; |
398 | open my $fh, ">:raw", "$filename~"; |
… | |
… | |
398 | } |
406 | } |
399 | |
407 | |
400 | sub object_thawer_load { |
408 | sub object_thawer_load { |
401 | my ($filename) = @_; |
409 | my ($filename) = @_; |
402 | |
410 | |
403 | warn "thaw $filename\n";#d# |
|
|
404 | |
|
|
405 | open my $fh, "<:raw:perlio", "$filename.pst" |
411 | open my $fh, "<:raw:perlio", "$filename.pst" |
406 | or return; |
412 | or return; |
407 | |
413 | |
408 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
414 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
409 | } |
415 | } |
… | |
… | |
412 | prio => -1000000, |
418 | prio => -1000000, |
413 | on_clone => sub { |
419 | on_clone => sub { |
414 | my ($src, $dst) = @_; |
420 | my ($src, $dst) = @_; |
415 | |
421 | |
416 | @{$dst->registry} = @{$src->registry}; |
422 | @{$dst->registry} = @{$src->registry}; |
417 | warn "registry clone ", join ":", @{$src->registry};#d# |
|
|
418 | |
423 | |
419 | %$dst = %$src; |
424 | %$dst = %$src; |
420 | |
425 | |
421 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
426 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
422 | if exists $src->{_attachment}; |
427 | if exists $src->{_attachment}; |
423 | |
|
|
424 | warn "clone<@_>\n";#d# |
|
|
425 | }, |
428 | }, |
426 | ; |
429 | ; |
427 | |
430 | |
428 | ############################################################################# |
431 | ############################################################################# |
429 | # old plug-in events |
432 | # old plug-in events |
… | |
… | |
859 | $TICK_WATCHER->at ($NEXT_TICK); |
862 | $TICK_WATCHER->at ($NEXT_TICK); |
860 | $TICK_WATCHER->start; |
863 | $TICK_WATCHER->start; |
861 | }, |
864 | }, |
862 | ); |
865 | ); |
863 | |
866 | |
|
|
867 | _reload_2; |
|
|
868 | |
864 | 1 |
869 | 1 |
865 | |
870 | |