… | |
… | |
49 | our $UPTIME; $UPTIME ||= time; |
49 | our $UPTIME; $UPTIME ||= time; |
50 | our $RUNTIME; |
50 | our $RUNTIME; |
51 | |
51 | |
52 | our %MAP; # all maps |
52 | our %MAP; # all maps |
53 | our $LINK_MAP; # the special {link} map |
53 | our $LINK_MAP; # the special {link} map |
54 | our $FREEZE; |
|
|
55 | our $RANDOM_MAPS = cf::localdir . "/random"; |
54 | our $RANDOM_MAPS = cf::localdir . "/random"; |
56 | our %EXT_CORO; |
55 | our %EXT_CORO; |
57 | |
56 | |
58 | binmode STDOUT; |
57 | binmode STDOUT; |
59 | binmode STDERR; |
58 | binmode STDERR; |
… | |
… | |
213 | Wait until the given lock is available and then acquires it and returns |
212 | Wait until the given lock is available and then acquires it and returns |
214 | a guard object. If the guard object gets destroyed (goes out of scope, |
213 | a guard object. If the guard object gets destroyed (goes out of scope, |
215 | for example when the coroutine gets canceled), the lock is automatically |
214 | for example when the coroutine gets canceled), the lock is automatically |
216 | returned. |
215 | returned. |
217 | |
216 | |
218 | Lock names should begin with a unique identifier (for example, find_map |
217 | Lock names should begin with a unique identifier (for example, cf::map::find |
219 | uses map_find and load_map uses map_load). |
218 | uses map_find and cf::map::load uses map_load). |
220 | |
219 | |
221 | =cut |
220 | =cut |
222 | |
221 | |
223 | our %LOCK; |
222 | our %LOCK; |
224 | |
223 | |
… | |
… | |
258 | |
257 | |
259 | Coro::async { |
258 | Coro::async { |
260 | eval { $cb->() }; |
259 | eval { $cb->() }; |
261 | warn $@ if $@; |
260 | warn $@ if $@; |
262 | } |
261 | } |
|
|
262 | } |
|
|
263 | |
|
|
264 | sub freeze_mainloop { |
|
|
265 | return unless $TICK_WATCHER->is_active; |
|
|
266 | |
|
|
267 | my $guard = guard { $TICK_WATCHER->start }; |
|
|
268 | $TICK_WATCHER->stop; |
|
|
269 | $guard |
263 | } |
270 | } |
264 | |
271 | |
265 | =item cf::sync_job { BLOCK } |
272 | =item cf::sync_job { BLOCK } |
266 | |
273 | |
267 | The design of crossfire+ requires that the main coro ($Coro::main) is |
274 | The design of crossfire+ requires that the main coro ($Coro::main) is |
… | |
… | |
282 | # this is the main coro, too bad, we have to block |
289 | # this is the main coro, too bad, we have to block |
283 | # till the operation succeeds, freezing the server :/ |
290 | # till the operation succeeds, freezing the server :/ |
284 | |
291 | |
285 | # TODO: use suspend/resume instead |
292 | # TODO: use suspend/resume instead |
286 | # (but this is cancel-safe) |
293 | # (but this is cancel-safe) |
287 | local $FREEZE = 1; |
294 | my $freeze_guard = freeze_mainloop; |
288 | |
295 | |
289 | my $busy = 1; |
296 | my $busy = 1; |
290 | my @res; |
297 | my @res; |
291 | |
298 | |
292 | (Coro::async { |
299 | (Coro::async { |
… | |
… | |
1136 | package cf::map; |
1143 | package cf::map; |
1137 | |
1144 | |
1138 | use Fcntl; |
1145 | use Fcntl; |
1139 | use Coro::AIO; |
1146 | use Coro::AIO; |
1140 | |
1147 | |
1141 | our $MAX_RESET = 7200; |
1148 | our $MAX_RESET = 3600; |
1142 | our $DEFAULT_RESET = 3600; |
1149 | our $DEFAULT_RESET = 3000; |
1143 | |
1150 | |
1144 | sub generate_random_map { |
1151 | sub generate_random_map { |
1145 | my ($path, $rmp) = @_; |
1152 | my ($path, $rmp) = @_; |
1146 | |
1153 | |
1147 | # mit "rum" bekleckern, nicht |
1154 | # mit "rum" bekleckern, nicht |
… | |
… | |
1185 | $map->{load_path} = $path; |
1192 | $map->{load_path} = $path; |
1186 | |
1193 | |
1187 | $map |
1194 | $map |
1188 | } |
1195 | } |
1189 | |
1196 | |
1190 | sub find_map; |
1197 | sub find; |
1191 | sub find_map { |
1198 | sub find { |
1192 | my ($path, $origin) = @_; |
1199 | my ($path, $origin) = @_; |
1193 | |
1200 | |
1194 | #warn "find_map<$path,$origin>\n";#d# |
1201 | #warn "find<$path,$origin>\n";#d# |
1195 | |
1202 | |
1196 | $path = new cf::path $path, $origin && $origin->path; |
1203 | $path = new cf::path $path, $origin && $origin->path; |
1197 | my $key = $path->as_string; |
1204 | my $key = $path->as_string; |
1198 | |
1205 | |
1199 | cf::lock_wait "map_find:$key"; |
1206 | cf::lock_wait "map_find:$key"; |
… | |
… | |
1233 | $map->{last_save} = $cf::RUNTIME; |
1240 | $map->{last_save} = $cf::RUNTIME; |
1234 | |
1241 | |
1235 | if ($map->should_reset) { |
1242 | if ($map->should_reset) { |
1236 | $map->reset; |
1243 | $map->reset; |
1237 | undef $guard; |
1244 | undef $guard; |
1238 | $map = find_map $path |
1245 | $map = find $path |
1239 | or return; |
1246 | or return; |
1240 | } |
1247 | } |
1241 | |
1248 | |
1242 | $cf::MAP{$key} = $map |
1249 | $cf::MAP{$key} = $map |
1243 | } |
1250 | } |
… | |
… | |
1285 | } |
1292 | } |
1286 | |
1293 | |
1287 | $self->in_memory (cf::MAP_IN_MEMORY); |
1294 | $self->in_memory (cf::MAP_IN_MEMORY); |
1288 | } |
1295 | } |
1289 | |
1296 | |
1290 | sub load_map_sync { |
1297 | sub find_sync { |
1291 | my ($path, $origin) = @_; |
1298 | my ($path, $origin) = @_; |
1292 | |
1299 | |
1293 | #warn "load_map_sync<$path, $origin>\n";#d# |
1300 | cf::sync_job { cf::map::find $path, $origin } |
|
|
1301 | } |
1294 | |
1302 | |
1295 | cf::sync_job { |
1303 | sub do_load_sync { |
1296 | my $map = cf::map::find_map $path, $origin |
1304 | my ($map) = @_; |
1297 | or return; |
1305 | |
1298 | $map->load; |
1306 | cf::sync_job { $map->load }; |
1299 | $map |
|
|
1300 | } |
|
|
1301 | } |
1307 | } |
1302 | |
1308 | |
1303 | sub save { |
1309 | sub save { |
1304 | my ($self) = @_; |
1310 | my ($self) = @_; |
1305 | |
1311 | |
… | |
… | |
1407 | |
1413 | |
1408 | sub customise_for { |
1414 | sub customise_for { |
1409 | my ($map, $ob) = @_; |
1415 | my ($map, $ob) = @_; |
1410 | |
1416 | |
1411 | if ($map->per_player) { |
1417 | if ($map->per_player) { |
1412 | return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; |
1418 | return cf::map::find "~" . $ob->name . "/" . $map->{path}{path}; |
1413 | } |
1419 | } |
1414 | |
1420 | |
1415 | $map |
1421 | $map |
1416 | } |
1422 | } |
1417 | |
1423 | |
1418 | sub emergency_save { |
1424 | sub emergency_save { |
1419 | local $cf::FREEZE = 1; |
1425 | my $freeze_guard = cf::freeze_mainloop; |
1420 | |
1426 | |
1421 | warn "enter emergency map save\n"; |
1427 | warn "enter emergency map save\n"; |
1422 | |
1428 | |
1423 | cf::sync_job { |
1429 | cf::sync_job { |
1424 | warn "begin emergency map save\n"; |
1430 | warn "begin emergency map save\n"; |
… | |
… | |
1514 | my $link_pos = delete $self->{_link_pos}; |
1520 | my $link_pos = delete $self->{_link_pos}; |
1515 | |
1521 | |
1516 | unless ($map) { |
1522 | unless ($map) { |
1517 | # restore original map position |
1523 | # restore original map position |
1518 | ($map, $x, $y) = @{ $link_pos || [] }; |
1524 | ($map, $x, $y) = @{ $link_pos || [] }; |
1519 | $map = cf::map::find_map $map; |
1525 | $map = cf::map::find $map; |
1520 | |
1526 | |
1521 | unless ($map) { |
1527 | unless ($map) { |
1522 | ($map, $x, $y) = @$EMERGENCY_POSITION; |
1528 | ($map, $x, $y) = @$EMERGENCY_POSITION; |
1523 | $map = cf::map::find_map $map |
1529 | $map = cf::map::find $map |
1524 | or die "FATAL: cannot load emergency map\n"; |
1530 | or die "FATAL: cannot load emergency map\n"; |
1525 | } |
1531 | } |
1526 | } |
1532 | } |
1527 | |
1533 | |
1528 | ($x, $y) = (-1, -1) |
1534 | ($x, $y) = (-1, -1) |
… | |
… | |
1576 | $self->enter_link; |
1582 | $self->enter_link; |
1577 | |
1583 | |
1578 | (cf::async { |
1584 | (cf::async { |
1579 | $path = new cf::path $path; |
1585 | $path = new cf::path $path; |
1580 | |
1586 | |
1581 | my $map = cf::map::find_map $path->as_string; |
1587 | my $map = cf::map::find $path->as_string; |
1582 | $map = $map->customise_for ($self) if $map; |
1588 | $map = $map->customise_for ($self) if $map; |
1583 | |
1589 | |
1584 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1590 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1585 | # if $map; |
1591 | # if $map; |
1586 | |
1592 | |
… | |
… | |
1646 | return unless $self->type == cf::PLAYER; |
1652 | return unless $self->type == cf::PLAYER; |
1647 | |
1653 | |
1648 | $self->enter_link; |
1654 | $self->enter_link; |
1649 | |
1655 | |
1650 | (cf::async { |
1656 | (cf::async { |
|
|
1657 | $self->deactivate_recursive; # just to be sure |
1651 | unless (eval { |
1658 | unless (eval { |
1652 | prepare_random_map $exit |
1659 | prepare_random_map $exit |
1653 | if $exit->slaying eq "/!"; |
1660 | if $exit->slaying eq "/!"; |
1654 | |
1661 | |
1655 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1662 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
… | |
… | |
2030 | return; |
2037 | return; |
2031 | } |
2038 | } |
2032 | |
2039 | |
2033 | warn "reloading..."; |
2040 | warn "reloading..."; |
2034 | |
2041 | |
2035 | local $FREEZE = 1; |
2042 | my $guard = freeze_mainloop; |
2036 | cf::emergency_save; |
2043 | cf::emergency_save; |
2037 | |
2044 | |
2038 | eval { |
2045 | eval { |
2039 | # if anything goes wrong in here, we should simply crash as we already saved |
2046 | # if anything goes wrong in here, we should simply crash as we already saved |
2040 | |
2047 | |
… | |
… | |
2176 | reentrant => 0, |
2183 | reentrant => 0, |
2177 | prio => 0, |
2184 | prio => 0, |
2178 | at => $NEXT_TICK || $TICK, |
2185 | at => $NEXT_TICK || $TICK, |
2179 | data => WF_AUTOCANCEL, |
2186 | data => WF_AUTOCANCEL, |
2180 | cb => sub { |
2187 | cb => sub { |
2181 | unless ($FREEZE) { |
|
|
2182 | cf::server_tick; # one server iteration |
2188 | cf::server_tick; # one server iteration |
2183 | $RUNTIME += $TICK; |
2189 | $RUNTIME += $TICK; |
2184 | } |
|
|
2185 | |
|
|
2186 | $NEXT_TICK += $TICK; |
2190 | $NEXT_TICK += $TICK; |
2187 | |
2191 | |
2188 | # if we are delayed by four ticks or more, skip them all |
2192 | # if we are delayed by four ticks or more, skip them all |
2189 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2193 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2190 | |
2194 | |