init.lua 13 KB

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