130 lines
4.2 KiB
Lua
130 lines
4.2 KiB
Lua
|
-- IndustrialTest
|
||
|
-- Copyright (C) 2024 mrkubax10
|
||
|
|
||
|
-- This program is free software: you can redistribute it and/or modify
|
||
|
-- it under the terms of the GNU General Public License as published by
|
||
|
-- the Free Software Foundation, either version 3 of the License, or
|
||
|
-- (at your option) any later version.
|
||
|
|
||
|
-- This program is distributed in the hope that it will be useful,
|
||
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
-- GNU General Public License for more details.
|
||
|
|
||
|
-- You should have received a copy of the GNU General Public License
|
||
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
industrialtest.ElectricMachine=table.copy(industrialtest.Machine)
|
||
|
|
||
|
function industrialtest.ElectricMachine.onConstruct(self,pos)
|
||
|
local meta=minetest.get_meta(pos)
|
||
|
|
||
|
industrialtest.api.addPowerStorage(meta,self.capacity,self.flow,self.ioConfig)
|
||
|
|
||
|
if self.hasPowerInput then
|
||
|
local connections=industrialtest.api.getConnections(pos)
|
||
|
for _,conn in ipairs(connections) do
|
||
|
local connectionMeta=minetest.get_meta(conn)
|
||
|
if industrialtest.api.isNetworkMaster(connectionMeta) then
|
||
|
industrialtest.api.createNetworkMapForNode(conn)
|
||
|
minetest.get_node_timer(conn):start(industrialtest.updateDelay)
|
||
|
else
|
||
|
local def=minetest.registered_nodes[minetest.get_node(conn).name]
|
||
|
if def.groups._industrialtest_cable then
|
||
|
local networks=industrialtest.api.isAttachedToNetwork(connectionMeta)
|
||
|
if networks then
|
||
|
for _,network in ipairs(networks) do
|
||
|
industrialtest.api.createNetworkMapForNode(network)
|
||
|
minetest.get_node_timer(network):start(industrialtest.updateDelay)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
if self.hasPowerOutput then
|
||
|
meta:set_string("industrialtest.network",minetest.serialize(industrialtest.api.createNetworkMap(pos)))
|
||
|
end
|
||
|
end
|
||
|
|
||
|
industrialtest.Machine.onConstruct(self,pos)
|
||
|
end
|
||
|
|
||
|
function industrialtest.ElectricMachine.onDestruct(self,pos)
|
||
|
local meta=minetest.get_meta(pos)
|
||
|
|
||
|
if industrialtest.api.isNetworkMaster(meta) then
|
||
|
local network=industrialtest.api.createNetworkMap(pos,true)
|
||
|
for _,endpoint in ipairs(network) do
|
||
|
local endpointMeta=minetest.get_meta(endpoint.position)
|
||
|
local networks=industrialtest.api.isAttachedToNetwork(endpointMeta)
|
||
|
for key,value in ipairs(networks) do
|
||
|
if value.x==pos.x and value.y==pos.y and value.z==pos.z then
|
||
|
table.remove(networks,key)
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
endpointMeta:set_string("industrialtest.networks",minetest.serialize(networks))
|
||
|
end
|
||
|
end
|
||
|
local networks=industrialtest.api.isAttachedToNetwork(meta)
|
||
|
if networks then
|
||
|
for _,network in ipairs(networks) do
|
||
|
industrialtest.api.removeNodeFromNetwork(network,pos)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
-- don't call onDestruct from parent class because it doesn't do anything
|
||
|
end
|
||
|
|
||
|
function industrialtest.ElectricMachine.onTimer(self,pos,elapsed)
|
||
|
local result=industrialtest.Machine.onTimer(self,pos,elapsed)
|
||
|
self:requestPower(pos)
|
||
|
return result
|
||
|
end
|
||
|
|
||
|
function industrialtest.Machine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
|
||
|
local found=false
|
||
|
if self.powerLists then
|
||
|
for _,value in ipairs(self.powerLists) do
|
||
|
if value==toList then
|
||
|
found=true
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
if found and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then
|
||
|
return 0
|
||
|
end
|
||
|
|
||
|
return industrialtest.Machine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
|
||
|
end
|
||
|
|
||
|
function industrialtest.Machine.allowMetadataInventoryPut(self,pos,listname,index,stack,player)
|
||
|
local found=false
|
||
|
if self.powerLists then
|
||
|
for _,value in ipairs(self.powerLists) do
|
||
|
if value==listname then
|
||
|
found=true
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
if found and not industrialtest.api.hasPowerStorage(stack:get_meta()) then
|
||
|
return 0
|
||
|
end
|
||
|
|
||
|
return industrialtest.Machine.allowMetadataInventoryPut(self,pos,listname,index,stack,player)
|
||
|
end
|
||
|
|
||
|
function industrialtest.ElectricMachine.requestPower(self,pos)
|
||
|
local meta=minetest.get_meta(pos)
|
||
|
if self.hasPowerInput and not industrialtest.api.isFullyCharged(meta) then
|
||
|
local networks=industrialtest.api.isAttachedToNetwork(meta)
|
||
|
if networks then
|
||
|
for _,network in ipairs(networks) do
|
||
|
minetest.get_node_timer(network):start(industrialtest.updateDelay)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|