]> git.lizzy.rs Git - Crafter.git/blob - mods/stairs/init.lua
Simplify redstone check
[Crafter.git] / mods / stairs / init.lua
1 --stairs - shift click to place upside down
2 for name,def in pairs(minetest.registered_nodes) do
3         if def.drawtype == "normal" and string.match(name, "main:") then
4         
5                 --set up fence
6                 local def2 = table.copy(def)
7                 local newname = "stairs:"..string.gsub(name, "main:", "").."_stair"
8                 def2.mod_origin = "stairs"
9                 def2.name = newname
10                 def2.description = def.description.." Stair"
11                 def2.drop = newname
12                 def2.paramtype = "light"
13                 def2.drawtype = "nodebox"
14                 def2.paramtype2 = "facedir"
15                 def2.node_placement_prediction = ""
16                 def2.on_dig = nil
17                 def2.node_box = {
18                         type = "fixed",
19                         fixed = {
20                         {-8/16, -8/16, -0/16, 8/16, 8/16, 8/16},
21                         {-8/16, -8/16, -8/16, 8/16, 0/16, 8/16},
22                         }
23                 }
24                 --ability to place stairs upside down
25                 def2.on_place = function(itemstack, placer, pointed_thing)
26                         local sneak = placer:get_player_control().sneak
27                         if sneak then
28                                 local _,worked = minetest.item_place(ItemStack(newname.."_upsidedown"), placer, pointed_thing)
29                                 if worked then
30                                         itemstack:take_item()
31                                 end
32                         else
33                                 minetest.item_place(itemstack, placer, pointed_thing)
34                         end
35                         return(itemstack)
36                 end
37                 def2.groups["stairs"] = 1
38                 minetest.register_node(newname,def2)
39                 
40                 minetest.register_craft({
41                         output = newname.." 6",
42                         recipe = {
43                                 { "","",name },
44                                 { "",name, name},
45                                 { name, name,name},
46                         }
47                 })
48                 
49                 minetest.register_craft({
50                         output = newname.." 6",
51                         recipe = {
52                                 { name,"","" },
53                                 { name, name,""},
54                                 { name, name,name},
55                         }
56                 })
57         end
58 end
59 --upside down stairs
60 for name,def in pairs(minetest.registered_nodes) do
61         if def.drawtype == "normal" and string.match(name, "main:") then
62                 local def2 = table.copy(def)
63                 local newname = "stairs:"..string.gsub(name, "main:", "").."_stair_upsidedown"
64                 def2.mod_origin = "stairs"
65                 def2.name = newname
66                 def2.description = def.description.." Stair"
67                 def2.drop = string.gsub(newname, "_upsidedown", "")
68                 def2.paramtype = "light"
69                 def2.drawtype = "nodebox"
70                 def2.paramtype2 = "facedir"
71                 def2.on_dig = nil
72                 def2.node_box = {
73                         type = "fixed",
74                         fixed = {
75                         {-8/16, -8/16, -0/16, 8/16, 8/16, 8/16},
76                         {-8/16, -0/16, -8/16, 8/16, 8/16, 8/16},
77                         }
78                 }
79                 def2.groups["stairs"] = 1
80                 minetest.register_node(newname,def2)
81         end
82 end
83
84
85 ------------------------------------------------------- slabs
86
87 local place_slab_sound = function(pos,newnode)
88         local node = minetest.registered_nodes[newnode]
89         local sound = node.sounds
90         local placing = ""
91         if sound then
92                 placing = sound.placing
93         end
94         --only play the sound when is defined
95         if type(placing) == "table" then
96                 minetest.sound_play(placing.name, {
97                           pos = pos,
98                           gain = placing.gain,
99                           --pitch = math.random(60,100)/100
100                 })
101         end
102 end
103 --slabs - shift click to place upside down
104 for name,def in pairs(minetest.registered_nodes) do
105         if def.drawtype == "normal" and string.match(name, "main:") then
106         
107                 --set up fence
108                 local def2 = table.copy(def)
109                 local newname = "stairs:"..string.gsub(name, "main:", "").."_slab"
110                 def2.mod_origin = "stairs"
111                 def2.name = newname
112                 def2.description = def.description.." Slab"
113                 def2.drop = newname
114                 def2.paramtype = "light"
115                 def2.drawtype = "nodebox"
116                 def2.on_dig = nil
117                 def2.node_placement_prediction = ""
118                 def2.node_box = {
119                         type = "fixed",
120                         fixed = {
121                         {-8/16, -8/16, -8/16, 8/16, 0/16, 8/16},
122                         }
123                 }
124                 --we're passing in the local variables newname and name into this function
125                 --calculating wether to turn a half slab into a full block
126                 def2.on_place = function(itemstack, placer, pointed_thing)
127                         --get all the required variables
128                         local sneak = placer:get_player_control().sneak
129                         local ydiff = pointed_thing.above.y-pointed_thing.under.y
130                         local node_under = minetest.get_node(pointed_thing.under).name
131                         local rightsideup = (newname == node_under)
132                         local upsidedown = (newname.."_upsidedown" == node_under)
133                         
134                         local placement_worked = false
135                         --upsidedown slab placement
136                         if sneak == true then
137                                 local _,worked = minetest.item_place(ItemStack(newname.."_upsidedown"), placer, pointed_thing)
138                                 if worked then
139                                         itemstack:take_item()
140                                         placement_worked = true
141                                 end
142                         --normal placement - (back of slab) or normal node
143                         elseif (rightsideup and ydiff == -1) or (upsidedown and ydiff == 1) or (not rightsideup and not upsidedown) or ydiff == 0 then
144                                 local itemstack,worked = minetest.item_place(itemstack, placer, pointed_thing)
145                                 if worked then
146                                         placement_worked = true
147                                 end
148                         --normal slab to full slab
149                         elseif rightsideup and ydiff == 1 then
150                                 place_slab_sound(pointed_thing.under,newname)
151                                 minetest.set_node(pointed_thing.under, {name = name})
152                                 itemstack:take_item()
153                                 placement_worked = true
154                         --upsidedown slab to full slab
155                         elseif upsidedown and ydiff == -1 then
156                                 place_slab_sound(pointed_thing.under,newname)
157                                 minetest.set_node(pointed_thing.under, {name = name})
158                                 itemstack:take_item()
159                                 placement_worked = true
160                         end
161                         
162                         --try to do pointed_thing above
163                         if placement_worked == false then
164                                 local node_above = minetest.get_node(pointed_thing.above).name
165                                 local rightsideup = (newname == node_above)
166                                 local upsidedown = (newname.."_upsidedown" == node_above)
167                                 if rightsideup or upsidedown then
168                                         place_slab_sound(pointed_thing.above,newname)
169                                         minetest.set_node(pointed_thing.above, {name = name})
170                                         itemstack:take_item()
171                                 end
172                         end
173                         
174                         
175                         return(itemstack)
176                 end
177                 def2.groups["slabs"] = 1
178                 def2.groups[name]=1
179                 minetest.register_node(newname,def2)
180                 --equalize recipe 6 half slabs turn into 3 full blocks
181                 minetest.register_craft({
182                         output = newname.." 6",
183                         recipe = {
184                                 { name, name,name},
185                         }
186                 })
187                 minetest.register_craft({
188                         output = name,
189                         recipe = {
190                                 { newname},
191                                 { newname},
192                         }
193                 })
194                 
195         end
196 end
197 --upside down stairs
198 for name,def in pairs(minetest.registered_nodes) do
199         if def.drawtype == "normal" and string.match(name, "main:") then
200                 local def2 = table.copy(def)
201                 local newname = "stairs:"..string.gsub(name, "main:", "").."_slab_upsidedown"
202                 def2.mod_origin = "stairs"
203                 def2.name = newname
204                 def2.description = def.description.." Slab"
205                 def2.drop = string.gsub(newname, "_upsidedown", "")
206                 def2.paramtype = "light"
207                 def2.on_dig = nil
208                 def2.drawtype = "nodebox"
209                 def2.node_box = {
210                         type = "fixed",
211                         fixed = {
212                         {-8/16, -0/16, -8/16, 8/16, 8/16, 8/16},
213                         }
214                 }
215                 def2.groups["slabs"] = 1
216                 def2.groups[name]=1
217                 minetest.register_node(newname,def2)
218         end
219 end