reuse the new screwdriver code of minetest/minetest_game in adapted form for the sonic screwdriver

This commit is contained in:
Tim 2015-01-10 22:42:49 +01:00 committed by Vanessa Ezekowitz
parent 3252da05d1
commit fe4372649f

View File

@ -4,60 +4,85 @@ local S = technic.getter
technic.register_power_tool("technic:sonic_screwdriver", sonic_screwdriver_max_charge) technic.register_power_tool("technic:sonic_screwdriver", sonic_screwdriver_max_charge)
minetest.register_tool("technic:sonic_screwdriver", { -- screwdriver handler code reused from minetest/minetest_game screwdriver @a9ac480
description = S("Sonic Screwdriver"), local ROTATE_FACE = 1
inventory_image = "technic_sonic_screwdriver.png", local ROTATE_AXIS = 2
wear_represents = "technic_RE_charge",
on_refill = technic.refill_RE_charge, local function nextrange(x, max)
on_use = function(itemstack, user, pointed_thing) x = x + 1
-- Must be pointing to facedir applicable node if x > max then
x = 0
end
return x
end
-- Handles rotation
local function screwdriver_handler(itemstack, user, pointed_thing, mode)
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
return return
end end
local pos = pointed_thing.under local pos = pointed_thing.under
if minetest.is_protected(pos, user:get_player_name()) then if minetest.is_protected(pos, user:get_player_name()) then
minetest.record_protection_violation(pos, user:get_player_name()) minetest.record_protection_violation(pos, user:get_player_name())
return return
end end
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local node_name = node.name local ndef = minetest.registered_nodes[node.name]
if minetest.registered_nodes[node_name].paramtype2 ~= "facedir" and if not ndef or not ndef.paramtype2 == "facedir" or
minetest.registered_nodes[node_name].paramtype2 ~= "wallmounted" then (ndef.drawtype == "nodebox" and
return not ndef.node_box.type == "fixed") or
end node.param2 == nil then
if node.param2 == nil then
return return
end end
-- contrary to the default screwdriver, do not check for can_dig, to allow rotating machines with CLU's in them
-- this is consistent with the previous sonic screwdriver
local meta1 = minetest.deserialize(itemstack:get_metadata()) local meta1 = minetest.deserialize(itemstack:get_metadata())
if not meta1 or not meta1.charge then if not meta1 or not meta1.charge or meta1.charge < 100 then
return return
end end
if meta1.charge >= 100 then
minetest.sound_play("technic_sonic_screwdriver", minetest.sound_play("technic_sonic_screwdriver", {pos = pos, gain = 0.3, max_hear_distance = 10})
{pos = pos, gain = 0.3, max_hear_distance = 10})
local p = node.param2 -- Set param2
if minetest.registered_nodes[node_name].paramtype2 == "facedir" then local rotationPart = node.param2 % 32 -- get first 4 bits
p = p + 1 local preservePart = node.param2 - rotationPart
if p == 4 then
p = 0 local axisdir = math.floor(rotationPart / 4)
local rotation = rotationPart - axisdir * 4
if mode == ROTATE_FACE then
rotationPart = axisdir * 4 + nextrange(rotation, 3)
elseif mode == ROTATE_AXIS then
rotationPart = nextrange(axisdir, 5) * 4
end end
else
p = p + 1 node.param2 = preservePart + rotationPart
if p == 6 then
p = 0
end
end
if minetest.is_protected(pos, user:get_player_name()) then
minetest.record_protection_violation(pos, user:get_player_name())
else
node.param2 = p
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
if not minetest.setting_getbool("creative_mode") then
meta1.charge = meta1.charge - 100 meta1.charge = meta1.charge - 100
itemstack:set_metadata(minetest.serialize(meta1)) itemstack:set_metadata(minetest.serialize(meta1))
technic.set_RE_wear(itemstack, meta1.charge, sonic_screwdriver_max_charge) technic.set_RE_wear(itemstack, meta1.charge, sonic_screwdriver_max_charge)
end end
end
return itemstack
end
minetest.register_tool("technic:sonic_screwdriver", {
description = S("Sonic Screwdriver (left-click rotates face, right-click rotates axis)"),
inventory_image = "technic_sonic_screwdriver.png",
wear_represents = "technic_RE_charge",
on_refill = technic.refill_RE_charge,
on_use = function(itemstack, user, pointed_thing)
screwdriver_handler(itemstack, user, pointed_thing, ROTATE_FACE)
return itemstack
end,
on_place = function(itemstack, user, pointed_thing)
screwdriver_handler(itemstack, user, pointed_thing, ROTATE_AXIS)
return itemstack return itemstack
end, end,
}) })