Merge branch 'main' into dev

This commit is contained in:
MihailRis
2025-09-30 23:40:02 +03:00
39 changed files with 658 additions and 233 deletions
@@ -54,7 +54,12 @@ 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::tovec3(L, 2).y;
auto& hitbox = entity->getRigidbody().hitbox;
if (lua::istable(L, 2)) {
hitbox.gravityScale = lua::tovec3(L, 2).y;
} else {
hitbox.gravityScale = lua::tonumber(L, 2);
}
}
return 0;
}
+16 -6
View File
@@ -1,8 +1,4 @@
#include <algorithm>
#include <filesystem>
#include <stdexcept>
#include <string>
#include <set>
#define VC_ENABLE_REFLECTION
#include "assets/AssetsLoader.hpp"
#include "content/Content.hpp"
@@ -19,6 +15,12 @@
#include "world/World.hpp"
#include "api_lua.hpp"
#include <algorithm>
#include <filesystem>
#include <stdexcept>
#include <string>
#include <set>
using namespace scripting;
static int l_pack_get_folder(lua::State* L) {
@@ -114,8 +116,16 @@ static int l_pack_get_info(
default:
throw std::runtime_error("");
}
auto opString = VersionOperatorMeta.getNameString(dpack.op);
lua::pushfstring(L, "%s%s@%s%s", prefix.c_str(), dpack.id.c_str(), dpack.op.c_str(), dpack.version.c_str());
lua::pushfstring(
L,
"%s%s@%s%s",
prefix.c_str(),
dpack.id.c_str(),
(dpack.op == VersionOperator::EQUAL ? "" : opString).c_str(),
dpack.version.c_str()
);
lua::rawseti(L, i + 1);
}
lua::setfield(L, "dependencies");
@@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <array>
#include <random>
#include "lua_commons.hpp"
@@ -114,4 +115,20 @@ namespace lua {
std::shared_ptr<ImageData> mData;
};
static_assert(!std::is_abstract<LuaCanvas>());
class LuaRandom : public Userdata {
public:
std::mt19937 rng;
explicit LuaRandom(uint64_t seed) : rng(seed) {}
virtual ~LuaRandom() override = default;
const std::string& getTypeName() const override {
return TYPENAME;
}
static int createMetatable(lua::State*);
inline static std::string TYPENAME = "__vc_Random";
};
static_assert(!std::is_abstract<LuaRandom>());
}
+8
View File
@@ -169,5 +169,13 @@ State* lua::create_state(const EnginePaths& paths, StateType stateType) {
auto file = "res:scripts/stdmin.lua";
auto src = io::read_string(file);
lua::pop(L, lua::execute(L, 0, src, "core:scripts/stdmin.lua"));
newusertype<LuaRandom>(L);
if (getglobal(L, "random")) {
if (getglobal(L, "__vc_Random")) {
setfield(L, "Random");
}
pop(L);
}
return L;
}
+9
View File
@@ -275,6 +275,15 @@ namespace lua {
}
return nullptr;
}
template <class T>
inline T& require_userdata(lua::State* L, int idx) {
if (void* rawptr = lua_touserdata(L, idx)) {
return *static_cast<T*>(rawptr);
}
throw std::runtime_error("invalid 'self' value");
}
template <class T, typename... Args>
inline int newuserdata(lua::State* L, Args&&... args) {
const auto& found = usertypeNames.find(typeid(T));
@@ -0,0 +1,56 @@
#include "../lua_custom_types.hpp"
#include "../lua_util.hpp"
#include <chrono>
using namespace lua;
using namespace std::chrono;
static int l_random(lua::State* L) {
std::uniform_int_distribution<> dist(0, std::numeric_limits<int>::max());
auto& rng = require_userdata<LuaRandom>(L, 1).rng;
size_t n = touinteger(L, 2);
createtable(L, n, 0);
for (size_t i = 0; i < n; i++) {
pushnumber(L, dist(rng) / (double)std::numeric_limits<int>::max());
rawseti(L, i + 1);
}
return 1;
}
static int l_seed(lua::State* L) {
require_userdata<LuaRandom>(L, 1).rng = std::mt19937(lua::touinteger(L, 2));
return 0;
}
static int l_meta_meta_call(lua::State* L) {
integer_t seed;
if (lua::isnoneornil(L, 1)) {
seed = system_clock::now().time_since_epoch().count();
} else {
seed = tointeger(L, 1);
}
return newuserdata<LuaRandom>(L, seed);
}
int LuaRandom::createMetatable(lua::State* L) {
createtable(L, 0, 3);
requireglobal(L, "__vc_create_random_methods");
createtable(L, 0, 0);
pushcfunction(L, wrap<l_random>);
setfield(L, "random");
pushcfunction(L, wrap<l_seed>);
setfield(L, "seed");
call(L, 1, 1);
setfield(L, "__index");
createtable(L, 0, 1);
pushcfunction(L, wrap<l_meta_meta_call>);
setfield(L, "__call");
setmetatable(L);
return 1;
}