Support for RE batteries for fuel

master
h-v-smacker 2017-10-10 22:02:35 +03:00
parent 19a0f193ad
commit 6290e57d8f
12 changed files with 411 additions and 17 deletions

View File

@ -36,6 +36,7 @@ function DigtronLayout.create(pos, player)
self.all = {}
self.inventories = {}
self.fuelstores = {}
self.battery_holders = {}
self.diggers = {}
self.builders = {}
self.extents = {}
@ -120,6 +121,8 @@ function DigtronLayout.create(pos, player)
elseif group_number == 6 then
table.insert(self.inventories, node_image)
table.insert(self.fuelstores, node_image)
elseif group_number == 7 then
table.insert(self.battery_holders, node_image)
end
if is_protected then

View File

@ -52,3 +52,8 @@ setting("float", "dig_cost_crumbly", 0.5, "Crumbly dig cost")
setting("float", "dig_cost_choppy", 0.75, "Choppy dig cost")
-- how much fuel is required to build a node
setting("float", "build_cost", 1.0, "Build cost")
-- How much charge (an RE battery holds 10000 EU) is equivalent to 1 unit of heat produced by burning coal
-- With 100, the battery is 2.5 better than a coal lump, yet 3.7 less powerful than a coal block
-- Being rechargeable should pay off for this "average" performance.
setting("int", "power_ratio", 100, "The electrical charge to 1 coal heat unit conversion ratio")

View File

@ -4,4 +4,5 @@ doc?
hopper?
awards?
catacomb?
intllib?
intllib?
technic?

20
doc.lua
View File

@ -74,6 +74,26 @@ if pipeworks_enabled then
S("Fuel modules are compatible with Pipeworks blocks. When a Digtron moves one of the inventory modules adjacent to a pipe it will automatically hook up to it, and disconnect again when it moves on.")
end
-- Battery holders
digtron.doc.batteryholder_longdesc = S("Holds RE batteries to run a Digtron")
digtron.doc.batteryholder_usagehelp = S("Digtrons have an appetite, and it can be satisfied by electricity as well. Build operations and dig operations require a certain amount of power, and that power comes from the batteries place in the holder. Note that movement does not consume charge, only digging and building."
.."\n\n"..
"When a control unit is triggered, it will tally up how much power is required for the next cycle and then discharge the batteries in the battery holder until a sufficient amount of heat has been generated to power the operation. Any leftover heat will be retained by the control unit for use in the next cycle; this is the \"heat remaining in controller furnace\". Thus no power is wasted (unless you dig away a control unit with some heat remaining in it, that heat does get wasted), and the discharged batteries can be taken away to be recharged."
.."\n\n"..
"The fuel costs for digging and building can be configured in the init.lua file. By default using one lump of coal as fuel a digtron can:\n"..
"\tBuild 40 blocks\n"..
"\tDig 40 stone blocks\n"..
"\tDig 60 wood blocks\n"..
"\tDig 80 dirt or sand blocks")
if pipeworks_enabled then
digtron.doc.batteryholder_usagehelp = digtron.doc.batteryholder_usagehelp
.."\n\n"..
S("Fuel modules are compatible with Pipeworks blocks. When a Digtron moves one of the inventory modules adjacent to a pipe it will automatically hook up to it, and disconnect again when it moves on.")
end
digtron.doc.combined_storage_longdesc = S("Stores fuel for a Digtron and also has an inventory for building materials")
digtron.doc.combined_storage_usagehelp = S("For smaller jobs the two dedicated modules may simply be too much of a good thing, wasting precious Digtron space to give unneeded capacity. The combined storage module is the best of both worlds, splitting its internal space between building material inventory and fuel storage. It has 3/4 building material capacity and 1/4 fuel storage capacity.")

View File

