Update helptext of obsidian
[MineClone/MineClone2.git] / mods / ITEMS / mcl_walls / init.lua
blob5704309db97c55aeef6d7f4b207b43646d593bcb
1 local S = minetest.get_translator("mcl_walls")
3 mcl_walls = {}
5 local function rshift(x, by)
6 return math.floor(x / 2 ^ by)
7 end
9 local directions = {
10 {x = 1, y = 0, z = 0},
11 {x = 0, y = 0, z = 1},
12 {x = -1, y = 0, z = 0},
13 {x = 0, y = 0, z = -1},
14 {x = 0, y = -1, z = 0},
17 local function connectable(itemstring)
18 return (minetest.get_item_group(itemstring, "wall") == 1) or (minetest.get_item_group(itemstring, "solid") == 1)
19 end
21 local function update_wall(pos)
22 local thisnode = minetest.get_node(pos)
24 if minetest.get_item_group(thisnode.name, "wall") == 0 then
25 return
26 end
28 -- Get the node's base name, including the underscore since we will need it
29 local colonpos = thisnode.name:find(":")
30 local underscorepos
31 local itemname, basename, modname
32 if colonpos ~= nil then
33 itemname = thisnode.name:sub(colonpos+1)
34 modname = thisnode.name:sub(1, colonpos-1)
35 end
36 underscorepos = itemname:find("_")
37 if underscorepos == nil then -- New wall
38 basename = thisnode.name .. "_"
39 else -- Already placed wall
40 basename = modname .. ":" .. itemname:sub(1, underscorepos)
41 end
43 local sum = 0
45 -- Neighbouring walkable nodes
46 for i = 1, 4 do
47 local dir = directions[i]
48 local node = minetest.get_node({x = pos.x + dir.x, y = pos.y + dir.y, z = pos.z + dir.z})
49 if connectable(node.name) then
50 sum = sum + 2 ^ (i - 1)
51 end
52 end
54 -- Torches or walkable nodes above the wall
55 local upnode = minetest.get_node({x = pos.x, y = pos.y+1, z = pos.z})
56 if sum == 5 or sum == 10 then
57 if (connectable(upnode.name)) or (minetest.get_item_group(upnode.name, "torch") == 1) then
58 sum = sum + 11
59 end
60 end
62 --[[if sum == 0 then
63 sum = 15
64 end]]
66 minetest.add_node(pos, {name = basename..sum})
67 end
69 local function update_wall_global(pos)
70 for i = 1,5 do
71 local dir = directions[i]
72 update_wall({x = pos.x + dir.x, y = pos.y + dir.y, z = pos.z + dir.z})
73 end
74 end
76 local half_blocks = {
77 {4/16, -0.5, -3/16, 0.5, 5/16, 3/16},
78 {-3/16, -0.5, 4/16, 3/16, 5/16, 0.5},
79 {-0.5, -0.5, -3/16, -4/16, 5/16, 3/16},
80 {-3/16, -0.5, -0.5, 3/16, 5/16, -4/16}
83 local pillar = {-4/16, -0.5, -4/16, 4/16, 0.5, 4/16}
85 local full_blocks = {
86 {-0.5, -0.5, -3/16, 0.5, 5/16, 3/16},
87 {-3/16, -0.5, -0.5, 3/16, 5/16, 0.5}
90 --[[ Adds a new wall type.
91 * nodename: Itemstring of base node to add. Must not contain an underscore
92 * description: Item description (tooltip), visible to user
93 * source: Source block to craft this thing, for graphics, tiles and crafting (optional)
94 * tiles: Wall textures table
95 * inventory_image: Inventory image (optional)
96 * groups: Base group memberships (optional, default is {pickaxey=1})
97 * sounds: Sound table (optional, default is stone)
99 function mcl_walls.register_wall(nodename, description, source, tiles, inventory_image, groups, sounds)
101 local base_groups = groups
102 if not base_groups then
103 base_groups = {pickaxey=1}
105 base_groups.wall = 1
107 local internal_groups = table.copy(base_groups)
108 internal_groups.not_in_creative_inventory = 1
110 local main_node_groups = table.copy(base_groups)
111 main_node_groups.deco_block = 1
113 -- TODO: Stop hardcoding blast resistance
115 if not sounds then
116 sounds = mcl_sounds.node_sound_stone_defaults()
119 if (not tiles) and source then
120 if minetest.registered_nodes[source] then
121 tiles = minetest.registered_nodes[source].tiles
125 for i = 0, 15 do
126 local need = {}
127 local need_pillar = false
128 for j = 1, 4 do
129 if rshift(i, j - 1) % 2 == 1 then
130 need[j] = true
134 local take = {}
135 if need[1] == true and need[3] == true then
136 need[1] = nil
137 need[3] = nil
138 table.insert(take, full_blocks[1])
140 if need[2] == true and need[4] == true then
141 need[2] = nil
142 need[4] = nil
143 table.insert(take, full_blocks[2])
145 for k in pairs(need) do
146 table.insert(take, half_blocks[k])
147 need_pillar = true
149 if i == 15 or i == 0 then need_pillar = true end
150 if need_pillar then table.insert(take, pillar) end
152 minetest.register_node(nodename.."_"..i, {
153 collision_box = {
154 type = 'fixed',
155 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
157 drawtype = "nodebox",
158 is_ground_content = false,
159 tiles = tiles,
160 paramtype = "light",
161 sunlight_propagates = true,
162 groups = internal_groups,
163 drop = nodename,
164 node_box = {
165 type = "fixed",
166 fixed = take
168 sounds = sounds,
169 _mcl_blast_resistance = 6,
170 _mcl_hardness = 2,
173 -- Add entry alias for the Help
174 if minetest.get_modpath("doc") then
175 doc.add_entry_alias("nodes", nodename, "nodes", nodename.."_"..i)
179 minetest.register_node(nodename.."_16", {
180 drawtype = "nodebox",
181 collision_box = {
182 type = 'fixed',
183 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
185 tiles = tiles,
186 paramtype = "light",
187 sunlight_propagates = true,
188 is_ground_content = false,
189 groups = internal_groups,
190 drop = nodename,
191 node_box = {
192 type = "fixed",
193 fixed = {pillar, full_blocks[1]}
195 sounds = sounds,
196 _mcl_blast_resistance = 6,
197 _mcl_hardness = 2,
199 -- Add entry alias for the Help
200 if minetest.get_modpath("doc") then
201 doc.add_entry_alias("nodes", nodename, "nodes", nodename.."_16")
204 minetest.register_node(nodename.."_21", {
205 drawtype = "nodebox",
206 collision_box = {
207 type = 'fixed',
208 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
210 tiles = tiles,
211 paramtype = "light",
212 sunlight_propagates = true,
213 is_ground_content = false,
214 groups = internal_groups,
215 drop = nodename,
216 node_box = {
217 type = "fixed",
218 fixed = {pillar, full_blocks[2]}
220 sounds = sounds,
221 _mcl_blast_resistance = 6,
222 _mcl_hardness = 2,
224 -- Add entry alias for the Help
225 if minetest.get_modpath("doc") then
226 doc.add_entry_alias("nodes", nodename, "nodes", nodename.."_21")
229 -- Inventory item
230 minetest.register_node(nodename, {
231 description = description,
232 _doc_items_longdesc = S("A piece of wall. It cannot be jumped over with a simple jump. When multiple of these are placed to next to each other, they will automatically build a nice wall structure."),
233 paramtype = "light",
234 sunlight_propagates = true,
235 is_ground_content = false,
236 groups = main_node_groups,
237 tiles = tiles,
238 inventory_image = inventory_image,
239 stack_max = 64,
240 drawtype = "nodebox",
241 node_box = {
242 type = "fixed",
243 fixed = pillar
245 collision_box = {
246 type = 'fixed',
247 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
249 collisionbox = {-0.2, 0, -0.2, 0.2, 1.4, 0.2},
250 on_construct = update_wall,
251 sounds = sounds,
252 _mcl_blast_resistance = 6,
253 _mcl_hardness = 2,
255 if source then
256 minetest.register_craft({
257 output = nodename .. " 6",
258 recipe = {
259 {source, source, source},
260 {source, source, source},
266 dofile(minetest.get_modpath("mcl_walls") .. "/register.lua")
268 minetest.register_on_placenode(update_wall_global)
269 minetest.register_on_dignode(update_wall_global)