init.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. preload_tp = preload_tp or {}
  2. preload_tp.modpath = minetest.get_modpath("preload_tp")
  3. -- Localize for performance.
  4. local vector_distance = vector.distance
  5. local vector_round = vector.round
  6. local math_floor = math.floor
  7. function preload_tp.finalize(parameters)
  8. local pname = parameters.player_name
  9. local force = parameters.force_teleport
  10. local pp = parameters.start_position
  11. local tp = parameters.target_position
  12. local pre_cb = parameters.pre_teleport_callback
  13. local post_cb = parameters.post_teleport_callback
  14. local cb_param = parameters.callback_param
  15. local tpsound = parameters.teleport_sound
  16. local pfx = parameters.particle_effects
  17. -- Find the player.
  18. local player = minetest.get_player_by_name(pname)
  19. if not player or not player:is_player() then
  20. -- The player left, or something. Do not teleport them.
  21. minetest.log("action", pname .. " left the game while a teleport callback was in progress")
  22. return
  23. end
  24. -- The player may optionally be force-teleported.
  25. if not force then
  26. -- Did the player move?
  27. if vector_distance(pp, player:get_pos()) > 1.5 then
  28. minetest.chat_send_player(pname, "# Server: Transport error. You cannot move while a transport is in progress.")
  29. return
  30. end
  31. -- If the player killed themselves, do not teleport them.
  32. if player:get_hp() == 0 then
  33. minetest.chat_send_player(pname, "# Server: Transport error. You are dead.")
  34. return
  35. end
  36. end
  37. -- But we must never teleport a player who is attached.
  38. if default.player_attached[pname] then
  39. minetest.chat_send_player(pname, "# Server: Transport error. Player attached!")
  40. return
  41. end
  42. -- Execute the callback function, if everything else succeeded.
  43. if pre_cb then
  44. -- If the pre-teleport callback returns 'success' then that
  45. -- signals that the teleport must be aborted for some reason.
  46. if pre_cb(cb_param) then
  47. minetest.chat_send_player(pname, "# Server: Transport canceled.")
  48. return
  49. end
  50. end
  51. minetest.log("action", "executing teleport callback for " .. pname .. "!")
  52. -- Teleport player only if they didn't move (or teleporting is forced).
  53. wield3d.on_teleport()
  54. rc.notify_realm_update(player, tp)
  55. player:set_pos(tp)
  56. minetest.log("action", pname .. " actually teleports to " .. minetest.pos_to_string(tp))
  57. -- Execute the callback function, if everything else succeeded.
  58. if post_cb then
  59. post_cb(cb_param)
  60. end
  61. local thesound = "teleport"
  62. if type(tpsound) == "string" then
  63. thesound = tpsound
  64. end
  65. -- The teleport sound, played @ old & new locations.
  66. ambiance.sound_play(thesound, pp, 1.0, 50)
  67. ambiance.sound_play(thesound, tp, 1.0, 50)
  68. if pfx then
  69. preload_tp.spawn_particles(pp)
  70. preload_tp.spawn_particles(tp)
  71. preload_tp.spawn_spinup_particles(vector_round(tp), 3)
  72. end
  73. end
  74. function preload_tp.wait_for_timeout(parameters)
  75. local start_time = parameters.start_time
  76. local total_time = parameters.total_time
  77. local end_time = os.time()
  78. if (end_time - start_time) < total_time then
  79. minetest.after(1, function()
  80. preload_tp.wait_for_timeout(parameters)
  81. end)
  82. return
  83. end
  84. preload_tp.finalize(parameters)
  85. end
  86. function preload_tp.spawn_spinup_particles(pos, time)
  87. local xd = 1
  88. local zd = 1
  89. -- We used to use the coal & mese textures (ew).
  90. -- Now that we have better particles, we don't need multiple spawners.
  91. -- Can animate the particles, too.
  92. minetest.add_particlespawner({
  93. amount = 160,
  94. time = time,
  95. minpos = {x=pos.x-xd, y=pos.y-0, z=pos.z-zd},
  96. maxpos = {x=pos.x+xd, y=pos.y+2, z=pos.z+zd},
  97. minvel = {x=0, y=-1, z=0},
  98. maxvel = {x=0, y=1, z=0},
  99. minacc = {x=0, y=-1, z=0},
  100. maxacc = {x=0, y=1, z=0},
  101. minexptime = 1.0,
  102. maxexptime = 2.5,
  103. minsize = 1.0,
  104. maxsize = 1.0,
  105. collisiondetection = true,
  106. collision_removal = true,
  107. vertical = false,
  108. texture = "nether_particle_anim1.png",
  109. animation = {
  110. type = "vertical_frames",
  111. aspect_w = 7,
  112. aspect_h = 7,
  113. -- Disabled for now due to causing older clients to hang.
  114. --length = -1,
  115. length = 0.3,
  116. },
  117. glow = 14,
  118. })
  119. end
  120. -- API function. Preload the area, then teleport the player there
  121. -- only if they have not moved during the preload. After a successful
  122. -- teleport, execute the callback function if it's not nil.
  123. function preload_tp.execute(parameters)
  124. -- Copy table so we don't end up modifying the original.
  125. parameters = table.copy(parameters)
  126. -- Set default parameters.
  127. parameters.player_name = parameters.player_name or ""
  128. parameters.target_position = parameters.target_position or {x=0, y=0, z=0}
  129. parameters.emerge_radius = parameters.emerge_radius or 16
  130. parameters.pre_teleport_callback = parameters.pre_teleport_callback or nil
  131. parameters.post_teleport_callback = parameters.post_teleport_callback or nil
  132. parameters.callback_param = parameters.callback_param or nil
  133. parameters.force_teleport = parameters.force_teleport or false
  134. parameters.teleport_sound = parameters.teleport_sound or nil
  135. parameters.send_blocks = parameters.send_blocks or false
  136. parameters.particle_effects = parameters.particle_effects or false
  137. local pname = parameters.player_name
  138. local tpos = parameters.target_position
  139. local radius = parameters.emerge_radius
  140. local pre_cb = parameters.pre_teleport_callback
  141. local post_cb = parameters.post_teleport_callback
  142. local cb_param = parameters.callback_param
  143. local force = parameters.force_teleport
  144. local tpsound = parameters.teleport_sound
  145. local sendblocks = parameters.send_blocks
  146. local pfx = parameters.particle_effects
  147. local player = minetest.get_player_by_name(pname)
  148. if not player or not player:is_player() then
  149. return
  150. end
  151. local tp = table.copy(tpos)
  152. local pp = player:get_pos()
  153. local start_time = os.time()
  154. -- Time to teleport depends on distance.
  155. local total_time = math_floor(vector_distance(pp, tp) / 1000)
  156. if total_time < 2 then
  157. total_time = 2
  158. end
  159. parameters.start_position = pp
  160. parameters.start_time = start_time
  161. parameters.total_time = total_time
  162. minetest.log("action", pname .. " initiates teleport to " .. minetest.pos_to_string(tp))
  163. if pfx then
  164. preload_tp.spawn_spinup_particles(vector_round(pp), total_time + 2)
  165. preload_tp.spawn_spinup_particles(vector_round(tp), total_time + 1)
  166. end
  167. -- Build callback function. When the map is loaded, we can teleport the player.
  168. local cb = function(blockpos, action, calls_remaining, parameters)
  169. -- Check if there was an error.
  170. -- This avoids false error reports if the area to be generated exceeds the max map edge.
  171. -- Update: actually it doesn't?
  172. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  173. minetest.chat_send_player(pname, "# Server: Internal error, try again or report.")
  174. return
  175. end
  176. -- Send blocks to client as soon as they're available; looks better that way.
  177. if sendblocks then
  178. if blockpos then
  179. local pref = minetest.get_player_by_name(pname)
  180. if pref then
  181. pref:send_mapblock(blockpos)
  182. end
  183. end
  184. end
  185. -- We don't do anything until the last callback.
  186. if calls_remaining ~= 0 then
  187. return
  188. end
  189. if not force then
  190. preload_tp.wait_for_timeout(parameters)
  191. return
  192. end
  193. -- Forced teleport always teleports as soon as possible!
  194. preload_tp.finalize(parameters)
  195. end
  196. local minp = vector.add(tp, vector.new(-radius, -radius, -radius))
  197. local maxp = vector.add(tp, vector.new(radius, radius, radius))
  198. -- Emerge the target area. Once emergence is complete player can be teleported.
  199. minetest.chat_send_player(pname, "# Server: Spatially translating! Stand by.")
  200. minetest.emerge_area(minp, maxp, cb, parameters)
  201. end
  202. local particles = {
  203. amount = 20,
  204. time = 1,
  205. -- ^ If time is 0 has infinite lifespan and spawns the amount on a per-second base
  206. minpos = {x=0, y=0, z=0},
  207. maxpos = {x=0, y=0, z=0},
  208. minvel = {x=0, y=6, z=0},
  209. maxvel = {x=0, y=9, z=0},
  210. minacc = {x=0, y=2, z=0},
  211. maxacc = {x=0, y=4, z=0},
  212. minexptime = 0.5,
  213. maxexptime = 1,
  214. minsize = 1.0,
  215. maxsize = 1.0,
  216. -- ^ The particle's properties are random values in between the bounds:
  217. -- ^ minpos/maxpos, minvel/maxvel (velocity), minacc/maxacc (acceleration),
  218. -- ^ minsize/maxsize, minexptime/maxexptime (expirationtime)
  219. collisiondetection = false,
  220. -- ^ collisiondetection: if true uses collision detection
  221. collision_removal = false,
  222. -- ^ collision_removal: if true then particle is removed when it collides,
  223. -- ^ requires collisiondetection = true to have any effect
  224. --attached = ObjectRef,
  225. -- ^ attached: if defined, particle positions, velocities and accelerations
  226. -- ^ are relative to this object's position and yaw.
  227. vertical = false,
  228. -- ^ vertical: if true faces player using y axis only
  229. texture = "nether_particle_anim1.png",
  230. animation = {
  231. type = "vertical_frames",
  232. aspect_w = 7,
  233. aspect_h = 7,
  234. -- Disabled for now due to causing older clients to hang.
  235. --length = -1,
  236. length = 0.3,
  237. },
  238. }
  239. function preload_tp.spawn_particles(pos)
  240. particles.minpos = vector.add(pos, {x=-1.0, y=-1.0, z=-1.0})
  241. particles.maxpos = vector.add(pos, {x=1.0, y=-1.0, z=1.0})
  242. minetest.add_particlespawner(particles)
  243. end
  244. if not preload_tp.run_once then
  245. local c = "preload_tp:core"
  246. local f = preload_tp.modpath .. "/init.lua"
  247. reload.register_file(c, f, false)
  248. preload_tp.run_once = true
  249. end