EXTREME Redstone rewrite

This commit is contained in:
oilboi 2020-03-23 23:46:35 -04:00
parent ae8e1b84e3
commit 41ce55b178
9 changed files with 542 additions and 435 deletions

View File

@ -97,7 +97,7 @@ for _,door in pairs({"top","bottom"}) do
end
end
minetest.register_craft({
output = "door:wood_bottom",
output = "door:bottom_wood",
recipe = {
{"main:wood","main:wood"},
{"main:wood","main:wood"},
@ -105,7 +105,7 @@ minetest.register_craft({
}
})
minetest.register_craft({
output = "door:iron_bottom",
output = "door:bottom_iron",
recipe = {
{"main:iron","main:iron"},
{"main:iron","main:iron"},

View File

@ -1,6 +1,10 @@
local function comparator_logic(pos)
local old_power = get_powered_state_directional(pos)
local param2 = minetest.get_node(pos).param2
local dir = minetest.facedir_to_dir(param2)
local get_group = minetest.get_node_group
--get inputs and outputs
local input_pos_straight = vector.subtract(pos,dir)
@ -12,105 +16,98 @@ local function comparator_logic(pos)
local output_pos = vector.add(pos,dir)
local output = false
local input_level = string.gsub(minetest.get_node(input_pos_straight).name, "redstone:dust_", "")
local output_level = string.gsub(minetest.get_node(output_pos).name, "redstone:dust_", "")
local comparator_level = get_group(minetest.get_node(pos).name, "comparator")
input_level = tonumber(input_level)
output_level = tonumber(output_level)
local input_level = get_group(minetest.get_node(input_pos_straight).name, "redstone_power")
local output_level = get_group(minetest.get_node(output_pos).name, "redstone_power")
local getgroup = minetest.get_node_group
--this prefers right to left--
local output_node = minetest.get_node(output_pos).name
local input_node_straight = minetest.get_node(input_pos_straight).name
local left_level = string.gsub(minetest.get_node(input_pos_left).name, "redstone:dust_", "")
local right_level = string.gsub(minetest.get_node(input_pos_right).name, "redstone:dust_", "")
left_level = tonumber(left_level)
right_level = tonumber(right_level)
local left_level = get_group(minetest.get_node(input_pos_left).name, "redstone_power")
local right_level = get_group(minetest.get_node(input_pos_right).name, "redstone_power")
local compare_level = 0
if type(right_level) == "number" then
if right_level > compare_level then
compare_level = right_level
end
if right_level > compare_level then
compare_level = right_level
end
if type(left_level) == "number" then
if left_level > compare_level then
compare_level = left_level
end
if left_level > compare_level then
compare_level = left_level
end
local new_output_level = 0
if input_level > compare_level then
new_output_level = 9
elseif input_level <= compare_level then
new_output_level = 0
end
--charge
if getgroup(input_node_straight,"redstone_dust")>0 and getgroup(output_node,"redstone_dust")>0 then
print("charge")
if input_level > 0 and input_level >= compare_level then
minetest.set_node(output_pos, {name="redstone:dust_powered"})
elseif input_level > 0 and getgroup(output_node,"redstone_hack") > 0 then
minetest.set_node(output_pos, {name="redstone:dust_0"})
end
end
--discharge
if getgroup(input_node_straight,"redstone_dust")>0 and getgroup(output_node,"redstone_hack")>0 then
print("discharge")
if input_level == 0 then
minetest.set_node(output_pos, {name="redstone:dust_0"})
if input_level > 0 then--and output_level ~= new_output_level-1 then
if comparator_level ~= new_output_level then
--print("comparator on")
minetest.swap_node(pos, {name="redstone:comparator_"..new_output_level,param2=param2})
redstone.collect_info(output_pos)
end
elseif output_level ~= new_output_level-1 then
if comparator_level ~= new_output_level then
--print("comparator off")
minetest.swap_node(pos, {name="redstone:comparator_"..new_output_level,param2=param2})
redstone.collect_info(output_pos)
end
end
set_old_power(pos,old_power)
end
minetest.register_node("redstone:comparator", {
description = "Redstone Comparator",
tiles = {"repeater_on.png"},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation=1},
sounds = main.stoneSound(),
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = true,
walkable = false,
drawtype= "nodebox",
drop="redstone:comparator",
node_box = {
type = "fixed",
fixed = {
--left front bottom right back top
{-0.5, -0.5, -0.5, 0.5, -0.3, 0.5}, --base
{-0.1, -0.5, 0.2, 0.1, 0.1, 0.4}, --output post
{-0.4, -0.5, -0.35, -0.2, 0.1, -0.15}, --input post
{0.4, -0.5, -0.35, 0.2, 0.1, -0.15}, --input post
for i = 0,9 do
minetest.register_node("redstone:comparator_"..i, {
description = "Redstone Comparator",
tiles = {"repeater_on.png"},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation_directional=1,torch_directional=1,redstone_power=i,comparator=i},
sounds = main.stoneSound(),
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = true,
walkable = false,
drawtype= "nodebox",
drop="redstone:comparator_0",
node_box = {
type = "fixed",
fixed = {
--left front bottom right back top
{-0.5, -0.5, -0.5, 0.5, -0.3, 0.5}, --base
{-0.1, -0.5, 0.2, 0.1, 0.1, 0.4}, --output post
{-0.4, -0.5, -0.35, -0.2, 0.1, -0.15}, --input post
{0.4, -0.5, -0.35, 0.2, 0.1, -0.15}, --input post
},
},
},
--[[
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local newlevel = level + 1
if newlevel > 2 then
newlevel = 0
end
minetest.set_node(pos,{name="redstone:repeater_off_"..newlevel,param2=node.param2})
minetest.sound_play("lever", {pos=pos,pitch=1-(newlevel*0.1)})
end,
]]
redstone_activation = function(pos)
comparator_logic(pos)
end,
redstone_deactivation = function(pos)
comparator_logic(pos)
end,
on_construct = function(pos)
redstone.collect_info(pos)
end,
on_destruct = function(pos)
local param2 = minetest.get_node(pos).param2
local dir = minetest.facedir_to_dir(param2)
local output_pos = vector.add(pos,dir)
if minetest.get_node_group(minetest.get_node(output_pos).name, "redstone_hack") > 0 then
minetest.set_node(output_pos, {name="redstone:dust_0"})
end
redstone.collect_info(output_pos)
end,
})
redstone_activation = function(pos)
comparator_logic(pos)
end,
redstone_deactivation = function(pos)
comparator_logic(pos)
end,
redstone_update = function(pos)
comparator_logic(pos)
end,
on_construct = function(pos)
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
redstone.collect_info(pos)
end,
on_dig = function(pos, node, digger)
local param2 = minetest.get_node(pos).param2
minetest.node_dig(pos, node, digger)
local dir = minetest.facedir_to_dir(param2)
redstone.collect_info(vector.add(pos,dir))
end,
after_destruct = function(pos)
end,
})
end

View File

@ -26,6 +26,13 @@ minetest.register_craft({
{"main:stone","main:stone","main:stone"},
}
})
minetest.register_craft({
output = "redstone:inverter_off",
recipe = {
{"redstone:dust","redstone:torch","redstone:dust"},
{"main:stone","main:stone","main:stone"},
}
})
minetest.register_craft({
output = "redstone:comparator",
recipe = {

View File

@ -19,11 +19,86 @@ dofile(path.."/piston.lua")
dofile(path.."/comparator.lua")
dofile(path.."/craft.lua")
dofile(path.."/ore.lua")
dofile(path.."/inverter.lua")
redstone = {}
local r_index = {}
get_old_power = function(pos)
local meta = minetest.get_meta(pos)
local oldpower = meta:get_int("old_power")
return(oldpower)
end
--set the data for powered states
get_local_power = function(pos)
local max_level = 0
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
--index only direct neighbors
if not (math.abs(x)+math.abs(z) > 1) or (math.abs(x)+math.abs(z) == 0) then
--print(minetest.get_node(vector.add(vector.new(x,y,z),pos)).name)
local level = minetest.get_node_group(minetest.get_node(vector.add(vector.new(x,y,z),pos)).name, "redstone_power")
if level > max_level then
max_level = level
end
end
end
end
end
return(max_level)
end
--this is used for a power state comparison
set_old_power = function(pos,level)
local meta = minetest.get_meta(pos)
meta:set_int("old_power",level)
end
get_powered_state_directional = function(pos)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local param2 = node.param2
local dir = minetest.facedir_to_dir(param2)
local input_pos = vector.subtract(pos,dir)
local behind_node = minetest.get_node(input_pos)
local level = minetest.get_node_group(behind_node.name, "redstone_power")
return(level)
end
local redstone_activate = function(name,pos,power)
minetest.after(0,function(name,pos)
if minetest.registered_nodes[name].redstone_activation then
minetest.registered_nodes[name].redstone_activation(pos)
end
set_old_power(pos,power)
end,name,pos,power)
end
local redstone_deactivate = function(name,pos,power)
minetest.after(0,function(name,pos)
if minetest.registered_nodes[name].redstone_deactivation then
minetest.registered_nodes[name].redstone_deactivation(pos)
end
set_old_power(pos,power)
end,name,pos,power)
end
local redstone_update = function(name,pos,power)
minetest.after(0,function(name,pos,power)
if minetest.registered_nodes[name].redstone_update then
minetest.registered_nodes[name].redstone_update(pos)
end
set_old_power(pos,power)
end,name,pos,power)
end
--collect all nodes that are local to the modified
--node of redstone dust and store in memory
function redstone.collect_info(pos)
@ -31,18 +106,18 @@ function redstone.collect_info(pos)
--print("-----------------------")
--print("started indexing")
--end
local get_name = minetest.get_node
local get_node = minetest.get_node
local group = minetest.get_node_group
local function get_group(i,gotten_group)
return(group(get_name(i).name, gotten_group))
return(group(get_node(i).name, gotten_group))
end
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
--do not index self
if not (math.abs(x)+math.abs(z) > 1) then
--index only direct neighbors
if not (math.abs(x)+math.abs(z) > 1) or (math.abs(x)+math.abs(z) == 0) then
local r_type = ""
local i = vector.add(pos,vector.new(x,y,z))
local execute_collection = true
@ -51,40 +126,101 @@ function redstone.collect_info(pos)
execute_collection = false
end
end
--[[ EXPLANATION
we run through the groups
1 redstone_torch overrides dust if defined
2 torch_directional overrides torch if defined
3 redstone activation is bolted on with directional overriding general
this is to prevent weird behavior
This new method also uses a table bolted onto the x,y,z positional data value
what does this mean?
It's much easier to work with and modify
]]--
if execute_collection == true then
--index dust
if get_group(i,"redstone_dust") > 0 then
--add data to both maps
if not r_index[i.x] then r_index[i.x] = {} end
if not r_index[i.x][i.y] then r_index[i.x][i.y] = {} end
r_index[i.x][i.y][i.z] = 0
r_index[i.x][i.y][i.z] = {dust = true,level = 0} --get_group(i,"redstone_power")}
--the data to the 3d array must be written to memory before this is executed
--or a stack overflow occurs!!!
redstone.collect_info(i)
--pass down info for activators
redstone.collect_info(i,get_group(i,"redstone_power"))
end
--index power sources
elseif get_group(i,"redstone_torch") > 0 then
if get_group(i,"redstone_torch") > 0 then
if not r_index[i.x] then r_index[i.x] = {} end
if not r_index[i.x][i.y] then r_index[i.x][i.y] = {} end
r_index[i.x][i.y][i.z] = "torch"
r_index[i.x][i.y][i.z] = {torch = true,power=get_group(i,"redstone_power")}
end
--index directional power sources (Like repeaters/comparators)
--only outputs forwards
if get_group(i,"torch_directional") > 0 then
if not r_index[i.x] then r_index[i.x] = {} end
if not r_index[i.x][i.y] then r_index[i.x][i.y] = {} end
r_index[i.x][i.y][i.z] = {torch_directional = true, dir = get_node(i).param2 , power = get_group(i,"redstone_power")}
end
--index directional activators (Like repeaters/comparators)
--only accepts input from the back
if get_group(i,"redstone_activation_directional") > 0 then
--print("indexing directional")
if not r_index[i.x] then r_index[i.x] = {} end
if not r_index[i.x][i.y] then r_index[i.x][i.y] = {} end
if not r_index[i.x][i.y][i.z] then r_index[i.x][i.y][i.z] = {} end
--r_index[i.x][i.y][i.z].activate = false
r_index[i.x][i.y][i.z].redstone_activation = true
r_index[i.x][i.y][i.z].directional = true
r_index[i.x][i.y][i.z].dir = get_node(i).param2
end
--index objects that activate
elseif get_group(i,"redstone_activation") > 0 then
if get_group(i,"redstone_activation") > 0 then
if not r_index[i.x] then r_index[i.x] = {} end
if not r_index[i.x][i.y] then r_index[i.x][i.y] = {} end
r_index[i.x][i.y][i.z] = "deactivate"
if not r_index[i.x][i.y][i.z] then r_index[i.x][i.y][i.z] = {} end
--r_index[i.x][i.y][i.z].activate = false
r_index[i.x][i.y][i.z].redstone_activation = true
r_index[i.x][i.y][i.z].name = get_node(i).name
--set_powered_state(i)
--local powered = get_powered_state(i)
--print("powered:"..powered,"oldpowered:"..old_powered)
--r_index[i.x][i.y][i.z].powered = powered
--r_index[i.x][i.y][i.z].old_powered = old_powered
--split this into update up and down
--if powered > old_powered then
-- r_index[i.x][i.y][i.z].deactivate = true
---elseif powered > 0 and old_powered == 0 then
-- r_index[i.x][i.y][i.z].deactivate = true
--end
end
end
end
end
end
end
end
--check if index table contains items
--then execute an update
minetest.register_globalstep(function(dtime)
--if indexes exist then calculate redstone
if r_index and next(r_index) then
--create the old version to help with deactivation calculation
r_copy = table.copy(r_index)
redstone.calculate()
end
--clear the index to avoid cpu looping wasting processing power
@ -97,13 +233,16 @@ function redstone.calculate()
for x,index_x in pairs(r_index) do
for y,index_y in pairs(index_x) do
for z,data in pairs(index_y) do
if data == "torch" then
redstone.pathfind(vector.new(x,y,z),9)
--allow data values for torches
if data.torch then
redstone.pathfind(vector.new(x,y,z),data.power)
elseif data.torch_directional then
redstone.pathfind(vector.new(x,y,z),data.power,data.dir)
end
end
end
end
--calculate values for voxel manip
local x_min,x_max,y_min,y_max,z_min,z_max
for x,index_x in pairs(r_index) do
@ -135,95 +274,143 @@ function redstone.calculate()
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local content_id = minetest.get_name_from_content_id
--reassemble the table into a position list minetest can understand
--run through and set dust
for x,datax in pairs(r_index) do
for y,datay in pairs(datax) do
for z,level in pairs(datay) do
local p_pos = area:index(x,y,z)
--print(dump(z),dump(dataz))
if type(level) == "number" then
data[p_pos] = minetest.get_content_id("redstone:dust_"..level)
elseif type(level) == "string" and level == "activate" then
--print("activating")
local name = content_id(data[p_pos])
if minetest.registered_nodes[name].redstone_activation then
minetest.after(0,function(name,x,y,z)
minetest.registered_nodes[name].redstone_activation(vector.new(x,y,z))
end,name,x,y,z)
end
elseif type(level) == "string" and level == "deactivate" then
--print("deactivating")
local name = content_id(data[p_pos])
if minetest.registered_nodes[name].redstone_deactivation then
minetest.after(0,function(name,x,y,z)
minetest.registered_nodes[name].redstone_deactivation(vector.new(x,y,z))
end,name,x,y,z)
end
for z,index in pairs(datay) do
local p_pos = area:index(x,y,z)
if index.dust then
data[p_pos] = minetest.get_content_id("redstone:dust_"..index.level)
end
end
end
end
vm:set_data(data)
vm:write_to_map()
end
--make redstone wire pass on current one level lower than it is
function redstone.pathfind(source,source_level)
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
i = vector.add(vector.new(source.x,source.y,source.z),vector.new(x,y,z))
if r_index and r_index[i.x] and r_index[i.x][i.y] and r_index[i.x][i.y][i.z] then
level = r_index[i.x][i.y][i.z]
--normal redstone
if type(level) == "number" then
if level < source_level then
local passed_on_level = source_level - 1
r_index[i.x][i.y][i.z] = passed_on_level
if passed_on_level > 0 then
redstone.pathfind(i,passed_on_level)
--run through activators
for x,datax in pairs(r_index) do
for y,datay in pairs(datax) do
for z,index in pairs(datay) do
local pos = vector.new(x,y,z)
local node = minetest.get_node(pos)
--directional activators
if index.redstone_activation == true and index.directional == true then
local power = get_powered_state_directional(pos)
local old_power = get_old_power(pos)
if power > 0 and old_power == 0 then
redstone_activate(node.name,pos,power)
elseif power == 0 and old_power > 0 then
redstone_deactivate(node.name,pos,power)
--do an update if state has not changed
elseif power > 0 and old_power > 0 then
redstone_update(node.name,pos,power)
elseif power == 0 and old_power == 0 then
redstone_update(node.name,pos,power)
end
--non directional activators
elseif index.redstone_activation == true then
local power = get_local_power(pos)
local old_power = get_old_power(pos)
if power > 0 and old_power == 0 then
redstone_activate(node.name,pos,power)
elseif power == 0 and old_power > 0 then
redstone_deactivate(node.name,pos,power)
--do an update if state has not changed
elseif power > 0 and old_power > 0 then
redstone_update(node.name,pos,power)
elseif power == 0 and old_power == 0 then
redstone_update(node.name,pos,power)
end
end
--activators
elseif type(level) == "string" and level == "deactivate" then
local passed_on_level = source_level - 1
if source_level > 0 then
r_index[i.x][i.y][i.z] = "activate"
end
end
end
end
end
end
end
--make torches activate activators when placed
function redstone.torch_activate(pos)
--print("test")
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
local i = vector.add(pos,vector.new(x,y,z))
if minetest.get_node_group(minetest.get_node(i).name, "redstone_activation") > 0 then
minetest.registered_nodes[minetest.get_node(i).name].redstone_activation(i)
--make redstone wire pass on current one level lower than it is
function redstone.pathfind(source,source_level,direction)
--directional torches
if direction then
--print("starting direction")
local dir = minetest.facedir_to_dir(direction)
local i = vector.add(source,dir)
if r_index and r_index[i.x] and r_index[i.x][i.y] and r_index[i.x][i.y][i.z] then
local index = r_index[i.x][i.y][i.z]
--dust
if index.dust then
local passed_on_level = source_level - 1
if passed_on_level > 0 then
r_index[i.x][i.y][i.z].level = passed_on_level
redstone.pathfind(i,passed_on_level)
end
end
end
else
--redstone and torch
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
local i = vector.add(source,vector.new(x,y,z))
if r_index and r_index[i.x] and r_index[i.x][i.y] and r_index[i.x][i.y][i.z] then
local index = r_index[i.x][i.y][i.z]
if index.dust then
local passed_on_level = source_level - 1
if passed_on_level > 0 and index.level < source_level then
r_index[i.x][i.y][i.z].level = passed_on_level
redstone.pathfind(i,passed_on_level)
end
end
end
end
end
end
end
end
end
end
----------------------------------------------------------------------------
minetest.register_craftitem("redstone:dust", {
@ -272,14 +459,14 @@ for i = 0,8 do
type = "fixed",
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups={dig_immediate=1,attached=1,redstone_dust=1,redstone=1},
groups={dig_immediate=1,attached=1,redstone_dust=1,redstone=1,redstone_power=i},
drop="redstone:dust",
on_construct = function(pos)
redstone.collect_info(pos)
end,
after_destruct = function(pos)
--redstone.remove(pos,minetest.registered_nodes[minetest.get_node(pos).name].power)
redstone.collect_info(pos)
redstone.collect_info(pos,i)
end,
connects_to = {"group:redstone"},
})

View File

@ -0,0 +1,89 @@
--inverts redstone signal
minetest.register_node("redstone:inverter_on", {
description = "Redstone Inverter",
tiles = {"repeater_on.png"},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation_directional=1},
sounds = main.stoneSound(),
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = true,
walkable = false,
drawtype= "nodebox",
drop="redstone:inverter_off",
node_box = {
type = "fixed",
fixed = {
--left front bottom right back top
{-0.5, -0.5, -0.5, 0.5, -0.3, 0.5}, --base
{-0.2, -0.5, 0.2, 0.2, 0.1, 0.4}, --output post
},
},
--make the repeater turn on
redstone_activation = function(pos)
end,
on_timer = function(pos, elapsed)
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
end,
redstone_deactivation = function(pos)
local param2 = minetest.get_node(pos).param2
minetest.swap_node(pos,{name="redstone:inverter_off",param2=param2})
local dir = minetest.facedir_to_dir(param2)
redstone.collect_info(vector.add(pos,dir))
end,
redstone_update = function(pos)
end,
on_construct = function(pos)
end,
after_destruct = function(pos)
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
end,
})
minetest.register_node("redstone:inverter_off", {
description = "Redstone Inverter",
tiles = {"repeater_off.png"},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation_directional=1,torch_directional=1,redstone_power=9},
sounds = main.stoneSound(),
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = true,
walkable = false,
drawtype= "nodebox",
drop="redstone:inverter_off",
node_box = {
type = "fixed",
fixed = {
--left front bottom right back top
{-0.5, -0.5, -0.5, 0.5, -0.3, 0.5}, --base
{-0.2, -0.5, 0.2, 0.2, 0.1, 0.4}, --output post
},
},
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
end,
redstone_activation = function(pos)
local param2 = minetest.get_node(pos).param2
minetest.swap_node(pos,{name="redstone:inverter_on",param2=param2})
local dir = minetest.facedir_to_dir(param2)
redstone.collect_info(vector.add(pos,dir))
end,
redstone_deactivation = function(pos)
end,
on_timer = function(pos, elapsed)
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
redstone.collect_info(pos)
end,
on_dig = function(pos, node, digger)
minetest.node_dig(pos, node, digger)
redstone.collect_info(pos)
end,
redstone_update = function(pos)
end,
on_construct = function(pos)
end,
after_destruct = function(pos)
end,
})

View File

@ -48,14 +48,13 @@ minetest.register_node("redstone:piston_off", {
minetest.set_node(pos,{name="redstone:piston_on",param2=facedir})
end
end,
redstone_deactivation = function(pos)
end,
--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)
dir = minetest.dir_to_facedir(look, true)
minetest.set_node(pos,{name="redstone:piston_off",param2=dir})
redstone.collect_info(pos)
end,
})
minetest.register_node("redstone:piston_on", {
@ -74,8 +73,6 @@ minetest.register_node("redstone:piston_on", {
{-0.5, -0.5, -0.5, 0.5, 0.5, 3/16},
},
},
redstone_activation = function(pos)
end,
redstone_deactivation = function(pos)
--this is where the piston deactivates
local facedir = minetest.get_node(pos).param2

View File

@ -1,39 +1,44 @@
minetest.register_node("redstone:dust_powered",{
description = "Redstone Dust",
wield_image = "redstone_dust_item.png",
tiles = {
"redstone_dust_main.png^[colorize:red:255", "redstone_turn.png^[colorize:red:255",
"redstone_t.png^[colorize:red:255", "redstone_cross.png^[colorize:red:255"
},
power=i,
drawtype = "raillike",
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
walkable = false,
node_placement_prediction = "",
selection_box = {
type = "fixed",
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups={dig_immediate=1,attached=1,redstone=1,redstone_torch=1,redstone_hack=1},
drop="redstone:dust",
on_construct = function(pos)
redstone.collect_info(pos)
end,
after_destruct = function(pos)
--redstone.remove(pos,minetest.registered_nodes[minetest.get_node(pos).name].power)
redstone.collect_info(pos)
end,
connects_to = {"group:redstone"},
})
local function repeater_toggle(pos,repeater_level)
local old_power = get_powered_state_directional(pos)
local param2 = minetest.get_node(pos).param2
local input = minetest.facedir_to_dir(param2)
input = vector.multiply(input,-1)
input = vector.add(pos,input)
local output = minetest.facedir_to_dir(param2)
output = vector.add(pos,output)
local input_node = minetest.get_node(input)
local output_node = minetest.get_node(output)
local self_repeater_level = minetest.get_node_group(minetest.get_node(pos).name, "repeater_level")
if minetest.get_node_group(input_node.name, "redstone_power") == 0 then
minetest.swap_node(pos,{name="redstone:repeater_off_"..self_repeater_level,param2=param2})
elseif minetest.get_node_group(input_node.name, "redstone_power") > 0 then
minetest.swap_node(pos,{name="redstone:repeater_on_"..self_repeater_level,param2=param2})
end
set_old_power(pos,old_power)
--minetest.after(0.5, function(pos,param2,input,output,input_node,output_node)
if minetest.get_node_group(output_node.name, "redstone_dust") > 0 then
redstone.collect_info(output)
elseif minetest.get_node_group(output_node.name, "repeater") > 0 then
local timer = minetest.get_node_timer(output)
timer:start(repeater_level/2)
elseif minetest.get_node_group(output_node.name, "redstone_activation") > 0 then
redstone.collect_info(output)
end
--end,pos,param2,input,output,input_node,output_node)
end
for level = 0,2 do
minetest.register_node("redstone:repeater_off_"..level, {
description = "Redstone Repeater",
tiles = {"repeater_off.png"},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation=1,repeater_off=level+1},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,repeater_off=1,repeater=1,redstone_activation_directional=1,repeater_level=level},
sounds = main.stoneSound(),
paramtype = "light",
paramtype2 = "facedir",
@ -50,54 +55,47 @@ minetest.register_node("redstone:repeater_off_"..level, {
{-0.1, -0.5, -0.05-(level*0.15)--[[]], 0.1, 0.1, 0.15-(level*0.15)--[[]]}, --input post
},
},
--make the repeater turn on
redstone_activation = function(pos)
--repeater_toggle(pos)
local timer = minetest.get_node_timer(pos)
timer:start(level/2)
end,
on_timer = function(pos, elapsed)
repeater_toggle(pos,level)
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local newlevel = level + 1
if newlevel > 2 then
newlevel = 0
end
minetest.set_node(pos,{name="redstone:repeater_off_"..newlevel,param2=node.param2})
minetest.swap_node(pos,{name="redstone:repeater_off_"..newlevel,param2=node.param2})
minetest.sound_play("lever", {pos=pos,pitch=1-(newlevel*0.1)})
end,
--make the repeater turn on
redstone_activation = function(pos)
local param2 = minetest.get_node(pos).param2
local dir = minetest.facedir_to_dir(param2)
--get inputs and outputs
local input_pos = vector.add(pos,vector.multiply(dir,-1))
local output_pos = vector.add(pos,dir)
--check if powered redstone before
if (minetest.get_node_group(minetest.get_node(input_pos).name, "redstone_dust") > 0 and minetest.get_node(input_pos).name ~= "redstone:dust_0") or minetest.get_node_group(minetest.get_node(input_pos).name, "repeater") > 0 then
minetest.set_node(pos,{name="redstone:repeater_on_"..level,param2=param2})
local timer = minetest.get_node_timer(pos)
timer:start((level+1)/2)
end
end,
redstone_deactivation = function(pos)
--local param2 = minetest.get_node(pos).param2
end,
redstone_update = function(pos)
end,
on_construct = function(pos)
redstone.collect_info(pos)
end,
on_destruct = function(pos)
--redstone.remove(pos,minetest.registered_nodes[minetest.get_node(pos).name].power)
after_destruct = function(pos)
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
local timer = minetest.get_node_timer(pos)
timer:start(level/2)
end,
on_dig = function(pos, node, digger)
local param2 = minetest.get_node(pos).param2
minetest.node_dig(pos, node, digger)
local dir = minetest.facedir_to_dir(param2)
local output_pos = vector.add(pos,dir)
--check if powered redstone before
if minetest.get_node_group(minetest.get_node(output_pos).name, "repeater") > 0 then
local timer = minetest.get_node_timer(output_pos)
timer:start((level+1)/2)
elseif minetest.get_node_group(minetest.get_node(output_pos).name, "redstone_hack") > 0 then
minetest.set_node(output_pos, {name="redstone:dust_0"})
end
redstone.collect_info(output_pos)
redstone.collect_info(vector.add(pos,dir))
end,
})
minetest.register_node("redstone:repeater_on_"..level, {
description = "Redstone Repeater",
tiles = {"repeater_on.png"},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation=1,repeater=level+1},
groups = {stone = 1, hard = 1, pickaxe = 1, hand = 4,attached_node = 1,redstone_activation_directional=1,repeater_on=1,repeater=1,torch_directional=1,redstone_power=9,repeater_level=level},
sounds = main.stoneSound(),
paramtype = "light",
paramtype2 = "facedir",
@ -114,74 +112,43 @@ minetest.register_node("redstone:repeater_on_"..level, {
{-0.1, -0.5, -0.05-(level*0.15)--[[]], 0.1, 0.1, 0.15-(level*0.15)--[[]]}, --input post
},
},
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local newlevel = level + 1
if newlevel > 2 then
newlevel = 0
end
minetest.set_node(pos,{name="redstone:repeater_on_"..newlevel,param2=node.param2})
minetest.swap_node(pos,{name="redstone:repeater_on_"..newlevel,param2=node.param2})
minetest.sound_play("lever", {pos=pos,pitch=1-(newlevel*0.1)})
end,
on_timer = function(pos)
local param2 = minetest.get_node(pos).param2
local dir = minetest.facedir_to_dir(param2)
local input_pos = vector.add(pos,vector.multiply(dir,-1))
local output_pos = vector.add(pos,dir)
local output_node = minetest.get_node(output_pos)
local input_node = minetest.get_node(input_pos)
local repeater_output = minetest.get_node_group(output_node.name, "repeater_off")
local dust_output = minetest.get_node_group(output_node.name, "redstone")
if repeater_output > 0 then
minetest.registered_nodes[output_node.name].redstone_activation(output_pos)
elseif dust_output > 0 then
minetest.set_node(output_pos, {name="redstone:dust_powered"})
end
end,
redstone_activation = function(pos)
end,
redstone_deactivation = function(pos)
minetest.after((level+1)/2,function(pos)
local param2 = minetest.get_node(pos).param2
local dir = minetest.facedir_to_dir(param2)
--get inputs and outputs
local input_pos = vector.add(pos,vector.multiply(dir,-1))
local output_pos = vector.add(pos,dir)
--check if powered redstone before
if (minetest.get_node_group(minetest.get_node(input_pos).name, "redstone_dust") > 0 and minetest.get_node(input_pos).name == "redstone:dust_0") or minetest.get_node_group(minetest.get_node(input_pos).name, "repeater_off") > 0 then
minetest.set_node(pos,{name="redstone:repeater_off_"..level,param2=param2})
--check output node is repeater
local output_node = minetest.get_node(output_pos)
local repeater_output = minetest.get_node_group(output_node.name, "repeater")
local dust_output = minetest.get_node_group(output_node.name, "redstone")
if repeater_output > 0 then
minetest.registered_nodes[output_node.name].redstone_deactivation(output_pos)
elseif dust_output > 0 then
minetest.set_node(output_pos, {name="redstone:dust_0"})
end
end
end,pos)
--repeater_toggle(pos)
local timer = minetest.get_node_timer(pos)
timer:start(level/2)
end,
on_timer = function(pos, elapsed)
repeater_toggle(pos,level)
end,
on_dig = function(pos, node, digger)
repeater_toggle(pos,level)
minetest.node_dig(pos, node, digger)
end,
redstone_update = function(pos)
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
local timer = minetest.get_node_timer(pos)
timer:start(level/2)
end,
on_construct = function(pos)
redstone.collect_info(pos)
end,
on_destruct = function(pos)
--redstone.remove(pos,minetest.registered_nodes[minetest.get_node(pos).name].power)
after_destruct = function(pos)
end,
on_dig = function(pos, node, digger)
local param2 = minetest.get_node(pos).param2
minetest.node_dig(pos, node, digger)
local dir = minetest.facedir_to_dir(param2)
local output_pos = vector.add(pos,dir)
--check if powered redstone before
if minetest.get_node_group(minetest.get_node(output_pos).name, "repeater") > 0 then
local timer = minetest.get_node_timer(output_pos)
timer:start((level+1)/2)
elseif minetest.get_node_group(minetest.get_node(output_pos).name, "redstone_hack") > 0 then
minetest.set_node(output_pos, {name="redstone:dust_0"})
end
redstone.collect_info(output_pos)
redstone.collect_info(vector.add(pos,dir))
end,
})
end

View File

@ -3,9 +3,13 @@ for name,def in pairs(minetest.registered_nodes) do
if def.drawtype == "normal" and string.match(name, "main:") then
local def2 = table.copy(def)
def2.groups.redstone_torch = 1
def2.groups.redstone_power=9
def2.drop = name
def2.mod_origin = "redstone"
--def2.textures = "dirt.png"
def2.after_destruct = function(pos, oldnode)
redstone.collect_info(pos)
end
local newname = "redstone:"..string.gsub(name, "main:", "")
def2.name = newname
minetest.register_node(newname,def2)

View File

@ -1,83 +1,3 @@
--create torch versions of the nodes
local function turn_torch_off(pos)
local found_dust = false
print("finding dust")
for x = -1,1 do
for z = -1,1 do
if not (math.abs(x)+math.abs(z) > 1) then
local index_pos = vector.add(pos,vector.new(x,0,z))
local node = minetest.get_node(index_pos)
if minetest.get_node_group(node.name,"redstone_dust")>0 then
local level = string.gsub(minetest.get_node(index_pos).name, "redstone:dust_", "")
level = tonumber(level)
print(level)
if level > 0 then
found_dust = true
end
end
end
end
end
if found_dust == true then
for x = -1,1 do
for z = -1,1 do
if not (math.abs(x)+math.abs(z) > 1) then
local index_pos = vector.add(pos,vector.new(x,0,z))
local node = minetest.get_node(index_pos)
if node.name == "redstone:torch_wall" then
minetest.set_node(index_pos,{name="redstone:torch_wall_off",param2=node.param2})
end
end
end
end
end
end
local function turn_torch_on(pos)
local found_dust = false
for x = -1,1 do
for z = -1,1 do
if not (math.abs(x)+math.abs(z) > 1) then
local index_pos = vector.add(pos,vector.new(x,0,z))
local node = minetest.get_node(index_pos)
if minetest.get_node_group(node.name,"redstone_dust")>0 and node.name == "redstone:dust_0" then
found_dust = true
end
end
end
end
if found_dust == true then
for x = -1,1 do
for z = -1,1 do
if not (math.abs(x)+math.abs(z) > 1) then
local index_pos = vector.add(pos,vector.new(x,0,z))
local node = minetest.get_node(index_pos)
if node.name == "redstone:torch_wall_off" then
minetest.set_node(index_pos,{name="redstone:torch_wall",param2=node.param2})
end
end
end
end
end
end
for name,def in pairs(minetest.registered_nodes) do
if def.drawtype == "normal" and string.match(name, "main:") then
local def2 = table.copy(def)
def2.groups.redstone_activation = 1
def2.drop = name
def2.mod_origin = "redstone"
def2.redstone_activation = function(pos)
turn_torch_off(pos)
end
def2.redstone_deactivation = function(pos)
turn_torch_on(pos)
end
--def2.textures = "dirt.png"
local newname = "redstone:"..string.gsub(name, "main:", "").."_deactivator"
def2.name = newname
minetest.register_node(newname,def2)
end
end
--get point where particle spawner is added
local function get_offset(wdir)
local z = 0
@ -189,11 +109,11 @@ minetest.register_craftitem("redstone:torch", {
retval = fakestack:set_name("redstone:torch_floor")
else
retval = fakestack:set_name("redstone:torch_wall")
local name = minetest.get_node(pointed_thing.under).name
local def = minetest.registered_nodes[name]
if def.drawtype == "normal" and string.match(name, "main:") then
minetest.set_node(pointed_thing.under,{name="redstone:"..string.gsub(name, "main:", "").."_deactivator"})
end
--local name = minetest.get_node(pointed_thing.under).name
--local def = minetest.registered_nodes[name]
--if def.drawtype == "normal" and string.match(name, "main:") then
-- minetest.set_node(pointed_thing.under,{name="redstone:"..string.gsub(name, "main:", "").."_deactivator"})
--end
end
if not retval then
return itemstack
@ -223,7 +143,7 @@ minetest.register_node("redstone:torch_floor", {
drop = "redstone:torch",
walkable = false,
light_source = 13,
groups = {choppy=2, dig_immediate=3, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,connect_to_raillike=1},
groups = {choppy=2, dig_immediate=3, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,redstone_power=9},
legacy_wallmounted = true,
selection_box = {
type = "fixed",
@ -253,7 +173,7 @@ minetest.register_node("redstone:torch_wall", {
walkable = false,
light_source = 13,
power = 9,
groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,connect_to_raillike=1},
groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,redstone_torch=1,redstone_power=9},
drop = "redstone:torch",
selection_box = {
type = "wallmounted",
@ -271,64 +191,3 @@ minetest.register_node("redstone:torch_wall", {
sounds = main.woodSound(),
})
minetest.register_node("redstone:torch_floor_off", {
inventory_image = "redstone_torch.png",
wield_image = "redstone_torch.png",
wield_scale = {x = 1, y = 1, z = 1 + 2/16},
drawtype = "mesh",
mesh = "torch_floor.obj",
tiles = {"redstone_torch.png^[colorize:black:150"},
paramtype = "light",
paramtype2 = "none",
power = 9,
sunlight_propagates = true,
drop = "redstone:torch",
walkable = false,
light_source = 13,
groups = {choppy=2, dig_immediate=3, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,connect_to_raillike=1},
legacy_wallmounted = true,
selection_box = {
type = "fixed",
fixed = {-1/16, -0.5, -1/16, 1/16, 2/16, 1/16},
},
on_construct = function(pos)
redstone.collect_info(pos)
end,
after_destruct = function(pos, oldnode)
redstone.collect_info(pos)
end,
sounds = main.woodSound(),
})
minetest.register_node("redstone:torch_wall_off", {
inventory_image = "redstone_torch.png",
wield_image = "redstone_torch.png",
wield_scale = {x = 1, y = 1, z = 1 + 1/16},
drawtype = "mesh",
mesh = "torch_wall.obj",
tiles = {"redstone_torch.png^[colorize:black:150"},
paramtype = "light",
paramtype2 = "wallmounted",
sunlight_propagates = true,
walkable = false,
light_source = 13,
power = 9,
groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1,redstone=1,connect_to_raillike=1},
drop = "redstone:torch",
selection_box = {
type = "wallmounted",
wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
wall_side = {-0.5, -0.3, -0.1, -0.2, 0.3, 0.1},
},
on_construct = function(pos)
redstone.collect_info(pos)
end,
after_destruct = function(pos, oldnode)
redstone.collect_info(pos)
end,
sounds = main.woodSound(),
})