]> git.lizzy.rs Git - Crafter.git/blob - mods/door/init.lua
remove server debug
[Crafter.git] / mods / door / init.lua
1 local
2 minetest,math,pairs,table
3 =
4 minetest,math,pairs,table
5 --this is a really lazy way to make a door and I'll improve it in the future
6 for _,material in pairs({"wood","iron"}) do
7 --this is the function that makes the door open and close when rightclicked
8 local door_rightclick = function(pos)
9         local node = minetest.get_node(pos)
10         local name = node.name
11         local opened = minetest.get_item_group(name, "door_open")
12         local closed = minetest.get_item_group(name, "door_closed")
13         local closed = minetest.get_item_group(name, "door_closed")
14         local top = minetest.get_item_group(name, "top")
15         local bottom = minetest.get_item_group(name, "bottom")
16         local param2 = node.param2
17         local pos2 = table.copy(pos)
18         
19         --close the door
20         if opened > 0 then
21                 minetest.sound_play("door_close", {pos=pos,pitch=math.random(80,100)/100})
22                 if top > 0 then
23                         pos2.y = pos2.y - 1
24                         minetest.set_node(pos,{name="door:top_"..material.."_closed",param2=param2})
25                         minetest.set_node(pos2,{name="door:bottom_"..material.."_closed",param2=param2})
26                 elseif bottom > 0 then
27                         pos2.y = pos2.y + 1
28                         minetest.set_node(pos,{name="door:bottom_"..material.."_closed",param2=param2})
29                         minetest.set_node(pos2,{name="door:top_"..material.."_closed",param2=param2})
30                 end
31         --open the door
32         elseif closed > 0 then
33                 minetest.sound_play("door_open", {pos=pos,pitch=math.random(80,100)/100})
34                 if top > 0 then
35                         pos2.y = pos2.y - 1
36                         minetest.set_node(pos,{name="door:top_"..material.."_open",param2=param2})
37                         minetest.set_node(pos2,{name="door:bottom_"..material.."_open",param2=param2})
38                 elseif bottom > 0 then
39                         pos2.y = pos2.y + 1
40                         minetest.set_node(pos,{name="door:bottom_"..material.."_open",param2=param2})
41                         minetest.set_node(pos2,{name="door:top_"..material.."_open",param2=param2})
42                 end
43         end
44 end
45
46 --this is where the top and bottom of the door are created
47 for _,door in pairs({"top","bottom"}) do
48                 for _,state in pairs({"open","closed"}) do
49                         local door_node_box = {}
50                         if state == "closed" then
51                                 door_node_box = {-0.5, -0.5,  -0.5, 0.5,  0.5, -0.3}
52                         elseif state == "open" then
53                                 door_node_box = {5/16, -0.5,  -0.5, 0.5,  0.5, 0.5}
54                         end
55                 
56                         local tiles
57                         local groups
58                         local sounds
59                         local on_rightclick
60                         local redstone_deactivation
61                         local redstone_activation
62                         --make it so only the bottom activates
63                         
64                         if material == "wood" then
65                                 sounds = main.woodSound()
66                                 on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
67                                         door_rightclick(pos)
68                                 end
69                                 --bottom
70                                 if door == "bottom" then
71                                         tiles = {"wood.png"}
72                                         groups = {wood = 2, tree = 1, hard = 1, axe = 1, hand = 3, redstone_activation = 1,bottom = 1,door_open = ((state == "open" and 1) or 0),door_closed = ((state == "closed" and 1) or 0)}
73                                         --redstone input
74                                         if state == "open" then
75                                                 redstone_deactivation = function(pos)
76                                                         door_rightclick(pos)
77                                                 end
78                                         elseif state == "closed" then
79                                                 redstone_activation = function(pos)
80                                                         door_rightclick(pos)
81                                                 end
82                                         end
83                                 --top
84                                 else
85                                         if state == "closed" then
86                                                 tiles = {"wood.png","wood.png","wood.png","wood.png","wood_door_top.png","wood_door_top.png"}
87                                         elseif state == "open" then
88                                                 tiles = {"wood.png","wood.png","wood_door_top.png","wood_door_top.png","wood.png","wood.png"}
89                                         end
90                                         groups = {wood = 2, tree = 1, hard = 1, axe = 1, hand = 3,top = 1,door_open = ((state == "open" and 1) or 0),door_closed = ((state == "closed" and 1) or 0)}
91                                 end
92                         elseif material == "iron" then
93                                 sounds = main.stoneSound()
94                                 
95                                 
96                                 
97                                 if door == "bottom" then
98                                         tiles = {"iron_block.png"}
99                                         groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4, redstone_activation = 1,bottom = 1,door_open = ((state == "open" and 1) or 0),door_closed = ((state == "closed" and 1) or 0)}
100                                         --redstone input
101                                         if state == "open" then
102                                                 redstone_deactivation = function(pos)
103                                                         door_rightclick(pos)
104                                                 end
105                                         elseif state == "closed" then
106                                                 redstone_activation = function(pos)
107                                                         door_rightclick(pos)
108                                                 end
109                                         end
110                                 else
111                                         if state == "closed" then
112                                                 tiles = {"iron_block.png","iron_block.png","iron_block.png","iron_block.png","iron_door_top.png","iron_door_top.png"}
113                                         elseif state == "open" then
114                                                 tiles = {"iron_block.png","iron_block.png","iron_door_top.png","iron_door_top.png","iron_block.png","iron_block.png"}
115                                         end
116                                         
117                                         
118                                         groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,top = 1,door_open = ((state == "open" and 1) or 0),door_closed = ((state == "closed" and 1) or 0)}
119                                 end
120                         end
121                         minetest.register_node("door:"..door.."_"..material.."_"..state, {
122                                 description = material:gsub("^%l", string.upper).." Door",
123                                 tiles = tiles,
124                                 wield_image = "door_inv_"..material..".png",
125                                 inventory_image = "door_inv_"..material..".png",
126                                 drawtype = "nodebox",
127                                 paramtype = "light",
128                                 paramtype2 = "facedir",
129                                 groups = groups,
130                                 sounds = sounds,
131                                 drop = "door:bottom_"..material.."_closed",
132                                 node_placement_prediction = "",
133                                 node_box = {
134                                         type = "fixed",
135                                         fixed = {
136                                                         --left front bottom right back top
137                                                         door_node_box
138                                                 },
139                                         },
140                                 --redstone activation is in both because only the bottom is defined as an activator and it's easier to do it like this
141                                 redstone_activation = redstone_activation,
142                                 redstone_deactivation = redstone_deactivation,
143                                 on_rightclick = on_rightclick,
144                                 after_place_node = function(pos, placer, itemstack, pointed_thing)
145                                         local node = minetest.get_node(pos)
146                                         local param2 = node.param2
147                                         local pos2 = table.copy(pos)
148                                         pos2.y = pos2.y + 1
149                                         if minetest.get_node(pos2).name == "air" then
150                                                 minetest.set_node(pos2,{name="door:top_"..material.."_closed",param2=param2})
151                                         else
152                                                 minetest.remove_node(pos)
153                                                 itemstack:add_item(ItemStack("door:bottom_"..material.."_closed"))
154                                         end
155                                 end,    
156                                 after_dig_node = function(pos, oldnode, oldmetadata, digger)
157                                         if string.match(oldnode.name, ":bottom") then
158                                                 pos.y = pos.y + 1
159                                                 minetest.remove_node(pos)
160                                         else
161                                                 pos.y = pos.y - 1
162                                                 minetest.remove_node(pos)
163                                         end
164                                 end,
165                         })
166                 end
167         end
168 minetest.register_craft({
169         output = "door:bottom_"..material.."_closed",
170         recipe = {
171                 {"main:"..material,"main:"..material},
172                 {"main:"..material,"main:"..material},
173                 {"main:"..material,"main:"..material}
174         }
175 })
176 end
177
178
179