init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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_yaw() + math.pi)
  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. minetest.register_craft({
  222. output = "teleport_potion:potion",
  223. recipe = {
  224. {"", "default:diamond", ""},
  225. {"default:diamond", "vessels:glass_bottle", "default:diamond"},
  226. {"", "default:diamond", ""},
  227. },
  228. })
  229. --------------------------------------------------------------------------------
  230. --- Teleport pad
  231. --------------------------------------------------------------------------------
  232. local teleport_formspec_context = {}
  233. minetest.register_node("teleport_potion:pad", {
  234. tiles = {"padd.png", "padd.png^[transformFY"},
  235. drawtype = "nodebox",
  236. paramtype = "light",
  237. paramtype2 = "facedir",
  238. legacy_wallmounted = true,
  239. walkable = true,
  240. sunlight_propagates = true,
  241. description = S("Teleport Pad (use to set destination; place to open portal)"),
  242. inventory_image = "padd.png",
  243. wield_image = "padd.png",
  244. light_source = 5,
  245. groups = {snappy = 3},
  246. node_box = {
  247. type = "fixed",
  248. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  249. },
  250. selection_box = {
  251. type = "fixed",
  252. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  253. },
  254. -- Save pointed nodes coordinates as destination for further portals
  255. on_use = function(itemstack, user, pointed_thing)
  256. if pointed_thing.type == "node" then
  257. set_teleport_destination(user:get_player_name(), pointed_thing.above)
  258. end
  259. end,
  260. -- Initialize teleport to saved location or the current position
  261. after_place_node = function(pos, placer, itemstack, pointed_thing)
  262. local meta = minetest.get_meta(pos)
  263. local name = placer:get_player_name()
  264. local dest = teleport_destinations[name]
  265. if not dest then
  266. dest = pos
  267. end
  268. -- Set coords
  269. meta:set_int("x", dest.x)
  270. meta:set_int("y", dest.y)
  271. meta:set_int("z", dest.z)
  272. meta:set_string("infotext", S("Pad Active (@1,@2,@3)",
  273. dest.x, dest.y, dest.z))
  274. minetest.sound_play("portal_open", {
  275. pos = pos,
  276. gain = 1.0,
  277. max_hear_distance = 10
  278. })
  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. minetest.register_craft({
  344. output = "teleport_potion:pad",
  345. recipe = {
  346. {"teleport_potion:potion", "default:glass", "teleport_potion:potion"},
  347. {"default:glass", "default:mese", "default:glass"},
  348. {"teleport_potion:potion", "default:glass", "teleport_potion:potion"}
  349. }
  350. })
  351. -- check portal & pad, teleport any entities on top
  352. minetest.register_abm({
  353. label = "Potion/Pad teleportation",
  354. nodenames = {"teleport_potion:portal", "teleport_potion:pad"},
  355. interval = 2,
  356. chance = 1,
  357. catch_up = false,
  358. action = function(pos, node, active_object_count, active_object_count_wider)
  359. -- check objects inside pad/portal
  360. local objs = minetest.get_objects_inside_radius(pos, 1)
  361. if #objs == 0 then
  362. return
  363. end
  364. -- get coords from pad/portal
  365. local meta = minetest.get_meta(pos)
  366. if not meta then return end -- errorcheck
  367. local target_coords = {
  368. x = meta:get_int("x"),
  369. y = meta:get_int("y"),
  370. z = meta:get_int("z")
  371. }
  372. for n = 1, #objs do
  373. if objs[n]:is_player() then
  374. -- play sound on portal end
  375. minetest.sound_play("portal_close", {
  376. pos = pos,
  377. gain = 1.0,
  378. max_hear_distance = 5
  379. })
  380. -- move player
  381. objs[n]:set_pos(target_coords)
  382. -- paricle effects on arrival
  383. tp_effect(target_coords)
  384. -- play sound on destination end
  385. minetest.sound_play("portal_close", {
  386. pos = target_coords,
  387. gain = 1.0,
  388. max_hear_distance = 5
  389. })
  390. -- rotate player to look in pad placement direction
  391. local rot = node.param2
  392. local yaw = 0
  393. if rot == 0 or rot == 20 then
  394. yaw = 0 -- north
  395. elseif rot == 2 or rot == 22 then
  396. yaw = 3.14 -- south
  397. elseif rot == 1 or rot == 23 then
  398. yaw = 4.71 -- west
  399. elseif rot == 3 or rot == 21 then
  400. yaw = 1.57 -- east
  401. end
  402. objs[n]:set_look_yaw(yaw)
  403. end
  404. end
  405. end
  406. })
  407. -- add lucky blocks
  408. -- Teleport Potion mod
  409. if minetest.get_modpath("lucky_block") then
  410. lucky_block:add_blocks({
  411. {"dro", {"teleport_potion:potion"}, 2},
  412. {"tel"},
  413. {"dro", {"teleport_potion:pad"}, 1},
  414. {"lig"},
  415. })
  416. end
  417. print ("[MOD] Teleport Potion loaded")