1 | # |
1 | # |
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | # |
3 | # |
4 | # Copyright (©) 2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # |
5 | # |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
7 | # the terms of the Affero GNU General Public License as published by the |
7 | # the terms of the Affero GNU General Public License as published by the |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
9 | # option) any later version. |
9 | # option) any later version. |
10 | # |
10 | # |
11 | # This program is distributed in the hope that it will be useful, |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | # GNU General Public License for more details. |
14 | # GNU General Public License for more details. |
15 | # |
15 | # |
16 | # You should have received a copy of the Affero GNU General Public License |
16 | # You should have received a copy of the Affero GNU General Public License |
17 | # and the GNU General Public License along with this program. If not, see |
17 | # and the GNU General Public License along with this program. If not, see |
18 | # <http://www.gnu.org/licenses/>. |
18 | # <http://www.gnu.org/licenses/>. |
19 | # |
19 | # |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
21 | # |
21 | # |
22 | |
22 | |
23 | package cf; |
23 | package cf; |
24 | |
24 | |
… | |
… | |
108 | our $RANDOMDIR = "$LOCALDIR/random"; |
108 | our $RANDOMDIR = "$LOCALDIR/random"; |
109 | our $BDBDIR = "$LOCALDIR/db"; |
109 | our $BDBDIR = "$LOCALDIR/db"; |
110 | our $PIDFILE = "$LOCALDIR/pid"; |
110 | our $PIDFILE = "$LOCALDIR/pid"; |
111 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
111 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
112 | |
112 | |
113 | our %RESOURCE; # unused |
113 | #our %RESOURCE; # unused |
114 | |
114 | |
115 | our $OUTPUT_RATE_MIN = 3000; |
115 | our $OUTPUT_RATE_MIN = 3000; |
116 | our $OUTPUT_RATE_MAX = 1000000; |
116 | our $OUTPUT_RATE_MAX = 1000000; |
117 | |
117 | |
118 | our $MAX_LINKS = 32; # how many chained exits to follow |
118 | our $MAX_LINKS = 32; # how many chained exits to follow |
… | |
… | |
129 | our $DB_ENV; |
129 | our $DB_ENV; |
130 | |
130 | |
131 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
131 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
132 | |
132 | |
133 | our %CFG; |
133 | our %CFG; |
|
|
134 | our %EXT_CFG; # cfgkeyname => [var-ref, defaultvalue] |
134 | |
135 | |
135 | our $UPTIME; $UPTIME ||= time; |
136 | our $UPTIME; $UPTIME ||= time; |
136 | our $RUNTIME; |
137 | our $RUNTIME = 0; |
|
|
138 | our $SERVER_TICK = 0; |
137 | our $NOW; |
139 | our $NOW; |
138 | |
140 | |
139 | our (%PLAYER, %PLAYER_LOADING); # all users |
141 | our (%PLAYER, %PLAYER_LOADING); # all users |
140 | our (%MAP, %MAP_LOADING ); # all maps |
142 | our (%MAP, %MAP_LOADING ); # all maps |
141 | our $LINK_MAP; # the special {link} map, which is always available |
143 | our $LINK_MAP; # the special {link} map, which is always available |
… | |
… | |
150 | |
152 | |
151 | our @POST_INIT; |
153 | our @POST_INIT; |
152 | |
154 | |
153 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
155 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
154 | our $REALLY_UNLOOP; # never set to true, please :) |
156 | our $REALLY_UNLOOP; # never set to true, please :) |
|
|
157 | |
|
|
158 | our $WAIT_FOR_TICK = new Coro::Signal; |
|
|
159 | our @WAIT_FOR_TICK_BEGIN; |
155 | |
160 | |
156 | binmode STDOUT; |
161 | binmode STDOUT; |
157 | binmode STDERR; |
162 | binmode STDERR; |
158 | |
163 | |
159 | # read virtual server time, if available |
164 | # read virtual server time, if available |
… | |
… | |
193 | |
198 | |
194 | =over 4 |
199 | =over 4 |
195 | |
200 | |
196 | =item $cf::UPTIME |
201 | =item $cf::UPTIME |
197 | |
202 | |
198 | The timestamp of the server start (so not actually an uptime). |
203 | The timestamp of the server start (so not actually an "uptime"). |
|
|
204 | |
|
|
205 | =item $cf::SERVER_TICK |
|
|
206 | |
|
|
207 | An unsigned integer that starts at zero when the server is started and is |
|
|
208 | incremented on every tick. |
|
|
209 | |
|
|
210 | =item $cf::NOW |
|
|
211 | |
|
|
212 | The (real) time of the last (current) server tick - updated before and |
|
|
213 | after tick processing, so this is useful only as a rough "what time is it |
|
|
214 | now" estimate. |
|
|
215 | |
|
|
216 | =item $cf::TICK |
|
|
217 | |
|
|
218 | The interval between each server tick, in seconds. |
199 | |
219 | |
200 | =item $cf::RUNTIME |
220 | =item $cf::RUNTIME |
201 | |
221 | |
202 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
222 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
203 | every server tick. |
223 | every server tick. |
… | |
… | |
209 | Various directories - "/etc", read-only install directory, perl-library |
229 | Various directories - "/etc", read-only install directory, perl-library |
210 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
230 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
211 | unique-items directory, player file directory, random maps directory and |
231 | unique-items directory, player file directory, random maps directory and |
212 | database environment. |
232 | database environment. |
213 | |
233 | |
214 | =item $cf::NOW |
|
|
215 | |
|
|
216 | The time of the last (current) server tick. |
|
|
217 | |
|
|
218 | =item $cf::TICK |
|
|
219 | |
|
|
220 | The interval between server ticks, in seconds. |
|
|
221 | |
|
|
222 | =item $cf::LOADAVG |
234 | =item $cf::LOADAVG |
223 | |
235 | |
224 | The current CPU load on the server (alpha-smoothed), as a value between 0 |
236 | The current CPU load on the server (alpha-smoothed), as a value between 0 |
225 | (none) and 1 (overloaded), indicating how much time is spent on processing |
237 | (none) and 1 (overloaded), indicating how much time is spent on processing |
226 | objects per tick. Healthy values are < 0.5. |
238 | objects per tick. Healthy values are < 0.5. |
… | |
… | |
237 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
249 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
238 | |
250 | |
239 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
251 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
240 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
252 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
241 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
253 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
254 | |
|
|
255 | Note that cf::Wait_for_tick will immediately return when the server is not |
|
|
256 | ticking, making it suitable for small pauses in threads that need to run |
|
|
257 | when the server is paused. If that is not applicable (i.e. you I<really> |
|
|
258 | want to wait, use C<$cf::WAIT_FOR_TICK>). |
|
|
259 | |
|
|
260 | =item $cf::WAIT_FOR_TICK |
|
|
261 | |
|
|
262 | Note that C<cf::wait_for_tick> is probably the correct thing to use. This |
|
|
263 | variable contains a L<Coro::Signal> that is broadcats after every server |
|
|
264 | tick. Calling C<< ->wait >> on it will suspend the caller until after the |
|
|
265 | next server tick. |
242 | |
266 | |
243 | =cut |
267 | =cut |
244 | |
268 | |
245 | sub wait_for_tick(); |
269 | sub wait_for_tick(); |
246 | sub wait_for_tick_begin(); |
270 | sub wait_for_tick_begin(); |
… | |
… | |
349 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
373 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
350 | $d |
374 | $d |
351 | } || "[unable to dump $_[0]: '$@']"; |
375 | } || "[unable to dump $_[0]: '$@']"; |
352 | } |
376 | } |
353 | |
377 | |
354 | =item $scalar = load_file $path |
378 | =item $scalar = cf::load_file $path |
355 | |
379 | |
356 | Loads the given file from path and returns its contents. Croaks on error |
380 | Loads the given file from path and returns its contents. Croaks on error |
357 | and can block. |
381 | and can block. |
358 | |
382 | |
359 | =cut |
383 | =cut |
… | |
… | |
361 | sub load_file($) { |
385 | sub load_file($) { |
362 | 0 <= aio_load $_[0], my $data |
386 | 0 <= aio_load $_[0], my $data |
363 | or Carp::croak "$_[0]: $!"; |
387 | or Carp::croak "$_[0]: $!"; |
364 | |
388 | |
365 | $data |
389 | $data |
|
|
390 | } |
|
|
391 | |
|
|
392 | =item $success = cf::replace_file $path, $data, $sync |
|
|
393 | |
|
|
394 | Atomically replaces the file at the given $path with new $data, and |
|
|
395 | optionally $sync the data to disk before replacing the file. |
|
|
396 | |
|
|
397 | =cut |
|
|
398 | |
|
|
399 | sub replace_file($$;$) { |
|
|
400 | my ($path, $data, $sync) = @_; |
|
|
401 | |
|
|
402 | my $lock = cf::lock_acquire ("replace_file:$path"); |
|
|
403 | |
|
|
404 | my $fh = aio_open "$path~", Fcntl::O_WRONLY | Fcntl::O_CREAT | Fcntl::O_TRUNC, 0644 |
|
|
405 | or return; |
|
|
406 | |
|
|
407 | $data = $data->() if ref $data; |
|
|
408 | |
|
|
409 | length $data == aio_write $fh, 0, (length $data), $data, 0 |
|
|
410 | or return; |
|
|
411 | |
|
|
412 | !$sync |
|
|
413 | or !aio_fsync $fh |
|
|
414 | or return; |
|
|
415 | |
|
|
416 | aio_close $fh |
|
|
417 | and return; |
|
|
418 | |
|
|
419 | aio_rename "$path~", $path |
|
|
420 | and return; |
|
|
421 | |
|
|
422 | if ($sync) { |
|
|
423 | $path =~ s%/[^/]*$%%; |
|
|
424 | aio_pathsync $path; |
|
|
425 | } |
|
|
426 | |
|
|
427 | 1 |
366 | } |
428 | } |
367 | |
429 | |
368 | =item $ref = cf::decode_json $json |
430 | =item $ref = cf::decode_json $json |
369 | |
431 | |
370 | Converts a JSON string into the corresponding perl data structure. |
432 | Converts a JSON string into the corresponding perl data structure. |
… | |
… | |
545 | } |
607 | } |
546 | } |
608 | } |
547 | |
609 | |
548 | if (@SLOT_QUEUE) { |
610 | if (@SLOT_QUEUE) { |
549 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
611 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
550 | push @cf::WAIT_FOR_TICK, $signal; |
612 | $WAIT_FOR_TICK->wait; |
551 | $signal->wait; |
|
|
552 | } else { |
613 | } else { |
553 | $busy = 0; |
614 | $busy = 0; |
554 | Coro::schedule; |
615 | Coro::schedule; |
555 | } |
616 | } |
556 | } |
617 | } |
… | |
… | |
1439 | my ($pl, $buf) = @_; |
1500 | my ($pl, $buf) = @_; |
1440 | |
1501 | |
1441 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1502 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1442 | |
1503 | |
1443 | if (ref $msg) { |
1504 | if (ref $msg) { |
1444 | my ($type, $reply, @payload) = |
1505 | my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash |
1445 | "ARRAY" eq ref $msg |
|
|
1446 | ? @$msg |
|
|
1447 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
1448 | |
1506 | |
1449 | my @reply; |
1507 | my @reply; |
1450 | |
1508 | |
1451 | if (my $cb = $EXTCMD{$type}) { |
1509 | if (my $cb = $EXTCMD{$type}) { |
1452 | @reply = $cb->($pl, @payload); |
1510 | @reply = $cb->($pl, @payload); |
… | |
… | |
1474 | }; |
1532 | }; |
1475 | |
1533 | |
1476 | $grp |
1534 | $grp |
1477 | } |
1535 | } |
1478 | |
1536 | |
|
|
1537 | sub _ext_cfg_reg($$$$) { |
|
|
1538 | my ($rvar, $varname, $cfgname, $default) = @_; |
|
|
1539 | |
|
|
1540 | $cfgname = lc $varname |
|
|
1541 | unless length $cfgname; |
|
|
1542 | |
|
|
1543 | $EXT_CFG{$cfgname} = [$rvar, $default]; |
|
|
1544 | |
|
|
1545 | $$rvar = exists $CFG{$cfgname} ? $CFG{$cfgname} : $default; |
|
|
1546 | } |
|
|
1547 | |
1479 | sub load_extensions { |
1548 | sub load_extensions { |
1480 | info "loading extensions..."; |
1549 | info "loading extensions..."; |
|
|
1550 | |
|
|
1551 | %EXT_CFG = (); |
1481 | |
1552 | |
1482 | cf::sync_job { |
1553 | cf::sync_job { |
1483 | my %todo; |
1554 | my %todo; |
1484 | |
1555 | |
1485 | for my $path (<$LIBDIR/*.ext>) { |
1556 | for my $path (<$LIBDIR/*.ext>) { |
… | |
… | |
1528 | unless exists $done{$_}; |
1599 | unless exists $done{$_}; |
1529 | } |
1600 | } |
1530 | |
1601 | |
1531 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1602 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1532 | |
1603 | |
|
|
1604 | my $source = $v->{source}; |
|
|
1605 | |
|
|
1606 | # support "CONF varname :confname = default" pseudo-statements |
|
|
1607 | $source =~ s{ |
|
|
1608 | ^ CONF \s+ ([^\s:=]+) \s* (?:: \s* ([^\s:=]+) \s* )? = ([^\n#]+) |
|
|
1609 | }{ |
|
|
1610 | "our \$$1; BEGIN { cf::_ext_cfg_reg \\\$$1, q\x00$1\x00, q\x00$2\x00, $3 }"; |
|
|
1611 | }gmxe; |
|
|
1612 | |
1533 | my $active = eval $v->{source}; |
1613 | my $active = eval $source; |
1534 | |
1614 | |
1535 | if (length $@) { |
1615 | if (length $@) { |
1536 | error "$v->{path}: $@\n"; |
1616 | error "$v->{path}: $@\n"; |
1537 | |
1617 | |
1538 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1618 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
… | |
… | |
1954 | # {... are special paths that are not being touched |
2034 | # {... are special paths that are not being touched |
1955 | # ?xxx/... are special absolute paths |
2035 | # ?xxx/... are special absolute paths |
1956 | # ?random/... random maps |
2036 | # ?random/... random maps |
1957 | # /... normal maps |
2037 | # /... normal maps |
1958 | # ~user/... per-player map of a specific user |
2038 | # ~user/... per-player map of a specific user |
1959 | # !up !down for quad maps, or other maps with up/down layers |
|
|
1960 | |
2039 | |
1961 | $path =~ s/$PATH_SEP/\//go; |
2040 | $path =~ s/$PATH_SEP/\//go; |
1962 | |
2041 | |
1963 | # treat it as relative path if it starts with |
2042 | # treat it as relative path if it starts with |
1964 | # something that looks reasonable |
2043 | # something that looks reasonable |
1965 | if ($path =~ m{^(?:\./|\.\./|\w)}) { |
2044 | if ($path =~ m{^(?:\./|\.\./|\w)}) { |
1966 | $base or Carp::carp "normalise called with relative path and no base: '$path'"; |
2045 | $base or Carp::carp "normalise called with relative path and no base: '$path'"; |
1967 | |
2046 | |
1968 | $base =~ s{[^/]+/?$}{}; |
2047 | $base =~ s{[^/]+/?$}{}; |
1969 | $path = "$base/$path"; |
2048 | $path = "$base/$path"; |
1970 | |
|
|
1971 | } elsif ($path eq '!up') { |
|
|
1972 | $base && ref $base |
|
|
1973 | or Carp::carp "normalise called with relative tile path and no base: '$path'"; |
|
|
1974 | |
|
|
1975 | my $uppth = $base->tile_path (cf::TILE_UP); |
|
|
1976 | $path = $uppth if $uppth; |
|
|
1977 | |
|
|
1978 | } elsif ($path eq '!down') { |
|
|
1979 | $base && ref $base |
|
|
1980 | or Carp::carp "normalise called with relative tile path and no base: '$path'"; |
|
|
1981 | |
|
|
1982 | my $dpth = $base->tile_path (cf::TILE_DOWN); |
|
|
1983 | $path = $dpth if $dpth; |
|
|
1984 | } |
2049 | } |
1985 | |
2050 | |
1986 | for ($path) { |
2051 | for ($path) { |
1987 | redo if s{/\.?/}{/}; |
2052 | redo if s{/\.?/}{/}; |
1988 | redo if s{/[^/]+/\.\./}{/}; |
2053 | redo if s{/[^/]+/\.\./}{/}; |
… | |
… | |
2084 | $self->{load_path} = $path; |
2149 | $self->{load_path} = $path; |
2085 | |
2150 | |
2086 | 1 |
2151 | 1 |
2087 | } |
2152 | } |
2088 | |
2153 | |
|
|
2154 | # used to laod the header of an original map |
2089 | sub load_header_orig { |
2155 | sub load_header_orig { |
2090 | my ($self) = @_; |
2156 | my ($self) = @_; |
2091 | |
2157 | |
2092 | $self->load_header_from ($self->load_path) |
2158 | $self->load_header_from ($self->load_path) |
2093 | } |
2159 | } |
2094 | |
2160 | |
|
|
2161 | # used to laod the header of an instantiated map |
2095 | sub load_header_temp { |
2162 | sub load_header_temp { |
2096 | my ($self) = @_; |
2163 | my ($self) = @_; |
2097 | |
2164 | |
2098 | $self->load_header_from ($self->save_path) |
2165 | $self->load_header_from ($self->save_path) |
2099 | } |
2166 | } |
2100 | |
2167 | |
|
|
2168 | # called after loading the header from an instantiated map |
2101 | sub prepare_temp { |
2169 | sub prepare_temp { |
2102 | my ($self) = @_; |
2170 | my ($self) = @_; |
2103 | |
2171 | |
2104 | $self->last_access ((delete $self->{last_access}) |
2172 | $self->last_access ((delete $self->{last_access}) |
2105 | || $cf::RUNTIME); #d# |
2173 | || $cf::RUNTIME); #d# |
2106 | # safety |
2174 | # safety |
2107 | $self->{instantiate_time} = $cf::RUNTIME |
2175 | $self->{instantiate_time} = $cf::RUNTIME |
2108 | if $self->{instantiate_time} > $cf::RUNTIME; |
2176 | if $self->{instantiate_time} > $cf::RUNTIME; |
2109 | } |
2177 | } |
2110 | |
2178 | |
|
|
2179 | # called after loading the header from an original map |
2111 | sub prepare_orig { |
2180 | sub prepare_orig { |
2112 | my ($self) = @_; |
2181 | my ($self) = @_; |
2113 | |
2182 | |
2114 | $self->{load_original} = 1; |
2183 | $self->{load_original} = 1; |
2115 | $self->{instantiate_time} = $cf::RUNTIME; |
2184 | $self->{instantiate_time} = $cf::RUNTIME; |
… | |
… | |
2182 | |
2251 | |
2183 | { |
2252 | { |
2184 | my $guard = cf::lock_acquire "map_data:$path"; |
2253 | my $guard = cf::lock_acquire "map_data:$path"; |
2185 | |
2254 | |
2186 | return unless $self->valid; |
2255 | return unless $self->valid; |
2187 | return unless $self->in_memory == cf::MAP_SWAPPED; |
2256 | return unless $self->state == cf::MAP_SWAPPED; |
2188 | |
|
|
2189 | $self->in_memory (cf::MAP_LOADING); |
|
|
2190 | |
2257 | |
2191 | $self->alloc; |
2258 | $self->alloc; |
2192 | |
2259 | |
2193 | $self->pre_load; |
2260 | $self->pre_load; |
2194 | cf::cede_to_tick; |
2261 | cf::cede_to_tick; |
… | |
… | |
2212 | } |
2279 | } |
2213 | } |
2280 | } |
2214 | } |
2281 | } |
2215 | |
2282 | |
2216 | $f->resolve_delayed_derefs; |
2283 | $f->resolve_delayed_derefs; |
|
|
2284 | } else { |
|
|
2285 | $self->post_load_original |
|
|
2286 | if delete $self->{load_original}; |
2217 | } |
2287 | } |
|
|
2288 | |
|
|
2289 | $self->state (cf::MAP_INACTIVE); |
2218 | |
2290 | |
2219 | cf::cede_to_tick; |
2291 | cf::cede_to_tick; |
2220 | # now do the right thing for maps |
2292 | # now do the right thing for maps |
2221 | $self->link_multipart_objects; |
2293 | $self->link_multipart_objects; |
2222 | $self->difficulty ($self->estimate_difficulty) |
2294 | $self->difficulty ($self->estimate_difficulty) |
… | |
… | |
2226 | unless ($self->{deny_activate}) { |
2298 | unless ($self->{deny_activate}) { |
2227 | $self->decay_objects; |
2299 | $self->decay_objects; |
2228 | $self->fix_auto_apply; |
2300 | $self->fix_auto_apply; |
2229 | $self->update_buttons; |
2301 | $self->update_buttons; |
2230 | cf::cede_to_tick; |
2302 | cf::cede_to_tick; |
2231 | $self->activate; |
2303 | #$self->activate; # no longer activate maps automatically |
2232 | } |
2304 | } |
2233 | |
2305 | |
2234 | $self->{last_save} = $cf::RUNTIME; |
2306 | $self->{last_save} = $cf::RUNTIME; |
2235 | $self->last_access ($cf::RUNTIME); |
2307 | $self->last_access ($cf::RUNTIME); |
2236 | |
|
|
2237 | $self->in_memory (cf::MAP_ACTIVE); |
|
|
2238 | } |
2308 | } |
2239 | |
2309 | |
2240 | $self->post_load; |
2310 | $self->post_load; |
2241 | |
2311 | |
2242 | 1 |
2312 | 1 |
… | |
… | |
2255 | # if $self->per_party; |
2325 | # if $self->per_party; |
2256 | |
2326 | |
2257 | $self |
2327 | $self |
2258 | } |
2328 | } |
2259 | |
2329 | |
2260 | # find and load all maps in the 3x3 area around a map |
|
|
2261 | sub load_neighbours { |
|
|
2262 | my ($map) = @_; |
|
|
2263 | |
|
|
2264 | my @neigh; # diagonal neighbours |
|
|
2265 | |
|
|
2266 | for (0 .. 3) { |
|
|
2267 | my $neigh = $map->tile_path ($_) |
|
|
2268 | or next; |
|
|
2269 | $neigh = find $neigh, $map |
|
|
2270 | or next; |
|
|
2271 | $neigh->load; |
|
|
2272 | |
|
|
2273 | # now find the diagonal neighbours |
|
|
2274 | push @neigh, |
|
|
2275 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
|
|
2276 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
|
|
2277 | } |
|
|
2278 | |
|
|
2279 | for (grep defined $_->[0], @neigh) { |
|
|
2280 | my ($path, $origin) = @$_; |
|
|
2281 | my $neigh = find $path, $origin |
|
|
2282 | or next; |
|
|
2283 | $neigh->load; |
|
|
2284 | } |
|
|
2285 | } |
|
|
2286 | |
|
|
2287 | sub find_sync { |
2330 | sub find_sync { |
2288 | my ($path, $origin) = @_; |
2331 | my ($path, $origin) = @_; |
2289 | |
2332 | |
|
|
2333 | # it's a bug to call this from the main context |
2290 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
2334 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
2291 | if $Coro::current == $Coro::main; |
2335 | if $Coro::current == $Coro::main; |
2292 | |
2336 | |
2293 | find $path, $origin |
2337 | find $path, $origin |
2294 | } |
2338 | } |
2295 | |
2339 | |
2296 | sub do_load_sync { |
2340 | sub do_load_sync { |
2297 | my ($map) = @_; |
2341 | my ($map) = @_; |
2298 | |
2342 | |
|
|
2343 | # it's a bug to call this from the main context |
2299 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2344 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2300 | if $Coro::current == $Coro::main; |
2345 | if $Coro::current == $Coro::main; |
2301 | |
2346 | |
2302 | $map->load; |
2347 | $map->load; |
2303 | } |
2348 | } |
… | |
… | |
2309 | my ($path, $origin, $load) = @_; |
2354 | my ($path, $origin, $load) = @_; |
2310 | |
2355 | |
2311 | $path = normalise $path, $origin; |
2356 | $path = normalise $path, $origin; |
2312 | |
2357 | |
2313 | if (my $map = $cf::MAP{$path}) { |
2358 | if (my $map = $cf::MAP{$path}) { |
2314 | return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; |
2359 | return $map if !$load || $map->linkable; |
2315 | } |
2360 | } |
2316 | |
2361 | |
2317 | $MAP_PREFETCH{$path} |= $load; |
2362 | $MAP_PREFETCH{$path} |= $load; |
2318 | |
2363 | |
2319 | $MAP_PREFETCHER ||= cf::async { |
2364 | $MAP_PREFETCHER ||= cf::async { |
… | |
… | |
2378 | sub swap_out { |
2423 | sub swap_out { |
2379 | my ($self) = @_; |
2424 | my ($self) = @_; |
2380 | |
2425 | |
2381 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2426 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2382 | |
2427 | |
2383 | return if $self->in_memory != cf::MAP_ACTIVE; |
2428 | return if !$self->linkable; |
2384 | return if $self->{deny_save}; |
2429 | return if $self->{deny_save}; |
2385 | return if $self->players; |
2430 | return if $self->players; |
2386 | |
2431 | |
2387 | # first deactivate the map and "unlink" it from the core |
2432 | # first deactivate the map and "unlink" it from the core |
2388 | $self->deactivate; |
2433 | $self->deactivate; |
2389 | $_->clear_links_to ($self) for values %cf::MAP; |
2434 | $_->clear_links_to ($self) for values %cf::MAP; |
2390 | $self->in_memory (cf::MAP_SWAPPED); |
2435 | $self->state (cf::MAP_SWAPPED); |
2391 | |
2436 | |
2392 | # then atomically save |
2437 | # then atomically save |
2393 | $self->_save; |
2438 | $self->_save; |
2394 | |
2439 | |
2395 | # then free the map |
2440 | # then free the map |
… | |
… | |
2421 | |
2466 | |
2422 | return if $self->players; |
2467 | return if $self->players; |
2423 | |
2468 | |
2424 | cf::trace "resetting map ", $self->path, "\n"; |
2469 | cf::trace "resetting map ", $self->path, "\n"; |
2425 | |
2470 | |
2426 | $self->in_memory (cf::MAP_SWAPPED); |
2471 | $self->state (cf::MAP_SWAPPED); |
2427 | |
2472 | |
2428 | # need to save uniques path |
2473 | # need to save uniques path |
2429 | unless ($self->{deny_save}) { |
2474 | unless ($self->{deny_save}) { |
2430 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
2475 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
2431 | |
2476 | |
… | |
… | |
2761 | # use -1 or undef as default coordinates, not 0, 0 |
2806 | # use -1 or undef as default coordinates, not 0, 0 |
2762 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2807 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2763 | if $x <= 0 && $y <= 0; |
2808 | if $x <= 0 && $y <= 0; |
2764 | |
2809 | |
2765 | $map->load; |
2810 | $map->load; |
2766 | $map->load_neighbours; |
|
|
2767 | |
2811 | |
2768 | return unless $self->contr->active; |
2812 | return unless $self->contr->active; |
2769 | |
2813 | |
2770 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2814 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2771 | if ($self->enter_map ($map, $x, $y)) { |
2815 | if ($self->enter_map ($map, $x, $y)) { |
… | |
… | |
2927 | $Coro::current->{desc} = "enter_exit"; |
2971 | $Coro::current->{desc} = "enter_exit"; |
2928 | |
2972 | |
2929 | unless (eval { |
2973 | unless (eval { |
2930 | $self->deactivate_recursive; # just to be sure |
2974 | $self->deactivate_recursive; # just to be sure |
2931 | |
2975 | |
2932 | # random map handling |
|
|
2933 | { |
|
|
2934 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
|
|
2935 | |
|
|
2936 | prepare_random_map $exit |
|
|
2937 | if $exit->slaying eq "/!"; |
|
|
2938 | } |
|
|
2939 | |
|
|
2940 | my $map = cf::map::normalise $exit->slaying, $exit->map; |
2976 | my $map = cf::map::normalise $exit->slaying, $exit->map; |
2941 | my $x = $exit->stats->hp; |
2977 | my $x = $exit->stats->hp; |
2942 | my $y = $exit->stats->sp; |
2978 | my $y = $exit->stats->sp; |
|
|
2979 | |
|
|
2980 | # special map handling |
|
|
2981 | my $slaying = $exit->slaying; |
|
|
2982 | |
|
|
2983 | # special map handling |
|
|
2984 | if ($slaying eq "/!") { |
|
|
2985 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
|
|
2986 | |
|
|
2987 | prepare_random_map $exit |
|
|
2988 | if $exit->slaying eq "/!"; # need to re-check after getting the lock |
|
|
2989 | |
|
|
2990 | $map = $exit->slaying; |
|
|
2991 | |
|
|
2992 | } elsif ($slaying eq '!up') { |
|
|
2993 | $map = $exit->map->tile_path (cf::TILE_UP); |
|
|
2994 | $x = $exit->x; |
|
|
2995 | $y = $exit->y; |
|
|
2996 | |
|
|
2997 | } elsif ($slaying eq '!down') { |
|
|
2998 | $map = $exit->map->tile_path (cf::TILE_DOWN); |
|
|
2999 | $x = $exit->x; |
|
|
3000 | $y = $exit->y; |
|
|
3001 | } |
2943 | |
3002 | |
2944 | $self->goto ($map, $x, $y); |
3003 | $self->goto ($map, $x, $y); |
2945 | |
3004 | |
2946 | # if exit is damned, update players death & WoR home-position |
3005 | # if exit is damned, update players death & WoR home-position |
2947 | $self->contr->savebed ($map, $x, $y) |
3006 | $self->contr->savebed ($map, $x, $y) |
… | |
… | |
3189 | =cut |
3248 | =cut |
3190 | |
3249 | |
3191 | sub cf::client::ext_reply($$@) { |
3250 | sub cf::client::ext_reply($$@) { |
3192 | my ($self, $id, @msg) = @_; |
3251 | my ($self, $id, @msg) = @_; |
3193 | |
3252 | |
3194 | if ($self->extcmd == 2) { |
3253 | return unless $self->extcmd == 2; |
|
|
3254 | |
3195 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3255 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3196 | } elsif ($self->extcmd == 1) { |
|
|
3197 | #TODO: version 1, remove |
|
|
3198 | unshift @msg, msgtype => "reply", msgid => $id; |
|
|
3199 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
|
|
3200 | } |
|
|
3201 | } |
3256 | } |
3202 | |
3257 | |
3203 | =item $success = $client->query ($flags, "text", \&cb) |
3258 | =item $success = $client->query ($flags, "text", \&cb) |
3204 | |
3259 | |
3205 | Queues a query to the client, calling the given callback with |
3260 | Queues a query to the client, calling the given callback with |
… | |
… | |
3260 | my ($ns, $buf) = @_; |
3315 | my ($ns, $buf) = @_; |
3261 | |
3316 | |
3262 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
3317 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
3263 | |
3318 | |
3264 | if (ref $msg) { |
3319 | if (ref $msg) { |
3265 | my ($type, $reply, @payload) = |
3320 | my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash |
3266 | "ARRAY" eq ref $msg |
|
|
3267 | ? @$msg |
|
|
3268 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
3269 | |
3321 | |
3270 | my @reply; |
3322 | my @reply; |
3271 | |
3323 | |
3272 | if (my $cb = $EXTICMD{$type}) { |
3324 | if (my $cb = $EXTICMD{$type}) { |
3273 | @reply = $cb->($ns, @payload); |
3325 | @reply = $cb->($ns, @payload); |
… | |
… | |
3523 | |
3575 | |
3524 | { |
3576 | { |
3525 | my $res = $facedata->{resource}; |
3577 | my $res = $facedata->{resource}; |
3526 | |
3578 | |
3527 | while (my ($name, $info) = each %$res) { |
3579 | while (my ($name, $info) = each %$res) { |
3528 | if (defined $info->{type}) { |
3580 | if (defined (my $type = $info->{type})) { |
|
|
3581 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3529 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3582 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3530 | |
3583 | |
3531 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3584 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3532 | cf::face::set_type $idx, $info->{type}; |
3585 | cf::face::set_type $idx, $type; |
|
|
3586 | cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3533 | } else { |
3587 | } else { |
3534 | $RESOURCE{$name} = $info; # unused |
3588 | # $RESOURCE{$name} = $info; # unused |
3535 | } |
3589 | } |
3536 | |
3590 | |
3537 | cf::cede_to_tick; |
3591 | cf::cede_to_tick; |
3538 | } |
3592 | } |
3539 | } |
3593 | } |
… | |
… | |
3675 | |
3729 | |
3676 | sub main { |
3730 | sub main { |
3677 | cf::init_globals; # initialise logging |
3731 | cf::init_globals; # initialise logging |
3678 | |
3732 | |
3679 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3733 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3680 | LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3734 | LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3681 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3735 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3682 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3736 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3683 | |
3737 | |
3684 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3738 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3685 | |
3739 | |
… | |
… | |
3727 | }; |
3781 | }; |
3728 | |
3782 | |
3729 | cf::object::thawer::errors_are_fatal 0; |
3783 | cf::object::thawer::errors_are_fatal 0; |
3730 | info "parse errors in files are no longer fatal from this point on.\n"; |
3784 | info "parse errors in files are no longer fatal from this point on.\n"; |
3731 | |
3785 | |
3732 | my $free_main; $free_main = EV::idle sub { |
3786 | AE::postpone { |
3733 | undef $free_main; |
|
|
3734 | undef &main; # free gobs of memory :) |
3787 | undef &main; # free gobs of memory :) |
3735 | }; |
3788 | }; |
3736 | |
3789 | |
3737 | goto &main_loop; |
3790 | goto &main_loop; |
3738 | } |
3791 | } |
… | |
… | |
3895 | |
3948 | |
3896 | cf::write_runtime_sync; # external watchdog should not bark |
3949 | cf::write_runtime_sync; # external watchdog should not bark |
3897 | |
3950 | |
3898 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3951 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3899 | while (IO::AIO::nreqs || BDB::nreqs) { |
3952 | while (IO::AIO::nreqs || BDB::nreqs) { |
3900 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
3953 | Coro::AnyEvent::sleep 0.01; # let the sync_job do it's thing |
3901 | } |
3954 | } |
3902 | |
3955 | |
3903 | cf::write_runtime_sync; # external watchdog should not bark |
3956 | cf::write_runtime_sync; # external watchdog should not bark |
3904 | }; |
3957 | }; |
3905 | |
3958 | |
… | |
… | |
4086 | |
4139 | |
4087 | ############################################################################# |
4140 | ############################################################################# |
4088 | |
4141 | |
4089 | my $bug_warning = 0; |
4142 | my $bug_warning = 0; |
4090 | |
4143 | |
4091 | our @WAIT_FOR_TICK; |
|
|
4092 | our @WAIT_FOR_TICK_BEGIN; |
|
|
4093 | |
|
|
4094 | sub wait_for_tick() { |
4144 | sub wait_for_tick() { |
4095 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4145 | return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main; |
4096 | |
4146 | |
4097 | my $signal = new Coro::Signal; |
4147 | $WAIT_FOR_TICK->wait; |
4098 | push @WAIT_FOR_TICK, $signal; |
|
|
4099 | $signal->wait; |
|
|
4100 | } |
4148 | } |
4101 | |
4149 | |
4102 | sub wait_for_tick_begin() { |
4150 | sub wait_for_tick_begin() { |
4103 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4151 | return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main; |
4104 | |
4152 | |
4105 | my $signal = new Coro::Signal; |
4153 | my $signal = new Coro::Signal; |
4106 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4154 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4107 | $signal->wait; |
4155 | $signal->wait; |
4108 | } |
4156 | } |
… | |
… | |
4112 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
4160 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
4113 | unless ++$bug_warning > 10; |
4161 | unless ++$bug_warning > 10; |
4114 | return; |
4162 | return; |
4115 | } |
4163 | } |
4116 | |
4164 | |
4117 | cf::server_tick; # one server iteration |
4165 | cf::one_tick; # one server iteration |
4118 | |
4166 | |
4119 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
4167 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
4120 | |
4168 | |
4121 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4169 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4122 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4170 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
… | |
… | |
4128 | } |
4176 | } |
4129 | |
4177 | |
4130 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4178 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4131 | $sig->send; |
4179 | $sig->send; |
4132 | } |
4180 | } |
4133 | while (my $sig = shift @WAIT_FOR_TICK) { |
4181 | $WAIT_FOR_TICK->broadcast; |
4134 | $sig->send; |
|
|
4135 | } |
|
|
4136 | |
4182 | |
4137 | $LOAD = ($NOW - $TICK_START) / $TICK; |
4183 | $LOAD = ($NOW - $TICK_START) / $TICK; |
4138 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
4184 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
4139 | |
4185 | |
4140 | if (0) { |
4186 | if (0) { |
… | |
… | |
4149 | { |
4195 | { |
4150 | # configure BDB |
4196 | # configure BDB |
4151 | |
4197 | |
4152 | BDB::min_parallel 16; |
4198 | BDB::min_parallel 16; |
4153 | BDB::max_poll_reqs $TICK * 0.1; |
4199 | BDB::max_poll_reqs $TICK * 0.1; |
4154 | $AnyEvent::BDB::WATCHER->priority (1); |
4200 | #$AnyEvent::BDB::WATCHER->priority (1); |
4155 | |
4201 | |
4156 | unless ($DB_ENV) { |
4202 | unless ($DB_ENV) { |
4157 | $DB_ENV = BDB::db_env_create; |
4203 | $DB_ENV = BDB::db_env_create; |
4158 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
4204 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
4159 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
4205 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |