message.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. -- This module is responsible for informing players of someone's death.
  2. -- We also inform the dead player where their bones are.
  3. -- We also write a message to the logfile.
  4. if not minetest.global_exists("bones") then bones = {} end
  5. bones.players = bones.players or {}
  6. -- Localize for performance.
  7. local math_random = math.random
  8. local release_player = function(player)
  9. if bones.players[player] then
  10. bones.players[player] = nil
  11. end
  12. end
  13. bones.release_player = release_player
  14. local msg_str1 = {
  15. "Blackbox",
  16. "Blackbox",
  17. "Bonebox",
  18. "Bonebox",
  19. "Bonebox",
  20. "Death signal",
  21. "Blackbox signal",
  22. "Bonebox signal",
  23. "Death beacon",
  24. "Blackbox beacon",
  25. "Bonebox beacon",
  26. "Ritual box",
  27. }
  28. local msg_str2 = {
  29. "perished",
  30. "perished",
  31. "perished",
  32. "died",
  33. "died",
  34. "died",
  35. "kicked the bucket",
  36. "kicked the bucket",
  37. "was killed",
  38. "was slain",
  39. "lost a life",
  40. "croaked",
  41. "bit the dust",
  42. "passed away",
  43. "passed on",
  44. "expired",
  45. "expired",
  46. "did something fatal",
  47. "became honestly dead",
  48. "became somewhat dead",
  49. "became mostly dead",
  50. "was involved in a fatal occurrence",
  51. "had a fatal accident",
  52. "is honestly dead",
  53. "is completely dead",
  54. "is mostly dead",
  55. "passed out (permanently)",
  56. "is somewhat dead",
  57. "suffered corporeal malfunction",
  58. "gave up the ghost",
  59. "gave up on life",
  60. "failed survival lessons",
  61. "threw out the book",
  62. "met the grim reaper",
  63. "ended life",
  64. "met a sticky end",
  65. "met a horrid end",
  66. "met a terrifying end",
  67. }
  68. local function random_str(strs)
  69. return strs[math_random(1, #strs)]
  70. end
  71. local send_chat_world = function(pos, player)
  72. -- Don't spam the message console.
  73. if bones.players[player] then return end
  74. local show_everyone = true
  75. if player_labels.query_nametag_onoff(player) == false then
  76. show_everyone = false
  77. end
  78. if cloaking.is_cloaked(player) then
  79. show_everyone = false
  80. end
  81. if show_everyone then
  82. local dname = rename.gpn(player)
  83. minetest.chat_send_all("# Server: " .. random_str(msg_str1) .. " detected. " ..
  84. "<" .. dname .. "> " .. random_str(msg_str2) .. " at " .. rc.pos_to_namestr(pos) .. ".")
  85. else
  86. minetest.chat_send_all("# Server: " .. random_str(msg_str1) .. " detected. ID and location unknown.")
  87. end
  88. -- Print this on the next server step, to ensure it is printed AFTER any other
  89. -- messages that should be printed first.
  90. minetest.after(0, function()
  91. minetest.chat_send_player(player, "# Server: You died at " .. rc.pos_to_namestr(pos) .. ".")
  92. minetest.chat_send_player(player, "# Server: Find your bone-loot at the above coordinates.")
  93. end)
  94. -- The player can't trigger any more chat messages until released.
  95. bones.players[player] = true
  96. minetest.after(60, release_player, player)
  97. return show_everyone
  98. end
  99. bones.do_messages = function(pos, player, num_stacks)
  100. minetest.log("action", "Player <" .. player .. "> died at (" .. pos.x .. "," .. pos.y .. "," .. pos.z .. "): stackcount=" .. num_stacks)
  101. return send_chat_world(pos, player)
  102. end
  103. -- Note: this is not called if the player is cloaked or their ID is off.
  104. function bones.death_reason(pname, reason)
  105. local dname = rename.gpn(pname)
  106. --minetest.chat_send_all('message dump: ' .. dump(reason))
  107. -- WARNING: do not do if reason.type == "punch", that will create a bug!
  108. if reason.type == "fall" then
  109. minetest.chat_send_all("# Server: <" .. dname .. "> fell.")
  110. elseif reason.type == "drown" then
  111. minetest.chat_send_all("# Server: <" .. dname .. "> drowned.")
  112. elseif reason.reason == "node_damage" then
  113. -- Note: the engine's builtin 'reason.type' is not used here, because the
  114. -- armor code nulifies it and converts it to a punch.
  115. if reason.source_node then
  116. local ndef = minetest.registered_nodes[reason.source_node]
  117. if ndef and ndef._death_message then
  118. local msg = ndef._death_message
  119. if type(msg) == "table" then
  120. msg = msg[math.random(1, #msg)]
  121. elseif type(msg) == "function" then
  122. msg = msg()
  123. end
  124. -- Assume message is string.
  125. if type(msg) == "string" then
  126. msg = msg:gsub("<player>", "<" .. dname .. ">")
  127. minetest.chat_send_all("# Server: " .. msg)
  128. end
  129. end
  130. end
  131. end
  132. end