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