… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
8 | use Socket; |
8 | use Socket; |
9 | use EV; |
9 | use EV 1.86; |
10 | use Opcode; |
10 | use Opcode; |
11 | use Safe; |
11 | use Safe; |
12 | use Safe::Hole; |
12 | use Safe::Hole; |
13 | use Storable (); |
13 | use Storable (); |
14 | |
14 | |
… | |
… | |
27 | use JSON::XS 2.01 (); |
27 | use JSON::XS 2.01 (); |
28 | use BDB (); |
28 | use BDB (); |
29 | use Data::Dumper; |
29 | use Data::Dumper; |
30 | use Digest::MD5; |
30 | use Digest::MD5; |
31 | use Fcntl; |
31 | use Fcntl; |
32 | use YAML::Syck (); |
32 | use YAML (); |
33 | use IO::AIO 2.51 (); |
33 | use IO::AIO 2.51 (); |
34 | use Time::HiRes; |
34 | use Time::HiRes; |
35 | use Compress::LZF; |
35 | use Compress::LZF; |
36 | use Digest::MD5 (); |
36 | use Digest::MD5 (); |
37 | |
37 | |
38 | # configure various modules to our taste |
38 | # configure various modules to our taste |
39 | # |
39 | # |
40 | $Storable::canonical = 1; # reduce rsync transfers |
40 | $Storable::canonical = 1; # reduce rsync transfers |
41 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
41 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
42 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
42 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
43 | |
|
|
44 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
45 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
46 | |
43 | |
47 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
44 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
48 | |
45 | |
49 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
46 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
50 | |
47 | |
… | |
… | |
69 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
66 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
70 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
67 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
71 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
68 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
72 | our $RANDOMDIR = "$LOCALDIR/random"; |
69 | our $RANDOMDIR = "$LOCALDIR/random"; |
73 | our $BDBDIR = "$LOCALDIR/db"; |
70 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
71 | our %RESOURCE; |
74 | |
72 | |
75 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
73 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
76 | our $TICK_WATCHER; |
74 | our $TICK_WATCHER; |
77 | our $AIO_POLL_WATCHER; |
75 | our $AIO_POLL_WATCHER; |
78 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
76 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
… | |
… | |
192 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
190 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
193 | |
191 | |
194 | LOG llevError, $msg; |
192 | LOG llevError, $msg; |
195 | }; |
193 | }; |
196 | } |
194 | } |
|
|
195 | |
|
|
196 | $Coro::State::DIEHOOK = sub { |
|
|
197 | warn Carp::longmess $_[0]; |
|
|
198 | Coro::terminate; |
|
|
199 | }; |
197 | |
200 | |
198 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
201 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
199 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
202 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
200 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
203 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
201 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
204 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
968 | } |
971 | } |
969 | |
972 | |
970 | 0 |
973 | 0 |
971 | } |
974 | } |
972 | |
975 | |
973 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
976 | =item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) |
974 | |
977 | |
975 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
978 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
976 | |
979 | |
977 | Generate an object-specific event with the given arguments. |
980 | Generate an object-specific event with the given arguments. |
978 | |
981 | |
… | |
… | |
3149 | { |
3152 | { |
3150 | my $faces = $facedata->{faceinfo}; |
3153 | my $faces = $facedata->{faceinfo}; |
3151 | |
3154 | |
3152 | while (my ($face, $info) = each %$faces) { |
3155 | while (my ($face, $info) = each %$faces) { |
3153 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3156 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
3157 | |
3154 | cf::face::set_visibility $idx, $info->{visibility}; |
3158 | cf::face::set_visibility $idx, $info->{visibility}; |
3155 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3159 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3156 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3160 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3157 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3161 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3158 | |
3162 | |
3159 | cf::cede_to_tick; |
3163 | cf::cede_to_tick; |
3160 | } |
3164 | } |
3161 | |
3165 | |
3162 | while (my ($face, $info) = each %$faces) { |
3166 | while (my ($face, $info) = each %$faces) { |
3163 | next unless $info->{smooth}; |
3167 | next unless $info->{smooth}; |
|
|
3168 | |
3164 | my $idx = cf::face::find $face |
3169 | my $idx = cf::face::find $face |
3165 | or next; |
3170 | or next; |
|
|
3171 | |
3166 | if (my $smooth = cf::face::find $info->{smooth}) { |
3172 | if (my $smooth = cf::face::find $info->{smooth}) { |
3167 | cf::face::set_smooth $idx, $smooth; |
3173 | cf::face::set_smooth $idx, $smooth; |
3168 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3174 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3169 | } else { |
3175 | } else { |
3170 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3176 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
… | |
… | |
3188 | { |
3194 | { |
3189 | # TODO: for gcfclient pleasure, we should give resources |
3195 | # TODO: for gcfclient pleasure, we should give resources |
3190 | # that gcfclient doesn't grok a >10000 face index. |
3196 | # that gcfclient doesn't grok a >10000 face index. |
3191 | my $res = $facedata->{resource}; |
3197 | my $res = $facedata->{resource}; |
3192 | |
3198 | |
3193 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
3194 | |
|
|
3195 | while (my ($name, $info) = each %$res) { |
3199 | while (my ($name, $info) = each %$res) { |
|
|
3200 | if (defined $info->{type}) { |
3196 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3201 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3197 | my $data; |
3202 | my $data; |
3198 | |
3203 | |
3199 | if ($info->{type} & 1) { |
3204 | if ($info->{type} & 1) { |
3200 | # prepend meta info |
3205 | # prepend meta info |
3201 | |
3206 | |
3202 | my $meta = $enc->encode ({ |
3207 | my $meta = $enc->encode ({ |
3203 | name => $name, |
3208 | name => $name, |
3204 | %{ $info->{meta} || {} }, |
3209 | %{ $info->{meta} || {} }, |
3205 | }); |
3210 | }); |
3206 | |
3211 | |
3207 | $data = pack "(w/a*)*", $meta, $info->{data}; |
3212 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3213 | } else { |
|
|
3214 | $data = $info->{data}; |
|
|
3215 | } |
|
|
3216 | |
|
|
3217 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3218 | cf::face::set_type $idx, $info->{type}; |
3208 | } else { |
3219 | } else { |
3209 | $data = $info->{data}; |
3220 | $RESOURCE{$name} = $info; |
3210 | } |
3221 | } |
3211 | |
3222 | |
3212 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3213 | cf::face::set_type $idx, $info->{type}; |
|
|
3214 | |
|
|
3215 | cf::cede_to_tick; |
3223 | cf::cede_to_tick; |
3216 | } |
3224 | } |
3217 | |
|
|
3218 | if ($soundconf) { |
|
|
3219 | $soundconf = $enc->decode (delete $soundconf->{data}); |
|
|
3220 | |
|
|
3221 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3222 | my $sound = $soundconf->{compat}[$_] |
|
|
3223 | or next; |
|
|
3224 | |
|
|
3225 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3226 | cf::sound::set $sound->[0] => $face; |
|
|
3227 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3228 | } |
|
|
3229 | |
|
|
3230 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3231 | my $face = cf::face::find "sound/$v"; |
|
|
3232 | cf::sound::set $k => $face; |
|
|
3233 | } |
|
|
3234 | } |
|
|
3235 | } |
3225 | } |
|
|
3226 | |
|
|
3227 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3236 | |
3228 | |
3237 | 1 |
3229 | 1 |
3238 | } |
3230 | } |
|
|
3231 | |
|
|
3232 | cf::global->attach (on_resource_update => sub { |
|
|
3233 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3234 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3235 | |
|
|
3236 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3237 | my $sound = $soundconf->{compat}[$_] |
|
|
3238 | or next; |
|
|
3239 | |
|
|
3240 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3241 | cf::sound::set $sound->[0] => $face; |
|
|
3242 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3243 | } |
|
|
3244 | |
|
|
3245 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3246 | my $face = cf::face::find "sound/$v"; |
|
|
3247 | cf::sound::set $k => $face; |
|
|
3248 | } |
|
|
3249 | } |
|
|
3250 | }); |
3239 | |
3251 | |
3240 | register_exticmd fx_want => sub { |
3252 | register_exticmd fx_want => sub { |
3241 | my ($ns, $want) = @_; |
3253 | my ($ns, $want) = @_; |
3242 | |
3254 | |
3243 | while (my ($k, $v) = each %$want) { |
3255 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3298 | sub reload_config { |
3310 | sub reload_config { |
3299 | open my $fh, "<:utf8", "$CONFDIR/config" |
3311 | open my $fh, "<:utf8", "$CONFDIR/config" |
3300 | or return; |
3312 | or return; |
3301 | |
3313 | |
3302 | local $/; |
3314 | local $/; |
3303 | *CFG = YAML::Syck::Load <$fh>; |
3315 | *CFG = YAML::Load <$fh>; |
3304 | |
3316 | |
3305 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3317 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3306 | |
3318 | |
3307 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3319 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3308 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3320 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3603 | |
3615 | |
3604 | $NOW = $tick_start = EV::now; |
3616 | $NOW = $tick_start = EV::now; |
3605 | |
3617 | |
3606 | cf::server_tick; # one server iteration |
3618 | cf::server_tick; # one server iteration |
3607 | |
3619 | |
3608 | $RUNTIME += $TICK; |
3620 | $RUNTIME += $TICK; |
3609 | $NEXT_TICK += $TICK; |
3621 | $NEXT_TICK = $_[0]->at; |
3610 | |
3622 | |
3611 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3623 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3612 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3624 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3613 | Coro::async_pool { |
3625 | Coro::async_pool { |
3614 | $Coro::current->{desc} = "runtime saver"; |
3626 | $Coro::current->{desc} = "runtime saver"; |