… | |
… | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
17 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
18 | |
19 | use strict; |
19 | use strict; |
20 | |
20 | |
|
|
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
22 | |
21 | our %COMMAND = (); |
23 | our %COMMAND = (); |
22 | our %COMMAND_TIME = (); |
24 | our %COMMAND_TIME = (); |
23 | our %EXTCMD = (); |
25 | our %EXTCMD = (); |
24 | |
26 | |
25 | _init_vars; |
27 | _init_vars; |
26 | |
28 | |
27 | our @EVENT; |
29 | our @EVENT; |
28 | our $LIBDIR = maps_directory "perl"; |
30 | our $LIBDIR = datadir . "/ext"; |
29 | |
31 | |
30 | our $TICK = MAX_TIME * 1e-6; |
32 | our $TICK = MAX_TIME * 1e-6; |
31 | our $TICK_WATCHER; |
33 | our $TICK_WATCHER; |
32 | our $NEXT_TICK; |
34 | our $NEXT_TICK; |
33 | |
35 | |
… | |
… | |
72 | print STDERR "cfperl: $msg"; |
74 | print STDERR "cfperl: $msg"; |
73 | LOG llevError, "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
74 | }; |
76 | }; |
75 | } |
77 | } |
76 | |
78 | |
|
|
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
|
|
80 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
|
|
81 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
|
|
82 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
|
|
83 | @safe::cf::map::ISA = @cf::map::ISA = 'cf::attachable'; |
77 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
78 | |
85 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
87 | # within the Safe compartment. |
81 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
88 | for my $pkg (qw( |
|
|
89 | cf::global |
|
|
90 | cf::object cf::object::player |
|
|
91 | cf::client cf::player |
|
|
92 | cf::arch cf::living |
|
|
93 | cf::map cf::party cf::region |
|
|
94 | )) { |
82 | no strict 'refs'; |
95 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
96 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
97 | } |
85 | |
98 | |
86 | $Event::DIED = sub { |
99 | $Event::DIED = sub { |
… | |
… | |
125 | |
138 | |
126 | =cut |
139 | =cut |
127 | |
140 | |
128 | ############################################################################# |
141 | ############################################################################# |
129 | |
142 | |
130 | =head2 EVENTS AND OBJECT ATTACHMENTS |
143 | =head2 ATTACHABLE OBJECTS |
|
|
144 | |
|
|
145 | You can define and attach attachments to each "attachable" object in |
|
|
146 | crossfire+ (objects, players, clients, maps and the special "global" |
|
|
147 | class). In the following description, CLASS can be any of C<global>, |
|
|
148 | C<object> C<player>, C<client> or C<map>. |
131 | |
149 | |
132 | =over 4 |
150 | =over 4 |
133 | |
151 | |
134 | =item $object->attach ($attachment, key => $value...) |
152 | =item cf::CLASS::attachment $name, ... |
135 | |
153 | |
136 | =item $object->detach ($attachment) |
154 | Register an attachment by name through which attachable objects can refer |
|
|
155 | to this attachment. |
137 | |
156 | |
138 | Attach/detach a pre-registered attachment to an object. |
|
|
139 | |
|
|
140 | =item $player->attach ($attachment, key => $value...) |
|
|
141 | |
|
|
142 | =item $player->detach ($attachment) |
|
|
143 | |
|
|
144 | Attach/detach a pre-registered attachment to a player. |
|
|
145 | |
|
|
146 | =item $map->attach ($attachment, key => $value...) |
|
|
147 | |
|
|
148 | =item $map->detach ($attachment) |
|
|
149 | |
|
|
150 | Attach/detach a pre-registered attachment to a map. |
|
|
151 | |
|
|
152 | =item $bool = $object->attached ($name) |
|
|
153 | |
|
|
154 | =item $bool = $player->attached ($name) |
|
|
155 | |
|
|
156 | =item $bool = $map->attached ($name) |
157 | =item $bool = $attachable->attached ($name) |
157 | |
158 | |
158 | Checks wether the named attachment is currently attached to the object. |
159 | Checks wether the named attachment is currently attached to the object. |
159 | |
160 | |
160 | =item cf::attach_global ... |
161 | =item $attachable->attach ($attachment, key => $value...) |
161 | |
162 | |
162 | Attach handlers for global events. |
163 | =item $attachable->detach ($attachment) |
163 | |
164 | |
164 | This and all following C<attach_*>-functions expect any number of the |
165 | Attach/detach a pre-registered attachment either to a specific object |
165 | following handler/hook descriptions: |
166 | (C<$attachable>) or all objects of the given class (if C<$attachable> is a |
|
|
167 | class in a static method call). |
|
|
168 | |
|
|
169 | You can attach to global events by using the C<cf::global> class. |
|
|
170 | |
|
|
171 | These method calls expect any number of the following handler/hook |
|
|
172 | descriptions: |
166 | |
173 | |
167 | =over 4 |
174 | =over 4 |
168 | |
175 | |
169 | =item prio => $number |
176 | =item prio => $number |
170 | |
177 | |
… | |
… | |
172 | by another C<prio> setting). Lower priority handlers get executed |
179 | by another C<prio> setting). Lower priority handlers get executed |
173 | earlier. The default priority is C<0>, and many built-in handlers are |
180 | earlier. The default priority is C<0>, and many built-in handlers are |
174 | registered at priority C<-1000>, so lower priorities should not be used |
181 | registered at priority C<-1000>, so lower priorities should not be used |
175 | unless you know what you are doing. |
182 | unless you know what you are doing. |
176 | |
183 | |
|
|
184 | =item type => $type |
|
|
185 | |
|
|
186 | (Only for C<< cf::object->attach >> calls), limits the attachment to the |
|
|
187 | given type of objects only (the additional parameter C<subtype> can be |
|
|
188 | used to further limit to the given subtype). |
|
|
189 | |
177 | =item on_I<event> => \&cb |
190 | =item on_I<event> => \&cb |
178 | |
191 | |
179 | Call the given code reference whenever the named event happens (event is |
192 | Call the given code reference whenever the named event happens (event is |
180 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
193 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
181 | handlers are recognised generally depends on the type of object these |
194 | handlers are recognised generally depends on the type of object these |
… | |
… | |
190 | package and register them. Only handlers for eevents supported by the |
203 | package and register them. Only handlers for eevents supported by the |
191 | object/class are recognised. |
204 | object/class are recognised. |
192 | |
205 | |
193 | =back |
206 | =back |
194 | |
207 | |
195 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
196 | |
|
|
197 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
198 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
199 | type. |
|
|
200 | |
|
|
201 | =item cf::attach_to_objects ... |
|
|
202 | |
|
|
203 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
204 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
205 | the game. |
|
|
206 | |
|
|
207 | =item cf::attach_to_players ... |
|
|
208 | |
|
|
209 | Attach handlers to all players. |
|
|
210 | |
|
|
211 | =item cf::attach_to_maps ... |
|
|
212 | |
|
|
213 | Attach handlers to all maps. |
|
|
214 | |
|
|
215 | =item cf:register_attachment $name, ... |
|
|
216 | |
|
|
217 | Register an attachment by name through which objects can refer to this |
|
|
218 | attachment. |
|
|
219 | |
|
|
220 | =item cf:register_player_attachment $name, ... |
|
|
221 | |
|
|
222 | Register an attachment by name through which players can refer to this |
|
|
223 | attachment. |
|
|
224 | |
|
|
225 | =item cf:register_map_attachment $name, ... |
|
|
226 | |
|
|
227 | Register an attachment by name through which maps can refer to this |
|
|
228 | attachment. |
|
|
229 | |
|
|
230 | =cut |
208 | =cut |
231 | |
209 | |
232 | # the following variables are defined in .xs and must not be re-created |
210 | # the following variables are defined in .xs and must not be re-created |
233 | our @CB_GLOBAL = (); # registry for all global events |
211 | our @CB_GLOBAL = (); # registry for all global events |
234 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
212 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
235 | our @CB_PLAYER = (); |
213 | our @CB_PLAYER = (); |
|
|
214 | our @CB_CLIENT = (); |
236 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
215 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
237 | our @CB_MAP = (); |
216 | our @CB_MAP = (); |
238 | |
217 | |
239 | my %attachment; |
218 | my %attachment; |
240 | |
219 | |
241 | sub _attach_cb($\%$$$) { |
220 | sub _attach_cb($$$$) { |
242 | my ($registry, $undo, $event, $prio, $cb) = @_; |
221 | my ($registry, $event, $prio, $cb) = @_; |
243 | |
222 | |
244 | use sort 'stable'; |
223 | use sort 'stable'; |
245 | |
224 | |
246 | $cb = [$prio, $cb]; |
225 | $cb = [$prio, $cb]; |
247 | |
226 | |
248 | @{$registry->[$event]} = sort |
227 | @{$registry->[$event]} = sort |
249 | { $a->[0] cmp $b->[0] } |
228 | { $a->[0] cmp $b->[0] } |
250 | @{$registry->[$event] || []}, $cb; |
229 | @{$registry->[$event] || []}, $cb; |
251 | |
|
|
252 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
253 | } |
230 | } |
254 | |
231 | |
255 | # attach handles attaching event callbacks |
232 | # attach handles attaching event callbacks |
256 | # the only thing the caller has to do is pass the correct |
233 | # the only thing the caller has to do is pass the correct |
257 | # registry (== where the callback attaches to). |
234 | # registry (== where the callback attaches to). |
258 | sub _attach(\@$@) { |
235 | sub _attach { |
259 | my ($registry, $klass, @arg) = @_; |
236 | my ($registry, $klass, @arg) = @_; |
260 | |
237 | |
|
|
238 | my $object_type; |
261 | my $prio = 0; |
239 | my $prio = 0; |
262 | |
|
|
263 | my %undo = ( |
|
|
264 | registry => $registry, |
|
|
265 | cb => [], |
|
|
266 | ); |
|
|
267 | |
|
|
268 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
240 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
269 | |
241 | |
270 | while (@arg) { |
242 | while (@arg) { |
271 | my $type = shift @arg; |
243 | my $type = shift @arg; |
272 | |
244 | |
273 | if ($type eq "prio") { |
245 | if ($type eq "prio") { |
274 | $prio = shift @arg; |
246 | $prio = shift @arg; |
275 | |
247 | |
|
|
248 | } elsif ($type eq "type") { |
|
|
249 | $object_type = shift @arg; |
|
|
250 | $registry = $CB_TYPE[$object_type] ||= []; |
|
|
251 | |
|
|
252 | } elsif ($type eq "subtype") { |
|
|
253 | defined $object_type or Carp::croak "subtype specified without type"; |
|
|
254 | my $object_subtype = shift @arg; |
|
|
255 | $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= []; |
|
|
256 | |
276 | } elsif ($type eq "package") { |
257 | } elsif ($type eq "package") { |
277 | my $pkg = shift @arg; |
258 | my $pkg = shift @arg; |
278 | |
259 | |
279 | while (my ($name, $id) = each %cb_id) { |
260 | while (my ($name, $id) = each %cb_id) { |
280 | if (my $cb = $pkg->can ($name)) { |
261 | if (my $cb = $pkg->can ($name)) { |
281 | _attach_cb $registry, %undo, $id, $prio, $cb; |
262 | _attach_cb $registry, $id, $prio, $cb; |
282 | } |
263 | } |
283 | } |
264 | } |
284 | |
265 | |
285 | } elsif (exists $cb_id{$type}) { |
266 | } elsif (exists $cb_id{$type}) { |
286 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
267 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
287 | |
268 | |
288 | } elsif (ref $type) { |
269 | } elsif (ref $type) { |
289 | warn "attaching objects not supported, ignoring.\n"; |
270 | warn "attaching objects not supported, ignoring.\n"; |
290 | |
271 | |
291 | } else { |
272 | } else { |
292 | shift @arg; |
273 | shift @arg; |
293 | warn "attach argument '$type' not supported, ignoring.\n"; |
274 | warn "attach argument '$type' not supported, ignoring.\n"; |
294 | } |
275 | } |
295 | } |
276 | } |
296 | |
|
|
297 | \%undo |
|
|
298 | } |
277 | } |
299 | |
278 | |
300 | sub _attach_attachment { |
279 | sub _object_attach { |
301 | my ($obj, $name, %arg) = @_; |
280 | my ($obj, $name, %arg) = @_; |
302 | |
281 | |
303 | return if exists $obj->{_attachment}{$name}; |
282 | return if exists $obj->{_attachment}{$name}; |
304 | |
|
|
305 | my $res; |
|
|
306 | |
283 | |
307 | if (my $attach = $attachment{$name}) { |
284 | if (my $attach = $attachment{$name}) { |
308 | my $registry = $obj->registry; |
285 | my $registry = $obj->registry; |
309 | |
286 | |
310 | for (@$attach) { |
287 | for (@$attach) { |
311 | my ($klass, @attach) = @$_; |
288 | my ($klass, @attach) = @$_; |
312 | $res = _attach @$registry, $klass, @attach; |
289 | _attach $registry, $klass, @attach; |
313 | } |
290 | } |
314 | |
291 | |
315 | $obj->{$name} = \%arg; |
292 | $obj->{$name} = \%arg; |
316 | } else { |
293 | } else { |
317 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
294 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
318 | } |
295 | } |
319 | |
296 | |
320 | $obj->{_attachment}{$name} = undef; |
297 | $obj->{_attachment}{$name} = undef; |
321 | |
|
|
322 | $res->{attachment} = $name; |
|
|
323 | $res |
|
|
324 | } |
298 | } |
325 | |
299 | |
326 | *cf::object::attach = |
300 | sub cf::attachable::attach { |
327 | *cf::player::attach = |
301 | if (ref $_[0]) { |
328 | *cf::map::attach = sub { |
302 | _object_attach @_; |
329 | my ($obj, $name, %arg) = @_; |
303 | } else { |
330 | |
304 | _attach shift->_attach_registry, @_; |
331 | _attach_attachment $obj, $name, %arg; |
305 | } |
332 | }; |
306 | }; |
333 | |
307 | |
334 | # all those should be optimised |
308 | # all those should be optimised |
335 | *cf::object::detach = |
309 | sub cf::attachable::detach { |
336 | *cf::player::detach = |
|
|
337 | *cf::map::detach = sub { |
|
|
338 | my ($obj, $name) = @_; |
310 | my ($obj, $name) = @_; |
339 | |
311 | |
|
|
312 | if (ref $obj) { |
340 | delete $obj->{_attachment}{$name}; |
313 | delete $obj->{_attachment}{$name}; |
341 | reattach ($obj); |
314 | reattach ($obj); |
|
|
315 | } else { |
|
|
316 | Carp::croak "cannot, currently, detach class attachments"; |
|
|
317 | } |
342 | }; |
318 | }; |
343 | |
319 | |
344 | *cf::object::attached = |
320 | sub cf::attachable::attached { |
345 | *cf::player::attached = |
|
|
346 | *cf::map::attached = sub { |
|
|
347 | my ($obj, $name) = @_; |
321 | my ($obj, $name) = @_; |
348 | |
322 | |
349 | exists $obj->{_attachment}{$name} |
323 | exists $obj->{_attachment}{$name} |
350 | }; |
|
|
351 | |
|
|
352 | sub attach_global { |
|
|
353 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
354 | } |
324 | } |
355 | |
325 | |
356 | sub attach_to_type { |
326 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
357 | my $type = shift; |
327 | eval "#line " . __LINE__ . " 'cf.pm' |
358 | my $subtype = shift; |
328 | sub cf::\L$klass\E::_attach_registry { |
|
|
329 | (\\\@CB_$klass, KLASS_$klass) |
|
|
330 | } |
359 | |
331 | |
360 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
332 | sub cf::\L$klass\E::attachment { |
361 | } |
|
|
362 | |
|
|
363 | sub attach_to_objects { |
|
|
364 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
365 | } |
|
|
366 | |
|
|
367 | sub attach_to_players { |
|
|
368 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
369 | } |
|
|
370 | |
|
|
371 | sub attach_to_maps { |
|
|
372 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
373 | } |
|
|
374 | |
|
|
375 | sub register_attachment { |
|
|
376 | my $name = shift; |
333 | my \$name = shift; |
377 | |
334 | |
378 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
379 | } |
|
|
380 | |
|
|
381 | sub register_player_attachment { |
|
|
382 | my $name = shift; |
|
|
383 | |
|
|
384 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
385 | } |
|
|
386 | |
|
|
387 | sub register_map_attachment { |
|
|
388 | my $name = shift; |
|
|
389 | |
|
|
390 | $attachment{$name} = [[KLASS_MAP, @_]]; |
335 | \$attachment{\$name} = [[KLASS_$klass, \@_]]; |
|
|
336 | } |
|
|
337 | "; |
|
|
338 | die if $@; |
391 | } |
339 | } |
392 | |
340 | |
393 | our $override; |
341 | our $override; |
394 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
342 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
395 | |
343 | |
… | |
… | |
425 | |
373 | |
426 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
374 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
427 | |
375 | |
428 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
376 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
429 | |
377 | |
|
|
378 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
|
|
379 | |
430 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
380 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
431 | |
381 | |
432 | Generate a global/object/player/map-specific event with the given arguments. |
382 | Generate a global/object/player/map-specific event with the given arguments. |
433 | |
383 | |
434 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
384 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
… | |
… | |
439 | |
389 | |
440 | =cut |
390 | =cut |
441 | |
391 | |
442 | ############################################################################# |
392 | ############################################################################# |
443 | |
393 | |
444 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
394 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
395 | |
|
|
396 | Attachable objects includes objects, players, clients and maps. |
445 | |
397 | |
446 | =over 4 |
398 | =over 4 |
447 | |
399 | |
448 | =item $object->valid, $player->valid, $map->valid |
400 | =item $object->valid |
449 | |
401 | |
450 | Just because you have a perl object does not mean that the corresponding |
402 | Just because you have a perl object does not mean that the corresponding |
451 | C-level object still exists. If you try to access an object that has no |
403 | C-level object still exists. If you try to access an object that has no |
452 | valid C counterpart anymore you get an exception at runtime. This method |
404 | valid C counterpart anymore you get an exception at runtime. This method |
453 | can be used to test for existence of the C object part without causing an |
405 | can be used to test for existence of the C object part without causing an |
… | |
… | |
455 | |
407 | |
456 | =back |
408 | =back |
457 | |
409 | |
458 | =cut |
410 | =cut |
459 | |
411 | |
460 | *cf::object::valid = |
|
|
461 | *cf::player::valid = |
|
|
462 | *cf::map::valid = \&cf::_valid; |
|
|
463 | |
|
|
464 | ############################################################################# |
412 | ############################################################################# |
465 | # object support |
413 | # object support |
466 | |
414 | |
467 | sub instantiate { |
415 | sub instantiate { |
468 | my ($obj, $data) = @_; |
416 | my ($obj, $data) = @_; |
… | |
… | |
487 | |
435 | |
488 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
436 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
489 | if (my $attach = $attachment{$name}) { |
437 | if (my $attach = $attachment{$name}) { |
490 | for (@$attach) { |
438 | for (@$attach) { |
491 | my ($klass, @attach) = @$_; |
439 | my ($klass, @attach) = @$_; |
492 | _attach @$registry, $klass, @attach; |
440 | _attach $registry, $klass, @attach; |
493 | } |
441 | } |
494 | } else { |
442 | } else { |
495 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
443 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
496 | } |
444 | } |
497 | } |
445 | } |
… | |
… | |
553 | } |
501 | } |
554 | |
502 | |
555 | () |
503 | () |
556 | } |
504 | } |
557 | |
505 | |
558 | attach_to_objects |
506 | cf::object->attach ( |
559 | prio => -1000000, |
507 | prio => -1000000, |
560 | on_clone => sub { |
508 | on_clone => sub { |
561 | my ($src, $dst) = @_; |
509 | my ($src, $dst) = @_; |
562 | |
510 | |
563 | @{$dst->registry} = @{$src->registry}; |
511 | @{$dst->registry} = @{$src->registry}; |
… | |
… | |
565 | %$dst = %$src; |
513 | %$dst = %$src; |
566 | |
514 | |
567 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
515 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
568 | if exists $src->{_attachment}; |
516 | if exists $src->{_attachment}; |
569 | }, |
517 | }, |
570 | ; |
518 | ); |
571 | |
519 | |
572 | ############################################################################# |
520 | ############################################################################# |
573 | # command handling &c |
521 | # command handling &c |
574 | |
522 | |
575 | =item cf::register_command $name => \&callback($ob,$args); |
523 | =item cf::register_command $name => \&callback($ob,$args); |
… | |
… | |
604 | #warn "registering extcmd '$name' to '$caller'"; |
552 | #warn "registering extcmd '$name' to '$caller'"; |
605 | |
553 | |
606 | $EXTCMD{$name} = [$cb, $caller]; |
554 | $EXTCMD{$name} = [$cb, $caller]; |
607 | } |
555 | } |
608 | |
556 | |
609 | attach_to_players |
557 | cf::player->attach ( |
610 | on_command => sub { |
558 | on_command => sub { |
611 | my ($pl, $name, $params) = @_; |
559 | my ($pl, $name, $params) = @_; |
612 | |
560 | |
613 | my $cb = $COMMAND{$name} |
561 | my $cb = $COMMAND{$name} |
614 | or return; |
562 | or return; |
… | |
… | |
634 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
582 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
635 | } |
583 | } |
636 | |
584 | |
637 | cf::override; |
585 | cf::override; |
638 | }, |
586 | }, |
639 | ; |
587 | ); |
640 | |
588 | |
641 | sub register { |
589 | sub register { |
642 | my ($base, $pkg) = @_; |
590 | my ($base, $pkg) = @_; |
643 | |
591 | |
644 | #TODO |
592 | #TODO |
… | |
… | |
713 | |
661 | |
714 | Symbol::delete_package $pkg; |
662 | Symbol::delete_package $pkg; |
715 | } |
663 | } |
716 | |
664 | |
717 | sub load_extensions { |
665 | sub load_extensions { |
718 | my $LIBDIR = maps_directory "perl"; |
|
|
719 | |
|
|
720 | for my $ext (<$LIBDIR/*.ext>) { |
666 | for my $ext (<$LIBDIR/*.ext>) { |
721 | next unless -r $ext; |
667 | next unless -r $ext; |
722 | eval { |
668 | eval { |
723 | load_extension $ext; |
669 | load_extension $ext; |
724 | 1 |
670 | 1 |
… | |
… | |
736 | defined $path or return; |
682 | defined $path or return; |
737 | |
683 | |
738 | unlink "$path.pst"; |
684 | unlink "$path.pst"; |
739 | }; |
685 | }; |
740 | |
686 | |
741 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
687 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
742 | |
688 | |
743 | ############################################################################# |
689 | ############################################################################# |
744 | # load/save perl data associated with player->ob objects |
690 | # load/save perl data associated with player->ob objects |
745 | |
691 | |
746 | sub all_objects(@) { |
692 | sub all_objects(@) { |
747 | @_, map all_objects ($_->inv), @_ |
693 | @_, map all_objects ($_->inv), @_ |
748 | } |
694 | } |
749 | |
695 | |
750 | # TODO: compatibility cruft, remove when no longer needed |
696 | # TODO: compatibility cruft, remove when no longer needed |
751 | attach_to_players |
697 | cf::player->attach ( |
752 | on_load => sub { |
698 | on_load => sub { |
753 | my ($pl, $path) = @_; |
699 | my ($pl, $path) = @_; |
754 | |
700 | |
755 | for my $o (all_objects $pl->ob) { |
701 | for my $o (all_objects $pl->ob) { |
756 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
702 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
… | |
… | |
758 | |
704 | |
759 | %$o = %{ Storable::thaw pack "H*", $value }; |
705 | %$o = %{ Storable::thaw pack "H*", $value }; |
760 | } |
706 | } |
761 | } |
707 | } |
762 | }, |
708 | }, |
763 | ; |
709 | ); |
764 | |
710 | |
765 | ############################################################################# |
711 | ############################################################################# |
766 | |
712 | |
767 | =head2 CORE EXTENSIONS |
713 | =head2 CORE EXTENSIONS |
768 | |
714 | |
… | |
… | |
859 | |
805 | |
860 | =pod |
806 | =pod |
861 | |
807 | |
862 | The following fucntions and emthods are available within a safe environment: |
808 | The following fucntions and emthods are available within a safe environment: |
863 | |
809 | |
864 | cf::object contr pay_amount pay_player |
810 | cf::object contr pay_amount pay_player map |
865 | cf::object::player player |
811 | cf::object::player player |
866 | cf::player peaceful |
812 | cf::player peaceful |
|
|
813 | cf::map trigger |
867 | |
814 | |
868 | =cut |
815 | =cut |
869 | |
816 | |
870 | for ( |
817 | for ( |
871 | ["cf::object" => qw(contr pay_amount pay_player)], |
818 | ["cf::object" => qw(contr pay_amount pay_player map)], |
872 | ["cf::object::player" => qw(player)], |
819 | ["cf::object::player" => qw(player)], |
873 | ["cf::player" => qw(peaceful)], |
820 | ["cf::player" => qw(peaceful)], |
|
|
821 | ["cf::map" => qw(trigger)], |
874 | ) { |
822 | ) { |
875 | no strict 'refs'; |
823 | no strict 'refs'; |
876 | my ($pkg, @funs) = @$_; |
824 | my ($pkg, @funs) = @$_; |
877 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
825 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
878 | for @funs; |
826 | for @funs; |
… | |
… | |
1018 | sub db_sync() { |
966 | sub db_sync() { |
1019 | db_save if $dirty; |
967 | db_save if $dirty; |
1020 | undef $dirty; |
968 | undef $dirty; |
1021 | } |
969 | } |
1022 | |
970 | |
1023 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
971 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
1024 | db_sync; |
972 | db_sync; |
1025 | }); |
973 | }); |
1026 | |
974 | |
1027 | sub db_dirty() { |
975 | sub db_dirty() { |
1028 | $dirty = 1; |
976 | $dirty = 1; |
… | |
… | |
1042 | $DB->{$_[0]} = $_[1]; |
990 | $DB->{$_[0]} = $_[1]; |
1043 | } |
991 | } |
1044 | db_dirty; |
992 | db_dirty; |
1045 | } |
993 | } |
1046 | |
994 | |
1047 | attach_global |
995 | cf::global->attach ( |
1048 | prio => 10000, |
996 | prio => 10000, |
1049 | on_cleanup => sub { |
997 | on_cleanup => sub { |
1050 | db_sync; |
998 | db_sync; |
1051 | }, |
999 | }, |
1052 | ; |
1000 | ); |
1053 | } |
1001 | } |
1054 | |
1002 | |
1055 | ############################################################################# |
1003 | ############################################################################# |
1056 | # the server's main() |
1004 | # the server's main() |
1057 | |
1005 | |
… | |
… | |
1078 | |
1026 | |
1079 | $msg->("reloading..."); |
1027 | $msg->("reloading..."); |
1080 | |
1028 | |
1081 | eval { |
1029 | eval { |
1082 | # cancel all watchers |
1030 | # cancel all watchers |
1083 | $_->cancel for Event::all_watchers; |
1031 | for (Event::all_watchers) { |
|
|
1032 | $_->cancel if $_->data & WF_AUTOCANCEL; |
|
|
1033 | } |
1084 | |
1034 | |
1085 | # unload all extensions |
1035 | # unload all extensions |
1086 | for (@exts) { |
1036 | for (@exts) { |
1087 | $msg->("unloading <$_>"); |
1037 | $msg->("unloading <$_>"); |
1088 | unload_extension $_; |
1038 | unload_extension $_; |
… | |
… | |
1164 | }; |
1114 | }; |
1165 | |
1115 | |
1166 | unshift @INC, $LIBDIR; |
1116 | unshift @INC, $LIBDIR; |
1167 | |
1117 | |
1168 | $TICK_WATCHER = Event->timer ( |
1118 | $TICK_WATCHER = Event->timer ( |
1169 | prio => 1, |
1119 | prio => 0, |
1170 | async => 1, |
|
|
1171 | at => $NEXT_TICK || 1, |
1120 | at => $NEXT_TICK || 1, |
|
|
1121 | data => WF_AUTOCANCEL, |
1172 | cb => sub { |
1122 | cb => sub { |
1173 | cf::server_tick; # one server iteration |
1123 | cf::server_tick; # one server iteration |
1174 | |
1124 | |
1175 | my $NOW = Event::time; |
1125 | my $NOW = Event::time; |
1176 | $NEXT_TICK += $TICK; |
1126 | $NEXT_TICK += $TICK; |
… | |
… | |
1186 | IO::AIO::max_poll_time $TICK * 0.2; |
1136 | IO::AIO::max_poll_time $TICK * 0.2; |
1187 | |
1137 | |
1188 | Event->io (fd => IO::AIO::poll_fileno, |
1138 | Event->io (fd => IO::AIO::poll_fileno, |
1189 | poll => 'r', |
1139 | poll => 'r', |
1190 | prio => 5, |
1140 | prio => 5, |
|
|
1141 | data => WF_AUTOCANCEL, |
1191 | cb => \&IO::AIO::poll_cb); |
1142 | cb => \&IO::AIO::poll_cb); |
1192 | |
1143 | |
1193 | 1 |
1144 | 1 |
1194 | |
1145 | |