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.4 by root, Sat Dec 30 16:56:16 2006 UTC vs.
Revision 1.8 by root, Sun Dec 31 10:28:36 2006 UTC

138 exit 1; 138 exit 1;
139 } 139 }
140})->prio (Coro::PRIO_MAX); 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;
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
240 warn "sync<@res>\n";#d#
197 wantarray ? @res : $res[0] 241 wantarray ? @res : $res[0]
198} 242}
199 243
200# and all this just because we cannot iterate over 244# and all this just because we cannot iterate over
201# all maps in C++... 245# all maps in C++...
224 $map->{load_path} = $path; 268 $map->{load_path} = $path;
225 269
226 $map 270 $map
227} 271}
228 272
229sub cf::map::find_map_nb { 273sub cf::map::find_map {
230 my ($path, $origin) = @_; 274 my ($path, $origin) = @_;
231 275
232 warn "find_map_nb<$path,$origin>\n";#d# 276 warn "find_map<$path,$origin>\n";#d#
233 277
234 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; 278 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
235 my $key = $path->as_string; 279 my $key = $path->as_string;
236 280
237 $cf::MAP{$key} || do { 281 $cf::MAP{$key} || do {
239 my $map = try_load_header $path->save_path; 283 my $map = try_load_header $path->save_path;
240 284
241 if (!$map) { 285 if (!$map) {
242 $map = try_load_header $path->load_path 286 $map = try_load_header $path->load_path
243 or return; 287 or return;
244 }
245 288
246 $map or return;
247
248 $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
249 $map->path ($key); 295 $map->path ($key);
250 $map->{path} = $path; 296 $map->{path} = $path;
251 297
252 $map->per_player (0) if $path->{user_rel};
253
254 $map->reset if $map->should_reset; 298 $map->reset if $map->should_reset;
255 299
256 $cf::MAP{$key} = $map 300 $cf::MAP{$key} = $map
257 } 301 }
258} 302}
259 303
260sub cf::map::find_map {
261 my ($path, $origin) = @_;
262
263 $path = new cf::path $path, $origin && $origin->path;
264 my $key = $path->as_string;
265
266 warn "find_map<$path,$origin>\n";#d#
267
268 $cf::MAP{$key} || sync_job {
269 cf::map::find_map_nb $path;
270 }
271}
272
273sub cf::map::do_load_nb { 304sub cf::map::load {
274 my ($self) = @_; 305 my ($self) = @_;
275 306
276 return 0 unless $self->in_memory == cf::MAP_SWAPPED; 307 return if $self->in_memory != cf::MAP_SWAPPED;
277 308
278 $self->in_memory (cf::MAP_LOADING); 309 $self->in_memory (cf::MAP_LOADING);
279 310
280 my $path = $self->{path}; 311 my $path = $self->{path};
281 312
297 $self->decay_objects; 328 $self->decay_objects;
298 $self->update_buttons; 329 $self->update_buttons;
299 $self->set_darkness_map; 330 $self->set_darkness_map;
300 $self->difficulty ($self->estimate_difficulty) 331 $self->difficulty ($self->estimate_difficulty)
301 unless $self->difficulty; 332 unless $self->difficulty;
333 $self->activate;
302 334
303 $self->in_memory (cf::MAP_IN_MEMORY); 335 $self->in_memory (cf::MAP_IN_MEMORY);
304} 336}
305 337
306sub cf::map::do_load { 338sub cf::map::load_map_sync {
307 my ($self) = @_; 339 my ($path, $origin) = @_;
308 340
309 warn "do_load<$self>\n";#d# 341 warn "load_map_sync<$path, $origin>\n";#d#
310 342
311 sync_job { 343 sync_job {
312 cf::map::do_load_nb $self; 344 my $map = cf::map::find_map $path, $origin
345 or return;
346 $map->load;
347 $map
313 }; 348 }
314} 349}
315 350
316sub cf::map::save { 351sub cf::map::save {
317 my ($self) = @_; 352 my ($self) = @_;
318 353
333} 368}
334 369
335sub cf::map::swap_out { 370sub cf::map::swap_out {
336 my ($self) = @_; 371 my ($self) = @_;
337 372
373 return if $self->players;
338 $self->save if $self->in_memory == cf::MAP_IN_MEMORY; 374 return if $self->in_memory != cf::MAP_IN_MEMORY;
375
376 $self->save;
339 $self->clear; 377 $self->clear;
340 $self->in_memory (cf::MAP_SWAPPED); 378 $self->in_memory (cf::MAP_SWAPPED);
341} 379}
342 380
343sub cf::map::should_reset { 381sub cf::map::should_reset {
344 my ($map) = @_; 382 my ($map) = @_;
345 383
346 # TODO: safety, remove and allow resettable per-player maps 384 # TODO: safety, remove and allow resettable per-player maps
347 return if $map->{path}{user_rel};#d# 385 return if $map->{path}{user_rel};#d#
348 return if $map->per_player;
349 return unless $map->reset_timeout; 386 return unless $map->reset_timeout;
350 387
351 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 388 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
352 389
353 $time + $map->reset_timeout < $cf::RUNTIME 390 $time + $map->reset_timeout < $cf::RUNTIME
371} 408}
372 409
373sub cf::object::player::enter_exit { 410sub cf::object::player::enter_exit {
374 my ($ob, $exit) = @_; 411 my ($ob, $exit) = @_;
375 412
376 # if at login, move to interim map immediately 413 return unless $ob->type == cf::PLAYER;
377 unless ($exit) { 414
378 # used on login only 415 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
379 $ob->enter_map ($LINK_MAP, 0, 0);
380 }
381 416
382 #TODO: do this in the background, freeze the player if required 417 #TODO: do this in the background, freeze the player if required
383 sync_job { 418 sync_job {
384 my ($map, $x, $y); 419 my ($map, $x, $y);
385 420
386 unless ($exit) { 421 if ($exit->slaying eq "/!") {
387 # used on login only(?) 422 prepare_random_map $exit;
388 $map = cf::map::find_map_nb $ob->contr->maplevel; 423 }
389 ($x, $y) = ($ob->x, $ob->y); 424
390 } else {
391 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;
392 426
393 $map = cf::map::find_map_nb $path->as_string; 427 $map = cf::map::find_map $path->as_string;
394 $map = $map->customise_for ($ob) if $map; 428 $map = $map->customise_for ($ob) if $map;
395 ($x, $y) = ($exit->stats->hp, $exit->stats->sp); 429 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
396 }
397 430
398 unless ($map) { 431 unless ($map) {
399 $map = cf::map::find_map_nb $emergency_position->[0]
400 or die "FATAL: cannot load emergency map\n";
401 $x = $emergency_position->[1];
402 $y = $emergency_position->[2];
403 }
404
405 if ($map) {
406 warn "entering ", $map->path, " at ($x, $y)\n";#d#
407 $map->do_load_nb;
408 $ob->enter_map ($map, $x, $y);
409 } else {
410 $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);
433
434 # restore original map position
435 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
436
437 unless ($map) {
438 $map = cf::map::find_map $emergency_position->[0]
439 or die "FATAL: cannot load emergency map\n";
440 $x = $emergency_position->[1];
441 $y = $emergency_position->[2];
411 } 442 }
443 }
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#
450 $map->load;
451 $ob->enter_map ($map, $x, $y);
412 } 452 }
413} 453}
414 454
415sub cf::map::customise_for { 455sub cf::map::customise_for {
416 my ($map, $ob) = @_; 456 my ($map, $ob) = @_;
417 457
418 if ($map->per_player) { 458 if ($map->per_player) {
419 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; 459 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
420 } 460 }
421 461
422 $map 462 $map
423} 463}
424 464

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines