ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
Revision: 1.9
Committed: Sun Dec 31 17:17:22 2006 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.8: +134 -94 lines
Log Message:
many minor changes everywhere, random maps crash sometimes but design is in place

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     }
135    
136     sub write_runtime {
137     my $runtime = cf::localdir . "/runtime";
138    
139     my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
140     or return;
141    
142     my $value = $cf::RUNTIME;
143     (aio_write $fh, 0, (length $value), $value, 0) <= 0
144     and return;
145    
146     aio_fsync $fh
147     and return;
148    
149     close $fh
150     or return;
151    
152     aio_rename "$runtime~", $runtime
153     and return;
154    
155     1
156     }
157    
158 root 1.4 (Coro::async {
159 root 1.1 unless (write_runtime) {
160     warn "unable to write runtime file: $!";
161     exit 1;
162     }
163 root 1.4 })->prio (Coro::PRIO_MAX);
164 root 1.1
165     our $SCHEDULER = cf::coro {
166 root 1.8 Coro::Timer::sleep 3600;#d#TODO#for debugging only
167 root 1.1 while () {
168     Coro::Timer::sleep $SCHEDULE_INTERVAL;
169    
170     write_runtime
171     or warn "unable to write runtime file: $!";
172    
173     for my $map (values %cf::MAP) {
174     eval {
175     next if $map->in_memory != cf::MAP_IN_MEMORY;
176 root 1.6 next if $map->players;
177 root 1.1 my $last_access = $map->last_access;
178     # not yet, because maps might become visible to players nearby
179     # we need a tiled meta map for this to work
180     # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
181     # $map->deactivate;
182     # delete $map->{active};
183     # }
184     if ($map->should_reset) {
185     $map->reset;
186     } elsif ($last_access + $SWAP_TIMEOUT <= $cf::RUNTIME) {
187     $map->swap_out;
188     Coro::Timer::sleep $SAVE_INTERVAL;
189     } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
190     $map->save;
191     Coro::Timer::sleep $SAVE_INTERVAL;
192     }
193     };
194     warn $@ if $@;
195     cede;
196     }
197     }
198     };
199     $SCHEDULER->prio (-2);
200    
201 root 1.8 sub generate_random_map {
202     my ($path, $rmp) = @_;
203    
204     # mit "rum" bekleckert, nicht
205     cf::map::_create_random_map
206     $path,
207     $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
208     $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
209     $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
210     $rmp->{exit_on_final_map},
211     $rmp->{xsize}, $rmp->{ysize},
212     $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
213     $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
214     $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
215     $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
216     $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
217     $rmp->{region}
218     }
219    
220     sub parse_random_map_params {
221     my ($spec) = @_;
222    
223 root 1.9 my $rmp = { # defaults
224     xsize => 10,
225     ysize => 10,
226     };
227 root 1.8
228     for (split /\n/, $spec) {
229     my ($k, $v) = split /\s+/, $_, 2;
230    
231     $rmp->{lc $k} = $v if (length $k) && (length $v);
232     }
233    
234     $rmp
235     }
236    
237     sub prepare_random_map {
238     my ($exit) = @_;
239    
240 root 1.9 # all this does is basically rpelace the /! path by
241     # a new random map path (?random/...) with a seed
242     # that depends on the exit object
243    
244 root 1.8 my $rmp = parse_random_map_params $exit->msg;
245    
246 root 1.9 if ($exit->map) {
247     $rmp->{region} = $exit->map->region ? $exit->map->region->name : undef;
248     $rmp->{origin_map} = $exit->map->path;
249     $rmp->{origin_x} = $exit->x;
250     $rmp->{origin_y} = $exit->y;
251     }
252 root 1.1
253 root 1.9 $rmp->{random_seed} = $exit->random_seed;
254 root 1.1
255 root 1.9 $exit->slaying ("?random/" . cf::to_json $rmp);
256     $exit->msg (undef);
257 root 1.1 }
258    
259     # and all this just because we cannot iterate over
260     # all maps in C++...
261     sub cf::map::change_all_map_light {
262     my ($change) = @_;
263    
264     $_->change_map_light ($change) for values %cf::MAP;
265     }
266    
267     sub try_load_header($) {
268     my ($path) = @_;
269    
270     utf8::encode $path;
271     aio_open $path, O_RDONLY, 0
272     or return;
273    
274     my $map = cf::map::new
275     or return;
276    
277     $map->load_header ($path)
278     or return;
279    
280     $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
281     $map->reset_timeout (10);#d#
282    
283     $map->{load_path} = $path;
284    
285     $map
286     }
287    
288 root 1.8 sub cf::map::find_map {
289 root 1.1 my ($path, $origin) = @_;
290    
291 root 1.8 warn "find_map<$path,$origin>\n";#d#
292 root 1.3
293 root 1.1 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
294     my $key = $path->as_string;
295    
296     $cf::MAP{$key} || do {
297     # do it the slow way
298     my $map = try_load_header $path->save_path;
299    
300     if (!$map) {
301 root 1.9 if (my $rmp = $path->random_map_params) {
302     $map = generate_random_map $key, $rmp;
303     } else {
304     $map = try_load_header $path->load_path;
305     }
306    
307     $map or return;
308 root 1.8
309 root 1.9 $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout;
310 root 1.8 $map->instantiate;
311    
312     # per-player maps become, after loading, normal maps
313     $map->per_player (0) if $path->{user_rel};
314 root 1.1 }
315    
316     $map->path ($key);
317     $map->{path} = $path;
318    
319     $map->reset if $map->should_reset;
320    
321     $cf::MAP{$key} = $map
322     }
323     }
324    
325 root 1.8 sub cf::map::load {
326 root 1.1 my ($self) = @_;
327    
328 root 1.8 return if $self->in_memory != cf::MAP_SWAPPED;
329 root 1.1
330     $self->in_memory (cf::MAP_LOADING);
331    
332     my $path = $self->{path};
333    
334     $self->alloc;
335     $self->load_objects ($self->{load_path}, 1)
336     or return;
337    
338     if (my $uniq = $path->uniq_path) {
339     utf8::encode $uniq;
340     if (aio_open $uniq, O_RDONLY, 0) {
341     $self->clear_unique_items;
342     $self->load_objects ($uniq, 0);
343     }
344     }
345    
346     # now do the right thing for maps
347     $self->link_multipart_objects;
348     $self->fix_auto_apply;
349     $self->decay_objects;
350     $self->update_buttons;
351     $self->set_darkness_map;
352     $self->difficulty ($self->estimate_difficulty)
353     unless $self->difficulty;
354 elmex 1.5 $self->activate;
355 root 1.1
356     $self->in_memory (cf::MAP_IN_MEMORY);
357     }
358    
359 root 1.8 sub cf::map::load_map_sync {
360     my ($path, $origin) = @_;
361 root 1.1
362 root 1.8 warn "load_map_sync<$path, $origin>\n";#d#
363 root 1.1
364 root 1.9 cf::abort if $path =~ /CVS/;#d#
365    
366     cf::sync_job {
367 root 1.8 my $map = cf::map::find_map $path, $origin
368     or return;
369     $map->load;
370     $map
371     }
372 root 1.1 }
373    
374     sub cf::map::save {
375     my ($self) = @_;
376    
377     my $save = $self->{path}->save_path; utf8::encode $save;
378     my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
379    
380 root 1.9 $self->{last_save} = $cf::RUNTIME;
381    
382     return unless $self->dirty;
383    
384     $self->{load_path} = $save;
385    
386     return if $self->{path}{path} =~ /^\/styles\//;
387    
388     warn "saving map ", $self->path;
389    
390 root 1.1 if ($uniq) {
391     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
392     $self->save_objects ($uniq, cf::IO_UNIQUES);
393     } else {
394     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
395     }
396     }
397    
398 root 1.2 sub cf::map::swap_out {
399     my ($self) = @_;
400    
401 root 1.6 return if $self->players;
402 root 1.8 return if $self->in_memory != cf::MAP_IN_MEMORY;
403 root 1.6
404 root 1.8 $self->save;
405 root 1.2 $self->clear;
406     $self->in_memory (cf::MAP_SWAPPED);
407     }
408    
409 root 1.1 sub cf::map::should_reset {
410     my ($map) = @_;
411    
412 root 1.9 return;#d#
413 root 1.1 # TODO: safety, remove and allow resettable per-player maps
414     return if $map->{path}{user_rel};#d#
415     return unless $map->reset_timeout;
416    
417 root 1.9 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
418 root 1.1
419     $time + $map->reset_timeout < $cf::RUNTIME
420     }
421    
422     sub cf::map::reset {
423     my ($self) = @_;
424    
425     return if $self->players;
426 root 1.3 return if $self->{path}{user_rel};#d#
427 root 1.1
428     warn "resetting map ", $self->path;#d#
429 root 1.9 return;#d#
430 root 1.3
431     utf8::encode (my $save = $self->{path}->save_path);
432     aioreq_pri 3; IO::AIO::aio_unlink $save;
433     aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
434 root 1.1
435     $self->clear;
436     $self->in_memory (cf::MAP_SWAPPED);
437 root 1.3 utf8::encode ($self->{load_path} = $self->{path}->load_path);
438 root 1.1 }
439    
440     sub cf::object::player::enter_exit {
441     my ($ob, $exit) = @_;
442    
443 root 1.6 return unless $ob->type == cf::PLAYER;
444    
445     my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
446    
447 root 1.9 $ob->enter_map ($LINK_MAP, 20, 20);
448     $ob->deactivate_recursive;
449    
450     (Coro::async {
451 root 1.3 my ($map, $x, $y);
452 root 1.9 unless (eval {
453 root 1.1
454 root 1.9 prepare_random_map $exit
455     if $exit->slaying eq "/!";
456 root 1.1
457 root 1.9 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
458 root 1.8
459 root 1.9 $map = cf::map::find_map $path->as_string;
460     $map = $map->customise_for ($ob) if $map;
461     ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
462 root 1.8
463 root 1.9 unless ($map) {
464     $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
465 root 1.7
466 root 1.9 # restore original map position
467     ($map, $x, $y) = ($oldmap, $oldx, $oldy);
468 root 1.7
469 root 1.9 unless ($map) {
470     $map = cf::map::find_map $emergency_position->[0]
471     or die "FATAL: cannot load emergency map\n";
472     $x = $emergency_position->[1];
473     $y = $emergency_position->[2];
474     }
475 root 1.7 }
476 root 1.1
477 root 1.9 # use -1, -1 as default coordinates, not 0, 0
478     ($x, $y) = ($map->enter_x, $map->enter_y)
479     if $x <=0 && $y <= 0;
480    
481     warn "entering ", $map->path, " at ($x, $y)\n";#d#
482     $map->load;
483     1;
484     }) {
485     ($map, $x, $y) = ($oldmap, $oldx, $oldy);
486    
487     $ob->message ("Something went wrong within the server. "
488     . "I'll try to bring you back to the map you were before. "
489     . "Please report this to the dungeon master",
490     cf::NDI_UNIQUE | cf::NDI_RED);
491 root 1.7
492 root 1.9 warn "ERROR in enter_exit: $@";
493     }
494     $ob->activate_recursive;
495 root 1.6 $ob->enter_map ($map, $x, $y);
496 root 1.9 })->prio (1);
497 root 1.1 }
498    
499     sub cf::map::customise_for {
500     my ($map, $ob) = @_;
501    
502     if ($map->per_player) {
503 root 1.8 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
504 root 1.1 }
505    
506     $map
507     }
508    
509     sub cf::map::emergency_save {
510     local $cf::FREEZE = 1;
511    
512 root 1.4 warn "enter emergency map save\n";
513    
514 root 1.9 cf::sync_job {
515 root 1.4 warn "begin emergency map save\n";
516     $_->save for values %cf::MAP;
517     };
518    
519 root 1.1 warn "end emergency map save\n";
520     }
521