… | |
… | |
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; |
… | |
… | |
82 | mkdir cf::localdir . "/" . cf::tmpdir; |
82 | mkdir cf::localdir . "/" . cf::tmpdir; |
83 | mkdir cf::localdir . "/" . cf::uniquedir; |
83 | mkdir cf::localdir . "/" . cf::uniquedir; |
84 | mkdir $RANDOM_MAPS; |
84 | mkdir $RANDOM_MAPS; |
85 | |
85 | |
86 | our $EMERGENCY_POSITION; |
86 | our $EMERGENCY_POSITION; |
|
|
87 | |
|
|
88 | sub cf::map::normalise; |
87 | |
89 | |
88 | ############################################################################# |
90 | ############################################################################# |
89 | |
91 | |
90 | =head2 GLOBAL VARIABLES |
92 | =head2 GLOBAL VARIABLES |
91 | |
93 | |
… | |
… | |
1078 | for (@$files) { |
1080 | for (@$files) { |
1079 | utf8::decode $_; |
1081 | utf8::decode $_; |
1080 | next if /\.(?:pl|pst)$/; |
1082 | next if /\.(?:pl|pst)$/; |
1081 | next unless /^$PATH_SEP/o; |
1083 | next unless /^$PATH_SEP/o; |
1082 | |
1084 | |
1083 | s/\.map$//; |
|
|
1084 | push @paths, "~" . $pl->ob->name . "/" . $_; |
1085 | push @paths, cf::map::normalise "~" . $pl->ob->name . "/" . $_; |
1085 | } |
1086 | } |
1086 | |
1087 | |
1087 | \@paths |
1088 | \@paths |
1088 | } |
1089 | } |
1089 | |
1090 | |
… | |
… | |
1176 | } |
1177 | } |
1177 | |
1178 | |
1178 | sub normalise { |
1179 | sub normalise { |
1179 | my ($path, $base) = @_; |
1180 | my ($path, $base) = @_; |
1180 | |
1181 | |
|
|
1182 | $path = "$path"; # make sure its a string |
|
|
1183 | |
|
|
1184 | $path =~ s/\.map$//; |
|
|
1185 | |
1181 | # map plan: |
1186 | # map plan: |
1182 | # |
1187 | # |
1183 | # /! non-realised random map exit (special hack!) |
1188 | # /! non-realised random map exit (special hack!) |
1184 | # {... are special paths that are not being touched |
1189 | # {... are special paths that are not being touched |
1185 | # ?xxx/... are special absolute paths |
1190 | # ?xxx/... are special absolute paths |
… | |
… | |
1221 | $self->init; # pass $1 etc. |
1226 | $self->init; # pass $1 etc. |
1222 | return $self; |
1227 | return $self; |
1223 | } |
1228 | } |
1224 | } |
1229 | } |
1225 | |
1230 | |
1226 | Carp::carp "unable to resolve path '$path'."; |
1231 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1227 | () |
1232 | () |
1228 | } |
1233 | } |
1229 | |
1234 | |
1230 | sub init { |
1235 | sub init { |
1231 | my ($self) = @_; |
1236 | my ($self) = @_; |
… | |
… | |
1360 | $map->{last_save} = $cf::RUNTIME; |
1365 | $map->{last_save} = $cf::RUNTIME; |
1361 | |
1366 | |
1362 | $map->load_header |
1367 | $map->load_header |
1363 | or return; |
1368 | or return; |
1364 | |
1369 | |
1365 | if ($map->should_reset) { |
1370 | if ($map->should_reset && 0) {#d#TODO# disabled, crashy (locking issue?) |
1366 | # doing this can freeze the server in a sync job, obviously |
1371 | # doing this can freeze the server in a sync job, obviously |
1367 | #$cf::WAIT_FOR_TICK->wait; |
1372 | #$cf::WAIT_FOR_TICK->wait; |
1368 | $map->reset; |
1373 | $map->reset; |
1369 | undef $guard; |
1374 | undef $guard; |
1370 | $map = find $path |
1375 | return find $path; |
1371 | or return; |
|
|
1372 | } |
1376 | } |
1373 | |
1377 | |
1374 | $cf::MAP{$path} = $map |
1378 | $cf::MAP{$path} = $map |
1375 | } |
1379 | } |
1376 | } |
1380 | } |
… | |
… | |
1378 | sub pre_load { } |
1382 | sub pre_load { } |
1379 | sub post_load { } |
1383 | sub post_load { } |
1380 | |
1384 | |
1381 | sub load { |
1385 | sub load { |
1382 | my ($self) = @_; |
1386 | my ($self) = @_; |
|
|
1387 | |
|
|
1388 | local $self->{deny_reset} = 1; # loading can take a long time |
1383 | |
1389 | |
1384 | my $path = $self->{path}; |
1390 | my $path = $self->{path}; |
1385 | my $guard = cf::lock_acquire "map_load:$path"; |
1391 | my $guard = cf::lock_acquire "map_load:$path"; |
1386 | |
1392 | |
1387 | return if $self->in_memory != cf::MAP_SWAPPED; |
1393 | return if $self->in_memory != cf::MAP_SWAPPED; |
… | |
… | |
1496 | delete $MAP_PREFETCH{$path}; |
1502 | delete $MAP_PREFETCH{$path}; |
1497 | } |
1503 | } |
1498 | } |
1504 | } |
1499 | undef $MAP_PREFETCHER; |
1505 | undef $MAP_PREFETCHER; |
1500 | }; |
1506 | }; |
|
|
1507 | $MAP_PREFETCHER->prio (6); |
1501 | |
1508 | |
1502 | () |
1509 | () |
1503 | } |
1510 | } |
1504 | |
1511 | |
1505 | sub save { |
1512 | sub save { |
… | |
… | |
1624 | for (@$files) { |
1631 | for (@$files) { |
1625 | utf8::decode $_; |
1632 | utf8::decode $_; |
1626 | next if /\.pst$/; |
1633 | next if /\.pst$/; |
1627 | next unless /^$PATH_SEP/o; |
1634 | next unless /^$PATH_SEP/o; |
1628 | |
1635 | |
1629 | s/\.map$//; |
1636 | push @paths, cf::map::normalise $_; |
1630 | push @paths, $_; |
|
|
1631 | } |
1637 | } |
1632 | |
1638 | |
1633 | \@paths |
1639 | \@paths |
1634 | } |
1640 | } |
1635 | |
1641 | |
… | |
… | |
1729 | =cut |
1735 | =cut |
1730 | |
1736 | |
1731 | sub link_map { |
1737 | sub link_map { |
1732 | unless ($LINK_MAP) { |
1738 | unless ($LINK_MAP) { |
1733 | $LINK_MAP = cf::map::find "{link}" |
1739 | $LINK_MAP = cf::map::find "{link}" |
1734 | or do { warn "FATAL: unable to provide {link} map, exiting."; exit 1 }; |
1740 | or cf::cleanup "FATAL: unable to provide {link} map, exiting."; |
1735 | $LINK_MAP->load; |
1741 | $LINK_MAP->load; |
1736 | } |
1742 | } |
1737 | |
1743 | |
1738 | $LINK_MAP |
1744 | $LINK_MAP |
1739 | } |
1745 | } |
… | |
… | |
1746 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1752 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1747 | |
1753 | |
1748 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1754 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1749 | if $self->map; |
1755 | if $self->map; |
1750 | |
1756 | |
1751 | $self->enter_map ($LINK_MAP || link_map, 20, 20); |
1757 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
1752 | } |
1758 | } |
1753 | |
1759 | |
1754 | sub cf::object::player::leave_link { |
1760 | sub cf::object::player::leave_link { |
1755 | my ($self, $map, $x, $y) = @_; |
1761 | my ($self, $map, $x, $y) = @_; |
1756 | |
1762 | |
… | |
… | |
1823 | my ($self, $path, $x, $y) = @_; |
1829 | my ($self, $path, $x, $y) = @_; |
1824 | |
1830 | |
1825 | $self->enter_link; |
1831 | $self->enter_link; |
1826 | |
1832 | |
1827 | (async { |
1833 | (async { |
|
|
1834 | my $map = eval { |
1828 | my $map = cf::map::find $path; |
1835 | my $map = cf::map::find $path; |
1829 | $map = $map->customise_for ($self) if $map; |
1836 | $map = $map->customise_for ($self) if $map; |
1830 | |
1837 | $map |
1831 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1838 | } or |
1832 | # if $map; |
|
|
1833 | |
|
|
1834 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1839 | $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1835 | |
1840 | |
1836 | $self->leave_link ($map, $x, $y); |
1841 | $self->leave_link ($map, $x, $y); |
1837 | })->prio (1); |
1842 | })->prio (1); |
1838 | } |
1843 | } |
1839 | |
1844 | |
… | |
… | |
1870 | # that depends on the exit object |
1875 | # that depends on the exit object |
1871 | |
1876 | |
1872 | my $rmp = parse_random_map_params $exit->msg; |
1877 | my $rmp = parse_random_map_params $exit->msg; |
1873 | |
1878 | |
1874 | if ($exit->map) { |
1879 | if ($exit->map) { |
1875 | $rmp->{region} = $exit->map->region_name; |
1880 | $rmp->{region} = $exit->region->name; |
1876 | $rmp->{origin_map} = $exit->map->path; |
1881 | $rmp->{origin_map} = $exit->map->path; |
1877 | $rmp->{origin_x} = $exit->x; |
1882 | $rmp->{origin_x} = $exit->x; |
1878 | $rmp->{origin_y} = $exit->y; |
1883 | $rmp->{origin_y} = $exit->y; |
1879 | } |
1884 | } |
1880 | |
1885 | |
… | |
… | |
1897 | sub cf::object::player::enter_exit { |
1902 | sub cf::object::player::enter_exit { |
1898 | my ($self, $exit) = @_; |
1903 | my ($self, $exit) = @_; |
1899 | |
1904 | |
1900 | return unless $self->type == cf::PLAYER; |
1905 | return unless $self->type == cf::PLAYER; |
1901 | |
1906 | |
|
|
1907 | if ($exit->slaying eq "/!") { |
|
|
1908 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1909 | cf::sync_job { prepare_random_map $exit }; |
|
|
1910 | } |
|
|
1911 | |
|
|
1912 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
|
|
1913 | my $hp = $exit->stats->hp; |
|
|
1914 | my $sp = $exit->stats->sp; |
|
|
1915 | |
1902 | $self->enter_link; |
1916 | $self->enter_link; |
1903 | |
1917 | |
1904 | (async { |
1918 | (async { |
1905 | $self->deactivate_recursive; # just to be sure |
1919 | $self->deactivate_recursive; # just to be sure |
1906 | unless (eval { |
1920 | unless (eval { |
1907 | prepare_random_map $exit |
1921 | $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 | |
1922 | |
1913 | 1; |
1923 | 1; |
1914 | }) { |
1924 | }) { |
1915 | $self->message ("Something went wrong deep within the crossfire server. " |
1925 | $self->message ("Something went wrong deep within the crossfire server. " |
1916 | . "I'll try to bring you back to the map you were before. " |
1926 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
2205 | sub db_sync() { |
2215 | sub db_sync() { |
2206 | db_save if $dirty; |
2216 | db_save if $dirty; |
2207 | undef $dirty; |
2217 | undef $dirty; |
2208 | } |
2218 | } |
2209 | |
2219 | |
2210 | my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2220 | my $idle = Event->idle ( |
2211 | db_sync; |
2221 | reentrant => 0, |
|
|
2222 | min => 10, |
|
|
2223 | max => 20, |
|
|
2224 | repeat => 0, |
|
|
2225 | data => WF_AUTOCANCEL, |
|
|
2226 | cb => \&db_sync, |
2212 | }); |
2227 | ); |
2213 | |
2228 | |
2214 | sub db_dirty() { |
2229 | sub db_dirty() { |
2215 | $dirty = 1; |
2230 | $dirty = 1; |
2216 | $idle->start; |
2231 | $idle->start; |
2217 | } |
2232 | } |
… | |
… | |
2285 | |
2300 | |
2286 | # install some emergency cleanup handlers |
2301 | # install some emergency cleanup handlers |
2287 | BEGIN { |
2302 | BEGIN { |
2288 | for my $signal (qw(INT HUP TERM)) { |
2303 | for my $signal (qw(INT HUP TERM)) { |
2289 | Event->signal ( |
2304 | Event->signal ( |
|
|
2305 | reentrant => 0, |
2290 | data => WF_AUTOCANCEL, |
2306 | data => WF_AUTOCANCEL, |
2291 | signal => $signal, |
2307 | signal => $signal, |
|
|
2308 | prio => 0, |
2292 | cb => sub { |
2309 | cb => sub { |
2293 | cf::cleanup "SIG$signal"; |
2310 | cf::cleanup "SIG$signal"; |
2294 | }, |
2311 | }, |
2295 | ); |
2312 | ); |
2296 | } |
2313 | } |
2297 | } |
2314 | } |
… | |
… | |
2452 | $who->message ("reloading server."); |
2469 | $who->message ("reloading server."); |
2453 | |
2470 | |
2454 | # doing reload synchronously and two reloads happen back-to-back, |
2471 | # doing reload synchronously and two reloads happen back-to-back, |
2455 | # coro crashes during coro_state_free->destroy here. |
2472 | # coro crashes during coro_state_free->destroy here. |
2456 | |
2473 | |
2457 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2474 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2475 | reentrant => 0, |
|
|
2476 | after => 0, |
|
|
2477 | data => WF_AUTOCANCEL, |
|
|
2478 | cb => sub { |
2458 | reload; |
2479 | reload; |
2459 | undef $RELOAD_WATCHER; |
2480 | undef $RELOAD_WATCHER; |
|
|
2481 | }, |
2460 | }); |
2482 | ); |
2461 | } |
2483 | } |
2462 | }; |
2484 | }; |
2463 | |
2485 | |
2464 | unshift @INC, $LIBDIR; |
2486 | unshift @INC, $LIBDIR; |
2465 | |
2487 | |
… | |
… | |
2485 | $NEXT_TICK += $TICK; |
2507 | $NEXT_TICK += $TICK; |
2486 | |
2508 | |
2487 | $WAIT_FOR_TICK->broadcast; |
2509 | $WAIT_FOR_TICK->broadcast; |
2488 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2510 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2489 | |
2511 | |
|
|
2512 | Event::sweep; |
|
|
2513 | Coro::cede_notself; |
|
|
2514 | |
|
|
2515 | # my $AFTER = Event::time; |
|
|
2516 | # warn $AFTER - $NOW;#d# |
|
|
2517 | |
2490 | # if we are delayed by four ticks or more, skip them all |
2518 | # if we are delayed by four ticks or more, skip them all |
2491 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2519 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2492 | |
2520 | |
2493 | $TICK_WATCHER->at ($NEXT_TICK); |
2521 | $TICK_WATCHER->at ($NEXT_TICK); |
2494 | $TICK_WATCHER->start; |
2522 | $TICK_WATCHER->start; |
2495 | }, |
2523 | }, |
2496 | ); |
2524 | ); |
2497 | |
2525 | |
2498 | IO::AIO::max_poll_time $TICK * 0.2; |
2526 | IO::AIO::max_poll_time $TICK * 0.1; |
2499 | |
2527 | |
|
|
2528 | undef $Coro::AIO::WATCHER; |
2500 | $AIO_POLL_WATCHER = Event->io ( |
2529 | $AIO_POLL_WATCHER = Event->io ( |
|
|
2530 | reentrant => 0, |
2501 | fd => IO::AIO::poll_fileno, |
2531 | fd => IO::AIO::poll_fileno, |
2502 | poll => 'r', |
2532 | poll => 'r', |
2503 | prio => 5, |
2533 | prio => 6, |
2504 | data => WF_AUTOCANCEL, |
2534 | data => WF_AUTOCANCEL, |
2505 | cb => \&IO::AIO::poll_cb, |
2535 | cb => \&IO::AIO::poll_cb, |
2506 | ); |
2536 | ); |
2507 | |
2537 | |
2508 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2538 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2539 | reentrant => 0, |
2509 | data => WF_AUTOCANCEL, |
2540 | data => WF_AUTOCANCEL, |
2510 | after => 1, |
2541 | after => 1, |
2511 | interval => 10, |
2542 | interval => 10, |
2512 | prio => 6, # keep it lowest so it acts like a watchdog |
2543 | prio => 6, # keep it lowest so it acts like a watchdog |
2513 | cb => Coro::unblock_sub { |
2544 | cb => Coro::unblock_sub { |
2514 | write_runtime |
2545 | write_runtime |
2515 | or warn "ERROR: unable to write runtime file: $!"; |
2546 | or warn "ERROR: unable to write runtime file: $!"; |
2516 | }, |
2547 | }, |
2517 | ); |
2548 | ); |
2518 | |
2549 | |