-
Notifications
You must be signed in to change notification settings - Fork 0
/
nohacks.c
185 lines (161 loc) · 4.05 KB
/
nohacks.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
#include <stdio.h>
#include <string.h>
#include "bitstuff.h"
#include "block.h"
#include "colour.h"
#include "client.h"
#include "level.h"
#include "player.h"
#include "position.h"
#include "mcc.h"
#include "network.h"
struct nohacks_t
{
bool nohacks;
bool game;
};
static bool nohacks_handle_chat(struct level_t *l, struct client_t *c, char *data, struct nohacks_t *arg)
{
if (c->player->rank < RANK_OP) return false;
if (strcasecmp(data, "nohacks on") == 0 || strcasecmp(data, "hacks off") == 0)
{
arg->nohacks = true;
level_notify_all(l, TAG_YELLOW "No hacks enabled");
}
else if (strcasecmp(data, "nohacks off") == 0 || strcasecmp(data, "hacks on") == 0)
{
arg->nohacks = false;
level_notify_all(l, TAG_YELLOW "No hacks disabled");
}
else if (strcasecmp(data, "game off") == 0)
{
arg->game = false;
int i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *cl = l->clients[i];
if (cl == NULL) continue;
ClrBit(cl->player->flags, FLAG_GAMES);
}
level_notify_all(l, TAG_YELLOW "Game mode disabled");
}
else if (strcasecmp(data, "game on") == 0)
{
arg->game = true;
int i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *cl = l->clients[i];
if (cl == NULL) continue;
SetBit(cl->player->flags, FLAG_GAMES);
}
level_notify_all(l, TAG_YELLOW "Game mode enabled");
}
else
{
return false;
}
return true;
}
static void nohacks_handle_move(struct level_t *l, struct client_t *c, int index, struct nohacks_t *arg)
{
/* Changing levels, don't handle teleports */
if (c->player->level != c->player->new_level) return;
if (!arg->nohacks) return;
if (!c->player->teleport)
{
struct player_t *player = c->player;
int dx = player->pos.x - player->lastpos.x;
int dy = player->pos.y - player->lastpos.y;
int dz = player->pos.z - player->lastpos.z;
/* Calculate diagonal distance */
int dp = dx * dx + dz * dz;
//LOG("nohacks: %d - %d\n", dp, dy);
if (dp > 100 || dy > 20 || dy < -120)
{
/* Player moved too far! */
player_teleport(player, &player->oldpos, true);
if (dp < 100000)
{
client_notify(c, TAG_RED "Client hacks are forbidden during game");
}
else
{
client_notify(c, TAG_RED "Teleporting forbidden during game");
}
}
}
}
static void nohacks_handle_spawn(struct level_t *l, struct client_t *c, char *data, struct nohacks_t *arg)
{
if (arg->nohacks)
{
char buf[512];
snprintf(buf, sizeof buf,
TAG_YELLOW "Welcome to %s. This level is running no hacks. "
TAG_RED "ALL hacks must be disabled.",
l->name);
client_notify(c, buf);
}
if (arg->game)
{
SetBit(c->player->flags, FLAG_GAMES);
}
}
static void nohacks_handle_despawn(struct level_t *l, struct client_t *c, char *data, struct nohacks_t *arg)
{
if (arg->game)
{
ClrBit(c->player->flags, FLAG_GAMES);
}
}
static bool nohacks_level_hook(int event, struct level_t *l, struct client_t *c, void *data, struct level_hook_data_t *arg)
{
switch (event)
{
case EVENT_CHAT: return nohacks_handle_chat(l, c, data, arg->data);
case EVENT_MOVE: nohacks_handle_move(l, c, *(int *)data, arg->data); break;
case EVENT_SPAWN: nohacks_handle_spawn(l, c, data, arg->data); break;
case EVENT_DESPAWN: nohacks_handle_despawn(l, c, data, arg->data); break;
case EVENT_INIT:
{
if (arg->size == 0)
{
LOG("Allocating new nohacks data on %s\n", l->name);
}
else
{
if (arg->size == sizeof (struct nohacks_t))
{
break;
}
LOG("Found invalid nohacks data on %s, erasing\n", l->name);
free(arg->data);
}
arg->size = sizeof (struct nohacks_t);
arg->data = calloc(1, arg->size);
break;
}
case EVENT_DEINIT:
{
if (l == NULL) break;
int i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *cl = l->clients[i];
if (cl == NULL) continue;
player_set_alias(cl->player, NULL, true);
ClrBit(cl->player->flags, FLAG_GAMES);
}
}
}
return false;
}
void module_init(void **data)
{
register_level_hook_func("nohacks", &nohacks_level_hook);
}
void module_deinit(void *data)
{
deregister_level_hook_func("nohacks");
}