]> git.lizzy.rs Git - Crafter.git/blob - mods/redstone/piston.lua
Fix piston glitch
[Crafter.git] / mods / redstone / piston.lua
1 --only allow pistons to move "main" nodes
2 local function piston_move(pos,dir)
3         local move_index = {}
4         local space = false
5         for i = 1,30 do
6                 local index_pos = vector.multiply(dir,i)
7                 local index_pos = vector.add(index_pos,pos)
8                 local node = minetest.get_node(index_pos)
9                 local def = minetest.registered_nodes[node.name]
10                 local name = node.name
11                 if def.drawtype == "normal" and string.match(name, "main:") then
12                         local index = {}
13                         index.pos = index_pos
14                         index.name = name
15                         table.insert(move_index,index)
16                 elseif name == "air" then
17                         space = true
18                         break
19                 else
20                         space = false
21                         break
22                 end             
23         end
24         --check if room to move and objects in log
25         if space == true and next(move_index) then
26                 for i = 1,table.getn(move_index) do
27                         move_index[i].pos = vector.add(move_index[i].pos,dir)
28                         minetest.set_node(move_index[i].pos,{name=move_index[i].name})
29                 end
30         end
31         return(space)
32 end
33
34 minetest.register_node("redstone:piston_off", {
35     description = "Redstone Piston",
36     tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","wood.png","stone.png"},
37     paramtype2 = "facedir",
38     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
39     sounds = main.stoneSound(),
40     drop = "redstone:piston_off",
41     paramtype = "light",
42     sunlight_propagates = true,
43     redstone_activation = function(pos)
44                 --this is where the piston activates
45                 local facedir = minetest.get_node(pos).param2
46                 local dir = minetest.facedir_to_dir(facedir)
47                 local piston_location = vector.add(pos,dir)
48                 local worked = piston_move(pos,dir)
49                 if worked == true then
50                         --push player
51                         for _,object in ipairs(minetest.get_objects_inside_radius(piston_location, 1.5)) do
52                                 if object:is_player() and object:get_hp() > 0 then
53                                         --print("adding player velocity")
54                                         object:add_player_velocity(vector.multiply(dir,15))
55                                 end
56                         end
57                         minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
58                         minetest.set_node(piston_location,{name="redstone:actuator",param2=facedir})
59                         minetest.set_node(pos,{name="redstone:piston_on",param2=facedir})
60                 end
61     end,
62     --reverse the direction to face the player
63     after_place_node = function(pos, placer, itemstack, pointed_thing)
64                 local look = placer:get_look_dir()
65                 look = vector.multiply(look,-1)
66                 dir = minetest.dir_to_facedir(look, true)
67                 minetest.set_node(pos,{name="redstone:piston_off",param2=dir})
68                 redstone.collect_info(pos)
69     end,
70 })
71 minetest.register_node("redstone:piston_on", {
72     description = "Redstone Piston",
73     tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","stone.png","stone.png"},
74     drawtype = "nodebox",
75     paramtype = "light",
76     paramtype2 = "facedir",
77     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
78     sounds = main.stoneSound(),
79     drop = "redstone:piston_off",
80     node_box = {
81                 type = "fixed",
82                 fixed = {
83                                 --left front bottom right back top
84                                 {-0.5, -0.5,  -0.5, 0.5,  0.5, 3/16},
85                         },
86                 },
87     redstone_deactivation = function(pos)
88                 --this is where the piston deactivates
89                 local facedir = minetest.get_node(pos).param2
90                 local dir = minetest.facedir_to_dir(facedir)
91                 local piston_location = vector.add(pos,dir)
92                 minetest.remove_node(piston_location)
93                 minetest.set_node(pos,{name="redstone:piston_off",param2=facedir})
94                 piston_location.y = piston_location.y + 1
95                 minetest.punch_node(piston_location)
96                 minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
97     end,
98     after_dig_node = function(pos, oldnode, oldmetadata, digger)
99                 local facedir = oldnode.param2
100                 local dir = minetest.facedir_to_dir(facedir)
101                 local piston_location = vector.add(pos,dir)
102                 minetest.remove_node(piston_location)
103     end,
104 })
105
106 minetest.register_node("redstone:actuator", {
107     description = "Redstone Piston",
108     tiles = {"wood.png"},
109     drawtype = "nodebox",
110     paramtype = "light",
111     paramtype2 = "facedir",
112     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1},
113     sounds = main.stoneSound(),
114     drop = "redstone:piston_off",
115     node_box = {
116                 type = "fixed",
117                 fixed = {
118                                 --left front bottom right back top
119                                 {-0.5, -0.5,  0.2, 0.5,  0.5, 0.5}, --mover
120                                 {-0.15, -0.15,  -0.9, 0.15,  0.15, 0.5}, --actuator
121                         },
122                 },
123         after_dig_node = function(pos, oldnode, oldmetadata, digger)
124                 local facedir = oldnode.param2
125                 local dir = minetest.facedir_to_dir(facedir)
126                 dir = vector.multiply(dir,-1)
127                 local piston_location = vector.add(pos,dir)
128                 minetest.remove_node(piston_location)
129     end,
130 })
131
132