ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
(Generate patch)

Comparing deliantra/server/ext/00_map_handling.ext (file contents):
Revision 1.3 by root, Sat Dec 30 15:07:59 2006 UTC vs.
Revision 1.14 by root, Mon Jan 1 00:41:03 2007 UTC

7use Coro; 7use Coro;
8use Coro::AIO; 8use Coro::AIO;
9 9
10our $emergency_position = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 10our $emergency_position = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37];
11 11
12our $DEACTIVATE_TIMEOUT = 60; # number of seconds after which maps get deactivated to save cpu 12our $DEACTIVATE_TIMEOUT = 60; # number of seconds after which maps get deactivated to save cpu
13our $SWAP_TIMEOUT = 600; # number of seconds after which maps inactive get swapped out 13our $SWAP_TIMEOUT = 600; # number of seconds after which maps inactive get swapped out
14our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs 14our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs
15our $SAVE_TIMEOUT = 60; # save maps every n seconds 15our $SAVE_TIMEOUT = 60; # save maps every n seconds
16our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD 16our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD
17our $MAX_RESET = 7200; 17our $MAX_RESET = 7200;
18 18
19$DEACTIVATE_TIMEOUT = 3;#d# 19$DEACTIVATE_TIMEOUT = 3;#d#
20$SWAP_TIMEOUT = 5;#d# 20$SWAP_TIMEOUT = 5;#d#
21$SCHEDULE_INTERVAL = 1; 21$SCHEDULE_INTERVAL = 1;
22 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
113sub 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::unblock_sub {
136 unless (write_runtime) {
137 warn "unable to write runtime file: $!";
138 exit 1;
139 }
140})->();
141
142our $SCHEDULER = cf::coro { 23our $SCHEDULER = cf::coro {
143 while () { 24 while () {
144 Coro::Timer::sleep $SCHEDULE_INTERVAL; 25 Coro::Timer::sleep $SCHEDULE_INTERVAL;
145 26
146 write_runtime
147 or warn "unable to write runtime file: $!";
148
149 for my $map (values %cf::MAP) { 27 for my $map (values %cf::MAP) {
150 eval { 28 eval {
151 next if $map->in_memory != cf::MAP_IN_MEMORY; 29 next if $map->in_memory != cf::MAP_IN_MEMORY;
30 next if $map->players;
152 my $last_access = $map->last_access; 31 my $last_access = $map->last_access;
153 # not yet, because maps might become visible to players nearby 32 # not yet, because maps might become visible to players nearby
154 # we need a tiled meta map for this to work 33 # we need a tiled meta map for this to work
155# if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { 34# if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) {
156# $map->deactivate; 35# $map->deactivate;
171 } 50 }
172 } 51 }
173}; 52};
174$SCHEDULER->prio (-2); 53$SCHEDULER->prio (-2);
175 54
176sub sync_job(&) { 55sub generate_random_map {
56 my ($path, $rmp) = @_;
57
58 # mit "rum" bekleckern, nicht
59 cf::map::_create_random_map
60 $path,
61 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
62 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
63 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
64 $rmp->{exit_on_final_map},
65 $rmp->{xsize}, $rmp->{ysize},
66 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
67 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
68 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
69 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
70 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
71 (cf::region::find $rmp->{region})
72}
73
74sub parse_random_map_params {
177 my ($job) = @_; 75 my ($spec) = @_;
178 76
179 my $done; 77 my $rmp = { # defaults
180 my @res; 78 xsize => 10,
181 79 ysize => 10,
182 Carp::confess "nested sync_job" if $cf::FREEZE;
183
184 local $cf::FREEZE = 1;
185
186 (async {
187 @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 } 80 };
196 81
197 wantarray ? @res : $res[0] 82 for (split /\n/, $spec) {
83 my ($k, $v) = split /\s+/, $_, 2;
84
85 $rmp->{lc $k} = $v if (length $k) && (length $v);
86 }
87
88 $rmp
89}
90
91sub prepare_random_map {
92 my ($exit) = @_;
93
94 # all this does is basically replace the /! path by
95 # a new random map path (?random/...) with a seed
96 # that depends on the exit object
97
98 my $rmp = parse_random_map_params $exit->msg;
99
100 if ($exit->map) {
101 $rmp->{region} = $exit->map->region_name;
102 $rmp->{origin_map} = $exit->map->path;
103 $rmp->{origin_x} = $exit->x;
104 $rmp->{origin_y} = $exit->y;
105 }
106
107 $rmp->{random_seed} ||= $exit->random_seed;
108
109 my $data = cf::to_json $rmp;
110 my $md5 = Digest::MD5::md5_hex $data;
111
112 if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) {
113 aio_write $fh, 0, (length $data), $data, 0;
114
115 $exit->slaying ("?random/$md5");
116 $exit->msg (undef);
117 }
198} 118}
199 119
200# and all this just because we cannot iterate over 120# and all this just because we cannot iterate over
201# all maps in C++... 121# all maps in C++...
202sub cf::map::change_all_map_light { 122sub cf::map::change_all_map_light {
216 or return; 136 or return;
217 137
218 $map->load_header ($path) 138 $map->load_header ($path)
219 or return; 139 or return;
220 140
221 $map->reset_time (0) if $map->reset_time > $cf::RUNTIME;
222 $map->reset_timeout (10);#d#
223
224 $map->{load_path} = $path; 141 $map->{load_path} = $path;
142 use Data::Dumper; warn Dumper $map;#d#
225 143
226 $map 144 $map
227} 145}
228 146
229sub cf::map::find_map_nb { 147sub cf::map::find_map {
230 my ($path, $origin) = @_; 148 my ($path, $origin) = @_;
231 149
232 warn "find_map_nb<$path,$origin>\n";#d# 150 #warn "find_map<$path,$origin>\n";#d#
233 151
234 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; 152 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
235 my $key = $path->as_string; 153 my $key = $path->as_string;
236 154
237 $cf::MAP{$key} || do { 155 $cf::MAP{$key} || do {
238 # do it the slow way 156 # do it the slow way
239 my $map = try_load_header $path->save_path; 157 my $map = try_load_header $path->save_path;
240 158
241 if (!$map) { 159 if ($map) {
160 # safety
161 $map->{reset_time} = $cf::RUNTIME + $MAX_RESET
162 if $map->{reset_time} > $cf::RUNTIME + $MAX_RESET;
163 } else {
164 if (my $rmp = $path->random_map_params) {
165 $map = generate_random_map $key, $rmp;
166 } else {
242 $map = try_load_header $path->load_path 167 $map = try_load_header $path->load_path;
168 }
169
243 or return; 170 $map or return;
171
172 $map->{reset_time} = $cf::RUNTIME + ($map->reset_timeout || 3600);
173 $map->instantiate;
174
175 # per-player maps become, after loading, normal maps
176 $map->per_player (0) if $path->{user_rel};
244 } 177 }
245 178
246 $map or return;
247
248 $map->instantiate;
249 $map->path ($key); 179 $map->path ($key);
250 $map->{path} = $path; 180 $map->{path} = $path;
251 181 $map->last_access ($cf::RUNTIME);
252 $map->per_player (0) if $path->{user_rel};
253 182
254 $map->reset if $map->should_reset; 183 $map->reset if $map->should_reset;
255 184
256 $cf::MAP{$key} = $map 185 $cf::MAP{$key} = $map
257 } 186 }
258} 187}
259 188
260sub 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
273sub cf::map::do_load_nb { 189sub cf::map::load {
274 my ($self) = @_; 190 my ($self) = @_;
275 191
276 return 0 unless $self->in_memory == cf::MAP_SWAPPED; 192 return if $self->in_memory != cf::MAP_SWAPPED;
277 193
278 $self->in_memory (cf::MAP_LOADING); 194 $self->in_memory (cf::MAP_LOADING);
279 195
280 my $path = $self->{path}; 196 my $path = $self->{path};
281 197
291 } 207 }
292 } 208 }
293 209
294 # now do the right thing for maps 210 # now do the right thing for maps
295 $self->link_multipart_objects; 211 $self->link_multipart_objects;
212
213 unless ($self->{path}->is_style_map) {
296 $self->fix_auto_apply; 214 $self->fix_auto_apply;
297 $self->decay_objects; 215 $self->decay_objects;
298 $self->update_buttons; 216 $self->update_buttons;
299 $self->set_darkness_map; 217 $self->set_darkness_map;
300 $self->difficulty ($self->estimate_difficulty) 218 $self->difficulty ($self->estimate_difficulty)
301 unless $self->difficulty; 219 unless $self->difficulty;
220 $self->activate;
221 }
302 222
303 $self->in_memory (cf::MAP_IN_MEMORY); 223 $self->in_memory (cf::MAP_IN_MEMORY);
304} 224}
305 225
306sub cf::map::do_load { 226sub cf::map::load_map_sync {
307 my ($self) = @_; 227 my ($path, $origin) = @_;
308 228
309 warn "do_load<$self>\n";#d# 229 #warn "load_map_sync<$path, $origin>\n";#d#
310 230
311 sync_job { 231 cf::sync_job {
312 cf::map::do_load_nb $self; 232 my $map = cf::map::find_map $path, $origin
233 or return;
234 $map->load;
235 $map
313 }; 236 }
314} 237}
315 238
316sub cf::map::save { 239sub cf::map::save {
317 my ($self) = @_; 240 my ($self) = @_;
318 241
319 warn "saving map ", $self->path;
320
321 my $save = $self->{path}->save_path; utf8::encode $save; 242 my $save = $self->{path}->save_path; utf8::encode $save;
322 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 243 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
244
245 $self->{last_save} = $cf::RUNTIME;
246
247 return unless $self->dirty;
248
249 $self->{load_path} = $save;
250
251 return if $self->{path}->is_style_map;
252
253 warn "saving map ", $self->path;
323 254
324 if ($uniq) { 255 if ($uniq) {
325 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 256 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
326 $self->save_objects ($uniq, cf::IO_UNIQUES); 257 $self->save_objects ($uniq, cf::IO_UNIQUES);
327 } else { 258 } else {
328 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); 259 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
329 } 260 }
330
331 $self->{load_path} = $save;
332 $self->{last_save} = $cf::RUNTIME;
333} 261}
334 262
335sub cf::map::swap_out { 263sub cf::map::swap_out {
336 my ($self) = @_; 264 my ($self) = @_;
337 265
266 return if $self->players;
338 $self->save if $self->in_memory == cf::MAP_IN_MEMORY; 267 return if $self->in_memory != cf::MAP_IN_MEMORY;
268
269 $self->save;
339 $self->clear; 270 $self->clear;
340 $self->in_memory (cf::MAP_SWAPPED); 271 $self->in_memory (cf::MAP_SWAPPED);
341} 272}
342 273
343sub cf::map::should_reset { 274sub cf::map::should_reset {
344 my ($map) = @_; 275 my ($map) = @_;
345 276
346 # TODO: safety, remove and allow resettable per-player maps 277 # TODO: safety, remove and allow resettable per-player maps
347 return if $map->{path}{user_rel};#d# 278 return if $map->{path}{user_rel};#d#
348 return if $map->per_player;
349 return unless $map->reset_timeout; 279 #return unless $map->reset_timeout;
350 280
351 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 281 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
352 282
353 $time + $map->reset_timeout < $cf::RUNTIME 283 $time < $cf::RUNTIME
354} 284}
355 285
356sub cf::map::reset { 286sub cf::map::reset {
357 my ($self) = @_; 287 my ($self) = @_;
358 288
359 return if $self->players; 289 return if $self->players;
360 return if $self->{path}{user_rel};#d# 290 return if $self->{path}{user_rel};#d#
361 291
362 warn "resetting map ", $self->path;#d# 292 warn "resetting map ", $self->path;#d#
293 return;#d#
363 294
364 utf8::encode (my $save = $self->{path}->save_path); 295 utf8::encode (my $save = $self->{path}->save_path);
365 aioreq_pri 3; IO::AIO::aio_unlink $save; 296 aioreq_pri 3; IO::AIO::aio_unlink $save;
366 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; 297 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
367 298
371} 302}
372 303
373sub cf::object::player::enter_exit { 304sub cf::object::player::enter_exit {
374 my ($ob, $exit) = @_; 305 my ($ob, $exit) = @_;
375 306
376 sync_job { 307 return unless $ob->type == cf::PLAYER;
308
309 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
310
311 $ob->enter_map ($LINK_MAP, 20, 20);
312 $ob->deactivate_recursive;
313
314 (Coro::async {
377 my ($map, $x, $y); 315 my ($map, $x, $y);
316 unless (eval {
378 317
379 if ($exit) { 318 prepare_random_map $exit
319 if $exit->slaying eq "/!";
320
380 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 321 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
381 322
382 $map = cf::map::find_map_nb $path->as_string; 323 $map = cf::map::find_map $path->as_string;
383 $map = $map->customise_for ($ob) if $map; 324 $map = $map->customise_for ($ob) if $map;
384 ($x, $y) = ($exit->stats->hp, $exit->stats->sp); 325 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
385 } else {
386 # used on login only(?)
387 $map = cf::map::find_map_nb $ob->contr->maplevel;
388 ($x, $y) = ($ob->x, $ob->y);
389 326
390 unless ($map) { 327 unless ($map) {
328 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
329
330 # restore original map position
331 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
332
333 unless ($map) {
391 $map = cf::map::find_map_nb $emergency_position->[0] 334 $map = cf::map::find_map $emergency_position->[0]
392 or die "FATAL: cannot load emergency map\n"; 335 or die "FATAL: cannot load emergency map\n";
393 $x = $emergency_position->[1]; 336 $x = $emergency_position->[1];
394 $y = $emergency_position->[2]; 337 $y = $emergency_position->[2];
338 }
395 } 339 }
340
341 # use -1, -1 as default coordinates, not 0, 0
342 ($x, $y) = ($map->enter_x, $map->enter_y)
343 if $x <=0 && $y <= 0;
344
345 warn "entering ", $map->path, " at ($x, $y)\n";#d#
346 $map->load;
347 1;
348 }) {
349 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
350
351 $ob->message ("Something went wrong deep within the crossfire server. "
352 . "I'll try to bring you back to the map you were before. "
353 . "Please report this to the dungeon master",
354 cf::NDI_UNIQUE | cf::NDI_RED);
355
356 warn "ERROR in enter_exit: $@";
396 } 357 }
397 358 $ob->activate_recursive;
398 if ($map) {
399 warn "entering ", $map->path, " at ($x, $y)\n";#d#
400 $map->do_load_nb;
401 $ob->enter_map ($map, $x, $y); 359 $ob->enter_map ($map, $x, $y);
402 } else { 360 })->prio (1);
403 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
404 }
405 }
406} 361}
407 362
408sub cf::map::customise_for { 363sub cf::map::customise_for {
409 my ($map, $ob) = @_; 364 my ($map, $ob) = @_;
410 365
411 if ($map->per_player) { 366 if ($map->per_player) {
412 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; 367 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
413 } 368 }
414 369
415 $map 370 $map
416} 371}
417 372
418sub cf::map::emergency_save { 373sub cf::map::emergency_save {
419 local $cf::FREEZE = 1; 374 local $cf::FREEZE = 1;
420 375
376 warn "enter emergency map save\n";
377
378 cf::sync_job {
421 warn "begin emergency map save\n"; 379 warn "begin emergency map save\n";
422 $_->save for values %cf::MAP; 380 $_->save for values %cf::MAP;
423 warn "emergency map save drain\n"; 381 };
424 382
425 Event::one_event while IO::AIO::nreqs;
426 warn "end emergency map save\n"; 383 warn "end emergency map save\n";
427} 384}
428 385

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines