… | |
… | |
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. |
… | |
… | |
197 | =cut |
276 | =cut |
198 | |
277 | |
199 | sub sync_job(&) { |
278 | sub sync_job(&) { |
200 | my ($job) = @_; |
279 | my ($job) = @_; |
201 | |
280 | |
202 | my $busy = 1; |
|
|
203 | my @res; |
|
|
204 | |
|
|
205 | my $coro = Coro::async { |
|
|
206 | @res = eval { $job->() }; |
|
|
207 | warn $@ if $@; |
|
|
208 | undef $busy; |
|
|
209 | }; |
|
|
210 | |
|
|
211 | if ($Coro::current == $Coro::main) { |
281 | if ($Coro::current == $Coro::main) { |
|
|
282 | # this is the main coro, too bad, we have to block |
|
|
283 | # till the operation succeeds, freezing the server :/ |
|
|
284 | |
212 | # TODO: use suspend/resume instead |
285 | # TODO: use suspend/resume instead |
|
|
286 | # (but this is cancel-safe) |
213 | local $FREEZE = 1; |
287 | local $FREEZE = 1; |
|
|
288 | |
|
|
289 | my $busy = 1; |
|
|
290 | my @res; |
|
|
291 | |
|
|
292 | (Coro::async { |
|
|
293 | @res = eval { $job->() }; |
|
|
294 | warn $@ if $@; |
|
|
295 | undef $busy; |
214 | $coro->prio (Coro::PRIO_MAX); |
296 | })->prio (Coro::PRIO_MAX); |
|
|
297 | |
215 | while ($busy) { |
298 | while ($busy) { |
216 | Coro::cede_notself; |
299 | Coro::cede_notself; |
217 | Event::one_event unless Coro::nready; |
300 | Event::one_event unless Coro::nready; |
218 | } |
301 | } |
|
|
302 | |
|
|
303 | wantarray ? @res : $res[0] |
219 | } else { |
304 | } else { |
220 | $coro->join; |
305 | # we are in another coroutine, how wonderful, everything just works |
|
|
306 | |
|
|
307 | $job->() |
221 | } |
308 | } |
222 | |
|
|
223 | wantarray ? @res : $res[0] |
|
|
224 | } |
309 | } |
225 | |
310 | |
226 | =item $coro = cf::coro { BLOCK } |
311 | =item $coro = cf::coro { BLOCK } |
227 | |
312 | |
228 | Creates and returns a new coro. This coro is automcatially being canceled |
313 | Creates and returns a new coro. This coro is automcatially being canceled |
… | |
… | |
231 | =cut |
316 | =cut |
232 | |
317 | |
233 | sub coro(&) { |
318 | sub coro(&) { |
234 | my $cb = shift; |
319 | my $cb = shift; |
235 | |
320 | |
236 | my $coro; $coro = async { |
321 | my $coro = &cf::async ($cb); |
237 | eval { |
|
|
238 | $cb->(); |
|
|
239 | }; |
|
|
240 | warn $@ if $@; |
|
|
241 | }; |
|
|
242 | |
322 | |
243 | $coro->on_destroy (sub { |
323 | $coro->on_destroy (sub { |
244 | delete $EXT_CORO{$coro+0}; |
324 | delete $EXT_CORO{$coro+0}; |
245 | }); |
325 | }); |
246 | $EXT_CORO{$coro+0} = $coro; |
326 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
252 | my $runtime = cf::localdir . "/runtime"; |
332 | my $runtime = cf::localdir . "/runtime"; |
253 | |
333 | |
254 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
334 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
255 | or return; |
335 | or return; |
256 | |
336 | |
257 | my $value = $cf::RUNTIME; |
337 | my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock |
258 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
338 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
259 | and return; |
339 | and return; |
260 | |
340 | |
261 | aio_fsync $fh |
341 | aio_fsync $fh |
262 | and return; |
342 | and return; |
… | |
… | |
283 | |
363 | |
284 | $path = $path->as_string if ref $path; |
364 | $path = $path->as_string if ref $path; |
285 | |
365 | |
286 | my $self = bless { }, $class; |
366 | my $self = bless { }, $class; |
287 | |
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 |
288 | if ($path =~ s{^\?random/}{}) { |
378 | } elsif ($path =~ s{^\?random/}{}) { |
289 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
379 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
290 | $self->{random} = cf::from_json $data; |
380 | $self->{random} = cf::from_json $data; |
291 | } else { |
381 | } else { |
292 | if ($path =~ s{^~([^/]+)?}{}) { |
382 | if ($path =~ s{^~([^/]+)?}{}) { |
293 | $self->{user_rel} = 1; |
383 | $self->{user_rel} = 1; |
… | |
… | |
798 | or return; |
888 | or return; |
799 | |
889 | |
800 | unless (aio_stat "$filename.pst") { |
890 | unless (aio_stat "$filename.pst") { |
801 | (aio_load "$filename.pst", $av) >= 0 |
891 | (aio_load "$filename.pst", $av) >= 0 |
802 | or return; |
892 | or return; |
803 | $av = eval { (Storable::thaw <$av>)->{objs} }; |
893 | $av = eval { (Storable::thaw $av)->{objs} }; |
804 | } |
894 | } |
805 | |
895 | |
|
|
896 | warn sprintf "loading %s (%d)\n", |
|
|
897 | $filename, length $data, scalar @{$av || []};#d# |
806 | return ($data, $av); |
898 | return ($data, $av); |
807 | } |
899 | } |
808 | |
900 | |
809 | ############################################################################# |
901 | ############################################################################# |
810 | # command handling &c |
902 | # command handling &c |
… | |
… | |
1046 | use Fcntl; |
1138 | use Fcntl; |
1047 | use Coro::AIO; |
1139 | use Coro::AIO; |
1048 | |
1140 | |
1049 | our $MAX_RESET = 7200; |
1141 | our $MAX_RESET = 7200; |
1050 | our $DEFAULT_RESET = 3600; |
1142 | our $DEFAULT_RESET = 3600; |
1051 | $MAX_RESET = 10;#d# |
|
|
1052 | $DEFAULT_RESET = 10;#d# |
|
|
1053 | |
1143 | |
1054 | sub generate_random_map { |
1144 | sub generate_random_map { |
1055 | my ($path, $rmp) = @_; |
1145 | my ($path, $rmp) = @_; |
1056 | |
1146 | |
1057 | # mit "rum" bekleckern, nicht |
1147 | # mit "rum" bekleckern, nicht |
… | |
… | |
1073 | # and all this just because we cannot iterate over |
1163 | # and all this just because we cannot iterate over |
1074 | # all maps in C++... |
1164 | # all maps in C++... |
1075 | sub change_all_map_light { |
1165 | sub change_all_map_light { |
1076 | my ($change) = @_; |
1166 | my ($change) = @_; |
1077 | |
1167 | |
1078 | $_->change_map_light ($change) for values %cf::MAP; |
1168 | $_->change_map_light ($change) |
|
|
1169 | for grep $_->outdoor, values %cf::MAP; |
1079 | } |
1170 | } |
1080 | |
1171 | |
1081 | sub try_load_header($) { |
1172 | sub try_load_header($) { |
1082 | my ($path) = @_; |
1173 | my ($path) = @_; |
1083 | |
1174 | |
… | |
… | |
1090 | |
1181 | |
1091 | $map->load_header ($path) |
1182 | $map->load_header ($path) |
1092 | or return; |
1183 | or return; |
1093 | |
1184 | |
1094 | $map->{load_path} = $path; |
1185 | $map->{load_path} = $path; |
1095 | use Data::Dumper; warn Dumper $map;#d# |
|
|
1096 | |
1186 | |
1097 | $map |
1187 | $map |
1098 | } |
1188 | } |
1099 | |
1189 | |
|
|
1190 | sub find_map; |
1100 | sub find_map { |
1191 | sub find_map { |
1101 | my ($path, $origin) = @_; |
1192 | my ($path, $origin) = @_; |
1102 | |
1193 | |
1103 | #warn "find_map<$path,$origin>\n";#d# |
1194 | #warn "find_map<$path,$origin>\n";#d# |
1104 | |
1195 | |
1105 | $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; |
1196 | $path = new cf::path $path, $origin && $origin->path; |
1106 | my $key = $path->as_string; |
1197 | my $key = $path->as_string; |
1107 | |
1198 | |
|
|
1199 | cf::lock_wait "map_find:$key"; |
|
|
1200 | |
1108 | $cf::MAP{$key} || do { |
1201 | $cf::MAP{$key} || do { |
|
|
1202 | my $guard = cf::lock_acquire "map_find:$key"; |
|
|
1203 | |
1109 | # do it the slow way |
1204 | # do it the slow way |
1110 | my $map = try_load_header $path->save_path; |
1205 | my $map = try_load_header $path->save_path; |
1111 | |
1206 | |
1112 | if ($map) { |
1207 | if ($map) { |
|
|
1208 | $map->last_access ((delete $map->{last_access}) |
|
|
1209 | || $cf::RUNTIME); #d# |
1113 | # safety |
1210 | # safety |
1114 | $map->{instantiate_time} = $cf::RUNTIME |
1211 | $map->{instantiate_time} = $cf::RUNTIME |
1115 | if $map->{instantiate_time} > $cf::RUNTIME; |
1212 | if $map->{instantiate_time} > $cf::RUNTIME; |
1116 | } else { |
1213 | } else { |
1117 | if (my $rmp = $path->random_map_params) { |
1214 | if (my $rmp = $path->random_map_params) { |
… | |
… | |
1120 | $map = try_load_header $path->load_path; |
1217 | $map = try_load_header $path->load_path; |
1121 | } |
1218 | } |
1122 | |
1219 | |
1123 | $map or return; |
1220 | $map or return; |
1124 | |
1221 | |
|
|
1222 | $map->{load_original} = 1; |
1125 | $map->{instantiate_time} = $cf::RUNTIME; |
1223 | $map->{instantiate_time} = $cf::RUNTIME; |
|
|
1224 | $map->last_access ($cf::RUNTIME); |
1126 | $map->instantiate; |
1225 | $map->instantiate; |
1127 | |
1226 | |
1128 | # per-player maps become, after loading, normal maps |
1227 | # per-player maps become, after loading, normal maps |
1129 | $map->per_player (0) if $path->{user_rel}; |
1228 | $map->per_player (0) if $path->{user_rel}; |
1130 | } |
1229 | } |
1131 | |
1230 | |
1132 | $map->path ($key); |
1231 | $map->path ($key); |
1133 | $map->{path} = $path; |
1232 | $map->{path} = $path; |
1134 | $map->last_access ($cf::RUNTIME); |
1233 | $map->{last_save} = $cf::RUNTIME; |
1135 | |
1234 | |
1136 | $map->reset if $map->should_reset; |
1235 | if ($map->should_reset) { |
|
|
1236 | $map->reset; |
|
|
1237 | undef $guard; |
|
|
1238 | $map = find_map $path |
|
|
1239 | or return; |
|
|
1240 | } |
1137 | |
1241 | |
1138 | $cf::MAP{$key} = $map |
1242 | $cf::MAP{$key} = $map |
1139 | } |
1243 | } |
1140 | } |
1244 | } |
1141 | |
1245 | |
1142 | sub load { |
1246 | sub load { |
1143 | my ($self) = @_; |
1247 | my ($self) = @_; |
1144 | |
1248 | |
|
|
1249 | my $path = $self->{path}; |
|
|
1250 | my $guard = cf::lock_acquire "map_load:" . $path->as_string; |
|
|
1251 | |
1145 | return if $self->in_memory != cf::MAP_SWAPPED; |
1252 | return if $self->in_memory != cf::MAP_SWAPPED; |
1146 | |
1253 | |
1147 | $self->in_memory (cf::MAP_LOADING); |
1254 | $self->in_memory (cf::MAP_LOADING); |
1148 | |
|
|
1149 | my $path = $self->{path}; |
|
|
1150 | |
1255 | |
1151 | $self->alloc; |
1256 | $self->alloc; |
1152 | $self->load_objects ($self->{load_path}, 1) |
1257 | $self->load_objects ($self->{load_path}, 1) |
1153 | or return; |
1258 | or return; |
|
|
1259 | |
|
|
1260 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1261 | if delete $self->{load_original}; |
1154 | |
1262 | |
1155 | if (my $uniq = $path->uniq_path) { |
1263 | if (my $uniq = $path->uniq_path) { |
1156 | utf8::encode $uniq; |
1264 | utf8::encode $uniq; |
1157 | if (aio_open $uniq, O_RDONLY, 0) { |
1265 | if (aio_open $uniq, O_RDONLY, 0) { |
1158 | $self->clear_unique_items; |
1266 | $self->clear_unique_items; |
… | |
… | |
1193 | } |
1301 | } |
1194 | |
1302 | |
1195 | sub save { |
1303 | sub save { |
1196 | my ($self) = @_; |
1304 | my ($self) = @_; |
1197 | |
1305 | |
|
|
1306 | $self->{last_save} = $cf::RUNTIME; |
|
|
1307 | |
|
|
1308 | return unless $self->dirty; |
|
|
1309 | |
1198 | my $save = $self->{path}->save_path; utf8::encode $save; |
1310 | my $save = $self->{path}->save_path; utf8::encode $save; |
1199 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
1311 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
1200 | |
1312 | |
1201 | $self->{last_save} = $cf::RUNTIME; |
|
|
1202 | |
|
|
1203 | return unless $self->dirty; |
|
|
1204 | |
|
|
1205 | $self->{load_path} = $save; |
1313 | $self->{load_path} = $save; |
1206 | |
1314 | |
1207 | return if $self->{deny_save}; |
1315 | return if $self->{deny_save}; |
1208 | |
1316 | |
1209 | warn "saving map ", $self->path; |
1317 | local $self->{last_access} = $self->last_access;#d# |
1210 | |
1318 | |
1211 | if ($uniq) { |
1319 | if ($uniq) { |
1212 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1320 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1213 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1321 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1214 | } else { |
1322 | } else { |
… | |
… | |
1217 | } |
1325 | } |
1218 | |
1326 | |
1219 | sub swap_out { |
1327 | sub swap_out { |
1220 | my ($self) = @_; |
1328 | my ($self) = @_; |
1221 | |
1329 | |
|
|
1330 | # save first because save cedes |
|
|
1331 | $self->save; |
|
|
1332 | |
1222 | return if $self->players; |
1333 | return if $self->players; |
1223 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1334 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1224 | return if $self->{deny_save}; |
1335 | return if $self->{deny_save}; |
1225 | |
1336 | |
1226 | $self->save; |
|
|
1227 | $self->clear; |
1337 | $self->clear; |
1228 | $self->in_memory (cf::MAP_SWAPPED); |
1338 | $self->in_memory (cf::MAP_SWAPPED); |
1229 | } |
1339 | } |
1230 | |
1340 | |
|
|
1341 | sub reset_at { |
|
|
1342 | my ($self) = @_; |
|
|
1343 | |
|
|
1344 | # TODO: safety, remove and allow resettable per-player maps |
|
|
1345 | return 1e99 if $self->{path}{user_rel}; |
|
|
1346 | return 1e99 if $self->{deny_reset}; |
|
|
1347 | |
|
|
1348 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
1349 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
1350 | |
|
|
1351 | $time + $to |
|
|
1352 | } |
|
|
1353 | |
1231 | sub should_reset { |
1354 | sub should_reset { |
1232 | my ($map) = @_; |
|
|
1233 | |
|
|
1234 | # TODO: safety, remove and allow resettable per-player maps |
|
|
1235 | return if $map->{path}{user_rel};#d# |
|
|
1236 | return if $map->{deny_reset}; |
|
|
1237 | #return unless $map->reset_timeout; |
|
|
1238 | |
|
|
1239 | my $time = $map->fixed_resettime ? $map->{instantiate_time} : $map->last_access; |
|
|
1240 | |
|
|
1241 | $time + ($map->reset_timeout || $DEFAULT_RESET) < $cf::RUNTIME |
|
|
1242 | } |
|
|
1243 | |
|
|
1244 | sub reset { |
|
|
1245 | my ($self) = @_; |
1355 | my ($self) = @_; |
1246 | |
1356 | |
1247 | return if $self->players; |
1357 | $self->reset_at <= $cf::RUNTIME |
1248 | return if $self->{path}{user_rel};#d# |
1358 | } |
1249 | |
1359 | |
1250 | warn "resetting map ", $self->path;#d# |
1360 | sub unlink_save { |
|
|
1361 | my ($self) = @_; |
1251 | |
1362 | |
1252 | utf8::encode (my $save = $self->{path}->save_path); |
1363 | utf8::encode (my $save = $self->{path}->save_path); |
1253 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
1364 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
1254 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
1365 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
|
|
1366 | } |
|
|
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 | |
|
|
1381 | sub reset { |
|
|
1382 | my ($self) = @_; |
|
|
1383 | |
|
|
1384 | return if $self->players; |
|
|
1385 | return if $self->{path}{user_rel};#d# |
|
|
1386 | |
|
|
1387 | warn "resetting map ", $self->path;#d# |
|
|
1388 | |
|
|
1389 | delete $cf::MAP{$self->path}; |
1255 | |
1390 | |
1256 | $_->clear_links_to ($self) for values %cf::MAP; |
1391 | $_->clear_links_to ($self) for values %cf::MAP; |
1257 | |
1392 | |
|
|
1393 | $self->unlink_save; |
1258 | $self->clear; |
1394 | $self->destroy; |
1259 | $self->in_memory (cf::MAP_SWAPPED); |
1395 | } |
1260 | utf8::encode ($self->{load_path} = $self->{path}->load_path); |
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 |
1261 | } |
1406 | } |
1262 | |
1407 | |
1263 | sub customise_for { |
1408 | sub customise_for { |
1264 | my ($map, $ob) = @_; |
1409 | my ($map, $ob) = @_; |
1265 | |
1410 | |
… | |
… | |
1330 | (ref $cf::CFG{"may_$access"} |
1475 | (ref $cf::CFG{"may_$access"} |
1331 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
1476 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
1332 | : $cf::CFG{"may_$access"}) |
1477 | : $cf::CFG{"may_$access"}) |
1333 | } |
1478 | } |
1334 | |
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 | |
1335 | sub cf::object::player::enter_link { |
1498 | sub cf::object::player::enter_link { |
1336 | my ($self) = @_; |
1499 | my ($self) = @_; |
1337 | |
1500 | |
|
|
1501 | $self->deactivate_recursive; |
|
|
1502 | |
1338 | return if $self->map == $LINK_MAP; |
1503 | return if $self->map == $LINK_MAP; |
1339 | |
1504 | |
1340 | $self->{_link_pos} = [$self->map->{path}, $self->x, $self->y] |
1505 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1341 | if $self->map; |
1506 | if $self->map; |
1342 | |
1507 | |
1343 | $self->enter_map ($LINK_MAP, 20, 20); |
1508 | $self->enter_map ($LINK_MAP, 20, 20); |
1344 | $self->deactivate_recursive; |
|
|
1345 | } |
1509 | } |
1346 | |
1510 | |
1347 | sub cf::object::player::leave_link { |
1511 | sub cf::object::player::leave_link { |
1348 | my ($self, $map, $x, $y) = @_; |
1512 | my ($self, $map, $x, $y) = @_; |
1349 | |
1513 | |
1350 | my $link_pos = delete $self->{_link_pos}; |
1514 | my $link_pos = delete $self->{_link_pos}; |
1351 | |
1515 | |
1352 | unless ($map) { |
1516 | unless ($map) { |
1353 | $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
1354 | |
|
|
1355 | # restore original map position |
1517 | # restore original map position |
1356 | ($map, $x, $y) = @{ $link_pos || [] }; |
1518 | ($map, $x, $y) = @{ $link_pos || [] }; |
1357 | $map = cf::map::find_map $map; |
1519 | $map = cf::map::find_map $map; |
1358 | |
1520 | |
1359 | unless ($map) { |
1521 | unless ($map) { |
… | |
… | |
1374 | |
1536 | |
1375 | $self->activate_recursive; |
1537 | $self->activate_recursive; |
1376 | $self->enter_map ($map, $x, $y); |
1538 | $self->enter_map ($map, $x, $y); |
1377 | } |
1539 | } |
1378 | |
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 | |
1379 | =item $player_object->goto_map ($map, $x, $y) |
1569 | =item $player_object->goto_map ($path, $x, $y) |
1380 | |
1570 | |
1381 | =cut |
1571 | =cut |
1382 | |
1572 | |
1383 | sub cf::object::player::goto_map { |
1573 | sub cf::object::player::goto_map { |
1384 | my ($self, $path, $x, $y) = @_; |
1574 | my ($self, $path, $x, $y) = @_; |
1385 | |
1575 | |
1386 | $self->enter_link; |
1576 | $self->enter_link; |
1387 | |
1577 | |
1388 | (Coro::async { |
1578 | (cf::async { |
1389 | $path = new cf::path $path; |
1579 | $path = new cf::path $path; |
1390 | |
1580 | |
1391 | my $map = cf::map::find_map $path->as_string; |
1581 | my $map = cf::map::find_map $path->as_string; |
1392 | $map = $map->customise_for ($self) if $map; |
1582 | $map = $map->customise_for ($self) if $map; |
1393 | |
1583 | |
1394 | warn "entering ", $map->path, " at ($x, $y)\n" |
1584 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1395 | if $map; |
1585 | # if $map; |
|
|
1586 | |
|
|
1587 | $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1396 | |
1588 | |
1397 | $self->leave_link ($map, $x, $y); |
1589 | $self->leave_link ($map, $x, $y); |
1398 | })->prio (1); |
1590 | })->prio (1); |
1399 | } |
1591 | } |
1400 | |
1592 | |
… | |
… | |
1453 | |
1645 | |
1454 | return unless $self->type == cf::PLAYER; |
1646 | return unless $self->type == cf::PLAYER; |
1455 | |
1647 | |
1456 | $self->enter_link; |
1648 | $self->enter_link; |
1457 | |
1649 | |
1458 | (Coro::async { |
1650 | (cf::async { |
1459 | unless (eval { |
1651 | unless (eval { |
1460 | |
|
|
1461 | prepare_random_map $exit |
1652 | prepare_random_map $exit |
1462 | if $exit->slaying eq "/!"; |
1653 | if $exit->slaying eq "/!"; |
1463 | |
1654 | |
1464 | 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; |
1465 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1656 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
… | |
… | |
1527 | on_reply => sub { |
1718 | on_reply => sub { |
1528 | my ($ns, $msg) = @_; |
1719 | my ($ns, $msg) = @_; |
1529 | |
1720 | |
1530 | # this weird shuffling is so that direct followup queries |
1721 | # this weird shuffling is so that direct followup queries |
1531 | # get handled first |
1722 | # get handled first |
1532 | 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 |
1533 | |
1725 | |
1534 | (shift @$queue)->[1]->($msg); |
1726 | (shift @$queue)->[1]->($msg); |
1535 | |
1727 | |
1536 | push @{ $ns->{query_queue} }, @$queue; |
1728 | push @{ $ns->{query_queue} }, @$queue; |
1537 | |
1729 | |
… | |
… | |
1554 | =cut |
1746 | =cut |
1555 | |
1747 | |
1556 | sub cf::client::coro { |
1748 | sub cf::client::coro { |
1557 | my ($self, $cb) = @_; |
1749 | my ($self, $cb) = @_; |
1558 | |
1750 | |
1559 | my $coro; $coro = async { |
1751 | my $coro = &cf::async ($cb); |
1560 | eval { |
|
|
1561 | $cb->(); |
|
|
1562 | }; |
|
|
1563 | warn $@ if $@; |
|
|
1564 | }; |
|
|
1565 | |
1752 | |
1566 | $coro->on_destroy (sub { |
1753 | $coro->on_destroy (sub { |
1567 | delete $self->{_coro}{$coro+0}; |
1754 | delete $self->{_coro}{$coro+0}; |
1568 | }); |
1755 | }); |
1569 | |
1756 | |
… | |
… | |
1741 | |
1928 | |
1742 | { |
1929 | { |
1743 | my $path = cf::localdir . "/database.pst"; |
1930 | my $path = cf::localdir . "/database.pst"; |
1744 | |
1931 | |
1745 | sub db_load() { |
1932 | sub db_load() { |
1746 | warn "loading database $path\n";#d# remove later |
|
|
1747 | $DB = stat $path ? Storable::retrieve $path : { }; |
1933 | $DB = stat $path ? Storable::retrieve $path : { }; |
1748 | } |
1934 | } |
1749 | |
1935 | |
1750 | my $pid; |
1936 | my $pid; |
1751 | |
1937 | |
1752 | sub db_save() { |
1938 | sub db_save() { |
1753 | warn "saving database $path\n";#d# remove later |
|
|
1754 | waitpid $pid, 0 if $pid; |
1939 | waitpid $pid, 0 if $pid; |
1755 | if (0 == ($pid = fork)) { |
1940 | if (0 == ($pid = fork)) { |
1756 | $DB->{_meta}{version} = 1; |
1941 | $DB->{_meta}{version} = 1; |
1757 | Storable::nstore $DB, "$path~"; |
1942 | Storable::nstore $DB, "$path~"; |
1758 | rename "$path~", $path; |
1943 | rename "$path~", $path; |
… | |
… | |
1806 | open my $fh, "<:utf8", cf::confdir . "/config" |
1991 | open my $fh, "<:utf8", cf::confdir . "/config" |
1807 | or return; |
1992 | or return; |
1808 | |
1993 | |
1809 | local $/; |
1994 | local $/; |
1810 | *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 | } |
1811 | } |
2006 | } |
1812 | |
2007 | |
1813 | sub main { |
2008 | sub main { |
1814 | # we must not ever block the main coroutine |
2009 | # we must not ever block the main coroutine |
1815 | local $Coro::idle = sub { |
2010 | local $Coro::idle = sub { |
1816 | 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# |
1817 | (Coro::unblock_sub { |
2012 | (Coro::unblock_sub { |
1818 | Event::one_event; |
2013 | Event::one_event; |
1819 | })->(); |
2014 | })->(); |
1820 | }; |
2015 | }; |
1821 | |
2016 | |
… | |
… | |
1826 | } |
2021 | } |
1827 | |
2022 | |
1828 | ############################################################################# |
2023 | ############################################################################# |
1829 | # initialisation |
2024 | # initialisation |
1830 | |
2025 | |
1831 | sub perl_reload() { |
2026 | sub reload() { |
1832 | # can/must only be called in main |
2027 | # can/must only be called in main |
1833 | if ($Coro::current != $Coro::main) { |
2028 | if ($Coro::current != $Coro::main) { |
1834 | warn "can only reload from main coroutine\n"; |
2029 | warn "can only reload from main coroutine\n"; |
1835 | return; |
2030 | return; |
1836 | } |
2031 | } |
… | |
… | |
1963 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2158 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
1964 | } |
2159 | } |
1965 | |
2160 | |
1966 | register "<global>", __PACKAGE__; |
2161 | register "<global>", __PACKAGE__; |
1967 | |
2162 | |
1968 | register_command "perl-reload" => sub { |
2163 | register_command "reload" => sub { |
1969 | my ($who, $arg) = @_; |
2164 | my ($who, $arg) = @_; |
1970 | |
2165 | |
1971 | if ($who->flag (FLAG_WIZ)) { |
2166 | if ($who->flag (FLAG_WIZ)) { |
1972 | $who->message ("start of reload."); |
2167 | $who->message ("start of reload."); |
1973 | perl_reload; |
2168 | reload; |
1974 | $who->message ("end of reload."); |
2169 | $who->message ("end of reload."); |
1975 | } |
2170 | } |
1976 | }; |
2171 | }; |
1977 | |
2172 | |
1978 | unshift @INC, $LIBDIR; |
2173 | unshift @INC, $LIBDIR; |
… | |
… | |
2018 | or warn "ERROR: unable to write runtime file: $!"; |
2213 | or warn "ERROR: unable to write runtime file: $!"; |
2019 | })->(); |
2214 | })->(); |
2020 | }, |
2215 | }, |
2021 | ); |
2216 | ); |
2022 | |
2217 | |
|
|
2218 | END { cf::emergency_save } |
|
|
2219 | |
2023 | 1 |
2220 | 1 |
2024 | |
2221 | |