… | |
… | |
1181 | my ($pl, $buf) = @_; |
1181 | my ($pl, $buf) = @_; |
1182 | |
1182 | |
1183 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1183 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1184 | |
1184 | |
1185 | if (ref $msg) { |
1185 | if (ref $msg) { |
|
|
1186 | my ($type, $reply, @payload) = |
|
|
1187 | "ARRAY" eq ref $msg |
|
|
1188 | ? @$msg |
|
|
1189 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
1190 | |
1186 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
1191 | if (my $cb = $EXTCMD{$type}) { |
1187 | if (my %reply = $cb->($pl, $msg)) { |
1192 | my @reply = $cb->($pl, @payload); |
|
|
1193 | |
1188 | $pl->ext_reply ($msg->{msgid}, %reply); |
1194 | $pl->ext_reply ($reply, @reply) |
1189 | } |
1195 | if $reply; |
1190 | } |
1196 | } |
1191 | } else { |
1197 | } else { |
1192 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1198 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1193 | } |
1199 | } |
1194 | |
1200 | |
… | |
… | |
1364 | $self->{deny_save} = 1; |
1370 | $self->{deny_save} = 1; |
1365 | |
1371 | |
1366 | $cf::PLAYER{$login} = $self; |
1372 | $cf::PLAYER{$login} = $self; |
1367 | |
1373 | |
1368 | $self |
1374 | $self |
|
|
1375 | } |
|
|
1376 | |
|
|
1377 | =item $player->send_msg ($channel, $msg, $color, [extra...]) |
|
|
1378 | |
|
|
1379 | =cut |
|
|
1380 | |
|
|
1381 | sub send_msg { |
|
|
1382 | my $ns = shift->ns |
|
|
1383 | or return; |
|
|
1384 | $ns->send_msg (@_); |
1369 | } |
1385 | } |
1370 | |
1386 | |
1371 | =item $pl->quit_character |
1387 | =item $pl->quit_character |
1372 | |
1388 | |
1373 | Nukes the player without looking back. If logged in, the connection will |
1389 | Nukes the player without looking back. If logged in, the connection will |
… | |
… | |
1517 | sub hintmode { |
1533 | sub hintmode { |
1518 | $_[0]{hintmode} = $_[1] if @_ > 1; |
1534 | $_[0]{hintmode} = $_[1] if @_ > 1; |
1519 | $_[0]{hintmode} |
1535 | $_[0]{hintmode} |
1520 | } |
1536 | } |
1521 | |
1537 | |
1522 | =item $player->ext_reply ($msgid, %msg) |
1538 | =item $player->ext_reply ($msgid, @msg) |
1523 | |
1539 | |
1524 | Sends an ext reply to the player. |
1540 | Sends an ext reply to the player. |
1525 | |
1541 | |
1526 | =cut |
1542 | =cut |
1527 | |
1543 | |
1528 | sub ext_reply($$%) { |
1544 | sub ext_reply($$@) { |
1529 | my ($self, $id, %msg) = @_; |
1545 | my ($self, $id, @msg) = @_; |
1530 | |
1546 | |
1531 | $msg{msgid} = $id; |
1547 | if ($self->ns->extcmd == 2) { |
|
|
1548 | $self->send ("ext " . $self->ns->{json_coder}->encode (["reply-$id", @msg])); |
|
|
1549 | } elsif ($self->ns->extcmd == 1) { |
|
|
1550 | #TODO: version 1, remove |
|
|
1551 | unshift @msg, msgtype => "reply", msgid => $id; |
1532 | $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg)); |
1552 | $self->send ("ext " . $self->ns->{json_coder}->encode ({@msg})); |
|
|
1553 | } |
1533 | } |
1554 | } |
1534 | |
1555 | |
1535 | =item $player->ext_event ($type, %msg) |
1556 | =item $player->ext_msg ($type, @msg) |
1536 | |
1557 | |
1537 | Sends an ext event to the client. |
1558 | Sends an ext event to the client. |
1538 | |
1559 | |
1539 | =cut |
1560 | =cut |
1540 | |
1561 | |
1541 | sub ext_event($$%) { |
1562 | sub ext_msg($$@) { |
1542 | my ($self, $type, %msg) = @_; |
1563 | my ($self, $type, @msg) = @_; |
1543 | |
1564 | |
1544 | $self->ns->ext_event ($type, %msg); |
1565 | $self->ns->ext_msg ($type, @msg); |
1545 | } |
1566 | } |
1546 | |
1567 | |
1547 | =head3 cf::region |
1568 | =head3 cf::region |
1548 | |
1569 | |
1549 | =over 4 |
1570 | =over 4 |
… | |
… | |
1910 | $self->set_darkness_map; |
1931 | $self->set_darkness_map; |
1911 | Coro::cede; |
1932 | Coro::cede; |
1912 | $self->activate; |
1933 | $self->activate; |
1913 | } |
1934 | } |
1914 | |
1935 | |
|
|
1936 | $self->{last_save} = $cf::RUNTIME; |
|
|
1937 | $self->last_access ($cf::RUNTIME); |
|
|
1938 | |
1915 | $self->in_memory (cf::MAP_IN_MEMORY); |
1939 | $self->in_memory (cf::MAP_IN_MEMORY); |
1916 | } |
1940 | } |
1917 | |
1941 | |
1918 | $self->post_load; |
1942 | $self->post_load; |
1919 | } |
1943 | } |
… | |
… | |
2219 | |
2243 | |
2220 | } else { |
2244 | } else { |
2221 | my $pl = $self->contr; |
2245 | my $pl = $self->contr; |
2222 | |
2246 | |
2223 | if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) { |
2247 | if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) { |
2224 | my $diag = $pl->{npc_dialog}; |
2248 | my $dialog = $pl->{npc_dialog}; |
2225 | $diag->{pl}->ext_reply ( |
2249 | $dialog->{pl}->ext_msg ($dialog->{id}, update => msg => $dialog->{pl}->expand_cfpod ($msg)); |
2226 | $diag->{id}, |
|
|
2227 | msgtype => "reply", |
|
|
2228 | msg => $diag->{pl}->expand_cfpod ($msg), |
|
|
2229 | add_topics => [] |
|
|
2230 | ); |
|
|
2231 | |
2250 | |
2232 | } else { |
2251 | } else { |
2233 | $msg = $npc->name . " says: $msg" if $npc; |
2252 | $msg = $npc->name . " says: $msg" if $npc; |
2234 | $self->message ($msg, $flags); |
2253 | $self->message ($msg, $flags); |
2235 | } |
2254 | } |
2236 | } |
2255 | } |
|
|
2256 | } |
|
|
2257 | |
|
|
2258 | =item $object->send_msg ($channel, $msg, $color, [extra...]) |
|
|
2259 | |
|
|
2260 | =cut |
|
|
2261 | |
|
|
2262 | sub cf::object::send_msg { |
|
|
2263 | my $pl = shift->contr |
|
|
2264 | or return; |
|
|
2265 | $pl->send_msg (@_); |
2237 | } |
2266 | } |
2238 | |
2267 | |
2239 | =item $player_object->may ("access") |
2268 | =item $player_object->may ("access") |
2240 | |
2269 | |
2241 | Returns wether the given player is authorized to access resource "access" |
2270 | Returns wether the given player is authorized to access resource "access" |
… | |
… | |
2506 | sub cf::client::send_msg { |
2535 | sub cf::client::send_msg { |
2507 | my ($self, $channel, $msg, $color, @extra) = @_; |
2536 | my ($self, $channel, $msg, $color, @extra) = @_; |
2508 | |
2537 | |
2509 | $msg = $self->pl->expand_cfpod ($msg); |
2538 | $msg = $self->pl->expand_cfpod ($msg); |
2510 | |
2539 | |
2511 | $color &= ~cf::NDI_UNIQUE; # just in case... |
2540 | $color &= cf::NDI_CLIENT_MASK; # just in case... |
2512 | |
2541 | |
2513 | if (ref $channel) { |
2542 | if (ref $channel) { |
2514 | # send meta info to client, if not yet sent |
2543 | # send meta info to client, if not yet sent |
2515 | unless (exists $self->{channel}{$channel->{id}}) { |
2544 | unless (exists $self->{channel}{$channel->{id}}) { |
2516 | $self->{channel}{$channel->{id}} = $channel; |
2545 | $self->{channel}{$channel->{id}} = $channel; |
2517 | $self->ext_event (channel_info => %$channel); |
2546 | $self->ext_msg (channel_info => $channel); |
2518 | } |
2547 | } |
2519 | |
2548 | |
2520 | $channel = $channel->{id}; |
2549 | $channel = $channel->{id}; |
2521 | } |
2550 | } |
2522 | |
2551 | |
2523 | return unless @extra || length $msg; |
2552 | return unless @extra || length $msg; |
2524 | |
2553 | |
2525 | if ($self->can_msg) { |
2554 | if ($self->can_msg) { |
|
|
2555 | # default colour, mask it out |
|
|
2556 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
|
|
2557 | if $color & cf::NDI_DEF; |
|
|
2558 | |
2526 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); |
2559 | $self->send_packet ("msg " . $self->{json_coder}->encode ( |
|
|
2560 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra])); |
2527 | } else { |
2561 | } else { |
2528 | # replace some tags by gcfclient-compatible ones |
|
|
2529 | for ($msg) { |
|
|
2530 | 1 while |
|
|
2531 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
|
|
2532 | || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ |
|
|
2533 | || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ |
|
|
2534 | || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ |
|
|
2535 | || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/; |
|
|
2536 | } |
|
|
2537 | |
|
|
2538 | if ($color >= 0) { |
2562 | if ($color >= 0) { |
|
|
2563 | # replace some tags by gcfclient-compatible ones |
|
|
2564 | for ($msg) { |
|
|
2565 | 1 while |
|
|
2566 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
|
|
2567 | || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ |
|
|
2568 | || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ |
|
|
2569 | || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ |
|
|
2570 | || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/; |
|
|
2571 | } |
|
|
2572 | |
|
|
2573 | $color &= cf::NDI_COLOR_MASK; |
|
|
2574 | |
|
|
2575 | utf8::encode $msg; |
|
|
2576 | |
2539 | if (0 && $msg =~ /\[/) { |
2577 | if (0 && $msg =~ /\[/) { |
2540 | $self->send_packet ("drawextinfo $color 4 0 $msg") |
2578 | $self->send_packet ("drawextinfo $color 4 0 $msg") |
2541 | } else { |
2579 | } else { |
2542 | $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; |
2580 | $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; |
2543 | $self->send_packet ("drawinfo $color $msg") |
2581 | $self->send_packet ("drawinfo $color $msg") |
2544 | } |
2582 | } |
2545 | } |
2583 | } |
2546 | } |
2584 | } |
2547 | } |
2585 | } |
2548 | |
2586 | |
2549 | =item $client->ext_event ($type, %msg) |
2587 | =item $client->ext_msg ($type, @msg) |
2550 | |
2588 | |
2551 | Sends an ext event to the client. |
2589 | Sends an ext event to the client. |
2552 | |
2590 | |
2553 | =cut |
2591 | =cut |
2554 | |
2592 | |
2555 | sub cf::client::ext_event($$%) { |
2593 | sub cf::client::ext_msg($$@) { |
2556 | my ($self, $type, %msg) = @_; |
2594 | my ($self, $type, @msg) = @_; |
2557 | |
2595 | |
2558 | return unless $self->extcmd; |
2596 | my $extcmd = $self->extcmd; |
2559 | |
2597 | |
|
|
2598 | if ($extcmd == 2) { |
|
|
2599 | $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); |
|
|
2600 | } elsif ($extcmd == 1) { # TODO: remove |
2560 | $msg{msgtype} = "event_$type"; |
2601 | push @msg, msgtype => "event_$type"; |
2561 | $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); |
2602 | $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
|
|
2603 | } |
2562 | } |
2604 | } |
2563 | |
2605 | |
2564 | =item $success = $client->query ($flags, "text", \&cb) |
2606 | =item $success = $client->query ($flags, "text", \&cb) |
2565 | |
2607 | |
2566 | Queues a query to the client, calling the given callback with |
2608 | Queues a query to the client, calling the given callback with |
… | |
… | |
2621 | my ($ns, $buf) = @_; |
2663 | my ($ns, $buf) = @_; |
2622 | |
2664 | |
2623 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
2665 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
2624 | |
2666 | |
2625 | if (ref $msg) { |
2667 | if (ref $msg) { |
|
|
2668 | my ($type, $reply, @payload) = |
|
|
2669 | "ARRAY" eq ref $msg |
|
|
2670 | ? @$msg |
|
|
2671 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
2672 | |
2626 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
2673 | if (my $cb = $EXTICMD{$type}) { |
2627 | if (my %reply = $cb->($ns, $msg)) { |
2674 | my @reply = $cb->($ns, @payload); |
2628 | $reply{msgid} = $msg->{msgid}; |
2675 | |
2629 | $ns->send ("ext " . $ns->{json_coder}->encode (\%reply)); |
2676 | $ns->ext_reply ($reply, @reply) |
2630 | } |
2677 | if $reply; |
2631 | } |
2678 | } |
2632 | } else { |
2679 | } else { |
2633 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
2680 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
2634 | } |
2681 | } |
2635 | |
2682 | |
… | |
… | |
2683 | our $safe = new Safe "safe"; |
2730 | our $safe = new Safe "safe"; |
2684 | our $safe_hole = new Safe::Hole; |
2731 | our $safe_hole = new Safe::Hole; |
2685 | |
2732 | |
2686 | $SIG{FPE} = 'IGNORE'; |
2733 | $SIG{FPE} = 'IGNORE'; |
2687 | |
2734 | |
2688 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
2735 | $safe->permit_only (Opcode::opset qw( |
|
|
2736 | :base_core :base_mem :base_orig :base_math |
|
|
2737 | grepstart grepwhile mapstart mapwhile |
|
|
2738 | sort time |
|
|
2739 | )); |
2689 | |
2740 | |
2690 | # here we export the classes and methods available to script code |
2741 | # here we export the classes and methods available to script code |
2691 | |
2742 | |
2692 | =pod |
2743 | =pod |
2693 | |
2744 | |
… | |
… | |
2708 | |
2759 | |
2709 | =cut |
2760 | =cut |
2710 | |
2761 | |
2711 | for ( |
2762 | for ( |
2712 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2763 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2713 | insert remove)], |
2764 | insert remove inv)], |
2714 | ["cf::object::player" => qw(player)], |
2765 | ["cf::object::player" => qw(player)], |
2715 | ["cf::player" => qw(peaceful)], |
2766 | ["cf::player" => qw(peaceful)], |
2716 | ["cf::map" => qw(trigger)], |
2767 | ["cf::map" => qw(trigger)], |
2717 | ) { |
2768 | ) { |
2718 | no strict 'refs'; |
2769 | no strict 'refs'; |
… | |
… | |
2850 | # TODO: for gcfclient pleasure, we should give resources |
2901 | # TODO: for gcfclient pleasure, we should give resources |
2851 | # that gcfclient doesn't grok a >10000 face index. |
2902 | # that gcfclient doesn't grok a >10000 face index. |
2852 | my $res = $facedata->{resource}; |
2903 | my $res = $facedata->{resource}; |
2853 | my $enc = JSON::XS->new->utf8->canonical; |
2904 | my $enc = JSON::XS->new->utf8->canonical; |
2854 | |
2905 | |
|
|
2906 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
2907 | |
2855 | while (my ($name, $info) = each %$res) { |
2908 | while (my ($name, $info) = each %$res) { |
2856 | my $meta = $enc->encode ({ |
2909 | my $meta = $enc->encode ({ |
2857 | name => $name, |
2910 | name => $name, |
2858 | type => $info->{type}, |
2911 | %{ $info->{meta} || {} }, |
2859 | copyright => $info->{copyright}, #TODO# |
|
|
2860 | }); |
2912 | }); |
2861 | |
2913 | |
2862 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
2914 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
2863 | |
2915 | |
2864 | if ($name =~ /\.jpg$/) { |
2916 | if ($info->{type} & 1) { |
2865 | cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack |
2917 | # prepend meta info |
2866 | cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack |
2918 | |
2867 | } else { |
|
|
2868 | my $data = pack "(w/a*)*", $meta, $info->{data}; |
2919 | my $data = pack "(w/a*)*", $meta, $info->{data}; |
2869 | my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata |
2920 | my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata |
2870 | |
2921 | |
2871 | cf::face::set_type $idx, 1; |
|
|
2872 | cf::face::set_data $idx, 0, $data, $chk; |
2922 | cf::face::set_data $idx, 0, $data, $chk; |
|
|
2923 | } else { |
|
|
2924 | cf::face::set_data $idx, 0, $info->{data}, $info->{chksum}; |
2873 | } |
2925 | } |
2874 | |
2926 | |
|
|
2927 | cf::face::set_type $idx, $info->{type}; |
|
|
2928 | |
2875 | cf::cede_to_tick; |
2929 | cf::cede_to_tick; |
2876 | } |
2930 | } |
|
|
2931 | |
|
|
2932 | if ($soundconf) { |
|
|
2933 | $soundconf = $enc->decode (delete $soundconf->{data}); |
|
|
2934 | |
|
|
2935 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
2936 | my $sound = $soundconf->{compat}[$_] |
|
|
2937 | or next; |
|
|
2938 | |
|
|
2939 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
2940 | cf::sound::set $sound->[0] => $face; |
|
|
2941 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
2942 | } |
|
|
2943 | |
|
|
2944 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
2945 | my $face = cf::face::find "sound/$v"; |
|
|
2946 | cf::sound::set $k => $face; |
|
|
2947 | } |
|
|
2948 | } |
2877 | } |
2949 | } |
2878 | |
2950 | |
2879 | 1 |
2951 | 1 |
2880 | } |
2952 | } |
|
|
2953 | |
|
|
2954 | register_exticmd fx_want => sub { |
|
|
2955 | my ($ns, $want) = @_; |
|
|
2956 | |
|
|
2957 | while (my ($k, $v) = each %$want) { |
|
|
2958 | $ns->fx_want ($k, $v); |
|
|
2959 | } |
|
|
2960 | }; |
2881 | |
2961 | |
2882 | sub reload_regions { |
2962 | sub reload_regions { |
2883 | load_resource_file "$MAPDIR/regions" |
2963 | load_resource_file "$MAPDIR/regions" |
2884 | or die "unable to load regions file\n"; |
2964 | or die "unable to load regions file\n"; |
2885 | |
2965 | |