… | |
… | |
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.3; |
13 | use Coro 3.3 (); |
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; |
… | |
… | |
71 | mkdir cf::localdir . "/" . cf::uniquedir; |
71 | mkdir cf::localdir . "/" . cf::uniquedir; |
72 | mkdir $RANDOM_MAPS; |
72 | mkdir $RANDOM_MAPS; |
73 | |
73 | |
74 | # a special map that is always available |
74 | # a special map that is always available |
75 | our $LINK_MAP; |
75 | our $LINK_MAP; |
76 | |
76 | our $EMERGENCY_POSITION; |
77 | our $EMERGENCY_POSITION = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
|
|
78 | |
77 | |
79 | ############################################################################# |
78 | ############################################################################# |
80 | |
79 | |
81 | =head2 GLOBAL VARIABLES |
80 | =head2 GLOBAL VARIABLES |
82 | |
81 | |
… | |
… | |
181 | sub to_json($) { |
180 | sub to_json($) { |
182 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
181 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
183 | JSON::Syck::Dump $_[0] |
182 | JSON::Syck::Dump $_[0] |
184 | } |
183 | } |
185 | |
184 | |
|
|
185 | =item my $guard = cf::guard { BLOCK } |
|
|
186 | |
|
|
187 | Run the given callback when the guard object gets destroyed (useful for |
|
|
188 | coroutine cancellations). |
|
|
189 | |
|
|
190 | You can call C<< ->cancel >> on the guard object to stop the block from |
|
|
191 | being executed. |
|
|
192 | |
|
|
193 | =cut |
|
|
194 | |
|
|
195 | sub guard(&) { |
|
|
196 | bless \(my $cb = $_[0]), cf::guard::; |
|
|
197 | } |
|
|
198 | |
|
|
199 | sub cf::guard::cancel { |
|
|
200 | ${$_[0]} = sub { }; |
|
|
201 | } |
|
|
202 | |
|
|
203 | sub cf::guard::DESTROY { |
|
|
204 | ${$_[0]}->(); |
|
|
205 | } |
|
|
206 | |
|
|
207 | =item cf::lock_wait $string |
|
|
208 | |
|
|
209 | Wait until the given lock is available. See cf::lock_acquire. |
|
|
210 | |
|
|
211 | =item my $lock = cf::lock_acquire $string |
|
|
212 | |
|
|
213 | 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, |
|
|
215 | for example when the coroutine gets canceled), the lock is automatically |
|
|
216 | returned. |
|
|
217 | |
|
|
218 | Lock names should begin with a unique identifier (for example, find_map |
|
|
219 | uses map_find and load_map uses map_load). |
|
|
220 | |
|
|
221 | =cut |
|
|
222 | |
|
|
223 | our %LOCK; |
|
|
224 | |
|
|
225 | sub lock_wait($) { |
|
|
226 | my ($key) = @_; |
|
|
227 | |
|
|
228 | # wait for lock, if any |
|
|
229 | while ($LOCK{$key}) { |
|
|
230 | push @{ $LOCK{$key} }, $Coro::current; |
|
|
231 | Coro::schedule; |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | sub lock_acquire($) { |
|
|
236 | my ($key) = @_; |
|
|
237 | |
|
|
238 | # wait, to be sure we are not locked |
|
|
239 | lock_wait $key; |
|
|
240 | |
|
|
241 | $LOCK{$key} = []; |
|
|
242 | |
|
|
243 | cf::guard { |
|
|
244 | # wake up all waiters, to be on the safe side |
|
|
245 | $_->ready for @{ delete $LOCK{$key} }; |
|
|
246 | } |
|
|
247 | } |
|
|
248 | |
|
|
249 | =item cf::async { BLOCK } |
|
|
250 | |
|
|
251 | Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the |
|
|
252 | error instead of exiting the server in case of a problem. |
|
|
253 | |
|
|
254 | =cut |
|
|
255 | |
|
|
256 | sub async(&) { |
|
|
257 | my ($cb) = @_; |
|
|
258 | |
|
|
259 | Coro::async { |
|
|
260 | eval { $cb->() }; |
|
|
261 | warn $@ if $@; |
|
|
262 | } |
|
|
263 | } |
|
|
264 | |
186 | =item cf::sync_job { BLOCK } |
265 | =item cf::sync_job { BLOCK } |
187 | |
266 | |
188 | The design of crossfire+ requires that the main coro ($Coro::main) is |
267 | The design of crossfire+ requires that the main coro ($Coro::main) is |
189 | always able to handle events or runnable, as crossfire+ is only partly |
268 | always able to handle events or runnable, as crossfire+ is only partly |
190 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
269 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
… | |
… | |
237 | =cut |
316 | =cut |
238 | |
317 | |
239 | sub coro(&) { |
318 | sub coro(&) { |
240 | my $cb = shift; |
319 | my $cb = shift; |
241 | |
320 | |
242 | my $coro; $coro = async { |
321 | my $coro = &cf::async ($cb); |
243 | eval { |
|
|
244 | $cb->(); |
|
|
245 | }; |
|
|
246 | warn $@ if $@; |
|
|
247 | }; |
|
|
248 | |
322 | |
249 | $coro->on_destroy (sub { |
323 | $coro->on_destroy (sub { |
250 | delete $EXT_CORO{$coro+0}; |
324 | delete $EXT_CORO{$coro+0}; |
251 | }); |
325 | }); |
252 | $EXT_CORO{$coro+0} = $coro; |
326 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
289 | |
363 | |
290 | $path = $path->as_string if ref $path; |
364 | $path = $path->as_string if ref $path; |
291 | |
365 | |
292 | my $self = bless { }, $class; |
366 | my $self = bless { }, $class; |
293 | |
367 | |
|
|
368 | # {... are special paths that are not touched |
|
|
369 | # ?xxx/... are special absolute paths |
|
|
370 | # ?random/... random maps |
|
|
371 | # /! non-realised random map exit |
|
|
372 | # /... normal maps |
|
|
373 | # ~/... per-player maps without a specific player (DO NOT USE) |
|
|
374 | # ~user/... per-player map of a specific user |
|
|
375 | |
|
|
376 | if ($path =~ /^{/) { |
|
|
377 | # fine as it is |
294 | if ($path =~ s{^\?random/}{}) { |
378 | } elsif ($path =~ s{^\?random/}{}) { |
295 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
379 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
296 | $self->{random} = cf::from_json $data; |
380 | $self->{random} = cf::from_json $data; |
297 | } else { |
381 | } else { |
298 | if ($path =~ s{^~([^/]+)?}{}) { |
382 | if ($path =~ s{^~([^/]+)?}{}) { |
299 | $self->{user_rel} = 1; |
383 | $self->{user_rel} = 1; |
… | |
… | |
804 | or return; |
888 | or return; |
805 | |
889 | |
806 | unless (aio_stat "$filename.pst") { |
890 | unless (aio_stat "$filename.pst") { |
807 | (aio_load "$filename.pst", $av) >= 0 |
891 | (aio_load "$filename.pst", $av) >= 0 |
808 | or return; |
892 | or return; |
809 | $av = eval { (Storable::thaw <$av>)->{objs} }; |
893 | $av = eval { (Storable::thaw $av)->{objs} }; |
810 | } |
894 | } |
811 | |
895 | |
|
|
896 | warn sprintf "loading %s (%d)\n", |
|
|
897 | $filename, length $data, scalar @{$av || []};#d# |
812 | return ($data, $av); |
898 | return ($data, $av); |
813 | } |
899 | } |
814 | |
900 | |
815 | ############################################################################# |
901 | ############################################################################# |
816 | # command handling &c |
902 | # command handling &c |
… | |
… | |
1077 | # and all this just because we cannot iterate over |
1163 | # and all this just because we cannot iterate over |
1078 | # all maps in C++... |
1164 | # all maps in C++... |
1079 | sub change_all_map_light { |
1165 | sub change_all_map_light { |
1080 | my ($change) = @_; |
1166 | my ($change) = @_; |
1081 | |
1167 | |
1082 | $_->change_map_light ($change) for values %cf::MAP; |
1168 | $_->change_map_light ($change) |
|
|
1169 | for grep $_->outdoor, values %cf::MAP; |
1083 | } |
1170 | } |
1084 | |
1171 | |
1085 | sub try_load_header($) { |
1172 | sub try_load_header($) { |
1086 | my ($path) = @_; |
1173 | my ($path) = @_; |
1087 | |
1174 | |
… | |
… | |
1098 | $map->{load_path} = $path; |
1185 | $map->{load_path} = $path; |
1099 | |
1186 | |
1100 | $map |
1187 | $map |
1101 | } |
1188 | } |
1102 | |
1189 | |
|
|
1190 | sub find_map; |
1103 | sub find_map { |
1191 | sub find_map { |
1104 | my ($path, $origin) = @_; |
1192 | my ($path, $origin) = @_; |
1105 | |
1193 | |
1106 | #warn "find_map<$path,$origin>\n";#d# |
1194 | #warn "find_map<$path,$origin>\n";#d# |
1107 | |
1195 | |
1108 | $path = new cf::path $path, $origin && $origin->path; |
1196 | $path = new cf::path $path, $origin && $origin->path; |
1109 | my $key = $path->as_string; |
1197 | my $key = $path->as_string; |
1110 | |
1198 | |
|
|
1199 | cf::lock_wait "map_find:$key"; |
|
|
1200 | |
1111 | $cf::MAP{$key} || do { |
1201 | $cf::MAP{$key} || do { |
|
|
1202 | my $guard = cf::lock_acquire "map_find:$key"; |
|
|
1203 | |
1112 | # do it the slow way |
1204 | # do it the slow way |
1113 | my $map = try_load_header $path->save_path; |
1205 | my $map = try_load_header $path->save_path; |
1114 | |
1206 | |
1115 | if ($map) { |
1207 | if ($map) { |
|
|
1208 | $map->last_access ((delete $map->{last_access}) |
|
|
1209 | || $cf::RUNTIME); #d# |
1116 | # safety |
1210 | # safety |
1117 | $map->{instantiate_time} = $cf::RUNTIME |
1211 | $map->{instantiate_time} = $cf::RUNTIME |
1118 | if $map->{instantiate_time} > $cf::RUNTIME; |
1212 | if $map->{instantiate_time} > $cf::RUNTIME; |
1119 | } else { |
1213 | } else { |
1120 | if (my $rmp = $path->random_map_params) { |
1214 | if (my $rmp = $path->random_map_params) { |
… | |
… | |
1125 | |
1219 | |
1126 | $map or return; |
1220 | $map or return; |
1127 | |
1221 | |
1128 | $map->{load_original} = 1; |
1222 | $map->{load_original} = 1; |
1129 | $map->{instantiate_time} = $cf::RUNTIME; |
1223 | $map->{instantiate_time} = $cf::RUNTIME; |
|
|
1224 | $map->last_access ($cf::RUNTIME); |
1130 | $map->instantiate; |
1225 | $map->instantiate; |
1131 | |
1226 | |
1132 | # per-player maps become, after loading, normal maps |
1227 | # per-player maps become, after loading, normal maps |
1133 | $map->per_player (0) if $path->{user_rel}; |
1228 | $map->per_player (0) if $path->{user_rel}; |
1134 | } |
1229 | } |
1135 | |
1230 | |
1136 | $map->path ($key); |
1231 | $map->path ($key); |
1137 | $map->{path} = $path; |
1232 | $map->{path} = $path; |
1138 | $map->last_access ($cf::RUNTIME); |
1233 | $map->{last_save} = $cf::RUNTIME; |
1139 | |
1234 | |
1140 | if ($map->should_reset) { |
1235 | if ($map->should_reset) { |
1141 | $map->reset; |
1236 | $map->reset; |
|
|
1237 | undef $guard; |
1142 | $map = find_map $path; |
1238 | $map = find_map $path |
|
|
1239 | or return; |
1143 | } |
1240 | } |
1144 | |
1241 | |
1145 | $cf::MAP{$key} = $map |
1242 | $cf::MAP{$key} = $map |
1146 | } |
1243 | } |
1147 | } |
1244 | } |
1148 | |
1245 | |
1149 | sub load { |
1246 | sub load { |
1150 | my ($self) = @_; |
1247 | my ($self) = @_; |
1151 | |
1248 | |
|
|
1249 | my $path = $self->{path}; |
|
|
1250 | my $guard = cf::lock_acquire "map_load:" . $path->as_string; |
|
|
1251 | |
1152 | return if $self->in_memory != cf::MAP_SWAPPED; |
1252 | return if $self->in_memory != cf::MAP_SWAPPED; |
1153 | |
1253 | |
1154 | $self->in_memory (cf::MAP_LOADING); |
1254 | $self->in_memory (cf::MAP_LOADING); |
1155 | |
|
|
1156 | my $path = $self->{path}; |
|
|
1157 | |
1255 | |
1158 | $self->alloc; |
1256 | $self->alloc; |
1159 | $self->load_objects ($self->{load_path}, 1) |
1257 | $self->load_objects ($self->{load_path}, 1) |
1160 | or return; |
1258 | or return; |
1161 | |
1259 | |
… | |
… | |
1203 | } |
1301 | } |
1204 | |
1302 | |
1205 | sub save { |
1303 | sub save { |
1206 | my ($self) = @_; |
1304 | my ($self) = @_; |
1207 | |
1305 | |
|
|
1306 | $self->{last_save} = $cf::RUNTIME; |
|
|
1307 | |
|
|
1308 | return unless $self->dirty; |
|
|
1309 | |
1208 | my $save = $self->{path}->save_path; utf8::encode $save; |
1310 | my $save = $self->{path}->save_path; utf8::encode $save; |
1209 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
1311 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
1210 | |
1312 | |
1211 | $self->{last_save} = $cf::RUNTIME; |
|
|
1212 | |
|
|
1213 | return unless $self->dirty; |
|
|
1214 | |
|
|
1215 | $self->{load_path} = $save; |
1313 | $self->{load_path} = $save; |
1216 | |
1314 | |
1217 | return if $self->{deny_save}; |
1315 | return if $self->{deny_save}; |
|
|
1316 | |
|
|
1317 | local $self->{last_access} = $self->last_access;#d# |
1218 | |
1318 | |
1219 | if ($uniq) { |
1319 | if ($uniq) { |
1220 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1320 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1221 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1321 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1222 | } else { |
1322 | } else { |
… | |
… | |
1225 | } |
1325 | } |
1226 | |
1326 | |
1227 | sub swap_out { |
1327 | sub swap_out { |
1228 | my ($self) = @_; |
1328 | my ($self) = @_; |
1229 | |
1329 | |
|
|
1330 | # save first because save cedes |
|
|
1331 | $self->save; |
|
|
1332 | |
1230 | return if $self->players; |
1333 | return if $self->players; |
1231 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1334 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1232 | return if $self->{deny_save}; |
1335 | return if $self->{deny_save}; |
1233 | |
1336 | |
1234 | $self->save; |
|
|
1235 | $self->clear; |
1337 | $self->clear; |
1236 | $self->in_memory (cf::MAP_SWAPPED); |
1338 | $self->in_memory (cf::MAP_SWAPPED); |
1237 | } |
1339 | } |
1238 | |
1340 | |
1239 | sub reset_at { |
1341 | sub reset_at { |
1240 | my ($self) = @_; |
1342 | my ($self) = @_; |
1241 | |
1343 | |
1242 | # TODO: safety, remove and allow resettable per-player maps |
1344 | # TODO: safety, remove and allow resettable per-player maps |
1243 | return 1e100 if $self->{path}{user_rel}; |
1345 | return 1e99 if $self->{path}{user_rel}; |
1244 | return 1e100 if $self->{deny_reset}; |
1346 | return 1e99 if $self->{deny_reset}; |
1245 | |
1347 | |
1246 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1348 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1247 | my $to = $self->reset_timeout || $DEFAULT_RESET; |
1349 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
1248 | $to = $MAX_RESET if $to > $MAX_RESET; |
|
|
1249 | |
1350 | |
1250 | $time + $to |
1351 | $time + $to |
1251 | } |
1352 | } |
1252 | |
1353 | |
1253 | sub should_reset { |
1354 | sub should_reset { |
… | |
… | |
1262 | utf8::encode (my $save = $self->{path}->save_path); |
1363 | utf8::encode (my $save = $self->{path}->save_path); |
1263 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
1364 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
1264 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
1365 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
1265 | } |
1366 | } |
1266 | |
1367 | |
|
|
1368 | sub rename { |
|
|
1369 | my ($self, $new_path) = @_; |
|
|
1370 | |
|
|
1371 | $self->unlink_save; |
|
|
1372 | |
|
|
1373 | delete $cf::MAP{$self->path}; |
|
|
1374 | $self->{path} = new cf::path $new_path; |
|
|
1375 | $self->path ($self->{path}->as_string); |
|
|
1376 | $cf::MAP{$self->path} = $self; |
|
|
1377 | |
|
|
1378 | $self->save; |
|
|
1379 | } |
|
|
1380 | |
1267 | sub reset { |
1381 | sub reset { |
1268 | my ($self) = @_; |
1382 | my ($self) = @_; |
1269 | |
1383 | |
1270 | return if $self->players; |
1384 | return if $self->players; |
1271 | return if $self->{path}{user_rel};#d# |
1385 | return if $self->{path}{user_rel};#d# |
… | |
… | |
1276 | |
1390 | |
1277 | $_->clear_links_to ($self) for values %cf::MAP; |
1391 | $_->clear_links_to ($self) for values %cf::MAP; |
1278 | |
1392 | |
1279 | $self->unlink_save; |
1393 | $self->unlink_save; |
1280 | $self->destroy; |
1394 | $self->destroy; |
|
|
1395 | } |
|
|
1396 | |
|
|
1397 | my $nuke_counter = "aaaa"; |
|
|
1398 | |
|
|
1399 | sub nuke { |
|
|
1400 | my ($self) = @_; |
|
|
1401 | |
|
|
1402 | $self->{deny_save} = 1; |
|
|
1403 | $self->reset_timeout (1); |
|
|
1404 | $self->rename ("{nuke}/" . ($nuke_counter++)); |
|
|
1405 | $self->reset; # polite request, might not happen |
1281 | } |
1406 | } |
1282 | |
1407 | |
1283 | sub customise_for { |
1408 | sub customise_for { |
1284 | my ($map, $ob) = @_; |
1409 | my ($map, $ob) = @_; |
1285 | |
1410 | |
… | |
… | |
1350 | (ref $cf::CFG{"may_$access"} |
1475 | (ref $cf::CFG{"may_$access"} |
1351 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
1476 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
1352 | : $cf::CFG{"may_$access"}) |
1477 | : $cf::CFG{"may_$access"}) |
1353 | } |
1478 | } |
1354 | |
1479 | |
|
|
1480 | =item $player_object->enter_link |
|
|
1481 | |
|
|
1482 | Freezes the player and moves him/her to a special map (C<{link}>). |
|
|
1483 | |
|
|
1484 | The player should be reaosnably safe there for short amounts of time. You |
|
|
1485 | I<MUST> call C<leave_link> as soon as possible, though. |
|
|
1486 | |
|
|
1487 | =item $player_object->leave_link ($map, $x, $y) |
|
|
1488 | |
|
|
1489 | Moves the player out of the specila link map onto the given map. If the |
|
|
1490 | map is not valid (or omitted), the player will be moved back to the |
|
|
1491 | location he/she was before the call to C<enter_link>, or, if that fails, |
|
|
1492 | to the emergency map position. |
|
|
1493 | |
|
|
1494 | Might block. |
|
|
1495 | |
|
|
1496 | =cut |
|
|
1497 | |
1355 | sub cf::object::player::enter_link { |
1498 | sub cf::object::player::enter_link { |
1356 | my ($self) = @_; |
1499 | my ($self) = @_; |
1357 | |
1500 | |
|
|
1501 | $self->deactivate_recursive; |
|
|
1502 | |
1358 | return if $self->map == $LINK_MAP; |
1503 | return if $self->map == $LINK_MAP; |
1359 | |
1504 | |
1360 | $self->{_link_pos} = [$self->map->{path}, $self->x, $self->y] |
1505 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1361 | if $self->map; |
1506 | if $self->map; |
1362 | |
1507 | |
1363 | $self->enter_map ($LINK_MAP, 20, 20); |
1508 | $self->enter_map ($LINK_MAP, 20, 20); |
1364 | $self->deactivate_recursive; |
|
|
1365 | } |
1509 | } |
1366 | |
1510 | |
1367 | sub cf::object::player::leave_link { |
1511 | sub cf::object::player::leave_link { |
1368 | my ($self, $map, $x, $y) = @_; |
1512 | my ($self, $map, $x, $y) = @_; |
1369 | |
1513 | |
1370 | my $link_pos = delete $self->{_link_pos}; |
1514 | my $link_pos = delete $self->{_link_pos}; |
1371 | |
1515 | |
1372 | unless ($map) { |
1516 | unless ($map) { |
1373 | $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
1374 | |
|
|
1375 | # restore original map position |
1517 | # restore original map position |
1376 | ($map, $x, $y) = @{ $link_pos || [] }; |
1518 | ($map, $x, $y) = @{ $link_pos || [] }; |
1377 | $map = cf::map::find_map $map; |
1519 | $map = cf::map::find_map $map; |
1378 | |
1520 | |
1379 | unless ($map) { |
1521 | unless ($map) { |
… | |
… | |
1394 | |
1536 | |
1395 | $self->activate_recursive; |
1537 | $self->activate_recursive; |
1396 | $self->enter_map ($map, $x, $y); |
1538 | $self->enter_map ($map, $x, $y); |
1397 | } |
1539 | } |
1398 | |
1540 | |
|
|
1541 | cf::player->attach ( |
|
|
1542 | on_logout => sub { |
|
|
1543 | my ($pl) = @_; |
|
|
1544 | |
|
|
1545 | # abort map switching before logout |
|
|
1546 | if ($pl->ob->{_link_pos}) { |
|
|
1547 | cf::sync_job { |
|
|
1548 | $pl->ob->leave_link |
|
|
1549 | }; |
|
|
1550 | } |
|
|
1551 | }, |
|
|
1552 | on_login => sub { |
|
|
1553 | my ($pl) = @_; |
|
|
1554 | |
|
|
1555 | # try to abort aborted map switching on player login :) |
|
|
1556 | # should happen only on crashes |
|
|
1557 | if ($pl->ob->{_link_pos}) { |
|
|
1558 | $pl->ob->enter_link; |
|
|
1559 | cf::async { |
|
|
1560 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1561 | # and this sleep increases chances of the player not ending up in scorn |
|
|
1562 | Coro::Timer::sleep 1; |
|
|
1563 | $pl->ob->leave_link; |
|
|
1564 | }; |
|
|
1565 | } |
|
|
1566 | }, |
|
|
1567 | ); |
|
|
1568 | |
1399 | =item $player_object->goto_map ($map, $x, $y) |
1569 | =item $player_object->goto_map ($path, $x, $y) |
1400 | |
1570 | |
1401 | =cut |
1571 | =cut |
1402 | |
1572 | |
1403 | sub cf::object::player::goto_map { |
1573 | sub cf::object::player::goto_map { |
1404 | my ($self, $path, $x, $y) = @_; |
1574 | my ($self, $path, $x, $y) = @_; |
1405 | |
1575 | |
1406 | $self->enter_link; |
1576 | $self->enter_link; |
1407 | |
1577 | |
1408 | (Coro::async { |
1578 | (cf::async { |
1409 | $path = new cf::path $path; |
1579 | $path = new cf::path $path; |
1410 | |
1580 | |
1411 | my $map = cf::map::find_map $path->as_string; |
1581 | my $map = cf::map::find_map $path->as_string; |
1412 | $map = $map->customise_for ($self) if $map; |
1582 | $map = $map->customise_for ($self) if $map; |
1413 | |
1583 | |
1414 | warn "entering ", $map->path, " at ($x, $y)\n" |
1584 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1415 | if $map; |
1585 | # if $map; |
|
|
1586 | |
|
|
1587 | $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1416 | |
1588 | |
1417 | $self->leave_link ($map, $x, $y); |
1589 | $self->leave_link ($map, $x, $y); |
1418 | })->prio (1); |
1590 | })->prio (1); |
1419 | } |
1591 | } |
1420 | |
1592 | |
… | |
… | |
1473 | |
1645 | |
1474 | return unless $self->type == cf::PLAYER; |
1646 | return unless $self->type == cf::PLAYER; |
1475 | |
1647 | |
1476 | $self->enter_link; |
1648 | $self->enter_link; |
1477 | |
1649 | |
1478 | (Coro::async { |
1650 | (cf::async { |
1479 | unless (eval { |
1651 | unless (eval { |
1480 | |
|
|
1481 | prepare_random_map $exit |
1652 | prepare_random_map $exit |
1482 | if $exit->slaying eq "/!"; |
1653 | if $exit->slaying eq "/!"; |
1483 | |
1654 | |
1484 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1655 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1485 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1656 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
… | |
… | |
1547 | on_reply => sub { |
1718 | on_reply => sub { |
1548 | my ($ns, $msg) = @_; |
1719 | my ($ns, $msg) = @_; |
1549 | |
1720 | |
1550 | # this weird shuffling is so that direct followup queries |
1721 | # this weird shuffling is so that direct followup queries |
1551 | # get handled first |
1722 | # get handled first |
1552 | my $queue = delete $ns->{query_queue}; |
1723 | my $queue = delete $ns->{query_queue} |
|
|
1724 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1553 | |
1725 | |
1554 | (shift @$queue)->[1]->($msg); |
1726 | (shift @$queue)->[1]->($msg); |
1555 | |
1727 | |
1556 | push @{ $ns->{query_queue} }, @$queue; |
1728 | push @{ $ns->{query_queue} }, @$queue; |
1557 | |
1729 | |
… | |
… | |
1574 | =cut |
1746 | =cut |
1575 | |
1747 | |
1576 | sub cf::client::coro { |
1748 | sub cf::client::coro { |
1577 | my ($self, $cb) = @_; |
1749 | my ($self, $cb) = @_; |
1578 | |
1750 | |
1579 | my $coro; $coro = async { |
1751 | my $coro = &cf::async ($cb); |
1580 | eval { |
|
|
1581 | $cb->(); |
|
|
1582 | }; |
|
|
1583 | warn $@ if $@; |
|
|
1584 | }; |
|
|
1585 | |
1752 | |
1586 | $coro->on_destroy (sub { |
1753 | $coro->on_destroy (sub { |
1587 | delete $self->{_coro}{$coro+0}; |
1754 | delete $self->{_coro}{$coro+0}; |
1588 | }); |
1755 | }); |
1589 | |
1756 | |
… | |
… | |
1761 | |
1928 | |
1762 | { |
1929 | { |
1763 | my $path = cf::localdir . "/database.pst"; |
1930 | my $path = cf::localdir . "/database.pst"; |
1764 | |
1931 | |
1765 | sub db_load() { |
1932 | sub db_load() { |
1766 | warn "loading database $path\n";#d# remove later |
|
|
1767 | $DB = stat $path ? Storable::retrieve $path : { }; |
1933 | $DB = stat $path ? Storable::retrieve $path : { }; |
1768 | } |
1934 | } |
1769 | |
1935 | |
1770 | my $pid; |
1936 | my $pid; |
1771 | |
1937 | |
1772 | sub db_save() { |
1938 | sub db_save() { |
1773 | warn "saving database $path\n";#d# remove later |
|
|
1774 | waitpid $pid, 0 if $pid; |
1939 | waitpid $pid, 0 if $pid; |
1775 | if (0 == ($pid = fork)) { |
1940 | if (0 == ($pid = fork)) { |
1776 | $DB->{_meta}{version} = 1; |
1941 | $DB->{_meta}{version} = 1; |
1777 | Storable::nstore $DB, "$path~"; |
1942 | Storable::nstore $DB, "$path~"; |
1778 | rename "$path~", $path; |
1943 | rename "$path~", $path; |
… | |
… | |
1826 | open my $fh, "<:utf8", cf::confdir . "/config" |
1991 | open my $fh, "<:utf8", cf::confdir . "/config" |
1827 | or return; |
1992 | or return; |
1828 | |
1993 | |
1829 | local $/; |
1994 | local $/; |
1830 | *CFG = YAML::Syck::Load <$fh>; |
1995 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1996 | |
|
|
1997 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
|
|
1998 | |
|
|
1999 | if (exists $CFG{mlockall}) { |
|
|
2000 | eval { |
|
|
2001 | $CFG{mlockall} ? &mlockall : &munlockall |
|
|
2002 | and die "WARNING: m(un)lockall failed: $!\n"; |
|
|
2003 | }; |
|
|
2004 | warn $@ if $@; |
|
|
2005 | } |
1831 | } |
2006 | } |
1832 | |
2007 | |
1833 | sub main { |
2008 | sub main { |
1834 | # we must not ever block the main coroutine |
2009 | # we must not ever block the main coroutine |
1835 | local $Coro::idle = sub { |
2010 | local $Coro::idle = sub { |
1836 | Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# |
2011 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
1837 | (Coro::unblock_sub { |
2012 | (Coro::unblock_sub { |
1838 | Event::one_event; |
2013 | Event::one_event; |
1839 | })->(); |
2014 | })->(); |
1840 | }; |
2015 | }; |
1841 | |
2016 | |
… | |
… | |
2038 | or warn "ERROR: unable to write runtime file: $!"; |
2213 | or warn "ERROR: unable to write runtime file: $!"; |
2039 | })->(); |
2214 | })->(); |
2040 | }, |
2215 | }, |
2041 | ); |
2216 | ); |
2042 | |
2217 | |
|
|
2218 | END { cf::emergency_save } |
|
|
2219 | |
2043 | 1 |
2220 | 1 |
2044 | |
2221 | |