… | |
… | |
8 | use Storable; |
8 | use Storable; |
9 | use Opcode; |
9 | use Opcode; |
10 | use Safe; |
10 | use Safe; |
11 | use Safe::Hole; |
11 | use Safe::Hole; |
12 | |
12 | |
13 | use Coro 3.4 (); |
13 | use Coro 3.5 (); |
14 | use Coro::Event; |
14 | use Coro::Event; |
15 | use Coro::Timer; |
15 | use Coro::Timer; |
16 | use Coro::Signal; |
16 | use Coro::Signal; |
17 | use Coro::Semaphore; |
17 | use Coro::Semaphore; |
18 | use Coro::AIO; |
18 | use Coro::AIO; |
… | |
… | |
1176 | } |
1176 | } |
1177 | |
1177 | |
1178 | sub normalise { |
1178 | sub normalise { |
1179 | my ($path, $base) = @_; |
1179 | my ($path, $base) = @_; |
1180 | |
1180 | |
|
|
1181 | $path = "$path"; # make sure its a string |
|
|
1182 | |
1181 | # map plan: |
1183 | # map plan: |
1182 | # |
1184 | # |
1183 | # /! non-realised random map exit (special hack!) |
1185 | # /! non-realised random map exit (special hack!) |
1184 | # {... are special paths that are not being touched |
1186 | # {... are special paths that are not being touched |
1185 | # ?xxx/... are special absolute paths |
1187 | # ?xxx/... are special absolute paths |
… | |
… | |
1221 | $self->init; # pass $1 etc. |
1223 | $self->init; # pass $1 etc. |
1222 | return $self; |
1224 | return $self; |
1223 | } |
1225 | } |
1224 | } |
1226 | } |
1225 | |
1227 | |
1226 | Carp::carp "unable to resolve path '$path'."; |
1228 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1227 | () |
1229 | () |
1228 | } |
1230 | } |
1229 | |
1231 | |
1230 | sub init { |
1232 | sub init { |
1231 | my ($self) = @_; |
1233 | my ($self) = @_; |
… | |
… | |
1360 | $map->{last_save} = $cf::RUNTIME; |
1362 | $map->{last_save} = $cf::RUNTIME; |
1361 | |
1363 | |
1362 | $map->load_header |
1364 | $map->load_header |
1363 | or return; |
1365 | or return; |
1364 | |
1366 | |
1365 | if ($map->should_reset) { |
1367 | if ($map->should_reset && 0) {#d#TODO# disabled, crashy (locking issue?) |
1366 | # doing this can freeze the server in a sync job, obviously |
1368 | # doing this can freeze the server in a sync job, obviously |
1367 | #$cf::WAIT_FOR_TICK->wait; |
1369 | #$cf::WAIT_FOR_TICK->wait; |
1368 | $map->reset; |
1370 | $map->reset; |
1369 | undef $guard; |
1371 | undef $guard; |
1370 | $map = find $path |
1372 | return find $path; |
1371 | or return; |
|
|
1372 | } |
1373 | } |
1373 | |
1374 | |
1374 | $cf::MAP{$path} = $map |
1375 | $cf::MAP{$path} = $map |
1375 | } |
1376 | } |
1376 | } |
1377 | } |
… | |
… | |
1496 | delete $MAP_PREFETCH{$path}; |
1497 | delete $MAP_PREFETCH{$path}; |
1497 | } |
1498 | } |
1498 | } |
1499 | } |
1499 | undef $MAP_PREFETCHER; |
1500 | undef $MAP_PREFETCHER; |
1500 | }; |
1501 | }; |
|
|
1502 | $MAP_PREFETCHER->prio (6); |
1501 | |
1503 | |
1502 | () |
1504 | () |
1503 | } |
1505 | } |
1504 | |
1506 | |
1505 | sub save { |
1507 | sub save { |
… | |
… | |
1746 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1748 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1747 | |
1749 | |
1748 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1750 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1749 | if $self->map; |
1751 | if $self->map; |
1750 | |
1752 | |
1751 | $self->enter_map ($LINK_MAP || link_map, 20, 20); |
1753 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
1752 | } |
1754 | } |
1753 | |
1755 | |
1754 | sub cf::object::player::leave_link { |
1756 | sub cf::object::player::leave_link { |
1755 | my ($self, $map, $x, $y) = @_; |
1757 | my ($self, $map, $x, $y) = @_; |
1756 | |
1758 | |
… | |
… | |
1897 | sub cf::object::player::enter_exit { |
1899 | sub cf::object::player::enter_exit { |
1898 | my ($self, $exit) = @_; |
1900 | my ($self, $exit) = @_; |
1899 | |
1901 | |
1900 | return unless $self->type == cf::PLAYER; |
1902 | return unless $self->type == cf::PLAYER; |
1901 | |
1903 | |
|
|
1904 | if ($exit->slaying eq "/!") { |
|
|
1905 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1906 | cf::sync_job { prepare_random_map $exit }; |
|
|
1907 | } |
|
|
1908 | |
|
|
1909 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
|
|
1910 | my $hp = $exit->stats->hp; |
|
|
1911 | my $sp = $exit->stats->sp; |
|
|
1912 | |
1902 | $self->enter_link; |
1913 | $self->enter_link; |
1903 | |
1914 | |
1904 | (async { |
1915 | (async { |
1905 | $self->deactivate_recursive; # just to be sure |
1916 | $self->deactivate_recursive; # just to be sure |
1906 | unless (eval { |
1917 | unless (eval { |
1907 | prepare_random_map $exit |
1918 | $self->goto ($slaying, $hp, $sp); |
1908 | if $exit->slaying eq "/!"; |
|
|
1909 | |
|
|
1910 | my $path = new_from_path cf::map $exit->slaying, $exit->map && $exit->map->path; |
|
|
1911 | $self->goto ($path, $exit->stats->hp, $exit->stats->sp); |
|
|
1912 | |
1919 | |
1913 | 1; |
1920 | 1; |
1914 | }) { |
1921 | }) { |
1915 | $self->message ("Something went wrong deep within the crossfire server. " |
1922 | $self->message ("Something went wrong deep within the crossfire server. " |
1916 | . "I'll try to bring you back to the map you were before. " |
1923 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
2205 | sub db_sync() { |
2212 | sub db_sync() { |
2206 | db_save if $dirty; |
2213 | db_save if $dirty; |
2207 | undef $dirty; |
2214 | undef $dirty; |
2208 | } |
2215 | } |
2209 | |
2216 | |
2210 | my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2217 | my $idle = Event->idle ( |
2211 | db_sync; |
2218 | reentrant => 0, |
|
|
2219 | min => 10, |
|
|
2220 | max => 20, |
|
|
2221 | repeat => 0, |
|
|
2222 | data => WF_AUTOCANCEL, |
|
|
2223 | cb => \&db_sync, |
2212 | }); |
2224 | ); |
2213 | |
2225 | |
2214 | sub db_dirty() { |
2226 | sub db_dirty() { |
2215 | $dirty = 1; |
2227 | $dirty = 1; |
2216 | $idle->start; |
2228 | $idle->start; |
2217 | } |
2229 | } |
… | |
… | |
2285 | |
2297 | |
2286 | # install some emergency cleanup handlers |
2298 | # install some emergency cleanup handlers |
2287 | BEGIN { |
2299 | BEGIN { |
2288 | for my $signal (qw(INT HUP TERM)) { |
2300 | for my $signal (qw(INT HUP TERM)) { |
2289 | Event->signal ( |
2301 | Event->signal ( |
|
|
2302 | reentrant => 0, |
2290 | data => WF_AUTOCANCEL, |
2303 | data => WF_AUTOCANCEL, |
2291 | signal => $signal, |
2304 | signal => $signal, |
|
|
2305 | prio => 0, |
2292 | cb => sub { |
2306 | cb => sub { |
2293 | cf::cleanup "SIG$signal"; |
2307 | cf::cleanup "SIG$signal"; |
2294 | }, |
2308 | }, |
2295 | ); |
2309 | ); |
2296 | } |
2310 | } |
2297 | } |
2311 | } |
… | |
… | |
2452 | $who->message ("reloading server."); |
2466 | $who->message ("reloading server."); |
2453 | |
2467 | |
2454 | # doing reload synchronously and two reloads happen back-to-back, |
2468 | # doing reload synchronously and two reloads happen back-to-back, |
2455 | # coro crashes during coro_state_free->destroy here. |
2469 | # coro crashes during coro_state_free->destroy here. |
2456 | |
2470 | |
2457 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2471 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2472 | reentrant => 0, |
|
|
2473 | after => 0, |
|
|
2474 | data => WF_AUTOCANCEL, |
|
|
2475 | cb => sub { |
2458 | reload; |
2476 | reload; |
2459 | undef $RELOAD_WATCHER; |
2477 | undef $RELOAD_WATCHER; |
|
|
2478 | }, |
2460 | }); |
2479 | ); |
2461 | } |
2480 | } |
2462 | }; |
2481 | }; |
2463 | |
2482 | |
2464 | unshift @INC, $LIBDIR; |
2483 | unshift @INC, $LIBDIR; |
2465 | |
2484 | |
… | |
… | |
2485 | $NEXT_TICK += $TICK; |
2504 | $NEXT_TICK += $TICK; |
2486 | |
2505 | |
2487 | $WAIT_FOR_TICK->broadcast; |
2506 | $WAIT_FOR_TICK->broadcast; |
2488 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2507 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2489 | |
2508 | |
|
|
2509 | Event::sweep; |
|
|
2510 | Coro::cede_notself; |
|
|
2511 | |
|
|
2512 | # my $AFTER = Event::time; |
|
|
2513 | # warn $AFTER - $NOW;#d# |
|
|
2514 | |
2490 | # if we are delayed by four ticks or more, skip them all |
2515 | # if we are delayed by four ticks or more, skip them all |
2491 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2516 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2492 | |
2517 | |
2493 | $TICK_WATCHER->at ($NEXT_TICK); |
2518 | $TICK_WATCHER->at ($NEXT_TICK); |
2494 | $TICK_WATCHER->start; |
2519 | $TICK_WATCHER->start; |
2495 | }, |
2520 | }, |
2496 | ); |
2521 | ); |
2497 | |
2522 | |
2498 | IO::AIO::max_poll_time $TICK * 0.2; |
2523 | IO::AIO::max_poll_time $TICK * 0.1; |
2499 | |
2524 | |
|
|
2525 | undef $Coro::AIO::WATCHER; |
2500 | $AIO_POLL_WATCHER = Event->io ( |
2526 | $AIO_POLL_WATCHER = Event->io ( |
|
|
2527 | reentrant => 0, |
2501 | fd => IO::AIO::poll_fileno, |
2528 | fd => IO::AIO::poll_fileno, |
2502 | poll => 'r', |
2529 | poll => 'r', |
2503 | prio => 5, |
2530 | prio => 6, |
2504 | data => WF_AUTOCANCEL, |
2531 | data => WF_AUTOCANCEL, |
2505 | cb => \&IO::AIO::poll_cb, |
2532 | cb => \&IO::AIO::poll_cb, |
2506 | ); |
2533 | ); |
2507 | |
2534 | |
2508 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2535 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2536 | reentrant => 0, |
2509 | data => WF_AUTOCANCEL, |
2537 | data => WF_AUTOCANCEL, |
2510 | after => 1, |
2538 | after => 1, |
2511 | interval => 10, |
2539 | interval => 10, |
2512 | prio => 6, # keep it lowest so it acts like a watchdog |
2540 | prio => 6, # keep it lowest so it acts like a watchdog |
2513 | cb => Coro::unblock_sub { |
2541 | cb => Coro::unblock_sub { |
2514 | write_runtime |
2542 | write_runtime |
2515 | or warn "ERROR: unable to write runtime file: $!"; |
2543 | or warn "ERROR: unable to write runtime file: $!"; |
2516 | }, |
2544 | }, |
2517 | ); |
2545 | ); |
2518 | |
2546 | |