… | |
… | |
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 | |
… | |
… | |
774 | } |
776 | } |
775 | } else { |
777 | } else { |
776 | aio_unlink $filename; |
778 | aio_unlink $filename; |
777 | aio_unlink "$filename.pst"; |
779 | aio_unlink "$filename.pst"; |
778 | } |
780 | } |
|
|
781 | |
|
|
782 | #d##TODO# nuke non .map-files if exist |
|
|
783 | if ($filename =~ s/\.map$//) { |
|
|
784 | aio_unlink $filename; |
|
|
785 | aio_unlink "$filename.pst"; |
|
|
786 | } |
779 | } |
787 | } |
780 | } |
788 | } |
781 | |
789 | |
782 | sub object_freezer_as_string { |
790 | sub object_freezer_as_string { |
783 | my ($rdata, $objs) = @_; |
791 | my ($rdata, $objs) = @_; |
… | |
… | |
789 | |
797 | |
790 | sub object_thawer_load { |
798 | sub object_thawer_load { |
791 | my ($filename) = @_; |
799 | my ($filename) = @_; |
792 | |
800 | |
793 | my ($data, $av); |
801 | my ($data, $av); |
|
|
802 | |
|
|
803 | #d#TODO remove .map if file does not exist |
|
|
804 | aio_stat $filename and $filename =~ s/\.map$//; |
794 | |
805 | |
795 | (aio_load $filename, $data) >= 0 |
806 | (aio_load $filename, $data) >= 0 |
796 | or return; |
807 | or return; |
797 | |
808 | |
798 | unless (aio_stat "$filename.pst") { |
809 | unless (aio_stat "$filename.pst") { |
… | |
… | |
1068 | =cut |
1079 | =cut |
1069 | |
1080 | |
1070 | sub maps($) { |
1081 | sub maps($) { |
1071 | my ($pl) = @_; |
1082 | my ($pl) = @_; |
1072 | |
1083 | |
|
|
1084 | $pl = ref $pl ? $pl->ob->name : $pl; |
|
|
1085 | |
1073 | my $files = aio_readdir playerdir $pl |
1086 | my $files = aio_readdir playerdir $pl |
1074 | or return; |
1087 | or return; |
1075 | |
1088 | |
1076 | my @paths; |
1089 | my @paths; |
1077 | |
1090 | |
1078 | for (@$files) { |
1091 | for (@$files) { |
1079 | utf8::decode $_; |
1092 | utf8::decode $_; |
1080 | next if /\.(?:pl|pst)$/; |
1093 | next if /\.(?:pl|pst)$/; |
1081 | next unless /^$PATH_SEP/o; |
1094 | next unless /^$PATH_SEP/o; |
1082 | |
1095 | |
1083 | s/\.map$//; |
1096 | push @paths, cf::map::normalise "~$pl/$_"; |
1084 | push @paths, "~" . $pl->ob->name . "/" . $_; |
|
|
1085 | } |
1097 | } |
1086 | |
1098 | |
1087 | \@paths |
1099 | \@paths |
1088 | } |
1100 | } |
1089 | |
1101 | |
… | |
… | |
1140 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1152 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1141 | (cf::region::find $rmp->{region}), $rmp->{custom} |
1153 | (cf::region::find $rmp->{region}), $rmp->{custom} |
1142 | ) |
1154 | ) |
1143 | } |
1155 | } |
1144 | |
1156 | |
|
|
1157 | =item cf::map->register ($regex, $prio) |
|
|
1158 | |
|
|
1159 | Register a handler for the map path matching the given regex at the |
|
|
1160 | givne priority (higher is better, built-in handlers have priority 0, the |
|
|
1161 | default). |
|
|
1162 | |
|
|
1163 | =cut |
|
|
1164 | |
1145 | sub register { |
1165 | sub register { |
1146 | my (undef, $regex) = @_; |
1166 | my (undef, $regex, $prio) = @_; |
1147 | my $pkg = caller; |
1167 | my $pkg = caller; |
1148 | |
1168 | |
1149 | no strict; |
1169 | no strict; |
1150 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1170 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1151 | |
1171 | |
1152 | $EXT_MAP{$pkg} = qr<$regex>; |
1172 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1153 | } |
1173 | } |
1154 | |
1174 | |
1155 | # also paths starting with '/' |
1175 | # also paths starting with '/' |
1156 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1176 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
1157 | |
1177 | |
1158 | sub thawer_merge { |
1178 | sub thawer_merge { |
1159 | my ($self, $merge) = @_; |
1179 | my ($self, $merge) = @_; |
1160 | |
1180 | |
1161 | # we have to keep some variables in memory intact |
1181 | # we have to keep some variables in memory intact |
… | |
… | |
1167 | $self->SUPER::thawer_merge ($merge); |
1187 | $self->SUPER::thawer_merge ($merge); |
1168 | } |
1188 | } |
1169 | |
1189 | |
1170 | sub normalise { |
1190 | sub normalise { |
1171 | my ($path, $base) = @_; |
1191 | my ($path, $base) = @_; |
|
|
1192 | |
|
|
1193 | $path = "$path"; # make sure its a string |
|
|
1194 | |
|
|
1195 | $path =~ s/\.map$//; |
1172 | |
1196 | |
1173 | # map plan: |
1197 | # map plan: |
1174 | # |
1198 | # |
1175 | # /! non-realised random map exit (special hack!) |
1199 | # /! non-realised random map exit (special hack!) |
1176 | # {... are special paths that are not being touched |
1200 | # {... are special paths that are not being touched |
… | |
… | |
1204 | |
1228 | |
1205 | return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object |
1229 | return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object |
1206 | |
1230 | |
1207 | $path = normalise $path, $base; |
1231 | $path = normalise $path, $base; |
1208 | |
1232 | |
1209 | for my $pkg (keys %EXT_MAP) { |
1233 | for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) { |
1210 | if ($path =~ $EXT_MAP{$pkg}) { |
1234 | if ($path =~ $EXT_MAP{$pkg}[1]) { |
1211 | my $self = bless cf::map::new, $pkg; |
1235 | my $self = bless cf::map::new, $pkg; |
1212 | $self->{path} = $path; $self->path ($path); |
1236 | $self->{path} = $path; $self->path ($path); |
1213 | $self->init; # pass $1 etc. |
1237 | $self->init; # pass $1 etc. |
1214 | return $self; |
1238 | return $self; |
1215 | } |
1239 | } |
1216 | } |
1240 | } |
1217 | |
1241 | |
1218 | Carp::carp "unable to resolve path '$path'."; |
1242 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1219 | () |
1243 | () |
1220 | } |
1244 | } |
1221 | |
1245 | |
1222 | sub init { |
1246 | sub init { |
1223 | my ($self) = @_; |
1247 | my ($self) = @_; |
… | |
… | |
1238 | |
1262 | |
1239 | # the original (read-only) location |
1263 | # the original (read-only) location |
1240 | sub load_path { |
1264 | sub load_path { |
1241 | my ($self) = @_; |
1265 | my ($self) = @_; |
1242 | |
1266 | |
1243 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
1267 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1244 | } |
1268 | } |
1245 | |
1269 | |
1246 | # the temporary/swap location |
1270 | # the temporary/swap location |
1247 | sub save_path { |
1271 | sub save_path { |
1248 | my ($self) = @_; |
1272 | my ($self) = @_; |
1249 | |
1273 | |
1250 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1274 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1251 | sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $path |
1275 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1252 | } |
1276 | } |
1253 | |
1277 | |
1254 | # the unique path, undef == no special unique path |
1278 | # the unique path, undef == no special unique path |
1255 | sub uniq_path { |
1279 | sub uniq_path { |
1256 | my ($self) = @_; |
1280 | my ($self) = @_; |
… | |
… | |
1272 | my ($self) = @_; |
1296 | my ($self) = @_; |
1273 | |
1297 | |
1274 | utf8::encode (my $save = $self->save_path); |
1298 | utf8::encode (my $save = $self->save_path); |
1275 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1299 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1276 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1300 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
|
|
1301 | |
|
|
1302 | #d#TODO remove .map and also nuke |
|
|
1303 | $save =~ s/\.map// or return;#d# |
|
|
1304 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1305 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
1277 | } |
1306 | } |
1278 | |
1307 | |
1279 | sub load_header_from($) { |
1308 | sub load_header_from($) { |
1280 | my ($self, $path) = @_; |
1309 | my ($self, $path) = @_; |
1281 | |
1310 | |
1282 | utf8::encode $path; |
1311 | utf8::encode $path; |
1283 | aio_open $path, O_RDONLY, 0 |
1312 | #aio_open $path, O_RDONLY, 0 |
1284 | or return; |
1313 | # or return; |
1285 | |
1314 | |
1286 | $self->_load_header ($path) |
1315 | $self->_load_header ($path) |
1287 | or return; |
1316 | or return; |
1288 | |
1317 | |
1289 | $self->{load_path} = $path; |
1318 | $self->{load_path} = $path; |
1290 | |
1319 | |
1291 | 1 |
1320 | 1 |
1292 | } |
1321 | } |
1293 | |
1322 | |
1294 | sub load_orig { |
1323 | sub load_header_orig { |
1295 | my ($self) = @_; |
1324 | my ($self) = @_; |
1296 | |
1325 | |
1297 | $self->load_header_from ($self->load_path) |
1326 | $self->load_header_from ($self->load_path) |
1298 | } |
1327 | } |
1299 | |
1328 | |
1300 | sub load_temp { |
1329 | sub load_header_temp { |
1301 | my ($self) = @_; |
1330 | my ($self) = @_; |
1302 | |
1331 | |
1303 | $self->load_header_from ($self->save_path) |
1332 | $self->load_header_from ($self->save_path) |
|
|
1333 | } |
|
|
1334 | |
|
|
1335 | sub prepare_temp { |
|
|
1336 | my ($self) = @_; |
|
|
1337 | |
|
|
1338 | $self->last_access ((delete $self->{last_access}) |
|
|
1339 | || $cf::RUNTIME); #d# |
|
|
1340 | # safety |
|
|
1341 | $self->{instantiate_time} = $cf::RUNTIME |
|
|
1342 | if $self->{instantiate_time} > $cf::RUNTIME; |
|
|
1343 | } |
|
|
1344 | |
|
|
1345 | sub prepare_orig { |
|
|
1346 | my ($self) = @_; |
|
|
1347 | |
|
|
1348 | $self->{load_original} = 1; |
|
|
1349 | $self->{instantiate_time} = $cf::RUNTIME; |
|
|
1350 | $self->last_access ($cf::RUNTIME); |
|
|
1351 | $self->instantiate; |
1304 | } |
1352 | } |
1305 | |
1353 | |
1306 | sub load_header { |
1354 | sub load_header { |
1307 | my ($self) = @_; |
1355 | my ($self) = @_; |
1308 | |
1356 | |
1309 | if ($self->load_temp) { |
1357 | if ($self->load_header_temp) { |
1310 | $self->last_access ((delete $self->{last_access}) |
1358 | $self->prepare_temp; |
1311 | || $cf::RUNTIME); #d# |
|
|
1312 | # safety |
|
|
1313 | $self->{instantiate_time} = $cf::RUNTIME |
|
|
1314 | if $self->{instantiate_time} > $cf::RUNTIME; |
|
|
1315 | } else { |
1359 | } else { |
1316 | $self->load_orig |
1360 | $self->load_header_orig |
1317 | or return; |
1361 | or return; |
1318 | |
1362 | $self->prepare_orig; |
1319 | $self->{load_original} = 1; |
|
|
1320 | $self->{instantiate_time} = $cf::RUNTIME; |
|
|
1321 | $self->last_access ($cf::RUNTIME); |
|
|
1322 | $self->instantiate; |
|
|
1323 | } |
1363 | } |
1324 | |
1364 | |
1325 | 1 |
1365 | 1 |
1326 | } |
1366 | } |
1327 | |
1367 | |
… | |
… | |
1341 | $map->{last_save} = $cf::RUNTIME; |
1381 | $map->{last_save} = $cf::RUNTIME; |
1342 | |
1382 | |
1343 | $map->load_header |
1383 | $map->load_header |
1344 | or return; |
1384 | or return; |
1345 | |
1385 | |
1346 | if ($map->should_reset) { |
1386 | if ($map->should_reset && 0) {#d#TODO# disabled, crashy (locking issue?) |
1347 | # doing this can freeze the server in a sync job, obviously |
1387 | # doing this can freeze the server in a sync job, obviously |
1348 | #$cf::WAIT_FOR_TICK->wait; |
1388 | #$cf::WAIT_FOR_TICK->wait; |
1349 | $map->reset; |
1389 | $map->reset; |
1350 | undef $guard; |
1390 | undef $guard; |
1351 | $map = find $path |
1391 | return find $path; |
1352 | or return; |
|
|
1353 | } |
1392 | } |
1354 | |
1393 | |
1355 | $cf::MAP{$path} = $map |
1394 | $cf::MAP{$path} = $map |
1356 | } |
1395 | } |
1357 | } |
1396 | } |
|
|
1397 | |
|
|
1398 | sub pre_load { } |
|
|
1399 | sub post_load { } |
1358 | |
1400 | |
1359 | sub load { |
1401 | sub load { |
1360 | my ($self) = @_; |
1402 | my ($self) = @_; |
1361 | |
1403 | |
|
|
1404 | local $self->{deny_reset} = 1; # loading can take a long time |
|
|
1405 | |
1362 | my $path = $self->{path}; |
1406 | my $path = $self->{path}; |
1363 | my $guard = cf::lock_acquire "map_load:$path"; |
1407 | my $guard = cf::lock_acquire "map_load:$path"; |
1364 | |
1408 | |
1365 | return if $self->in_memory != cf::MAP_SWAPPED; |
1409 | return if $self->in_memory != cf::MAP_SWAPPED; |
1366 | |
1410 | |
1367 | $self->in_memory (cf::MAP_LOADING); |
1411 | $self->in_memory (cf::MAP_LOADING); |
1368 | |
1412 | |
1369 | $self->alloc; |
1413 | $self->alloc; |
|
|
1414 | |
|
|
1415 | $self->pre_load; |
|
|
1416 | |
1370 | $self->_load_objects ($self->{load_path}, 1) |
1417 | $self->_load_objects ($self->{load_path}, 1) |
1371 | or return; |
1418 | or return; |
1372 | |
1419 | |
1373 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1420 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1374 | if delete $self->{load_original}; |
1421 | if delete $self->{load_original}; |
… | |
… | |
1396 | unless $self->difficulty; |
1443 | unless $self->difficulty; |
1397 | Coro::cede; |
1444 | Coro::cede; |
1398 | $self->activate; |
1445 | $self->activate; |
1399 | } |
1446 | } |
1400 | |
1447 | |
|
|
1448 | $self->post_load; |
|
|
1449 | |
1401 | $self->in_memory (cf::MAP_IN_MEMORY); |
1450 | $self->in_memory (cf::MAP_IN_MEMORY); |
1402 | } |
1451 | } |
1403 | |
1452 | |
1404 | sub customise_for { |
1453 | sub customise_for { |
1405 | my ($self, $ob) = @_; |
1454 | my ($self, $ob) = @_; |
… | |
… | |
1469 | delete $MAP_PREFETCH{$path}; |
1518 | delete $MAP_PREFETCH{$path}; |
1470 | } |
1519 | } |
1471 | } |
1520 | } |
1472 | undef $MAP_PREFETCHER; |
1521 | undef $MAP_PREFETCHER; |
1473 | }; |
1522 | }; |
|
|
1523 | $MAP_PREFETCHER->prio (6); |
1474 | |
1524 | |
1475 | () |
1525 | () |
1476 | } |
1526 | } |
1477 | |
1527 | |
1478 | sub save { |
1528 | sub save { |
… | |
… | |
1597 | for (@$files) { |
1647 | for (@$files) { |
1598 | utf8::decode $_; |
1648 | utf8::decode $_; |
1599 | next if /\.pst$/; |
1649 | next if /\.pst$/; |
1600 | next unless /^$PATH_SEP/o; |
1650 | next unless /^$PATH_SEP/o; |
1601 | |
1651 | |
1602 | s/\.map$//; |
1652 | push @paths, cf::map::normalise $_; |
1603 | push @paths, $_; |
|
|
1604 | } |
1653 | } |
1605 | |
1654 | |
1606 | \@paths |
1655 | \@paths |
1607 | } |
1656 | } |
1608 | |
1657 | |
… | |
… | |
1702 | =cut |
1751 | =cut |
1703 | |
1752 | |
1704 | sub link_map { |
1753 | sub link_map { |
1705 | unless ($LINK_MAP) { |
1754 | unless ($LINK_MAP) { |
1706 | $LINK_MAP = cf::map::find "{link}" |
1755 | $LINK_MAP = cf::map::find "{link}" |
1707 | or do { warn "FATAL: unable to provide {link} map, exiting."; exit 1 }; |
1756 | or cf::cleanup "FATAL: unable to provide {link} map, exiting."; |
1708 | $LINK_MAP->load; |
1757 | $LINK_MAP->load; |
1709 | } |
1758 | } |
1710 | |
1759 | |
1711 | $LINK_MAP |
1760 | $LINK_MAP |
1712 | } |
1761 | } |
… | |
… | |
1719 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1768 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1720 | |
1769 | |
1721 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1770 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1722 | if $self->map; |
1771 | if $self->map; |
1723 | |
1772 | |
1724 | $self->enter_map ($LINK_MAP || link_map, 20, 20); |
1773 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
1725 | } |
1774 | } |
1726 | |
1775 | |
1727 | sub cf::object::player::leave_link { |
1776 | sub cf::object::player::leave_link { |
1728 | my ($self, $map, $x, $y) = @_; |
1777 | my ($self, $map, $x, $y) = @_; |
1729 | |
1778 | |
… | |
… | |
1796 | my ($self, $path, $x, $y) = @_; |
1845 | my ($self, $path, $x, $y) = @_; |
1797 | |
1846 | |
1798 | $self->enter_link; |
1847 | $self->enter_link; |
1799 | |
1848 | |
1800 | (async { |
1849 | (async { |
|
|
1850 | my $map = eval { |
1801 | my $map = cf::map::find $path; |
1851 | my $map = cf::map::find $path; |
1802 | $map = $map->customise_for ($self) if $map; |
1852 | $map = $map->customise_for ($self) if $map; |
1803 | |
1853 | $map |
1804 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1854 | } or |
1805 | # if $map; |
|
|
1806 | |
|
|
1807 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1855 | $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1808 | |
1856 | |
1809 | $self->leave_link ($map, $x, $y); |
1857 | $self->leave_link ($map, $x, $y); |
1810 | })->prio (1); |
1858 | })->prio (1); |
1811 | } |
1859 | } |
1812 | |
1860 | |
… | |
… | |
1843 | # that depends on the exit object |
1891 | # that depends on the exit object |
1844 | |
1892 | |
1845 | my $rmp = parse_random_map_params $exit->msg; |
1893 | my $rmp = parse_random_map_params $exit->msg; |
1846 | |
1894 | |
1847 | if ($exit->map) { |
1895 | if ($exit->map) { |
1848 | $rmp->{region} = $exit->map->region_name; |
1896 | $rmp->{region} = $exit->region->name; |
1849 | $rmp->{origin_map} = $exit->map->path; |
1897 | $rmp->{origin_map} = $exit->map->path; |
1850 | $rmp->{origin_x} = $exit->x; |
1898 | $rmp->{origin_x} = $exit->x; |
1851 | $rmp->{origin_y} = $exit->y; |
1899 | $rmp->{origin_y} = $exit->y; |
1852 | } |
1900 | } |
1853 | |
1901 | |
… | |
… | |
1870 | sub cf::object::player::enter_exit { |
1918 | sub cf::object::player::enter_exit { |
1871 | my ($self, $exit) = @_; |
1919 | my ($self, $exit) = @_; |
1872 | |
1920 | |
1873 | return unless $self->type == cf::PLAYER; |
1921 | return unless $self->type == cf::PLAYER; |
1874 | |
1922 | |
|
|
1923 | if ($exit->slaying eq "/!") { |
|
|
1924 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1925 | cf::sync_job { prepare_random_map $exit }; |
|
|
1926 | } |
|
|
1927 | |
|
|
1928 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
|
|
1929 | my $hp = $exit->stats->hp; |
|
|
1930 | my $sp = $exit->stats->sp; |
|
|
1931 | |
1875 | $self->enter_link; |
1932 | $self->enter_link; |
1876 | |
1933 | |
1877 | (async { |
1934 | (async { |
1878 | $self->deactivate_recursive; # just to be sure |
1935 | $self->deactivate_recursive; # just to be sure |
1879 | unless (eval { |
1936 | unless (eval { |
1880 | prepare_random_map $exit |
1937 | $self->goto ($slaying, $hp, $sp); |
1881 | if $exit->slaying eq "/!"; |
|
|
1882 | |
|
|
1883 | my $path = new_from_path cf::map $exit->slaying, $exit->map && $exit->map->path; |
|
|
1884 | $self->goto ($path, $exit->stats->hp, $exit->stats->sp); |
|
|
1885 | |
1938 | |
1886 | 1; |
1939 | 1; |
1887 | }) { |
1940 | }) { |
1888 | $self->message ("Something went wrong deep within the crossfire server. " |
1941 | $self->message ("Something went wrong deep within the crossfire server. " |
1889 | . "I'll try to bring you back to the map you were before. " |
1942 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
1950 | # get handled first |
2003 | # get handled first |
1951 | my $queue = delete $ns->{query_queue} |
2004 | my $queue = delete $ns->{query_queue} |
1952 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
2005 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1953 | |
2006 | |
1954 | (shift @$queue)->[1]->($msg); |
2007 | (shift @$queue)->[1]->($msg); |
|
|
2008 | return unless $ns->valid; # temporary(?) workaround for callback destroying socket |
1955 | |
2009 | |
1956 | push @{ $ns->{query_queue} }, @$queue; |
2010 | push @{ $ns->{query_queue} }, @$queue; |
1957 | |
2011 | |
1958 | if (@{ $ns->{query_queue} } == @$queue) { |
2012 | if (@{ $ns->{query_queue} } == @$queue) { |
1959 | if (@$queue) { |
2013 | if (@$queue) { |
… | |
… | |
2178 | sub db_sync() { |
2232 | sub db_sync() { |
2179 | db_save if $dirty; |
2233 | db_save if $dirty; |
2180 | undef $dirty; |
2234 | undef $dirty; |
2181 | } |
2235 | } |
2182 | |
2236 | |
2183 | my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2237 | my $idle = Event->idle ( |
2184 | db_sync; |
2238 | reentrant => 0, |
|
|
2239 | min => 10, |
|
|
2240 | max => 20, |
|
|
2241 | repeat => 0, |
|
|
2242 | data => WF_AUTOCANCEL, |
|
|
2243 | cb => \&db_sync, |
2185 | }); |
2244 | ); |
2186 | |
2245 | |
2187 | sub db_dirty() { |
2246 | sub db_dirty() { |
2188 | $dirty = 1; |
2247 | $dirty = 1; |
2189 | $idle->start; |
2248 | $idle->start; |
2190 | } |
2249 | } |
… | |
… | |
2211 | }, |
2270 | }, |
2212 | ); |
2271 | ); |
2213 | } |
2272 | } |
2214 | |
2273 | |
2215 | ############################################################################# |
2274 | ############################################################################# |
2216 | # the server's main() |
2275 | # the server's init and main functions |
|
|
2276 | |
|
|
2277 | sub load_resources { |
|
|
2278 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
|
|
2279 | or die "unable to load regions file\n";#d# |
|
|
2280 | } |
2217 | |
2281 | |
2218 | sub cfg_load { |
2282 | sub cfg_load { |
2219 | open my $fh, "<:utf8", cf::confdir . "/config" |
2283 | open my $fh, "<:utf8", cf::confdir . "/config" |
2220 | or return; |
2284 | or return; |
2221 | |
2285 | |
… | |
… | |
2232 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2296 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2233 | and die "WARNING: m(un)lockall failed: $!\n"; |
2297 | and die "WARNING: m(un)lockall failed: $!\n"; |
2234 | }; |
2298 | }; |
2235 | warn $@ if $@; |
2299 | warn $@ if $@; |
2236 | } |
2300 | } |
|
|
2301 | } |
|
|
2302 | |
|
|
2303 | sub init { |
|
|
2304 | load_resources; |
2237 | } |
2305 | } |
2238 | |
2306 | |
2239 | sub main { |
2307 | sub main { |
2240 | # we must not ever block the main coroutine |
2308 | # we must not ever block the main coroutine |
2241 | local $Coro::idle = sub { |
2309 | local $Coro::idle = sub { |
… | |
… | |
2258 | |
2326 | |
2259 | # install some emergency cleanup handlers |
2327 | # install some emergency cleanup handlers |
2260 | BEGIN { |
2328 | BEGIN { |
2261 | for my $signal (qw(INT HUP TERM)) { |
2329 | for my $signal (qw(INT HUP TERM)) { |
2262 | Event->signal ( |
2330 | Event->signal ( |
|
|
2331 | reentrant => 0, |
2263 | data => WF_AUTOCANCEL, |
2332 | data => WF_AUTOCANCEL, |
2264 | signal => $signal, |
2333 | signal => $signal, |
|
|
2334 | prio => 0, |
2265 | cb => sub { |
2335 | cb => sub { |
2266 | cf::cleanup "SIG$signal"; |
2336 | cf::cleanup "SIG$signal"; |
2267 | }, |
2337 | }, |
2268 | ); |
2338 | ); |
2269 | } |
2339 | } |
2270 | } |
2340 | } |
… | |
… | |
2400 | warn "reattaching attachments to objects/players"; |
2470 | warn "reattaching attachments to objects/players"; |
2401 | _global_reattach; |
2471 | _global_reattach; |
2402 | warn "reattaching attachments to maps"; |
2472 | warn "reattaching attachments to maps"; |
2403 | reattach $_ for values %MAP; |
2473 | reattach $_ for values %MAP; |
2404 | |
2474 | |
|
|
2475 | warn "loading reloadable resources"; |
|
|
2476 | load_resources; |
|
|
2477 | |
2405 | warn "restarting server ticker"; |
2478 | warn "restarting server ticker"; |
2406 | |
2479 | |
2407 | $TICK_WATCHER->start; |
2480 | $TICK_WATCHER->start; |
2408 | }; |
2481 | }; |
2409 | |
2482 | |
… | |
… | |
2425 | $who->message ("reloading server."); |
2498 | $who->message ("reloading server."); |
2426 | |
2499 | |
2427 | # doing reload synchronously and two reloads happen back-to-back, |
2500 | # doing reload synchronously and two reloads happen back-to-back, |
2428 | # coro crashes during coro_state_free->destroy here. |
2501 | # coro crashes during coro_state_free->destroy here. |
2429 | |
2502 | |
2430 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2503 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2504 | reentrant => 0, |
|
|
2505 | after => 0, |
|
|
2506 | data => WF_AUTOCANCEL, |
|
|
2507 | cb => sub { |
2431 | reload; |
2508 | reload; |
2432 | undef $RELOAD_WATCHER; |
2509 | undef $RELOAD_WATCHER; |
|
|
2510 | }, |
2433 | }); |
2511 | ); |
2434 | } |
2512 | } |
2435 | }; |
2513 | }; |
2436 | |
2514 | |
2437 | unshift @INC, $LIBDIR; |
2515 | unshift @INC, $LIBDIR; |
2438 | |
2516 | |
… | |
… | |
2458 | $NEXT_TICK += $TICK; |
2536 | $NEXT_TICK += $TICK; |
2459 | |
2537 | |
2460 | $WAIT_FOR_TICK->broadcast; |
2538 | $WAIT_FOR_TICK->broadcast; |
2461 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2539 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2462 | |
2540 | |
|
|
2541 | Event::sweep; |
|
|
2542 | Coro::cede_notself; |
|
|
2543 | |
|
|
2544 | # my $AFTER = Event::time; |
|
|
2545 | # warn $AFTER - $NOW;#d# |
|
|
2546 | |
2463 | # if we are delayed by four ticks or more, skip them all |
2547 | # if we are delayed by four ticks or more, skip them all |
2464 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2548 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2465 | |
2549 | |
2466 | $TICK_WATCHER->at ($NEXT_TICK); |
2550 | $TICK_WATCHER->at ($NEXT_TICK); |
2467 | $TICK_WATCHER->start; |
2551 | $TICK_WATCHER->start; |
2468 | }, |
2552 | }, |
2469 | ); |
2553 | ); |
2470 | |
2554 | |
2471 | IO::AIO::max_poll_time $TICK * 0.2; |
2555 | IO::AIO::max_poll_time $TICK * 0.1; |
2472 | |
2556 | |
|
|
2557 | undef $Coro::AIO::WATCHER; |
2473 | $AIO_POLL_WATCHER = Event->io ( |
2558 | $AIO_POLL_WATCHER = Event->io ( |
|
|
2559 | reentrant => 0, |
2474 | fd => IO::AIO::poll_fileno, |
2560 | fd => IO::AIO::poll_fileno, |
2475 | poll => 'r', |
2561 | poll => 'r', |
2476 | prio => 5, |
2562 | prio => 6, |
2477 | data => WF_AUTOCANCEL, |
2563 | data => WF_AUTOCANCEL, |
2478 | cb => \&IO::AIO::poll_cb, |
2564 | cb => \&IO::AIO::poll_cb, |
2479 | ); |
2565 | ); |
2480 | |
2566 | |
2481 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2567 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2568 | reentrant => 0, |
2482 | data => WF_AUTOCANCEL, |
2569 | data => WF_AUTOCANCEL, |
2483 | after => 1, |
2570 | after => 1, |
2484 | interval => 10, |
2571 | interval => 10, |
2485 | prio => 6, # keep it lowest so it acts like a watchdog |
2572 | prio => 6, # keep it lowest so it acts like a watchdog |
2486 | cb => Coro::unblock_sub { |
2573 | cb => Coro::unblock_sub { |
2487 | write_runtime |
2574 | write_runtime |
2488 | or warn "ERROR: unable to write runtime file: $!"; |
2575 | or warn "ERROR: unable to write runtime file: $!"; |
2489 | }, |
2576 | }, |
2490 | ); |
2577 | ); |
2491 | |
2578 | |