init.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. minetest.chat_send_player(
  43. pname, "# Server: Nope. You go right back to jail, crook!")
  44. end
  45. end
  46. preload_tp.execute({
  47. player_name = pname,
  48. target_position = jp,
  49. emerge_radius = 8,
  50. post_teleport_callback = cb,
  51. callback_param = pname,
  52. force_teleport = true,
  53. send_blocks = false,
  54. particle_effects = false,
  55. })
  56. end
  57. function jail.is_player_in_jail(pref)
  58. local jp = jail.get_nearest_jail_pos(pref) -- Get position of jail.
  59. if not jp then
  60. return false -- No jails available, player cannot be in one.
  61. end
  62. local pp = pref:get_pos() -- Position of player.
  63. local dt = vector_distance(jp, pp) -- Distance between points.
  64. if dt > jail.noclip_radius then
  65. return false -- Player is NOT in jail!
  66. end
  67. return true -- Player is in jail.
  68. end
  69. function jail.check_player_in_jail(pname)
  70. local pref = minetest.get_player_by_name(pname)
  71. if pref then
  72. local meta = pref:get_meta()
  73. if meta:get_int("should_be_in_jail") == 1 then
  74. -- Here we check to make sure player actually is still in jail!
  75. if not jail.is_player_in_jail(pref) then
  76. jail.on_player_escaped_jail(pref)
  77. end
  78. -- Check again in 1 second.
  79. minetest.after(1, jail.check_player_in_jail, pname)
  80. end -- Else player is no longer marked to be in jail.
  81. end -- Else player has logged out.
  82. -- Checks will resume when player (any player) logs in again.
  83. end
  84. function jail.go_to_jail(player, bcb)
  85. local pname = player:get_player_name()
  86. local fwrap = function(...)
  87. local pref = minetest.get_player_by_name(pname)
  88. if pref then
  89. jail.notify_sent_to_jail(pref)
  90. end
  91. if bcb then
  92. bcb(...)
  93. end
  94. end
  95. local jailpos = jail.get_nearest_jail_pos(player)
  96. if not jailpos then
  97. return -- Failed to send player to jail.
  98. end
  99. preload_tp.execute({
  100. player_name = pname,
  101. target_position = jailpos,
  102. emerge_radius = 32,
  103. post_teleport_callback = fwrap,
  104. force_teleport = true,
  105. send_blocks = true,
  106. particle_effects = true,
  107. })
  108. -- Player should be sent to jail successfully, no reason for error right now.
  109. return true
  110. end
  111. function jail.notify_sent_to_jail(pref)
  112. -- Set key on player indicating that they should currently be in jail.
  113. -- This key should be cleared only if they leave jail through legit means!
  114. local meta = pref:get_meta()
  115. meta:set_int("should_be_in_jail", 1)
  116. minetest.after(1, jail.check_player_in_jail, pref:get_player_name())
  117. end
  118. function jail.notify_player_death(pref)
  119. local meta = pref:get_meta()
  120. meta:set_int("should_be_in_jail", 0)
  121. end
  122. jail.discharge_pref = jail.notify_player_death
  123. if not jail.registered then
  124. local c = "jail:core"
  125. local f = jail.modpath .. "/init.lua"
  126. reload.register_file(c, f, false)
  127. local jail_data = {
  128. name = "Jail",
  129. codename = "jail:jail",
  130. position = function(...) return jail.get_nearest_jail_pos(...) end,
  131. min_dist = 30,
  132. }
  133. jail_data.on_success = function(name)
  134. local pref = minetest.get_player_by_name(name)
  135. if pref then
  136. jail.notify_sent_to_jail(pref)
  137. local dname = rename.gpn(name)
  138. minetest.chat_send_all("# Server: <" .. dname .. "> sent to jail for no particular reason.")
  139. end
  140. end
  141. jail_data.suppress = function(name)
  142. local player = minetest.get_player_by_name(name)
  143. if player and player:is_player() then
  144. if jail.is_player_in_jail(player) then
  145. minetest.chat_send_player(name, "# Server: Error: security override. Recall is disabled within convict re-education block.")
  146. easyvend.sound_error(name)
  147. return true -- Too close to jail.
  148. end
  149. end
  150. end
  151. jail.suppress = jail_data.suppress
  152. -- The jail recall is mandatory.
  153. -- It is not grouped with other recall buttons in the passport formspec.
  154. passport.register_recall(jail_data)
  155. minetest.register_on_joinplayer(function(pref)
  156. minetest.after(5, jail.check_player_in_jail, pref:get_player_name())
  157. end)
  158. jail.registered = true
  159. end