1 local minetest,math,ipairs,vector,table = minetest,math,ipairs,vector,table
2 --exclude certain mods and nodes from being pushed and pulled to stop glitches
4 minetest.register_on_mods_loaded(function()
5 registered_nodes = minetest.registered_nodes
8 local excluded_mods = {utility=true,craftingtable=true,buildtest=true,sign=true,bed=true}
9 local excluded_nodes = {
10 ["redstone:piston_on"]=true,
11 ["redstone:sticky_piston_on"]=true,
12 ["redstone:actuator"]=true,
13 ["redstone:sticky_actuator"]=true,
14 ["redstone:inverter_on"]=true,
15 ["redstone:inverter_off"]=true,
16 ["redstone:torch_wall"]=true,
17 ["redstone:torch_floor"]=true,
18 ["redstone:lever_on"]=true,
19 ["redstone:lever_off"]=true,
20 ["redstone:button_on"]=true,
21 ["redstone:button_off"]=true,
24 excluded_nodes["redstone:dust_"..i] = true
27 excluded_nodes["redstone:repeater_on_"..i] = true
28 excluded_nodes["redstone:repeater_off_"..i] = true
31 excluded_nodes["redstone:comparator_"..i] = true
34 excluded_nodes["redstone:pressure_plate_"..i] = true
37 excluded_nodes["redstone:ore_"..i] = true
42 ███████╗██╗ ██╗███╗ ██╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗
43 ██╔════╝██║ ██║████╗ ██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║
44 █████╗ ██║ ██║██╔██╗ ██║██║ ██║ ██║██║ ██║██╔██╗ ██║
45 ██╔══╝ ██║ ██║██║╚██╗██║██║ ██║ ██║██║ ██║██║╚██╗██║
46 ██║ ╚██████╔╝██║ ╚████║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║
47 ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
50 --this is how the piston pushes nodes
59 local function push_nodes(pos,dir)
63 index_pos = vector.add(vector.multiply(dir,i),pos)
64 node = minetest.get_node(index_pos)
66 def = minetest.registered_nodes[node.name]
68 push = ((excluded_mods[def.mod_origin] ~= true) and (excluded_nodes[name] ~= true))
69 if push and name ~= "air" then
74 table.insert(move_index,index)
75 elseif name == "air" then
84 --check if room to move and objects in log
85 if space == true and next(move_index) then
86 if table.getn(move_index) == 1 and minetest.get_item_group(move_index[1].name, "falling_node") > 0 then
87 for i = 1,table.getn(move_index) do
88 move_index[i].pos = vector.add(move_index[i].pos,dir)
89 minetest.set_node(move_index[i].pos,{name="air"})
91 local obj = minetest.add_entity(vector.add(move_index[i].pos,dir), "__builtin:falling_node")
92 obj:get_luaentity():set_node({name=move_index[i].name})
93 obj:set_velocity(vector.multiply(dir,19))
96 for i = 1,table.getn(move_index) do
98 if move_index[i].pos then
99 move_index[i].pos = vector.add(move_index[i].pos,dir)
100 minetest.set_node(move_index[i].pos,move_index[i])
101 minetest.check_for_falling(move_index[i].pos)
110 --this is the logic of the piston
113 local piston_location
115 local function actuator_arm_function(pos)
116 --this is where the piston activates
117 facedir = minetest.get_node(pos).param2
118 dir = minetest.facedir_to_dir(facedir)
119 piston_location = vector.add(pos,dir)
120 worked = push_nodes(pos,dir)
121 local node = minetest.get_node(vector.add(pos,dir)).name
123 if worked == true then
125 if node == "air" then
126 for _,object in ipairs(minetest.get_objects_inside_radius(piston_location, 2)) do
128 if object:is_player() and object:get_hp() > 0 then
129 local pos2 = object:get_pos()
130 local compare = vector.subtract(pos2,piston_location)
131 local real_y = compare.y
132 compare = vector.abs(compare)
135 if compare.y <= 0.5 and compare.x < 0.8 and compare.z < 0.8 then
136 object:move_to(vector.add(dir,pos2))
137 object:add_player_velocity(vector.multiply(dir,20))
140 elseif dir.x ~=0 or dir.z ~= 0 then
141 if real_y <= 0.5 and real_y >= -1.6 and compare.x < 0.8 and compare.z < 0.8 then
142 object:move_to(vector.add(dir,pos2))
143 object:add_player_velocity(vector.multiply(dir,19))
146 elseif not object:is_player() and object:get_luaentity().name == "__builtin:falling_node" then
147 local pos2 = object:get_pos()
148 local compare = vector.subtract(pos2,piston_location)
149 local real_y = compare.y
150 compare = vector.abs(compare)
151 if compare.y <= 1.5 and compare.x <= 1.5 and compare.z <= 1.5 then
152 object:move_to(vector.add(dir,pos2))
153 object:add_velocity(vector.multiply(dir,20))
155 elseif not object:is_player() and object:get_luaentity().name == "__builtin:item" then
156 local pos2 = object:get_pos()
157 local compare = vector.subtract(pos2,piston_location)
158 local real_y = compare.y
159 compare = vector.abs(compare)
160 if compare.y <= 1 and compare.x <= 1 and compare.z <= 1 then
161 object:move_to(vector.add(dir,pos2))
162 object:add_velocity(vector.multiply(dir,20))
163 object:get_luaentity().poll_timer = 0
168 minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
169 minetest.set_node(piston_location,{name="redstone:actuator",param2=facedir})
170 minetest.swap_node(pos,{name="redstone:piston_on",param2=facedir})
172 redstone.inject(pos,{
173 name = "redstone:piston_on",
182 ██████╗ ███████╗███████╗
183 ██╔═══██╗██╔════╝██╔════╝
191 minetest.register_node("redstone:piston_off", {
192 description = "Piston",
193 tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","wood.png","stone.png"},
194 paramtype2 = "facedir",
195 groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
196 sounds = main.stoneSound(),
197 drop = "redstone:piston_off",
199 sunlight_propagates = true,
200 --reverse the direction to face the player
201 on_construct = function(pos)
202 redstone.inject(pos,{
203 name = "redstone:piston_off",
208 after_place_node = function(pos, placer, itemstack, pointed_thing)
209 local look = placer:get_look_dir()
210 look = vector.multiply(look,-1)
211 local dir = minetest.dir_to_facedir(look, true)
212 minetest.swap_node(pos,{name="redstone:piston_off",param2=dir})
214 after_destruct = function(pos, oldnode)
215 redstone.inject(pos,nil)
220 redstone.register_activator({
221 name = "redstone:piston_off",
222 activate = function(pos)
223 minetest.after(0,function()
224 actuator_arm_function(pos)
229 minetest.register_lbm({
230 name = "redstone:piston_off",
231 nodenames = {"redstone:piston_off"},
232 run_at_every_load = true,
233 action = function(pos)
234 redstone.inject(pos,{
235 name = "redstone:piston_off",
238 --redstone needs to warm up
239 minetest.after(0,function()
255 minetest.register_node("redstone:piston_on", {
256 description = "Piston",
257 tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","stone.png","stone.png"},
258 drawtype = "nodebox",
260 paramtype2 = "facedir",
261 groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
262 sounds = main.stoneSound(),
263 drop = "redstone:piston_off",
267 --left front bottom right back top
268 {-0.5, -0.5, -0.5, 0.5, 0.5, 3/16},
271 after_destruct = function(pos, oldnode)
272 local facedir = oldnode.param2
273 local dir = minetest.facedir_to_dir(facedir)
274 local piston_location = vector.add(pos,dir)
275 minetest.remove_node(piston_location)
276 redstone.inject(pos,nil)
280 minetest.register_lbm({
281 name = "redstone:piston_on",
282 nodenames = {"redstone:piston_on"},
283 run_at_every_load = true,
284 action = function(pos)
285 redstone.inject(pos,{
286 name = "redstone:piston_on",
289 --redstone needs to warm up
290 minetest.after(0,function()
296 redstone.register_activator({
297 name = "redstone:piston_on",
298 deactivate = function(pos)
299 --this is where the piston deactivates
300 local facedir = minetest.get_node(pos).param2
301 local dir = minetest.facedir_to_dir(facedir)
302 local piston_location = vector.add(pos,dir)
303 minetest.remove_node(piston_location)
304 minetest.swap_node(pos,{name="redstone:piston_off",param2=facedir})
305 piston_location.y = piston_location.y + 1
306 minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
307 redstone.inject(pos,{
308 name = "redstone:piston_off",
316 █████╗ ██████╗ ███╗ ███╗
317 ██╔══██╗██╔══██╗████╗ ████║
318 ███████║██████╔╝██╔████╔██║
319 ██╔══██║██╔══██╗██║╚██╔╝██║
320 ██║ ██║██║ ██║██║ ╚═╝ ██║
321 ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝
326 minetest.register_node("redstone:actuator", {
327 description = "Piston Actuator",
328 tiles = {"wood.png"},
329 drawtype = "nodebox",
331 paramtype2 = "facedir",
332 groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1},
333 sounds = main.stoneSound(),
334 drop = "redstone:piston_off",
338 --left front bottom right back top
339 {-0.5, -0.5, 0.2, 0.5, 0.5, 0.5}, --mover
340 {-0.15, -0.15, -0.9, 0.15, 0.15, 0.5}, --actuator
343 after_destruct = function(pos, oldnode)
344 local facedir = oldnode.param2
345 local dir = minetest.facedir_to_dir(facedir)
346 dir = vector.multiply(dir,-1)
347 local piston_location = vector.add(pos,dir)
348 minetest.remove_node(piston_location)
357 ███████╗████████╗██╗ ██████╗██╗ ██╗██╗ ██╗ ██████╗ ██╗███████╗████████╗ ██████╗ ███╗ ██╗ ██████╗ ███████╗ ██████╗ ██╗███╗ ██╗███████╗
358 ██╔════╝╚══██╔══╝██║██╔════╝██║ ██╔╝╚██╗ ██╔╝ ██╔══██╗██║██╔════╝╚══██╔══╝██╔═══██╗████╗ ██║ ██╔══██╗██╔════╝██╔════╝ ██║████╗ ██║██╔════╝
359 ███████╗ ██║ ██║██║ █████╔╝ ╚████╔╝ ██████╔╝██║███████╗ ██║ ██║ ██║██╔██╗ ██║ ██████╔╝█████╗ ██║ ███╗██║██╔██╗ ██║███████╗
360 ╚════██║ ██║ ██║██║ ██╔═██╗ ╚██╔╝ ██╔═══╝ ██║╚════██║ ██║ ██║ ██║██║╚██╗██║ ██╔══██╗██╔══╝ ██║ ██║██║██║╚██╗██║╚════██║
361 ███████║ ██║ ██║╚██████╗██║ ██╗ ██║ ██║ ██║███████║ ██║ ╚██████╔╝██║ ╚████║ ██████╔╝███████╗╚██████╔╝██║██║ ╚████║███████║
362 ╚══════╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝╚══════╝
372 ███████╗██╗ ██╗███╗ ██╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗
373 ██╔════╝██║ ██║████╗ ██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║
374 █████╗ ██║ ██║██╔██╗ ██║██║ ██║ ██║██║ ██║██╔██╗ ██║
375 ██╔══╝ ██║ ██║██║╚██╗██║██║ ██║ ██║██║ ██║██║╚██╗██║
376 ██║ ╚██████╔╝██║ ╚████║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║
377 ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
381 --this is how the piston pushes nodes
390 local function sticky_push_nodes(pos,dir)
394 index_pos = vector.add(vector.multiply(dir,i),pos)
395 node = minetest.get_node(index_pos)
397 def = minetest.registered_nodes[node.name]
399 push = ((excluded_mods[def.mod_origin] ~= true) and (excluded_nodes[name] ~= true))
400 if push and name ~= "air" then
402 index.pos = index_pos
404 index.param2 = param2
405 table.insert(move_index,index)
406 elseif name == "air" then
415 --check if room to move and objects in log
416 if space == true and next(move_index) then
417 for i = 1,table.getn(move_index) do
418 move_index[i].pos = vector.add(move_index[i].pos,dir)
419 minetest.set_node(move_index[i].pos,move_index[i])
420 minetest.check_for_falling(move_index[i].pos)
426 --this is the logic of the piston
429 local piston_location
431 local function sticky_actuator_arm_function(pos)
432 --this is where the piston activates
433 facedir = minetest.get_node(pos).param2
434 dir = minetest.facedir_to_dir(facedir)
435 piston_location = vector.add(pos,dir)
436 worked = sticky_push_nodes(pos,dir)
437 local node = minetest.get_node(vector.add(pos,dir)).name
439 if worked == true then
441 if node == "air" then
442 for _,object in ipairs(minetest.get_objects_inside_radius(piston_location, 2)) do
444 if object:is_player() and object:get_hp() > 0 then
445 local pos2 = object:get_pos()
446 local compare = vector.subtract(pos2,piston_location)
447 local real_y = compare.y
448 compare = vector.abs(compare)
451 if compare.y <= 0.5 and compare.x < 0.8 and compare.z < 0.8 then
452 object:move_to(vector.add(dir,pos2))
455 elseif dir.x ~=0 or dir.z ~= 0 then
456 if real_y <= 0.5 and real_y >= -1.6 and compare.x < 0.8 and compare.z < 0.8 then
457 object:move_to(vector.add(dir,pos2))
460 elseif not object:is_player() and object:get_luaentity().name == "__builtin:falling_node" then
461 local pos2 = object:get_pos()
462 local compare = vector.subtract(pos2,piston_location)
463 local real_y = compare.y
464 compare = vector.abs(compare)
465 if compare.y <= 1.5 and compare.x <= 1.5 and compare.z <= 1.5 then
466 object:move_to(vector.add(dir,pos2))
468 elseif not object:is_player() and object:get_luaentity().name == "__builtin:item" then
469 local pos2 = object:get_pos()
470 local compare = vector.subtract(pos2,piston_location)
471 local real_y = compare.y
472 compare = vector.abs(compare)
473 if compare.y <= 1 and compare.x <= 1 and compare.z <= 1 then
474 object:move_to(vector.add(dir,pos2))
475 object:get_luaentity().poll_timer = 0
480 minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
481 minetest.set_node(piston_location,{name="redstone:sticky_actuator",param2=facedir})
482 minetest.swap_node(pos,{name="redstone:sticky_piston_on",param2=facedir})
484 redstone.inject(pos,{
485 name = "redstone:sticky_piston_on",
495 ██████╗ ███████╗███████╗
496 ██╔═══██╗██╔════╝██╔════╝
504 minetest.register_node("redstone:sticky_piston_off", {
505 description = "Sticky Piston",
506 tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","sticky_piston.png","stone.png"},
507 paramtype2 = "facedir",
508 groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
509 sounds = main.stoneSound(),
510 drop = "redstone:sticky_piston_off",
512 sunlight_propagates = true,
513 on_construct = function(pos)
514 redstone.inject(pos,{
515 name = "redstone:sticky_piston_off",
520 --reverse the direction to face the player
521 after_place_node = function(pos, placer, itemstack, pointed_thing)
522 local look = placer:get_look_dir()
523 look = vector.multiply(look,-1)
524 local dir = minetest.dir_to_facedir(look, true)
525 minetest.swap_node(pos,{name="redstone:sticky_piston_off",param2=dir})
527 after_destruct = function(pos, oldnode)
528 redstone.inject(pos,nil)
533 redstone.register_activator({
534 name = "redstone:sticky_piston_off",
535 activate = function(pos)
536 minetest.after(0,function()
537 sticky_actuator_arm_function(pos)
542 minetest.register_lbm({
543 name = "redstone:sticky_piston_off",
544 nodenames = {"redstone:sticky_piston_off"},
545 run_at_every_load = true,
546 action = function(pos)
547 redstone.inject(pos,{
548 name = "redstone:sticky_piston_off",
551 --redstone needs to warm up
552 minetest.after(0,function()
568 local function sticky_piston_pull_nodes(pos,dir)
569 local move_index = {}
570 local index_pos = vector.add(pos,dir)
572 local node = minetest.get_node(index_pos)
573 local param2 = node.param2
574 local def = minetest.registered_nodes[node.name]
575 local name = node.name
576 local pull = ((excluded_mods[def.mod_origin] ~= true) and (excluded_nodes[name] ~= true))
577 --if it can be pulled pull it
578 if pull and name ~= "air" then
579 minetest.remove_node(index_pos)
580 minetest.set_node(pos,{name=name,param2=param2})
587 minetest.register_node("redstone:sticky_piston_on", {
588 description = "Sticky Piston",
589 tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","stone.png","stone.png"},
590 drawtype = "nodebox",
592 paramtype2 = "facedir",
593 groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
594 sounds = main.stoneSound(),
595 drop = "redstone:sticky_piston_off",
599 --left front bottom right back top
600 {-0.5, -0.5, -0.5, 0.5, 0.5, 3/16},
603 after_destruct = function(pos, oldnode)
604 local facedir = oldnode.param2
605 local dir = minetest.facedir_to_dir(facedir)
606 local piston_location = vector.add(pos,dir)
607 minetest.remove_node(piston_location)
608 redstone.inject(pos,nil)
612 minetest.register_lbm({
613 name = "redstone:sticky_piston_on",
614 nodenames = {"redstone:sticky_piston_on"},
615 run_at_every_load = true,
616 action = function(pos)
617 redstone.inject(pos,{
618 name = "redstone:sticky_piston_on",
621 --redstone needs to warm up
622 minetest.after(0,function()
628 redstone.register_activator({
629 name = "redstone:sticky_piston_on",
630 deactivate = function(pos)
631 --this is where the piston deactivates
632 local facedir = minetest.get_node(pos).param2
633 local dir = minetest.facedir_to_dir(facedir)
634 local piston_location = vector.add(pos,dir)
636 minetest.remove_node(piston_location)
638 sticky_piston_pull_nodes(piston_location,dir)
639 minetest.swap_node(pos,{name="redstone:sticky_piston_off",param2=facedir})
641 minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
642 redstone.inject(pos,{
643 name = "redstone:sticky_piston_off",
651 █████╗ ██████╗ ███╗ ███╗
652 ██╔══██╗██╔══██╗████╗ ████║
653 ███████║██████╔╝██╔████╔██║
654 ██╔══██║██╔══██╗██║╚██╔╝██║
655 ██║ ██║██║ ██║██║ ╚═╝ ██║
656 ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝
661 minetest.register_node("redstone:sticky_actuator", {
662 description = "Piston Actuator",
663 tiles = {"wood.png","wood.png","wood.png","wood.png","sticky_piston.png","wood.png"},
664 drawtype = "nodebox",
666 paramtype2 = "facedir",
667 groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1},
668 sounds = main.stoneSound(),
669 drop = "redstone:piston_off",
673 --left front bottom right back top
674 {-0.5, -0.5, 0.2, 0.5, 0.5, 0.5}, --mover
675 {-0.15, -0.15, -0.9, 0.15, 0.15, 0.5}, --actuator
678 after_destruct = function(pos, oldnode)
679 local facedir = oldnode.param2
680 local dir = minetest.facedir_to_dir(facedir)
681 dir = vector.multiply(dir,-1)
682 local piston_location = vector.add(pos,dir)
683 minetest.remove_node(piston_location)