industrialtest/machines.lua

124 lines
4.3 KiB
Lua
Raw Normal View History

2023-03-01 12:16:33 +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/>.
local S=minetest.get_translator("industrialtest")
-- Generators
2023-03-01 19:29:20 +01:00
local function generatorFormspec(fuelPercent,charged)
minetest.chat_send_all(charged)
2023-03-01 12:16:33 +01:00
local formspec={
industrialtest.formspecVersion(),
2023-03-01 12:16:33 +01:00
"size[10.8,12]",
"label[0.5,0.5;"..S("Generator").."]",
industrialtest.formspecList("context","charged",4.9,1.8,1,1),
2023-03-01 12:16:33 +01:00
"listring[context;charged]",
"image[4.9,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]",
industrialtest.formspecList("context","fuel",4.9,3.9,1,1),
2023-03-01 12:16:33 +01:00
"listring[context;fuel]",
2023-03-01 19:29:20 +01:00
"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()
2023-03-01 12:16:33 +01:00
}
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
2023-03-01 12:16:33 +01:00
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)
2023-03-01 19:29:20 +01:00
meta:set_string("formspec",generatorFormspec(0,0))
2023-03-01 12:16:33 +01:00
meta:set_int("fuelTime",0)
industrialtest.api.addPowerStorage(meta,5000,10,"oooooo")
end,
on_timer=function(pos,elapsed)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local fuelTime=meta:get_int("fuelTime")
local fuelList=inv:get_list("fuel")
local chargedList=inv:get_list("charged")
2023-03-01 19:29:20 +01:00
if fuelTime==0 and not industrialtest.api.isFullyCharged(meta) then
local fuel,after=minetest.get_craft_result({
method="fuel",
width=1,
items=fuelList
})
if fuel.time>0 then
inv:set_stack("fuel",1,after.items[1])
fuelTime=fuel.time
meta:set_int("maxFuelTime",fuelTime)
else
return false
2023-03-01 12:16:33 +01:00
end
end
fuelTime=fuelTime-elapsed
if fuelTime<0 then
fuelTime=0
end
meta:set_int("fuelTime",fuelTime)
industrialtest.api.addPower(meta,100)
2023-03-01 19:29:20 +01:00
meta:set_string("formspec",generatorFormspec(fuelTime/meta:get_int("maxFuelTime")*100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")))
if fuelTime>0 or fuelList[1]:get_count()>0 then
2023-03-01 12:16:33 +01:00
return true
end
return false
2023-03-01 12:16:33 +01:00
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}
2023-03-01 12:16:33 +01:00
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
2023-03-01 12:16:33 +01:00
end
minetest.register_node("industrialtest:generator",definition)