ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
Revision: 1.8
Committed: Sun Dec 31 10:28:36 2006 UTC (17 years, 4 months ago) by root
Branch: MAIN
Changes since 1.7: +73 -37 lines
Log Message:
different interface design for c++/perl map handling, some random map framework

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 root 1.8 Coro::Timer::sleep 3600;#d#TODO#for debugging only
144 root 1.1 while () {
145     Coro::Timer::sleep $SCHEDULE_INTERVAL;
146    
147     write_runtime
148     or warn "unable to write runtime file: $!";
149    
150     for my $map (values %cf::MAP) {
151     eval {
152     next if $map->in_memory != cf::MAP_IN_MEMORY;
153 root 1.6 next if $map->players;
154 root 1.1 my $last_access = $map->last_access;
155     # not yet, because maps might become visible to players nearby
156     # we need a tiled meta map for this to work
157     # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
158     # $map->deactivate;
159     # delete $map->{active};
160     # }
161     if ($map->should_reset) {
162     $map->reset;
163     } elsif ($last_access + $SWAP_TIMEOUT <= $cf::RUNTIME) {
164     $map->swap_out;
165     Coro::Timer::sleep $SAVE_INTERVAL;
166     } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
167     $map->save;
168     Coro::Timer::sleep $SAVE_INTERVAL;
169     }
170     };
171     warn $@ if $@;
172     cede;
173     }
174     }
175     };
176     $SCHEDULER->prio (-2);
177    
178 root 1.8 sub 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    
197     sub 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    
211     sub 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    
219 root 1.1 sub sync_job(&) {
220     my ($job) = @_;
221    
222     my $done;
223     my @res;
224    
225     Carp::confess "nested sync_job" if $cf::FREEZE;
226    
227     local $cf::FREEZE = 1;
228    
229 root 1.4 (Coro::async {
230 root 1.1 @res = eval { $job->() };
231     warn $@ if $@;
232     $done = 1;
233     })->prio (Coro::PRIO_MAX);
234    
235     while (!$done) {
236     Coro::cede_notself;
237     Event::one_event unless Coro::nready;
238     }
239    
240 root 1.8 warn "sync<@res>\n";#d#
241 root 1.1 wantarray ? @res : $res[0]
242     }
243    
244     # and all this just because we cannot iterate over
245     # all maps in C++...
246     sub cf::map::change_all_map_light {
247     my ($change) = @_;
248    
249     $_->change_map_light ($change) for values %cf::MAP;
250     }
251    
252     sub try_load_header($) {
253     my ($path) = @_;
254    
255     utf8::encode $path;
256     aio_open $path, O_RDONLY, 0
257     or return;
258    
259     my $map = cf::map::new
260     or return;
261    
262     $map->load_header ($path)
263     or return;
264    
265     $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
266     $map->reset_timeout (10);#d#
267    
268     $map->{load_path} = $path;
269    
270     $map
271     }
272    
273 root 1.8 sub cf::map::find_map {
274 root 1.1 my ($path, $origin) = @_;
275    
276 root 1.8 warn "find_map<$path,$origin>\n";#d#
277 root 1.3
278 root 1.1 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
279     my $key = $path->as_string;
280    
281     $cf::MAP{$key} || do {
282     # do it the slow way
283     my $map = try_load_header $path->save_path;
284    
285     if (!$map) {
286     $map = try_load_header $path->load_path
287     or return;
288 root 1.8
289     $map->instantiate;
290    
291     # per-player maps become, after loading, normal maps
292     $map->per_player (0) if $path->{user_rel};
293 root 1.1 }
294    
295     $map->path ($key);
296     $map->{path} = $path;
297    
298     $map->reset if $map->should_reset;
299    
300     $cf::MAP{$key} = $map
301     }
302     }
303    
304 root 1.8 sub cf::map::load {
305 root 1.1 my ($self) = @_;
306    
307 root 1.8 return if $self->in_memory != cf::MAP_SWAPPED;
308 root 1.1
309     $self->in_memory (cf::MAP_LOADING);
310    
311     my $path = $self->{path};
312    
313     $self->alloc;
314     $self->load_objects ($self->{load_path}, 1)
315     or return;
316    
317     if (my $uniq = $path->uniq_path) {
318     utf8::encode $uniq;
319     if (aio_open $uniq, O_RDONLY, 0) {
320     $self->clear_unique_items;
321     $self->load_objects ($uniq, 0);
322     }
323     }
324    
325     # now do the right thing for maps
326     $self->link_multipart_objects;
327     $self->fix_auto_apply;
328     $self->decay_objects;
329     $self->update_buttons;
330     $self->set_darkness_map;
331     $self->difficulty ($self->estimate_difficulty)
332     unless $self->difficulty;
333 elmex 1.5 $self->activate;
334 root 1.1
335     $self->in_memory (cf::MAP_IN_MEMORY);
336     }
337    
338 root 1.8 sub cf::map::load_map_sync {
339     my ($path, $origin) = @_;
340 root 1.1
341 root 1.8 warn "load_map_sync<$path, $origin>\n";#d#
342 root 1.1
343     sync_job {
344 root 1.8 my $map = cf::map::find_map $path, $origin
345     or return;
346     $map->load;
347     $map
348     }
349 root 1.1 }
350    
351     sub cf::map::save {
352     my ($self) = @_;
353    
354     warn "saving map ", $self->path;
355    
356     my $save = $self->{path}->save_path; utf8::encode $save;
357     my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
358    
359     if ($uniq) {
360     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
361     $self->save_objects ($uniq, cf::IO_UNIQUES);
362     } else {
363     $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
364     }
365    
366     $self->{load_path} = $save;
367     $self->{last_save} = $cf::RUNTIME;
368     }
369    
370 root 1.2 sub cf::map::swap_out {
371     my ($self) = @_;
372    
373 root 1.6 return if $self->players;
374 root 1.8 return if $self->in_memory != cf::MAP_IN_MEMORY;
375 root 1.6
376 root 1.8 $self->save;
377 root 1.2 $self->clear;
378     $self->in_memory (cf::MAP_SWAPPED);
379     }
380    
381 root 1.1 sub cf::map::should_reset {
382     my ($map) = @_;
383    
384     # TODO: safety, remove and allow resettable per-player maps
385     return if $map->{path}{user_rel};#d#
386     return unless $map->reset_timeout;
387    
388     my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access;
389    
390     $time + $map->reset_timeout < $cf::RUNTIME
391     }
392    
393     sub cf::map::reset {
394     my ($self) = @_;
395    
396     return if $self->players;
397 root 1.3 return if $self->{path}{user_rel};#d#
398 root 1.1
399     warn "resetting map ", $self->path;#d#
400 root 1.3
401     utf8::encode (my $save = $self->{path}->save_path);
402     aioreq_pri 3; IO::AIO::aio_unlink $save;
403     aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
404 root 1.1
405     $self->clear;
406     $self->in_memory (cf::MAP_SWAPPED);
407 root 1.3 utf8::encode ($self->{load_path} = $self->{path}->load_path);
408 root 1.1 }
409    
410     sub cf::object::player::enter_exit {
411     my ($ob, $exit) = @_;
412    
413 root 1.6 return unless $ob->type == cf::PLAYER;
414    
415     my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
416    
417 root 1.4 #TODO: do this in the background, freeze the player if required
418 root 1.1 sync_job {
419 root 1.3 my ($map, $x, $y);
420 root 1.1
421 root 1.8 if ($exit->slaying eq "/!") {
422     prepare_random_map $exit;
423 root 1.4 }
424 root 1.1
425 root 1.8 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
426    
427     $map = cf::map::find_map $path->as_string;
428     $map = $map->customise_for ($ob) if $map;
429     ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
430    
431 root 1.4 unless ($map) {
432 root 1.6 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
433 root 1.7
434 root 1.6 # restore original map position
435     ($map, $x, $y) = ($oldmap, $oldx, $oldy);
436 root 1.7
437     unless ($map) {
438 root 1.8 $map = cf::map::find_map $emergency_position->[0]
439 root 1.7 or die "FATAL: cannot load emergency map\n";
440     $x = $emergency_position->[1];
441     $y = $emergency_position->[2];
442     }
443 root 1.3 }
444 root 1.1
445 root 1.7 # 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 root 1.6 warn "entering ", $map->path, " at ($x, $y)\n";#d#
450 root 1.8 $map->load;
451 root 1.6 $ob->enter_map ($map, $x, $y);
452 root 1.1 }
453     }
454    
455     sub cf::map::customise_for {
456     my ($map, $ob) = @_;
457    
458     if ($map->per_player) {
459 root 1.8 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
460 root 1.1 }
461    
462     $map
463     }
464    
465     sub cf::map::emergency_save {
466     local $cf::FREEZE = 1;
467    
468 root 1.4 warn "enter emergency map save\n";
469    
470     my $saver = async {
471     warn "begin emergency map save\n";
472     $_->save for values %cf::MAP;
473     };
474     $saver->prio (Coro::PRIO_MAX);
475     $saver->join;
476    
477 root 1.1 warn "emergency map save drain\n";
478     Event::one_event while IO::AIO::nreqs;
479     warn "end emergency map save\n";
480     }
481