init.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. if not minetest.global_exists("preload_tp") then preload_tp = {} end
  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. parameters.on_map_loaded = parameters.on_map_loaded or nil
  138. local pname = parameters.player_name
  139. local tpos = parameters.target_position
  140. local radius = parameters.emerge_radius
  141. local pre_cb = parameters.pre_teleport_callback
  142. local post_cb = parameters.post_teleport_callback
  143. local cb_param = parameters.callback_param
  144. local force = parameters.force_teleport
  145. local tpsound = parameters.teleport_sound
  146. local sendblocks = parameters.send_blocks
  147. local pfx = parameters.particle_effects
  148. local player = minetest.get_player_by_name(pname)
  149. if not player or not player:is_player() then
  150. return
  151. end
  152. local tp = table.copy(tpos)
  153. local pp = player:get_pos()
  154. local start_time = os.time()
  155. -- Time to teleport depends on distance.
  156. -- But allow calling code to override this for special cases.
  157. local total_time = math_floor(vector_distance(pp, tp) / 1000)
  158. if parameters.spinup_time then
  159. total_time = parameters.spinup_time
  160. end
  161. if total_time < 2 then
  162. total_time = 2
  163. end
  164. parameters.start_position = pp
  165. parameters.start_time = start_time
  166. parameters.total_time = total_time
  167. minetest.log("action", pname .. " initiates teleport to " .. minetest.pos_to_string(tp))
  168. local map_min, map_max = minetest.get_mapgen_edges()
  169. if tp.x < map_min.x or tp.y < map_min.y or tp.z < map_min.z or
  170. tp.x > map_max.x or tp.y > map_max.y or tp.z > map_max.z then
  171. minetest.chat_send_player(pname, "# Server: Internal error, void target destination.")
  172. return
  173. end
  174. if pfx then
  175. preload_tp.spawn_spinup_particles(vector_round(pp), total_time + 2)
  176. preload_tp.spawn_spinup_particles(vector_round(tp), total_time + 1)
  177. end
  178. -- Build callback function. When the map is loaded, we can teleport the player.
  179. local cb = function(blockpos, action, calls_remaining, parameters)
  180. -- Check if there was an error.
  181. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  182. minetest.chat_send_player(pname, "# Server: Internal error, block loading canceled.")
  183. return
  184. end
  185. -- Send blocks to client as soon as they're available; looks better that way.
  186. if sendblocks then
  187. if blockpos then
  188. local pref = minetest.get_player_by_name(pname)
  189. if pref then
  190. pref:send_mapblock(blockpos)
  191. end
  192. end
  193. end
  194. -- We don't do anything until the last callback.
  195. if calls_remaining ~= 0 then
  196. return
  197. end
  198. if parameters.on_map_loaded then
  199. parameters.on_map_loaded()
  200. end
  201. if not force then
  202. preload_tp.wait_for_timeout(parameters)
  203. return
  204. end
  205. -- Forced teleport always teleports as soon as possible!
  206. preload_tp.finalize(parameters)
  207. end
  208. local minp = vector.add(tp, vector.new(-radius, -radius, -radius))
  209. local maxp = vector.add(tp, vector.new(radius, radius, radius))
  210. minp.x = math.max(minp.x, map_min.x)
  211. minp.y = math.max(minp.y, map_min.y)
  212. minp.z = math.max(minp.z, map_min.z)
  213. maxp.x = math.min(maxp.x, map_max.x)
  214. maxp.y = math.min(maxp.y, map_max.y)
  215. maxp.z = math.min(maxp.z, map_max.z)
  216. -- Emerge the target area. Once emergence is complete player can be teleported.
  217. minetest.chat_send_player(pname, "# Server: Spatially translating! Stand by.")
  218. minetest.emerge_area(minp, maxp, cb, parameters)
  219. end
  220. local particles = {
  221. amount = 20,
  222. time = 1,
  223. -- ^ If time is 0 has infinite lifespan and spawns the amount on a per-second base
  224. minpos = {x=0, y=0, z=0},
  225. maxpos = {x=0, y=0, z=0},
  226. minvel = {x=0, y=6, z=0},
  227. maxvel = {x=0, y=9, z=0},
  228. minacc = {x=0, y=2, z=0},
  229. maxacc = {x=0, y=4, z=0},
  230. minexptime = 0.5,
  231. maxexptime = 1,
  232. minsize = 1.0,
  233. maxsize = 1.0,
  234. -- ^ The particle's properties are random values in between the bounds:
  235. -- ^ minpos/maxpos, minvel/maxvel (velocity), minacc/maxacc (acceleration),
  236. -- ^ minsize/maxsize, minexptime/maxexptime (expirationtime)
  237. collisiondetection = false,
  238. -- ^ collisiondetection: if true uses collision detection
  239. collision_removal = false,
  240. -- ^ collision_removal: if true then particle is removed when it collides,
  241. -- ^ requires collisiondetection = true to have any effect
  242. --attached = ObjectRef,
  243. -- ^ attached: if defined, particle positions, velocities and accelerations
  244. -- ^ are relative to this object's position and yaw.
  245. vertical = false,
  246. -- ^ vertical: if true faces player using y axis only
  247. texture = "nether_particle_anim1.png",
  248. animation = {
  249. type = "vertical_frames",
  250. aspect_w = 7,
  251. aspect_h = 7,
  252. -- Disabled for now due to causing older clients to hang.
  253. --length = -1,
  254. length = 0.3,
  255. },
  256. glow = 14,
  257. }
  258. function preload_tp.spawn_particles(pos)
  259. particles.minpos = vector.add(pos, {x=-1.0, y=-1.0, z=-1.0})
  260. particles.maxpos = vector.add(pos, {x=1.0, y=-1.0, z=1.0})
  261. minetest.add_particlespawner(particles)
  262. end
  263. if not preload_tp.run_once then
  264. local c = "preload_tp:core"
  265. local f = preload_tp.modpath .. "/init.lua"
  266. reload.register_file(c, f, false)
  267. preload_tp.run_once = true
  268. end