init.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. jail = jail or {}
  2. jail.modpath = minetest.get_modpath("jail")
  3. jail.noclip_radius = 15 -- Max distance of player from jail.
  4. -- Localize vector.distance() for performance.
  5. local vector_distance = vector.distance
  6. local vector_round = vector.round
  7. local vector_add = vector.add
  8. function jail.notify_jail_destruct(pos)
  9. local p1 = vector_add(pos, {x=-1, y=0, z=-1})
  10. local p2 = vector_add(pos, {x=1, y=0, z=1})
  11. local positions, counts = minetest.find_nodes_in_area(p1, p2, "city_block:cityblock")
  12. for k, v in ipairs(positions) do
  13. city_block.erase_jail(v)
  14. end
  15. end
  16. function jail.get_nearest_jail_pos(player)
  17. local pp = vector_round(player:get_pos())
  18. -- Find the location of the nearest player-constructed jail.
  19. local jails = city_block:nearest_jails_to_position(pp, 1, 3000)
  20. if jails[1] then
  21. return vector_add(jails[1].pos, {x=0, y=1, z=0})
  22. end
  23. end
  24. -- This function shall be called only when player escapes jail via hack, etc.
  25. -- Shall return the player to the nearest jail within their current dimension.
  26. function jail.on_player_escaped_jail(pref)
  27. local jp = jail.get_nearest_jail_pos(pref)
  28. if not jp then
  29. -- If there's no nearby jail we might as well let them out.
  30. -- As if the player died.
  31. jail.notify_player_death(pref)
  32. return
  33. end
  34. default.detach_player_if_attached(pref) -- Otherwise teleport could fail.
  35. local pname = pref:get_player_name()
  36. local cb = function(pname)
  37. local pref = minetest.get_player_by_name(pname)
  38. if pref then
  39. -- AFTER player has been teleported back, damage them.
  40. pref:set_pos(jp)
  41. pref:set_hp(pref:get_hp() - 1)
  42. end
  43. end
  44. preload_tp.execute({
  45. player_name = pname,
  46. target_position = jp,
  47. emerge_radius = 8,
  48. post_teleport_callback = cb,
  49. callback_param = pname,
  50. force_teleport = true,
  51. send_blocks = false,
  52. particle_effects = false,
  53. })
  54. end
  55. function jail.is_player_in_jail(pref)
  56. local jp = jail.get_nearest_jail_pos(pref) -- Get position of jail.
  57. if not jp then
  58. return false -- No jails available, player cannot be in one.
  59. end
  60. local pp = pref:get_pos() -- Position of player.
  61. local dt = vector_distance(jp, pp) -- Distance between points.
  62. if dt > jail.noclip_radius then
  63. return false -- Player is NOT in jail!
  64. end
  65. return true -- Player is in jail.
  66. end
  67. function jail.check_player_in_jail(pname)
  68. local pref = minetest.get_player_by_name(pname)
  69. if pref then
  70. local meta = pref:get_meta()
  71. if meta:get_int("should_be_in_jail") == 1 then
  72. -- Here we check to make sure player actually is still in jail!
  73. if not jail.is_player_in_jail(pref) then
  74. jail.on_player_escaped_jail(pref)
  75. end
  76. -- Check again in 1 second.
  77. minetest.after(1, jail.check_player_in_jail, pname)
  78. end -- Else player is no longer marked to be in jail.
  79. end -- Else player has logged out.
  80. -- Checks will resume when player (any player) logs in again.
  81. end
  82. function jail.go_to_jail(player, bcb)
  83. local pname = player:get_player_name()
  84. local fwrap = function(...)
  85. local pref = minetest.get_player_by_name(pname)
  86. if pref then
  87. jail.notify_sent_to_jail(pref)
  88. end
  89. if bcb then
  90. bcb(...)
  91. end
  92. end
  93. local jailpos = jail.get_nearest_jail_pos(player)
  94. if not jailpos then
  95. return -- Failed to send player to jail.
  96. end
  97. preload_tp.execute({
  98. player_name = pname,
  99. target_position = jailpos,
  100. emerge_radius = 32,
  101. post_teleport_callback = fwrap,
  102. force_teleport = true,
  103. send_blocks = true,
  104. particle_effects = true,
  105. })
  106. -- Player should be sent to jail successfully, no reason for error right now.
  107. return true
  108. end
  109. function jail.notify_sent_to_jail(pref)
  110. -- Set key on player indicating that they should currently be in jail.
  111. -- This key should be cleared only if they leave jail through legit means!
  112. local meta = pref:get_meta()
  113. meta:set_int("should_be_in_jail", 1)
  114. minetest.after(1, jail.check_player_in_jail, pref:get_player_name())
  115. end
  116. function jail.notify_player_death(pref)
  117. local meta = pref:get_meta()
  118. meta:set_int("should_be_in_jail", 0)
  119. end
  120. if not jail.registered then
  121. local c = "jail:core"
  122. local f = jail.modpath .. "/init.lua"
  123. reload.register_file(c, f, false)
  124. local jail_data = {
  125. name = "Jail",
  126. codename = "jail:jail",
  127. position = function(...) return jail.get_nearest_jail_pos(...) end,
  128. min_dist = 30,
  129. }
  130. jail_data.on_success = function(name)
  131. local pref = minetest.get_player_by_name(name)
  132. if pref then
  133. jail.notify_sent_to_jail(pref)
  134. local dname = rename.gpn(name)
  135. minetest.chat_send_all("# Server: <" .. dname .. "> sent to jail for no particular reason.")
  136. end
  137. end
  138. jail_data.suppress = function(name)
  139. local player = minetest.get_player_by_name(name)
  140. if player and player:is_player() then
  141. if jail.is_player_in_jail(player) then
  142. minetest.chat_send_player(name, "# Server: Error: security override. Recall is disabled within convict re-education block.")
  143. easyvend.sound_error(name)
  144. return true -- Too close to jail.
  145. end
  146. end
  147. end
  148. jail.suppress = jail_data.suppress
  149. -- The jail recall is mandatory.
  150. -- It is not grouped with other recall buttons in the passport formspec.
  151. passport.register_recall(jail_data)
  152. minetest.register_on_joinplayer(function(pref)
  153. minetest.after(5, jail.check_player_in_jail, pref:get_player_name())
  154. end)
  155. jail.registered = true
  156. end