Implement RE-Battery, generator fixes

This commit is contained in:
mrkubax10 2023-03-03 22:28:05 +01:00
parent 5ffa07e59b
commit 3671e96a18
6 changed files with 114 additions and 30 deletions

36
api.lua
View File

@ -23,7 +23,7 @@ industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig)
meta:set_string("industrialtest.ioConfig",ioConfig)
end
industrialtest.api.hasPowerStorage=function(meta)
local values={"industrialtest.powerCapacity","industrialtest.powerFlow","industrialtest.powerAmount"}
local values={"industrialtest.powerCapacity","industrialtest.powerFlow","industrialtest.powerAmount","industrialtest.ioConfig"}
for _,value in ipairs(values) do
if not meta:contains(value) then
return false
@ -45,6 +45,33 @@ industrialtest.api.addPower=function(meta,amount)
meta:set_int("industrialtest.powerAmount",powerAmount+amount)
return amount
end
industrialtest.api.addPowerToItem=function(itemstack,amount)
local meta=itemstack:get_meta()
if not industrialtest.api.hasPowerStorage(meta) then
return 0
end
local added=industrialtest.api.addPower(meta,amount)
itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65535)
return added
end
industrialtest.api.transferPower=function(srcMeta,destMeta,amount)
local currentFlow=math.min(srcMeta:get_int("industrialtest.powerAmount"),amount)
if currentFlow==0 then
return 0
end
local actualFlow=industrialtest.api.addPower(destMeta,currentFlow)
srcMeta:set_int("industrialtest.powerAmount",srcMeta:get_int("industrialtest.powerAmount")-actualFlow)
return actualFlow
end
industrialtest.api.transferPowerToItem=function(srcMeta,itemstack,amount)
local currentFlow=math.min(srcMeta:get_int("industrialtest.powerAmount"),amount)
if currentFlow==0 then
return 0
end
local actualFlow=industrialtest.api.addPowerToItem(itemstack,currentFlow)
srcMeta:set_int("industrialtest.powerAmount",srcMeta:get_int("industrialtest.powerAmount")-actualFlow)
return actualFlow
end
industrialtest.api.powerFlow=function(pos)
local meta=minetest.get_meta(pos)
if not industrialtest.api.hasPowerStorage(meta) then
@ -78,11 +105,6 @@ industrialtest.api.powerFlow=function(pos)
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)
industrialtest.api.transferPower(meta,value,powerDistribution)
end
end

View File

@ -632,6 +632,7 @@ elseif industrialtest.mtgAvailable then
industrialtest.elementKeys.tinIngot="default:tin_ingot"
industrialtest.elementKeys.bronzeIngot="default:bronze_ingot"
industrialtest.elementKeys.stick="default:stick"
industrialtest.elementKeys.powerCarrier="default:mese_crystal_fragment"
else
error("No compatible games found!")
end

42
craftitems.lua Normal file
View File

@ -0,0 +1,42 @@
-- 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/>.
local S=minetest.get_translator("industrialtest")
-- Power storage items
minetest.register_tool("industrialtest:re_battery",{
description=S("RE-Battery"),
inventory_image="industrialtest_re_battery.png"
})
minetest.register_craft({
type="shaped",
output="industrialtest:re_battery",
recipe={
-- TODO: Change default:tin_ingot to copper cable when it will be added
{"",industrialtest.elementKeys.tinIngot,""},
{industrialtest.elementKeys.tinIngot,industrialtest.elementKeys.powerCarrier,industrialtest.elementKeys.tinIngot},
{industrialtest.elementKeys.tinIngot,industrialtest.elementKeys.powerCarrier,industrialtest.elementKeys.tinIngot}
}
})
-- Item callbacks
minetest.register_on_craft(function(itemstack,player,oldCraftGrid,craftInv)
if itemstack:get_name()=="industrialtest:re_battery" then
itemstack:set_wear(65535)
local meta=itemstack:get_meta()
industrialtest.api.addPowerStorage(meta,7000,100,"n/a")
end
end)

