diff --git a/res/scripts/world.lua b/res/scripts/world.lua index a63f8ee1..c48b5615 100644 --- a/res/scripts/world.lua +++ b/res/scripts/world.lua @@ -2,8 +2,8 @@ -- must be empty in release -- must not be modified by content-packs -local W = 16 -local H = 16 +local W = 1024 +local H = 1024 for t=1,1 do local tm = time.uptime() @@ -28,10 +28,10 @@ for t=1,1 do local rivermap = Heightmap(W, H) rivermap:noise({21, 12}, 0.05, 3) rivermap:abs() - rivermap:min(0.02) - rivermap:mul(50.0) - rivermap:pow(0.4) - map:add(1.7) + rivermap:min(0.1) + rivermap:mul(10.0) + rivermap:pow(0.8) + map:add(1.2) map:mul(rivermap) map:add(-1.0) map:mul(0.5) diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 44721ca6..5515fc0a 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -77,7 +77,7 @@ static int l_mul(lua::State* L) { /// transformed copy of matrix mat4.(matrix: float[16], vec: float[3], /// dst: float[16]) -> sets dst to transformed version of matrix template -inline int l_transform_func(lua::State* L) { +inline int l_binop_func(lua::State* L) { uint argc = lua::gettop(L); switch (argc) { case 1: { @@ -272,9 +272,9 @@ static int l_tostring(lua::State* L) { const luaL_Reg mat4lib[] = { {"idt", lua::wrap}, {"mul", lua::wrap}, - {"scale", lua::wrap>}, + {"scale", lua::wrap>}, {"rotate", lua::wrap}, - {"translate", lua::wrap>}, + {"translate", lua::wrap>}, {"inverse", lua::wrap}, {"transpose", lua::wrap}, {"determinant", lua::wrap}, diff --git a/src/logic/scripting/lua/lua_type_heightmap.cpp b/src/logic/scripting/lua/lua_type_heightmap.cpp index e53ecb48..ecbdc1bf 100644 --- a/src/logic/scripting/lua/lua_type_heightmap.cpp +++ b/src/logic/scripting/lua/lua_type_heightmap.cpp @@ -5,6 +5,7 @@ #include #include +#include "util/functional_util.hpp" #include "maths/FastNoiseLite.h" #include "coders/png.hpp" #include "graphics/core/ImageData.hpp" @@ -94,7 +95,38 @@ static int l_noise(lua::State* L) { return 0; } -static int l_pow(lua::State* L) { +template class Op> +static int l_binop_func(lua::State* L) { + Op op; + if (auto heightmap = touserdata(L, 1)) { + uint w = heightmap->getWidth(); + uint h = heightmap->getHeight(); + auto heights = heightmap->getValues(); + + if (isnumber(L, 2)) { + float scalar = tonumber(L, 2); + for (uint y = 0; y < h; y++) { + for (uint x = 0; x < w; x++) { + uint i = y * w + x; + heights[i] = op(heights[i], scalar); + } + } + } else { + auto map = touserdata(L, 2); + for (uint y = 0; y < h; y++) { + for (uint x = 0; x < w; x++) { + uint i = y * w + x; + heights[i] = op(heights[i], map->getValues()[i]); + } + } + } + } + return 0; +} + +template class Op> +static int l_unaryop_func(lua::State* L) { + Op op; if (auto heightmap = touserdata(L, 1)) { uint w = heightmap->getWidth(); uint h = heightmap->getHeight(); @@ -103,131 +135,7 @@ static int l_pow(lua::State* L) { for (uint y = 0; y < h; y++) { for (uint x = 0; x < w; x++) { uint i = y * w + x; - heights[i] = glm::pow(heights[i], power); - } - } - } - return 0; -} - -static int l_add(lua::State* L) { - if (auto heightmap = touserdata(L, 1)) { - uint w = heightmap->getWidth(); - uint h = heightmap->getHeight(); - auto heights = heightmap->getValues(); - - if (isnumber(L, 2)) { - float scalar = tonumber(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] += scalar; - } - } - } else { - auto map = touserdata(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] += map->getValues()[i]; - } - } - } - } - return 0; -} - -static int l_mul(lua::State* L) { - if (auto heightmap = touserdata(L, 1)) { - uint w = heightmap->getWidth(); - uint h = heightmap->getHeight(); - auto heights = heightmap->getValues(); - - if (isnumber(L, 2)) { - float scalar = tonumber(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] *= scalar; - } - } - } else { - auto map = touserdata(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] *= map->getValues()[i]; - } - } - } - } - return 0; -} - -static int l_max(lua::State* L) { - if (auto heightmap = touserdata(L, 1)) { - uint w = heightmap->getWidth(); - uint h = heightmap->getHeight(); - auto heights = heightmap->getValues(); - - if (isnumber(L, 2)) { - float scalar = tonumber(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] *= scalar; - } - } - } else { - auto map = touserdata(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] = glm::max(map->getValues()[i], heights[i]); - } - } - } - } - return 0; -} - -static int l_min(lua::State* L) { - if (auto heightmap = touserdata(L, 1)) { - uint w = heightmap->getWidth(); - uint h = heightmap->getHeight(); - auto heights = heightmap->getValues(); - - if (isnumber(L, 2)) { - float scalar = tonumber(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] *= scalar; - } - } - } else { - auto map = touserdata(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] = glm::min(map->getValues()[i], heights[i]); - } - } - } - } - return 0; -} - -static int l_abs(lua::State* L) { - if (auto heightmap = touserdata(L, 1)) { - uint w = heightmap->getWidth(); - uint h = heightmap->getHeight(); - auto heights = heightmap->getValues(); - float power = tonumber(L, 2); - for (uint y = 0; y < h; y++) { - for (uint x = 0; x < w; x++) { - uint i = y * w + x; - heights[i] = glm::abs(heights[i]); + heights[i] = op(heights[i]); } } } @@ -237,12 +145,12 @@ static int l_abs(lua::State* L) { static std::unordered_map methods { {"dump", lua::wrap}, {"noise", lua::wrap}, - {"pow", lua::wrap}, - {"add", lua::wrap}, - {"mul", lua::wrap}, - {"min", lua::wrap}, - {"max", lua::wrap}, - {"abs", lua::wrap}, + {"pow", lua::wrap>}, + {"add", lua::wrap>}, + {"mul", lua::wrap>}, + {"min", lua::wrap>}, + {"max", lua::wrap>}, + {"abs", lua::wrap>}, }; static int l_meta_meta_call(lua::State* L) { diff --git a/src/util/functional_util.hpp b/src/util/functional_util.hpp new file mode 100644 index 00000000..2672c4fc --- /dev/null +++ b/src/util/functional_util.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace util { + template + struct pow { + constexpr T operator()(T a, T b) const { + return glm::pow(a, b); + } + }; + + template + struct min { + constexpr T operator()(T a, T b) const { + return glm::min(a, b); + } + }; + + template + struct max { + constexpr T operator()(T a, T b) const { + return glm::max(a, b); + } + }; + + template + struct abs { + constexpr T operator()(T a) const { + return glm::abs(a); + } + }; +}