Add many more walls
[MineClone/MineClone2.git] / mods / ITEMS / mcl_walls / init.lua
blobb8e3756c97e4f4149db4ca865969cb190b7bd368
1 mcl_walls = {}
3 local function rshift(x, by)
4 return math.floor(x / 2 ^ by)
5 end
7 local directions = {
8 {x = 1, y = 0, z = 0},
9 {x = 0, y = 0, z = 1},
10 {x = -1, y = 0, z = 0},
11 {x = 0, y = 0, z = -1},
12 {x = 0, y = -1, z = 0},
15 local function update_wall(pos)
16 local thisnode = minetest.get_node(pos)
18 if minetest.get_item_group(thisnode.name, "wall") == 0 then
19 return
20 end
22 -- Get the node's base name, including the underscore since we will need it
23 local colonpos = thisnode.name:find(":")
24 local underscorepos
25 local itemname, basename, modname
26 if colonpos ~= nil then
27 itemname = thisnode.name:sub(colonpos+1)
28 modname = thisnode.name:sub(1, colonpos-1)
29 end
30 underscorepos = itemname:find("_")
31 if underscorepos == nil then -- New wall
32 basename = thisnode.name .. "_"
33 else -- Already placed wall
34 basename = modname .. ":" .. itemname:sub(1, underscorepos)
35 end
37 local sum = 0
39 -- Neighbouring walkable nodes
40 for i = 1, 4 do
41 local dir = directions[i]
42 local node = minetest.get_node({x = pos.x + dir.x, y = pos.y + dir.y, z = pos.z + dir.z})
43 local def = minetest.registered_nodes[node.name]
44 if def and def.walkable then
45 sum = sum + 2 ^ (i - 1)
46 end
47 end
49 -- Torches or walkable nodes above the wall
50 local upnode = minetest.get_node({x = pos.x, y = pos.y+1, z = pos.z})
51 if sum == 5 or sum == 10 then
52 local def = minetest.registered_nodes[upnode.name]
53 if (def and def.walkable) or upnode.name == "mcl_torches:torch" then
54 sum = sum + 11
55 end
56 end
58 --[[if sum == 0 then
59 sum = 15
60 end]]
62 minetest.add_node(pos, {name = basename..sum})
63 end
65 local function update_wall_global(pos)
66 for i = 1,5 do
67 local dir = directions[i]
68 update_wall({x = pos.x + dir.x, y = pos.y + dir.y, z = pos.z + dir.z})
69 end
70 end
72 local half_blocks = {
73 {4/16, -0.5, -3/16, 0.5, 5/16, 3/16},
74 {-3/16, -0.5, 4/16, 3/16, 5/16, 0.5},
75 {-0.5, -0.5, -3/16, -4/16, 5/16, 3/16},
76 {-3/16, -0.5, -0.5, 3/16, 5/16, -4/16}
79 local pillar = {-4/16, -0.5, -4/16, 4/16, 0.5, 4/16}
81 local full_blocks = {
82 {-0.5, -0.5, -3/16, 0.5, 5/16, 3/16},
83 {-3/16, -0.5, -0.5, 3/16, 5/16, 0.5}
86 --[[ Adds a new wall type.
87 * nodename: Itemstring of base node to add. Must not contain an underscore
88 * description: Item description (tooltip), visible to user
89 * craft_material: Material for the default crafting recipe (optional)
90 * tiles: Wall textures table
91 * inventory_image: Inventory image (optional)
92 * groups: Base group memberships (optional, default is {pickaxey=1})
93 * sounds: Sound table (optional, default is stone)
95 function mcl_walls.register_wall(nodename, description, craft_material, tiles, inventory_image, groups, sounds)
97 local base_groups = groups
98 if not base_groups then
99 base_groups = {pickaxey=1}
101 base_groups.wall = 1
103 local internal_groups = table.copy(base_groups)
104 internal_groups.not_in_creative_inventory = 1
106 local main_node_groups = table.copy(base_groups)
107 main_node_groups.deco_block = 1
109 -- TODO: Stop hardcoding blast resistance
111 if not sounds then
112 sounds = mcl_sounds.node_sound_stone_defaults()
115 for i = 0, 15 do
116 local need = {}
117 local need_pillar = false
118 for j = 1, 4 do
119 if rshift(i, j - 1) % 2 == 1 then
120 need[j] = true
124 local take = {}
125 if need[1] == true and need[3] == true then
126 need[1] = nil
127 need[3] = nil
128 table.insert(take, full_blocks[1])
130 if need[2] == true and need[4] == true then
131 need[2] = nil
132 need[4] = nil
133 table.insert(take, full_blocks[2])
135 for k in pairs(need) do
136 table.insert(take, half_blocks[k])
137 need_pillar = true
139 if i == 15 or i == 0 then need_pillar = true end
140 if need_pillar then table.insert(take, pillar) end
142 minetest.register_node(nodename.."_"..i, {
143 collision_box = {
144 type = 'fixed',
145 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
147 drawtype = "nodebox",
148 is_ground_content = false,
149 tiles = tiles,
150 paramtype = "light",
151 sunlight_propagates = true,
152 groups = internal_groups,
153 drop = nodename,
154 node_box = {
155 type = "fixed",
156 fixed = take
158 sounds = sounds,
159 _mcl_blast_resistance = 30,
160 _mcl_hardness = 2,
163 -- Add entry alias for the Help
164 if minetest.get_modpath("doc") then
165 doc.add_entry_alias("nodes", nodename, "nodes", nodename.."_"..i)
169 minetest.register_node(nodename.."_16", {
170 drawtype = "nodebox",
171 collision_box = {
172 type = 'fixed',
173 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
175 tiles = tiles,
176 paramtype = "light",
177 sunlight_propagates = true,
178 is_ground_content = false,
179 groups = internal_groups,
180 drop = nodename,
181 node_box = {
182 type = "fixed",
183 fixed = {pillar, full_blocks[1]}
185 sounds = sounds,
186 _mcl_blast_resistance = 30,
187 _mcl_hardness = 2,
189 -- Add entry alias for the Help
190 if minetest.get_modpath("doc") then
191 doc.add_entry_alias("nodes", nodename, "nodes", nodename.."_16")
194 minetest.register_node(nodename.."_21", {
195 drawtype = "nodebox",
196 collision_box = {
197 type = 'fixed',
198 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
200 tiles = tiles,
201 paramtype = "light",
202 sunlight_propagates = true,
203 is_ground_content = false,
204 groups = internal_groups,
205 drop = nodename,
206 node_box = {
207 type = "fixed",
208 fixed = {pillar, full_blocks[2]}
210 sounds = sounds,
211 _mcl_blast_resistance = 30,
212 _mcl_hardness = 2,
214 -- Add entry alias for the Help
215 if minetest.get_modpath("doc") then
216 doc.add_entry_alias("nodes", nodename, "nodes", nodename.."_21")
219 -- Inventory item
220 minetest.register_node(nodename, {
221 description = description,
222 _doc_items_longdesc = "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.",
223 paramtype = "light",
224 sunlight_propagates = true,
225 is_ground_content = false,
226 groups = main_node_groups,
227 tiles = tiles,
228 inventory_image = inventory_image,
229 stack_max = 64,
230 drawtype = "nodebox",
231 node_box = {
232 type = "fixed",
233 fixed = pillar
235 collision_box = {
236 type = 'fixed',
237 fixed = {-4/16, -0.5, -4/16, 4/16, 1, 4/16}
239 collisionbox = {-0.2, 0, -0.2, 0.2, 1.4, 0.2},
240 on_construct = update_wall,
241 sounds = sounds,
242 _mcl_blast_resistance = 30,
243 _mcl_hardness = 2,
245 if craft_material then
246 minetest.register_craft({
247 output = nodename .. " 6",
248 recipe = {
249 {craft_material, craft_material, craft_material},
250 {craft_material, craft_material, craft_material},
256 dofile(minetest.get_modpath("mcl_walls") .. "/register.lua")
258 minetest.register_on_placenode(update_wall_global)
259 minetest.register_on_dignode(update_wall_global)