… | |
… | |
29 | use Fcntl; |
29 | use Fcntl; |
30 | use YAML::Syck (); |
30 | use YAML::Syck (); |
31 | use IO::AIO 2.32 (); |
31 | use IO::AIO 2.32 (); |
32 | use Time::HiRes; |
32 | use Time::HiRes; |
33 | use Compress::LZF; |
33 | use Compress::LZF; |
|
|
34 | use Digest::MD5 (); |
34 | |
35 | |
35 | # configure various modules to our taste |
36 | # configure various modules to our taste |
36 | # |
37 | # |
37 | $Storable::canonical = 1; # reduce rsync transfers |
38 | $Storable::canonical = 1; # reduce rsync transfers |
38 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
39 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
… | |
… | |
165 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
166 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
166 | |
167 | |
167 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
168 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
168 | returns directly I<after> the tick processing (and consequently, can only wake one process |
169 | returns directly I<after> the tick processing (and consequently, can only wake one process |
169 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
170 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
171 | |
|
|
172 | =item @cf::INVOKE_RESULTS |
|
|
173 | |
|
|
174 | This array contains the results of the last C<invoke ()> call. When |
|
|
175 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
|
|
176 | that call. |
170 | |
177 | |
171 | =back |
178 | =back |
172 | |
179 | |
173 | =cut |
180 | =cut |
174 | |
181 | |
… | |
… | |
315 | }; |
322 | }; |
316 | $TICK_WATCHER->stop; |
323 | $TICK_WATCHER->stop; |
317 | $guard |
324 | $guard |
318 | } |
325 | } |
319 | |
326 | |
|
|
327 | =item cf::get_slot $time[, $priority] |
|
|
328 | |
|
|
329 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
|
|
330 | This call blocks and returns only when you have at least C<$time> seconds |
|
|
331 | of cpu time till the next tick. The slot is only valid till the next cede. |
|
|
332 | |
|
|
333 | Useful for short background jobs. |
|
|
334 | |
|
|
335 | =cut |
|
|
336 | |
|
|
337 | our @SLOT_QUEUE; |
|
|
338 | our $SLOT_QUEUE; |
|
|
339 | |
|
|
340 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
|
|
341 | $SLOT_QUEUE = Coro::async { |
|
|
342 | my $signal = new Coro::Signal; |
|
|
343 | |
|
|
344 | while () { |
|
|
345 | next_job: |
|
|
346 | my $avail = cf::till_tick; |
|
|
347 | if ($avail > 0.01) { |
|
|
348 | for (0 .. $#SLOT_QUEUE) { |
|
|
349 | if ($SLOT_QUEUE[$_][0] < $avail) { |
|
|
350 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
|
|
351 | $job->[2]->send; |
|
|
352 | Coro::cede; |
|
|
353 | goto next_job; |
|
|
354 | } |
|
|
355 | } |
|
|
356 | } |
|
|
357 | |
|
|
358 | if (@SLOT_QUEUE) { |
|
|
359 | # we do not use wait_For_tick() as it returns immediately when tick is inactive |
|
|
360 | push @cf::WAIT_FOR_TICK, $signal; |
|
|
361 | $signal->wait; |
|
|
362 | } else { |
|
|
363 | Coro::schedule; |
|
|
364 | } |
|
|
365 | } |
|
|
366 | }; |
|
|
367 | |
|
|
368 | sub get_slot($;$) { |
|
|
369 | my ($time, $pri) = @_; |
|
|
370 | |
|
|
371 | push @SLOT_QUEUE, [$time, $pri, my $sig = new Coro::Signal]; |
|
|
372 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
|
|
373 | $SLOT_QUEUE->ready; |
|
|
374 | $sig->wait; |
|
|
375 | } |
|
|
376 | |
320 | =item cf::async { BLOCK } |
377 | =item cf::async { BLOCK } |
321 | |
378 | |
322 | Currently the same as Coro::async_pool, meaning you cannot use |
379 | Currently the same as Coro::async_pool, meaning you cannot use |
323 | C<on_destroy>, C<join> or other gimmicks on these coroutines. The only |
380 | C<on_destroy>, C<join> or other gimmicks on these coroutines. The only |
324 | thing you are allowed to do is call C<prio> on it. |
381 | thing you are allowed to do is call C<prio> on it. |
… | |
… | |
865 | "; |
922 | "; |
866 | die if $@; |
923 | die if $@; |
867 | } |
924 | } |
868 | |
925 | |
869 | our $override; |
926 | our $override; |
870 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
927 | our @INVOKE_RESULTS = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
871 | |
928 | |
872 | sub override { |
929 | sub override { |
873 | $override = 1; |
930 | $override = 1; |
874 | @invoke_results = (); |
931 | @INVOKE_RESULTS = (@_); |
875 | } |
932 | } |
876 | |
933 | |
877 | sub do_invoke { |
934 | sub do_invoke { |
878 | my $event = shift; |
935 | my $event = shift; |
879 | my $callbacks = shift; |
936 | my $callbacks = shift; |
880 | |
937 | |
881 | @invoke_results = (); |
938 | @INVOKE_RESULTS = (); |
882 | |
939 | |
883 | local $override; |
940 | local $override; |
884 | |
941 | |
885 | for (@$callbacks) { |
942 | for (@$callbacks) { |
886 | eval { &{$_->[1]} }; |
943 | eval { &{$_->[1]} }; |
… | |
… | |
903 | |
960 | |
904 | Generate an object-specific event with the given arguments. |
961 | Generate an object-specific event with the given arguments. |
905 | |
962 | |
906 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
963 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
907 | removed in future versions), and there is no public API to access override |
964 | removed in future versions), and there is no public API to access override |
908 | results (if you must, access C<@cf::invoke_results> directly). |
965 | results (if you must, access C<@cf::INVOKE_RESULTS> directly). |
909 | |
966 | |
910 | =back |
967 | =back |
911 | |
968 | |
912 | =cut |
969 | =cut |
913 | |
970 | |
914 | ############################################################################# |
971 | ############################################################################# |
915 | # object support |
972 | # object support |
|
|
973 | # |
|
|
974 | |
|
|
975 | sub _can_merge { |
|
|
976 | my ($ob1, $ob2) = @_; |
|
|
977 | |
|
|
978 | local $Storable::canonical = 1; |
|
|
979 | my $fob1 = Storable::freeze $ob1; |
|
|
980 | my $fob2 = Storable::freeze $ob2; |
|
|
981 | |
|
|
982 | $fob1 eq $fob2 |
|
|
983 | } |
916 | |
984 | |
917 | sub reattach { |
985 | sub reattach { |
918 | # basically do the same as instantiate, without calling instantiate |
986 | # basically do the same as instantiate, without calling instantiate |
919 | my ($obj) = @_; |
987 | my ($obj) = @_; |
920 | |
988 | |
… | |
… | |
1488 | my ($path) = @_; |
1556 | my ($path) = @_; |
1489 | |
1557 | |
1490 | my ($match, $specificity); |
1558 | my ($match, $specificity); |
1491 | |
1559 | |
1492 | for my $region (list) { |
1560 | for my $region (list) { |
1493 | if ($region->match && $path =~ $region->match) { |
1561 | if ($region->{match} && $path =~ $region->{match}) { |
1494 | ($match, $specificity) = ($region, $region->specificity) |
1562 | ($match, $specificity) = ($region, $region->specificity) |
1495 | if $region->specificity > $specificity; |
1563 | if $region->specificity > $specificity; |
1496 | } |
1564 | } |
1497 | } |
1565 | } |
1498 | |
1566 | |
… | |
… | |
1618 | $self->init; # pass $1 etc. |
1686 | $self->init; # pass $1 etc. |
1619 | return $self; |
1687 | return $self; |
1620 | } |
1688 | } |
1621 | } |
1689 | } |
1622 | |
1690 | |
1623 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1691 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1624 | () |
1692 | () |
1625 | } |
1693 | } |
1626 | |
1694 | |
1627 | sub init { |
1695 | sub init { |
1628 | my ($self) = @_; |
1696 | my ($self) = @_; |
… | |
… | |
1906 | |
1974 | |
1907 | undef $MAP_PREFETCH{$path}; |
1975 | undef $MAP_PREFETCH{$path}; |
1908 | $MAP_PREFETCHER ||= cf::async { |
1976 | $MAP_PREFETCHER ||= cf::async { |
1909 | while (%MAP_PREFETCH) { |
1977 | while (%MAP_PREFETCH) { |
1910 | for my $path (keys %MAP_PREFETCH) { |
1978 | for my $path (keys %MAP_PREFETCH) { |
1911 | my $map = find $path |
1979 | if (my $map = find $path) { |
1912 | or next; |
|
|
1913 | $map->load; |
1980 | $map->load; |
|
|
1981 | } |
1914 | |
1982 | |
1915 | delete $MAP_PREFETCH{$path}; |
1983 | delete $MAP_PREFETCH{$path}; |
1916 | } |
1984 | } |
1917 | } |
1985 | } |
1918 | undef $MAP_PREFETCHER; |
1986 | undef $MAP_PREFETCHER; |
… | |
… | |
2255 | |
2323 | |
2256 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2324 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2257 | $self->enter_map ($map, $x, $y); |
2325 | $self->enter_map ($map, $x, $y); |
2258 | } |
2326 | } |
2259 | |
2327 | |
2260 | =item $player_object->goto ($path, $x, $y[, $check->($map)]) |
2328 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2261 | |
2329 | |
2262 | Moves the player to the given map-path and coordinates by first freezing |
2330 | Moves the player to the given map-path and coordinates by first freezing |
2263 | her, loading and preparing them map, calling the provided $check callback |
2331 | her, loading and preparing them map, calling the provided $check callback |
2264 | that has to return the map if sucecssful, and then unfreezes the player on |
2332 | that has to return the map if sucecssful, and then unfreezes the player on |
2265 | the new (success) or old (failed) map position. |
2333 | the new (success) or old (failed) map position. In either case, $done will |
|
|
2334 | be called at the end of this process. |
2266 | |
2335 | |
2267 | =cut |
2336 | =cut |
2268 | |
2337 | |
2269 | our $GOTOGEN; |
2338 | our $GOTOGEN; |
2270 | |
2339 | |
2271 | sub cf::object::player::goto { |
2340 | sub cf::object::player::goto { |
2272 | my ($self, $path, $x, $y, $check) = @_; |
2341 | my ($self, $path, $x, $y, $check, $done) = @_; |
2273 | |
2342 | |
2274 | # do generation counting so two concurrent goto's will be executed in-order |
2343 | # do generation counting so two concurrent goto's will be executed in-order |
2275 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2344 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2276 | |
2345 | |
2277 | $self->enter_link; |
2346 | $self->enter_link; |
… | |
… | |
2297 | |
2366 | |
2298 | if ($gen == $self->{_goto_generation}) { |
2367 | if ($gen == $self->{_goto_generation}) { |
2299 | delete $self->{_goto_generation}; |
2368 | delete $self->{_goto_generation}; |
2300 | $self->leave_link ($map, $x, $y); |
2369 | $self->leave_link ($map, $x, $y); |
2301 | } |
2370 | } |
|
|
2371 | |
|
|
2372 | $done->() if $done; |
2302 | })->prio (1); |
2373 | })->prio (1); |
2303 | } |
2374 | } |
2304 | |
2375 | |
2305 | =item $player_object->enter_exit ($exit_object) |
2376 | =item $player_object->enter_exit ($exit_object) |
2306 | |
2377 | |
… | |
… | |
2415 | |
2486 | |
2416 | utf8::encode $text; |
2487 | utf8::encode $text; |
2417 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2488 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2418 | } |
2489 | } |
2419 | |
2490 | |
2420 | =item $client->send_msg ($color, $type, $msg, [extra...]) |
2491 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2421 | |
2492 | |
2422 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2493 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2423 | client if neccessary. C<$type> should be a string identifying the type of |
2494 | client if neccessary. C<$type> should be a string identifying the type of |
2424 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2495 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2425 | the message unless the client supports the msg packet. |
2496 | the message unless the client supports the msg packet. |
2426 | |
2497 | |
2427 | =cut |
2498 | =cut |
2428 | |
2499 | |
2429 | sub cf::client::send_msg { |
2500 | sub cf::client::send_msg { |
2430 | my ($self, $color, $type, $msg, @extra) = @_; |
2501 | my ($self, $channel, $msg, $color, @extra) = @_; |
2431 | |
2502 | |
2432 | $msg = $self->pl->expand_cfpod ($msg); |
2503 | $msg = $self->pl->expand_cfpod ($msg); |
2433 | |
2504 | |
|
|
2505 | $color &= ~cf::NDI_UNIQUE; # just in case... |
|
|
2506 | |
|
|
2507 | if (ref $channel) { |
|
|
2508 | # send meta info to client, if not yet sent |
|
|
2509 | unless (exists $self->{channel}{$channel->{id}}) { |
|
|
2510 | $self->{channel}{$channel->{id}} = $channel; |
|
|
2511 | $self->ext_event (channel_info => %$channel); |
|
|
2512 | } |
|
|
2513 | |
|
|
2514 | $channel = $channel->{id}; |
|
|
2515 | } |
|
|
2516 | |
2434 | return unless @extra || length $msg; |
2517 | return unless @extra || length $msg; |
2435 | |
2518 | |
2436 | if ($self->can_msg) { |
2519 | if ($self->can_msg) { |
2437 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); |
2520 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); |
2438 | } else { |
2521 | } else { |
2439 | # replace some tags by gcfclient-compatible ones |
2522 | # replace some tags by gcfclient-compatible ones |
2440 | for ($msg) { |
2523 | for ($msg) { |
2441 | 1 while |
2524 | 1 while |
2442 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
2525 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
… | |
… | |
2463 | |
2546 | |
2464 | =cut |
2547 | =cut |
2465 | |
2548 | |
2466 | sub cf::client::ext_event($$%) { |
2549 | sub cf::client::ext_event($$%) { |
2467 | my ($self, $type, %msg) = @_; |
2550 | my ($self, $type, %msg) = @_; |
|
|
2551 | |
|
|
2552 | return unless $self->extcmd; |
2468 | |
2553 | |
2469 | $msg{msgtype} = "event_$type"; |
2554 | $msg{msgtype} = "event_$type"; |
2470 | $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); |
2555 | $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); |
2471 | } |
2556 | } |
2472 | |
2557 | |
… | |
… | |
2719 | { |
2804 | { |
2720 | my $faces = $facedata->{faceinfo}; |
2805 | my $faces = $facedata->{faceinfo}; |
2721 | |
2806 | |
2722 | while (my ($face, $info) = each %$faces) { |
2807 | while (my ($face, $info) = each %$faces) { |
2723 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2808 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2724 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
2809 | cf::face::set_visibility $idx, $info->{visibility}; |
|
|
2810 | cf::face::set_magicmap $idx, $info->{magicmap}; |
2725 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2811 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2726 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2812 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2727 | Coro::cede; |
2813 | |
|
|
2814 | cf::cede_to_tick; |
2728 | } |
2815 | } |
2729 | |
2816 | |
2730 | while (my ($face, $info) = each %$faces) { |
2817 | while (my ($face, $info) = each %$faces) { |
2731 | next unless $info->{smooth}; |
2818 | next unless $info->{smooth}; |
2732 | my $idx = cf::face::find $face |
2819 | my $idx = cf::face::find $face |
2733 | or next; |
2820 | or next; |
2734 | if (my $smooth = cf::face::find $info->{smooth}) { |
2821 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2822 | cf::face::set_smooth $idx, $smooth; |
2735 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
2823 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
2736 | } else { |
2824 | } else { |
2737 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2825 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2738 | } |
2826 | } |
2739 | Coro::cede; |
2827 | |
|
|
2828 | cf::cede_to_tick; |
2740 | } |
2829 | } |
2741 | } |
2830 | } |
2742 | |
2831 | |
2743 | { |
2832 | { |
2744 | my $anims = $facedata->{animinfo}; |
2833 | my $anims = $facedata->{animinfo}; |
2745 | |
2834 | |
2746 | while (my ($anim, $info) = each %$anims) { |
2835 | while (my ($anim, $info) = each %$anims) { |
2747 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
2836 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
2748 | Coro::cede; |
2837 | cf::cede_to_tick; |
2749 | } |
2838 | } |
2750 | |
2839 | |
2751 | cf::anim::invalidate_all; # d'oh |
2840 | cf::anim::invalidate_all; # d'oh |
|
|
2841 | } |
|
|
2842 | |
|
|
2843 | { |
|
|
2844 | # TODO: for gcfclient pleasure, we should give resources |
|
|
2845 | # that gcfclient doesn't grok a >10000 face index. |
|
|
2846 | my $res = $facedata->{resource}; |
|
|
2847 | my $enc = JSON::XS->new->utf8->canonical; |
|
|
2848 | |
|
|
2849 | while (my ($name, $info) = each %$res) { |
|
|
2850 | my $meta = $enc->encode ({ |
|
|
2851 | name => $name, |
|
|
2852 | type => $info->{type}, |
|
|
2853 | copyright => $info->{copyright}, #TODO# |
|
|
2854 | }); |
|
|
2855 | |
|
|
2856 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
|
|
2857 | |
|
|
2858 | if ($name =~ /\.jpg$/) { |
|
|
2859 | cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack |
|
|
2860 | cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack |
|
|
2861 | } else { |
|
|
2862 | my $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
2863 | my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata |
|
|
2864 | |
|
|
2865 | cf::face::set_type $idx, 1; |
|
|
2866 | cf::face::set_data $idx, 0, $data, $chk; |
|
|
2867 | } |
|
|
2868 | |
|
|
2869 | cf::cede_to_tick; |
|
|
2870 | } |
2752 | } |
2871 | } |
2753 | |
2872 | |
2754 | 1 |
2873 | 1 |
2755 | } |
2874 | } |
2756 | |
2875 | |
2757 | sub reload_regions { |
2876 | sub reload_regions { |
2758 | load_resource_file "$MAPDIR/regions" |
2877 | load_resource_file "$MAPDIR/regions" |
2759 | or die "unable to load regions file\n"; |
2878 | or die "unable to load regions file\n"; |
|
|
2879 | |
|
|
2880 | for (cf::region::list) { |
|
|
2881 | $_->{match} = qr/$_->{match}/ |
|
|
2882 | if exists $_->{match}; |
|
|
2883 | } |
2760 | } |
2884 | } |
2761 | |
2885 | |
2762 | sub reload_facedata { |
2886 | sub reload_facedata { |
2763 | load_facedata "$DATADIR/facedata" |
2887 | load_facedata "$DATADIR/facedata" |
2764 | or die "unable to load facedata\n"; |
2888 | or die "unable to load facedata\n"; |