… | |
… | |
289 | |
289 | |
290 | $path = $path->as_string if ref $path; |
290 | $path = $path->as_string if ref $path; |
291 | |
291 | |
292 | my $self = bless { }, $class; |
292 | my $self = bless { }, $class; |
293 | |
293 | |
|
|
294 | # {... are special paths that are not touched |
|
|
295 | # ?xxx/... are special absolute paths |
|
|
296 | # ?random/... random maps |
|
|
297 | # /! non-realised random map exit |
|
|
298 | # /... normal maps |
|
|
299 | # ~/... per-player maps without a specific player (DO NOT USE) |
|
|
300 | # ~user/... per-player map of a specific user |
|
|
301 | |
|
|
302 | if ($path =~ /^{/) { |
|
|
303 | # fine as it is |
294 | if ($path =~ s{^\?random/}{}) { |
304 | } elsif ($path =~ s{^\?random/}{}) { |
295 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
305 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
296 | $self->{random} = cf::from_json $data; |
306 | $self->{random} = cf::from_json $data; |
297 | } else { |
307 | } else { |
298 | if ($path =~ s{^~([^/]+)?}{}) { |
308 | if ($path =~ s{^~([^/]+)?}{}) { |
299 | $self->{user_rel} = 1; |
309 | $self->{user_rel} = 1; |
… | |
… | |
804 | or return; |
814 | or return; |
805 | |
815 | |
806 | unless (aio_stat "$filename.pst") { |
816 | unless (aio_stat "$filename.pst") { |
807 | (aio_load "$filename.pst", $av) >= 0 |
817 | (aio_load "$filename.pst", $av) >= 0 |
808 | or return; |
818 | or return; |
809 | $av = eval { (Storable::thaw <$av>)->{objs} }; |
819 | $av = eval { (Storable::thaw $av)->{objs} }; |
810 | } |
820 | } |
811 | |
821 | |
812 | return ($data, $av); |
822 | return ($data, $av); |
813 | } |
823 | } |
814 | |
824 | |
… | |
… | |
1238 | |
1248 | |
1239 | sub reset_at { |
1249 | sub reset_at { |
1240 | my ($self) = @_; |
1250 | my ($self) = @_; |
1241 | |
1251 | |
1242 | # TODO: safety, remove and allow resettable per-player maps |
1252 | # TODO: safety, remove and allow resettable per-player maps |
1243 | return 1e100 if $self->{path}{user_rel}; |
1253 | return 1e99 if $self->{path}{user_rel}; |
1244 | return 1e100 if $self->{deny_reset}; |
1254 | return 1e99 if $self->{deny_reset}; |
1245 | |
1255 | |
1246 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1256 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1247 | my $to = $self->reset_timeout || $DEFAULT_RESET; |
1257 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
1248 | $to = $MAX_RESET if $to > $MAX_RESET; |
|
|
1249 | |
1258 | |
1250 | $time + $to |
1259 | $time + $to |
1251 | } |
1260 | } |
1252 | |
1261 | |
1253 | sub should_reset { |
1262 | sub should_reset { |
… | |
… | |
1262 | utf8::encode (my $save = $self->{path}->save_path); |
1271 | utf8::encode (my $save = $self->{path}->save_path); |
1263 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
1272 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
1264 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
1273 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
1265 | } |
1274 | } |
1266 | |
1275 | |
|
|
1276 | sub rename { |
|
|
1277 | my ($self, $new_path) = @_; |
|
|
1278 | |
|
|
1279 | $self->unlink_save; |
|
|
1280 | |
|
|
1281 | delete $cf::MAP{$self->path}; |
|
|
1282 | $self->{path} = new cf::path $new_path; |
|
|
1283 | $self->path ($self->{path}->as_string); |
|
|
1284 | $cf::MAP{$self->path} = $self; |
|
|
1285 | |
|
|
1286 | $self->save; |
|
|
1287 | } |
|
|
1288 | |
1267 | sub reset { |
1289 | sub reset { |
1268 | my ($self) = @_; |
1290 | my ($self) = @_; |
1269 | |
1291 | |
1270 | return if $self->players; |
1292 | return if $self->players; |
1271 | return if $self->{path}{user_rel};#d# |
1293 | return if $self->{path}{user_rel};#d# |
… | |
… | |
1276 | |
1298 | |
1277 | $_->clear_links_to ($self) for values %cf::MAP; |
1299 | $_->clear_links_to ($self) for values %cf::MAP; |
1278 | |
1300 | |
1279 | $self->unlink_save; |
1301 | $self->unlink_save; |
1280 | $self->destroy; |
1302 | $self->destroy; |
|
|
1303 | } |
|
|
1304 | |
|
|
1305 | my $nuke_counter = "aaaa"; |
|
|
1306 | |
|
|
1307 | sub nuke { |
|
|
1308 | my ($self) = @_; |
|
|
1309 | |
|
|
1310 | $self->{deny_save} = 1; |
|
|
1311 | $self->reset_timeout (1); |
|
|
1312 | $self->rename ("{nuke}/" . ($nuke_counter++)); |
|
|
1313 | $self->reset; # polite request, might not happen |
1281 | } |
1314 | } |
1282 | |
1315 | |
1283 | sub customise_for { |
1316 | sub customise_for { |
1284 | my ($map, $ob) = @_; |
1317 | my ($map, $ob) = @_; |
1285 | |
1318 | |
… | |
… | |
1350 | (ref $cf::CFG{"may_$access"} |
1383 | (ref $cf::CFG{"may_$access"} |
1351 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
1384 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
1352 | : $cf::CFG{"may_$access"}) |
1385 | : $cf::CFG{"may_$access"}) |
1353 | } |
1386 | } |
1354 | |
1387 | |
|
|
1388 | =item $player_object->enter_link |
|
|
1389 | |
|
|
1390 | Freezes the player and moves him/her to a special map (C<{link}>). |
|
|
1391 | |
|
|
1392 | The player should be reaosnably safe there for short amounts of time. You |
|
|
1393 | I<MUST> call C<leave_link> as soon as possible, though. |
|
|
1394 | |
|
|
1395 | =item $player_object->leave_link ($map, $x, $y) |
|
|
1396 | |
|
|
1397 | Moves the player out of the specila link map onto the given map. If the |
|
|
1398 | map is not valid (or omitted), the player will be moved back to the |
|
|
1399 | location he/she was before the call to C<enter_link>, or, if that fails, |
|
|
1400 | to the emergency map position. |
|
|
1401 | |
|
|
1402 | Might block. |
|
|
1403 | |
|
|
1404 | =cut |
|
|
1405 | |
1355 | sub cf::object::player::enter_link { |
1406 | sub cf::object::player::enter_link { |
1356 | my ($self) = @_; |
1407 | my ($self) = @_; |
1357 | |
1408 | |
1358 | return if $self->map == $LINK_MAP; |
1409 | return if $self->map == $LINK_MAP; |
1359 | |
1410 | |
… | |
… | |
1368 | my ($self, $map, $x, $y) = @_; |
1419 | my ($self, $map, $x, $y) = @_; |
1369 | |
1420 | |
1370 | my $link_pos = delete $self->{_link_pos}; |
1421 | my $link_pos = delete $self->{_link_pos}; |
1371 | |
1422 | |
1372 | unless ($map) { |
1423 | unless ($map) { |
1373 | $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
1374 | |
|
|
1375 | # restore original map position |
1424 | # restore original map position |
1376 | ($map, $x, $y) = @{ $link_pos || [] }; |
1425 | ($map, $x, $y) = @{ $link_pos || [] }; |
1377 | $map = cf::map::find_map $map; |
1426 | $map = cf::map::find_map $map; |
1378 | |
1427 | |
1379 | unless ($map) { |
1428 | unless ($map) { |
… | |
… | |
1411 | my $map = cf::map::find_map $path->as_string; |
1460 | my $map = cf::map::find_map $path->as_string; |
1412 | $map = $map->customise_for ($self) if $map; |
1461 | $map = $map->customise_for ($self) if $map; |
1413 | |
1462 | |
1414 | warn "entering ", $map->path, " at ($x, $y)\n" |
1463 | warn "entering ", $map->path, " at ($x, $y)\n" |
1415 | if $map; |
1464 | if $map; |
|
|
1465 | |
|
|
1466 | $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1416 | |
1467 | |
1417 | $self->leave_link ($map, $x, $y); |
1468 | $self->leave_link ($map, $x, $y); |
1418 | })->prio (1); |
1469 | })->prio (1); |
1419 | } |
1470 | } |
1420 | |
1471 | |
… | |
… | |
1831 | } |
1882 | } |
1832 | |
1883 | |
1833 | sub main { |
1884 | sub main { |
1834 | # we must not ever block the main coroutine |
1885 | # we must not ever block the main coroutine |
1835 | local $Coro::idle = sub { |
1886 | local $Coro::idle = sub { |
1836 | Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# |
1887 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
1837 | (Coro::unblock_sub { |
1888 | (Coro::unblock_sub { |
1838 | Event::one_event; |
1889 | Event::one_event; |
1839 | })->(); |
1890 | })->(); |
1840 | }; |
1891 | }; |
1841 | |
1892 | |