View File

@ -25,3 +25,4 @@ dofile(modpath.."/compatibility.lua")
dofile(modpath.."/api.lua")
dofile(modpath.."/minerals.lua")
dofile(modpath.."/machines.lua")
dofile(modpath.."/craftitems.lua")

View File

@ -24,7 +24,8 @@ local function generatorFormspec(fuelPercent,charged)
"label[0.5,0.5;"..S("Generator").."]",
industrialtest.formspecList("context","charged",4.9,1.8,1,1),
"listring[context;charged]",
"image[4.9,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]",
(fuelPercent>0 and "image[4.9,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]"
or "image[4.9,2.8;1,1;default_furnace_fire_bg.png]"),
industrialtest.formspecList("context","fuel",4.9,3.9,1,1),
"listring[context;fuel]",
"box[9,1;0.3,4.8;#202020]",
@ -59,40 +60,57 @@ local definition={
inv:set_size("fuel",1)
meta:set_string("formspec",generatorFormspec(0,0))
meta:set_int("fuelTime",0)
industrialtest.api.addPowerStorage(meta,5000,10,"oooooo")
meta:set_int("maxFuelTime",1)
industrialtest.api.addPowerStorage(meta,7000,100,"oooooo")
end,
on_timer=function(pos,elapsed)
local meta=minetest.get_meta(pos)
local powerFlow=meta:get_int("industrialtest.powerFlow")
local inv=meta:get_inventory()
local fuelTime=meta:get_int("fuelTime")
local fuelList=inv:get_list("fuel")
local chargedList=inv:get_list("charged")
if fuelTime==0 and not industrialtest.api.isFullyCharged(meta) then
local fuel,after=minetest.get_craft_result({
local chargedSlot=inv:get_stack("charged",1)
local fuelSlot=inv:get_stack("fuel",1)
local shouldUpdateFormspec=false
local shouldRerunTimer=false
if chargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0 then
industrialtest.api.transferPowerToItem(meta,chargedSlot,powerFlow)
inv:set_stack("charged",1,chargedSlot)
shouldUpdateFormspec=true
shouldRerunTimer=true
end
if fuelSlot:get_count()>0 and meta:get_int("fuelTime")<=0 and not industrialtest.api.isFullyCharged(meta) then
local output,after=minetest.get_craft_result({
method="fuel",
width=1,
items=fuelList
items={fuelSlot}
})
if fuel.time>0 then
if output.time>0 then
meta:set_int("fuelTime",output.time)
meta:set_int("maxFuelTime",output.time)
inv:set_stack("fuel",1,after.items[1])
fuelTime=fuel.time
meta:set_int("maxFuelTime",fuelTime)
else
return false
end
end
fuelTime=fuelTime-elapsed
if fuelTime<0 then
fuelTime=0
if meta:get_int("fuelTime")>0 then
meta:set_int("fuelTime",meta:get_int("fuelTime")-elapsed)
industrialtest.api.addPower(meta,200)
shouldUpdateFormspec=true
shouldRerunTimer=true
end
meta:set_int("fuelTime",fuelTime)
industrialtest.api.addPower(meta,100)
meta:set_string("formspec",generatorFormspec(fuelTime/meta:get_int("maxFuelTime")*100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")))
industrialtest.api.powerFlow(pos)
if fuelTime>0 or fuelList[1]:get_count()>0 then
return true
if shouldUpdateFormspec then
meta:set_string("formspec",generatorFormspec(meta:get_int("fuelTime")/meta:get_int("maxFuelTime")*100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")))
end
return false
return shouldRerunTimer
end,
allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count,player)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local movedItemStack=inv:get_list(fromList)[1]
if toList=="charged" and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then
return 0
end
return count
end,
allow_metadata_inventory_put=function(pos,listname,index,stack,player)
if listname=="charged" and not industrialtest.api.hasPowerStorage(stack:get_meta()) then

Binary file not shown.

After

Width:  |  Height:  |  Size: 658 B