Compare commits
3 Commits
9503a77c9d
...
dbede7d8bf
Author | SHA1 | Date |
---|---|---|
Pascal Abresch | dbede7d8bf | |
Pascal Abresch | 7d1694993e | |
Pascal Abresch | 6853f987bc |
|
@ -20,11 +20,12 @@ end
|
|||
sparktech.setoption = function(nodepos, optionname, optionvalue)
|
||||
local node = minetest.get_node(nodepos)
|
||||
local meta = minetest.get_meta(nodepos)
|
||||
local optionsdef = minetest.registered_nodes[node.name]._sparkoption
|
||||
if iodebug and not optionsdef[optionname] then
|
||||
error("optioname " .. optionname .. " for " .. node.name .." is not defined!")
|
||||
end
|
||||
meta:set(optionname, minetest:serialize(optionvalue))
|
||||
nodeminetest.registered_nodes[node.name]._sparkoptionchanged(nodepos, optionname)
|
||||
meta:set_string(optionname, minetest.serialize(optionvalue))
|
||||
minetest.registered_nodes[node.name]._sparkoptionchanged(nodepos, optionname)
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -17,7 +17,24 @@ minetest.register_node(NAME ..":capacitor", {
|
|||
sparktech_energy_storeonbreak = TRUE,
|
||||
sparktech_energy_max = MAX_ENERGY,
|
||||
sparktech_net_trigger = TRUE,
|
||||
}
|
||||
},
|
||||
_sparkoptionchanged = function(pos, name)
|
||||
error("Sparkoptionchanged called with no options defined!")
|
||||
end,
|
||||
_sparkoption = {
|
||||
--TODO: Implement this, may need changes in net_code to respect meta for max or min?
|
||||
--enabled = true,
|
||||
--minLevel = 0,
|
||||
--maxLevel = MAX_ENERGY
|
||||
},
|
||||
_sparkoutput = {
|
||||
energyLevel = function(nodepos) --todo adjust for maxLevel
|
||||
return minetest.get_meta(nodepos):get_int("energy") / MAX_ENERGY
|
||||
end,
|
||||
},
|
||||
_sparkoutputtype = {
|
||||
energyLevel = type(0)
|
||||
}
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
|
|
|
@ -9,28 +9,99 @@ end)
|
|||
|
||||
local spark_quick_put_enable = minetest.settings:get_bool("spark_quick_put_enabled")
|
||||
|
||||
local whitelist = { "fuel", "product", "source", }
|
||||
local whitelistPut = { fuel = true, source = true, charge = true }
|
||||
local whitelistTake = { charge = true, product = true }
|
||||
|
||||
local function quick_refuel(position, node, puncher, pointed)
|
||||
local nodemeta = minetest.get_meta(position)
|
||||
local noderef = minetest.registered_nodes[node.name]
|
||||
if not noderef then return false end
|
||||
local putfunc = noderef.allow_metadata_inventory_put
|
||||
local itemstack = puncher:get_wielded_item()
|
||||
local inventory = nodemeta:get_inventory()
|
||||
if not inventory or not putfunc then return false end -- putfunc so grass doesnt crash the game
|
||||
for list in pairs(whitelist) do
|
||||
local transstack = putfunc(position, whitelist[list], nil, itemstack, puncher)
|
||||
if transstack ~= 0 then
|
||||
local leftover = inventory:add_item(whitelist[list], itemstack)
|
||||
puncher:set_wielded_item(leftover)
|
||||
return
|
||||
local maxStackSize = tonumber(minetest.settings:get("default_stack_max"))
|
||||
local maxIncrement = function(stack1)
|
||||
return (maxStackSize - stack1:get_count()) * -1
|
||||
end
|
||||
|
||||
-- TODO: move this to sparkapi
|
||||
-- This should combine stack1 with stack2, respecting per-item metadata like genomes
|
||||
-- Just take random ones :D, if something else is wanted it can be done more explicitly
|
||||
local combineStack = function(stack1, stack2)
|
||||
if stack1:get_name() == stack2:get_name() then
|
||||
local count1 = stack1:get_count()
|
||||
local count2 = stack2:get_count()
|
||||
local newcount1 = count1 + count2
|
||||
local newcount2 = 0
|
||||
if newcount1 > maxStackSize then
|
||||
newcount2 = newcount1 - maxStackSize
|
||||
newcount1 = maxStackSize
|
||||
end
|
||||
stack1:set_count(newcount1)
|
||||
stack2:set_count(newcount2)
|
||||
return stack1, stack2
|
||||
elseif stack1:get_name() == "" then
|
||||
return stack2, stack1
|
||||
else
|
||||
return stack1, stack2
|
||||
end
|
||||
end
|
||||
|
||||
-- This functionality relies on allow_metadata_inventory_put
|
||||
-- and allow_metadata_inventory_take beeing defined correctly
|
||||
-- fallback code would be quite icky to write...
|
||||
local putItem = function(position, node, puncher, pointed)
|
||||
local noderef = minetest.registered_nodes[node.name]
|
||||
local putfunc = noderef.allow_metadata_inventory_put
|
||||
local wieldItem = puncher:get_wielded_item()
|
||||
local inventory = minetest.get_meta(position):get_inventory()
|
||||
if not noderef or not putfunc or not wieldItem or not inventory then return false end
|
||||
|
||||
if spark_quick_put_enable then
|
||||
minetest.register_on_punchnode(quick_refuel)
|
||||
for listName in pairs(inventory:get_lists()) do
|
||||
if whitelistPut[listName] then
|
||||
local transstack = 0
|
||||
if putfunc then
|
||||
transstack = putfunc(position, listName, nil, wieldItem, puncher)
|
||||
end
|
||||
if transstack ~= 0 then
|
||||
local leftover = inventory:add_item(listName, wieldItem)
|
||||
puncher:set_wielded_item(leftover)
|
||||
noderef.on_metadata_inventory_put(position)
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local takeItem = function(position, node, puncher, pointed)
|
||||
local noderef = minetest.registered_nodes[node.name]
|
||||
local takefunc = noderef.allow_metadata_inventory_take
|
||||
local wieldItem = puncher:get_wielded_item()
|
||||
local inventory = minetest.get_meta(position):get_inventory()
|
||||
if not noderef or not takefunc or not wieldItem or not inventory then return false end
|
||||
|
||||
for listName in pairs(inventory:get_lists()) do
|
||||
if whitelistTake[listName] then
|
||||
local result = false
|
||||
for index = 1, inventory:get_size(listName) do
|
||||
wieldItem = puncher:get_wielded_item()
|
||||
local item = inventory:get_stack(listName, index)
|
||||
if (wieldItem:get_name() == "" and item:get_name() ~= "") or wieldItem:get_name() == item:get_name() then
|
||||
local maxAmmount = takefunc(position, listName, index, item, player)
|
||||
local ammount = math.min(maxAmmount, maxIncrement(wieldItem))
|
||||
if ammount ~= 0 then
|
||||
local stack1, stack2 = combineStack(wieldItem, item)
|
||||
puncher:set_wielded_item(stack1)
|
||||
inventory:set_stack(listName, index, stack2)
|
||||
result = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if result then return result end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
minetest.register_on_punchnode(function(position, node, puncher, pointed)
|
||||
if not putItem(position, node, puncher, pointed) then
|
||||
return takeItem(position, node, puncher, pointed)
|
||||
else
|
||||
return true
|
||||
end
|
||||
end)
|
||||
|
|
|
@ -39,13 +39,33 @@ minetest.register_node(NAME ..":charger", {
|
|||
)
|
||||
return 1
|
||||
end,
|
||||
allow_metadata_inventory_put = function (_, _, _, itemstack)
|
||||
if minetest.get_item_group(itemstack:get_name(), "sparktech_chargable") > 0 then
|
||||
allow_metadata_inventory_put = function (pos, listname, index, itemstack, player)
|
||||
if listname ~= "charge" then return 0 end
|
||||
if index == nil then index = 1 end
|
||||
if index ~= 1 then return 0 end
|
||||
|
||||
local inventory = minetest.get_inventory({type="node", pos=pos})
|
||||
|
||||
if inventory:room_for_item(listname, itemstack) and
|
||||
minetest.get_item_group(itemstack:get_name(), "sparktech_chargable") > 0 then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end,
|
||||
allow_metadata_inventory_take = function(pos, listname, index, _--[[ Already have the listname AND index]], player)
|
||||
if listname ~= "charge" then return 0 end
|
||||
if index == nil then index = 1 end
|
||||
if index ~= 1 then return 0 end
|
||||
local inventory = minetest.get_inventory({type="node", pos=pos})
|
||||
local item = inventory:get_stack(listname, index)
|
||||
|
||||
if item:get_name() ~= "" then
|
||||
return item:get_count()
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end,
|
||||
on_metadata_inventory_put = function (pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
if meta:get_int("energy") <= 0 then return end
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
local NAME = minetest.get_current_modname()
|
||||
|
||||
minetest.register_tool(NAME .. ":screwdriver", {
|
||||
description = "Screwdriver",
|
||||
inventory_image = NAME .. "_screwdriver.png",
|
||||
range = 6.0,
|
||||
stack_max = 1,
|
||||
tool_capabilities = {},
|
||||
on_place = function (_, player, pointed_thing)
|
||||
local node = nil
|
||||
if pointed_thing["type"] == "node" then
|
||||
node = pointed_thing["under"]
|
||||
else
|
||||
return nil
|
||||
end
|
||||
local option = sparktech.getoption(node, "enabled")
|
||||
option = not option
|
||||
sparktech.setoption(node, "enabled", option)
|
||||
return nil
|
||||
end
|
||||
})
|
||||
|
Loading…
Reference in New Issue