-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.pl
217 lines (180 loc) · 6.68 KB
/
world.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/perl
use strict;
use warnings;
use Devel::SimpleTrace;
use Config::INI::Reader;
use DBI;
use DBD::mysql;
use lib './lib';
use DataUnits;
use Vaffel;
our ($listener,$select); # Socket stuff.
our (%clients,%world); # World stuff.
our ($last_tick); # Tick stuff.
our $config = Config::INI::Reader->read_file('config.ini');
require './subs/common.pl';
require './subs/world.pl';
console_prefix('WORLD') if $ENV{'vaffel_threads'};
console('Connecting to database' . ($config->{'MainDB'}->{'channel'} ? '' : 's') . '...');
our ($mdb,$cdb) = db_connect(1);
if (!$mdb || !$cdb) {
console('Could not connect to database!');
exit;
}
our $server = Vaffel->new(
'bind_address' => $config->{'World'}->{'address'},
'bind_port' => $config->{'World'}->{'port'},
'first_command' => 0x070B,
'dump' => 0 # Dump incoming packets to data directory.
);
$server->register_handler('start',\&event_start);
$server->register_handler('tick',\&event_tick);
$server->register_handler('connect',\&event_connect);
$server->register_handler('disconnect',\&event_disconnect);
$server->register_handler('unknown',\&event_unknown);
console('Loading packet handlers...');
for (<./packets/world/*.pl>) {
my $filename = lc($_);
$filename =~ s/.*\/(.+)\..+/$1/i;
console(' ' . $filename);
do $_;
warn $@ if $@;
warn $! if $!;
}
console('Loading STB files...');
our (%stb_cols,%stbs);
require './data/stb_cols.pl';
for (<./data/stb/*.STB>) {
my $filename = lc($_);
$filename =~ s/.*\/(.+)\..+/$1/i;
console(' ' . $filename);
%{$stbs{$filename}} = load_stb($_,@{$stb_cols{$filename}});
}
console('Loading chat commands...');
our %chat_commands;
for (<./commands/*.pl>) {
my $filename = lc($_);
$filename =~ s/.*\/(.+)\..+/$1/i;
console(' ' . $filename);
do $_;
warn $@ if $@;
warn $! if $!;
}
console('Loading NPCs...');
my $npc_count = 0;
my $npc_query = $mdb->prepare("SELECT id,type,map,dir,x,y,dialogid FROM npcs");
$npc_query->execute();
while (my $npc = $npc_query->fetchrow_hashref()) {
$npc_count++;
%{$world{$npc->{'map'}}{'npcs'}{client_id($npc->{'map'})}} = %{{
'type' => $npc->{'type'},
'direction' => $npc->{'dir'},
'x' => $npc->{'x'} * 100,
'y' => $npc->{'y'} * 100,
'dialog' => $npc->{'dialogid'} || $npc->{'type'} - 900
}}
}
console(' ' . $npc_count);
console('Loading mobs...');
my $mob_count = 0;
my $mob_query = $mdb->prepare("SELECT * FROM mobgroups WHERE isactive=1"); # Bad boy, using * =(
$mob_query->execute();
while (my $mob_group = $mob_query->fetchrow_hashref()) {
my $group_count = 0;
my ($mob,undef) = split(/\|/,$mob_group->{'moblist'},2);
my ($id,$amount,$tactical) = split(/\,/,$mob,3);
for (1..$amount) {
$mob_count++;
$group_count++;
my ($x,$y) = rand_in_circle($mob_group->{'x'} * 100,$mob_group->{'y'} * 100,$mob_group->{'range'} * 100);
my $client_id = client_id($mob_group->{'map'});
my $hp = $stbs{'list_npc'}{$id}{'hp'};
console('No HP info found for mob type ' . $id . '!') if !$hp;
%{$world{$mob_group->{'map'}}{'mobs'}{$client_id}} = %{{
'type' => $id,
'range' => $mob_group->{'range'} * 100,
'x_orig' => $mob_group->{'x'} * 100,
'y_orig' => $mob_group->{'y'} * 100,
'x' => $x,
'y' => $y,
'x_dest' => $x,
'y_dest' => $y,
'hp' => $hp,
'last_move' => time()
}};
last if $group_count >= $mob_group->{'limit'};
}
}
console(' ' . $mob_count);
console('Starting server...');
$server->run();
console('Server stopped!');
exit;
sub event_disconnect {
my ($handle) = @_;
if ($clients{$handle}{'map'}) {
for (keys(%{$world{$clients{$handle}{'map'}}{'players'}})) {
if ($clients{$handle}{'id'} != $_) {
clear_map_client_from_player($world{$clients{$handle}{'map'}}{'players'}{$_}{'handle'},$clients{$handle}{'id'});
}
}
# TODO: Remove ownership of drops.
delete $world{$clients{$handle}{'map'}}{'players'}{$clients{$handle}{'id'}};
delete $clients{$handle}{'map'};
delete $clients{$handle}{'id'};
}
my $query = $mdb->prepare("UPDATE users SET world=NULL WHERE id=?");
$query->execute($clients{$handle}{'user'}{'id'});
console(who($handle) . ' disconnected. Time online: ' . (time() - $clients{$handle}{'connected'}) . ' seconds.');
delete $clients{$handle};
$select->remove($handle);
$handle->close();
}
sub event_tick {
return if $last_tick && $last_tick == time(); # Only tick once a second.
$last_tick = time();
if (!$mdb || !$cdb) {
console('Lost database connection!');
console('Reconnecting...');
db_connect();
}
for my $map (keys(%world)) {
next if scalar(keys(%{$world{$map}{'players'}})) <= 0; # Don't process maps withut players.
my @move_mobs;
for my $mob (keys(%{$world{$map}{'mobs'}})) {
if (time() - $world{$map}{'mobs'}{$mob}{'last_move'} >= 8 + int(rand(5))) {
($world{$map}{'mobs'}{$mob}{'x_dest'},$world{$map}{'mobs'}{$mob}{'y_dest'}) = (0,0);
while (distance($world{$map}{'mobs'}{$mob}{'x_orig'},$world{$map}{'mobs'}{$mob}{'y_orig'},$world{$map}{'mobs'}{$mob}{'x_dest'},$world{$map}{'mobs'}{$mob}{'y_dest'}) > $world{$map}{'mobs'}{$mob}{'range'}) {
($world{$map}{'mobs'}{$mob}{'x_dest'},$world{$map}{'mobs'}{$mob}{'y_dest'}) = rand_in_circle($world{$map}{'mobs'}{$mob}{'x'},$world{$map}{'mobs'}{$mob}{'y'},1000);
}
push(@move_mobs,$mob);
$world{$map}{'mobs'}{$mob}{'last_move'} = time();
# TODO: Calculate ETA and do the same check we do for players.
$world{$map}{'mobs'}{$mob}{'x'} = $world{$map}{'mobs'}{$mob}{'x_dest'};
$world{$map}{'mobs'}{$mob}{'y'} = $world{$map}{'mobs'}{$mob}{'y_dest'};
}
}
my @clear_drops;
for my $drop (keys(%{$world{$map}{'drops'}})) {
if (time() - $world{$map}{'drops'}{$drop}{'time'} > 300) { # TODO: Make a config entry for this.
push(@clear_drops,$drop);
delete $world{$map}{'drops'}{$drop};
}
}
for my $player (keys(%{$world{$map}{'players'}})) {
for my $mob (@move_mobs) {
move_monster($world{$map}{'players'}{$player}{'handle'},$mob);
}
for my $drop (@clear_drops) {
clear_map_client_from_player($world{$map}{'players'}{$player}{'handle'},$drop);
}
if (
($world{$map}{'players'}{$player}{'x'} != $world{$map}{'players'}{$player}{'x_dest'} || $world{$map}{'players'}{$player}{'y'} != $world{$map}{'players'}{$player}{'y_dest'}) &&
($world{$map}{'players'}{$player}{'eta'} && time() - $world{$map}{'players'}{$player}{'last_move'} >= $world{$map}{'players'}{$player}{'eta'})
) {
$world{$map}{'players'}{$player}{'x'} = $world{$map}{'players'}{$player}{'x_dest'};
$world{$map}{'players'}{$player}{'y'} = $world{$map}{'players'}{$player}{'y_dest'};
}
}
}
}