ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
(Generate patch)

Comparing deliantra/server/ext/00_map_handling.ext (file contents):
Revision 1.1 by root, Sat Dec 30 10:16:10 2006 UTC vs.
Revision 1.8 by root, Sun Dec 31 10:28:36 2006 UTC

130 and return; 130 and return;
131 131
132 1 132 1
133} 133}
134 134
135(Coro::unblock_sub { 135(Coro::async {
136 unless (write_runtime) { 136 unless (write_runtime) {
137 warn "unable to write runtime file: $!"; 137 warn "unable to write runtime file: $!";
138 exit 1; 138 exit 1;
139 } 139 }
140})->(); 140})->prio (Coro::PRIO_MAX);
141 141
142our $SCHEDULER = cf::coro { 142our $SCHEDULER = cf::coro {
143 Coro::Timer::sleep 3600;#d#TODO#for debugging only
143 while () { 144 while () {
144 Coro::Timer::sleep $SCHEDULE_INTERVAL; 145 Coro::Timer::sleep $SCHEDULE_INTERVAL;
145 146
146 write_runtime 147 write_runtime
147 or warn "unable to write runtime file: $!"; 148 or warn "unable to write runtime file: $!";
148 149
149 for my $map (values %cf::MAP) { 150 for my $map (values %cf::MAP) {
150 eval { 151 eval {
151 next if $map->in_memory != cf::MAP_IN_MEMORY; 152 next if $map->in_memory != cf::MAP_IN_MEMORY;
153 next if $map->players;
152 my $last_access = $map->last_access; 154 my $last_access = $map->last_access;
153 # not yet, because maps might become visible to players nearby 155 # not yet, because maps might become visible to players nearby
154 # we need a tiled meta map for this to work 156 # we need a tiled meta map for this to work
155# if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { 157# if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
156# $map->deactivate; 158# $map->deactivate;
171 } 173 }
172 } 174 }
173}; 175};
174$SCHEDULER->prio (-2); 176$SCHEDULER->prio (-2);
175 177
178sub generate_random_map {
179 my ($path, $rmp) = @_;
180
181 # mit "rum" bekleckert, nicht
182 cf::map::_create_random_map
183 $path,
184 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
185 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
186 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
187 $rmp->{exit_on_final_map},
188 $rmp->{xsize}, $rmp->{ysize},
189 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
190 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
191 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
192 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
193 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
194 $rmp->{region}
195}
196
197sub parse_random_map_params {
198 my ($spec) = @_;
199
200 my $rmp;
201
202 for (split /\n/, $spec) {
203 my ($k, $v) = split /\s+/, $_, 2;
204
205 $rmp->{lc $k} = $v if (length $k) && (length $v);
206 }
207
208 $rmp
209}
210
211sub prepare_random_map {
212 my ($exit) = @_;
213
214 my $rmp = parse_random_map_params $exit->msg;
215 use Data::Dumper;
216 die Dumper $rmp;
217}
218
176sub sync_job(&) { 219sub sync_job(&) {
177 my ($job) = @_; 220 my ($job) = @_;
178 221
179 my $done; 222 my $done;
180 my @res; 223 my @res;
181 224
182 Carp::confess "nested sync_job" if $cf::FREEZE; 225 Carp::confess "nested sync_job" if $cf::FREEZE;
183 226
184 local $cf::FREEZE = 1; 227 local $cf::FREEZE = 1;
185 228
186 (async { 229 (Coro::async {
187 @res = eval { $job->() }; 230 @res = eval { $job->() };
188 warn $@ if $@; 231 warn $@ if $@;
189 $done = 1; 232 $done = 1;
190 })->prio (Coro::PRIO_MAX); 233 })->prio (Coro::PRIO_MAX);
191 234
192 while (!$done) { 235 while (!$done) {
193 Coro::cede_notself; 236 Coro::cede_notself;
194 Event::one_event unless Coro::nready; 237 Event::one_event unless Coro::nready;
195 } 238 }
196 239
197 warn "job done<@res>\n";#d# 240 warn "sync<@res>\n";#d#
198
199 wantarray ? @res : $res[0] 241 wantarray ? @res : $res[0]
200} 242}
201 243
202# and all this just because we cannot iterate over 244# and all this just because we cannot iterate over
203# all maps in C++... 245# all maps in C++...
222 264
223 $map->reset_time (0) if $map->reset_time > $cf::RUNTIME; 265 $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
224 $map->reset_timeout (10);#d# 266 $map->reset_timeout (10);#d#
225 267
226 $map->{load_path} = $path; 268 $map->{load_path} = $path;
227 warn "load header from $path\n";#d#
228 269
229 $map 270 $map
230} 271}
231 272
232sub cf::map::find_map_nb { 273sub cf::map::find_map {
233 my ($path, $origin) = @_; 274 my ($path, $origin) = @_;
275
276 warn "find_map<$path,$origin>\n";#d#
234 277
235 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; 278 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
236 my $key = $path->as_string; 279 my $key = $path->as_string;
237
238 warn "find_map_nb<$path,$origin>\n";#d#
239 280
240 $cf::MAP{$key} || do { 281 $cf::MAP{$key} || do {
241 # do it the slow way 282 # do it the slow way
242 my $map = try_load_header $path->save_path; 283 my $map = try_load_header $path->save_path;
243 284
244 if (!$map) { 285 if (!$map) {
245 $map = try_load_header $path->load_path 286 $map = try_load_header $path->load_path
246 or return; 287 or return;
247 }
248 288
249 $map or return;
250
251 $map->instantiate; 289 $map->instantiate;
290
291 # per-player maps become, after loading, normal maps
292 $map->per_player (0) if $path->{user_rel};
293 }
294
252 $map->path ($key); 295 $map->path ($key);
253 $map->{path} = $path; 296 $map->{path} = $path;
254 297
255 $map->reset if $map->should_reset; 298 $map->reset if $map->should_reset;
256 299
257 $cf::MAP{$key} = $map 300 $cf::MAP{$key} = $map
258 } 301 }
259} 302}
260 303
261sub cf::map::find_map {
262 my ($path, $origin) = @_;
263
264 $path = new cf::path $path, $origin && $origin->path;
265 my $key = $path->as_string;
266
267 warn "find_map<$path,$origin>\n";#d#
268
269 $cf::MAP{$key} || sync_job {
270 cf::map::find_map_nb $path;
271 }
272}
273
274sub cf::map::do_load_nb { 304sub cf::map::load {
275 my ($self) = @_; 305 my ($self) = @_;
276 306
277 return 0 unless $self->in_memory == cf::MAP_SWAPPED; 307 return if $self->in_memory != cf::MAP_SWAPPED;
278 308
279 $self->in_memory (cf::MAP_LOADING); 309 $self->in_memory (cf::MAP_LOADING);
280 310
281 my $path = $self->{path}; 311 my $path = $self->{path};
282 312
298 $self->decay_objects; 328 $self->decay_objects;
299 $self->update_buttons; 329 $self->update_buttons;
300 $self->set_darkness_map; 330 $self->set_darkness_map;
301 $self->difficulty ($self->estimate_difficulty) 331 $self->difficulty ($self->estimate_difficulty)
302 unless $self->difficulty; 332 unless $self->difficulty;
333 $self->activate;
303 334
304 $self->in_memory (cf::MAP_IN_MEMORY); 335 $self->in_memory (cf::MAP_IN_MEMORY);
305} 336}
306 337
307sub cf::map::do_load { 338sub cf::map::load_map_sync {
308 my ($self) = @_; 339 my ($path, $origin) = @_;
309 340
310 warn "do_load<$self>\n";#d# 341 warn "load_map_sync<$path, $origin>\n";#d#
311 342
312 sync_job { 343 sync_job {
313 cf::map::do_load_nb $self; 344 my $map = cf::map::find_map $path, $origin
345 or return;
346 $map->load;
347 $map
314 }; 348 }
315} 349}
316 350
317sub cf::map::save { 351sub cf::map::save {
318 my ($self) = @_; 352 my ($self) = @_;
319 353
331 365
332 $self->{load_path} = $save; 366 $self->{load_path} = $save;
333 $self->{last_save} = $cf::RUNTIME; 367 $self->{last_save} = $cf::RUNTIME;
334} 368}
335 369
336sub cf::map::should_reset {
337 my ($map) = @_;
338
339 # TODO: safety, remove and allow resettable per-player maps
340 return if $map->{path}{user_rel};#d#
341 return if $map->per_player;
342 return unless $map->reset_timeout;
343
344 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
345
346 $time + $map->reset_timeout < $cf::RUNTIME
347}
348
349sub cf::map::reset {
350 my ($self) = @_;
351
352 return if $self->players;
353
354 warn "resetting map ", $self->path;#d#
355 return;#d#
356
357 $self->clear;
358 $self->in_memory (cf::MAP_SWAPPED);
359 $self->{load_path} = $self->{path}->load_path;
360 utf8::encode $self->{load_path};
361}
362
363sub cf::map::swap_out { 370sub cf::map::swap_out {
364 my ($self) = @_; 371 my ($self) = @_;
372
373 return if $self->players;
374 return if $self->in_memory != cf::MAP_IN_MEMORY;
365 375
366 $self->save; 376 $self->save;
367 $self->clear; 377 $self->clear;
368 $self->in_memory (cf::MAP_SWAPPED); 378 $self->in_memory (cf::MAP_SWAPPED);
369} 379}
370 380
381sub cf::map::should_reset {
382 my ($map) = @_;
383
384 # TODO: safety, remove and allow resettable per-player maps
385 return if $map->{path}{user_rel};#d#
386 return unless $map->reset_timeout;
387
388 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
389
390 $time + $map->reset_timeout < $cf::RUNTIME
391}
392
393sub cf::map::reset {
394 my ($self) = @_;
395
396 return if $self->players;
397 return if $self->{path}{user_rel};#d#
398
399 warn "resetting map ", $self->path;#d#
400
401 utf8::encode (my $save = $self->{path}->save_path);
402 aioreq_pri 3; IO::AIO::aio_unlink $save;
403 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
404
405 $self->clear;
406 $self->in_memory (cf::MAP_SWAPPED);
407 utf8::encode ($self->{load_path} = $self->{path}->load_path);
408}
409
371sub cf::object::player::enter_exit { 410sub cf::object::player::enter_exit {
372 my ($ob, $exit) = @_; 411 my ($ob, $exit) = @_;
373 412
374 warn "enter_exit\n";#d# 413 return unless $ob->type == cf::PLAYER;
414
415 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
416
417 #TODO: do this in the background, freeze the player if required
375 sync_job { 418 sync_job {
376 warn "enter_exit<$ob,$exit>\n";#d# 419 my ($map, $x, $y);
377 420
378 if ($exit) { 421 if ($exit->slaying eq "/!") {
422 prepare_random_map $exit;
423 }
424
379 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 425 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
380 426
381 my $map = cf::map::find_map_nb $path->as_string; 427 $map = cf::map::find_map $path->as_string;
382 $map = $map->customise_for ($ob) if $map; 428 $map = $map->customise_for ($ob) if $map;
429 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
383 430
384 if ($map) { 431 unless ($map) {
385 $map->do_load_nb;
386 $ob->enter_map ($map, $exit->stats->hp, $exit->stats->sp);
387 } else {
388 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); 432 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
389 } 433
390 } else { 434 # restore original map position
391 # used on login only(?) 435 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
392 my $map = cf::map::find_map_nb $ob->contr->maplevel;
393 my ($x, $y) = ($ob->x, $ob->y);
394 436
395 unless ($map) { 437 unless ($map) {
396 $map = cf::map::find_map_nb $emergency_position->[0] 438 $map = cf::map::find_map $emergency_position->[0]
397 or die "FATAL: cannot load emergency map\n"; 439 or die "FATAL: cannot load emergency map\n";
398 $x = $emergency_position->[1]; 440 $x = $emergency_position->[1];
399 $y = $emergency_position->[2]; 441 $y = $emergency_position->[2];
400 } 442 }
443 }
401 444
445 # use -1, -1 as default coordinates, not 0, 0
446 ($x, $y) = ($map->enter_x, $map->enter_y)
447 if $x <=0 && $y <= 0;
448
449 warn "entering ", $map->path, " at ($x, $y)\n";#d#
402 $map->do_load_nb; 450 $map->load;
403 $ob->enter_map ($map, $x, $y); 451 $ob->enter_map ($map, $x, $y);
404 }
405 } 452 }
406} 453}
407 454
408sub cf::map::customise_for { 455sub cf::map::customise_for {
409 my ($map, $ob) = @_; 456 my ($map, $ob) = @_;
410 457
411 warn "customise_for<$map,$ob>\n";#d#
412
413 if ($map->per_player) { 458 if ($map->per_player) {
414 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; 459 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
415 } 460 }
416 461
417 $map 462 $map
418} 463}
419 464
420sub cf::map::emergency_save { 465sub cf::map::emergency_save {
421 local $cf::FREEZE = 1; 466 local $cf::FREEZE = 1;
422 467
468 warn "enter emergency map save\n";
469
470 my $saver = async {
423 warn "begin emergency map save\n"; 471 warn "begin emergency map save\n";
424 $_->save for values %cf::MAP; 472 $_->save for values %cf::MAP;
473 };
474 $saver->prio (Coro::PRIO_MAX);
475 $saver->join;
476
425 warn "emergency map save drain\n"; 477 warn "emergency map save drain\n";
426
427 Event::one_event while IO::AIO::nreqs; 478 Event::one_event while IO::AIO::nreqs;
428 warn "end emergency map save\n"; 479 warn "end emergency map save\n";
429} 480}
430 481

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines