… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use Time::HiRes; |
10 | use Event; |
11 | use Event; |
11 | $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 |
12 | |
13 | |
13 | use strict; |
14 | use strict; |
14 | |
15 | |
… | |
… | |
16 | our @EVENT; |
17 | our @EVENT; |
17 | our %PROP_TYPE; |
18 | our %PROP_TYPE; |
18 | our %PROP_IDX; |
19 | our %PROP_IDX; |
19 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
20 | |
21 | |
|
|
22 | our $TICK = MAX_TIME * 1e-6; |
|
|
23 | our $TICK_WATCHER; |
|
|
24 | our $NEXT_TICK; |
|
|
25 | |
21 | BEGIN { |
26 | BEGIN { |
22 | @EVENT = map lc, @EVENT; |
27 | @EVENT = map lc, @EVENT; |
23 | |
28 | |
24 | *CORE::GLOBAL::warn = sub { |
29 | *CORE::GLOBAL::warn = sub { |
25 | my $msg = join "", @_; |
30 | my $msg = join "", @_; |
… | |
… | |
70 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
75 | @ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
71 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
76 | @ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; |
72 | |
77 | |
73 | # we bless all objects into derived classes to force a method lookup |
78 | # we bless all objects into derived classes to force a method lookup |
74 | # within the Safe compartment. |
79 | # within the Safe compartment. |
75 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { |
80 | for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
76 | no strict 'refs'; |
81 | no strict 'refs'; |
77 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
82 | @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
78 | } |
83 | } |
79 | |
84 | |
80 | $Event::DIED = sub { |
85 | $Event::DIED = sub { |
… | |
… | |
180 | |
185 | |
181 | sub unload_extension { |
186 | sub unload_extension { |
182 | my ($pkg) = @_; |
187 | my ($pkg) = @_; |
183 | |
188 | |
184 | warn "removing extension $pkg\n"; |
189 | warn "removing extension $pkg\n"; |
185 | |
|
|
186 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
187 | $cb->($pkg); |
|
|
188 | } |
|
|
189 | |
190 | |
190 | # remove hooks |
191 | # remove hooks |
191 | for my $idx (0 .. $#EVENT) { |
192 | for my $idx (0 .. $#EVENT) { |
192 | delete $hook[$idx]{$pkg}; |
193 | delete $hook[$idx]{$pkg}; |
193 | } |
194 | } |
… | |
… | |
208 | # remove extcmds |
209 | # remove extcmds |
209 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
210 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
210 | delete $extcmd{$name}; |
211 | delete $extcmd{$name}; |
211 | } |
212 | } |
212 | |
213 | |
|
|
214 | if (my $cb = $pkg->can ("on_unload")) { |
|
|
215 | eval { |
|
|
216 | $cb->($pkg); |
|
|
217 | 1 |
|
|
218 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
219 | } |
|
|
220 | |
213 | Symbol::delete_package $pkg; |
221 | Symbol::delete_package $pkg; |
214 | } |
222 | } |
215 | |
223 | |
216 | sub load_extensions { |
224 | sub load_extensions { |
217 | my $LIBDIR = maps_directory "perl"; |
225 | my $LIBDIR = maps_directory "perl"; |
… | |
… | |
285 | $who->message ("Intruder Alert!"); |
293 | $who->message ("Intruder Alert!"); |
286 | } |
294 | } |
287 | }; |
295 | }; |
288 | |
296 | |
289 | ############################################################################# |
297 | ############################################################################# |
|
|
298 | # utility functions |
|
|
299 | |
|
|
300 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
301 | |
|
|
302 | sub from_json($) { |
|
|
303 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
304 | JSON::Syck::Load $_[0] |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub to_json($) { |
|
|
308 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
309 | JSON::Syck::Dump $_[0] |
|
|
310 | } |
|
|
311 | |
|
|
312 | ############################################################################# |
290 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
313 | # extcmd framework, basically convert ext <msg> |
291 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
314 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
292 | |
315 | |
293 | sub on_extcmd { |
316 | sub on_extcmd { |
294 | my ($pl, $buf) = @_; |
317 | my ($pl, $buf) = @_; |
295 | |
318 | |
296 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
319 | my $msg = eval { from_json $buf }; |
297 | |
320 | |
298 | $extcmd{$type}[0]->($pl, $buf) |
321 | if (ref $msg) { |
299 | if $extcmd{$type}; |
322 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
323 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
324 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
325 | } |
|
|
326 | } |
|
|
327 | } else { |
|
|
328 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
329 | } |
|
|
330 | |
|
|
331 | 1 |
300 | } |
332 | } |
301 | |
333 | |
302 | ############################################################################# |
334 | ############################################################################# |
303 | # load/save/clean perl data associated with a map |
335 | # load/save/clean perl data associated with a map |
304 | |
336 | |
… | |
… | |
357 | }; |
389 | }; |
358 | |
390 | |
359 | ############################################################################# |
391 | ############################################################################# |
360 | # load/save perl data associated with player->ob objects |
392 | # load/save perl data associated with player->ob objects |
361 | |
393 | |
|
|
394 | sub all_objects(@) { |
|
|
395 | @_, map all_objects ($_->inv), @_ |
|
|
396 | } |
|
|
397 | |
362 | *on_player_load = sub { |
398 | *on_player_load = sub { |
363 | my ($ob, $path) = @_; |
399 | my ($ob, $path) = @_; |
364 | |
400 | |
365 | for my $o ($ob, $ob->inv) { |
401 | for my $o (all_objects $ob) { |
366 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
402 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
367 | $o->set_ob_key_value ("_perl_data"); |
403 | $o->set_ob_key_value ("_perl_data"); |
368 | |
404 | |
369 | %$o = %{ Storable::thaw pack "H*", $value }; |
405 | %$o = %{ Storable::thaw pack "H*", $value }; |
370 | } |
406 | } |
… | |
… | |
373 | |
409 | |
374 | *on_player_save = sub { |
410 | *on_player_save = sub { |
375 | my ($ob, $path) = @_; |
411 | my ($ob, $path) = @_; |
376 | |
412 | |
377 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
413 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
378 | for grep %$_, $ob, $ob->inv; |
414 | for grep %$_, all_objects $ob; |
379 | }; |
415 | }; |
380 | |
416 | |
381 | ############################################################################# |
417 | ############################################################################# |
382 | # core extensions - in perl |
418 | # core extensions - in perl |
383 | |
419 | |
… | |
… | |
389 | |
425 | |
390 | sub cf::player::exists($) { |
426 | sub cf::player::exists($) { |
391 | cf::player::find $_[0] |
427 | cf::player::find $_[0] |
392 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
428 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
393 | } |
429 | } |
|
|
430 | |
|
|
431 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
432 | |
|
|
433 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
434 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
435 | dialogue with the given NPC character. |
|
|
436 | |
|
|
437 | =cut |
394 | |
438 | |
395 | # rough implementation of a future "reply" method that works |
439 | # rough implementation of a future "reply" method that works |
396 | # with dialog boxes. |
440 | # with dialog boxes. |
397 | sub cf::object::player::reply($$$;$) { |
441 | sub cf::object::player::reply($$$;$) { |
398 | my ($self, $npc, $msg, $flags) = @_; |
442 | my ($self, $npc, $msg, $flags) = @_; |
… | |
… | |
403 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
447 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
404 | } else { |
448 | } else { |
405 | $msg = $npc->name . " says: $msg" if $npc; |
449 | $msg = $npc->name . " says: $msg" if $npc; |
406 | $self->message ($msg, $flags); |
450 | $self->message ($msg, $flags); |
407 | } |
451 | } |
|
|
452 | } |
|
|
453 | |
|
|
454 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
455 | |
|
|
456 | Sends an ext reply to the player. |
|
|
457 | |
|
|
458 | =cut |
|
|
459 | |
|
|
460 | sub cf::player::ext_reply($$$%) { |
|
|
461 | my ($self, $id, %msg) = @_; |
|
|
462 | |
|
|
463 | $msg{msgid} = $id; |
|
|
464 | |
|
|
465 | $self->send ("ext " . to_json \%msg); |
408 | } |
466 | } |
409 | |
467 | |
410 | ############################################################################# |
468 | ############################################################################# |
411 | # map scripting support |
469 | # map scripting support |
412 | |
470 | |
… | |
… | |
461 | no strict 'refs'; |
519 | no strict 'refs'; |
462 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
520 | *{"ext::$fun"} = $safe_hole->wrap ($cb); |
463 | } |
521 | } |
464 | |
522 | |
465 | ############################################################################# |
523 | ############################################################################# |
|
|
524 | # the server's main() |
|
|
525 | |
|
|
526 | sub run { |
|
|
527 | Event::loop; |
|
|
528 | } |
|
|
529 | |
|
|
530 | ############################################################################# |
466 | # initialisation |
531 | # initialisation |
467 | |
532 | |
468 | register "<global>", __PACKAGE__; |
533 | register "<global>", __PACKAGE__; |
469 | |
534 | |
470 | unshift @INC, $LIBDIR; |
535 | unshift @INC, $LIBDIR; |
471 | |
536 | |
472 | load_extensions; |
537 | load_extensions; |
473 | |
538 | |
|
|
539 | $TICK_WATCHER = Event->timer ( |
|
|
540 | prio => 1, |
|
|
541 | at => $NEXT_TICK || 1, |
|
|
542 | cb => sub { |
|
|
543 | cf::server_tick; # one server iteration |
|
|
544 | |
|
|
545 | my $NOW = Event::time; |
|
|
546 | $NEXT_TICK += $TICK; |
|
|
547 | |
|
|
548 | # if we are delayed by > 0.25 second, skip ticks |
|
|
549 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; |
|
|
550 | |
|
|
551 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
552 | $TICK_WATCHER->start; |
|
|
553 | }, |
|
|
554 | ); |
|
|
555 | |
474 | 1 |
556 | 1 |
475 | |
557 | |