ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.460 by root, Mon Dec 15 17:04:53 2008 UTC vs.
Revision 1.472 by root, Sat Jun 6 21:43:13 2009 UTC

21 21
22package cf; 22package cf;
23 23
24use 5.10.0; 24use 5.10.0;
25use utf8; 25use utf8;
26use strict "vars", "subs"; 26use strict qw(vars subs);
27 27
28use Symbol; 28use Symbol;
29use List::Util; 29use List::Util;
30use Socket; 30use Socket;
31use EV; 31use EV;
105our %RESOURCE; 105our %RESOURCE;
106 106
107our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 107our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
108our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 108our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
109our $NEXT_TICK; 109our $NEXT_TICK;
110our $USE_FSYNC = 1; # use fsync to write maps - default off 110our $USE_FSYNC = 1; # use fsync to write maps - default on
111 111
112our $BDB_DEADLOCK_WATCHER; 112our $BDB_DEADLOCK_WATCHER;
113our $BDB_CHECKPOINT_WATCHER; 113our $BDB_CHECKPOINT_WATCHER;
114our $BDB_TRICKLE_WATCHER; 114our $BDB_TRICKLE_WATCHER;
115our $DB_ENV; 115our $DB_ENV;
116
117our @EXTRA_MODULES = qw(pod mapscript);
116 118
117our %CFG; 119our %CFG;
118 120
119our $UPTIME; $UPTIME ||= time; 121our $UPTIME; $UPTIME ||= time;
120our $RUNTIME; 122our $RUNTIME;
143unless ($RUNTIME || !-e $RUNTIMEFILE) { 145unless ($RUNTIME || !-e $RUNTIMEFILE) {
144 open my $fh, "<", $RUNTIMEFILE 146 open my $fh, "<", $RUNTIMEFILE
145 or die "unable to read $RUNTIMEFILE file: $!"; 147 or die "unable to read $RUNTIMEFILE file: $!";
146 $RUNTIME = <$fh> + 0.; 148 $RUNTIME = <$fh> + 0.;
147} 149}
150
151eval "sub TICK() { $TICK } 1" or die;
148 152
149mkdir $_ 153mkdir $_
150 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; 154 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
151 155
152our $EMERGENCY_POSITION; 156our $EMERGENCY_POSITION;
1316 1320
1317# "readahead" all extensions 1321# "readahead" all extensions
1318sub cache_extensions { 1322sub cache_extensions {
1319 my $grp = IO::AIO::aio_group; 1323 my $grp = IO::AIO::aio_group;
1320 1324
1321 add $grp IO::AIO::aio_readdir $LIBDIR, sub { 1325 add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub {
1322 for (grep /\.ext$/, @{$_[0]}) { 1326 for (grep /\.ext$/, @{$_[0]}) {
1323 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; 1327 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1324 } 1328 }
1325 }; 1329 };
1326 1330
1862 1866
1863 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; 1867 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go;
1864 "$UNIQUEDIR/$path" 1868 "$UNIQUEDIR/$path"
1865} 1869}
1866 1870
1867# and all this just because we cannot iterate over
1868# all maps in C++...
1869sub change_all_map_light {
1870 my ($change) = @_;
1871
1872 $_->change_map_light ($change)
1873 for grep $_->outdoor, values %cf::MAP;
1874}
1875
1876sub decay_objects { 1871sub decay_objects {
1877 my ($self) = @_; 1872 my ($self) = @_;
1878 1873
1879 return if $self->{deny_reset}; 1874 return if $self->{deny_reset};
1880 1875
2042 2037
2043 unless ($self->{deny_activate}) { 2038 unless ($self->{deny_activate}) {
2044 $self->decay_objects; 2039 $self->decay_objects;
2045 $self->fix_auto_apply; 2040 $self->fix_auto_apply;
2046 $self->update_buttons; 2041 $self->update_buttons;
2047 cf::cede_to_tick;
2048 $self->set_darkness_map;
2049 cf::cede_to_tick; 2042 cf::cede_to_tick;
2050 $self->activate; 2043 $self->activate;
2051 } 2044 }
2052 2045
2053 $self->{last_save} = $cf::RUNTIME; 2046 $self->{last_save} = $cf::RUNTIME;
2395 2388
2396our $SAY_CHANNEL = { 2389our $SAY_CHANNEL = {
2397 id => "say", 2390 id => "say",
2398 title => "Map", 2391 title => "Map",
2399 reply => "say ", 2392 reply => "say ",
2400 tooltip => "Things said to and replied from npcs near you and other players on the same map only.", 2393 tooltip => "Things said to and replied from NPCs near you and other players on the same map only.",
2401}; 2394};
2402 2395
2403our $CHAT_CHANNEL = { 2396our $CHAT_CHANNEL = {
2404 id => "chat", 2397 id => "chat",
2405 title => "Chat", 2398 title => "Chat",
2780 id => "infobox", 2773 id => "infobox",
2781 title => "Body Parts", 2774 title => "Body Parts",
2782 reply => undef, 2775 reply => undef,
2783 tooltip => "Shows which body parts you posess and are available", 2776 tooltip => "Shows which body parts you posess and are available",
2784 }, 2777 },
2778 "c/statistics" => {
2779 id => "infobox",
2780 title => "Statistics",
2781 reply => undef,
2782 tooltip => "Shows your primary statistics",
2783 },
2785 "c/skills" => { 2784 "c/skills" => {
2786 id => "infobox", 2785 id => "infobox",
2787 title => "Skills", 2786 title => "Skills",
2788 reply => undef, 2787 reply => undef,
2789 tooltip => "Shows your experience per skill and item power", 2788 tooltip => "Shows your experience per skill and item power",
2789 },
2790 "c/shopitems" => {
2791 id => "infobox",
2792 title => "Shop Items",
2793 reply => undef,
2794 tooltip => "Shows the items currently for sale in this shop",
2795 },
2796 "c/resistances" => {
2797 id => "infobox",
2798 title => "Resistances",
2799 reply => undef,
2800 tooltip => "Shows your resistances",
2801 },
2802 "c/pets" => {
2803 id => "infobox",
2804 title => "Pets",
2805 reply => undef,
2806 tooltip => "Shows information abotu your pets/a specific pet",
2807 },
2808 "c/perceiveself" => {
2809 id => "infobox",
2810 title => "Perceive Self",
2811 reply => undef,
2812 tooltip => "You gained detailed knowledge about yourself",
2790 }, 2813 },
2791 "c/uptime" => { 2814 "c/uptime" => {
2792 id => "infobox", 2815 id => "infobox",
2793 title => "Uptime", 2816 title => "Uptime",
2794 reply => undef, 2817 reply => undef,
2804 id => "party", 2827 id => "party",
2805 title => "Party", 2828 title => "Party",
2806 reply => "gsay ", 2829 reply => "gsay ",
2807 tooltip => "Messages and chat related to your party", 2830 tooltip => "Messages and chat related to your party",
2808 }, 2831 },
2832 "c/death" => {
2833 id => "death",
2834 title => "Death",
2835 reply => undef,
2836 tooltip => "Reason for and more info about your most recent death",
2837 },
2838 "c/say" => $SAY_CHANNEL,
2839 "c/chat" => $CHAT_CHANNEL,
2809); 2840);
2810 2841
2811sub cf::client::send_msg { 2842sub cf::client::send_msg {
2812 my ($self, $channel, $msg, $color, @extra) = @_; 2843 my ($self, $channel, $msg, $color, @extra) = @_;
2813 2844
2818 2849
2819 # check predefined channels, for the benefit of C 2850 # check predefined channels, for the benefit of C
2820 if ($CHANNEL{$channel}) { 2851 if ($CHANNEL{$channel}) {
2821 $channel = $CHANNEL{$channel}; 2852 $channel = $CHANNEL{$channel};
2822 2853
2823 $self->ext_msg (channel_info => $channel) 2854 $self->ext_msg (channel_info => $channel);
2824 if $self->can_msg;
2825
2826 $channel = $channel->{id}; 2855 $channel = $channel->{id};
2827 2856
2828 } elsif (ref $channel) { 2857 } elsif (ref $channel) {
2829 # send meta info to client, if not yet sent 2858 # send meta info to client, if not yet sent
2830 unless (exists $self->{channel}{$channel->{id}}) { 2859 unless (exists $self->{channel}{$channel->{id}}) {
2831 $self->{channel}{$channel->{id}} = $channel; 2860 $self->{channel}{$channel->{id}} = $channel;
2832 $self->ext_msg (channel_info => $channel) 2861 $self->ext_msg (channel_info => $channel);
2833 if $self->can_msg;
2834 } 2862 }
2835 2863
2836 $channel = $channel->{id}; 2864 $channel = $channel->{id};
2837 } 2865 }
2838 2866
2839 return unless @extra || length $msg; 2867 return unless @extra || length $msg;
2840 2868
2841 if ($self->can_msg) {
2842 # default colour, mask it out 2869 # default colour, mask it out
2843 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) 2870 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF)
2844 if $color & cf::NDI_DEF; 2871 if $color & cf::NDI_DEF;
2845 2872
2846 my $pkt = "msg " 2873 my $pkt = "msg "
2847 . $self->{json_coder}->encode ( 2874 . $self->{json_coder}->encode (
2848 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2875 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2849 ); 2876 );
2850 2877
2851 # try lzf for large packets 2878 # try lzf for large packets
2852 $pkt = "lzf " . Compress::LZF::compress $pkt 2879 $pkt = "lzf " . Compress::LZF::compress $pkt
2853 if 1024 <= length $pkt and $self->{can_lzf}; 2880 if 1024 <= length $pkt and $self->{can_lzf};
2854 2881
2855 # split very large packets 2882 # split very large packets
2856 if (8192 < length $pkt and $self->{can_lzf}) { 2883 if (8192 < length $pkt and $self->{can_lzf}) {
2857 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; 2884 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2858 $pkt = "frag"; 2885 $pkt = "frag";
2859 } 2886 }
2860 2887
2861 $self->send_packet ($pkt); 2888 $self->send_packet ($pkt);
2862 } else {
2863 if ($color >= 0) {
2864 # replace some tags by gcfclient-compatible ones
2865 for ($msg) {
2866 1 while
2867 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2868 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2869 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2870 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2871 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2872 }
2873
2874 $color &= cf::NDI_COLOR_MASK;
2875
2876 utf8::encode $msg;
2877
2878 if (0 && $msg =~ /\[/) {
2879 # COMMAND/INFO
2880 $self->send_packet ("drawextinfo $color 10 8 $msg")
2881 } else {
2882 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g;
2883 $self->send_packet ("drawinfo $color $msg")
2884 }
2885 }
2886 }
2887} 2889}
2888 2890
2889=item $client->ext_msg ($type, @msg) 2891=item $client->ext_msg ($type, @msg)
2890 2892
2891Sends an ext event to the client. 2893Sends an ext event to the client.
3066The following functions and methods are available within a safe environment: 3068The following functions and methods are available within a safe environment:
3067 3069
3068 cf::object 3070 cf::object
3069 contr pay_amount pay_player map x y force_find force_add destroy 3071 contr pay_amount pay_player map x y force_find force_add destroy
3070 insert remove name archname title slaying race decrease split 3072 insert remove name archname title slaying race decrease split
3073 value
3071 3074
3072 cf::object::player 3075 cf::object::player
3073 player 3076 player
3074 3077
3075 cf::player 3078 cf::player
3081=cut 3084=cut
3082 3085
3083for ( 3086for (
3084 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 3087 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
3085 insert remove inv nrof name archname title slaying race 3088 insert remove inv nrof name archname title slaying race
3086 decrease split destroy change_exp)], 3089 decrease split destroy change_exp value msg lore send_msg)],
3087 ["cf::object::player" => qw(player)], 3090 ["cf::object::player" => qw(player)],
3088 ["cf::player" => qw(peaceful)], 3091 ["cf::player" => qw(peaceful send_msg)],
3089 ["cf::map" => qw(trigger)], 3092 ["cf::map" => qw(trigger)],
3090) { 3093) {
3091 no strict 'refs'; 3094 no strict 'refs';
3092 my ($pkg, @funs) = @$_; 3095 my ($pkg, @funs) = @$_;
3093 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3096 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3110 my ($code, %vars) = @_; 3113 my ($code, %vars) = @_;
3111 3114
3112 my $qcode = $code; 3115 my $qcode = $code;
3113 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3116 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3114 $qcode =~ s/\n/\\n/g; 3117 $qcode =~ s/\n/\\n/g;
3118
3119 %vars = (_dummy => 0) unless %vars;
3115 3120
3116 local $_; 3121 local $_;
3117 local @safe::cf::_safe_eval_args = values %vars; 3122 local @safe::cf::_safe_eval_args = values %vars;
3118 3123
3119 my $eval = 3124 my $eval =
3401 3406
3402 cf::init_experience; 3407 cf::init_experience;
3403 cf::init_anim; 3408 cf::init_anim;
3404 cf::init_attackmess; 3409 cf::init_attackmess;
3405 cf::init_dynamic; 3410 cf::init_dynamic;
3406 cf::init_block;
3407 3411
3408 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3412 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3409 3413
3410 # we must not ever block the main coroutine 3414 # we must not ever block the main coroutine
3411 local $Coro::idle = sub { 3415 local $Coro::idle = sub {
3708 clear_package "safe::$_" 3712 clear_package "safe::$_"
3709 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3713 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3710 3714
3711 warn "unloading cf.pm \"a bit\""; 3715 warn "unloading cf.pm \"a bit\"";
3712 delete $INC{"cf.pm"}; 3716 delete $INC{"cf.pm"};
3713 delete $INC{"cf/pod.pm"}; 3717 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3714 3718
3715 # don't, removes xs symbols, too, 3719 # don't, removes xs symbols, too,
3716 # and global variables created in xs 3720 # and global variables created in xs
3717 #clear_package __PACKAGE__; 3721 #clear_package __PACKAGE__;
3718 3722
3935 LOG llevInfo, "[ABT] [suppressed]\n"; 3939 LOG llevInfo, "[ABT] [suppressed]\n";
3936 } 3940 }
3937} 3941}
3938 3942
3939# load additional modules 3943# load additional modules
3940use cf::pod; 3944require "cf/$_.pm" for @EXTRA_MODULES;
3941 3945
3942END { cf::emergency_save } 3946END { cf::emergency_save }
3943 3947
39441 39481
3945 3949

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines