industrialtest/machines.lua

169 lines
5.9 KiB
Lua

-- 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")
-- Generators
local function generatorFormspec(fuelPercent,charged)
local formspec={
industrialtest.formspecVersion(),
"size[10.8,12]",
"label[0.5,0.5;"..S("Generator").."]",
industrialtest.formspecList("context","charged",4.9,1.8,1,1),
"listring[context;charged]",
(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]",
(charged>0 and "box[9,"..(1+4.8-(charged*4.8))..";0.3,"..(charged*4.8)..";#FF1010]" or ""),
industrialtest.formspecPlayerInv()
}
return table.concat(formspec,"")
end
local function generatorMclAfterDigNode(pos,oldnode,oldmeta,digger)
-- Taken from https://git.minetest.land/MineClone2/MineClone2/src/branch/master/mods/ITEMS/mcl_furnaces/init.lua#L538
local meta=minetest.get_meta(pos)
local meta2=meta
meta:from_table(oldmeta)
local inv=meta:get_inventory()
for _,listname in ipairs({"src", "dst", "fuel"}) do
local stack=inv:get_stack(listname,1)
if not stack:is_empty() then
local p = {x=pos.x+math.random(0, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5}
minetest.add_item(p, stack)
end
end
meta:from_table(meta2:to_table())
end
local definition={
description=S("Generator"),
tiles={"industrialtest_iron_furnace.png"},
paramtype2="4dir",
on_construct=function(pos,placer)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
inv:set_size("charged",1)
inv:set_size("fuel",1)
meta:set_string("formspec",generatorFormspec(0,0))
meta:set_int("fuelTime",0)
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 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={fuelSlot}
})
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])
end
end
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
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 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
return 0
end
return stack:get_count()
end,
on_metadata_inventory_put=function(pos,listname,index,stack,player)
minetest.get_node_timer(pos):start(1.0)
end,
on_metadata_inventory_take=function(pos,listname,index,stack,player)
minetest.get_node_timer(pos):start(1.0)
end
}
if industrialtest.mtgAvailable then
definition.groups={cracky=2}
definition.sounds=default.node_sound_metal_defaults()
definition.can_dig=function(pos)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
return not (inv:get_list("charged")[1]:get_count()>0 or inv:get_list("fuel")[1]:get_count()>0)
end
elseif industrialtest.mclAvailable then
definition.after_dig_node=generatorMclAfterDigNode
definition._mcl_blast_resistance=3.5
definition._mcl_hardness=3.9
end
minetest.register_node("industrialtest:generator",definition)
minetest.register_craft({
type="shaped",
output="industrialtest:generator",
recipe={
{"industrialtest:re_battery","",""},
{"industrialtest:machine_block","",""},
{industrialtest.elementKeys.furnace,"",""}
}
})
minetest.register_craft({
type="shaped",
output="industrialtest:generator",
recipe={
{"","industrialtest:re_battery",""},
{"","industrialtest:machine_block",""},
{"",industrialtest.elementKeys.furnace,""}
}
})
minetest.register_craft({
type="shaped",
output="industrialtest:generator",
recipe={
{"","","industrialtest:re_battery"},
{"","","industrialtest:machine_block"},
{"","",industrialtest.elementKeys.furnace}
}
})