… | |
… | |
25 | our %EXTCMD = (); |
25 | our %EXTCMD = (); |
26 | |
26 | |
27 | _init_vars; |
27 | _init_vars; |
28 | |
28 | |
29 | our @EVENT; |
29 | our @EVENT; |
30 | our $LIBDIR = maps_directory "perl"; |
30 | our $LIBDIR = datadir . "/ext"; |
31 | |
31 | |
32 | our $TICK = MAX_TIME * 1e-6; |
32 | our $TICK = MAX_TIME * 1e-6; |
33 | our $TICK_WATCHER; |
33 | our $TICK_WATCHER; |
34 | our $NEXT_TICK; |
34 | our $NEXT_TICK; |
35 | |
35 | |
… | |
… | |
80 | |
80 | |
81 | # we bless all objects into (empty) derived classes to force a method lookup |
81 | # we bless all objects into (empty) derived classes to force a method lookup |
82 | # within the Safe compartment. |
82 | # within the Safe compartment. |
83 | for my $pkg (qw( |
83 | for my $pkg (qw( |
84 | cf::object cf::object::player |
84 | cf::object cf::object::player |
85 | cf::client_socket cf::player |
85 | cf::client cf::player |
86 | cf::arch cf::living |
86 | cf::arch cf::living |
87 | cf::map cf::party cf::region |
87 | cf::map cf::party cf::region |
88 | )) { |
88 | )) { |
89 | no strict 'refs'; |
89 | no strict 'refs'; |
90 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
90 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
… | |
… | |
152 | |
152 | |
153 | =item $map->attach ($attachment, key => $value...) |
153 | =item $map->attach ($attachment, key => $value...) |
154 | |
154 | |
155 | =item $map->detach ($attachment) |
155 | =item $map->detach ($attachment) |
156 | |
156 | |
|
|
157 | Attach/detach a pre-registered attachment to a client. |
|
|
158 | |
|
|
159 | =item $client->attach ($attachment, key => $value...) |
|
|
160 | |
|
|
161 | =item $client->detach ($attachment) |
|
|
162 | |
157 | Attach/detach a pre-registered attachment to a map. |
163 | Attach/detach a pre-registered attachment to a map. |
158 | |
164 | |
159 | =item $bool = $object->attached ($name) |
165 | =item $bool = $object->attached ($name) |
160 | |
166 | |
161 | =item $bool = $player->attached ($name) |
167 | =item $bool = $player->attached ($name) |
|
|
168 | |
|
|
169 | =item $bool = $client->attached ($name) |
162 | |
170 | |
163 | =item $bool = $map->attached ($name) |
171 | =item $bool = $map->attached ($name) |
164 | |
172 | |
165 | Checks wether the named attachment is currently attached to the object. |
173 | Checks wether the named attachment is currently attached to the object. |
166 | |
174 | |
… | |
… | |
213 | |
221 | |
214 | =item cf::attach_to_players ... |
222 | =item cf::attach_to_players ... |
215 | |
223 | |
216 | Attach handlers to all players. |
224 | Attach handlers to all players. |
217 | |
225 | |
|
|
226 | =item cf::attach_to_clients ... |
|
|
227 | |
|
|
228 | Attach handlers to all players. |
|
|
229 | |
218 | =item cf::attach_to_maps ... |
230 | =item cf::attach_to_maps ... |
219 | |
231 | |
220 | Attach handlers to all maps. |
232 | Attach handlers to all maps. |
221 | |
233 | |
222 | =item cf:register_attachment $name, ... |
234 | =item cf:register_attachment $name, ... |
… | |
… | |
238 | |
250 | |
239 | # the following variables are defined in .xs and must not be re-created |
251 | # the following variables are defined in .xs and must not be re-created |
240 | our @CB_GLOBAL = (); # registry for all global events |
252 | our @CB_GLOBAL = (); # registry for all global events |
241 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
253 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
242 | our @CB_PLAYER = (); |
254 | our @CB_PLAYER = (); |
|
|
255 | our @CB_CLIENT = (); |
243 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
256 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
244 | our @CB_MAP = (); |
257 | our @CB_MAP = (); |
245 | |
258 | |
246 | my %attachment; |
259 | my %attachment; |
247 | |
260 | |
… | |
… | |
330 | $res |
343 | $res |
331 | } |
344 | } |
332 | |
345 | |
333 | *cf::object::attach = |
346 | *cf::object::attach = |
334 | *cf::player::attach = |
347 | *cf::player::attach = |
|
|
348 | *cf::client::attach = |
335 | *cf::map::attach = sub { |
349 | *cf::map::attach = sub { |
336 | my ($obj, $name, %arg) = @_; |
350 | my ($obj, $name, %arg) = @_; |
337 | |
351 | |
338 | _attach_attachment $obj, $name, %arg; |
352 | _attach_attachment $obj, $name, %arg; |
339 | }; |
353 | }; |
340 | |
354 | |
341 | # all those should be optimised |
355 | # all those should be optimised |
342 | *cf::object::detach = |
356 | *cf::object::detach = |
343 | *cf::player::detach = |
357 | *cf::player::detach = |
|
|
358 | *cf::client::detach = |
344 | *cf::map::detach = sub { |
359 | *cf::map::detach = sub { |
345 | my ($obj, $name) = @_; |
360 | my ($obj, $name) = @_; |
346 | |
361 | |
347 | delete $obj->{_attachment}{$name}; |
362 | delete $obj->{_attachment}{$name}; |
348 | reattach ($obj); |
363 | reattach ($obj); |
349 | }; |
364 | }; |
350 | |
365 | |
351 | *cf::object::attached = |
366 | *cf::object::attached = |
352 | *cf::player::attached = |
367 | *cf::player::attached = |
|
|
368 | *cf::client::attached = |
353 | *cf::map::attached = sub { |
369 | *cf::map::attached = sub { |
354 | my ($obj, $name) = @_; |
370 | my ($obj, $name) = @_; |
355 | |
371 | |
356 | exists $obj->{_attachment}{$name} |
372 | exists $obj->{_attachment}{$name} |
357 | }; |
373 | }; |
… | |
… | |
373 | |
389 | |
374 | sub attach_to_players { |
390 | sub attach_to_players { |
375 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
391 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
376 | } |
392 | } |
377 | |
393 | |
|
|
394 | sub attach_to_clients { |
|
|
395 | _attach @CB_CLIENT, KLASS_CLIENT, @_ |
|
|
396 | } |
|
|
397 | |
378 | sub attach_to_maps { |
398 | sub attach_to_maps { |
379 | _attach @CB_MAP, KLASS_MAP, @_ |
399 | _attach @CB_MAP, KLASS_MAP, @_ |
380 | } |
400 | } |
381 | |
401 | |
382 | sub register_attachment { |
402 | sub register_attachment { |
… | |
… | |
387 | |
407 | |
388 | sub register_player_attachment { |
408 | sub register_player_attachment { |
389 | my $name = shift; |
409 | my $name = shift; |
390 | |
410 | |
391 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
411 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
412 | } |
|
|
413 | |
|
|
414 | sub register_client_attachment { |
|
|
415 | my $name = shift; |
|
|
416 | |
|
|
417 | $attachment{$name} = [[KLASS_CLIENT, @_]]; |
392 | } |
418 | } |
393 | |
419 | |
394 | sub register_map_attachment { |
420 | sub register_map_attachment { |
395 | my $name = shift; |
421 | my $name = shift; |
396 | |
422 | |
… | |
… | |
432 | |
458 | |
433 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
459 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
434 | |
460 | |
435 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
461 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
436 | |
462 | |
|
|
463 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
|
|
464 | |
437 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
465 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
438 | |
466 | |
439 | Generate a global/object/player/map-specific event with the given arguments. |
467 | Generate a global/object/player/map-specific event with the given arguments. |
440 | |
468 | |
441 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
469 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
… | |
… | |
450 | |
478 | |
451 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
479 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
452 | |
480 | |
453 | =over 4 |
481 | =over 4 |
454 | |
482 | |
455 | =item $object->valid, $player->valid, $map->valid |
483 | =item $object->valid, $player->valid, $client->valid, $map->valid |
456 | |
484 | |
457 | Just because you have a perl object does not mean that the corresponding |
485 | 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 |
486 | 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 |
487 | 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 |
488 | can be used to test for existence of the C object part without causing an |
… | |
… | |
464 | |
492 | |
465 | =cut |
493 | =cut |
466 | |
494 | |
467 | *cf::object::valid = |
495 | *cf::object::valid = |
468 | *cf::player::valid = |
496 | *cf::player::valid = |
|
|
497 | *cf::client::valid = |
469 | *cf::map::valid = \&cf::_valid; |
498 | *cf::map::valid = \&cf::_valid; |
470 | |
499 | |
471 | ############################################################################# |
500 | ############################################################################# |
472 | # object support |
501 | # object support |
473 | |
502 | |
… | |
… | |
720 | |
749 | |
721 | Symbol::delete_package $pkg; |
750 | Symbol::delete_package $pkg; |
722 | } |
751 | } |
723 | |
752 | |
724 | sub load_extensions { |
753 | sub load_extensions { |
725 | my $LIBDIR = maps_directory "perl"; |
|
|
726 | |
|
|
727 | for my $ext (<$LIBDIR/*.ext>) { |
754 | for my $ext (<$LIBDIR/*.ext>) { |
728 | next unless -r $ext; |
755 | next unless -r $ext; |
729 | eval { |
756 | eval { |
730 | load_extension $ext; |
757 | load_extension $ext; |
731 | 1 |
758 | 1 |
… | |
… | |
866 | |
893 | |
867 | =pod |
894 | =pod |
868 | |
895 | |
869 | The following fucntions and emthods are available within a safe environment: |
896 | The following fucntions and emthods are available within a safe environment: |
870 | |
897 | |
871 | cf::object contr pay_amount pay_player |
898 | cf::object contr pay_amount pay_player map |
872 | cf::object::player player |
899 | cf::object::player player |
873 | cf::player peaceful |
900 | cf::player peaceful |
|
|
901 | cf::map trigger |
874 | |
902 | |
875 | =cut |
903 | =cut |
876 | |
904 | |
877 | for ( |
905 | for ( |
878 | ["cf::object" => qw(contr pay_amount pay_player)], |
906 | ["cf::object" => qw(contr pay_amount pay_player map)], |
879 | ["cf::object::player" => qw(player)], |
907 | ["cf::object::player" => qw(player)], |
880 | ["cf::player" => qw(peaceful)], |
908 | ["cf::player" => qw(peaceful)], |
|
|
909 | ["cf::map" => qw(trigger)], |
881 | ) { |
910 | ) { |
882 | no strict 'refs'; |
911 | no strict 'refs'; |
883 | my ($pkg, @funs) = @$_; |
912 | my ($pkg, @funs) = @$_; |
884 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
913 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
885 | for @funs; |
914 | for @funs; |
… | |
… | |
1173 | }; |
1202 | }; |
1174 | |
1203 | |
1175 | unshift @INC, $LIBDIR; |
1204 | unshift @INC, $LIBDIR; |
1176 | |
1205 | |
1177 | $TICK_WATCHER = Event->timer ( |
1206 | $TICK_WATCHER = Event->timer ( |
1178 | prio => 1, |
1207 | prio => 0, |
1179 | async => 1, |
|
|
1180 | at => $NEXT_TICK || 1, |
1208 | at => $NEXT_TICK || 1, |
1181 | data => WF_AUTOCANCEL, |
1209 | data => WF_AUTOCANCEL, |
1182 | cb => sub { |
1210 | cb => sub { |
1183 | cf::server_tick; # one server iteration |
1211 | cf::server_tick; # one server iteration |
1184 | |
1212 | |