ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
Revision: 1.7
Committed: Sat Dec 30 21:15:59 2006 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.6: +13 -21 lines
Log Message:
get rid of annoying enter_exit (0)

File Contents

# Content
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 (Coro::async {
136 unless (write_runtime) {
137 warn "unable to write runtime file: $!";
138 exit 1;
139 }
140 })->prio (Coro::PRIO_MAX);
141
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 next if $map->players;
153 my $last_access = $map->last_access;
154 # not yet, because maps might become visible to players nearby
155 # we need a tiled meta map for this to work
156 # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
157 # $map->deactivate;
158 # delete $map->{active};
159 # }
160 if ($map->should_reset) {
161 $map->reset;
162 } elsif ($last_access + $SWAP_TIMEOUT <= $cf::RUNTIME) {
163 $map->swap_out;
164 Coro::Timer::sleep $SAVE_INTERVAL;
165 } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
166 $map->save;
167 Coro::Timer::sleep $SAVE_INTERVAL;
168 }
169 };
170 warn $@ if $@;
171 cede;
172 }
173 }
174 };
175 $SCHEDULER->prio (-2);
176
177 sub sync_job(&) {
178 my ($job) = @_;
179
180 my $done;
181 my @res;
182
183 Carp::confess "nested sync_job" if $cf::FREEZE;
184
185 local $cf::FREEZE = 1;
186
187 (Coro::async {
188 @res = eval { $job->() };
189 warn $@ if $@;
190 $done = 1;
191 })->prio (Coro::PRIO_MAX);
192
193 while (!$done) {
194 Coro::cede_notself;
195 Event::one_event unless Coro::nready;
196 }
197
198 wantarray ? @res : $res[0]
199 }
200
201 # and all this just because we cannot iterate over
202 # all maps in C++...
203 sub cf::map::change_all_map_light {
204 my ($change) = @_;
205
206 $_->change_map_light ($change) for values %cf::MAP;
207 }
208
209 sub try_load_header($) {
210 my ($path) = @_;
211
212 utf8::encode $path;
213 aio_open $path, O_RDONLY, 0
214 or return;
215
216 my $map = cf::map::new
217 or return;
218
219 $map->load_header ($path)
220 or return;
221
222 $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
223 $map->reset_timeout (10);#d#
224
225 $map->{load_path} = $path;
226
227 $map
228 }
229
230 sub cf::map::find_map_nb {
231 my ($path, $origin) = @_;
232
233 warn "find_map_nb<$path,$origin>\n";#d#
234
235 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
236 my $key = $path->as_string;
237
238 $cf::MAP{$key} || do {
239 # do it the slow way
240 my $map = try_load_header $path->save_path;
241
242 if (!$map) {
243 $map = try_load_header $path->load_path
244 or return;
245 }
246
247 $map or return;
248
249 $map->instantiate;
250 $map->path ($key);
251 $map->{path} = $path;
252
253 $map->per_player (0) if $path->{user_rel};
254
255 $map->reset if $map->should_reset;
256
257 $cf::MAP{$key} = $map
258 }
259 }
260
261 sub cf::map::find_map {
262 my ($path, $origin) = @_;
263
264 $path = new cf::path $path, $origin && $origin->path;
265 my $key = $path->as_string;
266
267 $cf::MAP{$key} || sync_job {
268 cf::map::find_map_nb $path;
269 }
270 }
271
272 sub cf::map::do_load_nb {
273 my ($self) = @_;
274
275 return 0 unless $self->in_memory == cf::MAP_SWAPPED;
276
277 $self->in_memory (cf::MAP_LOADING);
278
279 my $path = $self->{path};
280
281 $self->alloc;
282 $self->load_objects ($self->{load_path}, 1)
283 or return;
284
285 if (my $uniq = $path->uniq_path) {
286 utf8::encode $uniq;
287 if (aio_open $uniq, O_RDONLY, 0) {
288 $self->clear_unique_items;
289 $self->load_objects ($uniq, 0);
290 }
291 }
292
293 # now do the right thing for maps
294 $self->link_multipart_objects;
295 $self->fix_auto_apply;
296 $self->decay_objects;
297 $self->update_buttons;
298 $self->set_darkness_map;
299 $self->difficulty ($self->estimate_difficulty)
300 unless $self->difficulty;
301 $self->activate;
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 sub cf::map::swap_out {
336 my ($self) = @_;
337
338 return if $self->players;
339
340 $self->save if $self->in_memory == cf::MAP_IN_MEMORY;
341 $self->clear;
342 $self->in_memory (cf::MAP_SWAPPED);
343 }
344
345 sub cf::map::should_reset {
346 my ($map) = @_;
347
348 # TODO: safety, remove and allow resettable per-player maps
349 return if $map->{path}{user_rel};#d#
350 return if $map->per_player;
351 return unless $map->reset_timeout;
352
353 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
354
355 $time + $map->reset_timeout < $cf::RUNTIME
356 }
357
358 sub cf::map::reset {
359 my ($self) = @_;
360
361 return if $self->players;
362 return if $self->{path}{user_rel};#d#
363
364 warn "resetting map ", $self->path;#d#
365
366 utf8::encode (my $save = $self->{path}->save_path);
367 aioreq_pri 3; IO::AIO::aio_unlink $save;
368 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
369
370 $self->clear;
371 $self->in_memory (cf::MAP_SWAPPED);
372 utf8::encode ($self->{load_path} = $self->{path}->load_path);
373 }
374
375 sub cf::object::player::enter_exit {
376 my ($ob, $exit) = @_;
377
378 return unless $ob->type == cf::PLAYER;
379
380 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
381
382 #TODO: do this in the background, freeze the player if required
383 sync_job {
384 my ($map, $x, $y);
385
386 if ($exit->slaying eq "!") {
387 } else {
388 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
389
390 $map = cf::map::find_map_nb $path->as_string;
391 $map = $map->customise_for ($ob) if $map;
392 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
393 }
394
395 unless ($map) {
396 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
397
398 # restore original map position
399 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
400
401 unless ($map) {
402 $map = cf::map::find_map_nb $emergency_position->[0]
403 or die "FATAL: cannot load emergency map\n";
404 $x = $emergency_position->[1];
405 $y = $emergency_position->[2];
406 }
407 }
408
409 # use -1, -1 as default coordinates, not 0, 0
410 ($x, $y) = ($map->enter_x, $map->enter_y)
411 if $x <=0 && $y <= 0;
412
413 warn "entering ", $map->path, " at ($x, $y)\n";#d#
414 $map->do_load_nb;
415 $ob->enter_map ($map, $x, $y);
416 }
417 }
418
419 sub cf::map::customise_for {
420 my ($map, $ob) = @_;
421
422 if ($map->per_player) {
423 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path};
424 }
425
426 $map
427 }
428
429 sub cf::map::emergency_save {
430 local $cf::FREEZE = 1;
431
432 warn "enter emergency map save\n";
433
434 my $saver = async {
435 warn "begin emergency map save\n";
436 $_->save for values %cf::MAP;
437 };
438 $saver->prio (Coro::PRIO_MAX);
439 $saver->join;
440
441 warn "emergency map save drain\n";
442 Event::one_event while IO::AIO::nreqs;
443 warn "end emergency map save\n";
444 }
445