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