industrialtest/machines.lua

178 lines
6.2 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)
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]",
2023-03-03 22:28:05 +01:00
(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),
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"),
2023-03-04 20:52:40 +01:00
tiles={
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_iron_furnace_front.png",
"industrialtest_machine_block.png"
},
paramtype2="facedir",
legacy_facedir_simple=true,
2023-03-01 12:16:33 +01:00
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)
2023-03-03 22:28:05 +01:00
meta:set_int("maxFuelTime",1)
industrialtest.api.addPowerStorage(meta,7000,100,"oooooo")
2023-03-01 12:16:33 +01:00
end,
on_timer=function(pos,elapsed)
local meta=minetest.get_meta(pos)
2023-03-03 22:28:05 +01:00
local powerFlow=meta:get_int("industrialtest.powerFlow")
2023-03-01 12:16:33 +01:00
local inv=meta:get_inventory()
2023-03-03 22:28:05 +01:00
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({
2023-03-01 19:29:20 +01:00
method="fuel",
width=1,
2023-03-03 22:28:05 +01:00
items={fuelSlot}
2023-03-01 19:29:20 +01:00
})
2023-03-03 22:28:05 +01:00
if output.time>0 then
meta:set_int("fuelTime",output.time)
meta:set_int("maxFuelTime",output.time)
2023-03-01 19:29:20 +01:00
inv:set_stack("fuel",1,after.items[1])
2023-03-01 12:16:33 +01:00
end
end
2023-03-03 22:28:05 +01:00
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
2023-03-01 12:16:33 +01:00
end
2023-03-03 22:28:05 +01:00
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
2023-03-01 12:16:33 +01:00
end
2023-03-03 22:28:05 +01:00
return count
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)
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}
}
})