… | |
… | |
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"; |
… | |
… | |
1203 | |
1210 | |
1204 | # do it the slow way |
1211 | # do it the slow way |
1205 | my $map = try_load_header $path->save_path; |
1212 | my $map = try_load_header $path->save_path; |
1206 | |
1213 | |
1207 | if ($map) { |
1214 | if ($map) { |
|
|
1215 | $map->last_access ((delete $map->{last_access}) |
|
|
1216 | || $cf::RUNTIME); #d# |
1208 | # safety |
1217 | # safety |
1209 | $map->{instantiate_time} = $cf::RUNTIME |
1218 | $map->{instantiate_time} = $cf::RUNTIME |
1210 | if $map->{instantiate_time} > $cf::RUNTIME; |
1219 | if $map->{instantiate_time} > $cf::RUNTIME; |
1211 | } else { |
1220 | } else { |
1212 | if (my $rmp = $path->random_map_params) { |
1221 | if (my $rmp = $path->random_map_params) { |
… | |
… | |
1217 | |
1226 | |
1218 | $map or return; |
1227 | $map or return; |
1219 | |
1228 | |
1220 | $map->{load_original} = 1; |
1229 | $map->{load_original} = 1; |
1221 | $map->{instantiate_time} = $cf::RUNTIME; |
1230 | $map->{instantiate_time} = $cf::RUNTIME; |
|
|
1231 | $map->last_access ($cf::RUNTIME); |
1222 | $map->instantiate; |
1232 | $map->instantiate; |
1223 | |
1233 | |
1224 | # per-player maps become, after loading, normal maps |
1234 | # per-player maps become, after loading, normal maps |
1225 | $map->per_player (0) if $path->{user_rel}; |
1235 | $map->per_player (0) if $path->{user_rel}; |
1226 | } |
1236 | } |
1227 | |
1237 | |
1228 | $map->path ($key); |
1238 | $map->path ($key); |
1229 | $map->{path} = $path; |
1239 | $map->{path} = $path; |
1230 | $map->{last_save} = $cf::RUNTIME; |
1240 | $map->{last_save} = $cf::RUNTIME; |
1231 | $map->last_access ($cf::RUNTIME); |
|
|
1232 | |
1241 | |
1233 | if ($map->should_reset) { |
1242 | if ($map->should_reset) { |
1234 | $map->reset; |
1243 | $map->reset; |
1235 | undef $guard; |
1244 | undef $guard; |
1236 | $map = find_map $path |
1245 | $map = find $path |
1237 | or return; |
1246 | or return; |
1238 | } |
1247 | } |
1239 | |
1248 | |
1240 | $cf::MAP{$key} = $map |
1249 | $cf::MAP{$key} = $map |
1241 | } |
1250 | } |
… | |
… | |
1283 | } |
1292 | } |
1284 | |
1293 | |
1285 | $self->in_memory (cf::MAP_IN_MEMORY); |
1294 | $self->in_memory (cf::MAP_IN_MEMORY); |
1286 | } |
1295 | } |
1287 | |
1296 | |
1288 | sub load_map_sync { |
1297 | sub find_sync { |
1289 | my ($path, $origin) = @_; |
1298 | my ($path, $origin) = @_; |
1290 | |
1299 | |
1291 | #warn "load_map_sync<$path, $origin>\n";#d# |
1300 | cf::sync_job { cf::map::find $path, $origin } |
|
|
1301 | } |
1292 | |
1302 | |
1293 | cf::sync_job { |
1303 | sub do_load_sync { |
1294 | my $map = cf::map::find_map $path, $origin |
1304 | my ($map) = @_; |
1295 | or return; |
1305 | |
1296 | $map->load; |
1306 | cf::sync_job { $map->load }; |
1297 | $map |
|
|
1298 | } |
|
|
1299 | } |
1307 | } |
1300 | |
1308 | |
1301 | sub save { |
1309 | sub save { |
1302 | my ($self) = @_; |
1310 | my ($self) = @_; |
1303 | |
1311 | |
… | |
… | |
1309 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
1317 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
1310 | |
1318 | |
1311 | $self->{load_path} = $save; |
1319 | $self->{load_path} = $save; |
1312 | |
1320 | |
1313 | return if $self->{deny_save}; |
1321 | return if $self->{deny_save}; |
|
|
1322 | |
|
|
1323 | local $self->{last_access} = $self->last_access;#d# |
1314 | |
1324 | |
1315 | if ($uniq) { |
1325 | if ($uniq) { |
1316 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1326 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1317 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1327 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1318 | } else { |
1328 | } else { |
… | |
… | |
1403 | |
1413 | |
1404 | sub customise_for { |
1414 | sub customise_for { |
1405 | my ($map, $ob) = @_; |
1415 | my ($map, $ob) = @_; |
1406 | |
1416 | |
1407 | if ($map->per_player) { |
1417 | if ($map->per_player) { |
1408 | return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; |
1418 | return cf::map::find "~" . $ob->name . "/" . $map->{path}{path}; |
1409 | } |
1419 | } |
1410 | |
1420 | |
1411 | $map |
1421 | $map |
1412 | } |
1422 | } |
1413 | |
1423 | |
1414 | sub emergency_save { |
1424 | sub emergency_save { |
1415 | local $cf::FREEZE = 1; |
1425 | my $freeze_guard = cf::freeze_mainloop; |
1416 | |
1426 | |
1417 | warn "enter emergency map save\n"; |
1427 | warn "enter emergency map save\n"; |
1418 | |
1428 | |
1419 | cf::sync_job { |
1429 | cf::sync_job { |
1420 | warn "begin emergency map save\n"; |
1430 | warn "begin emergency map save\n"; |
… | |
… | |
1510 | my $link_pos = delete $self->{_link_pos}; |
1520 | my $link_pos = delete $self->{_link_pos}; |
1511 | |
1521 | |
1512 | unless ($map) { |
1522 | unless ($map) { |
1513 | # restore original map position |
1523 | # restore original map position |
1514 | ($map, $x, $y) = @{ $link_pos || [] }; |
1524 | ($map, $x, $y) = @{ $link_pos || [] }; |
1515 | $map = cf::map::find_map $map; |
1525 | $map = cf::map::find $map; |
1516 | |
1526 | |
1517 | unless ($map) { |
1527 | unless ($map) { |
1518 | ($map, $x, $y) = @$EMERGENCY_POSITION; |
1528 | ($map, $x, $y) = @$EMERGENCY_POSITION; |
1519 | $map = cf::map::find_map $map |
1529 | $map = cf::map::find $map |
1520 | or die "FATAL: cannot load emergency map\n"; |
1530 | or die "FATAL: cannot load emergency map\n"; |
1521 | } |
1531 | } |
1522 | } |
1532 | } |
1523 | |
1533 | |
1524 | ($x, $y) = (-1, -1) |
1534 | ($x, $y) = (-1, -1) |
… | |
… | |
1572 | $self->enter_link; |
1582 | $self->enter_link; |
1573 | |
1583 | |
1574 | (cf::async { |
1584 | (cf::async { |
1575 | $path = new cf::path $path; |
1585 | $path = new cf::path $path; |
1576 | |
1586 | |
1577 | my $map = cf::map::find_map $path->as_string; |
1587 | my $map = cf::map::find $path->as_string; |
1578 | $map = $map->customise_for ($self) if $map; |
1588 | $map = $map->customise_for ($self) if $map; |
1579 | |
1589 | |
1580 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1590 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1581 | # if $map; |
1591 | # if $map; |
1582 | |
1592 | |
… | |
… | |
1642 | return unless $self->type == cf::PLAYER; |
1652 | return unless $self->type == cf::PLAYER; |
1643 | |
1653 | |
1644 | $self->enter_link; |
1654 | $self->enter_link; |
1645 | |
1655 | |
1646 | (cf::async { |
1656 | (cf::async { |
|
|
1657 | $self->deactivate_recursive; # just to be sure |
1647 | unless (eval { |
1658 | unless (eval { |
1648 | prepare_random_map $exit |
1659 | prepare_random_map $exit |
1649 | if $exit->slaying eq "/!"; |
1660 | if $exit->slaying eq "/!"; |
1650 | |
1661 | |
1651 | 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; |
… | |
… | |
2026 | return; |
2037 | return; |
2027 | } |
2038 | } |
2028 | |
2039 | |
2029 | warn "reloading..."; |
2040 | warn "reloading..."; |
2030 | |
2041 | |
2031 | local $FREEZE = 1; |
2042 | my $guard = freeze_mainloop; |
2032 | cf::emergency_save; |
2043 | cf::emergency_save; |
2033 | |
2044 | |
2034 | eval { |
2045 | eval { |
2035 | # 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 |
2036 | |
2047 | |
… | |
… | |
2172 | reentrant => 0, |
2183 | reentrant => 0, |
2173 | prio => 0, |
2184 | prio => 0, |
2174 | at => $NEXT_TICK || $TICK, |
2185 | at => $NEXT_TICK || $TICK, |
2175 | data => WF_AUTOCANCEL, |
2186 | data => WF_AUTOCANCEL, |
2176 | cb => sub { |
2187 | cb => sub { |
2177 | unless ($FREEZE) { |
|
|
2178 | cf::server_tick; # one server iteration |
2188 | cf::server_tick; # one server iteration |
2179 | $RUNTIME += $TICK; |
2189 | $RUNTIME += $TICK; |
2180 | } |
|
|
2181 | |
|
|
2182 | $NEXT_TICK += $TICK; |
2190 | $NEXT_TICK += $TICK; |
2183 | |
2191 | |
2184 | # 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 |
2185 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2193 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2186 | |
2194 | |