ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
Revision: 1.10
Committed: Sun Dec 31 18:10:40 2006 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.9: +16 -8 lines
Log Message:
- style maps need very special treatment
- cf has a nonzero chance of crashing in a random map

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     # why this is an extension is a good question. next question.
4    
5 root 1.9 use Digest::MD5;
6 root 1.1 use Fcntl;
7    
8     use Coro;
9     use Coro::AIO;
10    
11     our $emergency_position = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37];
12    
13     our $DEACTIVATE_TIMEOUT = 60; # number of seconds after which maps get deactivated to save cpu
14     our $SWAP_TIMEOUT = 600; # number of seconds after which maps inactive get swapped out
15     our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs
16     our $SAVE_TIMEOUT = 60; # save maps every n seconds
17     our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD
18     our $MAX_RESET = 7200;
19    
20 root 1.9 our $RANDOM_MAPS = cf::localdir . "/random";
21     mkdir $RANDOM_MAPS;
22    
23 root 1.1 $DEACTIVATE_TIMEOUT = 3;#d#
24     $SWAP_TIMEOUT = 5;#d#
25     $SCHEDULE_INTERVAL = 1;
26    
27     $cf::LINK_MAP ||= do {
28     my $map = cf::map::new;
29    
30 root 1.9 $map->width (41);
31     $map->height (41);
32 root 1.1 $map->alloc;
33     $map->path ("{link}");
34     $map->{path} = bless { path => "{link}" }, "cf::path";
35     $map->in_memory (cf::MAP_IN_MEMORY);
36    
37     $map
38     };
39    
40     {
41     package cf::path;
42    
43     sub new {
44     my ($class, $path, $base) = @_;
45    
46 root 1.9 my $self = bless { }, $class;
47 root 1.1
48 root 1.9 if ($path =~ s{^\?random/}{}) {
49     $self->{random} = cf::from_json $path;
50     } else {
51     if ($path =~ s{^~([^/]+)?}{}) {
52     $self->{user_rel} = 1;
53 root 1.1
54 root 1.9 if (defined $1) {
55     $self->{user} = $1;
56     } elsif ($base =~ m{^~([^/]+)/}) {
57     $self->{user} = $1;
58     } else {
59     warn "cannot resolve user-relative path without user <$path,$base>\n";
60     }
61     } elsif ($path =~ /^\//) {
62     # already absolute
63 root 1.1 } else {
64 root 1.9 $base =~ s{[^/]+/?$}{};
65     return $class->new ("$base/$path");
66 root 1.1 }
67    
68 root 1.9 for ($path) {
69     redo if s{/\.?/}{/};
70     redo if s{/[^/]+/\.\./}{/};
71     }
72 root 1.1 }
73    
74 root 1.9 $self->{path} = $path;
75 root 1.1
76 root 1.9 $self
77 root 1.1 }
78    
79     # the name / primary key / in-game path
80     sub as_string {
81     my ($self) = @_;
82    
83 root 1.9 $self->{user_rel} ? "~$self->{user}$self->{path}"
84     : $self->{random} ? "?random/$self->{path}"
85     : $self->{path}
86     }
87    
88     # the displayed name, this is a one way mapping
89     sub visible_name {
90     my ($self) = @_;
91    
92     $self->{random} ? "?random/$self->{random}{origin_map}+$self->{random}{origin_x}+$self->{random}{origin_y}/$self->{random}{dungeon_level}"
93     : $self->as_string
94 root 1.1 }
95    
96     # escape the /'s in the path
97 root 1.9 sub _escaped_path {
98 root 1.1 # ∕ is U+2215
99     (my $path = $_[0]{path}) =~ s/\//∕/g;
100     $path
101     }
102    
103     # the original (read-only) location
104     sub load_path {
105     my ($self) = @_;
106    
107     sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path}
108     }
109    
110     # the temporary/swap location
111     sub save_path {
112     my ($self) = @_;
113    
114 root 1.9 $self->{user_rel} ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->_escaped_path
115     : $self->{random} ? sprintf "%s/%s", $RANDOM_MAPS, Digest::MD5::md5_hex $self->{path}
116     : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->_escaped_path
117 root 1.1 }
118    
119     # the unique path, might be eq to save_path
120     sub uniq_path {
121     my ($self) = @_;
122    
123 root 1.9 $self->{user_rel} || $self->{random}
124 root 1.1 ? undef
125 root 1.9 : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path
126     }
127    
128     # return random map parameters, or undef
129     sub random_map_params {
130     my ($self) = @_;
131    
132     $self->{random}
133 root 1.1 }
134 root 1.10
135     # this is somewhat ugly, but style maps do need special treatment
136     sub is_style_map {
137     $_[0]{path} =~ m{^/styles/}
138     }
139 root 1.1 }
140    
141     sub write_runtime {
142     my $runtime = cf::localdir . "/runtime";
143    
144     my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
145     or return;
146    
147     my $value = $cf::RUNTIME;
148     (aio_write $fh, 0, (length $value), $value, 0) <= 0
149     and return;
150    
151     aio_fsync $fh
152     and return;
153    
154     close $fh
155     or return;
156    
157     aio_rename "$runtime~", $runtime
158     and return;
159    
160     1
161     }
162    
163 root 1.4 (Coro::async {
164 root 1.1 unless (write_runtime) {
165     warn "unable to write runtime file: $!";
166     exit 1;
167     }
168 root 1.4 })->prio (Coro::PRIO_MAX);
169 root 1.1
170     our $SCHEDULER = cf::coro {
171 root 1.8 Coro::Timer::sleep 3600;#d#TODO#for debugging only
172 root 1.1 while () {
173     Coro::Timer::sleep $SCHEDULE_INTERVAL;
174    
175     write_runtime
176     or warn "unable to write runtime file: $!";
177    
178     for my $map (values %cf::MAP) {
179     eval {
180     next if $map->in_memory != cf::MAP_IN_MEMORY;
181 root 1.6 next if $map->players;
182 root 1.1 my $last_access = $map->last_access;
183     # not yet, because maps might become visible to players nearby
184     # we need a tiled meta map for this to work
185     # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
186     # $map->deactivate;
187     # delete $map->{active};
188     # }
189     if ($map->should_reset) {
190     $map->reset;
191     } elsif ($last_access + $SWAP_TIMEOUT <= $cf::RUNTIME) {
192     $map->swap_out;
193     Coro::Timer::sleep $SAVE_INTERVAL;
194     } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
195     $map->save;
196     Coro::Timer::sleep $SAVE_INTERVAL;
197     }
198     };
199     warn $@ if $@;
200     cede;
201     }
202     }
203     };
204     $SCHEDULER->prio (-2);
205    
206 root 1.8 sub generate_random_map {
207     my ($path, $rmp) = @_;
208    
209     # mit "rum" bekleckert, nicht
210     cf::map::_create_random_map
211     $path,
212     $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
213     $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
214     $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
215     $rmp->{exit_on_final_map},
216     $rmp->{xsize}, $rmp->{ysize},
217     $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
218     $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
219     $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
220     $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
221     $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
222     $rmp->{region}
223     }
224    
225     sub parse_random_map_params {
226     my ($spec) = @_;
227    
228 root 1.9 my $rmp = { # defaults
229     xsize => 10,
230     ysize => 10,
231     };
232 root 1.8
233     for (split /\n/, $spec) {
234     my ($k, $v) = split /\s+/, $_, 2;
235    
236     $rmp->{lc $k} = $v if (length $k) && (length $v);
237     }
238    
239     $rmp
240     }
241    
242     sub prepare_random_map {
243     my ($exit) = @_;
244    
245 root 1.9 # all this does is basically rpelace the /! path by
246     # a new random map path (?random/...) with a seed
247     # that depends on the exit object
248    
249 root 1.8 my $rmp = parse_random_map_params $exit->msg;
250    
251 root 1.9 if ($exit->map) {
252     $rmp->{region} = $exit->map->region ? $exit->map->region->name : undef;
253     $rmp->{origin_map} = $exit->map->path;
254     $rmp->{origin_x} = $exit->x;
255     $rmp->{origin_y} = $exit->y;
256     }
257 root 1.1
258 root 1.9 $rmp->{random_seed} = $exit->random_seed;
259 root 1.1
260 root 1.9 $exit->slaying ("?random/" . cf::to_json $rmp);
261     $exit->msg (undef);
262 root 1.1 }
263    
264     # and all this just because we cannot iterate over
265     # all maps in C++...
266     sub cf::map::change_all_map_light {
267     my ($change) = @_;
268    
269     $_->change_map_light ($change) for values %cf::MAP;
270     }
271    
272     sub try_load_header($) {
273     my ($path) = @_;
274    
275     utf8::encode $path;
276     aio_open $path, O_RDONLY, 0
277     or return;
278    
279     my $map = cf::map::new
280     or return;
281    
282     $map->load_header ($path)
283     or return;
284    
285     $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
286     $map->reset_timeout (10);#d#
287    
288     $map->{load_path} = $path;
289    
290     $map
291     }
292    
293 root 1.8 sub cf::map::find_map {
294 root 1.1 my ($path, $origin) = @_;
295    
296 root 1.8 warn "find_map<$path,$origin>\n";#d#
297 root 1.3
298 root 1.1 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
299     my $key = $path->as_string;
300    
301     $cf::MAP{$key} || do {
302     # do it the slow way
303     my $map = try_load_header $path->save_path;
304    
305     if (!$map) {
306 root 1.9 if (my $rmp = $path->random_map_params) {
307     $map = generate_random_map $key, $rmp;
308     } else {
309     $map = try_load_header $path->load_path;
310     }
311    
312     $map or return;
313 root 1.8
314 root 1.9 $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout;
315 root 1.8 $map->instantiate;
316    
317     # per-player maps become, after loading, normal maps
318     $map->per_player (0) if $path->{user_rel};
319 root 1.1 }
320    
321     $map->path ($key);
322     $map->{path} = $path;
323    
324     $map->reset if $map->should_reset;
325    
326     $cf::MAP{$key} = $map
327     }
328     }
329    
330 root 1.8 sub cf::map::load {
331 root 1.1 my ($self) = @_;
332    
333 root 1.8 return if $self->in_memory != cf::MAP_SWAPPED;
334 root 1.1
335     $self->in_memory (cf::MAP_LOADING);
336    
337     my $path = $self->{path};
338    
339     $self->alloc;
340     $self->load_objects ($self->{load_path}, 1)
341     or return;
342    
343     if (my $uniq = $path->uniq_path) {
344     utf8::encode $uniq;
345     if (aio_open $uniq, O_RDONLY, 0) {
346     $self->clear_unique_items;
347     $self->load_objects ($uniq, 0);
348     }
349     }
350    
351     # now do the right thing for maps
352     $self->link_multipart_objects;
353 root 1.10
354     unless ($self->{path}->is_style_map) {
355     $self->fix_auto_apply;
356     $self->decay_objects;
357     $self->update_buttons;
358     $self->set_darkness_map;
359     $self->difficulty ($self->estimate_difficulty)
360     unless $self->difficulty;
361     $self->activate;
362     }
363 root 1.1
364     $self->in_memory (cf::MAP_IN_MEMORY);
365     }
366    
367 root 1.8 sub cf::map::load_map_sync {
368     my ($path, $origin) = @_;
369 root 1.1
370 root 1.8 warn "load_map_sync<$path, $origin>\n";#d#
371 root 1.1
372 root 1.9 cf::abort if $path =~ /CVS/;#d#
373    
374     cf::sync_job {
375 root 1.8 my $map = cf::map::find_map $path, $origin
376     or return;
377     $map->load;
378     $map
379     }
380 root 1.1 }
381    
382     sub cf::map::save {
383     my ($self) = @_;
384    
385     my $save = $self->{path}->save_path; utf8::encode $save;
386     my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
387    
388 root 1.9 $self->{last_save} = $cf::RUNTIME;
389    
390     return unless $self->dirty;
391    
392     $self->{load_path} = $save;
393    
394 root 1.10 return if $self->{path}->is_style_map;
395 root 1.9
396     warn "saving map ", $self->path;
397    
398 root 1.1 if ($uniq) {
399     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
400     $self->save_objects ($uniq, cf::IO_UNIQUES);
401     } else {
402     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
403     }
404     }
405    
406 root 1.2 sub cf::map::swap_out {
407     my ($self) = @_;
408    
409 root 1.6 return if $self->players;
410 root 1.8 return if $self->in_memory != cf::MAP_IN_MEMORY;
411 root 1.6
412 root 1.8 $self->save;
413 root 1.2 $self->clear;
414     $self->in_memory (cf::MAP_SWAPPED);
415     }
416    
417 root 1.1 sub cf::map::should_reset {
418     my ($map) = @_;
419    
420 root 1.9 return;#d#
421 root 1.1 # TODO: safety, remove and allow resettable per-player maps
422     return if $map->{path}{user_rel};#d#
423     return unless $map->reset_timeout;
424    
425 root 1.9 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
426 root 1.1
427     $time + $map->reset_timeout < $cf::RUNTIME
428     }
429    
430     sub cf::map::reset {
431     my ($self) = @_;
432    
433     return if $self->players;
434 root 1.3 return if $self->{path}{user_rel};#d#
435 root 1.1
436     warn "resetting map ", $self->path;#d#
437 root 1.9 return;#d#
438 root 1.3
439     utf8::encode (my $save = $self->{path}->save_path);
440     aioreq_pri 3; IO::AIO::aio_unlink $save;
441     aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
442 root 1.1
443     $self->clear;
444     $self->in_memory (cf::MAP_SWAPPED);
445 root 1.3 utf8::encode ($self->{load_path} = $self->{path}->load_path);
446 root 1.1 }
447    
448     sub cf::object::player::enter_exit {
449     my ($ob, $exit) = @_;
450    
451 root 1.6 return unless $ob->type == cf::PLAYER;
452    
453     my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
454    
455 root 1.9 $ob->enter_map ($LINK_MAP, 20, 20);
456     $ob->deactivate_recursive;
457    
458     (Coro::async {
459 root 1.3 my ($map, $x, $y);
460 root 1.9 unless (eval {
461 root 1.1
462 root 1.9 prepare_random_map $exit
463     if $exit->slaying eq "/!";
464 root 1.1
465 root 1.9 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
466 root 1.8
467 root 1.9 $map = cf::map::find_map $path->as_string;
468     $map = $map->customise_for ($ob) if $map;
469     ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
470 root 1.8
471 root 1.9 unless ($map) {
472     $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
473 root 1.7
474 root 1.9 # restore original map position
475     ($map, $x, $y) = ($oldmap, $oldx, $oldy);
476 root 1.7
477 root 1.9 unless ($map) {
478     $map = cf::map::find_map $emergency_position->[0]
479     or die "FATAL: cannot load emergency map\n";
480     $x = $emergency_position->[1];
481     $y = $emergency_position->[2];
482     }
483 root 1.7 }
484 root 1.1
485 root 1.9 # use -1, -1 as default coordinates, not 0, 0
486     ($x, $y) = ($map->enter_x, $map->enter_y)
487     if $x <=0 && $y <= 0;
488    
489     warn "entering ", $map->path, " at ($x, $y)\n";#d#
490     $map->load;
491     1;
492     }) {
493     ($map, $x, $y) = ($oldmap, $oldx, $oldy);
494    
495     $ob->message ("Something went wrong within the server. "
496     . "I'll try to bring you back to the map you were before. "
497     . "Please report this to the dungeon master",
498     cf::NDI_UNIQUE | cf::NDI_RED);
499 root 1.7
500 root 1.9 warn "ERROR in enter_exit: $@";
501     }
502     $ob->activate_recursive;
503 root 1.6 $ob->enter_map ($map, $x, $y);
504 root 1.9 })->prio (1);
505 root 1.1 }
506    
507     sub cf::map::customise_for {
508     my ($map, $ob) = @_;
509    
510     if ($map->per_player) {
511 root 1.8 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
512 root 1.1 }
513    
514     $map
515     }
516    
517     sub cf::map::emergency_save {
518     local $cf::FREEZE = 1;
519    
520 root 1.4 warn "enter emergency map save\n";
521    
522 root 1.9 cf::sync_job {
523 root 1.4 warn "begin emergency map save\n";
524     $_->save for values %cf::MAP;
525     };
526    
527 root 1.1 warn "end emergency map save\n";
528     }
529