]> git.lizzy.rs Git - Crafter.git/commitdiff
Implement basic sticky piston
authoroilboi <47129783+oilboi@users.noreply.github.com>
Sun, 28 Jun 2020 12:36:05 +0000 (08:36 -0400)
committeroilboi <47129783+oilboi@users.noreply.github.com>
Sun, 28 Jun 2020 12:36:05 +0000 (08:36 -0400)
mods/redstone/piston.lua

index 52809c2d06131af9dc3c8f5c12f7a5e91d3e99ea..1acea75106b24a5fdc7344f5cdd5e74e8ac3d480 100644 (file)
@@ -361,27 +361,37 @@ minetest.register_node("redstone:actuator", {
 
 
 
+--[[
+███████╗██╗   ██╗███╗   ██╗ ██████╗████████╗██╗ ██████╗ ███╗   ██╗
+██╔════╝██║   ██║████╗  ██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗  ██║
+█████╗  ██║   ██║██╔██╗ ██║██║        ██║   ██║██║   ██║██╔██╗ ██║
+██╔══╝  ██║   ██║██║╚██╗██║██║        ██║   ██║██║   ██║██║╚██╗██║
+██║     ╚██████╔╝██║ ╚████║╚██████╗   ██║   ██║╚██████╔╝██║ ╚████║
+╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝   ╚═╝   ╚═╝ ╚═════╝ ╚═╝  ╚═══╝
+]]
 
 
-
-
-
---[[
--------------------------------------------------------------------------------------------------------------------------------------------------------
 --this is how the piston pushes nodes
-local function sticky_piston_push_nodes(pos,dir)
-       local move_index = {}
-       local space = false
+local move_index
+local space
+local index_pos
+local node
+local param2
+local def
+local push
+local index
+local function sticky_push_nodes(pos,dir)
+       move_index = {}
+       space = false
        for i = 1,30 do
-               local index_pos = vector.multiply(dir,i)
-               local index_pos = vector.add(index_pos,pos)
-               local node = minetest.get_node(index_pos)
-               local param2 = node.param2
-               local def = minetest.registered_nodes[node.name]
-               local name = node.name
-               local push = ((excluded_mods[def.mod_origin] ~= true) and (excluded_nodes[name] ~= true))
+               index_pos = vector.add(vector.multiply(dir,i),pos)
+               node = minetest.get_node(index_pos)
+               param2 = node.param2
+               def = minetest.registered_nodes[node.name]
+               name = node.name
+               push = ((excluded_mods[def.mod_origin] ~= true) and (excluded_nodes[name] ~= true))
                if push and name ~= "air" then
-                       local index = {}
+                       index = {}
                        index.pos = index_pos
                        index.name = name
                        index.param2 = param2
@@ -394,28 +404,36 @@ local function sticky_piston_push_nodes(pos,dir)
                        break
                end             
        end
+
        --check if room to move and objects in log
        if space == true and next(move_index) then
                for i = 1,table.getn(move_index) do
                        move_index[i].pos = vector.add(move_index[i].pos,dir)
-                       minetest.set_node(move_index[i].pos,{name=move_index[i].name,param2=move_index[i].param2})
+                       minetest.set_node(move_index[i].pos,move_index[i])
+                       minetest.check_for_falling(move_index[i].pos)
                end
        end
        return(space)
 end
 
 --this is the logic of the piston
-local function sticky_piston_push(pos)
+local facedir
+local dir
+local piston_location
+local worked
+local function sticky_actuator_arm_function(pos)
        --this is where the piston activates
-       local facedir = minetest.get_node(pos).param2
-       local dir = minetest.facedir_to_dir(facedir)
-       local piston_location = vector.add(pos,dir)
-       local worked = sticky_piston_push_nodes(pos,dir)
+       facedir = minetest.get_node(pos).param2
+       dir = minetest.facedir_to_dir(facedir)
+       piston_location = vector.add(pos,dir)
+       worked = sticky_push_nodes(pos,dir)
        local node = minetest.get_node(vector.add(pos,dir)).name
+       
        if worked == true then
                --push player
                if node == "air" then
                        for _,object in ipairs(minetest.get_objects_inside_radius(piston_location, 2)) do
+
                                if object:is_player() and object:get_hp() > 0 then
                                        local pos2 = object:get_pos()
                                        local compare = vector.subtract(pos2,piston_location)
@@ -425,120 +443,116 @@ local function sticky_piston_push(pos)
                                        if dir.y == 1 then
                                                if compare.y <= 0.5 and compare.x < 0.8 and compare.z < 0.8 then
                                                        object:move_to(vector.add(dir,pos2))
-                                                       --object:add_player_velocity(vector.multiply(dir,20))
                                                end
                                        --piston sideways
                                        elseif dir.x ~=0 or dir.z ~= 0 then
                                                if real_y <= 0.5 and real_y >= -1.6 and compare.x < 0.8 and compare.z < 0.8 then
                                                        object:move_to(vector.add(dir,pos2))
-                                                       --object:add_player_velocity(vector.multiply(dir,20))
-                                               
                                                end
                                        end
+                               elseif not object:is_player() and object:get_luaentity().name == "__builtin:falling_node" then
+                                       local pos2 = object:get_pos()
+                                       local compare = vector.subtract(pos2,piston_location)
+                                       local real_y = compare.y
+                                       compare = vector.abs(compare)
+                                       if compare.y <= 1.5 and compare.x <= 1.5 and compare.z <= 1.5 then
+                                               object:move_to(vector.add(dir,pos2))
+                                       end
+                               elseif not object:is_player() and object:get_luaentity().name == "__builtin:item" then
+                                       local pos2 = object:get_pos()
+                                       local compare = vector.subtract(pos2,piston_location)
+                                       local real_y = compare.y
+                                       compare = vector.abs(compare)
+                                       if compare.y <= 1 and compare.x <= 1 and compare.z <= 1 then
+                                               object:move_to(vector.add(dir,pos2))
+                                               object:get_luaentity().poll_timer = 0
+                                       end
                                end
                        end
                end
                minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
                minetest.set_node(piston_location,{name="redstone:sticky_actuator",param2=facedir})
-               minetest.set_node(pos,{name="redstone:sticky_piston_on",param2=facedir})
-       end
-end
+               minetest.swap_node(pos,{name="redstone:sticky_piston_on",param2=facedir})
 
-
-
---this is how sticky pistons pull nodes
-local function sticky_piston_pull_nodes(pos,dir)
-       
-       local move_index = {}
-       local index_pos = vector.add(pos,dir)
-       
-       local node = minetest.get_node(index_pos)
-       local param2 = node.param2
-       local def = minetest.registered_nodes[node.name]
-       local name = node.name
-       local pull = ((excluded_mods[def.mod_origin] ~= true) and (excluded_nodes[name] ~= true))
-       --if it can be pulled pull it
-       if pull and name ~= "air" then
-               minetest.remove_node(index_pos)
-               minetest.set_node(pos,{name=name,param2=param2})
+               redstone.inject(pos,{
+                       name = "redstone:sticky_piston_on",
+                       activator = true,
+               })
+               minetest.after(0,function()
+                       redstone.update(pos)
+               end)
        end
 end
 
 
---this is the logic of the sticky piston on return
-local function sticky_piston_pull(pos,dir)
-       --this is where the piston activates
-       --local facedir = minetest.get_node(pos).param2
-       --local dir = minetest.facedir_to_dir(facedir)
-       --local piston_location = vector.add(pos,dir)
-       
-       local in_front_pos = vector.add(pos,dir)
-       
-       local node = minetest.get_node(in_front_pos).name
-       --pull nodes
-       sticky_piston_pull_nodes(pos,dir)
-       
-       --pull player
-       if node == "air" then
-               for _,object in ipairs(minetest.get_objects_inside_radius(in_front_pos, 2)) do
-                       if object:is_player() and object:get_hp() > 0 then
-                               local pos2 = object:get_pos()
-                               local compare = vector.subtract(pos2,in_front_pos)
-                               local real_y = compare.y
-                               compare = vector.abs(compare)
-                               --piston pointing up
-                               if dir.y == 1 then
-                                       if compare.y <= 0.5 and compare.x < 0.8 and compare.z < 0.8 then
-                                               dir = vector.multiply(dir,-1)
-                                               object:move_to(vector.add(dir,pos2))
-                                               --object:add_player_velocity(vector.multiply(dir,20))
-                                       end
-                               --piston sideways
-                               elseif dir.x ~=0 or dir.z ~= 0 then
-                                       if real_y <= 0.5 and real_y >= -1.6 and compare.x < 0.8 and compare.z < 0.8 then
-                                               dir = vector.multiply(dir,-1)
-                                               object:move_to(vector.add(dir,pos2))
-                                               --object:add_player_velocity(vector.multiply(dir,20))
-                                       
-                                       end
-                               end
-                       end
-               end
-       end
-       minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
-       --minetest.set_node(piston_location,{name="redstone:sticky_actuator",param2=facedir})
-       --minetest.set_node(pos,{name="redstone:sticky_piston_on",param2=facedir})
-end
+--[[
+ ██████╗ ███████╗███████╗
+██╔═══██╗██╔════╝██╔════╝
+██║   ██║█████╗  █████╗  
+██║   ██║██╔══╝  ██╔══╝  
+╚██████╔╝██║     ██║     
+ ╚═════╝ ╚═╝     ╚═╝     
+]]
 
-------------------------------[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
 
 minetest.register_node("redstone:sticky_piston_off", {
     description = "Sticky Piston",
-    tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","sticky_piston.png","stone.png"},
+    tiles = {"redstone_piston.png","redstone_piston.png^[transformR180","redstone_piston.png^[transformR270","redstone_piston.png^[transformR90","wood.png","stone.png"},
     paramtype2 = "facedir",
     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1,redstone_activation=1},
     sounds = main.stoneSound(),
     drop = "redstone:sticky_piston_off",
     paramtype = "light",
-    sunlight_propagates = true,
-    redstone_activation = function(pos)
-               if minetest.get_node(pos).name == "redstone:sticky_piston_off" then
-                       sticky_piston_push(pos)
-               end
-    end,
+       sunlight_propagates = true,
     --reverse the direction to face the player
     after_place_node = function(pos, placer, itemstack, pointed_thing)
                local look = placer:get_look_dir()
                look = vector.multiply(look,-1)
                local dir = minetest.dir_to_facedir(look, true)
-               minetest.set_node(pos,{name="redstone:sticky_piston_off",param2=dir})
-               redstone.collect_info(pos)
+               minetest.swap_node(pos,{name="redstone:sticky_piston_off",param2=dir})
+               redstone.inject(pos,{
+                       name = "redstone:sticky_piston_off",
+                       activator = true,
+               })
+               redstone.update(pos)
+       end,
+       after_destruct = function(pos, oldnode)
+               redstone.inject(pos,nil)
     end,
 })
 
 
---------------------------
+redstone.register_activator({
+       name = "redstone:sticky_piston_off",
+       activate = function(pos)
+               sticky_actuator_arm_function(pos)
+       end
+})
+
+minetest.register_lbm({
+       name = "redstone:sticky_piston_off",
+       nodenames = {"redstone:sticky_piston_off"},
+       run_at_every_load = true,
+       action = function(pos)
+               redstone.inject(pos,{
+                       name = "redstone:sticky_piston_off",
+                       activator = true,
+               })
+               minetest.after(0,function()
+                       redstone.update(pos)
+               end)
+       end,
+})
+
 
+--[[
+ ██████╗ ███╗   ██╗
+██╔═══██╗████╗  ██║
+██║   ██║██╔██╗ ██║
+██║   ██║██║╚██╗██║
+╚██████╔╝██║ ╚████║
+ ╚═════╝ ╚═╝  ╚═══╝
+]]
 
 minetest.register_node("redstone:sticky_piston_on", {
     description = "Sticky Piston",
@@ -556,37 +570,69 @@ minetest.register_node("redstone:sticky_piston_on", {
                                {-0.5, -0.5,  -0.5, 0.5,  0.5, 3/16},
                        },
                },
-    redstone_deactivation = function(pos)
-               --this is where the piston deactivates
-               local facedir = minetest.get_node(pos).param2
+    after_destruct = function(pos, oldnode)
+               local facedir = oldnode.param2
                local dir = minetest.facedir_to_dir(facedir)
                local piston_location = vector.add(pos,dir)
                minetest.remove_node(piston_location)
-               minetest.set_node(pos,{name="redstone:sticky_piston_off",param2=facedir})
-               
-               sticky_piston_pull(piston_location,dir)
-               
-               piston_location.y = piston_location.y + 1
-               minetest.punch_node(piston_location)
-               --minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
+               redstone.inject(pos,nil)
     end,
-    after_dig_node = function(pos, oldnode, oldmetadata, digger)
-               local facedir = oldnode.param2
+})
+
+minetest.register_lbm({
+       name = "redstone:sticky_piston_on",
+       nodenames = {"redstone:sticky_piston_on"},
+       run_at_every_load = true,
+       action = function(pos)
+               redstone.inject(pos,{
+                       name = "redstone:sticky_piston_on",
+                       activator = true,
+               })
+               minetest.after(0,function()
+                       redstone.update(pos)
+               end)
+       end,
+})
+
+redstone.register_activator({
+       name = "redstone:piston_on",
+       deactivate = function(pos)
+               --this is where the piston deactivates
+               local facedir = minetest.get_node(pos).param2
                local dir = minetest.facedir_to_dir(facedir)
                local piston_location = vector.add(pos,dir)
                minetest.remove_node(piston_location)
-    end,
+               minetest.swap_node(pos,{name="redstone:sticky_piston_off",param2=facedir})
+               piston_location.y = piston_location.y + 1
+               minetest.sound_play("piston", {pos=pos,pitch=math.random(85,100)/100})
+               redstone.inject(pos,{
+                       name = "redstone:sticky_piston_off",
+                       activator = true,
+               })
+       end
 })
 
+
+--[[
+ █████╗ ██████╗ ███╗   ███╗
+██╔══██╗██╔══██╗████╗ ████║
+███████║██████╔╝██╔████╔██║
+██╔══██║██╔══██╗██║╚██╔╝██║
+██║  ██║██║  ██║██║ ╚═╝ ██║
+╚═╝  ╚═╝╚═╝  ╚═╝╚═╝     ╚═╝
+]]
+
+
+
 minetest.register_node("redstone:sticky_actuator", {
-    description = "Redstone Piston",
-    tiles = {"wood.png","wood.png","wood.png","wood.png","sticky_piston.png","wood.png"},
+    description = "Piston Actuator",
+    tiles = {"wood.png"},
     drawtype = "nodebox",
     paramtype = "light",
     paramtype2 = "facedir",
     groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,pathable = 1},
     sounds = main.stoneSound(),
-    drop = "redstone:sticky_piston_off",
+    drop = "redstone:piston_off",
     node_box = {
                type = "fixed",
                fixed = {
@@ -595,7 +641,7 @@ minetest.register_node("redstone:sticky_actuator", {
                                {-0.15, -0.15,  -0.9, 0.15,  0.15, 0.5}, --actuator
                        },
                },
-       after_dig_node = function(pos, oldnode, oldmetadata, digger)
+       after_destruct = function(pos, oldnode)
                local facedir = oldnode.param2
                local dir = minetest.facedir_to_dir(facedir)
                dir = vector.multiply(dir,-1)
@@ -603,5 +649,4 @@ minetest.register_node("redstone:sticky_actuator", {
                minetest.remove_node(piston_location)
     end,
 })
-]]--