init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. --= Teleport Potion mod by TenPlus1 (SFX are license free)
  2. -- Craft teleport potion or pad, use to bookmark location, place to open
  3. -- portal or place pad, portals show a blue flame that you can walk into
  4. -- before it closes (10 seconds), potions can also be thrown for local teleport.
  5. -- Load support for intllib.
  6. local MP = minetest.get_modpath(minetest.get_current_modname())
  7. local S = minetest.get_translator and minetest.get_translator("teleport_potion") or
  8. dofile(MP .. "/intllib.lua")
  9. -- check for MineClone2
  10. local mcl = minetest.get_modpath("mcl_core")
  11. -- max teleport distance
  12. local dist = tonumber(minetest.settings:get("map_generation_limit") or 31000)
  13. -- creative check
  14. local creative_mode_cache = minetest.settings:get_bool("creative_mode")
  15. local function is_creative(name)
  16. return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
  17. end
  18. -- make sure coordinates are valid
  19. local check_coordinates = function(str)
  20. if not str or str == "" then
  21. return nil
  22. end
  23. -- get coords from string
  24. local x, y, z = string.match(str, "^(-?%d+),(-?%d+),(-?%d+)$")
  25. -- check coords
  26. if x == nil or string.len(x) > 6
  27. or y == nil or string.len(y) > 6
  28. or z == nil or string.len(z) > 6 then
  29. return nil
  30. end
  31. -- convert string coords to numbers
  32. x = tonumber(x)
  33. y = tonumber(y)
  34. z = tonumber(z)
  35. -- are coords in map range ?
  36. if x > dist or x < -dist
  37. or y > dist or y < -dist
  38. or z > dist or z < -dist then
  39. return nil
  40. end
  41. -- return ok coords
  42. return {x = x, y = y, z = z}
  43. end
  44. -- particle effects
  45. local function tp_effect(pos)
  46. minetest.add_particlespawner({
  47. amount = 20,
  48. time = 0.25,
  49. minpos = pos,
  50. maxpos = pos,
  51. minvel = {x = -2, y = 1, z = -2},
  52. maxvel = {x = 2, y = 2, z = 2},
  53. minacc = {x = 0, y = -2, z = 0},
  54. maxacc = {x = 0, y = -4, z = 0},
  55. minexptime = 0.1,
  56. maxexptime = 1,
  57. minsize = 0.5,
  58. maxsize = 1.5,
  59. texture = "teleport_potion_particle.png",
  60. glow = 15
  61. })
  62. end
  63. local teleport_destinations = {}
  64. local function set_teleport_destination(playername, dest)
  65. teleport_destinations[playername] = dest
  66. tp_effect(dest)
  67. minetest.sound_play("portal_open", {
  68. pos = dest, gain = 1.0, max_hear_distance = 10}, true)
  69. end
  70. --- Teleport portal
  71. minetest.register_node("teleport_potion:portal", {
  72. drawtype = "plantlike",
  73. tiles = {
  74. {
  75. name = "teleport_potion_portal.png",
  76. animation = {
  77. type = "vertical_frames",
  78. aspect_w = 16,
  79. aspect_h = 16,
  80. length = 1.0
  81. }
  82. }
  83. },
  84. light_source = 13,
  85. walkable = false,
  86. paramtype = "light",
  87. pointable = false,
  88. buildable_to = true,
  89. waving = 1,
  90. sunlight_propagates = true,
  91. damage_per_second = 1, -- walking into portal hurts player
  92. groups = {not_in_creative_inventory = 1},
  93. -- start timer when portal appears
  94. on_construct = function(pos)
  95. minetest.get_node_timer(pos):start(10)
  96. end,
  97. -- remove portal after 10 seconds
  98. on_timer = function(pos)
  99. minetest.sound_play("portal_close", {
  100. pos = pos, gain = 1.0, max_hear_distance = 10}, true)
  101. minetest.remove_node(pos)
  102. end,
  103. on_blast = function() end,
  104. drop = {}
  105. })
  106. -- Throwable potion
  107. local function throw_potion(itemstack, player)
  108. local playerpos = player:get_pos()
  109. local obj = minetest.add_entity({
  110. x = playerpos.x,
  111. y = playerpos.y + 1.5,
  112. z = playerpos.z
  113. }, "teleport_potion:potion_entity")
  114. local dir = player:get_look_dir()
  115. local velocity = 20
  116. obj:set_velocity({
  117. x = dir.x * velocity,
  118. y = dir.y * velocity,
  119. z = dir.z * velocity
  120. })
  121. obj:set_acceleration({
  122. x = dir.x * -3,
  123. y = -9.5,
  124. z = dir.z * -3
  125. })
  126. obj:set_yaw(player:get_look_horizontal())
  127. obj:get_luaentity().player = player
  128. end
  129. -- potion entity
  130. local potion_entity = {
  131. physical = true,
  132. visual = "sprite",
  133. visual_size = {x = 1.0, y = 1.0},
  134. textures = {"teleport_potion_potion.png"},
  135. collisionbox = {-0.1,-0.1,-0.1,0.1,0.1,0.1},
  136. lastpos = {},
  137. player = ""
  138. }
  139. potion_entity.on_step = function(self, dtime)
  140. if not self.player then
  141. self.object:remove()
  142. return
  143. end
  144. local pos = self.object:get_pos()
  145. if self.lastpos.x ~= nil then
  146. local vel = self.object:get_velocity()
  147. -- only when potion hits something physical
  148. if vel.x == 0
  149. or vel.y == 0
  150. or vel.z == 0 then
  151. if self.player ~= "" then
  152. -- round up coords to fix glitching through doors
  153. self.lastpos = vector.round(self.lastpos)
  154. self.player:set_pos(self.lastpos)
  155. minetest.sound_play("portal_close", {
  156. pos = self.lastpos,
  157. gain = 1.0,
  158. max_hear_distance = 5
  159. }, true)
  160. tp_effect(self.lastpos)
  161. end
  162. self.object:remove()
  163. return
  164. end
  165. end
  166. self.lastpos = pos
  167. end
  168. minetest.register_entity("teleport_potion:potion_entity", potion_entity)
  169. --- Teleport potion
  170. minetest.register_node("teleport_potion:potion", {
  171. tiles = {"teleport_potion_potion.png"},
  172. drawtype = "signlike",
  173. paramtype = "light",
  174. paramtype2 = "wallmounted",
  175. walkable = false,
  176. sunlight_propagates = true,
  177. description = S("Teleport Potion (use to set destination, place to open portal)"),
  178. inventory_image = "teleport_potion_potion.png",
  179. wield_image = "teleport_potion_potion.png",
  180. groups = {dig_immediate = 3, vessel = 1},
  181. selection_box = {type = "wallmounted"},
  182. on_use = function(itemstack, user, pointed_thing)
  183. if pointed_thing.type == "node" then
  184. set_teleport_destination(user:get_player_name(), pointed_thing.above)
  185. else
  186. throw_potion(itemstack, user)
  187. if not is_creative(user:get_player_name()) then
  188. itemstack:take_item()
  189. return itemstack
  190. end
  191. end
  192. end,
  193. after_place_node = function(pos, placer, itemstack, pointed_thing)
  194. local name = placer:get_player_name()
  195. local dest = teleport_destinations[name]
  196. if dest then
  197. minetest.set_node(pos, {name = "teleport_potion:portal"})
  198. local meta = minetest.get_meta(pos)
  199. -- Set portal destination
  200. meta:set_int("x", dest.x)
  201. meta:set_int("y", dest.y)
  202. meta:set_int("z", dest.z)
  203. -- Portal open effect and sound
  204. tp_effect(pos)
  205. minetest.sound_play("portal_open", {
  206. pos = pos, gain = 1.0, max_hear_distance = 10}, true)
  207. else
  208. minetest.chat_send_player(name, S("Potion failed!"))
  209. minetest.remove_node(pos)
  210. minetest.add_item(pos, "teleport_potion:potion")
  211. end
  212. end
  213. })
  214. -- teleport potion recipe
  215. if mcl then
  216. minetest.register_craft({
  217. output = "teleport_potion:potion",
  218. recipe = {
  219. {"", "mcl_core:diamond", ""},
  220. {"mcl_core:diamond", "mcl_potions:glass_bottle", "mcl_core:diamond"},
  221. {"", "mcl_core:diamond", ""}
  222. }
  223. })
  224. else
  225. minetest.register_craft({
  226. output = "teleport_potion:potion",
  227. recipe = {
  228. {"", "default:diamond", ""},
  229. {"default:diamond", "vessels:glass_bottle", "default:diamond"},
  230. {"", "default:diamond", ""}
  231. }
  232. })
  233. end
  234. --- Teleport pad
  235. local teleport_formspec_context = {}
  236. minetest.register_node("teleport_potion:pad", {
  237. tiles = {"teleport_potion_pad.png", "teleport_potion_pad.png^[transformFY"},
  238. drawtype = "nodebox",
  239. paramtype = "light",
  240. paramtype2 = "facedir",
  241. legacy_wallmounted = true,
  242. walkable = true,
  243. sunlight_propagates = true,
  244. description = S("Teleport Pad (use to set destination, place to open portal)"),
  245. inventory_image = "teleport_potion_pad.png",
  246. wield_image = "teleport_potion_pad.png",
  247. light_source = 5,
  248. groups = {snappy = 3},
  249. node_box = {
  250. type = "fixed",
  251. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  252. },
  253. selection_box = {
  254. type = "fixed",
  255. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  256. },
  257. -- Save pointed nodes coordinates as destination for further portals
  258. on_use = function(itemstack, user, pointed_thing)
  259. if pointed_thing.type == "node" then
  260. set_teleport_destination(user:get_player_name(), pointed_thing.above)
  261. end
  262. end,
  263. -- Initialize teleport to saved location or the current position
  264. after_place_node = function(pos, placer, itemstack, pointed_thing)
  265. local meta = minetest.get_meta(pos)
  266. local name = placer:get_player_name()
  267. local dest = teleport_destinations[name]
  268. if not dest then
  269. dest = pos
  270. end
  271. -- Set coords
  272. meta:set_int("x", dest.x)
  273. meta:set_int("y", dest.y)
  274. meta:set_int("z", dest.z)
  275. meta:set_string("infotext", S("Pad Active (@1,@2,@3)",
  276. dest.x, dest.y, dest.z))
  277. minetest.sound_play("portal_open", {
  278. pos = pos, gain = 1.0, max_hear_distance = 10}, true)
  279. end,
  280. -- Show formspec depending on the players privileges.
  281. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  282. local name = clicker:get_player_name()
  283. if minetest.is_protected(pos, name) then
  284. minetest.record_protection_violation(pos, name)
  285. return
  286. end
  287. local meta = minetest.get_meta(pos)
  288. local coords = {
  289. x = meta:get_int("x"),
  290. y = meta:get_int("y"),
  291. z = meta:get_int("z")
  292. }
  293. local coords = coords.x .. "," .. coords.y .. "," .. coords.z
  294. local desc = meta:get_string("desc")
  295. formspec = "field[desc;" .. S("Description") .. ";"
  296. .. minetest.formspec_escape(desc) .. "]"
  297. -- Only allow privileged players to change coordinates
  298. if minetest.check_player_privs(name, "teleport") then
  299. formspec = formspec ..
  300. "field[coords;" .. S("Teleport coordinates") .. ";" .. coords .. "]"
  301. end
  302. teleport_formspec_context[name] = {
  303. pos = pos,
  304. coords = coords,
  305. desc = desc,
  306. }
  307. minetest.show_formspec(name, "teleport_potion:set_destination", formspec)
  308. end
  309. })
  310. -- Check and set coordinates
  311. minetest.register_on_player_receive_fields(function(player, formname, fields)
  312. if formname ~= "teleport_potion:set_destination" then
  313. return false
  314. end
  315. local name = player:get_player_name()
  316. local context = teleport_formspec_context[name]
  317. if not context then return false end
  318. teleport_formspec_context[name] = nil
  319. local meta = minetest.get_meta(context.pos)
  320. -- Coordinates were changed
  321. if fields.coords and fields.coords ~= context.coords then
  322. local coords = check_coordinates(fields.coords)
  323. if coords then
  324. meta:set_int("x", coords.x)
  325. meta:set_int("y", coords.y)
  326. meta:set_int("z", coords.z)
  327. else
  328. minetest.chat_send_player(name, S("Teleport Pad coordinates failed!"))
  329. end
  330. end
  331. -- Update infotext
  332. if fields.desc and fields.desc ~= "" then
  333. meta:set_string("desc", fields.desc)
  334. meta:set_string("infotext", S("Teleport to @1", fields.desc))
  335. else
  336. local coords = minetest.string_to_pos("(" .. context.coords .. ")")
  337. meta:set_string("infotext", S("Pad Active (@1,@2,@3)",
  338. coords.x, coords.y, coords.z))
  339. end
  340. return true
  341. end)
  342. -- teleport pad recipe
  343. if mcl then
  344. minetest.register_craft({
  345. output = "teleport_potion:pad",
  346. recipe = {
  347. {"teleport_potion:potion", "mcl_core:glass", "teleport_potion:potion"},
  348. {"mcl_core:glass", "mesecons:redstone", "mcl_core:glass"},
  349. {"teleport_potion:potion", "mcl_core:glass", "teleport_potion:potion"}
  350. }
  351. })
  352. else
  353. minetest.register_craft({
  354. output = "teleport_potion:pad",
  355. recipe = {
  356. {"teleport_potion:potion", "default:glass", "teleport_potion:potion"},
  357. {"default:glass", "default:mese", "default:glass"},
  358. {"teleport_potion:potion", "default:glass", "teleport_potion:potion"}
  359. }
  360. })
  361. end
  362. -- check portal & pad, teleport any entities on top
  363. minetest.register_abm({
  364. label = "Potion/Pad teleportation",
  365. nodenames = {"teleport_potion:portal", "teleport_potion:pad"},
  366. interval = 2,
  367. chance = 1,
  368. catch_up = false,
  369. action = function(pos, node, active_object_count, active_object_count_wider)
  370. -- check objects inside pad/portal
  371. local objs = minetest.get_objects_inside_radius(pos, 1)
  372. if #objs == 0 then
  373. return
  374. end
  375. -- get coords from pad/portal
  376. local meta = minetest.get_meta(pos)
  377. if not meta then return end -- errorcheck
  378. local target_coords = {
  379. x = meta:get_int("x"),
  380. y = meta:get_int("y"),
  381. z = meta:get_int("z")
  382. }
  383. for n = 1, #objs do
  384. if objs[n]:is_player() then
  385. -- play sound on portal end
  386. minetest.sound_play("portal_close", {
  387. pos = pos,
  388. gain = 1.0,
  389. max_hear_distance = 5
  390. }, true)
  391. -- move player
  392. objs[n]:set_pos(target_coords)
  393. -- paricle effects on arrival
  394. tp_effect(target_coords)
  395. -- play sound on destination end
  396. minetest.sound_play("portal_close", {
  397. pos = target_coords,
  398. gain = 1.0,
  399. max_hear_distance = 5
  400. }, true)
  401. -- rotate player to look in pad placement direction
  402. local rot = node.param2
  403. local yaw = 0
  404. if rot == 0 or rot == 20 then
  405. yaw = 0 -- north
  406. elseif rot == 2 or rot == 22 then
  407. yaw = 3.14 -- south
  408. elseif rot == 1 or rot == 23 then
  409. yaw = 4.71 -- west
  410. elseif rot == 3 or rot == 21 then
  411. yaw = 1.57 -- east
  412. end
  413. objs[n]:set_look_horizontal(yaw)
  414. end
  415. end
  416. end
  417. })
  418. -- lucky blocks
  419. if minetest.get_modpath("lucky_block") then
  420. lucky_block:add_blocks({
  421. {"dro", {"teleport_potion:potion"}, 2},
  422. {"tel"},
  423. {"dro", {"teleport_potion:pad"}, 1},
  424. {"lig"}
  425. })
  426. end
  427. print ("[MOD] Teleport Potion loaded")