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.5 by elmex, Sat Dec 30 17:00:44 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
302 $self->activate; 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
334} 368}
335 369
336sub cf::map::swap_out { 370sub cf::map::swap_out {
337 my ($self) = @_; 371 my ($self) = @_;
338 372
373 return if $self->players;
339 $self->save if $self->in_memory == cf::MAP_IN_MEMORY; 374 return if $self->in_memory != cf::MAP_IN_MEMORY;
375
376 $self->save;
340 $self->clear; 377 $self->clear;
341 $self->in_memory (cf::MAP_SWAPPED); 378 $self->in_memory (cf::MAP_SWAPPED);
342} 379}
343 380
344sub cf::map::should_reset { 381sub cf::map::should_reset {
345 my ($map) = @_; 382 my ($map) = @_;
346 383
347 # TODO: safety, remove and allow resettable per-player maps 384 # TODO: safety, remove and allow resettable per-player maps
348 return if $map->{path}{user_rel};#d# 385 return if $map->{path}{user_rel};#d#
349 return if $map->per_player;
350 return unless $map->reset_timeout; 386 return unless $map->reset_timeout;
351 387
352 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 388 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
353 389
354 $time + $map->reset_timeout < $cf::RUNTIME 390 $time + $map->reset_timeout < $cf::RUNTIME
372} 408}
373 409
374sub cf::object::player::enter_exit { 410sub cf::object::player::enter_exit {
375 my ($ob, $exit) = @_; 411 my ($ob, $exit) = @_;
376 412
377 # if at login, move to interim map immediately 413 return unless $ob->type == cf::PLAYER;
378 unless ($exit) { 414
379 # used on login only 415 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
380 $ob->enter_map ($LINK_MAP, 0, 0);
381 }
382 416
383 #TODO: do this in the background, freeze the player if required 417 #TODO: do this in the background, freeze the player if required
384 sync_job { 418 sync_job {
385 my ($map, $x, $y); 419 my ($map, $x, $y);
386 420
387 unless ($exit) { 421 if ($exit->slaying eq "/!") {
388 # used on login only(?) 422 prepare_random_map $exit;
389 $map = cf::map::find_map_nb $ob->contr->maplevel; 423 }
390 ($x, $y) = ($ob->x, $ob->y); 424
391 } else {
392 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;
393 426
394 $map = cf::map::find_map_nb $path->as_string; 427 $map = cf::map::find_map $path->as_string;
395 $map = $map->customise_for ($ob) if $map; 428 $map = $map->customise_for ($ob) if $map;
396 ($x, $y) = ($exit->stats->hp, $exit->stats->sp); 429 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
397 }
398 430
399 unless ($map) { 431 unless ($map) {
400 $map = cf::map::find_map_nb $emergency_position->[0]
401 or die "FATAL: cannot load emergency map\n";
402 $x = $emergency_position->[1];
403 $y = $emergency_position->[2];
404 }
405
406 if ($map) {
407 warn "entering ", $map->path, " at ($x, $y)\n";#d#
408 $map->do_load_nb;
409 $ob->enter_map ($map, $x, $y);
410 } else {
411 $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];
412 } 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);
413 } 452 }
414} 453}
415 454
416sub cf::map::customise_for { 455sub cf::map::customise_for {
417 my ($map, $ob) = @_; 456 my ($map, $ob) = @_;
418 457
419 if ($map->per_player) { 458 if ($map->per_player) {
420 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; 459 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
421 } 460 }
422 461
423 $map 462 $map
424} 463}
425 464

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines