ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
Revision: 1.4
Committed: Sat Dec 30 16:56:16 2006 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.3: +30 -17 lines
Log Message:
misc updates

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     use Fcntl;
6    
7     use Coro;
8     use Coro::AIO;
9    
10     our $emergency_position = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37];
11    
12     our $DEACTIVATE_TIMEOUT = 60; # number of seconds after which maps get deactivated to save cpu
13     our $SWAP_TIMEOUT = 600; # number of seconds after which maps inactive get swapped out
14     our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs
15     our $SAVE_TIMEOUT = 60; # save maps every n seconds
16     our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD
17     our $MAX_RESET = 7200;
18    
19     $DEACTIVATE_TIMEOUT = 3;#d#
20     $SWAP_TIMEOUT = 5;#d#
21     $SCHEDULE_INTERVAL = 1;
22    
23     $cf::LINK_MAP ||= do {
24     my $map = cf::map::new;
25    
26     $map->width (1);
27     $map->height (1);
28     $map->alloc;
29     $map->path ("{link}");
30     $map->{path} = bless { path => "{link}" }, "cf::path";
31     $map->in_memory (cf::MAP_IN_MEMORY);
32    
33     $map
34     };
35    
36     {
37     package cf::path;
38    
39     sub new {
40     my ($class, $path, $base) = @_;
41    
42     my %res;
43    
44     if ($path =~ s{^~([^/]+)?}{}) {
45     $res{user_rel} = 1;
46    
47     if (defined $1) {
48     $res{user} = $1;
49     } elsif ($base =~ m{^~([^/]+)/}) {
50     $res{user} = $1;
51     } else {
52     warn "cannot resolve user-relative path without user <$path,$base>\n";
53     }
54     } elsif ($path =~ /^\//) {
55     # already absolute
56     } else {
57     $base =~ s{[^/]+/?$}{};
58     return $class->new ("$base/$path");
59     }
60    
61     for ($path) {
62     redo if s{/\.?/}{/};
63     redo if s{/[^/]+/\.\./}{/};
64     }
65    
66     $res{path} = $path;
67    
68     bless \%res, $class
69     }
70    
71     # the name / primary key / in-game path
72     sub as_string {
73     my ($self) = @_;
74    
75     $self->{user_rel}
76     ? "~$self->{user}$self->{path}"
77     : $self->{path}
78     }
79    
80     # escape the /'s in the path
81     sub escaped_path {
82     # ∕ is U+2215
83     (my $path = $_[0]{path}) =~ s/\//∕/g;
84     $path
85     }
86    
87     # the original (read-only) location
88     sub load_path {
89     my ($self) = @_;
90    
91     sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path}
92     }
93    
94     # the temporary/swap location
95     sub save_path {
96     my ($self) = @_;
97    
98     $self->{user_rel}
99     ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->escaped_path
100     : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->escaped_path
101     }
102    
103     # the unique path, might be eq to save_path
104     sub uniq_path {
105     my ($self) = @_;
106    
107     $self->{user_rel}
108     ? undef
109     : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->escaped_path
110     }
111     }
112    
113     sub write_runtime {
114     my $runtime = cf::localdir . "/runtime";
115    
116     my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
117     or return;
118    
119     my $value = $cf::RUNTIME;
120     (aio_write $fh, 0, (length $value), $value, 0) <= 0
121     and return;
122    
123     aio_fsync $fh
124     and return;
125    
126     close $fh
127     or return;
128    
129     aio_rename "$runtime~", $runtime
130     and return;
131    
132     1
133     }
134    
135 root 1.4 (Coro::async {
136 root 1.1 unless (write_runtime) {
137     warn "unable to write runtime file: $!";
138     exit 1;
139     }
140 root 1.4 })->prio (Coro::PRIO_MAX);
141 root 1.1
142     our $SCHEDULER = cf::coro {
143     while () {
144     Coro::Timer::sleep $SCHEDULE_INTERVAL;
145    
146     write_runtime
147     or warn "unable to write runtime file: $!";
148    
149     for my $map (values %cf::MAP) {
150     eval {
151     next if $map->in_memory != cf::MAP_IN_MEMORY;
152     my $last_access = $map->last_access;
153     # not yet, because maps might become visible to players nearby
154     # we need a tiled meta map for this to work
155     # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
156     # $map->deactivate;
157     # delete $map->{active};
158     # }
159     if ($map->should_reset) {
160     $map->reset;
161     } elsif ($last_access + $SWAP_TIMEOUT <= $cf::RUNTIME) {
162     $map->swap_out;
163     Coro::Timer::sleep $SAVE_INTERVAL;
164     } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
165     $map->save;
166     Coro::Timer::sleep $SAVE_INTERVAL;
167     }
168     };
169     warn $@ if $@;
170     cede;
171     }
172     }
173     };
174     $SCHEDULER->prio (-2);
175    
176     sub sync_job(&) {
177     my ($job) = @_;
178    
179     my $done;
180     my @res;
181    
182     Carp::confess "nested sync_job" if $cf::FREEZE;
183    
184     local $cf::FREEZE = 1;
185    
186 root 1.4 (Coro::async {
187 root 1.1 @res = eval { $job->() };
188     warn $@ if $@;
189     $done = 1;
190     })->prio (Coro::PRIO_MAX);
191    
192     while (!$done) {
193     Coro::cede_notself;
194     Event::one_event unless Coro::nready;
195     }
196    
197     wantarray ? @res : $res[0]
198     }
199    
200     # and all this just because we cannot iterate over
201     # all maps in C++...
202     sub cf::map::change_all_map_light {
203     my ($change) = @_;
204    
205     $_->change_map_light ($change) for values %cf::MAP;
206     }
207    
208     sub try_load_header($) {
209     my ($path) = @_;
210    
211     utf8::encode $path;
212     aio_open $path, O_RDONLY, 0
213     or return;
214    
215     my $map = cf::map::new
216     or return;
217    
218     $map->load_header ($path)
219     or return;
220    
221     $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
222     $map->reset_timeout (10);#d#
223    
224     $map->{load_path} = $path;
225    
226     $map
227     }
228    
229     sub cf::map::find_map_nb {
230     my ($path, $origin) = @_;
231    
232 root 1.3 warn "find_map_nb<$path,$origin>\n";#d#
233    
234 root 1.1 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
235     my $key = $path->as_string;
236    
237     $cf::MAP{$key} || do {
238     # do it the slow way
239     my $map = try_load_header $path->save_path;
240    
241     if (!$map) {
242     $map = try_load_header $path->load_path
243     or return;
244     }
245    
246     $map or return;
247    
248     $map->instantiate;
249     $map->path ($key);
250     $map->{path} = $path;
251    
252 root 1.3 $map->per_player (0) if $path->{user_rel};
253    
254 root 1.1 $map->reset if $map->should_reset;
255    
256     $cf::MAP{$key} = $map
257     }
258     }
259    
260     sub 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    
273     sub cf::map::do_load_nb {
274     my ($self) = @_;
275    
276     return 0 unless $self->in_memory == cf::MAP_SWAPPED;
277    
278     $self->in_memory (cf::MAP_LOADING);
279    
280     my $path = $self->{path};
281    
282     $self->alloc;
283     $self->load_objects ($self->{load_path}, 1)
284     or return;
285    
286     if (my $uniq = $path->uniq_path) {
287     utf8::encode $uniq;
288     if (aio_open $uniq, O_RDONLY, 0) {
289     $self->clear_unique_items;
290     $self->load_objects ($uniq, 0);
291     }
292     }
293    
294     # now do the right thing for maps
295     $self->link_multipart_objects;
296     $self->fix_auto_apply;
297     $self->decay_objects;
298     $self->update_buttons;
299     $self->set_darkness_map;
300     $self->difficulty ($self->estimate_difficulty)
301     unless $self->difficulty;
302    
303     $self->in_memory (cf::MAP_IN_MEMORY);
304     }
305    
306     sub cf::map::do_load {
307     my ($self) = @_;
308    
309     warn "do_load<$self>\n";#d#
310    
311     sync_job {
312     cf::map::do_load_nb $self;
313     };
314     }
315    
316     sub cf::map::save {
317     my ($self) = @_;
318    
319     warn "saving map ", $self->path;
320    
321     my $save = $self->{path}->save_path; utf8::encode $save;
322     my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
323    
324     if ($uniq) {
325     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
326     $self->save_objects ($uniq, cf::IO_UNIQUES);
327     } else {
328     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
329     }
330    
331     $self->{load_path} = $save;
332     $self->{last_save} = $cf::RUNTIME;
333     }
334    
335 root 1.2 sub cf::map::swap_out {
336     my ($self) = @_;
337    
338 root 1.3 $self->save if $self->in_memory == cf::MAP_IN_MEMORY;
339 root 1.2 $self->clear;
340     $self->in_memory (cf::MAP_SWAPPED);
341     }
342    
343 root 1.1 sub cf::map::should_reset {
344     my ($map) = @_;
345    
346     # TODO: safety, remove and allow resettable per-player maps
347     return if $map->{path}{user_rel};#d#
348     return if $map->per_player;
349     return unless $map->reset_timeout;
350    
351     my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
352    
353     $time + $map->reset_timeout < $cf::RUNTIME
354     }
355    
356     sub cf::map::reset {
357     my ($self) = @_;
358    
359     return if $self->players;
360 root 1.3 return if $self->{path}{user_rel};#d#
361 root 1.1
362     warn "resetting map ", $self->path;#d#
363 root 1.3
364     utf8::encode (my $save = $self->{path}->save_path);
365     aioreq_pri 3; IO::AIO::aio_unlink $save;
366     aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
367 root 1.1
368     $self->clear;
369     $self->in_memory (cf::MAP_SWAPPED);
370 root 1.3 utf8::encode ($self->{load_path} = $self->{path}->load_path);
371 root 1.1 }
372    
373     sub cf::object::player::enter_exit {
374     my ($ob, $exit) = @_;
375    
376 root 1.4 # if at login, move to interim map immediately
377     unless ($exit) {
378     # used on login only
379     $ob->enter_map ($LINK_MAP, 0, 0);
380     }
381    
382     #TODO: do this in the background, freeze the player if required
383 root 1.1 sync_job {
384 root 1.3 my ($map, $x, $y);
385 root 1.1
386 root 1.4 unless ($exit) {
387     # used on login only(?)
388     $map = cf::map::find_map_nb $ob->contr->maplevel;
389     ($x, $y) = ($ob->x, $ob->y);
390     } else {
391 root 1.1 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
392    
393 root 1.3 $map = cf::map::find_map_nb $path->as_string;
394 root 1.1 $map = $map->customise_for ($ob) if $map;
395 root 1.3 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
396 root 1.4 }
397 root 1.1
398 root 1.4 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 root 1.3 }
404 root 1.1
405 root 1.3 if ($map) {
406     warn "entering ", $map->path, " at ($x, $y)\n";#d#
407 root 1.1 $map->do_load_nb;
408     $ob->enter_map ($map, $x, $y);
409 root 1.3 } else {
410     $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
411 root 1.1 }
412     }
413     }
414    
415     sub cf::map::customise_for {
416     my ($map, $ob) = @_;
417    
418     if ($map->per_player) {
419     return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path};
420     }
421    
422     $map
423     }
424    
425     sub cf::map::emergency_save {
426     local $cf::FREEZE = 1;
427    
428 root 1.4 warn "enter emergency map save\n";
429    
430     my $saver = async {
431     warn "begin emergency map save\n";
432     $_->save for values %cf::MAP;
433     };
434     $saver->prio (Coro::PRIO_MAX);
435     $saver->join;
436    
437 root 1.1 warn "emergency map save drain\n";
438     Event::one_event while IO::AIO::nreqs;
439     warn "end emergency map save\n";
440     }
441