… | |
… | |
16 | use Coro::Event; |
16 | use Coro::Event; |
17 | use Coro::Timer; |
17 | use Coro::Timer; |
18 | use Coro::Signal; |
18 | use Coro::Signal; |
19 | use Coro::Semaphore; |
19 | use Coro::Semaphore; |
20 | use Coro::AIO; |
20 | use Coro::AIO; |
|
|
21 | use Coro::Storable; |
21 | |
22 | |
22 | use BDB (); |
23 | use BDB (); |
23 | use Data::Dumper; |
24 | use Data::Dumper; |
24 | use Digest::MD5; |
25 | use Digest::MD5; |
25 | use Fcntl; |
26 | use Fcntl; |
… | |
… | |
28 | use Time::HiRes; |
29 | use Time::HiRes; |
29 | use Compress::LZF; |
30 | use Compress::LZF; |
30 | |
31 | |
31 | # configure various modules to our taste |
32 | # configure various modules to our taste |
32 | # |
33 | # |
|
|
34 | $Storable::canonical = 1; # reduce rsync transfers |
33 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
35 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
34 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
36 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
35 | |
37 | |
36 | $Event::Eval = 1; # no idea why this is required, but it is |
38 | $Event::Eval = 1; # no idea why this is required, but it is |
37 | |
39 | |
… | |
… | |
373 | and return; |
375 | and return; |
374 | |
376 | |
375 | 1 |
377 | 1 |
376 | } |
378 | } |
377 | |
379 | |
|
|
380 | =item cf::datalog type => key => value, ... |
|
|
381 | |
|
|
382 | Log a datalog packet of the given type with the given key-value pairs. |
|
|
383 | |
|
|
384 | =cut |
|
|
385 | |
|
|
386 | sub datalog($@) { |
|
|
387 | my ($type, %kv) = @_; |
|
|
388 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
|
|
389 | } |
|
|
390 | |
378 | =back |
391 | =back |
379 | |
392 | |
380 | =cut |
393 | =cut |
381 | |
394 | |
382 | ############################################################################# |
395 | ############################################################################# |
… | |
… | |
952 | } |
965 | } |
953 | |
966 | |
954 | sub path($) { |
967 | sub path($) { |
955 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
968 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
956 | |
969 | |
957 | (playerdir $login) . "/$login.pl" |
970 | (playerdir $login) . "/playerdata" |
958 | } |
971 | } |
959 | |
972 | |
960 | sub find_active($) { |
973 | sub find_active($) { |
961 | $cf::PLAYER{$_[0]} |
974 | $cf::PLAYER{$_[0]} |
962 | and $cf::PLAYER{$_[0]}->active |
975 | and $cf::PLAYER{$_[0]}->active |
… | |
… | |
975 | my $login = $_[0]; |
988 | my $login = $_[0]; |
976 | |
989 | |
977 | my $guard = cf::lock_acquire "user_find:$login"; |
990 | my $guard = cf::lock_acquire "user_find:$login"; |
978 | |
991 | |
979 | $cf::PLAYER{$_[0]} || do { |
992 | $cf::PLAYER{$_[0]} || do { |
|
|
993 | # rename old playerfiles to new ones |
|
|
994 | #TODO: remove when no longer required |
|
|
995 | aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; |
|
|
996 | aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; |
|
|
997 | aio_unlink +(playerdir $login) . "/$login.pl.pst"; |
|
|
998 | aio_unlink +(playerdir $login) . "/$login.pl"; |
|
|
999 | |
980 | my $pl = load_pl path $login |
1000 | my $pl = load_pl path $login |
981 | or return; |
1001 | or return; |
982 | $cf::PLAYER{$login} = $pl |
1002 | $cf::PLAYER{$login} = $pl |
983 | } |
1003 | } |
984 | } |
1004 | } |
… | |
… | |
1113 | } |
1133 | } |
1114 | |
1134 | |
1115 | \@paths |
1135 | \@paths |
1116 | } |
1136 | } |
1117 | |
1137 | |
1118 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1138 | =item $player->ext_reply ($msgid, %msg) |
1119 | |
1139 | |
1120 | Sends an ext reply to the player. |
1140 | Sends an ext reply to the player. |
1121 | |
1141 | |
1122 | =cut |
1142 | =cut |
1123 | |
1143 | |
1124 | sub ext_reply($$$%) { |
1144 | sub ext_reply($$%) { |
1125 | my ($self, $id, %msg) = @_; |
1145 | my ($self, $id, %msg) = @_; |
1126 | |
1146 | |
1127 | $msg{msgid} = $id; |
1147 | $msg{msgid} = $id; |
1128 | |
1148 | |
1129 | $self->send ("ext " . cf::to_json \%msg); |
1149 | $self->send ("ext " . cf::to_json \%msg); |
|
|
1150 | } |
|
|
1151 | |
|
|
1152 | =item $player->ext_event ($type, %msg) |
|
|
1153 | |
|
|
1154 | Sends an ext event to the client. |
|
|
1155 | |
|
|
1156 | =cut |
|
|
1157 | |
|
|
1158 | sub ext_event($$%) { |
|
|
1159 | my ($self, $type, %msg) = @_; |
|
|
1160 | |
|
|
1161 | $self->ns->ext_event ($type, %msg); |
1130 | } |
1162 | } |
1131 | |
1163 | |
1132 | package cf; |
1164 | package cf; |
1133 | |
1165 | |
1134 | =back |
1166 | =back |
… | |
… | |
1942 | |
1974 | |
1943 | return unless $self->type == cf::PLAYER; |
1975 | return unless $self->type == cf::PLAYER; |
1944 | |
1976 | |
1945 | if ($exit->slaying eq "/!") { |
1977 | if ($exit->slaying eq "/!") { |
1946 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
1978 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1979 | # the problem is that $exit might not survive long enough |
|
|
1980 | # so it needs to be done right now, right here |
1947 | cf::sync_job { prepare_random_map $exit }; |
1981 | cf::sync_job { prepare_random_map $exit }; |
1948 | } |
1982 | } |
1949 | |
1983 | |
1950 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1984 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1951 | my $hp = $exit->stats->hp; |
1985 | my $hp = $exit->stats->hp; |
… | |
… | |
1959 | $self->goto ($slaying, $hp, $sp); |
1993 | $self->goto ($slaying, $hp, $sp); |
1960 | |
1994 | |
1961 | 1; |
1995 | 1; |
1962 | }) { |
1996 | }) { |
1963 | $self->message ("Something went wrong deep within the crossfire server. " |
1997 | $self->message ("Something went wrong deep within the crossfire server. " |
1964 | . "I'll try to bring you back to the map you were before. " |
1998 | . "I'll try to bring you back to the map you were before. " |
1965 | . "Please report this to the dungeon master!", |
1999 | . "Please report this to the dungeon master!", |
1966 | cf::NDI_UNIQUE | cf::NDI_RED); |
2000 | cf::NDI_UNIQUE | cf::NDI_RED); |
1967 | |
2001 | |
1968 | warn "ERROR in enter_exit: $@"; |
2002 | warn "ERROR in enter_exit: $@"; |
1969 | $self->leave_link; |
2003 | $self->leave_link; |
1970 | } |
2004 | } |
1971 | })->prio (1); |
2005 | })->prio (1); |
… | |
… | |
1987 | |
2021 | |
1988 | utf8::encode $text; |
2022 | utf8::encode $text; |
1989 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2023 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
1990 | } |
2024 | } |
1991 | |
2025 | |
|
|
2026 | =item $client->ext_event ($type, %msg) |
|
|
2027 | |
|
|
2028 | Sends an exti event to the client. |
|
|
2029 | |
|
|
2030 | =cut |
|
|
2031 | |
|
|
2032 | sub cf::client::ext_event($$%) { |
|
|
2033 | my ($self, $type, %msg) = @_; |
|
|
2034 | |
|
|
2035 | $msg{msgtype} = "event_$type"; |
|
|
2036 | $self->send_packet ("ext " . cf::to_json \%msg); |
|
|
2037 | } |
1992 | |
2038 | |
1993 | =item $success = $client->query ($flags, "text", \&cb) |
2039 | =item $success = $client->query ($flags, "text", \&cb) |
1994 | |
2040 | |
1995 | Queues a query to the client, calling the given callback with |
2041 | Queues a query to the client, calling the given callback with |
1996 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2042 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
… | |
… | |
2258 | } |
2304 | } |
2259 | |
2305 | |
2260 | ############################################################################# |
2306 | ############################################################################# |
2261 | # the server's init and main functions |
2307 | # the server's init and main functions |
2262 | |
2308 | |
2263 | sub load_faces { |
2309 | sub load_facedata { |
2264 | my $path = sprintf "%s/faces", cf::datadir; |
2310 | my $path = sprintf "%s/facedata", cf::datadir; |
2265 | |
2311 | |
2266 | warn "loading faces from $path\n"; |
2312 | warn "loading facedata from $path\n"; |
2267 | |
2313 | |
2268 | my $faces; |
2314 | my $facedata; |
2269 | 0 < aio_load $path, $faces |
2315 | 0 < aio_load $path, $facedata |
2270 | or die "$path: $!"; |
2316 | or die "$path: $!"; |
2271 | |
2317 | |
2272 | Coro::cede; |
|
|
2273 | $faces = Storable::thaw $faces; |
2318 | $facedata = Coro::Storable::thaw $facedata; |
2274 | Coro::cede; |
|
|
2275 | |
2319 | |
2276 | my $meta = delete $faces->{""}; |
|
|
2277 | $meta->{version} == 1 |
2320 | $facedata->{version} == 2 |
2278 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
2321 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
2279 | |
2322 | |
|
|
2323 | { |
|
|
2324 | my $faces = $facedata->{faceinfo}; |
|
|
2325 | |
2280 | while (my ($face, $info) = each %$faces) { |
2326 | while (my ($face, $info) = each %$faces) { |
2281 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2327 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2282 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
2328 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
2283 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2329 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
|
|
2330 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2284 | Coro::cede; |
2331 | Coro::cede; |
2285 | } |
2332 | } |
2286 | |
2333 | |
2287 | while (my ($face, $info) = each %$faces) { |
2334 | while (my ($face, $info) = each %$faces) { |
2288 | next unless $info->{smooth}; |
2335 | next unless $info->{smooth}; |
2289 | my $idx = cf::face::find $face |
2336 | my $idx = cf::face::find $face |
2290 | or next; |
2337 | or next; |
2291 | if (my $smooth = cf::face::find $info->{smooth}) { |
2338 | if (my $smooth = cf::face::find $info->{smooth}) { |
2292 | cf::face::set_smooth $idx, $smooth; |
2339 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
2293 | } else { |
2340 | } else { |
2294 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2341 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2295 | } |
2342 | } |
2296 | Coro::cede; |
2343 | Coro::cede; |
|
|
2344 | } |
|
|
2345 | } |
|
|
2346 | |
|
|
2347 | { |
|
|
2348 | my $anims = $facedata->{animinfo}; |
|
|
2349 | |
|
|
2350 | while (my ($anim, $info) = each %$anims) { |
|
|
2351 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
|
|
2352 | Coro::cede; |
|
|
2353 | } |
|
|
2354 | |
|
|
2355 | cf::anim::invalidate_all; # d'oh |
2297 | } |
2356 | } |
2298 | |
2357 | |
2299 | 1 |
2358 | 1 |
2300 | } |
2359 | } |
2301 | |
2360 | |
2302 | sub reload_resources { |
2361 | sub reload_resources { |
2303 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2362 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2304 | or die "unable to load regions file\n";#d# |
2363 | or die "unable to load regions file\n";#d# |
2305 | load_faces |
2364 | load_facedata |
2306 | or die "unable to load faces\n";#d# |
2365 | or die "unable to load facedata\n";#d# |
2307 | } |
2366 | } |
2308 | |
2367 | |
2309 | sub init { |
2368 | sub init { |
2310 | reload_resources; |
2369 | reload_resources; |
2311 | } |
2370 | } |