init.lua 13 KB

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