Creative: Make handle_node_drops override work for non-player diggers
[oweals/minetest_game.git] / mods / xpanes / init.lua
1
2 local function is_pane(pos)
3         return minetest.get_item_group(minetest.get_node(pos).name, "pane") > 0
4 end
5
6 local function connects_dir(pos, name, dir)
7         local aside = vector.add(pos, minetest.facedir_to_dir(dir))
8         if is_pane(aside) then
9                 return true
10         end
11
12         local connects_to = minetest.registered_nodes[name].connects_to
13         if not connects_to then
14                 return false
15         end
16         local list = minetest.find_nodes_in_area(aside, aside, connects_to)
17
18         if #list > 0 then
19                 return true
20         end
21
22         return false
23 end
24
25 local function swap(pos, node, name, param2)
26         if node.name == name and node.param2 == param2 then
27                 return
28         end
29
30         minetest.set_node(pos, {name = name, param2 = param2})
31 end
32
33 local function update_pane(pos)
34         if not is_pane(pos) then
35                 return
36         end
37         local node = minetest.get_node(pos)
38         local name = node.name
39         if name:sub(-5) == "_flat" then
40                 name = name:sub(1, -6)
41         end
42
43         local any = node.param2
44         local c = {}
45         local count = 0
46         for dir = 0, 3 do
47                 c[dir] = connects_dir(pos, name, dir)
48                 if c[dir] then
49                         any = dir
50                         count = count + 1
51                 end
52         end
53
54         if count == 0 then
55                 swap(pos, node, name .. "_flat", any)
56         elseif count == 1 then
57                 swap(pos, node, name .. "_flat", (any + 1) % 4)
58         elseif count == 2 then
59                 if (c[0] and c[2]) or (c[1] and c[3]) then
60                         swap(pos, node, name .. "_flat", (any + 1) % 4)
61                 else
62                         swap(pos, node, name, 0)
63                 end
64         else
65                 swap(pos, node, name, 0)
66         end
67 end
68
69 minetest.register_on_placenode(function(pos, node)
70         if minetest.get_item_group(node, "pane") then
71                 update_pane(pos)
72         end
73         for i = 0, 3 do
74                 local dir = minetest.facedir_to_dir(i)
75                 update_pane(vector.add(pos, dir))
76         end
77 end)
78
79 minetest.register_on_dignode(function(pos)
80         for i = 0, 3 do
81                 local dir = minetest.facedir_to_dir(i)
82                 update_pane(vector.add(pos, dir))
83         end
84 end)
85
86 xpanes = {}
87 function xpanes.register_pane(name, def)
88         for i = 1, 15 do
89                 minetest.register_alias("xpanes:" .. name .. "_" .. i, "xpanes:" .. name .. "_flat")
90         end
91
92         local flatgroups = table.copy(def.groups)
93         flatgroups.pane = 1
94         minetest.register_node(":xpanes:" .. name .. "_flat", {
95                 description = def.description,
96                 drawtype = "nodebox",
97                 paramtype = "light",
98                 is_ground_content = false,
99                 sunlight_propagates = true,
100                 inventory_image = def.inventory_image,
101                 wield_image = def.wield_image,
102                 paramtype2 = "facedir",
103                 tiles = {def.textures[3], def.textures[3], def.textures[1]},
104                 groups = flatgroups,
105                 drop = "xpanes:" .. name .. "_flat",
106                 sounds = def.sounds,
107                 node_box = {
108                         type = "fixed",
109                         fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
110                 },
111                 selection_box = {
112                         type = "fixed",
113                         fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
114                 },
115                 connect_sides = { "left", "right" },
116         })
117
118         local groups = table.copy(def.groups)
119         groups.pane = 1
120         groups.not_in_creative_inventory = 1
121         minetest.register_node(":xpanes:" .. name, {
122                 drawtype = "nodebox",
123                 paramtype = "light",
124                 is_ground_content = false,
125                 sunlight_propagates = true,
126                 description = def.description,
127                 tiles = {def.textures[3], def.textures[3], def.textures[1]},
128                 groups = groups,
129                 drop = "xpanes:" .. name .. "_flat",
130                 sounds = def.sounds,
131                 node_box = {
132                         type = "connected",
133                         fixed = {{-1/32, -1/2, -1/32, 1/32, 1/2, 1/32}},
134                         connect_front = {{-1/32, -1/2, -1/2, 1/32, 1/2, -1/32}},
135                         connect_left = {{-1/2, -1/2, -1/32, -1/32, 1/2, 1/32}},
136                         connect_back = {{-1/32, -1/2, 1/32, 1/32, 1/2, 1/2}},
137                         connect_right = {{1/32, -1/2, -1/32, 1/2, 1/2, 1/32}},
138                 },
139                 connects_to = {"group:pane", "group:stone", "group:glass", "group:wood", "group:tree"},
140         })
141
142         minetest.register_craft({
143                 output = "xpanes:" .. name .. "_flat 16",
144                 recipe = def.recipe
145         })
146 end
147
148 xpanes.register_pane("pane", {
149         description = "Glass Pane",
150         textures = {"default_glass.png","xpanes_pane_half.png","xpanes_white.png"},
151         inventory_image = "default_glass.png",
152         wield_image = "default_glass.png",
153         sounds = default.node_sound_glass_defaults(),
154         groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3},
155         recipe = {
156                 {"default:glass", "default:glass", "default:glass"},
157                 {"default:glass", "default:glass", "default:glass"}
158         }
159 })
160
161 xpanes.register_pane("bar", {
162         description = "Iron bar",
163         textures = {"xpanes_bar.png","xpanes_bar.png","xpanes_bar_top.png"},
164         inventory_image = "xpanes_bar.png",
165         wield_image = "xpanes_bar.png",
166         groups = {cracky=2},
167         sounds = default.node_sound_metal_defaults(),
168         recipe = {
169                 {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
170                 {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
171         }
172 })
173
174 minetest.register_lbm({
175         name = "xpanes:gen2",
176         nodenames = {"group:pane"},
177         action = function(pos, node)
178                 update_pane(pos)
179                 for i = 0, 3 do
180                         local dir = minetest.facedir_to_dir(i)
181                         update_pane(vector.add(pos, dir))
182                 end
183         end
184 })