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.1 by root, Sat Dec 30 10:16:10 2006 UTC vs.
Revision 1.11 by root, Sun Dec 31 21:02:04 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines