owl_tech/electro_wire/electro_api.lua

214 lines
8.6 KiB
Lua
Raw Normal View History

2023-01-27 20:32:13 +01:00
--Get and set charge
function owl_tech:get_charge(meta)
return meta:get_int("charge")
end
function owl_tech:set_charge(meta,num)
meta:set_int("charge",num)
end
---------------------------------------
--Get and set max charge
function owl_tech:get_charge_max(meta)
return meta:get_int("max_charge")
end
function owl_tech:set_charge_max(meta,num)
meta:set_int("max_charge",num)
end
---------------------------------------
--Get and set voltage
function owl_tech:get_voltage(meta)
return meta:get_int("voltage")
end
function owl_tech:set_voltage(meta,num)
meta:set_int("voltage",num)
end
---------------------------------------
--Add base electro in node
function owl_tech:add_electro(pos,voltage,max_charge)
local meta=minetest.get_meta(pos)
meta:set_int("voltage",voltage)
meta:set_int("max_charge",max_charge)
meta:set_int("charge",0)
end
---------------------------------------
--Prepear vire
function owl_tech:prepear_vire(pos,voltage)
local meta=minetest.get_meta(pos)
owl_tech:add_electro(pos,voltage,voltage*6)
owl_tech.reset_connection_wire_meta(pos)
meta:set_string("infotext", meta:get_int(voltage).."--voltage")
end
---------------------------------------
--Reset connection meta
function owl_tech.reset_connection_wire_meta(pos)
local meta=minetest.get_meta(pos)
meta:set_int("connect_amount", 0)
meta:set_int("+Y", 0)
meta:set_int("-Y", 0)
meta:set_int("+Z", 0)
meta:set_int("-Z", 0)
meta:set_int("+X", 0)
meta:set_int("-X", 0)
end
---------------------------------------
--seeters and geters connection amount
function owl_tech:get_connection_amount_wire(pos)
return (minetest.get_meta(pos)):get_int("connect_amount")
end
function owl_tech:set_connection_amount_wire(pos,value)
(minetest.get_meta(pos)):set_int("connect_amount", value)
end
function owl_tech.add_1_conection_amount_wire(pos)
(minetest.get_meta(pos)):set_int("connect_amount",minetest.get_meta(pos):get_int("connect_amount")+1 )
end
---------------------------------------
--Can send electro in pos
function owl_tech:can_send_electro(pos,pos_send)
local meta = minetest.get_meta(pos)
local meta_send = minetest.get_meta(pos_send)
if owl_tech:get_voltage(meta)== owl_tech:get_voltage(meta_send)then-- if == voltage
if owl_tech:get_charge(meta)>=owl_tech:get_voltage(meta)
and owl_tech:get_charge(meta_send)+owl_tech:get_voltage(meta_send)<=owl_tech:get_charge_max(meta_send) then
return true
else
return false
end
else
return false
end
end
-----------------------------------------
--Send electro in pos
function owl_tech:send_electro(pos,pos_send)
local meta = minetest.get_meta(pos)
local meta_send = minetest.get_meta(pos_send)
owl_tech:set_charge(meta,owl_tech:get_voltage(meta)-owl_tech:get_voltage(meta))--sender
owl_tech:set_charge(meta_send,owl_tech:get_voltage(meta_send)+owl_tech:get_voltage(meta_send))--resiver
end
----------------------------------------
--node in pos ?in list
local conetc_to={
"group:owl_tech_electro_wire","owl_tech_electro_mashine","owl_tech_electro_gen"
}
function owl_tech.node_in_pos_connect_to_electro_wire(pos)
local ret=0
for index, value in ipairs(conetc_to) do
if minetest.get_item_group((minetest.get_node(pos)).name,value)>0 then
ret=1
end
end
return ret
end
-------------------------------------------
--look around for wire
function owl_tech:look_around_for_wire(pos)
owl_tech.reset_connection_wire_meta(pos)
local meta=minetest.get_meta(pos)
if owl_tech:get_voltage(meta)==owl_tech:get_voltage(minetest.get_meta({x=pos.x,y=pos.y+1,z=pos.z}))
and minetest.get_node_group(minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z}),"group:owl_tech_electro_wire") then --+Y
owl_tech.add_1_conection_amount_wire(pos)
meta:set_int("+Y", 1)
end
if owl_tech:get_voltage(meta)==owl_tech:get_voltage(minetest.get_meta({x=pos.x,y=pos.y-1,z=pos.z}))
and minetest.get_node_group(minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}),"group:owl_tech_electro_wire") then ---Y
owl_tech.add_1_conection_amount_wire(pos)
meta:set_int("-Y", 1)
end
if owl_tech:get_voltage(meta)==owl_tech:get_voltage(minetest.get_meta({x=pos.x,y=pos.y,z=pos.z+1}))
and minetest.get_node_group(minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1}),"group:owl_tech_electro_wire") then --+Z
owl_tech.add_1_conection_amount_wire(pos)
meta:set_int("+Z", 1)
end
if owl_tech:get_voltage(meta)==owl_tech:get_voltage(minetest.get_meta({x=pos.x,y=pos.y,z=pos.z-1}))
and minetest.get_node_group(minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1}),"group:owl_tech_electro_wire") then ---Z
owl_tech.add_1_conection_amount_wire(pos)
meta:set_int("-Z", 1)
end
if owl_tech:get_voltage(meta)==owl_tech:get_voltage(minetest.get_meta({x=pos.x+1,y=pos.y,z=pos.z}))
and minetest.get_node_group(minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z}),"group:owl_tech_electro_wire") then --+X
owl_tech.add_1_conection_amount_wire(pos)
meta:set_int("+X", 1)
end
if owl_tech:get_voltage(meta)==owl_tech:get_voltage(minetest.get_meta({x=pos.x-1,y=pos.y,z=pos.z}))
and minetest.get_node_group(minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z}),"group:owl_tech_electro_wire") then ---X
owl_tech.add_1_conection_amount_wire(pos)
meta:set_int("-X", 1)
end
end
----------------------------------------------------------------------
--select side to sand
function owl_tech:select_side_to_sande_wire(pos)
local meta=minetest.get_meta(pos)
local side_table ={ -- +y-1 -y-2 +x-3 -x-4 +z-5 -z-5
math.huge,math.huge,math.huge,math.huge,math.huge,math.huge
}
if meta:get_int("+Y")~=0 then
local meta_send= minetest.get_meta({x=pos.x,y=pos.y+1,z=pos.z})
side_table[1]=owl_tech:get_charge_max(meta_send)-owl_tech:get_charge(meta_send)
end
if meta:get_int("-Y")~=0 then
local meta_send= minetest.get_meta({x=pos.x,y=pos.y-1,z=pos.z})
side_table[2]=owl_tech:get_charge_max(meta_send)-owl_tech:get_charge(meta_send)
end
if meta:get_int("+X")~=0 then
local meta_send= minetest.get_meta({x=pos.x+1,y=pos.y,z=pos.z})
side_table[3]=owl_tech:get_charge_max(meta_send)-owl_tech:get_charge(meta_send)
end
if meta:get_int("-X")~=0 then
local meta_send= minetest.get_meta({x=pos.x-1,y=pos.y,z=pos.z})
side_table[4]=owl_tech:get_charge_max(meta_send)-owl_tech:get_charge(meta_send)
end
if meta:get_int("+Z")~=0 then
local meta_send= minetest.get_meta({x=pos.x,y=pos.y,z=pos.z+1})
side_table[5]=owl_tech:get_charge_max(meta_send)-owl_tech:get_charge(meta_send)
end
if meta:get_int("-Z")~=0 then
local meta_send= minetest.get_meta({x=pos.x,y=pos.y,z=pos.z-1})
side_table[6]=owl_tech:get_charge_max(meta_send)-owl_tech:get_charge(meta_send)
end
local adres_min =-1
local table_min= math.huge
for i = 1, #side_table, 1 do
if table_min>side_table[i] then
table_min=side_table[i]
adres_min=i
end
end
return adres_min
end
-------------------------------------------------------------------------
--Send to side electro
function owl_tech:send_to_side_electro(pos)
local meta = minetest.get_meta(pos)
local number_side_to_sand = owl_tech:select_side_to_sande_wire(pos)
if number_side_to_sand ==1 then
if owl_tech:can_send_electro(pos,{x=pos.x,y=pos.y+1,z=pos.z}) then
owl_tech:send_electro(pos,{x=pos.x,y=pos.y+1,z=pos.z})
end
elseif number_side_to_sand ==2 then
if owl_tech:can_send_electro(pos,{x=pos.x,y=pos.y-1,z=pos.z}) then
owl_tech:send_electro(pos,{x=pos.x,y=pos.y-1,z=pos.z})
end
elseif number_side_to_sand ==3 then
if owl_tech:can_send_electro(pos,{x=pos.x+1,y=pos.y,z=pos.z}) then
owl_tech:send_electro(pos,{x=pos.x+1,y=pos.y,z=pos.z})
end
elseif number_side_to_sand ==4 then
if owl_tech:can_send_electro(pos,{x=pos.x-1,y=pos.y,z=pos.z}) then
owl_tech:send_electro(pos,{x=pos.x-1,y=pos.y,z=pos.z})
end
elseif number_side_to_sand ==5 then
if owl_tech:can_send_electro(pos,{x=pos.x,y=pos.y,z=pos.z+1}) then
owl_tech:send_electro(pos,{x=pos.x,y=pos.y,z=pos.z+1})
end
elseif number_side_to_sand ==6 then
if owl_tech:can_send_electro(pos,{x=pos.x,y=pos.y,z=pos.z-1}) then
owl_tech:send_electro(pos,{x=pos.x,y=pos.y,z=pos.z-1})
end
elseif number_side_to_sand ==-1 then
return
end
end