]> git.lizzy.rs Git - minetest.git/blobdiff - builtin/common/vector.lua
Add chat HUD flag (#13189)
[minetest.git] / builtin / common / vector.lua
index 02fc1bdee0cfbde2d4aaa94086db2d87047d061f..5ea5277089c0d1d9f34eeb17edc34de6d5f06c8f 100644 (file)
@@ -61,7 +61,7 @@ function vector.from_string(s, init)
        if not (x and y and z) then
                return nil
        end
-       return {x = x, y = y, z = z}, np
+       return fast_new(x, y, z), np
 end
 
 function vector.to_string(v)
@@ -81,7 +81,7 @@ metatable.__eq = vector.equals
 function vector.length(v)
        return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
 end
--- Note: we can not use __len because it is already used for primitive table length
+-- Note: we cannot use __len because it is already used for primitive table length
 
 function vector.normalize(v)
        local len = vector.length(v)
@@ -112,6 +112,14 @@ function vector.apply(v, func)
        )
 end
 
+function vector.combine(a, b, func)
+       return fast_new(
+               func(a.x, b.x),
+               func(a.y, b.y),
+               func(a.z, b.z)
+       )
+end
+
 function vector.distance(a, b)
        local x = a.x - b.x
        local y = a.y - b.y
@@ -348,7 +356,7 @@ function vector.dir_to_rotation(forward, up)
 
        -- Since vector.angle never returns a negative value or a value greater
        -- than math.pi, rot.z has to be inverted sometimes.
-       -- To determine wether this is the case, we rotate the up vector back around
+       -- To determine whether this is the case, we rotate the up vector back around
        -- the forward vector and check if it worked out.
        local back = vector.rotate_around_axis(up, forward, -rot.z)
 
@@ -360,3 +368,22 @@ function vector.dir_to_rotation(forward, up)
        end
        return rot
 end
+
+if rawget(_G, "core") and core.set_read_vector and core.set_push_vector then
+       local function read_vector(v)
+               return v.x, v.y, v.z
+       end
+       core.set_read_vector(read_vector)
+       core.set_read_vector = nil
+
+       if rawget(_G, "jit") then
+               -- This is necessary to prevent trace aborts.
+               local function push_vector(x, y, z)
+                       return (fast_new(x, y, z))
+               end
+               core.set_push_vector(push_vector)
+       else
+               core.set_push_vector(fast_new)
+       end
+       core.set_push_vector = nil
+end