minetest_returnmirror/init.lua

166 lines
6.0 KiB
Lua
Raw Normal View History

2020-02-07 06:22:54 +01:00
local S = minetest.get_translator("returnmirror")
2016-08-11 17:56:23 +02:00
local returnmirror = {}
2015-02-14 03:22:32 +01:00
returnmirror.cost_teleport = 200
2015-02-14 00:25:55 +01:00
returnmirror.cost_set = 20
2015-02-13 21:43:24 +01:00
2017-06-20 16:50:11 +02:00
if tonumber(minetest.settings:get("returnmirror_cost_teleport")) ~= nil then
returnmirror.cost_teleport = tonumber(minetest.settings:get("returnmirror_cost_teleport"))
2015-02-14 00:40:19 +01:00
end
2017-06-20 16:50:11 +02:00
if tonumber(minetest.settings:get("returnmirror_cost_set")) ~= nil then
returnmirror.cost_set = tonumber(minetest.settings:get("returnmirror_cost_set"))
2015-02-14 00:40:19 +01:00
end
2015-02-14 00:02:35 +01:00
if minetest.get_modpath("mana") ~= nil then
returnmirror.mana = true
else
returnmirror.mana = false
end
2015-02-14 00:25:55 +01:00
returnmirror.mana_check = function(player, cost)
local allowed
if returnmirror.mana then
if mana.subtract(player:get_player_name(), cost) then
allowed = true
else
allowed = false
end
else
allowed = true
end
return allowed
end
returnmirror.set_position_inactive = function(itemstack, user, pointed_thing)
2017-06-01 00:48:43 +02:00
-- Use pointed node's on_rightclick function first, if present
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
2020-02-07 06:22:54 +01:00
local pos = user:get_pos()
if returnmirror.mana_check(user, returnmirror.cost_set) then
local newitem = ItemStack("returnmirror:mirror_active")
newitem:set_metadata(minetest.pos_to_string(pos))
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_set", gain=1}, {pos=pos, max_hear_distance=12}, false)
return newitem
2016-08-08 07:20:44 +02:00
else
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=pos, max_hear_distance=18}, false)
end
end
returnmirror.set_position_active = function(itemstack, user, pointed_thing)
2017-06-01 00:48:43 +02:00
-- Use pointed node's on_rightclick function first, if present
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
2017-06-01 00:52:14 +02:00
local pos = user:getpos()
if returnmirror.mana_check(user, returnmirror.cost_set) then
itemstack:set_metadata(minetest.pos_to_string(pos))
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_set", gain=1}, {pos=pos, max_hear_distance=12}, false)
return itemstack
2016-08-08 07:20:44 +02:00
else
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=pos, max_hear_distance=18}, false)
end
end
2016-11-02 22:53:34 +01:00
local longdesc, usagehelp
if minetest.get_modpath("doc_items") then
usagehelp = S("Rightclick to set the mirror's teleport location. Leftclick to immediately teleport back to the mirror's teleport location.")
if minetest.get_modpath("mana") ~= nil then
longdesc = S("This item allows to teleport the user back to a previously set location, at the cost of mana.")
2016-11-09 02:33:44 +01:00
usagehelp = usagehelp .. " " .. S("Setting the teleport location costs @1 mana, teleporting costs @2 mana.", returnmirror.cost_set, returnmirror.cost_teleport)
2016-11-02 22:53:34 +01:00
else
longdesc = S("This item allows to teleport its user back to a previously set location.")
end
end
minetest.register_tool("returnmirror:mirror_inactive", {
description = S("Mirror of Returning"),
2016-11-16 06:43:21 +01:00
_doc_items_longdesc = longdesc,
_doc_items_usagehelp = usagehelp,
inventory_image = "returnmirror_mirror_inactive.png",
wield_image = "returnmirror_mirror_inactive.png",
tool_capabilities = {},
2018-10-16 12:59:51 +02:00
groups = { disable_repair = 1 },
2017-06-01 00:48:43 +02:00
range = 2,
2016-08-08 07:20:44 +02:00
on_use = function(itemstack, user, pointed_thing)
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=user:getpos(), max_hear_distance=18}, false)
2016-08-08 07:20:44 +02:00
end,
on_place = returnmirror.set_position_inactive,
on_secondary_use = returnmirror.set_position_inactive,
})
minetest.register_tool("returnmirror:mirror_active", {
description = S("Mirror of Returning"),
2016-11-16 06:43:21 +01:00
_doc_items_create_entry = false,
2015-02-13 21:43:24 +01:00
stack_max = 1,
inventory_image = "returnmirror_mirror_active.png",
wield_image = "returnmirror_mirror_active.png",
2015-02-13 21:43:24 +01:00
tool_capabilities = {},
2018-10-16 12:59:51 +02:00
groups = { disable_repair = 1 },
2017-06-01 00:48:43 +02:00
range = 2,
2015-02-13 21:43:24 +01:00
on_use = function(itemstack, user, pointed_thing)
2015-02-13 23:21:25 +01:00
local dest_string = itemstack:get_metadata()
local dest = minetest.string_to_pos(dest_string)
2016-12-29 05:44:26 +01:00
local src = user:getpos()
2016-08-08 07:20:44 +02:00
local fail = true
2015-02-13 23:21:25 +01:00
if dest ~= nil then
2015-02-14 00:25:55 +01:00
if returnmirror.mana_check(user, returnmirror.cost_teleport) then
2016-08-08 07:20:44 +02:00
fail = false
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_teleport", gain=1}, {pos=src, max_hear_distance=30}, false)
2015-02-13 23:53:53 +01:00
minetest.add_particlespawner({
amount = 50,
time = 0.1,
minpos = {x=src.x-0.4, y=src.y+0.25, z=src.z-0.4},
maxpos = {x=src.x+0.4, y=src.y+0.75, z=src.z+0.4},
minvel = {x=-0.2, y=-0.2, z=-0.2},
maxvel = {x=0.2, y=0.2, z=0.2},
minexptime=3,
maxexptime=4.5,
minsize=1,
maxsize=1.25,
texture = "returnmirror_particle_departure.png",
})
2015-02-13 23:21:25 +01:00
user:setpos(dest)
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_teleport", gain=1}, {pos=dest, max_hear_distance=30}, false)
2015-02-13 23:53:53 +01:00
minetest.add_particlespawner({
amount = 100,
time = 0.1,
minpos = {x=dest.x-0.4, y=dest.y+0.25, z=dest.z-0.4},
maxpos = {x=dest.x+0.4, y=dest.y+0.75, z=dest.z+0.4},
minvel = {x=-0.4, y=-0.3, z=-0.4},
maxvel = {x=0.4, y=0.3, z=0.4},
minexptime=6,
maxexptime=12,
minsize=1,
maxsize=1.25,
texture = "returnmirror_particle_arrival.png",
})
2015-02-13 23:07:54 +01:00
end
2015-02-13 21:43:24 +01:00
end
2016-08-08 07:20:44 +02:00
if fail then
2020-04-06 00:09:42 +02:00
minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=src, max_hear_distance=18}, false)
2016-08-08 07:20:44 +02:00
end
2015-02-13 21:43:24 +01:00
end,
on_place = returnmirror.set_position_active,
on_secondary_use = returnmirror.set_position_active,
groups = { not_in_creative_inventory = 1 },
2015-02-13 21:43:24 +01:00
})
2016-12-29 05:20:17 +01:00
minetest.register_alias("returnmirror:returnmirror", "returnmirror:mirror_inactive")
2016-08-08 05:11:36 +02:00
2016-11-02 22:53:34 +01:00
if minetest.get_modpath("doc") ~= nil then
2016-12-28 17:37:15 +01:00
doc.add_entry_alias("tools", "returnmirror:mirror_inactive", "tools", "returnmirror:mirror_active")
2016-08-08 05:11:36 +02:00
end