@ -31,6 +31,7 @@ dofile( digtron_modpath .. "/class_layout.lua" )
dofile( digtron_modpath .. "/entities.lua" )
dofile( digtron_modpath .. "/nodes/node_misc.lua" ) -- contains structure and light nodes
dofile( digtron_modpath .. "/nodes/node_storage.lua" ) -- contains inventory and fuel storage nodes
dofile( digtron_modpath .. "/nodes/node_battery_holder.lua" ) -- holds rechargeable batteries from the technic mod
dofile( digtron_modpath .. "/nodes/node_diggers.lua" ) -- contains all diggers
dofile( digtron_modpath .. "/nodes/node_builders.lua" ) -- contains all builders (there's just one currently)
dofile( digtron_modpath .. "/nodes/node_controllers.lua" ) -- controllers
@ -47,6 +48,7 @@ dofile( digtron_modpath .. "/upgrades.lua" ) -- various LBMs for upgrading older
-- 4 - builder head, has a "test_build" and "execute_build" method in its definition
-- 5 - fuel-holding digtron, has a "fuel" invetory that the control node can draw fuel items from. Separate from general inventory, nothing gets put here automatically.
-- 6 - holds both fuel and main inventories
-- 7 - holds batteries (RE Battery from technic) to provide clean renewable power
-- This code was added for use with FaceDeer's fork of the [catacomb] mod. Paramat's version doesn't support customized protected nodes, which causes
-- it to "eat" Digtrons sometimes.
@ -54,6 +56,7 @@ if minetest.get_modpath("catacomb") and catacomb ~= nil and catacomb.chamber_pro
local digtron_nodes = {
minetest.get_content_id("digtron:inventory"),
minetest.get_content_id("digtron:fuelstore"),
minetest.get_content_id("digtron:battery_holder"),
minetest.get_content_id("digtron:combined_storage"),
minetest.get_content_id("digtron:axle"),
minetest.get_content_id("digtron:builder"),

View File

@ -0,0 +1,219 @@
-- internationalization boilerplate
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")
-- Battery storage. Controller node draws electrical power from here.
-- Note that batttery boxes are digtron group 7.
local battery_holder_formspec =
"size[8,9.3]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"label[0,0;" .. S("Batteries") .. "]" ..
"list[current_name;batteries;0,0.6;8,4;]" ..
"list[current_player;main;0,5.15;8,1;]" ..
"list[current_player;main;0,6.38;8,3;8]" ..
"listring[current_name;batteries]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,5.15)
minetest.register_node("digtron:battery_holder", {
description = S("Digtron Battery Holder"),
_doc_items_longdesc = digtron.doc.battery_holder_longdesc,
_doc_items_usagehelp = digtron.doc.battery_holder_usagehelp,
_digtron_formspec = battery_holder_formspec,
groups = {cracky = 3, oddly_breakable_by_hand = 3, digtron = 7, tubedevice = 1, tubedevice_receiver = 1},
drop = "digtron:battery_holder",
sounds = digtron.metal_sounds,
paramtype2= "facedir",
drawtype = "nodebox",
paramtype = "light",
is_ground_content = false,
tiles = {
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", battery_holder_formspec)
local inv = meta:get_inventory()
inv:set_size("batteries", 8*4)
end,
-- Only allow RE batteries to be placed in the inventory
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if listname == "batteries" then
local node_name = stack:get_name()
-- Only allow RE batteries from technic mode
if node_name == "technic:battery" then
local meta = stack:get_metadata()
local md = minetest.deserialize(meta)
-- And specifically if they hold any charge
-- Disregard empty batteries, the player should know better
if md.charge > 0 then
return stack:get_count()
else
return 0
end
else
return 0
end
end
return 0
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:is_empty("batteries")
end,
-- Pipeworks compatibility
-- Because who wouldn't send batteries through pipes if he could?
-----------------------------------------------------------------
tube = (function() if minetest.get_modpath("pipeworks") then return {
insert_object = function(pos, node, stack, direction)
if minetest.get_craft_result({method="batteries", width=1, items={stack}}).time ~= 0 then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("batteries", stack)
end
return stack
end,
can_insert = function(pos, node, stack, direction)
if minetest.get_craft_result({method="batteries", width=1, items={stack}}).time ~= 0 then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:room_for_item("batteries", stack)
end
return false
end,
input_inventory = "batteries",
connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
} end end)(),
after_place_node = (function() if minetest.get_modpath("pipeworks") then return pipeworks.after_place end end)(),
after_dig_node = (function() if minetest.get_modpath("pipeworks")then return pipeworks.after_dig end end)()
})
----------------------------- previous version below
--[[
minetest.register_node("digtron:battery_holder", {
description = S("Digtron Battery Holder"),
_doc_items_longdesc = digtron.doc.batteryholder_longdesc,
_doc_items_usagehelp = digtron.doc.batteryholder_usagehelp,
groups = {cracky = 3, oddly_breakable_by_hand=3, digtron = 7, tubedevice = 1, tubedevice_receiver = 1},
drop = "digtron:battery_holder",
sounds = digtron.metal_sounds,
paramtype2= "facedir",
drawtype = "nodebox",
paramtype = "light",
is_ground_content = false,
tiles = {
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",
"size[8,9.3]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"label[0,0;" .. S("Batteries") .. "]" ..
"list[current_name;batteries;0,0.6;8,4;]" ..
"list[current_player;main;0,5.15;8,1;]" ..
"list[current_player;main;0,6.38;8,3;8]" ..
"listring[current_name;batteries]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,5.15)
)
local inv = meta:get_inventory()
inv:set_size("batteries", 8*4)
end,
-- Only allow RE batteries to be placed in the inventory
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if listname == "batteries" then
local node_name = stack:get_name()
-- Only allow RE batteries from technic mode
if node_name == "technic:battery" then
local meta = stack:get_metadata()
-- minetest.chat_send_all(minetest.serialize(meta))
local md = minetest.deserialize(meta)
minetest.chat_send_all("Battery has charge: "..md.charge)
-- And specifically if they hold any charge
if md.charge > 0 then
return stack:get_count()
else
return 0
end
else
return 0
end
end
return 0
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:is_empty("batteries")
end,
-- Pipeworks compatibility
----------------------------------------------------------------
tube = (function() if minetest.get_modpath("pipeworks") then return {
insert_object = function(pos, node, stack, direction)
local node_name = stack:get_name()
if node_name == "technic:battery" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("batteries", stack)
end
return stack
end,
can_insert = function(pos, node, stack, direction)
local node_name = stack:get_name()
if node_name == "technic:battery" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:room_for_item("batteries", stack)
end
return false
end,
input_inventory = "batteries",
connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
} end end)(),
after_place_node = (function() if minetest.get_modpath("pipeworks") then return pipeworks.after_place end end)(),
after_dig_node = (function() if minetest.get_modpath("pipeworks")then return pipeworks.after_dig end end)()
})
]]

View File

@ -205,7 +205,7 @@ minetest.register_node("digtron:soft_digger", {
sounds = digtron.metal_sounds,
paramtype = "light",
paramtype2= "facedir",
is_ground_content = false,
is_ground_content = false,
drawtype="nodebox",
node_box = {
type = "fixed",

View File

@ -21,7 +21,7 @@ local inventory_formspec =
minetest.register_node("digtron:inventory", {
description = S("Digtron Inventory Storage"),
_doc_items_longdesc = digtron.doc.inventory_longdesc,
_doc_items_usagehelp = digtron.doc.inventory_usagehelp,
_doc_items_usagehelp = digtron.doc.inventory_usagehelp,
_digtron_formspec = inventory_formspec,
groups = {cracky = 3, oddly_breakable_by_hand=3, digtron = 2, tubedevice = 1, tubedevice_receiver = 1},
drop = "digtron:inventory",
@ -92,7 +92,7 @@ local fuelstore_formspec =
minetest.register_node("digtron:fuelstore", {
description = S("Digtron Fuel Storage"),
_doc_items_longdesc = digtron.doc.fuelstore_longdesc,
_doc_items_usagehelp = digtron.doc.fuelstore_usagehelp,
_doc_items_usagehelp = digtron.doc.fuelstore_usagehelp,
_digtron_formspec = fuelstore_formspec,
groups = {cracky = 3, oddly_breakable_by_hand=3, digtron = 5, tubedevice = 1, tubedevice_receiver = 1},
drop = "digtron:fuelstore",

View File

@ -90,6 +90,15 @@ minetest.register_craft({
}
})
minetest.register_craft({
output = "digtron:battery_holder",
recipe = {
{"","default:chest",""},
{"","digtron:digtron_core",""},
{"","default:steel_ingot",""}
}
})
minetest.register_craft({
output = "digtron:combined_storage",
recipe = {

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 B

View File

@ -159,8 +159,10 @@ digtron.get_controlling_coordinate = function(pos, facedir)
end
end
-- Searches fuel store inventories for burnable items and burns them until target is reached or surpassed (or there's nothing left to burn). Returns the total fuel value burned
-- if the "test" parameter is set to true, doesn't actually take anything out of inventories. We can get away with this sort of thing for fuel but not for builder inventory because there's just one
-- Searches fuel store inventories for burnable items and burns them until target is reached or surpassed
-- (or there's nothing left to burn). Returns the total fuel value burned
-- if the "test" parameter is set to true, doesn't actually take anything out of inventories.
-- We can get away with this sort of thing for fuel but not for builder inventory because there's just one
-- controller node burning stuff, not multiple build heads drawing from inventories in turn. Much simpler.
digtron.burn = function(fuelstore_positions, target, test)
local current_burned = 0
@ -195,6 +197,91 @@ digtron.burn = function(fuelstore_positions, target, test)
return current_burned
end
-- Consume energy from the batteries
-- The same as burning coal, except that instead of destroying the items in the inventory, we merely drain
-- the charge in them, leaving them empty. The charge is converted into "coal heat units" by a downscaling
-- factor, since if taken at face value (10000 EU), the batteries would be the ultimate power source barely
-- ever needing replacement.
digtron.tap_batteries = function(battery_positions, target, test)
local current_burned = 0
-- 1 coal block is 370 PU
-- 1 coal lump is 40 PU
-- An RE battery holds 10000 EU of charge
-- local power_ratio = 100 -- How much charge equals 1 unit of PU from coal
-- setting Moved to digtron.config.power_ratio
if (battery_positions == nil) then
return 0
end
for k, location in pairs(battery_positions) do
if current_burned > target then
break
end
local inv = minetest.get_inventory({type="node", pos=location.pos})
local invlist = inv:get_list("batteries")
if (invlist == nil) then
break
end
for i, itemstack in pairs(invlist) do
local meta = minetest.deserialize(itemstack:get_metadata())
if (meta =~ nil) then
local power_available = math.floor(meta.charge / digtron.config.power_ratio)
minetest.chat_send_all("Charge reported: "..meta.charge.." which is enough for "..power_available.." power")
if power_available ~= 0 then
local actual_burned = power_available -- we just take all we have from the battery, since they aren't stackable
if test ~= true then
-- don't bother recording the items if we're just testing, nothing is actually being removed.
local charge_left = meta.charge - power_available * digtron.config.power_ratio
local properties = itemstack:get_tool_capabilities()
-- itemstack = technic.set_RE_wear(itemstack, charge_left, properties.groupcaps.fleshy.uses)
-- we only need half the function, so why bother using it in the first place
-- Charge is stored separately, but shown as wear level
-- This calls for recalculating the value.
local charge_level
if charge_left == 0 then
charge_level = 0
else
charge_level = 65536 - math.floor(charge_left / properties.groupcaps.fleshy.uses * 65535)
if charge_level > 65535 then charge_level = 65535 end
if charge_level < 1 then charge_level = 1 end
end
itemstack:set_wear(charge_level)
meta.charge = charge_left
itemstack:set_metadata(minetest.serialize(meta))
end
current_burned = current_burned + actual_burned
end
end
if current_burned > target then
break
end
end
if (current_burned == 0) then
minetest.chat_send_all("Batteries not found!")
end
if test ~= true then
-- only update the list if we're doing this for real.
inv:set_list("batteries", invlist)
end
end
return current_burned
end
digtron.remove_builder_item = function(pos)
local objects = minetest.env:get_objects_inside_radius(pos, 0.5)
if objects ~= nil then

View File

@ -108,6 +108,7 @@ digtron.execute_dig_cycle = function(pos, clicker)
local dir = minetest.facedir_to_dir(facing)
local fuel_burning = meta:get_float("fuel_burning") -- get amount of burned fuel left over from last cycle
local status_text = S("Heat remaining in controller furnace: @1", math.max(0, fuel_burning))
local exhaust = meta:get_int("on_coal")
local layout = DigtronLayout.create(pos, clicker)
@ -206,8 +207,20 @@ digtron.execute_dig_cycle = function(pos, clicker)
local test_fuel_needed = test_build_fuel_cost + digging_fuel_cost - fuel_burning
local test_fuel_burned = 0
-- if test_fuel_needed > 0 then
-- test_fuel_burned = digtron.burn(layout.fuelstores, test_fuel_needed, true)
-- end
if test_fuel_needed > 0 then
test_fuel_burned = digtron.burn(layout.fuelstores, test_fuel_needed, true)
-- check for the available electrical power
test_fuel_burned = digtron.tap_batteries(layout.battery_holders, test_fuel_needed, true)
end
if (test_fuel_needed < test_fuel_burned) then
exhaust = 0 -- all power needs met by electricity, don't blow smoke
else
-- burn combustible fuel if not enough power
test_fuel_burned = test_fuel_burned + digtron.burn(layout.fuelstores, test_fuel_needed - test_fuel_burned, true)
exhaust = 1 -- burning fuel produces smoke
end
--Put everything back where it came from
@ -303,16 +316,32 @@ digtron.execute_dig_cycle = function(pos, clicker)
status_text = S("Digtron unexpectedly failed to execute one or more build operations, likely due to an inventory error.") .. "\n"
end
-- acutally burn the fuel needed
local fuel_cost = digging_fuel_cost + building_fuel_cost
fuel_burning = fuel_burning - fuel_cost
if digtron.config.particle_effects then
table.insert(particle_systems, burn_smoke(pos, fuel_cost))
-- -- acutally burn the fuel needed
-- local fuel_cost = digging_fuel_cost + building_fuel_cost
-- fuel_burning = fuel_burning - fuel_cost
-- if digtron.config.particle_effects then
-- table.insert(particle_systems, burn_smoke(pos, fuel_cost))
-- end
-- if fuel_burning < 0 then
-- fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
-- end
-- actually burn the fuel needed
fuel_burning = fuel_burning - digging_fuel_cost
if digtron.particle_effects and exhaust == 1 then
table.insert(particle_systems, burn_smoke(pos, digging_fuel_cost))
end
if fuel_burning < 0 then
fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
-- we tap into the batteries either way
fuel_burning = fuel_burning + digtron.tap_batteries(layout.battery_holders, -fuel_burning, false)
if exhaust == 1 then
-- but we burn coal only if we must (exhaust = flag)
fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
end
end
meta:set_float("fuel_burning", fuel_burning)
meta:set_int("on_coal", exhaust)
status_text = status_text .. S("Heat remaining in controller furnace: @1", math.max(0, fuel_burning))
-- Eyecandy
@ -391,7 +420,8 @@ digtron.execute_downward_dig_cycle = function(pos, clicker)
local dir = digtron.facedir_to_down_dir(facing)
local fuel_burning = meta:get_float("fuel_burning") -- get amount of burned fuel left over from last cycle
local status_text = S("Heat remaining in controller furnace: @1", math.max(0, fuel_burning))
local exhaust = meta:get_int("on_coal")
local layout = DigtronLayout.create(pos, clicker)
local status_text, return_code = neighbour_test(layout, status_text, dir)
@ -492,15 +522,32 @@ digtron.execute_downward_dig_cycle = function(pos, clicker)
local status_text = ""
-- acutally burn the fuel needed
-- -- acutally burn the fuel needed
-- fuel_burning = fuel_burning - digging_fuel_cost
-- if digtron.config.particle_effects then
-- table.insert(particle_systems, burn_smoke(pos, digging_fuel_cost))
-- end
-- if fuel_burning < 0 then
-- fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
-- end
-- actually burn the fuel needed
fuel_burning = fuel_burning - digging_fuel_cost
if digtron.config.particle_effects then
if digtron.particle_effects and exhaust == 1 then
table.insert(particle_systems, burn_smoke(pos, digging_fuel_cost))
end
if fuel_burning < 0 then
fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
-- we tap into the batteries either way
fuel_burning = fuel_burning + digtron.tap_batteries(layout.battery_holders, -fuel_burning, false)
if exhaust == 1 then
-- but we burn coal only if we must (exhaust = flag)
fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
end
end
meta:set_float("fuel_burning", fuel_burning)
meta:set_int("on_coal", exhaust)
status_text = status_text .. S("Heat remaining in controller furnace: @1", math.max(0, fuel_burning))
-- Eyecandy