-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
BlockAction.c
231 lines (225 loc) · 11.6 KB
/
BlockAction.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
#include "Util/Enums.h"
#include "Util/Uthash.h"
#include <Server/Gamemodes/Gamemodes.h>
#include <Server/IntelTent.h>
#include <Server/Nodes.h>
#include <Server/Server.h>
#include <Server/Staff.h>
#include <Util/Checks/PlayerChecks.h>
#include <Util/Checks/PositionChecks.h>
#include <Util/Checks/TimeChecks.h>
#include <Util/Log.h>
#include <Util/Nanos.h>
#include <Util/Utlist.h>
void send_block_action(server_t* server, player_t* player, uint8_t actionType, int X, int Y, int Z)
{
if (server->protocol.num_players == 0) {
return;
}
ENetPacket* packet = enet_packet_create(NULL, 15, ENET_PACKET_FLAG_RELIABLE);
stream_t stream = {packet->data, packet->dataLength, 0};
stream_write_u8(&stream, PACKET_TYPE_BLOCK_ACTION);
stream_write_u8(&stream, player->id);
stream_write_u8(&stream, actionType);
stream_write_u32(&stream, X);
stream_write_u32(&stream, Y);
stream_write_u32(&stream, Z);
uint8_t sent = 0;
player_t *check, *tmp;
HASH_ITER(hh, server->players, check, tmp) {
if (is_past_state_data(check)) {
if (enet_peer_send(player->peer, 0, packet) == 0) {
sent = 1;
}
} else if (player->state == STATE_STARTING_MAP || player->state == STATE_LOADING_CHUNKS) {
block_node_t* node = (block_node_t*) malloc(sizeof(*node));
node->position.x = X;
node->position.y = Y;
node->position.z = Z;
node->color = player->color;
node->type = actionType;
node->sender_id = player->id;
LL_APPEND(player->blockBuffer, node);
}
}
if (sent == 0) {
enet_packet_destroy(packet);
}
}
void send_block_action_to_player(server_t* server,
player_t* player,
player_t* receiver,
uint8_t actionType,
int X,
int Y,
int Z)
{
if (server->protocol.num_players == 0) {
return;
}
ENetPacket* packet = enet_packet_create(NULL, 15, ENET_PACKET_FLAG_RELIABLE);
stream_t stream = {packet->data, packet->dataLength, 0};
stream_write_u8(&stream, PACKET_TYPE_BLOCK_ACTION);
stream_write_u8(&stream, player->id);
stream_write_u8(&stream, actionType);
stream_write_u32(&stream, X);
stream_write_u32(&stream, Y);
stream_write_u32(&stream, Z);
uint8_t sent = 0;
if (enet_peer_send(receiver->peer, 0, packet) == 0) {
sent = 1;
}
if (sent == 0) {
enet_packet_destroy(packet);
}
}
void receive_block_action(server_t* server, player_t* player, stream_t* data)
{
uint8_t received_id = stream_read_u8(data);
if (player->id != received_id) {
LOG_WARNING("Assigned ID: %d doesnt match sent ID: %d in block action packet", player->id, received_id);
}
if (player->can_build && server->global_ab) {
uint8_t actionType = stream_read_u8(data);
uint32_t X = stream_read_u32(data);
uint32_t Y = stream_read_u32(data);
uint32_t Z = stream_read_u32(data);
if (player->sprinting) {
return;
}
vector3i_t vectorBlock = {X, Y, Z};
vector3f_t vectorfBlock = {(float) X, (float) Y, (float) Z};
vector3f_t playerVector = player->movement.position;
if (((player->item == 0 && (actionType == 1 || actionType == 2)) || (player->item == 1 && actionType == 0) ||
(player->item == 2 && actionType == 1)))
{
if ((distance_in_3d(vectorfBlock, playerVector) <= 4 || player->item == 2) &&
valid_pos_v3i(server, vectorBlock))
{
switch (actionType) {
case 0:
{
if (gamemode_block_checks(server, X, Y, Z)) {
uint64_t timeNow = get_nanos();
if (player->blocks > 0 &&
diff_is_older_then(timeNow, &player->timers.since_last_block_plac, BLOCK_DELAY) &&
diff_is_older_then_dont_update(
timeNow, player->timers.since_last_block_dest, BLOCK_DELAY) &&
diff_is_older_then_dont_update(
timeNow, player->timers.since_last_3block_dest, BLOCK_DELAY))
{
mapvxl_set_color(&server->s_map.map, X, Y, Z, player->tool_color.raw);
player->blocks--;
moveIntelAndTentUp(server);
send_block_action(server, player, actionType, X, Y, Z);
}
}
} break;
case 1:
{
if (Z < 62 && gamemode_block_checks(server, X, Y, Z)) {
uint64_t timeNow = get_nanos();
if ((diff_is_older_then(timeNow, &player->timers.since_last_block_dest, SPADE_DELAY) &&
diff_is_older_then_dont_update(
timeNow, player->timers.since_last_3block_dest, SPADE_DELAY) &&
diff_is_older_then_dont_update(
timeNow, player->timers.since_last_block_plac, SPADE_DELAY)) ||
player->item == 2)
{
if (player->item == 2) {
if (player->weapon_clip <= 0 && !diff_is_older_then_dont_update(timeNow, player->timers.since_last_primary_weapon_input, 10*NANO_IN_MILLI)) {
send_message_to_staff(server,
"Player %s (#%hhu) probably has hack to have more ammo",
player->name,
player->id);
return;
} else if (player->weapon == 0 &&
diff_is_older_then(timeNow,
&player->timers.since_last_block_dest_with_gun,
(RIFLE_DELAY - (NANO_IN_MILLI * 10))) == 0)
{
send_message_to_staff(server,
"Player %s (#%hhu) probably has rapid shooting hack",
player->name,
player->id);
return;
} else if (player->weapon == 1 &&
diff_is_older_then(timeNow,
&player->timers.since_last_block_dest_with_gun,
(SMG_DELAY - (NANO_IN_MILLI * 10))) == 0)
{
send_message_to_staff(server,
"Player %s (#%hhu) probably has rapid shooting hack",
player->name,
player->id);
return;
} else if (player->weapon == 2 &&
diff_is_older_then(timeNow,
&player->timers.since_last_block_dest_with_gun,
(SHOTGUN_DELAY - (NANO_IN_MILLI * 10))) == 0)
{
send_message_to_staff(server,
"Player %s (#%hhu) probably has rapid shooting hack",
player->name,
player->id);
return;
}
}
vector3i_t position = {X, Y, Z};
vector3i_t* neigh = get_neighbours(position);
mapvxl_set_air(&server->s_map.map, position.x, position.y, position.z);
for (int i = 0; i < 6; ++i) {
if (neigh[i].z < 62) {
check_node(server, neigh[i]);
}
}
if (player->item != 2) {
if (player->blocks < 50) {
player->blocks++;
}
}
send_block_action(server, player, actionType, X, Y, Z);
}
}
} break;
case 2:
{
if (gamemode_block_checks(server, X, Y, Z) && gamemode_block_checks(server, X, Y, Z + 1) &&
gamemode_block_checks(server, X, Y, Z - 1))
{
uint64_t timeNow = get_nanos();
if (diff_is_older_then(timeNow, &player->timers.since_last_3block_dest, THREEBLOCK_DELAY) &&
diff_is_older_then_dont_update(
timeNow, player->timers.since_last_block_dest, THREEBLOCK_DELAY) &&
diff_is_older_then_dont_update(
timeNow, player->timers.since_last_block_plac, THREEBLOCK_DELAY))
{
for (uint32_t z = Z - 1; z <= Z + 1; z++) {
if (z < 62) {
mapvxl_set_air(&server->s_map.map, X, Y, z);
vector3i_t position = {X, Y, z};
vector3i_t* neigh = get_neighbours(position);
mapvxl_set_air(&server->s_map.map, position.x, position.y, position.z);
for (int i = 0; i < 6; ++i) {
if (neigh[i].z < 62) {
check_node(server, neigh[i]);
}
}
}
}
send_block_action(server, player, actionType, X, Y, Z);
}
}
} break;
}
move_intel_and_tent_down(server);
}
} else {
LOG_WARNING("Player %s (#%hhu) may be using BlockExploit with Item: %d and Action: %d",
player->name,
player->id,
player->item,
actionType);
}
}
}