… | |
… | |
74 | print STDERR "cfperl: $msg"; |
74 | print STDERR "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
76 | }; |
76 | }; |
77 | } |
77 | } |
78 | |
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'; |
79 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
80 | |
85 | |
81 | # 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 |
82 | # within the Safe compartment. |
87 | # within the Safe compartment. |
83 | for my $pkg (qw( |
88 | for my $pkg (qw( |
|
|
89 | cf::global |
84 | cf::object cf::object::player |
90 | cf::object cf::object::player |
85 | cf::client_socket cf::player |
91 | cf::client cf::player |
86 | cf::arch cf::living |
92 | cf::arch cf::living |
87 | cf::map cf::party cf::region |
93 | cf::map cf::party cf::region |
88 | )) { |
94 | )) { |
89 | no strict 'refs'; |
95 | no strict 'refs'; |
90 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
96 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
… | |
… | |
132 | |
138 | |
133 | =cut |
139 | =cut |
134 | |
140 | |
135 | ############################################################################# |
141 | ############################################################################# |
136 | |
142 | |
137 | =head2 EVENTS AND OBJECT ATTACHMENTS |
143 | =head2 ATTACHABLE OBJECTS |
|
|
144 | |
|
|
145 | Many objects in crossfire are so-called attachable objects. That means you can |
|
|
146 | attach callbacks/event handlers (a collection of which is called an "attachment") |
|
|
147 | to it. All such attachable objects support the following methods. |
|
|
148 | |
|
|
149 | In the following description, CLASS can be any of C<global>, C<object> |
|
|
150 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
|
|
151 | crossfire+). |
138 | |
152 | |
139 | =over 4 |
153 | =over 4 |
140 | |
154 | |
141 | =item $object->attach ($attachment, key => $value...) |
|
|
142 | |
|
|
143 | =item $object->detach ($attachment) |
|
|
144 | |
|
|
145 | Attach/detach a pre-registered attachment to an object. |
|
|
146 | |
|
|
147 | =item $player->attach ($attachment, key => $value...) |
|
|
148 | |
|
|
149 | =item $player->detach ($attachment) |
|
|
150 | |
|
|
151 | Attach/detach a pre-registered attachment to a player. |
|
|
152 | |
|
|
153 | =item $map->attach ($attachment, key => $value...) |
155 | =item $attachable->attach ($attachment, key => $value...) |
154 | |
156 | |
155 | =item $map->detach ($attachment) |
157 | =item $attachable->detach ($attachment) |
156 | |
158 | |
157 | Attach/detach a pre-registered attachment to a map. |
159 | Attach/detach a pre-registered attachment to a specific object and give it |
|
|
160 | the specified key/value pairs as arguments. |
158 | |
161 | |
159 | =item $bool = $object->attached ($name) |
162 | Example, attach a minesweeper attachment to the given object, making it a |
|
|
163 | 10x10 minesweeper game: |
160 | |
164 | |
161 | =item $bool = $player->attached ($name) |
165 | $obj->attach (minesweeper => width => 10, height => 10); |
162 | |
166 | |
163 | =item $bool = $map->attached ($name) |
167 | =item $bool = $attachable->attached ($name) |
164 | |
168 | |
165 | Checks wether the named attachment is currently attached to the object. |
169 | Checks wether the named attachment is currently attached to the object. |
166 | |
170 | |
167 | =item cf::attach_global ... |
171 | =item cf::CLASS->attach ... |
168 | |
172 | |
169 | Attach handlers for global events. |
173 | =item cf::CLASS->detach ... |
170 | |
174 | |
171 | This and all following C<attach_*>-functions expect any number of the |
175 | Define an anonymous attachment and attach it to all objects of the given |
172 | following handler/hook descriptions: |
176 | CLASS. See the next function for an explanation of its arguments. |
|
|
177 | |
|
|
178 | You can attach to global events by using the C<cf::global> class. |
|
|
179 | |
|
|
180 | Example, log all player logins: |
|
|
181 | |
|
|
182 | cf::player->attach ( |
|
|
183 | on_login => sub { |
|
|
184 | my ($pl) = @_; |
|
|
185 | ... |
|
|
186 | }, |
|
|
187 | ); |
|
|
188 | |
|
|
189 | Example, attach to the jeweler skill: |
|
|
190 | |
|
|
191 | cf::object->attach ( |
|
|
192 | type => cf::SKILL, |
|
|
193 | subtype => cf::SK_JEWELER, |
|
|
194 | on_use_skill => sub { |
|
|
195 | my ($sk, $ob, $part, $dir, $msg) = @_; |
|
|
196 | ... |
|
|
197 | }, |
|
|
198 | ); |
|
|
199 | |
|
|
200 | =item cf::CLASS::attachment $name, ... |
|
|
201 | |
|
|
202 | Register an attachment by C<$name> through which attachable objects of the |
|
|
203 | given CLASS can refer to this attachment. |
|
|
204 | |
|
|
205 | Some classes such as crossfire maps and objects can specify attachments |
|
|
206 | that are attached at load/instantiate time, thus the need for a name. |
|
|
207 | |
|
|
208 | These calls expect any number of the following handler/hook descriptions: |
173 | |
209 | |
174 | =over 4 |
210 | =over 4 |
175 | |
211 | |
176 | =item prio => $number |
212 | =item prio => $number |
177 | |
213 | |
… | |
… | |
179 | by another C<prio> setting). Lower priority handlers get executed |
215 | by another C<prio> setting). Lower priority handlers get executed |
180 | earlier. The default priority is C<0>, and many built-in handlers are |
216 | earlier. The default priority is C<0>, and many built-in handlers are |
181 | registered at priority C<-1000>, so lower priorities should not be used |
217 | registered at priority C<-1000>, so lower priorities should not be used |
182 | unless you know what you are doing. |
218 | unless you know what you are doing. |
183 | |
219 | |
|
|
220 | =item type => $type |
|
|
221 | |
|
|
222 | (Only for C<< cf::object->attach >> calls), limits the attachment to the |
|
|
223 | given type of objects only (the additional parameter C<subtype> can be |
|
|
224 | used to further limit to the given subtype). |
|
|
225 | |
184 | =item on_I<event> => \&cb |
226 | =item on_I<event> => \&cb |
185 | |
227 | |
186 | Call the given code reference whenever the named event happens (event is |
228 | Call the given code reference whenever the named event happens (event is |
187 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
229 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
188 | handlers are recognised generally depends on the type of object these |
230 | handlers are recognised generally depends on the type of object these |
… | |
… | |
197 | package and register them. Only handlers for eevents supported by the |
239 | package and register them. Only handlers for eevents supported by the |
198 | object/class are recognised. |
240 | object/class are recognised. |
199 | |
241 | |
200 | =back |
242 | =back |
201 | |
243 | |
202 | =item cf::attach_to_type $object_type, $subtype, ... |
244 | Example, define an attachment called "sockpuppet" that calls the given |
|
|
245 | event handler when a monster attacks: |
203 | |
246 | |
204 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
247 | cf::object::attachment sockpuppet => |
205 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
248 | on_skill_attack => sub { |
206 | type. |
249 | my ($self, $victim) = @_; |
207 | |
250 | ... |
208 | =item cf::attach_to_objects ... |
251 | } |
209 | |
252 | } |
210 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
211 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
212 | the game. |
|
|
213 | |
|
|
214 | =item cf::attach_to_players ... |
|
|
215 | |
|
|
216 | Attach handlers to all players. |
|
|
217 | |
|
|
218 | =item cf::attach_to_maps ... |
|
|
219 | |
|
|
220 | Attach handlers to all maps. |
|
|
221 | |
|
|
222 | =item cf:register_attachment $name, ... |
|
|
223 | |
|
|
224 | Register an attachment by name through which objects can refer to this |
|
|
225 | attachment. |
|
|
226 | |
|
|
227 | =item cf:register_player_attachment $name, ... |
|
|
228 | |
|
|
229 | Register an attachment by name through which players can refer to this |
|
|
230 | attachment. |
|
|
231 | |
|
|
232 | =item cf:register_map_attachment $name, ... |
|
|
233 | |
|
|
234 | Register an attachment by name through which maps can refer to this |
|
|
235 | attachment. |
|
|
236 | |
253 | |
237 | =cut |
254 | =cut |
238 | |
255 | |
239 | # the following variables are defined in .xs and must not be re-created |
256 | # the following variables are defined in .xs and must not be re-created |
240 | our @CB_GLOBAL = (); # registry for all global events |
257 | our @CB_GLOBAL = (); # registry for all global events |
241 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
258 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
242 | our @CB_PLAYER = (); |
259 | our @CB_PLAYER = (); |
|
|
260 | our @CB_CLIENT = (); |
243 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
261 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
244 | our @CB_MAP = (); |
262 | our @CB_MAP = (); |
245 | |
263 | |
246 | my %attachment; |
264 | my %attachment; |
247 | |
265 | |
248 | sub _attach_cb($\%$$$) { |
266 | sub _attach_cb($$$$) { |
249 | my ($registry, $undo, $event, $prio, $cb) = @_; |
267 | my ($registry, $event, $prio, $cb) = @_; |
250 | |
268 | |
251 | use sort 'stable'; |
269 | use sort 'stable'; |
252 | |
270 | |
253 | $cb = [$prio, $cb]; |
271 | $cb = [$prio, $cb]; |
254 | |
272 | |
255 | @{$registry->[$event]} = sort |
273 | @{$registry->[$event]} = sort |
256 | { $a->[0] cmp $b->[0] } |
274 | { $a->[0] cmp $b->[0] } |
257 | @{$registry->[$event] || []}, $cb; |
275 | @{$registry->[$event] || []}, $cb; |
258 | |
|
|
259 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
260 | } |
276 | } |
261 | |
277 | |
262 | # attach handles attaching event callbacks |
278 | # attach handles attaching event callbacks |
263 | # the only thing the caller has to do is pass the correct |
279 | # the only thing the caller has to do is pass the correct |
264 | # registry (== where the callback attaches to). |
280 | # registry (== where the callback attaches to). |
265 | sub _attach(\@$@) { |
281 | sub _attach { |
266 | my ($registry, $klass, @arg) = @_; |
282 | my ($registry, $klass, @arg) = @_; |
267 | |
283 | |
|
|
284 | my $object_type; |
268 | my $prio = 0; |
285 | my $prio = 0; |
269 | |
|
|
270 | my %undo = ( |
|
|
271 | registry => $registry, |
|
|
272 | cb => [], |
|
|
273 | ); |
|
|
274 | |
|
|
275 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
286 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
276 | |
287 | |
277 | while (@arg) { |
288 | while (@arg) { |
278 | my $type = shift @arg; |
289 | my $type = shift @arg; |
279 | |
290 | |
280 | if ($type eq "prio") { |
291 | if ($type eq "prio") { |
281 | $prio = shift @arg; |
292 | $prio = shift @arg; |
282 | |
293 | |
|
|
294 | } elsif ($type eq "type") { |
|
|
295 | $object_type = shift @arg; |
|
|
296 | $registry = $CB_TYPE[$object_type] ||= []; |
|
|
297 | |
|
|
298 | } elsif ($type eq "subtype") { |
|
|
299 | defined $object_type or Carp::croak "subtype specified without type"; |
|
|
300 | my $object_subtype = shift @arg; |
|
|
301 | $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= []; |
|
|
302 | |
283 | } elsif ($type eq "package") { |
303 | } elsif ($type eq "package") { |
284 | my $pkg = shift @arg; |
304 | my $pkg = shift @arg; |
285 | |
305 | |
286 | while (my ($name, $id) = each %cb_id) { |
306 | while (my ($name, $id) = each %cb_id) { |
287 | if (my $cb = $pkg->can ($name)) { |
307 | if (my $cb = $pkg->can ($name)) { |
288 | _attach_cb $registry, %undo, $id, $prio, $cb; |
308 | _attach_cb $registry, $id, $prio, $cb; |
289 | } |
309 | } |
290 | } |
310 | } |
291 | |
311 | |
292 | } elsif (exists $cb_id{$type}) { |
312 | } elsif (exists $cb_id{$type}) { |
293 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
313 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
294 | |
314 | |
295 | } elsif (ref $type) { |
315 | } elsif (ref $type) { |
296 | warn "attaching objects not supported, ignoring.\n"; |
316 | warn "attaching objects not supported, ignoring.\n"; |
297 | |
317 | |
298 | } else { |
318 | } else { |
299 | shift @arg; |
319 | shift @arg; |
300 | warn "attach argument '$type' not supported, ignoring.\n"; |
320 | warn "attach argument '$type' not supported, ignoring.\n"; |
301 | } |
321 | } |
302 | } |
322 | } |
303 | |
|
|
304 | \%undo |
|
|
305 | } |
323 | } |
306 | |
324 | |
307 | sub _attach_attachment { |
325 | sub _object_attach { |
308 | my ($obj, $name, %arg) = @_; |
326 | my ($obj, $name, %arg) = @_; |
309 | |
327 | |
310 | return if exists $obj->{_attachment}{$name}; |
328 | return if exists $obj->{_attachment}{$name}; |
311 | |
|
|
312 | my $res; |
|
|
313 | |
329 | |
314 | if (my $attach = $attachment{$name}) { |
330 | if (my $attach = $attachment{$name}) { |
315 | my $registry = $obj->registry; |
331 | my $registry = $obj->registry; |
316 | |
332 | |
317 | for (@$attach) { |
333 | for (@$attach) { |
318 | my ($klass, @attach) = @$_; |
334 | my ($klass, @attach) = @$_; |
319 | $res = _attach @$registry, $klass, @attach; |
335 | _attach $registry, $klass, @attach; |
320 | } |
336 | } |
321 | |
337 | |
322 | $obj->{$name} = \%arg; |
338 | $obj->{$name} = \%arg; |
323 | } else { |
339 | } else { |
324 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
340 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
325 | } |
341 | } |
326 | |
342 | |
327 | $obj->{_attachment}{$name} = undef; |
343 | $obj->{_attachment}{$name} = undef; |
328 | |
|
|
329 | $res->{attachment} = $name; |
|
|
330 | $res |
|
|
331 | } |
344 | } |
332 | |
345 | |
333 | *cf::object::attach = |
346 | sub cf::attachable::attach { |
334 | *cf::player::attach = |
347 | if (ref $_[0]) { |
335 | *cf::map::attach = sub { |
348 | _object_attach @_; |
336 | my ($obj, $name, %arg) = @_; |
349 | } else { |
337 | |
350 | _attach shift->_attach_registry, @_; |
338 | _attach_attachment $obj, $name, %arg; |
351 | } |
339 | }; |
352 | }; |
340 | |
353 | |
341 | # all those should be optimised |
354 | # all those should be optimised |
342 | *cf::object::detach = |
355 | sub cf::attachable::detach { |
343 | *cf::player::detach = |
|
|
344 | *cf::map::detach = sub { |
|
|
345 | my ($obj, $name) = @_; |
356 | my ($obj, $name) = @_; |
346 | |
357 | |
|
|
358 | if (ref $obj) { |
347 | delete $obj->{_attachment}{$name}; |
359 | delete $obj->{_attachment}{$name}; |
348 | reattach ($obj); |
360 | reattach ($obj); |
|
|
361 | } else { |
|
|
362 | Carp::croak "cannot, currently, detach class attachments"; |
|
|
363 | } |
349 | }; |
364 | }; |
350 | |
365 | |
351 | *cf::object::attached = |
366 | sub cf::attachable::attached { |
352 | *cf::player::attached = |
|
|
353 | *cf::map::attached = sub { |
|
|
354 | my ($obj, $name) = @_; |
367 | my ($obj, $name) = @_; |
355 | |
368 | |
356 | exists $obj->{_attachment}{$name} |
369 | exists $obj->{_attachment}{$name} |
357 | }; |
|
|
358 | |
|
|
359 | sub attach_global { |
|
|
360 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
361 | } |
370 | } |
362 | |
371 | |
363 | sub attach_to_type { |
372 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
364 | my $type = shift; |
373 | eval "#line " . __LINE__ . " 'cf.pm' |
365 | my $subtype = shift; |
374 | sub cf::\L$klass\E::_attach_registry { |
|
|
375 | (\\\@CB_$klass, KLASS_$klass) |
|
|
376 | } |
366 | |
377 | |
367 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
378 | sub cf::\L$klass\E::attachment { |
368 | } |
|
|
369 | |
|
|
370 | sub attach_to_objects { |
|
|
371 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
372 | } |
|
|
373 | |
|
|
374 | sub attach_to_players { |
|
|
375 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
376 | } |
|
|
377 | |
|
|
378 | sub attach_to_maps { |
|
|
379 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
380 | } |
|
|
381 | |
|
|
382 | sub register_attachment { |
|
|
383 | my $name = shift; |
379 | my \$name = shift; |
384 | |
380 | |
385 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
386 | } |
|
|
387 | |
|
|
388 | sub register_player_attachment { |
|
|
389 | my $name = shift; |
|
|
390 | |
|
|
391 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
392 | } |
|
|
393 | |
|
|
394 | sub register_map_attachment { |
|
|
395 | my $name = shift; |
|
|
396 | |
|
|
397 | $attachment{$name} = [[KLASS_MAP, @_]]; |
381 | \$attachment{\$name} = [[KLASS_$klass, \@_]]; |
|
|
382 | } |
|
|
383 | "; |
|
|
384 | die if $@; |
398 | } |
385 | } |
399 | |
386 | |
400 | our $override; |
387 | our $override; |
401 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
388 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
402 | |
389 | |
… | |
… | |
432 | |
419 | |
433 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
420 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
434 | |
421 | |
435 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
422 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
436 | |
423 | |
|
|
424 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
|
|
425 | |
437 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
426 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
438 | |
427 | |
439 | Generate a global/object/player/map-specific event with the given arguments. |
428 | Generate a global/object/player/map-specific event with the given arguments. |
440 | |
429 | |
441 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
430 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
… | |
… | |
446 | |
435 | |
447 | =cut |
436 | =cut |
448 | |
437 | |
449 | ############################################################################# |
438 | ############################################################################# |
450 | |
439 | |
451 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
440 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
441 | |
|
|
442 | Attachable objects includes objects, players, clients and maps. |
452 | |
443 | |
453 | =over 4 |
444 | =over 4 |
454 | |
445 | |
455 | =item $object->valid, $player->valid, $map->valid |
446 | =item $object->valid |
456 | |
447 | |
457 | Just because you have a perl object does not mean that the corresponding |
448 | Just because you have a perl object does not mean that the corresponding |
458 | C-level object still exists. If you try to access an object that has no |
449 | C-level object still exists. If you try to access an object that has no |
459 | valid C counterpart anymore you get an exception at runtime. This method |
450 | valid C counterpart anymore you get an exception at runtime. This method |
460 | can be used to test for existence of the C object part without causing an |
451 | can be used to test for existence of the C object part without causing an |
… | |
… | |
462 | |
453 | |
463 | =back |
454 | =back |
464 | |
455 | |
465 | =cut |
456 | =cut |
466 | |
457 | |
467 | *cf::object::valid = |
|
|
468 | *cf::player::valid = |
|
|
469 | *cf::map::valid = \&cf::_valid; |
|
|
470 | |
|
|
471 | ############################################################################# |
458 | ############################################################################# |
472 | # object support |
459 | # object support |
473 | |
460 | |
474 | sub instantiate { |
461 | sub instantiate { |
475 | my ($obj, $data) = @_; |
462 | my ($obj, $data) = @_; |
… | |
… | |
494 | |
481 | |
495 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
482 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
496 | if (my $attach = $attachment{$name}) { |
483 | if (my $attach = $attachment{$name}) { |
497 | for (@$attach) { |
484 | for (@$attach) { |
498 | my ($klass, @attach) = @$_; |
485 | my ($klass, @attach) = @$_; |
499 | _attach @$registry, $klass, @attach; |
486 | _attach $registry, $klass, @attach; |
500 | } |
487 | } |
501 | } else { |
488 | } else { |
502 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
489 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
503 | } |
490 | } |
504 | } |
491 | } |
… | |
… | |
560 | } |
547 | } |
561 | |
548 | |
562 | () |
549 | () |
563 | } |
550 | } |
564 | |
551 | |
565 | attach_to_objects |
552 | cf::object->attach ( |
566 | prio => -1000000, |
553 | prio => -1000000, |
567 | on_clone => sub { |
554 | on_clone => sub { |
568 | my ($src, $dst) = @_; |
555 | my ($src, $dst) = @_; |
569 | |
556 | |
570 | @{$dst->registry} = @{$src->registry}; |
557 | @{$dst->registry} = @{$src->registry}; |
… | |
… | |
572 | %$dst = %$src; |
559 | %$dst = %$src; |
573 | |
560 | |
574 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
561 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
575 | if exists $src->{_attachment}; |
562 | if exists $src->{_attachment}; |
576 | }, |
563 | }, |
577 | ; |
564 | ); |
578 | |
565 | |
579 | ############################################################################# |
566 | ############################################################################# |
580 | # command handling &c |
567 | # command handling &c |
581 | |
568 | |
582 | =item cf::register_command $name => \&callback($ob,$args); |
569 | =item cf::register_command $name => \&callback($ob,$args); |
… | |
… | |
611 | #warn "registering extcmd '$name' to '$caller'"; |
598 | #warn "registering extcmd '$name' to '$caller'"; |
612 | |
599 | |
613 | $EXTCMD{$name} = [$cb, $caller]; |
600 | $EXTCMD{$name} = [$cb, $caller]; |
614 | } |
601 | } |
615 | |
602 | |
616 | attach_to_players |
603 | cf::player->attach ( |
617 | on_command => sub { |
604 | on_command => sub { |
618 | my ($pl, $name, $params) = @_; |
605 | my ($pl, $name, $params) = @_; |
619 | |
606 | |
620 | my $cb = $COMMAND{$name} |
607 | my $cb = $COMMAND{$name} |
621 | or return; |
608 | or return; |
… | |
… | |
641 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
628 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
642 | } |
629 | } |
643 | |
630 | |
644 | cf::override; |
631 | cf::override; |
645 | }, |
632 | }, |
646 | ; |
633 | ); |
647 | |
634 | |
648 | sub register { |
635 | sub register { |
649 | my ($base, $pkg) = @_; |
636 | my ($base, $pkg) = @_; |
650 | |
637 | |
651 | #TODO |
638 | #TODO |
… | |
… | |
741 | defined $path or return; |
728 | defined $path or return; |
742 | |
729 | |
743 | unlink "$path.pst"; |
730 | unlink "$path.pst"; |
744 | }; |
731 | }; |
745 | |
732 | |
746 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
733 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
747 | |
734 | |
748 | ############################################################################# |
735 | ############################################################################# |
749 | # load/save perl data associated with player->ob objects |
736 | # load/save perl data associated with player->ob objects |
750 | |
737 | |
751 | sub all_objects(@) { |
738 | sub all_objects(@) { |
752 | @_, map all_objects ($_->inv), @_ |
739 | @_, map all_objects ($_->inv), @_ |
753 | } |
740 | } |
754 | |
741 | |
755 | # TODO: compatibility cruft, remove when no longer needed |
742 | # TODO: compatibility cruft, remove when no longer needed |
756 | attach_to_players |
743 | cf::player->attach ( |
757 | on_load => sub { |
744 | on_load => sub { |
758 | my ($pl, $path) = @_; |
745 | my ($pl, $path) = @_; |
759 | |
746 | |
760 | for my $o (all_objects $pl->ob) { |
747 | for my $o (all_objects $pl->ob) { |
761 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
748 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
… | |
… | |
763 | |
750 | |
764 | %$o = %{ Storable::thaw pack "H*", $value }; |
751 | %$o = %{ Storable::thaw pack "H*", $value }; |
765 | } |
752 | } |
766 | } |
753 | } |
767 | }, |
754 | }, |
768 | ; |
755 | ); |
769 | |
756 | |
770 | ############################################################################# |
757 | ############################################################################# |
771 | |
758 | |
772 | =head2 CORE EXTENSIONS |
759 | =head2 CORE EXTENSIONS |
773 | |
760 | |
… | |
… | |
864 | |
851 | |
865 | =pod |
852 | =pod |
866 | |
853 | |
867 | The following fucntions and emthods are available within a safe environment: |
854 | The following fucntions and emthods are available within a safe environment: |
868 | |
855 | |
869 | cf::object contr pay_amount pay_player |
856 | cf::object contr pay_amount pay_player map |
870 | cf::object::player player |
857 | cf::object::player player |
871 | cf::player peaceful |
858 | cf::player peaceful |
|
|
859 | cf::map trigger |
872 | |
860 | |
873 | =cut |
861 | =cut |
874 | |
862 | |
875 | for ( |
863 | for ( |
876 | ["cf::object" => qw(contr pay_amount pay_player)], |
864 | ["cf::object" => qw(contr pay_amount pay_player map)], |
877 | ["cf::object::player" => qw(player)], |
865 | ["cf::object::player" => qw(player)], |
878 | ["cf::player" => qw(peaceful)], |
866 | ["cf::player" => qw(peaceful)], |
|
|
867 | ["cf::map" => qw(trigger)], |
879 | ) { |
868 | ) { |
880 | no strict 'refs'; |
869 | no strict 'refs'; |
881 | my ($pkg, @funs) = @$_; |
870 | my ($pkg, @funs) = @$_; |
882 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
871 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
883 | for @funs; |
872 | for @funs; |
… | |
… | |
1047 | $DB->{$_[0]} = $_[1]; |
1036 | $DB->{$_[0]} = $_[1]; |
1048 | } |
1037 | } |
1049 | db_dirty; |
1038 | db_dirty; |
1050 | } |
1039 | } |
1051 | |
1040 | |
1052 | attach_global |
1041 | cf::global->attach ( |
1053 | prio => 10000, |
1042 | prio => 10000, |
1054 | on_cleanup => sub { |
1043 | on_cleanup => sub { |
1055 | db_sync; |
1044 | db_sync; |
1056 | }, |
1045 | }, |
1057 | ; |
1046 | ); |
1058 | } |
1047 | } |
1059 | |
1048 | |
1060 | ############################################################################# |
1049 | ############################################################################# |
1061 | # the server's main() |
1050 | # the server's main() |
1062 | |
1051 | |
… | |
… | |
1171 | }; |
1160 | }; |
1172 | |
1161 | |
1173 | unshift @INC, $LIBDIR; |
1162 | unshift @INC, $LIBDIR; |
1174 | |
1163 | |
1175 | $TICK_WATCHER = Event->timer ( |
1164 | $TICK_WATCHER = Event->timer ( |
1176 | prio => 1, |
1165 | prio => 0, |
1177 | async => 1, |
|
|
1178 | at => $NEXT_TICK || 1, |
1166 | at => $NEXT_TICK || 1, |
1179 | data => WF_AUTOCANCEL, |
1167 | data => WF_AUTOCANCEL, |
1180 | cb => sub { |
1168 | cb => sub { |
1181 | cf::server_tick; # one server iteration |
1169 | cf::server_tick; # one server iteration |
1182 | |
1170 | |