iw5-mod/src/module/client_command.cpp

162 lines
4.7 KiB
C++
Raw Normal View History

2022-03-10 19:50:36 +00:00
#include <std_include.hpp>
2022-03-23 13:48:13 +00:00
#include <loader/module_loader.hpp>
#include "game/game.hpp"
2022-03-23 13:48:13 +00:00
#include "command.hpp"
2022-05-12 14:14:59 +01:00
#include "scheduler.hpp"
2022-03-10 19:50:36 +00:00
#include <utils/string.hpp>
2022-08-11 23:13:04 +02:00
static const game::native::dvar_t* g_cheats;
2022-03-10 19:50:36 +00:00
class client_command final : public module
{
public:
void post_load() override
{
if (game::is_mp())
{
2022-08-11 23:13:04 +02:00
g_cheats = game::native::Dvar_RegisterBool("sv_cheats", true, game::native::DVAR_CODINFO, "Enable cheats");
2022-03-10 19:50:36 +00:00
add_mp_client_commands();
}
}
private:
2022-08-11 23:04:29 +02:00
static bool cheats_ok(game::native::gentity_s* ent)
2022-03-10 19:50:36 +00:00
{
2022-08-11 23:13:04 +02:00
if (!g_cheats->current.enabled)
{
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
utils::string::va("%c \"GAME_CHEATSNOTENABLED\"", 0x65));
return false;
}
2022-08-11 23:04:29 +02:00
if (ent->health < 1)
2022-03-10 19:50:36 +00:00
{
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
utils::string::va("%c \"GAME_MUSTBEALIVECOMMAND\"", 0x65));
return false;
2022-03-10 19:50:36 +00:00
}
2022-08-11 23:04:29 +02:00
return true;
2022-03-10 19:50:36 +00:00
}
2022-08-11 23:04:29 +02:00
static bool cheats_ok_internal(game::native::sp::gentity_s* ent)
2022-03-10 19:50:36 +00:00
{
if (ent->health < 1)
{
2022-08-11 23:04:29 +02:00
game::native::sp::SV_GameSendServerCommand(ent->s.number, "print \"GAME_MUSTBEALIVECOMMAND\"");
2022-03-10 19:50:36 +00:00
return false;
}
return true;
}
static void add_mp_client_commands()
{
command::add_sv("noclip", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
if (!cheats_ok(ent))
return;
ent->client->flags ^= 1;
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
2022-03-10 19:50:36 +00:00
utils::string::va("%c \"%s\"", 0x65, (ent->client->flags & 1) ? "GAME_NOCLIPON" : "GAME_NOCLIPOFF"));
});
command::add_sv("ufo", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
if (!cheats_ok(ent))
return;
ent->client->flags ^= 2;
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
2022-03-10 19:50:36 +00:00
utils::string::va("%c \"%s\"", 0x65, (ent->client->flags & 2) ? "GAME_UFOON" : "GAME_UFOOFF"));
});
command::add_sv("god", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
if (!cheats_ok(ent))
return;
2022-03-10 20:02:25 +00:00
ent->flags ^= game::native::FL_GODMODE;
2022-03-10 19:50:36 +00:00
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
utils::string::va("%c \"%s\"", 0x65, (ent->flags & game::native::FL_GODMODE) ? "GAME_GODMODE_ON" : "GAME_GODMODE_OFF"));
2022-03-10 19:50:36 +00:00
});
command::add_sv("demigod", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
if (!cheats_ok(ent))
return;
2022-03-10 20:02:25 +00:00
ent->flags ^= game::native::FL_DEMI_GODMODE;
2022-03-10 19:50:36 +00:00
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
utils::string::va("%c \"%s\"", 0x65, (ent->flags & game::native::FL_DEMI_GODMODE) ? "GAME_DEMI_GODMODE_ON" : "GAME_DEMI_GODMODE_OFF"));
2022-03-10 19:50:36 +00:00
});
command::add_sv("notarget", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
if (!cheats_ok(ent))
return;
2022-03-10 20:02:25 +00:00
ent->flags ^= game::native::FL_NOTARGET;
2022-03-10 19:50:36 +00:00
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
utils::string::va("%c \"%s\"", 0x65, (ent->flags & game::native::FL_NOTARGET) ? "GAME_NOTARGETON" : "GAME_NOTARGETOFF"));
2022-03-10 19:50:36 +00:00
});
command::add_sv("setviewpos", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
if (!cheats_ok(ent))
return;
game::native::vec3_t origin, angles{0.f, 0.f, 0.f};
if (params.size() < 4 || params.size() > 6)
{
2022-08-11 23:04:29 +02:00
game::native::mp::SV_GameSendServerCommand(ent->s.number, game::native::SV_CMD_CAN_IGNORE,
2022-03-10 19:50:36 +00:00
utils::string::va("%c \"GAME_USAGE\x15: setviewpos x y z [yaw] [pitch]\"", 0x65));
return;
}
for (auto i = 0; i < 3; i++)
{
origin[i] = std::strtof(params.get(i + 1), nullptr);
}
if (params.size() > 4)
{
2023-01-11 12:21:45 +01:00
angles[game::native::YAW] = std::strtof(params.get(4), nullptr); // Yaw
2022-03-10 19:50:36 +00:00
}
2022-05-12 14:14:59 +01:00
if (params.size() == 6)
2022-03-10 19:50:36 +00:00
{
2023-01-11 12:21:45 +01:00
angles[game::native::PITCH] = std::strtof(params.get(5), nullptr); // Pitch
2022-03-10 19:50:36 +00:00
}
game::native::TeleportPlayer(ent, origin, angles);
});
2022-05-12 14:14:59 +01:00
command::add_sv("kill", [](game::native::gentity_s* ent, [[maybe_unused]] const command::params_sv& params)
{
assert(ent->client->sess.connected != game::native::CON_DISCONNECTED);
if (ent->client->sess.sessionState != game::native::SESS_STATE_PLAYING || !cheats_ok(ent))
return;
scheduler::once([ent]
{
2022-08-11 23:04:29 +02:00
ent->flags &= ~(game::native::FL_GODMODE | game::native::FL_DEMI_GODMODE);
2022-05-12 14:14:59 +01:00
ent->health = 0;
game::native::player_die(ent, ent, ent, 100000, 12, nullptr, false, nullptr, game::native::hitLocation_t::HITLOC_NONE, 0);
}, scheduler::pipeline::server);
});
2022-03-10 19:50:36 +00:00
}
};
REGISTER_MODULE(client_command);