… | |
… | |
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"; |
… | |
… | |
103 | } |
105 | } |
104 | |
106 | |
105 | ############################################################################# |
107 | ############################################################################# |
106 | # "new" plug-in system |
108 | # "new" plug-in system |
107 | |
109 | |
108 | =item cf::object::attach ... # NYI |
110 | =item $object->attach ($attachment, ...) |
|
|
111 | |
|
|
112 | Attach a pre-registered attachment to an object. |
|
|
113 | |
|
|
114 | =item $player->attach ($attachment, ...) |
|
|
115 | |
|
|
116 | Attach a pre-registered attachment to a player. |
|
|
117 | |
|
|
118 | =item $map->attach ($attachment, ...) # not yet persistent |
|
|
119 | |
|
|
120 | Attach a pre-registered attachment to a map. |
109 | |
121 | |
110 | =item cf::attach_global ... |
122 | =item cf::attach_global ... |
111 | |
123 | |
|
|
124 | Attach handlers for global events. |
|
|
125 | |
|
|
126 | This and all following C<attach_*>-functions expect any number of the |
|
|
127 | following handler/hook descriptions: |
|
|
128 | |
|
|
129 | =over 4 |
|
|
130 | |
|
|
131 | =item prio => $number |
|
|
132 | |
|
|
133 | Set the priority for all following handlers/hooks (unless overwritten |
|
|
134 | by another C<prio> setting). Lower priority handlers get executed |
|
|
135 | earlier. The default priority is C<0>, and many built-in handlers are |
|
|
136 | registered at priority C<-1000>, so lower priorities should not be used |
|
|
137 | unless you know what you are doing. |
|
|
138 | |
|
|
139 | =item on_I<event> => \&cb |
|
|
140 | |
|
|
141 | Call the given code reference whenever the named event happens (event is |
|
|
142 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
|
|
143 | handlers are recognised generally depends on the type of object these |
|
|
144 | handlers attach to). |
|
|
145 | |
|
|
146 | See F<include/eventinc.h> for the full list of events supported, and their |
|
|
147 | class. |
|
|
148 | |
|
|
149 | =item package => package:: |
|
|
150 | |
|
|
151 | Look for sub functions of the name C<< on_I<event> >> in the given |
|
|
152 | package and register them. Only handlers for eevents supported by the |
|
|
153 | object/class are recognised. |
|
|
154 | |
|
|
155 | =back |
|
|
156 | |
112 | =item cf::attach_to_type $object_type, ... |
157 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
158 | |
|
|
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. |
113 | |
162 | |
114 | =item cf::attach_to_objects ... |
163 | =item cf::attach_to_objects ... |
115 | |
164 | |
|
|
165 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
166 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
167 | the game. |
|
|
168 | |
116 | =item cf::attach_to_players ... |
169 | =item cf::attach_to_players ... |
117 | |
170 | |
|
|
171 | Attach handlers to all players. |
|
|
172 | |
118 | =item cf::attach_to_maps ... |
173 | =item cf::attach_to_maps ... |
119 | |
174 | |
|
|
175 | Attach handlers to all maps. |
|
|
176 | |
120 | =item cf:register_attachment $name, ... |
177 | =item cf:register_attachment $name, ... |
121 | |
|
|
122 | prio => $number, # lower is earlier |
|
|
123 | on_xxx => \&cb, |
|
|
124 | package => package::, |
|
|
125 | |
178 | |
126 | =cut |
179 | =cut |
127 | |
180 | |
128 | # the following variables are defined in .xs and must not be re-created |
181 | # the following variables are defined in .xs and must not be re-created |
129 | our @CB_GLOBAL = (); # registry for all global events |
182 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
191 | } |
244 | } |
192 | |
245 | |
193 | \%undo |
246 | \%undo |
194 | } |
247 | } |
195 | |
248 | |
|
|
249 | sub _attach_attachment { |
|
|
250 | my ($obj, $name, @args) = @_; |
|
|
251 | |
|
|
252 | my $res; |
|
|
253 | |
|
|
254 | if (my $attach = $attachment{$name}) { |
|
|
255 | my $registry = $obj->registry; |
|
|
256 | |
|
|
257 | for (@$attach) { |
|
|
258 | my ($klass, @attach) = @$_; |
|
|
259 | $res = _attach @$registry, $klass, @attach; |
|
|
260 | } |
|
|
261 | |
|
|
262 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
|
|
263 | for (@$cb) { |
|
|
264 | eval { $_->[1]->($obj, @args); }; |
|
|
265 | if ($@) { |
|
|
266 | warn "$@"; |
|
|
267 | warn "... while processing '$name' instantiate with args <@args>.\n"; |
|
|
268 | } |
|
|
269 | } |
|
|
270 | } |
|
|
271 | } else { |
|
|
272 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
273 | } |
|
|
274 | |
|
|
275 | push @{$obj->{_attachment}}, $name; |
|
|
276 | |
|
|
277 | $res->{attachment} = $name; |
|
|
278 | $res |
|
|
279 | } |
|
|
280 | |
196 | sub cf::object::attach { |
281 | sub cf::object::attach { |
197 | die; |
282 | my ($obj, $name, @args) = @_; |
|
|
283 | |
|
|
284 | _attach_attachment $obj, $name, @args; |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub cf::player::attach { |
|
|
288 | my ($obj, $name, @args) = @_; |
|
|
289 | |
|
|
290 | _attach_attachment KLASS_PLAYER, $obj, $name, @args; |
|
|
291 | } |
|
|
292 | |
|
|
293 | sub cf::map::attach { |
|
|
294 | my ($obj, $name, @args) = @_; |
|
|
295 | |
|
|
296 | _attach_attachment KLASS_MAP, $obj, $name, @args; |
198 | } |
297 | } |
199 | |
298 | |
200 | sub attach_global { |
299 | sub attach_global { |
201 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
202 | } |
301 | } |
203 | |
302 | |
204 | sub attach_to_type { |
303 | sub attach_to_type { |
205 | my $type = shift; |
304 | my $type = shift; |
|
|
305 | my $subtype = shift; |
206 | |
306 | |
207 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
307 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
208 | } |
308 | } |
209 | |
309 | |
210 | sub attach_to_objects { |
310 | sub attach_to_objects { |
211 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
311 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
212 | } |
312 | } |
… | |
… | |
220 | } |
320 | } |
221 | |
321 | |
222 | sub register_attachment { |
322 | sub register_attachment { |
223 | my $name = shift; |
323 | my $name = shift; |
224 | |
324 | |
225 | $attachment{$name} = [@_]; |
325 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
226 | } |
326 | } |
227 | |
327 | |
228 | our $override; |
328 | our $override; |
229 | 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? |
230 | |
330 | |
… | |
… | |
259 | ############################################################################# |
359 | ############################################################################# |
260 | # object support |
360 | # object support |
261 | |
361 | |
262 | sub instantiate { |
362 | sub instantiate { |
263 | my ($obj, $data) = @_; |
363 | my ($obj, $data) = @_; |
264 | my $registry = $obj->registry; |
|
|
265 | |
364 | |
266 | $data = from_json $data; |
365 | $data = from_json $data; |
267 | |
366 | |
268 | for (@$data) { |
367 | for (@$data) { |
269 | my ($pri, $name, @args) = @$_; |
368 | my ($name, $args) = @$_; |
270 | |
369 | attach $obj, $name, @{$args || [] }; |
271 | if (my $attach = $attachment{$name}) { |
|
|
272 | _attach @$registry, KLASS_OBJECT, @$attach; |
|
|
273 | |
|
|
274 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
|
|
275 | for (@$cb) { |
|
|
276 | eval { $_->[1]->($obj, @args); }; |
|
|
277 | if ($@) { |
|
|
278 | warn "$@"; |
|
|
279 | warn "... while processing '$name' instantiate with args <@args>\n"; |
|
|
280 | } |
|
|
281 | } |
|
|
282 | } |
|
|
283 | } else { |
|
|
284 | warn "object uses attachment $name that is not available, postponing.\n"; |
|
|
285 | } |
|
|
286 | |
|
|
287 | push @{$obj->{_attachment}}, $name; |
|
|
288 | } |
370 | } |
289 | } |
371 | } |
290 | |
372 | |
291 | # basically do the same as instantiate, without calling instantiate |
373 | # basically do the same as instantiate, without calling instantiate |
292 | sub reattach { |
374 | sub reattach { |
293 | my ($obj) = @_; |
375 | my ($obj) = @_; |
294 | my $registry = $obj->registry; |
376 | my $registry = $obj->registry; |
295 | |
377 | |
296 | warn "reattach<@_, $_>\n"; |
378 | @$registry = (); |
|
|
379 | |
|
|
380 | for my $name (@{ $obj->{_attachment} }) { |
|
|
381 | if (my $attach = $attachment{$name}) { |
|
|
382 | for (@$attach) { |
|
|
383 | my ($klass, @attach) = @$_; |
|
|
384 | _attach @$registry, $klass, @attach; |
|
|
385 | } |
|
|
386 | } else { |
|
|
387 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
388 | } |
|
|
389 | } |
|
|
390 | } |
|
|
391 | |
|
|
392 | sub object_freezer_save { |
|
|
393 | my ($filename, $objs) = @_; |
|
|
394 | |
|
|
395 | $filename .= ".pst"; |
|
|
396 | |
|
|
397 | if (@$objs) { |
|
|
398 | open my $fh, ">:raw", "$filename~"; |
|
|
399 | chmod $fh, SAVE_MODE; |
|
|
400 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
401 | close $fh; |
|
|
402 | rename "$filename~", $filename; |
|
|
403 | } else { |
|
|
404 | unlink $filename; |
|
|
405 | } |
|
|
406 | } |
|
|
407 | |
|
|
408 | sub object_thawer_load { |
|
|
409 | my ($filename) = @_; |
|
|
410 | |
|
|
411 | open my $fh, "<:raw:perlio", "$filename.pst" |
|
|
412 | or return; |
|
|
413 | |
|
|
414 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
297 | } |
415 | } |
298 | |
416 | |
299 | attach_to_objects |
417 | attach_to_objects |
300 | prio => -1000000, |
418 | prio => -1000000, |
301 | on_clone => sub { |
419 | on_clone => sub { |
302 | my ($src, $dst) = @_; |
420 | my ($src, $dst) = @_; |
303 | |
421 | |
304 | @{$dst->registry} = @{$src->registry}; |
422 | @{$dst->registry} = @{$src->registry}; |
305 | warn "registry clone ", join ":", @{$src->registry};#d# |
|
|
306 | |
423 | |
307 | %$dst = %$src; |
424 | %$dst = %$src; |
308 | |
425 | |
309 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
426 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
310 | if exists $src->{_attachment}; |
427 | if exists $src->{_attachment}; |
311 | |
|
|
312 | warn "clone<@_>\n";#d# |
|
|
313 | }, |
428 | }, |
314 | ; |
429 | ; |
315 | |
430 | |
316 | ############################################################################# |
431 | ############################################################################# |
317 | # old plug-in events |
432 | # old plug-in events |
… | |
… | |
558 | |
673 | |
559 | my $path = $map->tmpname; |
674 | my $path = $map->tmpname; |
560 | defined $path or return; |
675 | defined $path or return; |
561 | |
676 | |
562 | unlink "$path.cfperl"; |
677 | unlink "$path.cfperl"; |
|
|
678 | unlink "$path.pst"; |
563 | }; |
679 | }; |
564 | |
680 | |
565 | *cf::mapsupport::on_swapin = |
681 | *cf::mapsupport::on_swapin = |
566 | *cf::mapsupport::on_load = sub { |
682 | *cf::mapsupport::on_load = sub { |
567 | my ($map) = @_; |
683 | my ($map) = @_; |
… | |
… | |
578 | or return; # too new |
694 | or return; # too new |
579 | |
695 | |
580 | $map->_set_obs ($data->{obs}); |
696 | $map->_set_obs ($data->{obs}); |
581 | }; |
697 | }; |
582 | |
698 | |
583 | *cf::mapsupport::on_swapout = sub { |
|
|
584 | my ($map) = @_; |
|
|
585 | |
|
|
586 | my $path = $map->tmpname; |
|
|
587 | $path = $map->path unless defined $path; |
|
|
588 | |
|
|
589 | my $obs = $map->_get_obs; |
|
|
590 | |
|
|
591 | if (defined $obs) { |
|
|
592 | open my $fh, ">:raw", "$path.cfperl" |
|
|
593 | or die "$path.cfperl: $!"; |
|
|
594 | |
|
|
595 | stat $path; |
|
|
596 | |
|
|
597 | print $fh Storable::nfreeze { |
|
|
598 | size => (stat _)[7], |
|
|
599 | time => (stat _)[9], |
|
|
600 | version => 1, |
|
|
601 | obs => $obs, |
|
|
602 | }; |
|
|
603 | |
|
|
604 | chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* |
|
|
605 | } else { |
|
|
606 | unlink "$path.cfperl"; |
|
|
607 | } |
|
|
608 | }; |
|
|
609 | |
|
|
610 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
699 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
611 | |
700 | |
612 | ############################################################################# |
701 | ############################################################################# |
613 | # load/save perl data associated with player->ob objects |
702 | # load/save perl data associated with player->ob objects |
614 | |
703 | |
… | |
… | |
625 | $o->set_ob_key_value ("_perl_data"); |
714 | $o->set_ob_key_value ("_perl_data"); |
626 | |
715 | |
627 | %$o = %{ Storable::thaw pack "H*", $value }; |
716 | %$o = %{ Storable::thaw pack "H*", $value }; |
628 | } |
717 | } |
629 | } |
718 | } |
630 | }, |
|
|
631 | on_save => sub { |
|
|
632 | my ($pl, $path) = @_; |
|
|
633 | |
|
|
634 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
|
|
635 | for grep %$_, all_objects $pl->ob; |
|
|
636 | }, |
719 | }, |
637 | ; |
720 | ; |
638 | |
721 | |
639 | ############################################################################# |
722 | ############################################################################# |
640 | # core extensions - in perl |
723 | # core extensions - in perl |
… | |
… | |
779 | $TICK_WATCHER->at ($NEXT_TICK); |
862 | $TICK_WATCHER->at ($NEXT_TICK); |
780 | $TICK_WATCHER->start; |
863 | $TICK_WATCHER->start; |
781 | }, |
864 | }, |
782 | ); |
865 | ); |
783 | |
866 | |
|
|
867 | _reload_2; |
|
|
868 | |
784 | 1 |
869 | 1 |
785 | |
870 | |