Add failure sound
[minetest_returnmirror.git] / init.lua
blob45fdc24cc6c3fc6c7e484670afd8688e142c1033
1 returnmirror = {}
2 returnmirror.cost_teleport = 200
3 returnmirror.cost_set = 20
5 if tonumber(minetest.setting_get("returnmirror_cost_teleport")) ~= nil then
6 returnmirror.cost_teleport = tonumber(minetest.setting_get("returnmirror_cost_teleport"))
7 end
8 if tonumber(minetest.setting_get("returnmirror_cost_set")) ~= nil then
9 returnmirror.cost_set = tonumber(minetest.setting_get("returnmirror_cost_set"))
10 end
12 if minetest.get_modpath("mana") ~= nil then
13 returnmirror.mana = true
14 else
15 returnmirror.mana = false
16 end
18 returnmirror.mana_check = function(player, cost)
19 local allowed
20 if returnmirror.mana then
21 if mana.subtract(player:get_player_name(), cost) then
22 allowed = true
23 else
24 allowed = false
25 end
26 else
27 allowed = true
28 end
29 return allowed
30 end
32 returnmirror.set_position_inactive = function(itemstack, user, pointed_thing)
33 if returnmirror.mana_check(user, returnmirror.cost_set) then
34 local pos = user:getpos()
35 local newitem = ItemStack("returnmirror:mirror_active")
36 newitem:set_metadata(minetest.pos_to_string(pos))
37 minetest.sound_play( {name="returnmirror_set", gain=1}, {pos=pos, max_hear_distance=12})
38 return newitem
39 else
40 minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=pos, max_hear_distance=18})
41 end
42 end
44 returnmirror.set_position_active = function(itemstack, user, pointed_thing)
45 if returnmirror.mana_check(user, returnmirror.cost_set) then
46 local pos = user:getpos()
47 itemstack:set_metadata(minetest.pos_to_string(pos))
48 minetest.sound_play( {name="returnmirror_set", gain=1}, {pos=pos, max_hear_distance=12})
49 return itemstack
50 else
51 minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=pos, max_hear_distance=18})
52 end
53 end
55 minetest.register_tool("returnmirror:mirror_inactive", {
56 description = "Mirror of Returning",
57 inventory_image = "returnmirror_mirror_inactive.png",
58 wield_image = "returnmirror_mirror_inactive.png",
59 tool_capabilities = {},
60 range = 0,
61 on_use = function(itemstack, user, pointed_thing)
62 minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=user:getpos(), max_hear_distance=18})
63 end,
64 on_place = returnmirror.set_position_inactive,
65 on_secondary_use = returnmirror.set_position_inactive,
68 minetest.register_tool("returnmirror:mirror_active", {
69 description = "Mirror of Returning",
70 stack_max = 1,
71 inventory_image = "returnmirror_mirror_active.png",
72 wield_image = "returnmirror_mirror_active.png",
73 tool_capabilities = {},
74 range = 0,
75 on_use = function(itemstack, user, pointed_thing)
76 local dest_string = itemstack:get_metadata()
77 local dest = minetest.string_to_pos(dest_string)
78 local fail = true
79 if dest ~= nil then
80 if returnmirror.mana_check(user, returnmirror.cost_teleport) then
81 fail = false
82 local src = user:getpos()
83 minetest.sound_play( {name="returnmirror_teleport", gain=1}, {pos=src, max_hear_distance=30})
84 minetest.add_particlespawner({
85 amount = 50,
86 time = 0.1,
87 minpos = {x=src.x-0.4, y=src.y+0.25, z=src.z-0.4},
88 maxpos = {x=src.x+0.4, y=src.y+0.75, z=src.z+0.4},
89 minvel = {x=-0.2, y=-0.2, z=-0.2},
90 maxvel = {x=0.2, y=0.2, z=0.2},
91 minexptime=3,
92 maxexptime=4.5,
93 minsize=1,
94 maxsize=1.25,
95 texture = "returnmirror_particle_departure.png",
97 user:setpos(dest)
98 minetest.sound_play( {name="returnmirror_teleport", gain=1}, {pos=dest, max_hear_distance=30})
99 minetest.add_particlespawner({
100 amount = 100,
101 time = 0.1,
102 minpos = {x=dest.x-0.4, y=dest.y+0.25, z=dest.z-0.4},
103 maxpos = {x=dest.x+0.4, y=dest.y+0.75, z=dest.z+0.4},
104 minvel = {x=-0.4, y=-0.3, z=-0.4},
105 maxvel = {x=0.4, y=0.3, z=0.4},
106 minexptime=6,
107 maxexptime=12,
108 minsize=1,
109 maxsize=1.25,
110 texture = "returnmirror_particle_arrival.png",
114 if fail then
115 minetest.sound_play( {name="returnmirror_fail", gain=1}, {pos=pos, max_hear_distance=18})
117 end,
118 on_place = returnmirror.set_position_active,
119 on_secondary_use = returnmirror.set_position_active,
120 groups = { not_in_creative_inventory = 1 },
123 minetest.register_alias("returnmirror:mirror_inactive", "returnmirror:returnmirror")
125 if minetest.get_modpath("doc_items") ~= nil then
126 local longdesc, usagehelp
127 usagehelp = "Rightclick to set the mirror's teleport location. Leftclick to immediately teleport back to the mirror's teleport location."
128 if minetest.get_modpath("mana") ~= nil then
129 longdesc ="This item allows to teleport the user back to a previously set location, at the cost of mana."
130 usagehelp = usagehelp .. " " .. string.format("Setting the teleport location costs %d mana, teleporting costs %d mana.", returnmirror.cost_set, returnmirror.cost_teleport)
131 else
132 longdesc = "This item allows to teleport its user back to a previously set location."
135 doc.sub.items.set_items_longdesc({
136 ["returnmirror:mirror_inactive"] = longdesc,
138 doc.sub.items.set_items_usagehelp({
139 ["returnmirror:mirror_inactive"] = usagehelp
142 doc.add_entry_alias("tools", "returnmirror:mirror_inactive", "returnmirror:mirror_active")