summaryrefslogtreecommitdiff
path: root/utils.lua
blob: 52429495258acafa8fd771a526a7c02995d6e157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--[[
Eleficery  --- Elemental trinkets for minetest game
Copyright © 2026 bdunahu <bdunahu@operationnull.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
--]]

eleficery.is_water = function(pos)
    return core.get_node(pos).name == "default:water_source"
end

eleficery.snap_to_cardinal = function(look_dir)
    local d = { x = look_dir.x, y = 0, z = look_dir.z }

    if vector.length(d) == 0 then return {x=0, y=0, z=1} end

    if math.abs(d.x) > math.abs(d.z) then
        d.z = 0
        d.x = d.x > 0 and 1 or -1
    else
        d.x = 0
        d.z = d.z > 0 and 1 or -1
    end

    return d
end

eleficery.get_held_dir_to_player = function(player)
    -- 'to_player' because this gives a position relative
    -- to the player's perspective
    local ctrl = player:get_player_control()

    local zero = {x=0, y=0, z=0}
    local look_dir = player:get_look_dir(placer)

    local forward = eleficery.snap_to_cardinal(look_dir)
    local back = vector.multiply(forward, -1)
    local up = {x=0, y=1, z=0}
    local right = vector.cross(up, forward)
    local left = vector.multiply(right, -1)

    if ctrl.up then
        return forward
    elseif ctrl.down then
        return back
    elseif ctrl.left then
        return left
    elseif ctrl.right then
        return right
    end
end