-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
454 lines (380 loc) · 10.6 KB
/
player.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <stdio.h>
#include <string.h>
#include "bitstuff.h"
#include "client.h"
#include "level.h"
#include "packet.h"
#include "player.h"
#include "playerdb.h"
#include "mcc.h"
#include "network.h"
#include "util.h"
#include "level_worker.h"
#include "gettime.h"
static struct player_list_t s_players;
bool player_t_compare(struct player_t **a, struct player_t **b)
{
return *a == *b;
}
struct player_t *player_get_by_name(const char *username, bool partial)
{
unsigned i;
for (i = 0; i < s_players.used; i++)
{
if (strcasecmp(s_players.items[i]->username, username) == 0)
{
return s_players.items[i];
}
}
if (partial)
{
int n = strlen(username);
int c = 0;
struct player_t *match = NULL;
for (i = 0; i < s_players.used; i++)
{
struct player_t *p = s_players.items[i];
if (!p->client->hidden && strncasecmp(p->username, username, n) == 0)
{
c++;
match = p;
}
}
if (c == 1) return match;
}
return NULL;
}
void player_set_alias(struct player_t *p, const char *alias, bool send_spawn)
{
if (alias == NULL)
{
strncpy(p->gamealias, p->colourusername, sizeof p->gamealias);
strncpy(p->gameusername, p->colourusername, sizeof p->gameusername);
}
else
{
strncpy(p->gamealias, alias, sizeof p->gamealias);
strncpy(p->gameusername, p->colourusername, sizeof p->gameusername);
p->gameusername[1] = p->gamealias[1];
}
if (p->client != NULL && !p->client->hidden && send_spawn)
{
// Renaming own client doesn't work
// client_add_packet(p->client, packet_send_despawn_player(0xFF));
// client_add_packet(p->client, packet_send_spawn_player(0xFF, p->alias, &p->pos));
client_send_despawn(p->client, false);
client_send_spawn(p->client, false);
}
}
struct player_t *player_add(const char *username, struct client_t *c, bool *newuser, int *identified)
{
int globalid = playerdb_get_globalid(username, true, newuser);
if (globalid == -1) return NULL;
struct player_t *p = malloc(sizeof *p);
memset(p, 0, sizeof *p);
p->username = p->colourusername + 2;
p->rank = playerdb_get_rank(username);
snprintf(p->colourusername, sizeof p->colourusername, "&%x%s", rank_get_colour(p->rank), username);
p->globalid = globalid;
player_set_alias(p, NULL, false);
if (p->rank > RANK_BUILDER)
{
const char *last_ip = playerdb_get_last_ip(globalid, 1);
if (last_ip != NULL && strcmp(c->ip, last_ip) == 0)
{
*identified = 2;
}
else
{
//p->rank = RANK_BUILDER;
*identified = 2;
}
}
else
{
*identified = 0;
}
playerdb_log_visit(globalid, c->ip, (*identified) == 2);
enum blocktype_t i;
for (i = 0; i < BLOCK_END; i++)
{
p->bindings[i] = i;
}
player_list_add(&s_players, p);
g_server.players++;
return p;
}
void player_del(struct player_t *player)
{
if (player == NULL) return;
player_list_del_item(&s_players, player);
g_server.players--;
free(player);
}
bool player_change_level(struct player_t *player, struct level_t *level)
{
/* Special case, when level is NULL, choose a starting level */
if (level != NULL)
{
if (player->level == level) return false;
if (level->delete) return false;
}
player->new_level = level;
player->client->waiting_for_level = true;
level_send_queue(player->client);
return true;
}
void player_move(struct player_t *player, struct position_t *pos)
{
/* If we've just teleported, don't allow a position change too far */
if (player->teleport == true)
{
if (!position_match(&player->pos, pos, 64)) return;
player->teleport = false;
}
int dx = abs(player->pos.x - pos->x);
int dy = abs(player->pos.y - pos->y);
int dz = abs(player->pos.z - pos->z);
/* We only care that speed is above a threshold, therefore
* we don't need to find the square root. */
unsigned i;
player->speed = 0;
for (i = 0; i < 10 - 1; i++)
{
player->speeds[i] = player->speeds[i + 1];
player->speed += player->speeds[i];
}
player->speeds[i] = dx + dy + dz;
player->speed += player->speeds[i];
player->lastpos = player->pos;
player->pos = *pos;
if (player->level != NULL)
{
call_level_hook(EVENT_MOVE, player->level, player->client, &player->levelid);
}
}
void player_teleport(struct player_t *player, const struct position_t *pos, bool instant)
{
player->pos = *pos;
player->lastpos = *pos;
player->teleport = true;
if (instant)
{
client_add_packet(player->client, packet_send_teleport_player(0xFF, &player->pos));
}
}
bool player_check_spam(struct player_t *player)
{
int len = 60;
int ofs = player->spampos1 > player->spampos2 ? len : 0;
int now = gettime();
player->spam[player->spampos2] = now;
while (player->spam[player->spampos1] < now - 2000 && player->spampos1 < player->spampos2 + ofs)
{
player->spampos1++;
if (player->spampos1 >= len) { player->spampos1 = 0; ofs = 0; }
}
int blocks = player->spampos2 + ofs - player->spampos1;
if (blocks > 20)
{
char buf[128];
snprintf(buf, sizeof buf, "Anti-grief: %d blocks in %u ms", blocks, player->spam[player->spampos2] - player->spam[player->spampos1]);
LOG("%s by %s\n", buf, player->username);
if (blocks > 30)
{
net_close(player->client, buf);
return true;
}
player->warnings++;
}
player->spampos2++;
if (player->spampos2 >= len) player->spampos2 = 0;
return false;
}
static void player_send_position(struct player_t *player, unsigned cur_tick)
{
int changed = 0;
int dx = 0, dy = 0, dz = 0;
if (player->pos.x != player->oldpos.x || player->pos.y != player->oldpos.y || player->pos.z != player->oldpos.z)
{
changed = 1;
dx = player->pos.x - player->oldpos.x;
dy = player->pos.y - player->oldpos.y;
dz = player->pos.z - player->oldpos.z;
if (abs(dx) > 32 || abs(dy) > 32 || abs(dz) > 32)
{
changed = 4;
}
}
if (player->pos.h != player->oldpos.h || player->pos.p != player->oldpos.p)
{
changed |= 2;
}
if (changed == 0) return;
player->oldpos = player->pos;
if (player->client->hidden) return;
player->last_active = cur_tick;
unsigned i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *c = player->level->clients[i];
if (c == NULL || c->sending_level || c->player == player) continue;
switch (changed)
{
case 1:
client_add_packet(c, packet_send_position_update(player->levelid, dx, dy, dz));
break;
case 2:
client_add_packet(c, packet_send_orientation_update(player->levelid, &player->pos));
break;
case 3:
client_add_packet(c, packet_send_full_position_update(player->levelid, dx, dy, dz, &player->pos));
break;
default:
client_add_packet(c, packet_send_teleport_player(player->levelid, &player->pos));
break;
}
}
}
void player_send_positions(unsigned cur_tick)
{
unsigned i;
for (i = 0; i < s_players.used; i++)
{
struct player_t *player = s_players.items[i];
if (player->following != NULL)
{
player->pos = player->following->pos;
player->pos.y -= 23;
client_add_packet(player->client, packet_send_teleport_player(0xFF, &player->pos));
continue;
}
player_send_position(player, cur_tick);
}
}
void player_undo(struct client_t *c, const char *username, const char *levelname, const char *timestamp)
{
struct level_t *level;
if (!level_get_by_name(levelname, &level))
{
return;
}
char buf[256];
snprintf(buf, sizeof buf, "undo/%s_%s_%s.bin", levelname, username, timestamp);
lcase(buf);
FILE *f = fopen(buf, "rb");
if (f == NULL)
{
client_notify(c, "No actions to undo");
return;
}
/* Get length */
fseek(f, 0, SEEK_END);
size_t total_len = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned index;
struct block_t block;
size_t len = sizeof index + sizeof block;
int nmemb = total_len / len;
int pos;
for (pos = nmemb - 1; pos >= 0; pos--)
{
fseek(f, pos * len, SEEK_SET);
fread(&index, sizeof index, 1, f);
fread(&block, sizeof block, 1, f);
level_change_block_force(level, &block, index);
}
fclose(f);
snprintf(buf, sizeof buf, "Undone %d actions by %s", nmemb, username);
net_notify_all(buf);
}
enum rank_t rank_get_by_name(const char *rank)
{
if (!strcasecmp(rank, "banned")) return RANK_BANNED;
if (!strcasecmp(rank, "guest")) return RANK_GUEST;
if (!strcasecmp(rank, "regular")) return RANK_REGULAR;
if (!strcasecmp(rank, "builder")) return RANK_BUILDER;
if (!strcasecmp(rank, "advbuilder")) return RANK_ADV_BUILDER;
if (!strcasecmp(rank, "mod")) return RANK_MOD;
if (!strcasecmp(rank, "op")) return RANK_OP;
if (!strcasecmp(rank, "admin")) return RANK_ADMIN;
return -1;
}
const char *rank_get_name(enum rank_t rank)
{
static char numeric_rank[5];
switch (rank)
{
case RANK_BANNED: return "banned";
case RANK_GUEST: return "guest";
case RANK_REGULAR: return "regular";
case RANK_BUILDER: return "builder";
case RANK_ADV_BUILDER: return "advbuilder";
case RANK_MOD: return "mod";
case RANK_OP: return "op";
case RANK_ADMIN: return "admin";
default:
snprintf(numeric_rank, sizeof numeric_rank, "%d", rank);
return numeric_rank;
}
}
enum colour_t rank_get_colour(enum rank_t rank)
{
switch (rank)
{
case RANK_BANNED: return COLOUR_SILVER;
case RANK_GUEST: return COLOUR_SILVER;
case RANK_REGULAR: return COLOUR_GOLD;
case RANK_BUILDER: return COLOUR_LIME;
case RANK_ADV_BUILDER: return COLOUR_GREEN;
case RANK_MOD: return COLOUR_TEAL;
case RANK_OP: return COLOUR_AQUA;
case RANK_ADMIN: return COLOUR_MAROON;
}
return COLOUR_YELLOW;
}
void player_list(struct client_t *c, const struct level_t *l)
{
char buf[4096];
unsigned i;
for (i = 0; i < 5; i++)
{
bool added = false;
char *bufp = buf;
memset(buf, 0, sizeof buf);
switch (i) {
case 0: snprintf(buf, sizeof buf, TAG_TEAL "Ops: "); break;
case 1: snprintf(buf, sizeof buf, TAG_TEAL "Mods: "); break;
case 2: snprintf(buf, sizeof buf, TAG_GREEN "Builders: "); break;
case 3: snprintf(buf, sizeof buf, TAG_GOLD "Regulars: "); break;
case 4: snprintf(buf, sizeof buf, TAG_SILVER "Guests: "); break;
}
bufp += strlen(buf);
unsigned j;
for (j = 0; j < s_players.used; j++)
{
const struct player_t *p = s_players.items[j];
if (p == NULL) continue;
if (l != NULL && p->level != l) continue;
if (i == 0 && p->rank < RANK_OP) continue;
if (i == 1 && (p->rank < RANK_MOD || p->rank >= RANK_OP)) continue;
if (i == 2 && (p->rank < RANK_BUILDER || p->rank >= RANK_MOD)) continue;
if (i == 3 && (p->rank < RANK_REGULAR || p->rank >= RANK_BUILDER)) continue;
if (i == 4 && p->rank >= RANK_REGULAR) continue;
if (c->player->rank < RANK_OP && p->client->hidden) continue;
size_t len = strlen(p->colourusername) + 1;
if (len >= sizeof buf - (bufp - buf))
{
client_notify(c, buf);
bufp = buf;
memset(buf, 0, sizeof buf);
}
strcpy(bufp, p->colourusername);
bufp += len - 1;
*bufp = ' ';
bufp++;
added = true;
}
if (added) client_notify(c, buf);
}
}