… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use IO::AIO (); |
|
|
11 | use YAML::Syck (); |
10 | use Time::HiRes; |
12 | use Time::HiRes; |
11 | use Event; |
13 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
15 | |
|
|
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
17 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
18 | |
14 | use strict; |
19 | use strict; |
15 | |
20 | |
16 | _init_vars; |
21 | _init_vars; |
17 | |
22 | |
18 | our %COMMAND = (); |
23 | our %COMMAND = (); |
… | |
… | |
22 | our $TICK = MAX_TIME * 1e-6; |
27 | our $TICK = MAX_TIME * 1e-6; |
23 | our $TICK_WATCHER; |
28 | our $TICK_WATCHER; |
24 | our $NEXT_TICK; |
29 | our $NEXT_TICK; |
25 | |
30 | |
26 | our %CFG; |
31 | our %CFG; |
|
|
32 | |
|
|
33 | our $uptime; |
|
|
34 | |
|
|
35 | $uptime ||= time; |
27 | |
36 | |
28 | ############################################################################# |
37 | ############################################################################# |
29 | |
38 | |
30 | =head2 GLOBAL VARIABLES |
39 | =head2 GLOBAL VARIABLES |
31 | |
40 | |
… | |
… | |
766 | sub cf::player::exists($) { |
775 | sub cf::player::exists($) { |
767 | cf::player::find $_[0] |
776 | cf::player::find $_[0] |
768 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
777 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
769 | } |
778 | } |
770 | |
779 | |
771 | =item $player->reply ($npc, $msg[, $flags]) |
780 | =item $object->reply ($npc, $msg[, $flags]) |
772 | |
781 | |
773 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
782 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
774 | can be C<undef>. Does the right thing when the player is currently in a |
783 | can be C<undef>. Does the right thing when the player is currently in a |
775 | dialogue with the given NPC character. |
784 | dialogue with the given NPC character. |
776 | |
785 | |
… | |
… | |
1025 | } |
1034 | } |
1026 | |
1035 | |
1027 | ############################################################################# |
1036 | ############################################################################# |
1028 | # the server's main() |
1037 | # the server's main() |
1029 | |
1038 | |
|
|
1039 | sub cfg_load { |
|
|
1040 | open my $fh, "<:utf8", cf::confdir . "/config" |
|
|
1041 | or return; |
|
|
1042 | |
|
|
1043 | local $/; |
|
|
1044 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1045 | } |
|
|
1046 | |
1030 | sub main { |
1047 | sub main { |
|
|
1048 | cfg_load; |
1031 | db_load; |
1049 | db_load; |
1032 | load_extensions; |
1050 | load_extensions; |
1033 | Event::loop; |
1051 | Event::loop; |
1034 | } |
1052 | } |
1035 | |
1053 | |
… | |
… | |
1087 | |
1105 | |
1088 | # reload cf.pm |
1106 | # reload cf.pm |
1089 | $msg->("reloading cf.pm"); |
1107 | $msg->("reloading cf.pm"); |
1090 | require cf; |
1108 | require cf; |
1091 | |
1109 | |
1092 | # load database again |
1110 | # load config and database again |
|
|
1111 | cf::cfg_load; |
1093 | cf::db_load; |
1112 | cf::db_load; |
1094 | |
1113 | |
1095 | # load extensions |
1114 | # load extensions |
1096 | $msg->("load extensions"); |
1115 | $msg->("load extensions"); |
1097 | cf::load_extensions; |
1116 | cf::load_extensions; |
… | |
… | |
1126 | register "<global>", __PACKAGE__; |
1145 | register "<global>", __PACKAGE__; |
1127 | |
1146 | |
1128 | unshift @INC, $LIBDIR; |
1147 | unshift @INC, $LIBDIR; |
1129 | |
1148 | |
1130 | $TICK_WATCHER = Event->timer ( |
1149 | $TICK_WATCHER = Event->timer ( |
1131 | prio => 1, |
1150 | prio => 1, |
|
|
1151 | async => 1, |
1132 | at => $NEXT_TICK || 1, |
1152 | at => $NEXT_TICK || 1, |
1133 | cb => sub { |
1153 | cb => sub { |
1134 | cf::server_tick; # one server iteration |
1154 | cf::server_tick; # one server iteration |
1135 | |
1155 | |
1136 | my $NOW = Event::time; |
1156 | my $NOW = Event::time; |
1137 | $NEXT_TICK += $TICK; |
1157 | $NEXT_TICK += $TICK; |
1138 | |
1158 | |
… | |
… | |
1142 | $TICK_WATCHER->at ($NEXT_TICK); |
1162 | $TICK_WATCHER->at ($NEXT_TICK); |
1143 | $TICK_WATCHER->start; |
1163 | $TICK_WATCHER->start; |
1144 | }, |
1164 | }, |
1145 | ); |
1165 | ); |
1146 | |
1166 | |
|
|
1167 | IO::AIO::max_poll_time $TICK * 0.2; |
|
|
1168 | |
|
|
1169 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
1170 | poll => 'r', |
|
|
1171 | prio => 5, |
|
|
1172 | cb => \&IO::AIO::poll_cb); |
|
|
1173 | |
1147 | 1 |
1174 | 1 |
1148 | |
1175 | |