]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Make LuaVoxelManipulator available to CSM API
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 29 Nov 2021 10:34:12 +0000 (11:34 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 29 Nov 2021 10:34:12 +0000 (11:34 +0100)
builtin/client/init.lua
builtin/common/voxelarea.lua [new file with mode: 0644]
builtin/game/init.lua
builtin/game/voxelarea.lua [deleted file]
src/script/lua_api/l_env.cpp
src/script/scripting_client.cpp

index 8c6512ec1b576b77c7595ff1e1e10151d3ce2405..d750375b8e7ef8893516c1d87ccc808bdce09b74 100644 (file)
@@ -7,6 +7,7 @@ dofile(clientpath .. "register.lua")
 dofile(commonpath .. "after.lua")
 dofile(commonpath .. "chatcommands.lua")
 dofile(commonpath .. "vector.lua")
+dofile(commonpath .. "voxelarea.lua")
 dofile(clientpath .. "util.lua")
 dofile(clientpath .. "chatcommands.lua")
 dofile(clientpath .. "death_formspec.lua")
diff --git a/builtin/common/voxelarea.lua b/builtin/common/voxelarea.lua
new file mode 100644 (file)
index 0000000..64436bf
--- /dev/null
@@ -0,0 +1,132 @@
+VoxelArea = {
+       MinEdge = vector.new(1, 1, 1),
+       MaxEdge = vector.new(0, 0, 0),
+       ystride = 0,
+       zstride = 0,
+}
+
+function VoxelArea:new(o)
+       o = o or {}
+       setmetatable(o, self)
+       self.__index = self
+
+       local e = o:getExtent()
+       o.ystride = e.x
+       o.zstride = e.x * e.y
+
+       return o
+end
+
+function VoxelArea:getExtent()
+       local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
+       return vector.new(
+               MaxEdge.x - MinEdge.x + 1,
+               MaxEdge.y - MinEdge.y + 1,
+               MaxEdge.z - MinEdge.z + 1
+       )
+end
+
+function VoxelArea:getVolume()
+       local e = self:getExtent()
+       return e.x * e.y * e.z
+end
+
+function VoxelArea:index(x, y, z)
+       local MinEdge = self.MinEdge
+       local i = (z - MinEdge.z) * self.zstride +
+                         (y - MinEdge.y) * self.ystride +
+                         (x - MinEdge.x) + 1
+       return math.floor(i)
+end
+
+function VoxelArea:indexp(p)
+       local MinEdge = self.MinEdge
+       local i = (p.z - MinEdge.z) * self.zstride +
+                         (p.y - MinEdge.y) * self.ystride +
+                         (p.x - MinEdge.x) + 1
+       return math.floor(i)
+end
+
+function VoxelArea:position(i)
+       local p = {}
+       local MinEdge = self.MinEdge
+
+       i = i - 1
+
+       p.z = math.floor(i / self.zstride) + MinEdge.z
+       i = i % self.zstride
+
+       p.y = math.floor(i / self.ystride) + MinEdge.y
+       i = i % self.ystride
+
+       p.x = math.floor(i) + MinEdge.x
+
+       return p
+end
+
+function VoxelArea:contains(x, y, z)
+       local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
+       return (x >= MinEdge.x) and (x <= MaxEdge.x) and
+                  (y >= MinEdge.y) and (y <= MaxEdge.y) and
+                  (z >= MinEdge.z) and (z <= MaxEdge.z)
+end
+
+function VoxelArea:containsp(p)
+       local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
+       return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and
+                  (p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and
+                  (p.z >= MinEdge.z) and (p.z <= MaxEdge.z)
+end
+
+function VoxelArea:containsi(i)
+       return (i >= 1) and (i <= self:getVolume())
+end
+
+function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
+       local i = self:index(minx, miny, minz) - 1
+       local xrange = maxx - minx + 1
+       local nextaction = i + 1 + xrange
+
+       local y = 0
+       local yrange = maxy - miny + 1
+       local yreqstride = self.ystride - xrange
+
+       local z = 0
+       local zrange = maxz - minz + 1
+       local multistride = self.zstride - ((yrange - 1) * self.ystride + xrange)
+
+       return function()
+               -- continue i until it needs to jump
+               i = i + 1
+               if i ~= nextaction then
+                       return i
+               end
+
+               -- continue y until maxy is exceeded
+               y = y + 1
+               if y ~= yrange then
+                       -- set i to index(minx, miny + y, minz + z) - 1
+                       i = i + yreqstride
+                       nextaction = i + xrange
+                       return i
+               end
+
+               -- continue z until maxz is exceeded
+               z = z + 1
+               if z == zrange then
+                       -- cuboid finished, return nil
+                       return
+               end
+
+               -- set i to index(minx, miny, minz + z) - 1
+               i = i + multistride
+
+               y = 0
+               nextaction = i + xrange
+               return i
+       end
+end
+
+function VoxelArea:iterp(minp, maxp)
+       return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
+end
index bb007fabdfe85121d36cecf9aa6def76aa140268..9a9966d7e57ab8f2f999c2f6f74c0b54e9f41fe4 100644 (file)
@@ -16,6 +16,7 @@ if core.settings:get_bool("profiler.load") then
 end
 
 dofile(commonpath .. "after.lua")
+dofile(commonpath .. "voxelarea.lua")
 dofile(gamepath .. "item_entity.lua")
 dofile(gamepath .. "deprecated.lua")
 dofile(gamepath .. "misc.lua")
@@ -28,7 +29,6 @@ dofile(gamepath .. "static_spawn.lua")
 dofile(gamepath .. "detached_inventory.lua")
 assert(loadfile(gamepath .. "falling.lua"))(builtin_shared)
 dofile(gamepath .. "features.lua")
-dofile(gamepath .. "voxelarea.lua")
 dofile(gamepath .. "forceloading.lua")
 dofile(gamepath .. "statbars.lua")
 dofile(gamepath .. "knockback.lua")
diff --git a/builtin/game/voxelarea.lua b/builtin/game/voxelarea.lua
deleted file mode 100644 (file)
index 64436bf..0000000
+++ /dev/null
@@ -1,132 +0,0 @@
-VoxelArea = {
-       MinEdge = vector.new(1, 1, 1),
-       MaxEdge = vector.new(0, 0, 0),
-       ystride = 0,
-       zstride = 0,
-}
-
-function VoxelArea:new(o)
-       o = o or {}
-       setmetatable(o, self)
-       self.__index = self
-
-       local e = o:getExtent()
-       o.ystride = e.x
-       o.zstride = e.x * e.y
-
-       return o
-end
-
-function VoxelArea:getExtent()
-       local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
-       return vector.new(
-               MaxEdge.x - MinEdge.x + 1,
-               MaxEdge.y - MinEdge.y + 1,
-               MaxEdge.z - MinEdge.z + 1
-       )
-end
-
-function VoxelArea:getVolume()
-       local e = self:getExtent()
-       return e.x * e.y * e.z
-end
-
-function VoxelArea:index(x, y, z)
-       local MinEdge = self.MinEdge
-       local i = (z - MinEdge.z) * self.zstride +
-                         (y - MinEdge.y) * self.ystride +
-                         (x - MinEdge.x) + 1
-       return math.floor(i)
-end
-
-function VoxelArea:indexp(p)
-       local MinEdge = self.MinEdge
-       local i = (p.z - MinEdge.z) * self.zstride +
-                         (p.y - MinEdge.y) * self.ystride +
-                         (p.x - MinEdge.x) + 1
-       return math.floor(i)
-end
-
-function VoxelArea:position(i)
-       local p = {}
-       local MinEdge = self.MinEdge
-
-       i = i - 1
-
-       p.z = math.floor(i / self.zstride) + MinEdge.z
-       i = i % self.zstride
-
-       p.y = math.floor(i / self.ystride) + MinEdge.y
-       i = i % self.ystride
-
-       p.x = math.floor(i) + MinEdge.x
-
-       return p
-end
-
-function VoxelArea:contains(x, y, z)
-       local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
-       return (x >= MinEdge.x) and (x <= MaxEdge.x) and
-                  (y >= MinEdge.y) and (y <= MaxEdge.y) and
-                  (z >= MinEdge.z) and (z <= MaxEdge.z)
-end
-
-function VoxelArea:containsp(p)
-       local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
-       return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and
-                  (p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and
-                  (p.z >= MinEdge.z) and (p.z <= MaxEdge.z)
-end
-
-function VoxelArea:containsi(i)
-       return (i >= 1) and (i <= self:getVolume())
-end
-
-function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
-       local i = self:index(minx, miny, minz) - 1
-       local xrange = maxx - minx + 1
-       local nextaction = i + 1 + xrange
-
-       local y = 0
-       local yrange = maxy - miny + 1
-       local yreqstride = self.ystride - xrange
-
-       local z = 0
-       local zrange = maxz - minz + 1
-       local multistride = self.zstride - ((yrange - 1) * self.ystride + xrange)
-
-       return function()
-               -- continue i until it needs to jump
-               i = i + 1
-               if i ~= nextaction then
-                       return i
-               end
-
-               -- continue y until maxy is exceeded
-               y = y + 1
-               if y ~= yrange then
-                       -- set i to index(minx, miny + y, minz + z) - 1
-                       i = i + yreqstride
-                       nextaction = i + xrange
-                       return i
-               end
-
-               -- continue z until maxz is exceeded
-               z = z + 1
-               if z == zrange then
-                       -- cuboid finished, return nil
-                       return
-               end
-
-               -- set i to index(minx, miny, minz + z) - 1
-               i = i + multistride
-
-               y = 0
-               nextaction = i + xrange
-               return i
-       end
-end
-
-function VoxelArea:iterp(minp, maxp)
-       return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
-end
index 74db996a44490ad9b8d282c6c73b09482eddf727..876f84d537f443f50ef760a16e59de25bd84c5db 100644 (file)
@@ -757,7 +757,7 @@ int ModApiEnvMod::l_get_objects_in_area(lua_State *L)
 {
        GET_ENV_PTR;
        ScriptApiBase *script = getScriptApiBase(L);
-       
+
        v3f minp = read_v3f(L, 1) * BS;
        v3f maxp = read_v3f(L, 2) * BS;
        aabb3f box(minp, maxp);
@@ -900,13 +900,13 @@ int ModApiEnvMod::l_find_nodes_near(lua_State *L)
        if (Client *client = getClient(L))
                radius = client->CSMClampRadius(pos, radius);
 #endif
-       
+
        std::vector<u32> individual_count;
        individual_count.resize(filter.size());
-       
+
        lua_newtable(L);
        u32 i = 0;
-       
+
        for (int d = start_radius; d <= radius; d++) {
                const std::vector<v3s16> &list = FacePositionCache::getFacePositions(d);
                for (const v3s16 &posi : list) {
@@ -950,13 +950,13 @@ int ModApiEnvMod::l_find_nodes_near_under_air(lua_State *L)
        if (Client *client = getClient(L))
                radius = client->CSMClampRadius(pos, radius);
 #endif
-       
+
        std::vector<u32> individual_count;
        individual_count.resize(filter.size());
-       
+
        lua_newtable(L);
        u32 i = 0;
-       
+
        for (int d = start_radius; d <= radius; d++) {
                const std::vector<v3s16> &list = FacePositionCache::getFacePositions(d);
                for (const v3s16 &posi : list) {
@@ -1004,13 +1004,13 @@ int ModApiEnvMod::l_find_nodes_near_under_air_except(lua_State *L)
        if (Client *client = getClient(L))
                radius = client->CSMClampRadius(pos, radius);
 #endif
-       
+
        std::vector<u32> individual_count;
        individual_count.resize(filter.size());
-       
+
        lua_newtable(L);
        u32 i = 0;
-       
+
        for (int d = start_radius; d <= radius; d++) {
                const std::vector<v3s16> &list = FacePositionCache::getFacePositions(d);
                for (const v3s16 &posi : list) {
@@ -1457,7 +1457,7 @@ int ModApiEnvMod::l_find_path(lua_State *L)
                if (algorithm == "Dijkstra")
                        algo = PA_DIJKSTRA;
        }
-       
+
        std::vector<v3s16> path = get_path(&env->getMap(), env->getGameDef()->ndef(), pos1, pos2,
                searchdistance, max_jump, max_drop, algo);
 
@@ -1564,7 +1564,7 @@ int ModApiEnvMod::l_compare_block_status(lua_State *L)
        v3s16 nodepos = check_v3s16(L, 1);
        std::string condition_s = luaL_checkstring(L, 2);
        auto status = env->getBlockStatus(getNodeBlockPos(nodepos));
-       
+
        int condition_i = -1;
        if (!string_to_enum(es_BlockStatusType, condition_i, condition_s))
                return 0; // Unsupported
@@ -1664,6 +1664,7 @@ void ModApiEnvMod::InitializeClient(lua_State *L, int top)
        API_FCT(find_nodes_near_under_air_except);
        API_FCT(find_nodes_in_area);
        API_FCT(find_nodes_in_area_under_air);
+       API_FCT(get_voxel_manip);
        API_FCT(find_path);
        API_FCT(line_of_sight);
        API_FCT(raycast);
index 7e92eb576e4757d170eed35cc9a16d3113757d97..c30de9ee8245927ade7f4fe0e85145533a9da3fe 100644 (file)
@@ -41,6 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_camera.h"
 #include "lua_api/l_settings.h"
 #include "lua_api/l_http.h"
+#include "lua_api/l_vmanip.h"
 
 ClientScripting::ClientScripting(Client *client):
        ScriptApiBase(ScriptingType::Client)
@@ -88,6 +89,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
        LuaSettings::Register(L);
        ClientObjectRef::Register(L);
        LuaInventoryAction::Register(L);
+       LuaVoxelManip::Register(L);
 
        ModApiItemMod::Initialize(L, top);
        ModApiUtil::InitializeClient(L, top);