Merge branch 'dev' into pathfinding

This commit is contained in:
MihailRis
2025-08-19 19:47:10 +03:00
57 changed files with 818 additions and 207 deletions
@@ -54,7 +54,7 @@ static int l_get_gravity_scale(lua::State* L) {
static int l_set_gravity_scale(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->getRigidbody().hitbox.gravityScale = lua::tonumber(L, 2);
entity->getRigidbody().hitbox.gravityScale = lua::tovec3(L, 2).y;
}
return 0;
}
@@ -23,6 +23,9 @@ static void load_texture(
}
static int l_load_texture(lua::State* L) {
if (lua::isstring(L, 3) && lua::require_lstring(L, 3) != "png") {
throw std::runtime_error("unsupportd image format");
}
if (lua::istable(L, 1)) {
lua::pushvalue(L, 1);
size_t size = lua::objlen(L, 1);
+1 -4
View File
@@ -101,12 +101,9 @@ static int l_set(lua::State* L) {
if (static_cast<size_t>(id) >= indices->blocks.count()) {
return 0;
}
int cx = floordiv<CHUNK_W>(x);
int cz = floordiv<CHUNK_D>(z);
if (!blocks_agent::get_chunk(*level->chunks, cx, cz)) {
if (!blocks_agent::set(*level->chunks, x, y, z, id, int2blockstate(state))) {
return 0;
}
blocks_agent::set(*level->chunks, x, y, z, id, int2blockstate(state));
auto chunksController = controller->getChunksController();
if (chunksController == nullptr) {
+5 -2
View File
@@ -79,9 +79,12 @@ static int l_textbox_paste(lua::State* L) {
static int l_container_add(lua::State* L) {
auto docnode = get_document_node(L);
if (docnode.document == nullptr) {
throw std::runtime_error("target document not found");
}
auto node = dynamic_cast<Container*>(docnode.node.get());
if (node == nullptr) {
return 0;
throw std::runtime_error("target container not found");
}
auto xmlsrc = lua::require_string(L, 2);
try {
@@ -99,7 +102,7 @@ static int l_container_add(lua::State* L) {
UINode::getIndices(subnode, docnode.document->getMapWriteable());
node->add(std::move(subnode));
} catch (const std::exception& err) {
throw std::runtime_error(err.what());
throw std::runtime_error("container:add(...): " + std::string(err.what()));
}
return 0;
}
+1 -1
View File
@@ -46,7 +46,7 @@ static int l_open(lua::State* L) {
}
return lua::pushinteger(L, hud->openInventory(
layout,
level->inventories->get(invid),
lua::isnoneornil(L, 3) ? nullptr : level->inventories->get(invid),
playerInventory
)->getId());
}
@@ -45,6 +45,12 @@ namespace {
template <SlotFunc func>
int wrap_slot(lua::State* L) {
if (lua::isnoneornil(L, 1)) {
throw std::runtime_error("inventory id is nil");
}
if (lua::isnoneornil(L, 2)) {
throw std::runtime_error("slot index is nil");
}
auto invid = lua::tointeger(L, 1);
auto slotid = lua::tointeger(L, 2);
auto& inv = get_inventory(invid);
+44 -3
View File
@@ -25,6 +25,7 @@
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "interfaces/Process.hpp"
using namespace scripting;
@@ -112,11 +113,47 @@ public:
}
};
std::unique_ptr<Process> scripting::start_coroutine(
class LuaProjectScript : public IClientProjectScript {
public:
LuaProjectScript(lua::State* L, scriptenv env) : L(L), env(std::move(env)) {}
void onScreenChange(const std::string& name, bool show) override {
if (!lua::pushenv(L, *env)) {
return;
}
if (!lua::getfield(L, "on_" + name + (show ? "_setup" : "_clear"))) {
lua::pop(L);
return;
}
lua::call_nothrow(L, 0, 0);
lua::pop(L);
}
private:
lua::State* L;
scriptenv env;
};
std::unique_ptr<IClientProjectScript> scripting::load_client_project_script(
const io::path& script
) {
auto L = lua::get_main_state();
if (lua::getglobal(L, "__vc_start_coroutine")) {
auto source = io::read_string(script);
auto env = create_environment(nullptr);
lua::pushenv(L, *env);
if (lua::getglobal(L, "__vc_app")) {
lua::setfield(L, "app");
}
lua::pop(L);
lua::loadbuffer(L, *env, source, script.name());
lua::call(L, 0);
return std::make_unique<LuaProjectScript>(L, std::move(env));
}
std::unique_ptr<Process> scripting::start_coroutine(const io::path& script) {
auto L = lua::get_main_state();
auto method = "__vc_start_coroutine";
if (lua::getglobal(L, method)) {
auto source = io::read_string(script);
lua::loadbuffer(L, 0, source, script.name());
if (lua::call(L, 1)) {
@@ -259,7 +296,11 @@ void scripting::on_world_load(LevelController* controller) {
}
for (auto& pack : content_control->getAllContentPacks()) {
lua::emit_event(L, pack.id + ":.worldopen");
lua::emit_event(L, pack.id + ":.worldopen", [](auto L) {
return lua::pushboolean(
L, !scripting::level->getWorld()->getInfo().isLoaded
);
});
}
}
+10 -1
View File
@@ -65,10 +65,19 @@ namespace scripting {
void process_post_runnables();
std::unique_ptr<Process> start_coroutine(
class IClientProjectScript {
public:
virtual ~IClientProjectScript() {}
virtual void onScreenChange(const std::string& name, bool show) = 0;
};
std::unique_ptr<IClientProjectScript> load_client_project_script(
const io::path& script
);
std::unique_ptr<Process> start_coroutine(const io::path& script);
void on_world_load(LevelController* controller);
void on_world_tick(int tps);
void on_world_save();