summaryrefslogtreecommitdiff
path: root/mesecons_door/init.lua
blob: b2703c07b40e41dc6d1c2d247e17eb9af2c0ce79 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-- Mesecons Door

models = {
    {
        -- bottom part
        {-0.5, -0.5, -0.5, -0.4, 0.5, 0.5},
        -- A
        {-0.5, 0.5, -0.5, -0.4, 1.5, -0.3},
        -- B
        {-0.5, 0.5, 0.3, -0.4, 1.5, 0.5},
        -- C
        {-0.5, 1.3, -0.3, -0.4, 1.5, 0.3},
        -- D
        {-0.5, 0.5, -0.3, -0.4, 0.6, 0.3},
        -- E
        {-0.5, 0.6, -0.05, -0.4, 1.3, 0.05},
        -- F
        {-0.5, 0.9, -0.3, -0.4, 1, -0.05},
        -- G
        {-0.5, 0.9, 0.05, -0.4, 1, 0.3}
    },
    {
        {0.4, -0.5, -0.5, 0.5, 0.5, 0.5},
        {0.4, 0.5, -0.5, 0.5, 1.5, -0.3},
        {0.4, 0.5, 0.3, 0.5, 1.5, 0.5},
        {0.4, 1.3, -0.3, 0.5, 1.5, 0.3},
        {0.4, 0.5, -0.3, 0.5, 0.6, 0.3},
        {0.4, 0.6, -0.05, 0.5, 1.3, 0.05},
        {0.4, 0.9, -0.3, 0.5, 1, -0.05},
        {0.4, 0.9, 0.05, 0.5, 1, 0.3}
    },
    {
        {-0.5, -0.5, -0.5, 0.5, 0.5, -0.4},
        {-0.5, 0.5, -0.5, -0.3, 1.5, -0.4},
        {0.3, 0.5, -0.5, 0.5, 1.5, -0.4},
        {-0.3, 1.3, -0.5, 0.3, 1.5, -0.4},
        {-0.3, 0.5, -0.5, 0.3, 0.6, -0.4},
        {-0.05, 0.6, -0.5, 0.05, 1.3, -0.4},
        {-0.3, 0.9, -0.5, -0.05, 1, -0.4},
        {0.05, 0.9, -0.5, 0.3, 1, -0.4}
    },
    {
        {-0.5, -0.5, 0.4, 0.5, 0.5, 0.5},
        {-0.5, 0.5, 0.4, -0.3, 1.5, 0.5},
        {0.3, 0.5, 0.4, 0.5, 1.5, 0.5},
        {-0.3, 1.3, 0.4, 0.3, 1.5, 0.5},
        {-0.3, 0.5, 0.4, 0.3, 0.6, 0.5},
        {-0.05, 0.6, 0.4, 0.05, 1.3, 0.5},
        {-0.3, 0.9, 0.4, -0.05, 1, 0.5},
        {0.05, 0.9, 0.4, 0.3, 1, 0.5}
    }
}

selections = {
    {-0.5, -0.5, -0.5, -0.4, 1.5, 0.5},
    {0.5, -0.5, -0.5, 0.4, 1.5, 0.5},
    {-0.5, -0.5, -0.5, 0.5, 1.5, -0.4},
    {-0.5, -0.5, 0.4, 0.5, 1.5, 0.5}
}

transforms = {
    door_1_1 = "door_4_2",
    door_4_2 = "door_1_1",
    door_2_1 = "door_3_2",
    door_3_2 = "door_2_1",
    door_3_1 = "door_1_2",
    door_1_2 = "door_3_1",
    door_4_1 = "door_2_2",
    door_2_2 = "door_4_1"
}

mesecons_door_transform = function (pos, node)
    local x, y = node.name:find(":")
    local n = node.name:sub(x + 1)
    if transforms[n] ~= nil then
        minetest.env:add_node(pos, {name = "mesecons_door:"..transforms[n]})
    else
        print("not implemented")
    end
end

for i = 1, 4 do
    for j = 1, 2 do
        minetest.register_node("mesecons_door:door_"..i.."_"..j, {
            drawtype = "nodebox",
            tile_images = {"default_wood.png"},
            paramtype = "light",
            is_ground_content = true,
            groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
            drop = "mesecons_door:door",
            node_box = {
                type = "fixed",
                fixed = models[i]
            },
            selection_box = {
                type = "fixed",
                fixed = {
                    selections[i]
                }
            },
            on_punch = mesecons_door_transform
        })
    end
end

minetest.register_node("mesecons_door:door", {
    description = "Wooden door",
    node_placement_prediction = "",
	inventory_image     = 'door_wood.png',
	wield_image         = 'door_wood.png',
    after_place_node = function(node_pos, placer)
        local best_distance = 1e50
        local best_number = 1
        local pos = placer:getpos()
        for i = 1, 4 do
            local box = minetest.registered_nodes["mesecons_door:door_"..i.."_1"].selection_box.fixed[1]
            box = {box[1] + node_pos.x, box[2] + node_pos.y, box[3] + node_pos.z, box[4] + node_pos.x, box[5] + node_pos.y, box[6] + node_pos.z}
            local center = {x = (box[1] + box[4]) / 2, y = (box[2] + box[5]) / 2, z = (box[3] + box[6]) / 2}
            local dist = math.pow(math.pow(center.x - pos.x, 2) + math.pow(center.y - pos.y, 2) + math.pow(center.z - pos.z, 2), 0.5)
            if dist < best_distance then
                best_distance = dist
                best_number = i
            end
        end
        minetest.env:add_node(node_pos, {name = "mesecons_door:door_"..best_number.."_1"})
    end
})

minetest.register_on_placenode(function(pos, newnode, placer)
    local b_pos = {x = pos.x, y = pos.y - 1, z = pos.z}
    local node = minetest.env:get_node(b_pos)
    if node.name:find("mesecons_door:door") ~= nil then
        minetest.env:remove_node(pos)
        minetest.env:add_item(pos, newnode.name)
    end
end)

minetest.register_craft({
	output = 'mesecons_door:door',
	recipe = {
		{ 'default:wood', 'default:wood', '' },
		{ 'default:wood', 'default:wood', '' },
		{ 'default:wood', 'default:wood', '' },
	},
})

--MESECONS
mesecon:register_on_signal_change(function(pos, node)
	if string.find(node.name, "mesecons_door:door_") then
		mesecons_door_transform(pos, node)
	end
end)