diff --git a/res/modules/internal/maths_inline.lua b/res/modules/internal/maths_inline.lua new file mode 100644 index 00000000..e3e37fcd --- /dev/null +++ b/res/modules/internal/maths_inline.lua @@ -0,0 +1,212 @@ +-- =================================================== -- +-- ====================== vec3 ======================= -- +-- =================================================== -- +function vec3.add(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] + b[1] + dst[2] = a[2] + b[2] + dst[3] = a[3] + b[3] + else + dst[1] = a[1] + b + dst[2] = a[2] + b + dst[3] = a[3] + b + end + return dst + else + if btype == "table" then + return {a[1] + b[1], a[2] + b[2], a[3] + b[3]} + else + return {a[1] + b, a[2] + b, a[3] + b} + end + end +end + +function vec3.sub(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] - b[1] + dst[2] = a[2] - b[2] + dst[3] = a[3] - b[3] + else + dst[1] = a[1] - b + dst[2] = a[2] - b + dst[3] = a[3] - b + end + return dst + else + if btype == "table" then + return {a[1] - b[1], a[2] - b[2], a[3] - b[3]} + else + return {a[1] - b, a[2] - b, a[3] - b} + end + end +end + +function vec3.mul(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] * b[1] + dst[2] = a[2] * b[2] + dst[3] = a[3] * b[3] + else + dst[1] = a[1] * b + dst[2] = a[2] * b + dst[3] = a[3] * b + end + return dst + else + if btype == "table" then + return {a[1] * b[1], a[2] * b[2], a[3] * b[3]} + else + return {a[1] * b, a[2] * b, a[3] * b} + end + end +end + +function vec3.div(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] / b[1] + dst[2] = a[2] / b[2] + dst[3] = a[3] / b[3] + else + dst[1] = a[1] / b + dst[2] = a[2] / b + dst[3] = a[3] / b + end + return dst + else + if btype == "table" then + return {a[1] / b[1], a[2] / b[2], a[3] / b[3]} + else + return {a[1] / b, a[2] / b, a[3] / b} + end + end +end + +function vec3.abs(a, dst) + local x = a[1] + local y = a[2] + local z = a[3] + if dst then + dst[1] = x < 0.0 and -x or x + dst[2] = y < 0.0 and -y or y + dst[3] = z < 0.0 and -z or z + else + return { + x < 0.0 and -x or x, + y < 0.0 and -y or y, + z < 0.0 and -z or z, + } + end +end + +function vec3.dot(a, b) + return a[1] * b[1] + a[2] * b[2] + a[3] * b[3] +end + +-- =================================================== -- +-- ====================== vec2 ======================= -- +-- =================================================== -- +function vec2.add(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] + b[1] + dst[2] = a[2] + b[2] + else + dst[1] = a[1] + b + dst[2] = a[2] + b + end + return dst + else + if btype == "table" then + return {a[1] + b[1], a[2] + b[2]} + else + return {a[1] + b, a[2] + b} + end + end +end + +function vec2.sub(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] - b[1] + dst[2] = a[2] - b[2] + else + dst[1] = a[1] - b + dst[2] = a[2] - b + end + return dst + else + if btype == "table" then + return {a[1] - b[1], a[2] - b[2]} + else + return {a[1] - b, a[2] - b} + end + end +end + +function vec2.mul(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] * b[1] + dst[2] = a[2] * b[2] + else + dst[1] = a[1] * b + dst[2] = a[2] * b + end + return dst + else + if btype == "table" then + return {a[1] * b[1], a[2] * b[2]} + else + return {a[1] * b, a[2] * b} + end + end +end + +function vec2.div(a, b, dst) + local btype = type(b) + if dst then + if btype == "table" then + dst[1] = a[1] / b[1] + dst[2] = a[2] / b[2] + else + dst[1] = a[1] / b + dst[2] = a[2] / b + end + return dst + else + if btype == "table" then + return {a[1] / b[1], a[2] / b[2]} + else + return {a[1] / b, a[2] / b} + end + end +end + +function vec2.abs(a, dst) + local x = a[1] + local y = a[2] + if dst then + dst[1] = x < 0.0 and -x or x + dst[2] = y < 0.0 and -y or y + else + return { + x < 0.0 and -x or x, + y < 0.0 and -y or y, + } + end +end + +function vec2.dot(a, b) + return a[1] * b[1] + a[2] * b[2] +end diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 68b9e4f6..9e4f9556 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -1,3 +1,5 @@ +local enable_experimental = core.get_setting("debug.enable-experimental") + ------------------------------------------------ ------ Extended kit of standard functions ------ ------------------------------------------------ @@ -169,6 +171,10 @@ function inventory.set_description(invid, slot, description) inventory.set_data(invid, slot, "description", description) end +if enable_experimental then + require "core:internal/maths_inline" +end + events = require "core:internal/events" function pack.unload(prefix) diff --git a/src/io/settings_io.cpp b/src/io/settings_io.cpp index cdbf567e..56c5f038 100644 --- a/src/io/settings_io.cpp +++ b/src/io/settings_io.cpp @@ -86,6 +86,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) { builder.section("debug"); builder.add("generator-test-mode", &settings.debug.generatorTestMode); builder.add("do-write-lights", &settings.debug.doWriteLights); + builder.add("enable-experimental", &settings.debug.enableExperimental); } dv::value SettingsHandler::getValue(const std::string& name) const { diff --git a/src/settings.hpp b/src/settings.hpp index d4c21688..10986f39 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -90,6 +90,8 @@ struct DebugSettings { FlagSetting generatorTestMode {false}; /// @brief Write lights cache FlagSetting doWriteLights {true}; + /// @brief Enable experimental optimizations and features + FlagSetting enableExperimental {false}; }; struct UiSettings {