init.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. jaunt = jaunt or {}
  2. jaunt.modpath = minetest.get_modpath("jaunt")
  3. jaunt.jump_range = 3000
  4. -- Localize for performance.
  5. local vector_distance = vector.distance
  6. local vector_round = vector.round
  7. -- private: assemble a formspec string
  8. function jaunt.get_formspec(player)
  9. local formspec = "size[4.5,5.5]" ..
  10. default.gui_bg ..
  11. default.gui_bg_img ..
  12. default.gui_slots
  13. formspec = formspec ..
  14. "item_image[0,0;1,1;passport:passport_adv]" ..
  15. "label[1,0;Key: Teleport to player beacon.]" ..
  16. "label[1,0.4;Requires teleport for anchor.]" ..
  17. "field[0.3,1.3;2.9,1;player;;]" ..
  18. "button[3.0,1.0;1.5,1;go;Jaunt]" ..
  19. "button[1.25,2.0;2.25,1;cancel;Abort]" ..
  20. "label[0,3;Jaunt range is influenced by the status\nof the target's beacon. Marked players can\nbe found from farther.]" ..
  21. "item_image[1.25,4.5;1,1;command_tokens:mark_player]" ..
  22. "item_image[2.25,4.5;1,1;player_labels:show]"
  23. return formspec
  24. end
  25. -- api: show formspec to player
  26. function jaunt.show_formspec(player)
  27. local formspec = jaunt.get_formspec(player)
  28. minetest.show_formspec(player, "jaunt:fs", formspec)
  29. end
  30. jaunt.on_receive_fields = function(player, formname, fields)
  31. if formname ~= "jaunt:fs" then return end
  32. local pname = player:get_player_name()
  33. -- security check to make sure player can use this feature
  34. if not passport.player_has_key(pname) then
  35. return true
  36. end
  37. if not survivalist.player_beat_cave_challenge(pname) then
  38. return true
  39. end
  40. -- Jaunt ability is revoked for cheaters.
  41. if sheriff.is_cheater(pname) then
  42. return true
  43. end
  44. if fields.cancel then
  45. passport.show_formspec(pname)
  46. return true
  47. end
  48. if fields.quit then
  49. return true
  50. end
  51. local uspos = vector_round(player:get_pos())
  52. if fields.go then
  53. if minetest.find_node_near(uspos, 2, "teleports:teleport", true) then
  54. local target = rename.grn((fields.player or ""):trim())
  55. if target ~= pname then
  56. local other = minetest.get_player_by_name(target)
  57. if other and other:is_player() and not cloaking.is_cloaked(target) then
  58. local marked = command_tokens.mark.player_marked(target)
  59. local beacon = player_labels.query_nametag_onoff(target)
  60. -- a player can be located if either they're marked or their beacon is activated
  61. if marked or beacon then
  62. if passport.player_has_key(target) then
  63. -- if a player is marked, but their beacon is off, then the range at which
  64. -- they can be detected is halved
  65. local range = jaunt.jump_range
  66. if marked and not beacon then
  67. range = range / 2
  68. elseif marked and beacon then
  69. range = range * 2
  70. end
  71. local tarpos = vector_round(other:get_pos())
  72. if rc.current_realm_at_pos(tarpos) == rc.current_realm_at_pos(uspos) then
  73. if vector_distance(tarpos, uspos) < range then
  74. -- Alert player that someone's coming to them.
  75. local RED = core.get_color_escape_sequence("#ff0000")
  76. minetest.chat_send_player(target,
  77. RED .. "# Server: Alert! Incoming jaunt from <" ..
  78. rename.gpn(pname) .. ">.")
  79. chat_core.alert_player_sound(target)
  80. -- Teleport player to chosen location.
  81. preload_tp.execute({
  82. player_name = pname,
  83. target_position = tarpos,
  84. send_blocks = true,
  85. particle_effects = true,
  86. -- Pre-teleport callback.
  87. pre_teleport_callback = function()
  88. -- Abort teleport if target player cloaked themselves.
  89. if cloaking.is_cloaked(target) then
  90. minetest.chat_send_player(pname, "# Server: Lost link to target beacon.")
  91. return true -- Abort transport.
  92. end
  93. end,
  94. -- Post-teleport callback.
  95. post_teleport_callback = function()
  96. portal_sickness.on_use_portal(pname)
  97. end,
  98. })
  99. -- don't reshow the formspec
  100. minetest.close_formspec(pname, "jaunt:fs")
  101. return true
  102. else
  103. minetest.chat_send_player(pname, "# Server: Target Key's signal origin is too weak to accurately triangulate!")
  104. end
  105. else
  106. minetest.chat_send_player(pname, "# Server: Target's Key is not located in this realm!")
  107. end
  108. else
  109. minetest.chat_send_player(pname, "# Server: Target's beacon signal does not originate from an authentic Key device.")
  110. end
  111. else
  112. minetest.chat_send_player(pname, "# Server: Could not detect evidence of a Key's beacon signal.")
  113. end
  114. else
  115. minetest.chat_send_player(pname, "# Server: Could not detect evidence of a Key's beacon signal.")
  116. end
  117. else
  118. minetest.chat_send_player(pname, "# Server: Cleverly refusing to scan for your own Key's beacon signal.")
  119. end
  120. else
  121. minetest.chat_send_player(pname, "# Server: Your Key requires access to a proximate teleport to deploy this function.")
  122. end
  123. end
  124. jaunt.show_formspec(pname)
  125. return true
  126. end
  127. if not jaunt.registered then
  128. minetest.register_on_player_receive_fields(function(...)
  129. return jaunt.on_receive_fields(...)
  130. end)
  131. local c = "jaunt:core"
  132. local f = jaunt.modpath .. "/init.lua"
  133. reload.register_file(c, f, false)
  134. jaunt.registered = true
  135. end