ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.560 by root, Fri Apr 22 02:03:12 2011 UTC vs.
Revision 1.573 by root, Sun May 8 12:40:42 2011 UTC

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 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.
131our @EXTRA_MODULES = qw(pod match mapscript incloader); 131our @EXTRA_MODULES = qw(pod match mapscript incloader);
132 132
133our %CFG; 133our %CFG;
134 134
135our $UPTIME; $UPTIME ||= time; 135our $UPTIME; $UPTIME ||= time;
136our $RUNTIME; 136our $RUNTIME = 0;
137our $SERVER_TICK = 0;
137our $NOW; 138our $NOW;
138 139
139our (%PLAYER, %PLAYER_LOADING); # all users 140our (%PLAYER, %PLAYER_LOADING); # all users
140our (%MAP, %MAP_LOADING ); # all maps 141our (%MAP, %MAP_LOADING ); # all maps
141our $LINK_MAP; # the special {link} map, which is always available 142our $LINK_MAP; # the special {link} map, which is always available
150 151
151our @POST_INIT; 152our @POST_INIT;
152 153
153our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) 154our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow)
154our $REALLY_UNLOOP; # never set to true, please :) 155our $REALLY_UNLOOP; # never set to true, please :)
156
157our $WAIT_FOR_TICK = new Coro::Signal;
158our @WAIT_FOR_TICK_BEGIN;
155 159
156binmode STDOUT; 160binmode STDOUT;
157binmode STDERR; 161binmode 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
198The timestamp of the server start (so not actually an uptime). 202The timestamp of the server start (so not actually an "uptime").
203
204=item $cf::SERVER_TICK
205
206An unsigned integer that starts at zero when the server is started and is
207incremented on every tick.
208
209=item $cf::NOW
210
211The (real) time of the last (current) server tick - updated before and
212after tick processing, so this is useful only as a rough "what time is it
213now" estimate.
214
215=item $cf::TICK
216
217The interval between each server tick, in seconds.
199 218
200=item $cf::RUNTIME 219=item $cf::RUNTIME
201 220
202The time this server has run, starts at 0 and is increased by $cf::TICK on 221The time this server has run, starts at 0 and is increased by $cf::TICK on
203every server tick. 222every server tick.
209Various directories - "/etc", read-only install directory, perl-library 228Various directories - "/etc", read-only install directory, perl-library
210directory, pod-directory, read-only maps directory, "/var", "/var/tmp", 229directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
211unique-items directory, player file directory, random maps directory and 230unique-items directory, player file directory, random maps directory and
212database environment. 231database environment.
213 232
214=item $cf::NOW
215
216The time of the last (current) server tick.
217
218=item $cf::TICK
219
220The interval between server ticks, in seconds.
221
222=item $cf::LOADAVG 233=item $cf::LOADAVG
223 234
224The current CPU load on the server (alpha-smoothed), as a value between 0 235The 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
226objects per tick. Healthy values are < 0.5. 237objects 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
239These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only 250These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
240returns directly I<after> the tick processing (and consequently, can only wake one thread 251returns directly I<after> the tick processing (and consequently, can only wake one thread
241per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 252per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
253
254Note that cf::Wait_for_tick will immediately return when the server is not
255ticking, making it suitable for small pauses in threads that need to run
256when the server is paused. If that is not applicable (i.e. you I<really>
257want to wait, use C<$cf::WAIT_FOR_TICK>).
258
259=item $cf::WAIT_FOR_TICK
260
261Note that C<cf::wait_for_tick> is probably the correct thing to use. This
262variable contains a L<Coro::Signal> that is broadcats after every server
263tick. Calling C<< ->wait >> on it will suspend the caller until after the
264next server tick.
242 265
243=cut 266=cut
244 267
245sub wait_for_tick(); 268sub wait_for_tick();
246sub wait_for_tick_begin(); 269sub 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 }
1555 1577
1556 while (my ($k, $v) = each %todo) { 1578 while (my ($k, $v) = each %todo) {
1557 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." 1579 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting."
1558 if exists $v->{meta}{mandatory}; 1580 if exists $v->{meta}{mandatory};
1559 } 1581 }
1582
1583 last;
1560 } 1584 }
1561 } 1585 }
1562 }; 1586 };
1563} 1587}
1564 1588
1992 2016
1993 Carp::cluck "unable to resolve path '$path' (base '$base')"; 2017 Carp::cluck "unable to resolve path '$path' (base '$base')";
1994 () 2018 ()
1995} 2019}
1996 2020
2021# may re-bless or do other evil things
1997sub init { 2022sub init {
1998 my ($self) = @_; 2023 my ($self) = @_;
1999 2024
2000 $self 2025 $self
2001} 2026}
2066 $self->{load_path} = $path; 2091 $self->{load_path} = $path;
2067 2092
2068 1 2093 1
2069} 2094}
2070 2095
2096# used to laod the header of an original map
2071sub load_header_orig { 2097sub load_header_orig {
2072 my ($self) = @_; 2098 my ($self) = @_;
2073 2099
2074 $self->load_header_from ($self->load_path) 2100 $self->load_header_from ($self->load_path)
2075} 2101}
2076 2102
2103# used to laod the header of an instantiated map
2077sub load_header_temp { 2104sub load_header_temp {
2078 my ($self) = @_; 2105 my ($self) = @_;
2079 2106
2080 $self->load_header_from ($self->save_path) 2107 $self->load_header_from ($self->save_path)
2081} 2108}
2082 2109
2110# called after loading the header from an instantiated map
2083sub prepare_temp { 2111sub prepare_temp {
2084 my ($self) = @_; 2112 my ($self) = @_;
2085 2113
2086 $self->last_access ((delete $self->{last_access}) 2114 $self->last_access ((delete $self->{last_access})
2087 || $cf::RUNTIME); #d# 2115 || $cf::RUNTIME); #d#
2088 # safety 2116 # safety
2089 $self->{instantiate_time} = $cf::RUNTIME 2117 $self->{instantiate_time} = $cf::RUNTIME
2090 if $self->{instantiate_time} > $cf::RUNTIME; 2118 if $self->{instantiate_time} > $cf::RUNTIME;
2091} 2119}
2092 2120
2121# called after loading the header from an original map
2093sub prepare_orig { 2122sub prepare_orig {
2094 my ($self) = @_; 2123 my ($self) = @_;
2095 2124
2096 $self->{load_original} = 1; 2125 $self->{load_original} = 1;
2097 $self->{instantiate_time} = $cf::RUNTIME; 2126 $self->{instantiate_time} = $cf::RUNTIME;
2123sub find { 2152sub find {
2124 my ($path, $origin) = @_; 2153 my ($path, $origin) = @_;
2125 2154
2126 cf::cede_to_tick; 2155 cf::cede_to_tick;
2127 2156
2128 $path = normalise $path, $origin && $origin->path; 2157 $path = normalise $path, $origin;
2129 2158
2130 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2159 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2131 my $guard2 = cf::lock_acquire "map_find:$path"; 2160 my $guard2 = cf::lock_acquire "map_find:$path";
2132 2161
2133 $cf::MAP{$path} || do { 2162 $cf::MAP{$path} || do {
2164 2193
2165 { 2194 {
2166 my $guard = cf::lock_acquire "map_data:$path"; 2195 my $guard = cf::lock_acquire "map_data:$path";
2167 2196
2168 return unless $self->valid; 2197 return unless $self->valid;
2169 return unless $self->in_memory == cf::MAP_SWAPPED; 2198 return unless $self->state == cf::MAP_SWAPPED;
2170
2171 $self->in_memory (cf::MAP_LOADING);
2172 2199
2173 $self->alloc; 2200 $self->alloc;
2174 2201
2175 $self->pre_load; 2202 $self->pre_load;
2176 cf::cede_to_tick; 2203 cf::cede_to_tick;
2177 2204
2205 if (exists $self->{load_path}) {
2178 my $f = new_from_file cf::object::thawer $self->{load_path}; 2206 my $f = new_from_file cf::object::thawer $self->{load_path};
2179 $f->skip_block; 2207 $f->skip_block;
2180 $self->_load_objects ($f) 2208 $self->_load_objects ($f)
2181 or return; 2209 or return;
2182 2210
2183 $self->post_load_original 2211 $self->post_load_original
2184 if delete $self->{load_original}; 2212 if delete $self->{load_original};
2185 2213
2186 if (my $uniq = $self->uniq_path) { 2214 if (my $uniq = $self->uniq_path) {
2187 utf8::encode $uniq; 2215 utf8::encode $uniq;
2188 unless (aio_stat $uniq) { 2216 unless (aio_stat $uniq) {
2189 if (my $f = new_from_file cf::object::thawer $uniq) { 2217 if (my $f = new_from_file cf::object::thawer $uniq) {
2190 $self->clear_unique_items; 2218 $self->clear_unique_items;
2191 $self->_load_objects ($f); 2219 $self->_load_objects ($f);
2192 $f->resolve_delayed_derefs; 2220 $f->resolve_delayed_derefs;
2221 }
2193 } 2222 }
2194 } 2223 }
2195 }
2196 2224
2197 $f->resolve_delayed_derefs; 2225 $f->resolve_delayed_derefs;
2226 } else {
2227 $self->post_load_original
2228 if delete $self->{load_original};
2229 }
2230
2231 $self->state (cf::MAP_INACTIVE);
2198 2232
2199 cf::cede_to_tick; 2233 cf::cede_to_tick;
2200 # now do the right thing for maps 2234 # now do the right thing for maps
2201 $self->link_multipart_objects; 2235 $self->link_multipart_objects;
2202 $self->difficulty ($self->estimate_difficulty) 2236 $self->difficulty ($self->estimate_difficulty)
2206 unless ($self->{deny_activate}) { 2240 unless ($self->{deny_activate}) {
2207 $self->decay_objects; 2241 $self->decay_objects;
2208 $self->fix_auto_apply; 2242 $self->fix_auto_apply;
2209 $self->update_buttons; 2243 $self->update_buttons;
2210 cf::cede_to_tick; 2244 cf::cede_to_tick;
2211 $self->activate; 2245 #$self->activate; # no longer activate maps automatically
2212 } 2246 }
2213 2247
2214 $self->{last_save} = $cf::RUNTIME; 2248 $self->{last_save} = $cf::RUNTIME;
2215 $self->last_access ($cf::RUNTIME); 2249 $self->last_access ($cf::RUNTIME);
2216
2217 $self->in_memory (cf::MAP_ACTIVE);
2218 } 2250 }
2219 2251
2220 $self->post_load; 2252 $self->post_load;
2221 2253
2222 1 2254 1
2265} 2297}
2266 2298
2267sub find_sync { 2299sub find_sync {
2268 my ($path, $origin) = @_; 2300 my ($path, $origin) = @_;
2269 2301
2302 # it's a bug to call this from the main context
2270 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" 2303 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2271 if $Coro::current == $Coro::main; 2304 if $Coro::current == $Coro::main;
2272 2305
2273 find $path, $origin 2306 find $path, $origin
2274} 2307}
2275 2308
2276sub do_load_sync { 2309sub do_load_sync {
2277 my ($map) = @_; 2310 my ($map) = @_;
2278 2311
2312 # it's a bug to call this from the main context
2279 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" 2313 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2280 if $Coro::current == $Coro::main; 2314 if $Coro::current == $Coro::main;
2281 2315
2282 $map->load; 2316 $map->load;
2283} 2317}
2286our $MAP_PREFETCHER = undef; 2320our $MAP_PREFETCHER = undef;
2287 2321
2288sub find_async { 2322sub find_async {
2289 my ($path, $origin, $load) = @_; 2323 my ($path, $origin, $load) = @_;
2290 2324
2291 $path = normalise $path, $origin && $origin->{path}; 2325 $path = normalise $path, $origin;
2292 2326
2293 if (my $map = $cf::MAP{$path}) { 2327 if (my $map = $cf::MAP{$path}) {
2294 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; 2328 return $map if !$load || $map->linkable;
2295 } 2329 }
2296 2330
2297 $MAP_PREFETCH{$path} |= $load; 2331 $MAP_PREFETCH{$path} |= $load;
2298 2332
2299 $MAP_PREFETCHER ||= cf::async { 2333 $MAP_PREFETCHER ||= cf::async {
2358sub swap_out { 2392sub swap_out {
2359 my ($self) = @_; 2393 my ($self) = @_;
2360 2394
2361 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2395 my $lock = cf::lock_acquire "map_data:$self->{path}";
2362 2396
2363 return if $self->in_memory != cf::MAP_ACTIVE; 2397 return if !$self->linkable;
2364 return if $self->{deny_save}; 2398 return if $self->{deny_save};
2365 return if $self->players; 2399 return if $self->players;
2366 2400
2367 # first deactivate the map and "unlink" it from the core 2401 # first deactivate the map and "unlink" it from the core
2368 $self->deactivate; 2402 $self->deactivate;
2369 $_->clear_links_to ($self) for values %cf::MAP; 2403 $_->clear_links_to ($self) for values %cf::MAP;
2370 $self->in_memory (cf::MAP_SWAPPED); 2404 $self->state (cf::MAP_SWAPPED);
2371 2405
2372 # then atomically save 2406 # then atomically save
2373 $self->_save; 2407 $self->_save;
2374 2408
2375 # then free the map 2409 # then free the map
2401 2435
2402 return if $self->players; 2436 return if $self->players;
2403 2437
2404 cf::trace "resetting map ", $self->path, "\n"; 2438 cf::trace "resetting map ", $self->path, "\n";
2405 2439
2406 $self->in_memory (cf::MAP_SWAPPED); 2440 $self->state (cf::MAP_SWAPPED);
2407 2441
2408 # need to save uniques path 2442 # need to save uniques path
2409 unless ($self->{deny_save}) { 2443 unless ($self->{deny_save}) {
2410 my $uniq = $self->uniq_path; utf8::encode $uniq; 2444 my $uniq = $self->uniq_path; utf8::encode $uniq;
2411 2445
2807 ($path, $x, $y) = (undef, undef, undef); 2841 ($path, $x, $y) = (undef, undef, undef);
2808 } 2842 }
2809 } 2843 }
2810 2844
2811 my $map = eval { 2845 my $map = eval {
2812 my $map = defined $path ? cf::map::find $path : undef; 2846 my $map = defined $path ? cf::map::find $path, $self->map : undef;
2813 2847
2814 if ($map) { 2848 if ($map) {
2815 $map = $map->customise_for ($self); 2849 $map = $map->customise_for ($self);
2816 $map = $check->($map, $x, $y, $self) if $check && $map; 2850 $map = $check->($map, $x, $y, $self) if $check && $map;
2817 } else { 2851 } else {
2907 $Coro::current->{desc} = "enter_exit"; 2941 $Coro::current->{desc} = "enter_exit";
2908 2942
2909 unless (eval { 2943 unless (eval {
2910 $self->deactivate_recursive; # just to be sure 2944 $self->deactivate_recursive; # just to be sure
2911 2945
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; 2946 my $map = cf::map::normalise $exit->slaying, $exit->map;
2921 my $x = $exit->stats->hp; 2947 my $x = $exit->stats->hp;
2922 my $y = $exit->stats->sp; 2948 my $y = $exit->stats->sp;
2949
2950 # special map handling
2951 my $slaying = $exit->slaying;
2952
2953 # special map handling
2954 if ($slaying eq "/!") {
2955 my $guard = cf::lock_acquire "exit_prepare:$exit";
2956
2957 prepare_random_map $exit
2958 if $exit->slaying eq "/!"; # need to re-check after getting the lock
2959
2960 $map = $exit->slaying;
2961
2962 } elsif ($slaying eq '!up') {
2963 $map = $exit->map->tile_path (cf::TILE_UP);
2964 $x = $exit->x;
2965 $y = $exit->y;
2966
2967 } elsif ($slaying eq '!down') {
2968 $map = $exit->map->tile_path (cf::TILE_DOWN);
2969 $x = $exit->x;
2970 $y = $exit->y;
2971 }
2923 2972
2924 $self->goto ($map, $x, $y); 2973 $self->goto ($map, $x, $y);
2925 2974
2926 # if exit is damned, update players death & WoR home-position 2975 # if exit is damned, update players death & WoR home-position
2927 $self->contr->savebed ($map, $x, $y) 2976 $self->contr->savebed ($map, $x, $y)
3655 3704
3656sub main { 3705sub main {
3657 cf::init_globals; # initialise logging 3706 cf::init_globals; # initialise logging
3658 3707
3659 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3708 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3660 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3709 LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3661 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3710 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3662 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3711 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3663 3712
3664 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3713 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3665 3714
4066 4115
4067############################################################################# 4116#############################################################################
4068 4117
4069my $bug_warning = 0; 4118my $bug_warning = 0;
4070 4119
4071our @WAIT_FOR_TICK;
4072our @WAIT_FOR_TICK_BEGIN;
4073
4074sub wait_for_tick() { 4120sub wait_for_tick() {
4075 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4121 return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main;
4076 4122
4077 my $signal = new Coro::Signal; 4123 $WAIT_FOR_TICK->wait;
4078 push @WAIT_FOR_TICK, $signal;
4079 $signal->wait;
4080} 4124}
4081 4125
4082sub wait_for_tick_begin() { 4126sub wait_for_tick_begin() {
4083 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4127 return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main;
4084 4128
4085 my $signal = new Coro::Signal; 4129 my $signal = new Coro::Signal;
4086 push @WAIT_FOR_TICK_BEGIN, $signal; 4130 push @WAIT_FOR_TICK_BEGIN, $signal;
4087 $signal->wait; 4131 $signal->wait;
4088} 4132}
4092 Carp::cluck "major BUG: server tick called outside of main coro, skipping it" 4136 Carp::cluck "major BUG: server tick called outside of main coro, skipping it"
4093 unless ++$bug_warning > 10; 4137 unless ++$bug_warning > 10;
4094 return; 4138 return;
4095 } 4139 }
4096 4140
4097 cf::server_tick; # one server iteration 4141 cf::one_tick; # one server iteration
4098 4142
4099 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# 4143 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d#
4100 4144
4101 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4145 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4102 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4146 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4108 } 4152 }
4109 4153
4110 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4154 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4111 $sig->send; 4155 $sig->send;
4112 } 4156 }
4113 while (my $sig = shift @WAIT_FOR_TICK) { 4157 $WAIT_FOR_TICK->broadcast;
4114 $sig->send;
4115 }
4116 4158
4117 $LOAD = ($NOW - $TICK_START) / $TICK; 4159 $LOAD = ($NOW - $TICK_START) / $TICK;
4118 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; 4160 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25;
4119 4161
4120 if (0) { 4162 if (0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines