Skip to content

Commit

Permalink
Separate anticheat settings (minetest#15040)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmv7 authored Oct 11, 2024
1 parent d2b4c27 commit 1b2d247
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
9 changes: 7 additions & 2 deletions builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,13 @@ default_privs (Default privileges) string interact, shout
# Privileges that players with basic_privs can grant
basic_privs (Basic privileges) string interact, shout

# If enabled, disable cheat prevention in multiplayer.
disable_anticheat (Disable anticheat) bool false
# Server anticheat configuration.
# Flags are positive. Uncheck the flag to disable corresponding anticheat module.
anticheat_flags (Anticheat flags) flags digging,interaction,movement digging,interaction,movement

# Tolerance of movement cheat detector.
# Increase the value if players experience stuttery movement.
anticheat_movement_tolerance (Anticheat movement tolerance) float 1.0 1.0

# If enabled, actions are recorded for rollback.
# This option is only read when server starts.
Expand Down
5 changes: 4 additions & 1 deletion src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "mapgen/mapgen.h" // Mapgen::setDefaultSettings
#include "util/string.h"
#include "server.h"


/*
Expand Down Expand Up @@ -459,7 +460,9 @@ void set_default_settings()
settings->setDefault("enable_pvp", "true");
settings->setDefault("enable_mod_channels", "false");
settings->setDefault("disallow_empty_password", "false");
settings->setDefault("disable_anticheat", "false");
settings->setDefault("anticheat_flags", flagdesc_anticheat,
AC_DIGGING | AC_INTERACTION | AC_MOVEMENT);
settings->setDefault("anticheat_movement_tolerance", "1.0");
settings->setDefault("enable_rollback_recording", "false");
settings->setDefault("deprecated_lua_api_handling", "log");

Expand Down
9 changes: 9 additions & 0 deletions src/migratesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "settings.h"
#include "server.h"

void migrate_settings()
{
Expand All @@ -19,4 +20,12 @@ void migrate_settings()
g_settings->setBool("touch_gui", value);
g_settings->remove("enable_touch");
}

// Disables anticheat
if (g_settings->existsLocal("disable_anticheat")) {
if (g_settings->getBool("disable_anticheat")) {
g_settings->setFlagStr("anticheat_flags", 0, flagdesc_anticheat);
}
g_settings->remove("disable_anticheat");
}
}
8 changes: 4 additions & 4 deletions src/network/serverpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,12 +1011,12 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
/*
Check that target is reasonably close
*/
static thread_local const bool enable_anticheat =
!g_settings->getBool("disable_anticheat");
static thread_local const u32 anticheat_flags =
g_settings->getFlagStr("anticheat_flags", flagdesc_anticheat, nullptr);

if ((action == INTERACT_START_DIGGING || action == INTERACT_DIGGING_COMPLETED ||
action == INTERACT_PLACE || action == INTERACT_USE) &&
enable_anticheat && !isSingleplayer()) {
(anticheat_flags & AC_INTERACTION) && !isSingleplayer()) {
v3f target_pos = player_pos;
if (pointed.type == POINTEDTHING_NODE) {
target_pos = intToFloat(pointed.node_undersurface, BS);
Expand Down Expand Up @@ -1119,7 +1119,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)

/* Cheat prevention */
bool is_valid_dig = true;
if (enable_anticheat && !isSingleplayer()) {
if ((anticheat_flags & AC_DIGGING) && !isSingleplayer()) {
v3s16 nocheat_p = playersao->getNoCheatDigPos();
float nocheat_t = playersao->getNoCheatDigTime();
playersao->noCheatDigEnd();
Expand Down
14 changes: 14 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ struct PackedValue;
struct ParticleParameters;
struct ParticleSpawnerParameters;

// Anticheat flags
enum {
AC_DIGGING = 0x01,
AC_INTERACTION = 0x02,
AC_MOVEMENT = 0x04
};

constexpr const static FlagDesc flagdesc_anticheat[] = {
{"digging", AC_DIGGING},
{"interaction", AC_INTERACTION},
{"movement", AC_MOVEMENT},
{NULL, 0}
};

enum ClientDeletionReason {
CDR_LEAVE,
CDR_TIMEOUT,
Expand Down
10 changes: 9 additions & 1 deletion src/server/player_sao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,12 @@ void PlayerSAO::setMaxSpeedOverride(const v3f &vel)

bool PlayerSAO::checkMovementCheat()
{
static thread_local const u32 anticheat_flags =
g_settings->getFlagStr("anticheat_flags", flagdesc_anticheat, nullptr);

if (m_is_singleplayer ||
isAttached() ||
g_settings->getBool("disable_anticheat")) {
!(anticheat_flags & AC_MOVEMENT)) {
m_last_good_position = m_base_position;
return false;
}
Expand Down Expand Up @@ -729,6 +732,11 @@ bool PlayerSAO::checkMovementCheat()
required_time = MYMAX(required_time, d_vert / s);
}

static thread_local float anticheat_movement_tolerance =
std::max(g_settings->getFloat("anticheat_movement_tolerance"), 1.0f);

required_time /= anticheat_movement_tolerance;

if (m_move_pool.grab(required_time)) {
m_last_good_position = m_base_position;
} else {
Expand Down

0 comments on commit 1b2d247

Please sign in to comment.