add torches based wieldlight to pick_lava (closes #1)

master
tchncs 2016-08-03 23:10:01 +02:00
parent 3d03e45d1c
commit 03bbb5779a
1 changed files with 128 additions and 1 deletions

View File

@ -133,10 +133,137 @@ minetest.register_tool(":mobs:pick_lava", {
})
minetest.register_craft({
output = "mobs:pick_lava",
output = ":mobs:pick_lava",
recipe = {
{"mobs:lava_orb", "mobs:lava_orb", "mobs:lava_orb"},
{"", "default:obsidian_shard", ""},
{"", "default:obsidian_shard", ""},
}
})
--
-- pick_lava wield light based on github.com/minetest-mods/torches - deds to sofar <3
--
local picklight_update_interval = minetest.setting_get("torches_wieldlight_interval") or 0.15
minetest.register_node(":mobs:picklight", {
drawtype = "airlike",
groups = {not_in_creative_inventory = 1},
walkable = false,
paramtype = "light",
sunlight_propagates = true,
light_source = 11,
pointable = false,
buildable_to = true,
drops = {},
})
-- state tables
local picklight = {}
local playerlist = {}
local function wields_torch(player)
if not player then
return false
end
local item = player:get_wielded_item()
if not item then
return false
end
return item:get_name() == "mobs:pick_lava"
end
local function wielded_torch(name)
if not picklight[name] then
return false
end
return true
end
local function is_picklight(pos)
local node = minetest.get_node(pos)
return node.name == "mobs:picklight"
end
local function remove_picklight(pos)
if is_picklight(pos) then
minetest.swap_node(pos, {name = "air"})
end
end
local function place_picklight(pos)
local name = minetest.get_node(pos).name
if name == "mobs:picklight" then
return true
end
if (minetest.get_node_light(pos) or 0) > 11 then
-- no reason to place torch here, so save a bunch
-- of node updates this way
return false
end
if name == "air" then
minetest.swap_node(pos, {name = "mobs:picklight"})
return true
end
return false
end
local function get_torchpos(player)
return vector.add({x = 0, y = 1, z = 0}, vector.round(player:getpos()))
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
playerlist[name] = true
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
-- don't look at wielded() here, it's likely invalid
if picklight[name] then
remove_picklight(picklight[name])
picklight[name] = nil
end
playerlist[name] = nil
end)
minetest.register_on_shutdown(function()
for i, _ in pairs(picklight) do
remove_picklight(picklight[i])
end
end)
local function update_picklight(dtime)
for name, _ in pairs(playerlist) do
local player = minetest.get_player_by_name(name)
local wielded = wielded_torch(name)
local wields = wields_torch(player)
if not wielded and wields then
local torchpos = get_torchpos(player)
if place_picklight(torchpos) then
picklight[name] = vector.new(torchpos)
end
elseif wielded and not wields then
remove_picklight(picklight[name])
picklight[name] = nil
elseif wielded and wields then
local torchpos = get_torchpos(player)
if not vector.equals(torchpos, picklight[name]) or
not is_picklight(torchpos) then
if place_picklight(torchpos) then
remove_picklight(picklight[name])
picklight[name] = vector.new(torchpos)
elseif vector.distance(picklight[name], torchpos) > 2 then
-- player went into some node
remove_picklight(picklight[name])
picklight[name] = nil
end
end
end
end
minetest.after(picklight_update_interval, update_picklight)
end
minetest.after(picklight_update_interval, update_picklight)