… | |
… | |
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 | } |
… | |
… | |
1378 | sub pre_load { } |
1379 | sub pre_load { } |
1379 | sub post_load { } |
1380 | sub post_load { } |
1380 | |
1381 | |
1381 | sub load { |
1382 | sub load { |
1382 | my ($self) = @_; |
1383 | my ($self) = @_; |
|
|
1384 | |
|
|
1385 | local $self->{deny_reset} = 1; # loading can take a long time |
1383 | |
1386 | |
1384 | my $path = $self->{path}; |
1387 | my $path = $self->{path}; |
1385 | my $guard = cf::lock_acquire "map_load:$path"; |
1388 | my $guard = cf::lock_acquire "map_load:$path"; |
1386 | |
1389 | |
1387 | return if $self->in_memory != cf::MAP_SWAPPED; |
1390 | return if $self->in_memory != cf::MAP_SWAPPED; |
… | |
… | |
1496 | delete $MAP_PREFETCH{$path}; |
1499 | delete $MAP_PREFETCH{$path}; |
1497 | } |
1500 | } |
1498 | } |
1501 | } |
1499 | undef $MAP_PREFETCHER; |
1502 | undef $MAP_PREFETCHER; |
1500 | }; |
1503 | }; |
|
|
1504 | $MAP_PREFETCHER->prio (6); |
1501 | |
1505 | |
1502 | () |
1506 | () |
1503 | } |
1507 | } |
1504 | |
1508 | |
1505 | sub save { |
1509 | sub save { |
… | |
… | |
1729 | =cut |
1733 | =cut |
1730 | |
1734 | |
1731 | sub link_map { |
1735 | sub link_map { |
1732 | unless ($LINK_MAP) { |
1736 | unless ($LINK_MAP) { |
1733 | $LINK_MAP = cf::map::find "{link}" |
1737 | $LINK_MAP = cf::map::find "{link}" |
1734 | or do { warn "FATAL: unable to provide {link} map, exiting."; exit 1 }; |
1738 | or cf::cleanup "FATAL: unable to provide {link} map, exiting."; |
1735 | $LINK_MAP->load; |
1739 | $LINK_MAP->load; |
1736 | } |
1740 | } |
1737 | |
1741 | |
1738 | $LINK_MAP |
1742 | $LINK_MAP |
1739 | } |
1743 | } |
… | |
… | |
1746 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1750 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1747 | |
1751 | |
1748 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1752 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1749 | if $self->map; |
1753 | if $self->map; |
1750 | |
1754 | |
1751 | $self->enter_map ($LINK_MAP || link_map, 20, 20); |
1755 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
1752 | } |
1756 | } |
1753 | |
1757 | |
1754 | sub cf::object::player::leave_link { |
1758 | sub cf::object::player::leave_link { |
1755 | my ($self, $map, $x, $y) = @_; |
1759 | my ($self, $map, $x, $y) = @_; |
1756 | |
1760 | |
… | |
… | |
1823 | my ($self, $path, $x, $y) = @_; |
1827 | my ($self, $path, $x, $y) = @_; |
1824 | |
1828 | |
1825 | $self->enter_link; |
1829 | $self->enter_link; |
1826 | |
1830 | |
1827 | (async { |
1831 | (async { |
|
|
1832 | my $map = eval { |
1828 | my $map = cf::map::find $path; |
1833 | my $map = cf::map::find $path; |
1829 | $map = $map->customise_for ($self) if $map; |
1834 | $map = $map->customise_for ($self) if $map; |
1830 | |
1835 | $map |
1831 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1836 | } or |
1832 | # if $map; |
|
|
1833 | |
|
|
1834 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1837 | $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1835 | |
1838 | |
1836 | $self->leave_link ($map, $x, $y); |
1839 | $self->leave_link ($map, $x, $y); |
1837 | })->prio (1); |
1840 | })->prio (1); |
1838 | } |
1841 | } |
1839 | |
1842 | |
… | |
… | |
1897 | sub cf::object::player::enter_exit { |
1900 | sub cf::object::player::enter_exit { |
1898 | my ($self, $exit) = @_; |
1901 | my ($self, $exit) = @_; |
1899 | |
1902 | |
1900 | return unless $self->type == cf::PLAYER; |
1903 | return unless $self->type == cf::PLAYER; |
1901 | |
1904 | |
|
|
1905 | if ($exit->slaying eq "/!") { |
|
|
1906 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1907 | cf::sync_job { prepare_random_map $exit }; |
|
|
1908 | } |
|
|
1909 | |
|
|
1910 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
|
|
1911 | my $hp = $exit->stats->hp; |
|
|
1912 | my $sp = $exit->stats->sp; |
|
|
1913 | |
1902 | $self->enter_link; |
1914 | $self->enter_link; |
1903 | |
1915 | |
1904 | (async { |
1916 | (async { |
1905 | $self->deactivate_recursive; # just to be sure |
1917 | $self->deactivate_recursive; # just to be sure |
1906 | unless (eval { |
1918 | unless (eval { |
1907 | prepare_random_map $exit |
1919 | $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 | |
1920 | |
1913 | 1; |
1921 | 1; |
1914 | }) { |
1922 | }) { |
1915 | $self->message ("Something went wrong deep within the crossfire server. " |
1923 | $self->message ("Something went wrong deep within the crossfire server. " |
1916 | . "I'll try to bring you back to the map you were before. " |
1924 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
2205 | sub db_sync() { |
2213 | sub db_sync() { |
2206 | db_save if $dirty; |
2214 | db_save if $dirty; |
2207 | undef $dirty; |
2215 | undef $dirty; |
2208 | } |
2216 | } |
2209 | |
2217 | |
2210 | my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2218 | my $idle = Event->idle ( |
2211 | db_sync; |
2219 | reentrant => 0, |
|
|
2220 | min => 10, |
|
|
2221 | max => 20, |
|
|
2222 | repeat => 0, |
|
|
2223 | data => WF_AUTOCANCEL, |
|
|
2224 | cb => \&db_sync, |
2212 | }); |
2225 | ); |
2213 | |
2226 | |
2214 | sub db_dirty() { |
2227 | sub db_dirty() { |
2215 | $dirty = 1; |
2228 | $dirty = 1; |
2216 | $idle->start; |
2229 | $idle->start; |
2217 | } |
2230 | } |
… | |
… | |
2285 | |
2298 | |
2286 | # install some emergency cleanup handlers |
2299 | # install some emergency cleanup handlers |
2287 | BEGIN { |
2300 | BEGIN { |
2288 | for my $signal (qw(INT HUP TERM)) { |
2301 | for my $signal (qw(INT HUP TERM)) { |
2289 | Event->signal ( |
2302 | Event->signal ( |
|
|
2303 | reentrant => 0, |
2290 | data => WF_AUTOCANCEL, |
2304 | data => WF_AUTOCANCEL, |
2291 | signal => $signal, |
2305 | signal => $signal, |
|
|
2306 | prio => 0, |
2292 | cb => sub { |
2307 | cb => sub { |
2293 | cf::cleanup "SIG$signal"; |
2308 | cf::cleanup "SIG$signal"; |
2294 | }, |
2309 | }, |
2295 | ); |
2310 | ); |
2296 | } |
2311 | } |
2297 | } |
2312 | } |
… | |
… | |
2452 | $who->message ("reloading server."); |
2467 | $who->message ("reloading server."); |
2453 | |
2468 | |
2454 | # doing reload synchronously and two reloads happen back-to-back, |
2469 | # doing reload synchronously and two reloads happen back-to-back, |
2455 | # coro crashes during coro_state_free->destroy here. |
2470 | # coro crashes during coro_state_free->destroy here. |
2456 | |
2471 | |
2457 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2472 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2473 | reentrant => 0, |
|
|
2474 | after => 0, |
|
|
2475 | data => WF_AUTOCANCEL, |
|
|
2476 | cb => sub { |
2458 | reload; |
2477 | reload; |
2459 | undef $RELOAD_WATCHER; |
2478 | undef $RELOAD_WATCHER; |
|
|
2479 | }, |
2460 | }); |
2480 | ); |
2461 | } |
2481 | } |
2462 | }; |
2482 | }; |
2463 | |
2483 | |
2464 | unshift @INC, $LIBDIR; |
2484 | unshift @INC, $LIBDIR; |
2465 | |
2485 | |
… | |
… | |
2485 | $NEXT_TICK += $TICK; |
2505 | $NEXT_TICK += $TICK; |
2486 | |
2506 | |
2487 | $WAIT_FOR_TICK->broadcast; |
2507 | $WAIT_FOR_TICK->broadcast; |
2488 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2508 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2489 | |
2509 | |
|
|
2510 | Event::sweep; |
|
|
2511 | Coro::cede_notself; |
|
|
2512 | |
|
|
2513 | # my $AFTER = Event::time; |
|
|
2514 | # warn $AFTER - $NOW;#d# |
|
|
2515 | |
2490 | # if we are delayed by four ticks or more, skip them all |
2516 | # if we are delayed by four ticks or more, skip them all |
2491 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2517 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2492 | |
2518 | |
2493 | $TICK_WATCHER->at ($NEXT_TICK); |
2519 | $TICK_WATCHER->at ($NEXT_TICK); |
2494 | $TICK_WATCHER->start; |
2520 | $TICK_WATCHER->start; |
2495 | }, |
2521 | }, |
2496 | ); |
2522 | ); |
2497 | |
2523 | |
2498 | IO::AIO::max_poll_time $TICK * 0.2; |
2524 | IO::AIO::max_poll_time $TICK * 0.1; |
2499 | |
2525 | |
|
|
2526 | undef $Coro::AIO::WATCHER; |
2500 | $AIO_POLL_WATCHER = Event->io ( |
2527 | $AIO_POLL_WATCHER = Event->io ( |
|
|
2528 | reentrant => 0, |
2501 | fd => IO::AIO::poll_fileno, |
2529 | fd => IO::AIO::poll_fileno, |
2502 | poll => 'r', |
2530 | poll => 'r', |
2503 | prio => 5, |
2531 | prio => 6, |
2504 | data => WF_AUTOCANCEL, |
2532 | data => WF_AUTOCANCEL, |
2505 | cb => \&IO::AIO::poll_cb, |
2533 | cb => \&IO::AIO::poll_cb, |
2506 | ); |
2534 | ); |
2507 | |
2535 | |
2508 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2536 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2537 | reentrant => 0, |
2509 | data => WF_AUTOCANCEL, |
2538 | data => WF_AUTOCANCEL, |
2510 | after => 1, |
2539 | after => 1, |
2511 | interval => 10, |
2540 | interval => 10, |
2512 | prio => 6, # keep it lowest so it acts like a watchdog |
2541 | prio => 6, # keep it lowest so it acts like a watchdog |
2513 | cb => Coro::unblock_sub { |
2542 | cb => Coro::unblock_sub { |
2514 | write_runtime |
2543 | write_runtime |
2515 | or warn "ERROR: unable to write runtime file: $!"; |
2544 | or warn "ERROR: unable to write runtime file: $!"; |
2516 | }, |
2545 | }, |
2517 | ); |
2546 | ); |
2518 | |
2547 | |