Add powered tools, make them charge and dischargable in the power cell

This commit is contained in:
Evert Prants 2018-06-18 11:39:17 +03:00
parent de2c0a15ea
commit baff673cb8
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
7 changed files with 192 additions and 18 deletions

View File

@ -1,4 +1,8 @@
local function item_in_group(stack, grp)
return minetest.get_item_group(stack:get_name(), grp) > 0
end
function elepm.register_storage(nodename, nodedef)
local levels = nodedef.ele_levels or 8
local level_overlay = nodedef.ele_overlay or "elepower_power_level_"
@ -11,7 +15,8 @@ function elepm.register_storage(nodename, nodedef)
nodedef.groups["ele_provider"] = 1
nodedef.on_timer = function (pos, elapsed)
local meta = minetest.get_meta(pos)
local meta = minetest.get_meta(pos)
local refresh = false
local capacity = ele.helpers.get_node_property(meta, pos, "capacity")
local storage = ele.helpers.get_node_property(meta, pos, "storage")
@ -24,14 +29,80 @@ function elepm.register_storage(nodename, nodedef)
meta:set_string("formspec", ele.formspec.get_storage_formspec(rounded))
meta:set_string("infotext", ("%s Active"):format(nodedef.description))
return false
local inv = meta:get_inventory()
-- Powercell to item
local itemcharge = inv:get_stack("out", 1)
local output = ele.helpers.get_node_property(meta, pos, "output")
if itemcharge and not itemcharge:is_empty() and item_in_group(itemcharge, "ele_tool") then
local crg = ele.tools.get_tool_property(itemcharge, "storage")
local cap = ele.tools.get_tool_property(itemcharge, "capacity")
local tmeta = itemcharge:get_meta()
local append = 0
if crg + output < cap then
append = output
else
if crg <= cap then
append = cap - crg
end
end
if storage > append and append ~= 0 then
crg = crg + append
storage = storage - append
refresh = true
end
tmeta:set_int("storage", crg)
itemcharge = ele.tools.update_tool_wear(itemcharge)
inv:set_stack("out", 1, itemcharge)
end
-- Item to powercell
local itemdischarge = inv:get_stack("in", 1)
local inrush = ele.helpers.get_node_property(meta, pos, "inrush")
if itemdischarge and not itemdischarge:is_empty() and
(item_in_group(itemdischarge, "ele_tool") or item_in_group(itemdischarge, "ele_machine")) then
local crg = ele.tools.get_tool_property(itemdischarge, "storage")
local tmeta = itemdischarge:get_meta()
local discharge = 0
if crg >= inrush then
discharge = inrush
else
discharge = inrush - crg
end
if storage + discharge > capacity then
discharge = capacity - storage
end
if discharge <= crg and discharge ~= 0 then
crg = crg - discharge
storage = storage + discharge
refresh = true
end
tmeta:set_int("storage", crg)
itemdischarge = ele.tools.update_tool_wear(itemdischarge)
inv:set_stack("in", 1, itemdischarge)
end
if refresh then
meta:set_int("storage", storage)
end
return refresh
end
nodedef.on_construct = function (pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("src", 1)
inv:set_size("dst", 1)
inv:set_size("out", 1)
inv:set_size("in", 1)
meta:set_string("formspec", ele.formspec.get_storage_formspec(0))
end

View File

@ -52,14 +52,16 @@ function ele.formspec.get_storage_formspec(power)
default.gui_bg_img..
default.gui_slots..
ele.formspec.power_meter(power)..
"list[context;src;2,1.5;1,1;]"..
"list[context;dst;5,1.5;1,1;]"..
"image[2,0.5;1,1;gui_furnace_arrow_bg.png^[transformR180]"..
"list[context;out;2,1.5;1,1;]"..
"image[5,0.5;1,1;gui_furnace_arrow_bg.png]"..
"list[context;in;5,1.5;1,1;]"..
"list[current_player;main;0,4.25;8,1;]"..
"list[current_player;main;0,5.5;8,3;8]"..
"listring[current_player;main]"..
"listring[context;src]"..
"listring[context;out]"..
"listring[current_player;main]"..
"listring[context;dst]"..
"listring[context;in]"..
"listring[current_player;main]"..
default.get_hotbar_bg(0, 4.25)
end

View File

@ -155,7 +155,7 @@ local function give_node_power(pos, available)
end
if total_add + storage > capacity then
total_add = (total_add + storage) - capacity
total_add = capacity - storage
end
if storage >= capacity then

View File

@ -1,6 +0,0 @@
dofile(ele.modpath..'/power/network.lua')
dofile(ele.modpath..'/power/formspec.lua')
dofile(ele.modpath..'/power/machine.lua')
dofile(ele.modpath..'/power/conductor.lua')
dofile(ele.modpath..'/power/tool.lua')

View File

@ -0,0 +1,95 @@
ele.tools = {}
-- Get a tool property
function ele.tools.get_tool_property(itemstack, param)
local meta = itemstack:get_meta()
local value = meta:get_int(param)
if not value or value == 0 then
local itemdef = minetest.registered_items[itemstack:get_name()]
local dparam = itemdef["ele_" .. param]
if dparam then
value = dparam
end
end
return value
end
-- Set tool capabilities based on wear
function ele.tools.groupcaps(itemstack)
local wear = itemstack:get_wear()
local meta = itemstack:get_meta()
local itemdef = minetest.registered_items[itemstack:get_name()]
local toolcaps = itemdef.tool_capabilities
if wear == 65535 then
meta:set_tool_capabilities({})
else
meta:set_tool_capabilities(nil)
end
return itemstack
end
function ele.tools.update_tool_wear(itemstack)
local capacity = ele.tools.get_tool_property(itemstack, "capacity")
local storage = ele.tools.get_tool_property(itemstack, "storage")
local meta = itemstack:get_meta()
local itemdef = minetest.registered_items[itemstack:get_name()]
local percent = storage / capacity
local wear = math.floor((1-percent) * 65535)
meta:set_string("description", itemdef.description .. "\nCharge: " ..
storage .. "/" .. capacity .. " " .. ele.unit)
itemstack:set_wear(wear)
itemstack = ele.tools.groupcaps(itemstack)
return itemstack
end
function ele.register_tool(toolname, tooldef)
if not tooldef.groups then
tooldef.groups = {}
end
tooldef.groups["ele_tool"] = 1
-- Start cleaning up the tooldef
local defaults = {
ele_capacity = 1600,
ele_usage = 64,
}
-- Ensure everything that's required is present
for k,v in pairs(defaults) do
if not tooldef[k] then
tooldef[k] = v
end
end
tooldef.after_use = function (itemstack, user, node, digparams)
local meta = itemstack:get_meta()
local storage = ele.tools.get_tool_property(itemstack, "storage")
if digparams.wear == 0 then
return itemstack
end
storage = storage - math.floor(digparams.wear / 16)
if storage < 0 then
storage = 0
end
meta:set_int("storage", storage)
itemstack = ele.tools.update_tool_wear(itemstack)
return itemstack
end
minetest.register_tool(toolname, tooldef)
end

View File

@ -3,6 +3,18 @@
local modpath = minetest.get_modpath(minetest.get_current_modname())
elepd = rawget(_G, "elepd") or {}
elepd.modpath = modpath
eletool = rawget(_G, "eletool") or {}
eletool.modpath = modpath
ele.register_tool("elepower_tools:test_tool", {
description = "Powertool",
inventory_image = "elepower_tool_ironpick.png",
tool_capabilities = {
full_punch_interval = 1.0,
max_drop_level=1,
groupcaps={
cracky = {times={[1]=3.90, [2]=1.50, [3]=0.60}, maxlevel=2},
},
damage_groups = {fleshy=4},
},
})

View File

@ -1,3 +1,3 @@
name = elepower_tools
description = Elepower powered tools!
depends = elepower_machines
depends = elepower_papi