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 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. |
… | |
… | |
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 |
… | |
… | |
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 | |
134 | |
135 | our $UPTIME; $UPTIME ||= time; |
135 | our $UPTIME; $UPTIME ||= time; |
136 | our $RUNTIME; |
136 | our $RUNTIME = 0; |
|
|
137 | our $SERVER_TICK = 0; |
137 | our $NOW; |
138 | our $NOW; |
138 | |
139 | |
139 | our (%PLAYER, %PLAYER_LOADING); # all users |
140 | our (%PLAYER, %PLAYER_LOADING); # all users |
140 | our (%MAP, %MAP_LOADING ); # all maps |
141 | our (%MAP, %MAP_LOADING ); # all maps |
141 | our $LINK_MAP; # the special {link} map, which is always available |
142 | our $LINK_MAP; # the special {link} map, which is always available |
… | |
… | |
150 | |
151 | |
151 | our @POST_INIT; |
152 | our @POST_INIT; |
152 | |
153 | |
153 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
154 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
154 | our $REALLY_UNLOOP; # never set to true, please :) |
155 | our $REALLY_UNLOOP; # never set to true, please :) |
|
|
156 | |
|
|
157 | our $WAIT_FOR_TICK = new Coro::Signal; |
|
|
158 | our @WAIT_FOR_TICK_BEGIN; |
155 | |
159 | |
156 | binmode STDOUT; |
160 | binmode STDOUT; |
157 | binmode STDERR; |
161 | binmode STDERR; |
158 | |
162 | |
159 | # read virtual server time, if available |
163 | # read virtual server time, if available |
… | |
… | |
193 | |
197 | |
194 | =over 4 |
198 | =over 4 |
195 | |
199 | |
196 | =item $cf::UPTIME |
200 | =item $cf::UPTIME |
197 | |
201 | |
198 | The timestamp of the server start (so not actually an uptime). |
202 | The timestamp of the server start (so not actually an "uptime"). |
|
|
203 | |
|
|
204 | =item $cf::SERVER_TICK |
|
|
205 | |
|
|
206 | An unsigned integer that starts at zero when the server is started and is |
|
|
207 | incremented on every tick. |
|
|
208 | |
|
|
209 | =item $cf::NOW |
|
|
210 | |
|
|
211 | The (real) time of the last (current) server tick - updated before and |
|
|
212 | after tick processing, so this is useful only as a rough "what time is it |
|
|
213 | now" estimate. |
|
|
214 | |
|
|
215 | =item $cf::TICK |
|
|
216 | |
|
|
217 | The interval between each server tick, in seconds. |
199 | |
218 | |
200 | =item $cf::RUNTIME |
219 | =item $cf::RUNTIME |
201 | |
220 | |
202 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
221 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
203 | every server tick. |
222 | every server tick. |
… | |
… | |
209 | Various directories - "/etc", read-only install directory, perl-library |
228 | Various directories - "/etc", read-only install directory, perl-library |
210 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
229 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
211 | unique-items directory, player file directory, random maps directory and |
230 | unique-items directory, player file directory, random maps directory and |
212 | database environment. |
231 | database environment. |
213 | |
232 | |
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 |
233 | =item $cf::LOADAVG |
223 | |
234 | |
224 | The current CPU load on the server (alpha-smoothed), as a value between 0 |
235 | 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 |
236 | (none) and 1 (overloaded), indicating how much time is spent on processing |
226 | objects per tick. Healthy values are < 0.5. |
237 | objects per tick. Healthy values are < 0.5. |
… | |
… | |
237 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
248 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
238 | |
249 | |
239 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
250 | 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 |
251 | 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. |
252 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
253 | |
|
|
254 | Note that cf::Wait_for_tick will immediately return when the server is not |
|
|
255 | ticking, making it suitable for small pauses in threads that need to run |
|
|
256 | when the server is paused. If that is not applicable (i.e. you I<really> |
|
|
257 | want to wait, use C<$cf::WAIT_FOR_TICK>). |
|
|
258 | |
|
|
259 | =item $cf::WAIT_FOR_TICK |
|
|
260 | |
|
|
261 | Note that C<cf::wait_for_tick> is probably the correct thing to use. This |
|
|
262 | variable contains a L<Coro::Signal> that is broadcats after every server |
|
|
263 | tick. Calling C<< ->wait >> on it will suspend the caller until after the |
|
|
264 | next server tick. |
242 | |
265 | |
243 | =cut |
266 | =cut |
244 | |
267 | |
245 | sub wait_for_tick(); |
268 | sub wait_for_tick(); |
246 | sub wait_for_tick_begin(); |
269 | sub wait_for_tick_begin(); |
… | |
… | |
545 | } |
568 | } |
546 | } |
569 | } |
547 | |
570 | |
548 | if (@SLOT_QUEUE) { |
571 | if (@SLOT_QUEUE) { |
549 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
572 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
550 | push @cf::WAIT_FOR_TICK, $signal; |
573 | $WAIT_FOR_TICK->wait; |
551 | $signal->wait; |
|
|
552 | } else { |
574 | } else { |
553 | $busy = 0; |
575 | $busy = 0; |
554 | Coro::schedule; |
576 | Coro::schedule; |
555 | } |
577 | } |
556 | } |
578 | } |
… | |
… | |
1439 | my ($pl, $buf) = @_; |
1461 | my ($pl, $buf) = @_; |
1440 | |
1462 | |
1441 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1463 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1442 | |
1464 | |
1443 | if (ref $msg) { |
1465 | if (ref $msg) { |
1444 | my ($type, $reply, @payload) = |
1466 | 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 | |
1467 | |
1449 | my @reply; |
1468 | my @reply; |
1450 | |
1469 | |
1451 | if (my $cb = $EXTCMD{$type}) { |
1470 | if (my $cb = $EXTCMD{$type}) { |
1452 | @reply = $cb->($pl, @payload); |
1471 | @reply = $cb->($pl, @payload); |
… | |
… | |
1555 | |
1574 | |
1556 | while (my ($k, $v) = each %todo) { |
1575 | while (my ($k, $v) = each %todo) { |
1557 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1576 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1558 | if exists $v->{meta}{mandatory}; |
1577 | if exists $v->{meta}{mandatory}; |
1559 | } |
1578 | } |
|
|
1579 | |
|
|
1580 | last; |
1560 | } |
1581 | } |
1561 | } |
1582 | } |
1562 | }; |
1583 | }; |
1563 | } |
1584 | } |
1564 | |
1585 | |
… | |
… | |
1992 | |
2013 | |
1993 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
2014 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1994 | () |
2015 | () |
1995 | } |
2016 | } |
1996 | |
2017 | |
|
|
2018 | # may re-bless or do other evil things |
1997 | sub init { |
2019 | sub init { |
1998 | my ($self) = @_; |
2020 | my ($self) = @_; |
1999 | |
2021 | |
2000 | $self |
2022 | $self |
2001 | } |
2023 | } |
… | |
… | |
2066 | $self->{load_path} = $path; |
2088 | $self->{load_path} = $path; |
2067 | |
2089 | |
2068 | 1 |
2090 | 1 |
2069 | } |
2091 | } |
2070 | |
2092 | |
|
|
2093 | # used to laod the header of an original map |
2071 | sub load_header_orig { |
2094 | sub load_header_orig { |
2072 | my ($self) = @_; |
2095 | my ($self) = @_; |
2073 | |
2096 | |
2074 | $self->load_header_from ($self->load_path) |
2097 | $self->load_header_from ($self->load_path) |
2075 | } |
2098 | } |
2076 | |
2099 | |
|
|
2100 | # used to laod the header of an instantiated map |
2077 | sub load_header_temp { |
2101 | sub load_header_temp { |
2078 | my ($self) = @_; |
2102 | my ($self) = @_; |
2079 | |
2103 | |
2080 | $self->load_header_from ($self->save_path) |
2104 | $self->load_header_from ($self->save_path) |
2081 | } |
2105 | } |
2082 | |
2106 | |
|
|
2107 | # called after loading the header from an instantiated map |
2083 | sub prepare_temp { |
2108 | sub prepare_temp { |
2084 | my ($self) = @_; |
2109 | my ($self) = @_; |
2085 | |
2110 | |
2086 | $self->last_access ((delete $self->{last_access}) |
2111 | $self->last_access ((delete $self->{last_access}) |
2087 | || $cf::RUNTIME); #d# |
2112 | || $cf::RUNTIME); #d# |
2088 | # safety |
2113 | # safety |
2089 | $self->{instantiate_time} = $cf::RUNTIME |
2114 | $self->{instantiate_time} = $cf::RUNTIME |
2090 | if $self->{instantiate_time} > $cf::RUNTIME; |
2115 | if $self->{instantiate_time} > $cf::RUNTIME; |
2091 | } |
2116 | } |
2092 | |
2117 | |
|
|
2118 | # called after loading the header from an original map |
2093 | sub prepare_orig { |
2119 | sub prepare_orig { |
2094 | my ($self) = @_; |
2120 | my ($self) = @_; |
2095 | |
2121 | |
2096 | $self->{load_original} = 1; |
2122 | $self->{load_original} = 1; |
2097 | $self->{instantiate_time} = $cf::RUNTIME; |
2123 | $self->{instantiate_time} = $cf::RUNTIME; |
… | |
… | |
2123 | sub find { |
2149 | sub find { |
2124 | my ($path, $origin) = @_; |
2150 | my ($path, $origin) = @_; |
2125 | |
2151 | |
2126 | cf::cede_to_tick; |
2152 | cf::cede_to_tick; |
2127 | |
2153 | |
2128 | $path = normalise $path, $origin && $origin->path; |
2154 | $path = normalise $path, $origin; |
2129 | |
2155 | |
2130 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2156 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2131 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2157 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2132 | |
2158 | |
2133 | $cf::MAP{$path} || do { |
2159 | $cf::MAP{$path} || do { |
… | |
… | |
2164 | |
2190 | |
2165 | { |
2191 | { |
2166 | my $guard = cf::lock_acquire "map_data:$path"; |
2192 | my $guard = cf::lock_acquire "map_data:$path"; |
2167 | |
2193 | |
2168 | return unless $self->valid; |
2194 | return unless $self->valid; |
2169 | return unless $self->in_memory == cf::MAP_SWAPPED; |
2195 | return unless $self->state == cf::MAP_SWAPPED; |
2170 | |
|
|
2171 | $self->in_memory (cf::MAP_LOADING); |
|
|
2172 | |
2196 | |
2173 | $self->alloc; |
2197 | $self->alloc; |
2174 | |
2198 | |
2175 | $self->pre_load; |
2199 | $self->pre_load; |
2176 | cf::cede_to_tick; |
2200 | cf::cede_to_tick; |
2177 | |
2201 | |
|
|
2202 | if (exists $self->{load_path}) { |
2178 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2203 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2179 | $f->skip_block; |
2204 | $f->skip_block; |
2180 | $self->_load_objects ($f) |
2205 | $self->_load_objects ($f) |
2181 | or return; |
2206 | or return; |
2182 | |
2207 | |
2183 | $self->post_load_original |
2208 | $self->post_load_original |
2184 | if delete $self->{load_original}; |
2209 | if delete $self->{load_original}; |
2185 | |
2210 | |
2186 | if (my $uniq = $self->uniq_path) { |
2211 | if (my $uniq = $self->uniq_path) { |
2187 | utf8::encode $uniq; |
2212 | utf8::encode $uniq; |
2188 | unless (aio_stat $uniq) { |
2213 | unless (aio_stat $uniq) { |
2189 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2214 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2190 | $self->clear_unique_items; |
2215 | $self->clear_unique_items; |
2191 | $self->_load_objects ($f); |
2216 | $self->_load_objects ($f); |
2192 | $f->resolve_delayed_derefs; |
2217 | $f->resolve_delayed_derefs; |
|
|
2218 | } |
2193 | } |
2219 | } |
2194 | } |
2220 | } |
2195 | } |
|
|
2196 | |
2221 | |
2197 | $f->resolve_delayed_derefs; |
2222 | $f->resolve_delayed_derefs; |
|
|
2223 | } else { |
|
|
2224 | $self->post_load_original |
|
|
2225 | if delete $self->{load_original}; |
|
|
2226 | } |
|
|
2227 | |
|
|
2228 | $self->state (cf::MAP_INACTIVE); |
2198 | |
2229 | |
2199 | cf::cede_to_tick; |
2230 | cf::cede_to_tick; |
2200 | # now do the right thing for maps |
2231 | # now do the right thing for maps |
2201 | $self->link_multipart_objects; |
2232 | $self->link_multipart_objects; |
2202 | $self->difficulty ($self->estimate_difficulty) |
2233 | $self->difficulty ($self->estimate_difficulty) |
… | |
… | |
2206 | unless ($self->{deny_activate}) { |
2237 | unless ($self->{deny_activate}) { |
2207 | $self->decay_objects; |
2238 | $self->decay_objects; |
2208 | $self->fix_auto_apply; |
2239 | $self->fix_auto_apply; |
2209 | $self->update_buttons; |
2240 | $self->update_buttons; |
2210 | cf::cede_to_tick; |
2241 | cf::cede_to_tick; |
2211 | $self->activate; |
2242 | #$self->activate; # no longer activate maps automatically |
2212 | } |
2243 | } |
2213 | |
2244 | |
2214 | $self->{last_save} = $cf::RUNTIME; |
2245 | $self->{last_save} = $cf::RUNTIME; |
2215 | $self->last_access ($cf::RUNTIME); |
2246 | $self->last_access ($cf::RUNTIME); |
2216 | |
|
|
2217 | $self->in_memory (cf::MAP_ACTIVE); |
|
|
2218 | } |
2247 | } |
2219 | |
2248 | |
2220 | $self->post_load; |
2249 | $self->post_load; |
2221 | |
2250 | |
2222 | 1 |
2251 | 1 |
… | |
… | |
2235 | # if $self->per_party; |
2264 | # if $self->per_party; |
2236 | |
2265 | |
2237 | $self |
2266 | $self |
2238 | } |
2267 | } |
2239 | |
2268 | |
2240 | # find and load all maps in the 3x3 area around a map |
|
|
2241 | sub load_neighbours { |
|
|
2242 | my ($map) = @_; |
|
|
2243 | |
|
|
2244 | my @neigh; # diagonal neighbours |
|
|
2245 | |
|
|
2246 | for (0 .. 3) { |
|
|
2247 | my $neigh = $map->tile_path ($_) |
|
|
2248 | or next; |
|
|
2249 | $neigh = find $neigh, $map |
|
|
2250 | or next; |
|
|
2251 | $neigh->load; |
|
|
2252 | |
|
|
2253 | # now find the diagonal neighbours |
|
|
2254 | push @neigh, |
|
|
2255 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
|
|
2256 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
|
|
2257 | } |
|
|
2258 | |
|
|
2259 | for (grep defined $_->[0], @neigh) { |
|
|
2260 | my ($path, $origin) = @$_; |
|
|
2261 | my $neigh = find $path, $origin |
|
|
2262 | or next; |
|
|
2263 | $neigh->load; |
|
|
2264 | } |
|
|
2265 | } |
|
|
2266 | |
|
|
2267 | sub find_sync { |
2269 | sub find_sync { |
2268 | my ($path, $origin) = @_; |
2270 | my ($path, $origin) = @_; |
2269 | |
2271 | |
|
|
2272 | # it's a bug to call this from the main context |
2270 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
2273 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
2271 | if $Coro::current == $Coro::main; |
2274 | if $Coro::current == $Coro::main; |
2272 | |
2275 | |
2273 | find $path, $origin |
2276 | find $path, $origin |
2274 | } |
2277 | } |
2275 | |
2278 | |
2276 | sub do_load_sync { |
2279 | sub do_load_sync { |
2277 | my ($map) = @_; |
2280 | my ($map) = @_; |
2278 | |
2281 | |
|
|
2282 | # it's a bug to call this from the main context |
2279 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2283 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2280 | if $Coro::current == $Coro::main; |
2284 | if $Coro::current == $Coro::main; |
2281 | |
2285 | |
2282 | $map->load; |
2286 | $map->load; |
2283 | } |
2287 | } |
… | |
… | |
2286 | our $MAP_PREFETCHER = undef; |
2290 | our $MAP_PREFETCHER = undef; |
2287 | |
2291 | |
2288 | sub find_async { |
2292 | sub find_async { |
2289 | my ($path, $origin, $load) = @_; |
2293 | my ($path, $origin, $load) = @_; |
2290 | |
2294 | |
2291 | $path = normalise $path, $origin && $origin->{path}; |
2295 | $path = normalise $path, $origin; |
2292 | |
2296 | |
2293 | if (my $map = $cf::MAP{$path}) { |
2297 | if (my $map = $cf::MAP{$path}) { |
2294 | return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; |
2298 | return $map if !$load || $map->linkable; |
2295 | } |
2299 | } |
2296 | |
2300 | |
2297 | $MAP_PREFETCH{$path} |= $load; |
2301 | $MAP_PREFETCH{$path} |= $load; |
2298 | |
2302 | |
2299 | $MAP_PREFETCHER ||= cf::async { |
2303 | $MAP_PREFETCHER ||= cf::async { |
… | |
… | |
2358 | sub swap_out { |
2362 | sub swap_out { |
2359 | my ($self) = @_; |
2363 | my ($self) = @_; |
2360 | |
2364 | |
2361 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2365 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2362 | |
2366 | |
2363 | return if $self->in_memory != cf::MAP_ACTIVE; |
2367 | return if !$self->linkable; |
2364 | return if $self->{deny_save}; |
2368 | return if $self->{deny_save}; |
2365 | return if $self->players; |
2369 | return if $self->players; |
2366 | |
2370 | |
2367 | # first deactivate the map and "unlink" it from the core |
2371 | # first deactivate the map and "unlink" it from the core |
2368 | $self->deactivate; |
2372 | $self->deactivate; |
2369 | $_->clear_links_to ($self) for values %cf::MAP; |
2373 | $_->clear_links_to ($self) for values %cf::MAP; |
2370 | $self->in_memory (cf::MAP_SWAPPED); |
2374 | $self->state (cf::MAP_SWAPPED); |
2371 | |
2375 | |
2372 | # then atomically save |
2376 | # then atomically save |
2373 | $self->_save; |
2377 | $self->_save; |
2374 | |
2378 | |
2375 | # then free the map |
2379 | # then free the map |
… | |
… | |
2401 | |
2405 | |
2402 | return if $self->players; |
2406 | return if $self->players; |
2403 | |
2407 | |
2404 | cf::trace "resetting map ", $self->path, "\n"; |
2408 | cf::trace "resetting map ", $self->path, "\n"; |
2405 | |
2409 | |
2406 | $self->in_memory (cf::MAP_SWAPPED); |
2410 | $self->state (cf::MAP_SWAPPED); |
2407 | |
2411 | |
2408 | # need to save uniques path |
2412 | # need to save uniques path |
2409 | unless ($self->{deny_save}) { |
2413 | unless ($self->{deny_save}) { |
2410 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
2414 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
2411 | |
2415 | |
… | |
… | |
2741 | # use -1 or undef as default coordinates, not 0, 0 |
2745 | # use -1 or undef as default coordinates, not 0, 0 |
2742 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2746 | ($x, $y) = ($map->enter_x, $map->enter_y) |
2743 | if $x <= 0 && $y <= 0; |
2747 | if $x <= 0 && $y <= 0; |
2744 | |
2748 | |
2745 | $map->load; |
2749 | $map->load; |
2746 | $map->load_neighbours; |
|
|
2747 | |
2750 | |
2748 | return unless $self->contr->active; |
2751 | return unless $self->contr->active; |
2749 | |
2752 | |
2750 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2753 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2751 | if ($self->enter_map ($map, $x, $y)) { |
2754 | if ($self->enter_map ($map, $x, $y)) { |
… | |
… | |
2807 | ($path, $x, $y) = (undef, undef, undef); |
2810 | ($path, $x, $y) = (undef, undef, undef); |
2808 | } |
2811 | } |
2809 | } |
2812 | } |
2810 | |
2813 | |
2811 | my $map = eval { |
2814 | my $map = eval { |
2812 | my $map = defined $path ? cf::map::find $path : undef; |
2815 | my $map = defined $path ? cf::map::find $path, $self->map : undef; |
2813 | |
2816 | |
2814 | if ($map) { |
2817 | if ($map) { |
2815 | $map = $map->customise_for ($self); |
2818 | $map = $map->customise_for ($self); |
2816 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2819 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2817 | } else { |
2820 | } else { |
… | |
… | |
2907 | $Coro::current->{desc} = "enter_exit"; |
2910 | $Coro::current->{desc} = "enter_exit"; |
2908 | |
2911 | |
2909 | unless (eval { |
2912 | unless (eval { |
2910 | $self->deactivate_recursive; # just to be sure |
2913 | $self->deactivate_recursive; # just to be sure |
2911 | |
2914 | |
2912 | # random map handling |
|
|
2913 | { |
|
|
2914 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
|
|
2915 | |
|
|
2916 | prepare_random_map $exit |
|
|
2917 | if $exit->slaying eq "/!"; |
|
|
2918 | } |
|
|
2919 | |
|
|
2920 | my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2915 | my $map = cf::map::normalise $exit->slaying, $exit->map; |
2921 | my $x = $exit->stats->hp; |
2916 | my $x = $exit->stats->hp; |
2922 | my $y = $exit->stats->sp; |
2917 | my $y = $exit->stats->sp; |
|
|
2918 | |
|
|
2919 | # special map handling |
|
|
2920 | my $slaying = $exit->slaying; |
|
|
2921 | |
|
|
2922 | # special map handling |
|
|
2923 | if ($slaying eq "/!") { |
|
|
2924 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
|
|
2925 | |
|
|
2926 | prepare_random_map $exit |
|
|
2927 | if $exit->slaying eq "/!"; # need to re-check after getting the lock |
|
|
2928 | |
|
|
2929 | $map = $exit->slaying; |
|
|
2930 | |
|
|
2931 | } elsif ($slaying eq '!up') { |
|
|
2932 | $map = $exit->map->tile_path (cf::TILE_UP); |
|
|
2933 | $x = $exit->x; |
|
|
2934 | $y = $exit->y; |
|
|
2935 | |
|
|
2936 | } elsif ($slaying eq '!down') { |
|
|
2937 | $map = $exit->map->tile_path (cf::TILE_DOWN); |
|
|
2938 | $x = $exit->x; |
|
|
2939 | $y = $exit->y; |
|
|
2940 | } |
2923 | |
2941 | |
2924 | $self->goto ($map, $x, $y); |
2942 | $self->goto ($map, $x, $y); |
2925 | |
2943 | |
2926 | # if exit is damned, update players death & WoR home-position |
2944 | # if exit is damned, update players death & WoR home-position |
2927 | $self->contr->savebed ($map, $x, $y) |
2945 | $self->contr->savebed ($map, $x, $y) |
… | |
… | |
3169 | =cut |
3187 | =cut |
3170 | |
3188 | |
3171 | sub cf::client::ext_reply($$@) { |
3189 | sub cf::client::ext_reply($$@) { |
3172 | my ($self, $id, @msg) = @_; |
3190 | my ($self, $id, @msg) = @_; |
3173 | |
3191 | |
3174 | if ($self->extcmd == 2) { |
3192 | return unless $self->extcmd == 2; |
|
|
3193 | |
3175 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3194 | $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); |
3176 | } elsif ($self->extcmd == 1) { |
|
|
3177 | #TODO: version 1, remove |
|
|
3178 | unshift @msg, msgtype => "reply", msgid => $id; |
|
|
3179 | $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg})); |
|
|
3180 | } |
|
|
3181 | } |
3195 | } |
3182 | |
3196 | |
3183 | =item $success = $client->query ($flags, "text", \&cb) |
3197 | =item $success = $client->query ($flags, "text", \&cb) |
3184 | |
3198 | |
3185 | Queues a query to the client, calling the given callback with |
3199 | Queues a query to the client, calling the given callback with |
… | |
… | |
3240 | my ($ns, $buf) = @_; |
3254 | my ($ns, $buf) = @_; |
3241 | |
3255 | |
3242 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
3256 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
3243 | |
3257 | |
3244 | if (ref $msg) { |
3258 | if (ref $msg) { |
3245 | my ($type, $reply, @payload) = |
3259 | my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash |
3246 | "ARRAY" eq ref $msg |
|
|
3247 | ? @$msg |
|
|
3248 | : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove |
|
|
3249 | |
3260 | |
3250 | my @reply; |
3261 | my @reply; |
3251 | |
3262 | |
3252 | if (my $cb = $EXTICMD{$type}) { |
3263 | if (my $cb = $EXTICMD{$type}) { |
3253 | @reply = $cb->($ns, @payload); |
3264 | @reply = $cb->($ns, @payload); |
… | |
… | |
3503 | |
3514 | |
3504 | { |
3515 | { |
3505 | my $res = $facedata->{resource}; |
3516 | my $res = $facedata->{resource}; |
3506 | |
3517 | |
3507 | while (my ($name, $info) = each %$res) { |
3518 | while (my ($name, $info) = each %$res) { |
3508 | if (defined $info->{type}) { |
3519 | if (defined (my $type = $info->{type})) { |
|
|
3520 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3509 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3521 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3510 | |
3522 | |
3511 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3523 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3512 | cf::face::set_type $idx, $info->{type}; |
3524 | cf::face::set_type $idx, $type; |
|
|
3525 | cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3513 | } else { |
3526 | } else { |
3514 | $RESOURCE{$name} = $info; # unused |
3527 | # $RESOURCE{$name} = $info; # unused |
3515 | } |
3528 | } |
3516 | |
3529 | |
3517 | cf::cede_to_tick; |
3530 | cf::cede_to_tick; |
3518 | } |
3531 | } |
3519 | } |
3532 | } |
… | |
… | |
3655 | |
3668 | |
3656 | sub main { |
3669 | sub main { |
3657 | cf::init_globals; # initialise logging |
3670 | cf::init_globals; # initialise logging |
3658 | |
3671 | |
3659 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3672 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3660 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3673 | LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3661 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3674 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3662 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3675 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3663 | |
3676 | |
3664 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3677 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3665 | |
3678 | |
… | |
… | |
3707 | }; |
3720 | }; |
3708 | |
3721 | |
3709 | cf::object::thawer::errors_are_fatal 0; |
3722 | cf::object::thawer::errors_are_fatal 0; |
3710 | info "parse errors in files are no longer fatal from this point on.\n"; |
3723 | info "parse errors in files are no longer fatal from this point on.\n"; |
3711 | |
3724 | |
3712 | my $free_main; $free_main = EV::idle sub { |
3725 | AE::postpone { |
3713 | undef $free_main; |
|
|
3714 | undef &main; # free gobs of memory :) |
3726 | undef &main; # free gobs of memory :) |
3715 | }; |
3727 | }; |
3716 | |
3728 | |
3717 | goto &main_loop; |
3729 | goto &main_loop; |
3718 | } |
3730 | } |
… | |
… | |
3875 | |
3887 | |
3876 | cf::write_runtime_sync; # external watchdog should not bark |
3888 | cf::write_runtime_sync; # external watchdog should not bark |
3877 | |
3889 | |
3878 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3890 | trace "emergency_perl_save: flushing outstanding aio requests"; |
3879 | while (IO::AIO::nreqs || BDB::nreqs) { |
3891 | while (IO::AIO::nreqs || BDB::nreqs) { |
3880 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
3892 | Coro::AnyEvent::sleep 0.01; # let the sync_job do it's thing |
3881 | } |
3893 | } |
3882 | |
3894 | |
3883 | cf::write_runtime_sync; # external watchdog should not bark |
3895 | cf::write_runtime_sync; # external watchdog should not bark |
3884 | }; |
3896 | }; |
3885 | |
3897 | |
… | |
… | |
4066 | |
4078 | |
4067 | ############################################################################# |
4079 | ############################################################################# |
4068 | |
4080 | |
4069 | my $bug_warning = 0; |
4081 | my $bug_warning = 0; |
4070 | |
4082 | |
4071 | our @WAIT_FOR_TICK; |
|
|
4072 | our @WAIT_FOR_TICK_BEGIN; |
|
|
4073 | |
|
|
4074 | sub wait_for_tick() { |
4083 | sub wait_for_tick() { |
4075 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4084 | return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main; |
4076 | |
4085 | |
4077 | my $signal = new Coro::Signal; |
4086 | $WAIT_FOR_TICK->wait; |
4078 | push @WAIT_FOR_TICK, $signal; |
|
|
4079 | $signal->wait; |
|
|
4080 | } |
4087 | } |
4081 | |
4088 | |
4082 | sub wait_for_tick_begin() { |
4089 | sub wait_for_tick_begin() { |
4083 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4090 | return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main; |
4084 | |
4091 | |
4085 | my $signal = new Coro::Signal; |
4092 | my $signal = new Coro::Signal; |
4086 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4093 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4087 | $signal->wait; |
4094 | $signal->wait; |
4088 | } |
4095 | } |
… | |
… | |
4092 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
4099 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
4093 | unless ++$bug_warning > 10; |
4100 | unless ++$bug_warning > 10; |
4094 | return; |
4101 | return; |
4095 | } |
4102 | } |
4096 | |
4103 | |
4097 | cf::server_tick; # one server iteration |
4104 | cf::one_tick; # one server iteration |
4098 | |
4105 | |
4099 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
4106 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
4100 | |
4107 | |
4101 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4108 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4102 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4109 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
… | |
… | |
4108 | } |
4115 | } |
4109 | |
4116 | |
4110 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4117 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4111 | $sig->send; |
4118 | $sig->send; |
4112 | } |
4119 | } |
4113 | while (my $sig = shift @WAIT_FOR_TICK) { |
4120 | $WAIT_FOR_TICK->broadcast; |
4114 | $sig->send; |
|
|
4115 | } |
|
|
4116 | |
4121 | |
4117 | $LOAD = ($NOW - $TICK_START) / $TICK; |
4122 | $LOAD = ($NOW - $TICK_START) / $TICK; |
4118 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
4123 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
4119 | |
4124 | |
4120 | if (0) { |
4125 | if (0) { |
… | |
… | |
4129 | { |
4134 | { |
4130 | # configure BDB |
4135 | # configure BDB |
4131 | |
4136 | |
4132 | BDB::min_parallel 16; |
4137 | BDB::min_parallel 16; |
4133 | BDB::max_poll_reqs $TICK * 0.1; |
4138 | BDB::max_poll_reqs $TICK * 0.1; |
4134 | $AnyEvent::BDB::WATCHER->priority (1); |
4139 | #$AnyEvent::BDB::WATCHER->priority (1); |
4135 | |
4140 | |
4136 | unless ($DB_ENV) { |
4141 | unless ($DB_ENV) { |
4137 | $DB_ENV = BDB::db_env_create; |
4142 | $DB_ENV = BDB::db_env_create; |
4138 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
4143 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
4139 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
4144 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |