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