2023-02-28 20:09:23 +01:00
|
|
|
-- IndustrialTest
|
|
|
|
-- Copyright (C) 2023 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.api={}
|
|
|
|
|
|
|
|
industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig)
|
|
|
|
meta:set_int("industrialtest.powerCapacity",capacity)
|
|
|
|
meta:set_int("industrialtest.powerFlow",flow)
|
|
|
|
meta:set_int("industrialtest.powerAmount",0)
|
|
|
|
meta:set_string("industrialtest.ioConfig",ioConfig)
|
|
|
|
end
|
|
|
|
industrialtest.api.hasPowerStorage=function(meta)
|
|
|
|
local values={"industrialtest.powerCapacity","industrialtest.powerFlow","industrialtest.powerAmount"}
|
|
|
|
for _,value in ipairs(values) do
|
|
|
|
if not meta:contains(value) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2023-03-01 12:16:33 +01:00
|
|
|
industrialtest.api.isFullyCharged=function(meta)
|
|
|
|
return meta:get_int("industrialtest.powerAmount")>=meta:get_int("industrialtest.powerCapacity")
|
|
|
|
end
|
2023-02-28 20:09:23 +01:00
|
|
|
industrialtest.api.addPower=function(meta,amount)
|
|
|
|
local powerAmount=meta:get_int("industrialtest.powerAmount")
|
|
|
|
local powerCapacity=meta:get_int("industrialtest.powerCapacity")
|
|
|
|
if powerAmount+amount>powerCapacity then
|
|
|
|
local addedAmount=powerCapacity-powerAmount
|
|
|
|
meta:set_int("industrialtest.powerAmount",powerCapacity)
|
|
|
|
return addedAmount
|
|
|
|
end
|
|
|
|
meta:set_int("industrialtest.powerAmount",powerAmount+amount)
|
|
|
|
return amount
|
|
|
|
end
|
|
|
|
industrialtest.api.powerFlow=function(pos)
|
|
|
|
local meta=minetest.get_meta(pos)
|
|
|
|
if not industrialtest.api.hasPowerStorage(meta) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local neighbourPositions={
|
|
|
|
vector.offset(pos,-1,0,0),
|
|
|
|
vector.offset(pos,1,0,0),
|
|
|
|
vector.offset(pos,0,-1,0),
|
|
|
|
vector.offset(pos,0,1,0),
|
|
|
|
vector.offset(pos,0,0,-1),
|
2023-03-01 12:16:33 +01:00
|
|
|
vector.offset(pos,0,0,1)
|
2023-02-28 20:09:23 +01:00
|
|
|
}
|
|
|
|
local neighbours={}
|
|
|
|
for key,value in ipairs(neighbourPositions) do
|
|
|
|
neighbours[key]=minetest.get_meta(value)
|
|
|
|
end
|
|
|
|
local neighboursContainingPower=0
|
|
|
|
for key,value in ipairs(neighbours) do
|
|
|
|
if industrialtest.api.hasPowerStorage(value) then
|
|
|
|
neighboursContainingPower=neighboursContainingPower+1
|
|
|
|
else
|
|
|
|
neighbourPositions[key]=nil
|
|
|
|
neighbours[key]=nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if neighboursContainingPower==0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local powerFlow=meta:get_int("industrialtest.powerFlow")
|
|
|
|
local powerDistribution=math.floor(powerFlow/neighboursContainingPower)
|
|
|
|
-- TODO: if supplying machine power flow is too large for receiving machine to handle then that machine should explode
|
|
|
|
for _,value in ipairs(neighbours) do
|
|
|
|
local currentFlow=math.min(meta:get_int("industrialtest.powerAmount"),powerDistribution)
|
|
|
|
if currentFlow==0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
local actualFlow=industrialtest.api.addPower(value,currentFlow)
|
|
|
|
meta:set_int("industrialtest.powerAmount",meta:get_int("industrialtest.powerAmount")-actualFlow)
|
|
|
|
end
|
|
|
|
end
|