From d7f2771da4d03c27f22b62bdcb86775633257d74 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 23:10:06 +0300 Subject: [PATCH] lua: pack.get_folder(pack_name) + shader fix --- res/shaders/main.glslf | 2 +- src/engine.cpp | 2 +- src/logic/scripting/api_lua.cpp | 22 +++++++++++++++++++++- src/logic/scripting/scripting.cpp | 10 +++++----- src/logic/scripting/scripting.h | 5 +++-- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/res/shaders/main.glslf b/res/shaders/main.glslf index ab561063..498ef490 100644 --- a/res/shaders/main.glslf +++ b/res/shaders/main.glslf @@ -16,7 +16,7 @@ void main(){ float depth = (a_distance/256.0); float alpha = a_color.a * tex_color.a; // anyway it's any alpha-test alternative required - if (alpha < 0.1f) + if (alpha < 0.5f) discard; f_color = mix(a_color * tex_color, vec4(fogColor,1.0), min(1.0, pow(depth*u_fogFactor, u_fogCurve))); f_color.a = alpha; diff --git a/src/engine.cpp b/src/engine.cpp index ee3b4da1..2204945e 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -46,7 +46,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths) } auto resdir = paths->getResources(); - scripting::initialize(paths); + scripting::initialize(this); std::cout << "-- loading assets" << std::endl; std::vector roots {resdir}; diff --git a/src/logic/scripting/api_lua.cpp b/src/logic/scripting/api_lua.cpp index 8403604c..75eacfdc 100644 --- a/src/logic/scripting/api_lua.cpp +++ b/src/logic/scripting/api_lua.cpp @@ -13,6 +13,7 @@ #include "../../voxels/voxel.h" #include "../../lighting/Lighting.h" #include "../../logic/BlocksController.h" +#include "../../engine.h" inline int lua_pushivec3(lua_State* L, int x, int y, int z) { lua_pushinteger(L, x); @@ -27,7 +28,25 @@ inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfunc lua_setglobal(L, name); } -/* == world library ==*/ +/* == pack library == */ +static int l_pack_get_folder(lua_State* L) { + std::string packName = lua_tostring(L, 1); + for (auto& pack : scripting::engine->getContentPacks()) { + if (pack.id == packName) { + lua_pushstring(L, (pack.folder.u8string()+"/").c_str()); + return 1; + } + } + lua_pushstring(L, ""); + return 1; +} + +static const luaL_Reg packlib [] = { + {"get_folder", l_pack_get_folder}, + {NULL, NULL} +}; + +/* == world library == */ static int l_world_get_day_time(lua_State* L) { lua_pushnumber(L, scripting::level->world->daytime); return 1; @@ -266,6 +285,7 @@ static int l_is_replaceable_at(lua_State* L) { lua_setglobal(L, NAME)) void apilua::create_funcs(lua_State* L) { + luaL_openlib(L, "pack", packlib, 0); luaL_openlib(L, "world", worldlib, 0); luaL_openlib(L, "player", playerlib, 0); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 6564e216..4ab975f3 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -11,19 +11,19 @@ #include "../../voxels/Block.h" #include "../../items/ItemDef.h" #include "../../logic/BlocksController.h" +#include "../../engine.h" #include "api_lua.h" using namespace scripting; namespace scripting { extern lua_State* L; - extern EnginePaths* paths; } +Engine* scripting::engine = nullptr; lua_State* scripting::L = nullptr; Level* scripting::level = nullptr; const Content* scripting::content = nullptr; -EnginePaths* scripting::paths = nullptr; BlocksController* scripting::blocks = nullptr; inline int lua_pushivec3(lua_State* L, int x, int y, int z) { @@ -58,8 +58,8 @@ int call_func(lua_State* L, int argc, const std::string& name) { return 1; } -void scripting::initialize(EnginePaths* paths) { - scripting::paths = paths; +void scripting::initialize(Engine* engine) { + scripting::engine = engine; L = luaL_newstate(); if (L == nullptr) { @@ -87,7 +87,7 @@ void scripting::on_world_load(Level* level, BlocksController* blocks) { scripting::level = level; scripting::content = level->content; scripting::blocks = blocks; - + auto paths = scripting::engine->getPaths(); fs::path file = paths->getResources()/fs::path("scripts/world.lua"); std::string src = files::read_string(file); luaL_loadbuffer(L, src.c_str(), src.length(), file.string().c_str()); diff --git a/src/logic/scripting/scripting.h b/src/logic/scripting/scripting.h index ff4522bc..c4cd6b05 100644 --- a/src/logic/scripting/scripting.h +++ b/src/logic/scripting/scripting.h @@ -3,7 +3,7 @@ namespace fs = std::filesystem; -class EnginePaths; +class Engine; class Content; class Level; class Block; @@ -14,11 +14,12 @@ struct item_funcs_set; class BlocksController; namespace scripting { + extern Engine* engine; extern const Content* content; extern Level* level; extern BlocksController* blocks; - void initialize(EnginePaths* paths); + void initialize(Engine* engine); void on_world_load(Level* level, BlocksController* blocks); void on_world_quit(); void update_block(const Block* block, int x, int y, int z);