add more cheating-related rules

This commit is contained in:
MihailRis 2024-11-06 18:54:20 +03:00
parent 22fa082fc6
commit 6602584c13
5 changed files with 65 additions and 20 deletions

View File

@ -230,6 +230,21 @@ function __vc_create_hud_rules()
_rules.create("show-content-access", hud._is_content_access(), function(value)
hud._set_content_access(value)
end)
_rules.create("allow-flight", true, function(value)
input.set_enabled("player.flight", value)
end)
_rules.create("allow-noclip", true, function(value)
input.set_enabled("player.noclip", value)
end)
_rules.create("allow-destruct", true, function(value)
input.set_enabled("player.attack", value)
end)
_rules.create("allow-cheat-movement", true, function(value)
input.set_enabled("player.cheat", value)
end)
_rules.create("allow-debug-cheats", true, function(value)
hud._set_debug_cheats(value)
end)
end
-- --------- Deprecated functions ------ --

View File

@ -42,7 +42,8 @@ static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
std::shared_ptr<UINode> create_debug_panel(
Engine* engine,
Level* level,
Player* player
Player* player,
bool allowDebugCheats
) {
auto panel = std::make_shared<Panel>(glm::vec2(300, 200), glm::vec4(5.0f), 2.0f);
panel->setId("hud.debug-panel");
@ -162,16 +163,20 @@ std::shared_ptr<UINode> create_debug_panel(
box->setTextSupplier([=]() {
return util::to_wstring(player->getPosition()[ax], 2);
});
box->setTextConsumer([=](const std::wstring& text) {
try {
glm::vec3 position = player->getPosition();
position[ax] = std::stoi(text);
player->teleport(position);
} catch (std::exception& _){
}
});
box->setOnEditStart([=](){
boxRef->setText(std::to_wstring(static_cast<int>(player->getPosition()[ax])));
if (allowDebugCheats) {
box->setTextConsumer([=](const std::wstring& text) {
try {
glm::vec3 position = player->getPosition();
position[ax] = std::stoi(text);
player->teleport(position);
} catch (std::exception& _){
}
});
}
box->setOnEditStart([=]() {
boxRef->setText(
std::to_wstring(static_cast<int>(player->getPosition()[ax]))
);
});
box->setSize(glm::vec2(230, 27));
@ -188,13 +193,13 @@ std::shared_ptr<UINode> create_debug_panel(
util::lfill(std::to_wstring(minute), 2, L'0');
return L"time: "+timeString;
}));
{
if (allowDebugCheats) {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->setSupplier([&]() {return worldInfo.daytime;});
bar->setConsumer([&](double val) {worldInfo.daytime = val;});
panel->add(bar);
}
{
if (allowDebugCheats) {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->setSupplier([&]() {return worldInfo.fog;});
bar->setConsumer([&](double val) {worldInfo.fog = val;});

View File

@ -60,9 +60,10 @@ bool Hud::showGeneratorMinimap = false;
// implemented in debug_panel.cpp
extern std::shared_ptr<UINode> create_debug_panel(
Engine* engine,
Level* level,
Player* player
Engine* engine,
Level* level,
Player* player,
bool allowDebugCheats
);
HudElement::HudElement(
@ -149,7 +150,8 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
static constexpr uint WORLDGEN_IMG_SIZE = 128U;
Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
: assets(engine->getAssets()),
: engine(engine),
assets(engine->getAssets()),
gui(engine->getGUI()),
frontend(frontend),
player(player),
@ -175,12 +177,14 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
uicamera->perspective = false;
uicamera->flipped = true;
debugPanel = create_debug_panel(engine, frontend->getLevel(), player);
debugPanel = create_debug_panel(
engine, frontend->getLevel(), player, allowDebugCheats
);
debugPanel->setZIndex(2);
gui->add(debugPanel);
gui->add(darkOverlay);
gui->add(hotbarView);
gui->add(debugPanel);
gui->add(contentAccessPanel);
auto dplotter = std::make_shared<Plotter>(350, 250, 2000, 16);
@ -638,3 +642,14 @@ bool Hud::isContentAccess() const {
void Hud::setContentAccess(bool flag) {
showContentPanel = flag;
}
void Hud::setDebugCheats(bool flag) {
allowDebugCheats = flag;
gui->remove(debugPanel);
debugPanel = create_debug_panel(
engine, frontend->getLevel(), player, allowDebugCheats
);
debugPanel->setZIndex(2);
gui->add(debugPanel);
}

View File

@ -69,6 +69,7 @@ public:
};
class Hud : public util::ObjectsKeeper {
Engine* engine;
Assets* assets;
std::unique_ptr<Camera> uicamera;
gui::GUI* gui;
@ -107,7 +108,8 @@ class Hud : public util::ObjectsKeeper {
blockid_t currentblockid = 0;
/// @brief Show content access panel
bool showContentPanel = true;
/// @brief Provide cheat controllers to the debug panel
bool allowDebugCheats = true;
/// @brief UI element will be dynamicly positioned near to inventory or in screen center
std::shared_ptr<gui::UINode> secondUI = nullptr;
@ -178,6 +180,8 @@ public:
void setContentAccess(bool flag);
void setDebugCheats(bool flag);
static bool showGeneratorMinimap;
/// @brief Runtime updating debug visualization texture

View File

@ -146,6 +146,11 @@ static int l_set_content_access(lua::State* L) {
return 0;
}
static int l_set_debug_cheats(lua::State* L) {
hud->setDebugCheats(lua::toboolean(L, 1));
return 0;
}
const luaL_Reg hudlib[] = {
{"open_inventory", lua::wrap<l_open_inventory>},
{"close_inventory", lua::wrap<l_close_inventory>},
@ -161,4 +166,5 @@ const luaL_Reg hudlib[] = {
{"get_player", lua::wrap<l_get_player>},
{"_is_content_access", lua::wrap<l_is_content_access>},
{"_set_content_access", lua::wrap<l_set_content_access>},
{"_set_debug_cheats", lua::wrap<l_set_debug_cheats>},
{NULL, NULL}};