functions.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. -- This file is reloadable.
  2. if not minetest.global_exists("bones") then bones = {} end
  3. -- Localize for performance.
  4. local vector_round = vector.round
  5. local math_random = math.random
  6. local get_public_time = function()
  7. return os.date("!%Y/%m/%d, %H:%M:%S UTC")
  8. end
  9. local share_bones_time = 1200
  10. local share_bones_time_early = (share_bones_time * 0.75)
  11. local share_bones_time_city = (share_bones_time * 10.0)
  12. local function is_owner(pos, name)
  13. local owner = minetest.get_meta(pos):get_string("owner")
  14. -- Don't let dead players pick bones.
  15. local player = minetest.get_player_by_name(name)
  16. if not player or not player:is_player() then return false end
  17. if player:get_hp() <= 0 then return false end
  18. if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then
  19. return true
  20. end
  21. return false
  22. end
  23. local function may_replace(pos, player)
  24. local nn = minetest.get_node(pos).name
  25. local ndef = minetest.reg_ns_nodes[nn]
  26. -- If the node is unknown, we return false.
  27. if not ndef then
  28. return false
  29. end
  30. -- Allow replacing air and (non-walkable) liquids.
  31. if nn == "air" or (ndef.liquidtype ~= "none" and not ndef.walkable) then
  32. return true
  33. end
  34. -- Don't replace filled chests and other nodes that don't allow it.
  35. -- Includes bedrock, admin TNT, etc.
  36. local can_dig = ndef.can_dig
  37. if can_dig and not can_dig(pos, player) then
  38. return false
  39. end
  40. local pname = player and player:get_player_name() or ""
  41. local protected = minetest.test_protection(pos, pname)
  42. -- Default to each nodes buildable_to; if a placed block would replace it, why
  43. -- shouldn't bones? Flowers being squished by bones are more realistical than
  44. -- a squished stone, too. Exception are of course any protected buildable_to.
  45. return ndef.buildable_to and not protected
  46. end
  47. bones.may_replace = may_replace
  48. local drop = function(pos, itemstack)
  49. local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
  50. if obj then
  51. obj:set_velocity({
  52. x = math_random(-10, 10) / 9,
  53. y = 5,
  54. z = math_random(-10, 10) / 9,
  55. })
  56. end
  57. end
  58. local player_inventory_empty = function(inv, name)
  59. local list = inv:get_list(name)
  60. -- Nil check.
  61. if not list then
  62. -- Could not get list, list does not exist.
  63. -- This is true in the case of bags.
  64. return true -- Inventory list doesn't exist.
  65. end
  66. for i = 1, #list do
  67. local stack = list[i]
  68. if not stack:is_empty() then
  69. if not passport.is_passport(stack:get_name()) then
  70. return false -- Not empty.
  71. end
  72. end
  73. end
  74. return true -- Inventory list is empty.
  75. end
  76. local clear_player_inventory = function(inv, name)
  77. local list = inv:get_list(name)
  78. -- Nil check.
  79. if not list then
  80. -- Could not get list, list does not exist.
  81. -- This is true in the case of bags.
  82. return -- Inventory list doesn't exist.
  83. end
  84. for i = 1, #list do
  85. local stack = list[i]
  86. if not stack:is_empty() then
  87. if not passport.is_passport(stack:get_name()) then
  88. local newstack = ItemStack()
  89. list[i] = newstack
  90. end
  91. end
  92. end
  93. inv:set_list(name, list)
  94. end
  95. local clear_hand_after = function(pname)
  96. minetest.after(0, function()
  97. local ply = minetest.get_player_by_name(pname)
  98. if not ply then return end
  99. local inv = ply:get_inventory()
  100. if not inv then return end
  101. if inv:is_empty("hand") then return end
  102. inv:set_list("hand", {}) -- Fixes a bug if the player dies while holding something.
  103. end)
  104. end
  105. local find_ground = function(pos, player)
  106. return armor.find_ground_by_raycast(pos, player)
  107. --[[
  108. local p = {x=pos.x, y=pos.y, z=pos.z}
  109. local count = 0
  110. local cr1 = may_replace({x=p.x, y=p.y, z=p.z}, player)
  111. local cr2 = may_replace({x=p.x, y=p.y-1, z=p.z}, player)
  112. while (not (cr1 and not cr2)) or (cr1 and cr2) do
  113. p.y = p.y - 1
  114. count = count + 1
  115. if count > 10 then return pos end
  116. cr1 = may_replace({x=p.x, y=p.y, z=p.z}, player)
  117. cr2 = may_replace({x=p.x, y=p.y-1, z=p.z}, player)
  118. end
  119. -- Return position, if we can replace to it, but not to the node under it.
  120. if cr1 and not cr2 then
  121. return p
  122. end
  123. return pos
  124. --]]
  125. end
  126. local function find_suitable_bone_location(pos, player)
  127. -- Locate position directly above ground level.
  128. local air = find_ground(pos, player)
  129. -- Check whether the initial location is suitable.
  130. if not may_replace(air, player) then
  131. air = nil
  132. end
  133. -- Is the initial location not suitable? Try to locate a suitable location on
  134. -- a horizontal plane from the actual death location.
  135. if not air then
  136. local sidepos = {
  137. {x=pos.x, y=pos.y, z=pos.z},
  138. {x=pos.x+1, y=pos.y, z=pos.z},
  139. {x=pos.x-1, y=pos.y, z=pos.z},
  140. {x=pos.x, y=pos.y, z=pos.z+1},
  141. {x=pos.x, y=pos.y, z=pos.z-1},
  142. }
  143. for k, v in ipairs(sidepos) do
  144. if may_replace(v, player) then
  145. air = v
  146. break
  147. end
  148. end
  149. end
  150. -- Didn't find a replaceable node that way, try a short-range search for air.
  151. if not air then
  152. air = minetest.find_node_near(pos, 2, {"air"})
  153. end
  154. -- Still didn't find air? Try a longer range search,
  155. -- and include liquid/fire/etc nodes that player can die in.
  156. if not air then
  157. -- If we couldn't find air, fallback to finding a location in these
  158. -- substances. Search several meters farther than at first.
  159. air = minetest.find_node_near(pos, 5, {
  160. "air",
  161. "default:water_source",
  162. "default:water_flowing",
  163. "cw:water_source",
  164. "cw:water_flowing",
  165. "default:river_water_source",
  166. "default:river_water_flowing",
  167. "default:lava_flowing",
  168. "lbrim:lava_flowing",
  169. "group:gas",
  170. "rackstone:nether_grit",
  171. "rackstone:void",
  172. "fire:basic_flame",
  173. "handholds:climbable_air",
  174. -- Note: these two are excluded due to being solid nodes.
  175. -- Don't try to place bones here.
  176. --"default:lava_source",
  177. --"lbrim:lava_source",
  178. })
  179. end
  180. -- If we found air, try to make sure we found a replaceable node directly
  181. -- above ground.
  182. if air then
  183. air = find_ground(air, player)
  184. end
  185. -- By now, we have either found air or nothing.
  186. return air -- May be nil.
  187. end
  188. -- This function may be called from mods to dump player bones EXACTLY as if the
  189. -- player had died, including any side-effects. The sole exception is that the
  190. -- player's health is not actually changed. You might need to update a few other
  191. -- datums too, to get exactly the right result.
  192. function bones.dump_bones(pname, preserve_xp)
  193. local player = minetest.get_player_by_name(pname)
  194. if player then
  195. bones.on_dieplayer(player, {type="none"}, preserve_xp)
  196. end
  197. end
  198. bones.on_dieplayer = function(player, reason, preserve_xp)
  199. local bones_mode = "bones"
  200. local player_inv = player:get_inventory()
  201. local player_meta = player:get_meta()
  202. local pname = player:get_player_name()
  203. local death_time = os.time()
  204. -- If a notified reason is available, use that instead.
  205. if reason.type == "punch" or reason.type == "set_hp" then
  206. local huh = armor.get_death_reason(reason)
  207. if huh then
  208. reason = huh
  209. end
  210. end
  211. --minetest.chat_send_all('ondieplayer dump: ' .. dump(reason))
  212. --minetest.log(dump(reason))
  213. -- If player died while attached to cart/boat/etc, they must be detached.
  214. default.detach_player_if_attached(player)
  215. -- Record position of player on death.
  216. -- This is needed because this information is lost on respawn.
  217. -- We must record this info *always*, even if player does not leave bones.
  218. local death_pos = vector.round(player:get_pos())
  219. player_meta:set_string("last_death_pos", minetest.pos_to_string(death_pos))
  220. player_meta:set_string("last_death_time", tostring(death_time))
  221. -- If the player died in MIDFELD, record that fact with a flag.
  222. -- Note: flag is NOT cleared until player returns to MIDFELD via OUTBACK gate.
  223. -- (It is also cleared on respawn if player had a bed in MIDFELD.)
  224. if rc.current_realm_at_pos(death_pos) == "midfeld" then
  225. player:get_meta():set_int("abyss_return_midfeld", 1)
  226. end
  227. local pos = vector_round(utility.get_middle_pos(player:get_pos()))
  228. -- Record all player deaths, whether they leave bones or not.
  229. minetest.log("action", "player <" .. pname .. "> died @ " .. minetest.pos_to_string(pos))
  230. -- Death sound.
  231. coresounds.play_death_sound(player, pname)
  232. -- Notify of death.
  233. chat_colorize.notify_death(pname)
  234. jail.notify_player_death(player)
  235. -- Special case for admin. This helps prevent accidentally leaving
  236. -- admin/cheated stuff lying around.
  237. if gdac.player_is_admin(pname) then
  238. return
  239. end
  240. -- Don't make bones if player doesn't have anything.
  241. -- This also means that player won't lose XP. Keep this, it is a feature!
  242. if player_inventory_empty(player_inv, "main") and
  243. player_inventory_empty(player_inv, "craft") and
  244. player_inventory_empty(player_inv, "bag1contents") and
  245. player_inventory_empty(player_inv, "bag2contents") and
  246. player_inventory_empty(player_inv, "bag3contents") and
  247. player_inventory_empty(player_inv, "bag4contents") and
  248. player_inventory_empty(player_inv, "bag1") and
  249. player_inventory_empty(player_inv, "bag2") and
  250. player_inventory_empty(player_inv, "bag3") and
  251. player_inventory_empty(player_inv, "bag4") and
  252. player_inventory_empty(player_inv, "armor") then
  253. return
  254. end
  255. -- Check if it's possible to place bones, if not find space near player.
  256. if bones_mode == "bones" then
  257. local boneloc = find_suitable_bone_location(pos, player)
  258. if boneloc then
  259. pos = boneloc
  260. else
  261. bones_mode = "drop"
  262. end
  263. end
  264. if not rc.is_valid_realm_pos(pos) then
  265. bones_mode = "drop"
  266. end
  267. -- If player died and ought to leave bones because they have stuff in their
  268. -- inventory, BUT we cannot actually place bones (no suitable location found),
  269. -- AND PLAYER HAS A BED, then player doesn't lose their items. This is a
  270. -- safety feature since it is sometimes possible to die in locations where
  271. -- bones cannot physically be placed. E.g., player dies in a 1x1 mineshaft
  272. -- occupied by ladder nodes.
  273. if bones_mode ~= "bones" then
  274. -- Cannot create bones, therefore we don't modify player inventories.
  275. minetest.log("action", "Player <" .. pname .. "> died @ " .. minetest.pos_to_string(pos) .. ", but cannot create bones!")
  276. if not preserve_xp then
  277. -- Reduce player's mining XP without storing it anywhere.
  278. -- Prevents player from being able to use this as an exploit.
  279. -- Death should always have a cost!
  280. local xp_amount = xp.get_xp(pname, "digxp")
  281. xp_amount = (xp_amount / 3) * 2
  282. xp.set_xp(pname, "digxp", xp_amount)
  283. hud_clock.update_xp(pname)
  284. end
  285. -- If player died without a bed, they will return to the Outback when they
  286. -- respawn. Since we cannot allow this exploit, there is only one option.
  287. -- The inventory must be erased WITHOUT moving the items into bones.
  288. if beds.get_respawn_count(pname) == 0 then
  289. minetest.log("Player <" .. pname .. "> lost their bones due to dying in solid nodes.")
  290. clear_player_inventory(player_inv, "main")
  291. clear_player_inventory(player_inv, "craft")
  292. clear_player_inventory(player_inv, "bag1contents")
  293. clear_player_inventory(player_inv, "bag2contents")
  294. clear_player_inventory(player_inv, "bag3contents")
  295. clear_player_inventory(player_inv, "bag4contents")
  296. clear_player_inventory(player_inv, "bag1")
  297. clear_player_inventory(player_inv, "bag2")
  298. clear_player_inventory(player_inv, "bag3")
  299. clear_player_inventory(player_inv, "bag4")
  300. clear_player_inventory(player_inv, "armor")
  301. player_inv:set_list("craftpreview", {})
  302. player_inv:set_list("craftresult", {})
  303. local bags_inv = minetest.get_inventory({type="detached", name=pname .. "_bags"})
  304. bags_inv:set_list("bag1", {})
  305. bags_inv:set_list("bag2", {})
  306. bags_inv:set_list("bag3", {})
  307. bags_inv:set_list("bag4", {})
  308. clear_hand_after(pname)
  309. end
  310. return
  311. end
  312. local xp_for_bones = 0.0
  313. -- Halve player XP!
  314. if not preserve_xp then
  315. local xp_amount = xp.get_xp(pname, "digxp")
  316. -- You lose 25% or 10K XP, whichever is less.
  317. local xp_to_take = xp_amount / 4
  318. if xp_to_take > 10000 then xp_to_take = 10000 end
  319. xp_amount = xp_amount - xp_to_take
  320. if xp_amount < 0 then xp_amount = 0 end
  321. -- 75% of what you lost is put in the bones.
  322. xp_for_bones = xp_to_take * 0.75
  323. xp.set_xp(pname, "digxp", xp_amount)
  324. end
  325. -- Note: portal sickness only removed if player would leave bones.
  326. portal_sickness.on_die_player(pname)
  327. -- These inventories are always cleared.
  328. player_inv:set_list("craftpreview", {})
  329. player_inv:set_list("craftresult", {})
  330. -- Clear the virtual bags inventories.
  331. do
  332. local bags_inv = minetest.get_inventory({type="detached", name=pname .. "_bags"})
  333. bags_inv:set_list("bag1", {})
  334. bags_inv:set_list("bag2", {})
  335. bags_inv:set_list("bag3", {})
  336. bags_inv:set_list("bag4", {})
  337. end
  338. -- The player can die while holding something. Remove it.
  339. clear_hand_after(pname)
  340. -- Preload map just in case.
  341. local minp = vector.add(pos, -16)
  342. local maxp = vector.add(pos, 16)
  343. utility.ensure_map_loaded(minp, maxp)
  344. local param2 = minetest.dir_to_facedir(player:get_look_dir())
  345. minetest.add_node(pos, {name = "bones:bones", param2 = param2})
  346. bone_mark.add_corpse(pos, pname)
  347. local meta = minetest.get_meta(pos)
  348. meta:set_float("digxp", xp_for_bones)
  349. meta:set_string("diedate", get_public_time())
  350. meta:set_string("death_time", tostring(death_time))
  351. local inv = meta:get_inventory()
  352. inv:set_size("main", 200) -- Enuf space for everything!
  353. -- Keep track of how many stacks are stored in bones.
  354. local location = minetest.pos_to_string(pos)
  355. local num_stacks = 0
  356. minetest.log("action", "Put " .. xp_for_bones .. " XP in bones @ " .. location .. ".")
  357. -- Set player information.
  358. player_meta:set_string("last_bones_pos", minetest.pos_to_string(pos))
  359. player_meta:set_string("last_bones_time", tostring(death_time))
  360. -- Note: clear player inventory slot-by-slot to avoid clobbering PoC/KoC items.
  361. -- Empty the player's main inv. We must not to clobber any passports.
  362. do
  363. local list = player_inv:get_list("main")
  364. if list then -- Nil check necessary.
  365. for i = 1, #list do
  366. local stack = list[i]
  367. if not passport.is_passport(stack:get_name()) then
  368. if stack:get_count() > 0 and inv:room_for_item("main", stack) then
  369. inv:add_item("main", stack)
  370. minetest.log("action", "Put " .. stack:to_string() .. " in bones @ " .. location .. ".")
  371. num_stacks = num_stacks + 1
  372. -- Stack was added to bones inventory, remove it from list.
  373. list[i]:set_count(0)
  374. list[i]:set_name("")
  375. else
  376. drop(pos, stack)
  377. end
  378. end
  379. end
  380. player_inv:set_list("main", list)
  381. end
  382. end
  383. -- Empty the player's craft-grid. Passports are not preserved, here.
  384. do
  385. local list = player_inv:get_list("craft")
  386. if list then -- Nil check necessary.
  387. for i = 1, #list do
  388. local stack = list[i]
  389. if stack:get_count() > 0 and inv:room_for_item("main", stack) then
  390. inv:add_item("main", stack)
  391. minetest.log("action", "Put " .. stack:to_string() .. " in bones @ " .. location .. ".")
  392. num_stacks = num_stacks + 1
  393. else
  394. drop(pos, stack)
  395. end
  396. end
  397. player_inv:set_list("craft", {})
  398. end
  399. end
  400. -- Armor goes into bones after main and crafting grid items.
  401. armor.on_dieplayer(player, pos)
  402. -- Empty the bag slots. Passports are not preserved, here. (It should not be possible to store a passport in here, anyway.)
  403. for j = 1, 4, 1 do
  404. local bag = "bag" .. j
  405. local list = player_inv:get_list(bag)
  406. if list then -- Nil check necessary.
  407. for i = 1, #list do
  408. local stack = list[i]
  409. if stack:get_count() > 0 and inv:room_for_item("main", stack) then
  410. inv:add_item("main", stack)
  411. minetest.log("action", "Put " .. stack:to_string() .. " in bones @ " .. location .. ".")
  412. num_stacks = num_stacks + 1
  413. else
  414. drop(pos, stack)
  415. end
  416. end
  417. player_inv:set_list(bag, {})
  418. end
  419. end
  420. -- Empty the bag inventories. Passports are not preserved, here.
  421. for j = 1, 4, 1 do
  422. local bag = "bag" .. j .. "contents"
  423. local list = player_inv:get_list(bag)
  424. if list then -- Nil check necessary.
  425. for i = 1, #list do
  426. local stack = list[i]
  427. if stack:get_count() > 0 and inv:room_for_item("main", stack) then
  428. inv:add_item("main", stack)
  429. minetest.log("action", "Put " .. stack:to_string() .. " in bones @ " .. location .. ".")
  430. num_stacks = num_stacks + 1
  431. else
  432. drop(pos, stack)
  433. end
  434. end
  435. player_inv:set_list(bag, {})
  436. end
  437. end
  438. -- Not setting formspec string.
  439. -- We use on_rightclick instead.
  440. meta:set_string("owner", pname)
  441. meta:set_int("numstacks", num_stacks)
  442. -- Notify the mapping code it needs to recalculate the mapkit cache.
  443. -- Do it on the next server step.
  444. minetest.after(0, function()
  445. map.clear_inventory_info(pname)
  446. end)
  447. meta:set_string("infotext",
  448. "Unfortunate <" .. rename.gpn(pname) ..
  449. ">'s Undecayed Bones\nMineral XP: " .. string.format("%.2f", xp_for_bones) .. "\n" ..
  450. "Died On " .. meta:get_string("diedate"))
  451. meta:set_int("time", 0)
  452. minetest.get_node_timer(pos):start(10)
  453. hud_clock.update_xp(pname)
  454. armor.end_duel(player, "death")
  455. local print_reason = bones.do_messages(pos, pname, num_stacks)
  456. if print_reason then
  457. bones.death_reason(pname, reason)
  458. end
  459. minetest.log("action", "Successfully spawned bones @ " .. minetest.pos_to_string(pos) .. "!")
  460. end
  461. bones.can_dig = function(pos, player)
  462. local inv = minetest.get_meta(pos):get_inventory()
  463. local name = ""
  464. if player then
  465. name = player:get_player_name()
  466. end
  467. return is_owner(pos, name) and inv:is_empty("main")
  468. end
  469. bones.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  470. if is_owner(pos, player:get_player_name()) then
  471. return count
  472. end
  473. return 0
  474. end
  475. bones.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  476. minetest.chat_send_player(player:get_player_name(), "# Server: Bones are not a place to store items!")
  477. return 0
  478. end
  479. bones.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  480. -- Prevent picking bones right after respawn, before player is repositioned.
  481. if bones.nohack.on_hackdetect(player) then
  482. local pname = player:get_player_name()
  483. minetest.chat_send_player(pname, "# Server: Wait a bit before taking from bones.")
  484. return 0
  485. end
  486. if is_owner(pos, player:get_player_name()) then
  487. return stack:get_count()
  488. end
  489. return 0
  490. end
  491. bones.on_metadata_inventory_take = function(pos, listname, index, stack, player)
  492. local pname = player:get_player_name()
  493. local meta = minetest.get_meta(pos)
  494. if meta:get_inventory():is_empty("main") then
  495. bones.reward_xp(meta, pname)
  496. bones.do_grab_bones_message(pname, pos, meta)
  497. minetest.after(0, function() minetest.close_formspec(pname, "bones:main") end)
  498. minetest.log("action", "Player " .. pname .. " took all from bones @ " .. minetest.pos_to_string(pos) .. ". Removing bones.")
  499. minetest.remove_node(pos)
  500. end
  501. end
  502. bones.reward_xp = function(meta, pname)
  503. -- Give XP stored in bones to player.
  504. -- But only if player removed the bones!
  505. local digxp = meta:get_float("digxp")
  506. local current_xp = xp.get_xp(pname, "digxp")
  507. current_xp = current_xp + digxp
  508. if current_xp > xp.digxp_max then
  509. current_xp = xp.digxp_max
  510. end
  511. xp.set_xp(pname, "digxp", current_xp)
  512. hud_clock.update_xp(pname)
  513. meta:set_float("digxp", 0)
  514. end
  515. bones.on_punch = function(pos, node, player)
  516. -- Fix stopped timers.
  517. minetest.get_node_timer(pos):start(10)
  518. local pname = player:get_player_name()
  519. if not is_owner(pos, pname) then
  520. return
  521. end
  522. if sheriff.is_cheater(pname) then
  523. if sheriff.punish_probability(pname) then
  524. sheriff.punish_player(pname)
  525. return
  526. end
  527. end
  528. -- Prevent picking bones right after respawn, before player is repositioned.
  529. if bones.nohack.on_hackdetect(player) then
  530. minetest.chat_send_player(pname, "# Server: Wait a bit before picking bones.")
  531. return
  532. end
  533. -- Dead players cannot pick bones.
  534. if player:get_hp() <= 0 then
  535. return
  536. end
  537. local meta = minetest.get_meta(pos)
  538. -- Bones that are neither fresh nor old aren't 'punchable'.
  539. if meta:get_string("infotext") == "" then
  540. return
  541. end
  542. local inv = meta:get_inventory()
  543. local player_inv = player:get_inventory()
  544. local has_space = true
  545. local added_map = false
  546. for i = 1, inv:get_size("main") do
  547. local stk = inv:get_stack("main", i)
  548. if player_inv:room_for_item("main", stk) then
  549. inv:set_stack("main", i, nil)
  550. player_inv:add_item("main", stk)
  551. -- Notify if a mapping kit was added.
  552. if map.is_mapping_kit(stk:get_name()) then
  553. added_map = true
  554. end
  555. else
  556. has_space = false
  557. break
  558. end
  559. end
  560. -- Notify if a mapping kit was added.
  561. if added_map then
  562. map.update_inventory_info(pname)
  563. end
  564. -- remove bones if player emptied them
  565. if has_space then
  566. -- Give player the bones.
  567. if player_inv:room_for_item("main", {name = "bones:bones_type2"}) then
  568. player_inv:add_item("main", {name = "bones:bones_type2"})
  569. else
  570. minetest.add_item(pos,"bones:bones_type2")
  571. end
  572. bones.reward_xp(meta, pname)
  573. bones.do_grab_bones_message(pname, pos, meta)
  574. -- Remove the bones from the world.
  575. minetest.log("action", "Bones @ " .. minetest.pos_to_string(pos) .. " punched by " .. pname .. ". Removing bones.")
  576. minetest.remove_node(pos)
  577. end
  578. end
  579. bones.do_grab_bones_message = function(pname, pos, meta)
  580. -- Send chat messages about bones retrieval.
  581. -- Don't spam the chatstream.
  582. if not bones.players[pname] then
  583. local public = true
  584. if player_labels.query_nametag_onoff(pname) == false then
  585. public = false
  586. end
  587. if cloaking.is_cloaked(pname) then
  588. public = false
  589. end
  590. local numstacks = meta:get_int("numstacks")
  591. local stacks = "stacks"
  592. if numstacks == 1 then
  593. stacks = "stack"
  594. elseif numstacks == 0 then
  595. numstacks = "an unknown number of"
  596. end
  597. local boneowner = meta:get_string("owner")
  598. if boneowner == "" then
  599. boneowner = meta:get_string("oldowner")
  600. end
  601. local ownerstring = "<" .. rename.gpn(boneowner) .. ">'s"
  602. if pname == boneowner then
  603. local sex = skins.get_gender_strings(pname)
  604. ownerstring = sex.his .. " own"
  605. end
  606. if boneowner == "" then
  607. ownerstring = "someone's"
  608. end
  609. local agestring = "fresh"
  610. if meta:get_string("owner") == "" then
  611. agestring = "old"
  612. end
  613. if public then
  614. minetest.chat_send_all(
  615. "# Server: Player <" ..
  616. rename.gpn(pname) ..
  617. "> claimed " .. ownerstring .. " " .. agestring .. " bones at " ..
  618. rc.pos_to_namestr(vector_round(pos)) ..
  619. " with " .. numstacks .. " " .. stacks .. ".")
  620. else
  621. minetest.chat_send_all(
  622. "# Server: Someone claimed " .. agestring .. " bones with " .. numstacks ..
  623. " " .. stacks .. ".")
  624. end
  625. bones.players[pname] = true
  626. minetest.after(60, bones.release_player, pname)
  627. end
  628. end
  629. bones.on_timer = function(pos, elapsed)
  630. local meta = minetest.get_meta(pos)
  631. local time = meta:get_int("time") + elapsed
  632. local share_time = share_bones_time
  633. local owner = meta:get_string("owner")
  634. -- Function may have been called twice or more. This prevents an issue.
  635. if owner == "" then
  636. return
  637. end
  638. -- If bones are in a protected area, they can be shared earlier than normal.
  639. if minetest.test_protection(pos, "") then
  640. share_time = share_bones_time_early
  641. end
  642. -- But if bones are in city, preserve them a lot longer.
  643. if city_block:in_city(pos) then
  644. share_time = share_bones_time_city
  645. end
  646. if time >= share_time then
  647. -- Bones will NOT decay as long as cheaters are present on the server. This
  648. -- prevents cheaters from being able to steal other player's stuff. If the
  649. -- player that died is themself a cheater, they don't get this protection.
  650. if not sheriff.is_cheater(owner) then
  651. local cheaters_are_present = false
  652. local all_players = minetest.get_connected_players()
  653. for k, v in ipairs(all_players) do
  654. if sheriff.is_cheater(v:get_player_name()) then
  655. cheaters_are_present = true
  656. break
  657. end
  658. end
  659. if cheaters_are_present then
  660. -- Not updating "time" here, so decay time is effectively paused.
  661. return true
  662. end
  663. end
  664. minetest.log("action", "Fresh bones @ " .. minetest.pos_to_string(pos) .. " decay into old bones.")
  665. local diedate = meta:get_string("diedate")
  666. if diedate == "" then
  667. diedate = "An Unknown Date"
  668. end
  669. local digxp = string.format("%.2f", meta:get_float("digxp"))
  670. meta:set_string("infotext",
  671. "Unfortunate <" .. rename.gpn(owner) ..
  672. ">'s Old Bones\nMineral XP: " .. digxp .. "\n" ..
  673. "Died On " .. diedate)
  674. meta:set_string("oldowner", owner)
  675. meta:set_string("owner", "")
  676. return
  677. end
  678. meta:set_int("time", time)
  679. return true
  680. end
  681. bones.on_blast = function(pos)
  682. minetest.log("action", "Bones @ " .. minetest.pos_to_string(pos) .. " experienced explosion blast.")
  683. end
  684. bones.on_destruct = function(pos)
  685. minetest.log("action", "Bones @ " .. minetest.pos_to_string(pos) .. " destructor called. Bones removed.")
  686. local meta = minetest.get_meta(pos)
  687. local inv = meta:get_inventory()
  688. if inv:is_empty("main") then
  689. return
  690. end
  691. -- If the inventory is not empty, we must respawn the bones.
  692. -- This is a workaround for a bug that just won't go away.
  693. local node = minetest.get_node(pos)
  694. local list = inv:get_list("main")
  695. local infotext = meta:get_string("infotext")
  696. local owner = meta:get_string("owner")
  697. local oldowner = meta:get_string("oldowner")
  698. local numstacks = meta:get_int("numstacks")
  699. local time = meta:get_int("time")
  700. local digxp = meta:get_float("digxp")
  701. local diedate = meta:get_string("diedate")
  702. minetest.after(0, function()
  703. minetest.log("action", "Bones @ " .. minetest.pos_to_string(pos) .. " were not empty! Attempting to restore bones.")
  704. minetest.set_node(pos, {name="bones:bones", param2=node.param2})
  705. local meta2 = minetest.get_meta(pos)
  706. local inv2 = meta2:get_inventory()
  707. inv2:set_size("main", 200)
  708. inv2:set_list("main", list)
  709. meta2:set_string("infotext", infotext)
  710. meta2:set_string("owner", owner)
  711. meta2:set_string("oldowner", oldowner)
  712. meta2:set_int("numstacks", numstacks)
  713. meta2:set_int("time", time)
  714. meta2:set_float("digxp", digxp)
  715. meta2:set_string("diedate", diedate)
  716. minetest.get_node_timer(pos):start(10)
  717. end)
  718. end
  719. -- Show bones inventory.
  720. function bones.on_rightclick(pos, node, clicker, itemstack, pt)
  721. local meta = minetest.get_meta(pos)
  722. if meta:get_string("infotext") == "" then
  723. return
  724. end
  725. -- Put all inventory contents toward the front of the list.
  726. local inv = meta:get_inventory()
  727. local list = inv:get_list("main") or {}
  728. local tmp = {}
  729. for index, stack in ipairs(list) do
  730. if not stack:is_empty() then
  731. tmp[#tmp+1] = stack
  732. end
  733. end
  734. inv:set_list("main", {})
  735. for index, stack in ipairs(tmp) do
  736. if inv:room_for_item("main", stack) then
  737. inv:add_item("main", stack)
  738. else
  739. minetest.add_item(pos, stack)
  740. end
  741. end
  742. local pname = clicker:get_player_name()
  743. minetest.log("action", "Player " .. pname .. " opens bones @ " .. minetest.pos_to_string(pos) .. ".")
  744. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  745. local formspec =
  746. "size[8,9]" ..
  747. default.gui_bg ..
  748. default.gui_bg_img ..
  749. default.gui_slots ..
  750. "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
  751. "list[current_player;main;0,4.85;8,1;]" ..
  752. "list[current_player;main;0,6.08;8,3;8]" ..
  753. "listring[nodemeta:" .. spos .. ";main]" ..
  754. "listring[current_player;main]" ..
  755. default.get_hotbar_bg(0,4.85)
  756. minetest.show_formspec(pname, "bones:main", formspec)
  757. end
  758. function bones.kill_bully_on_leaveplayer(pref, timeout)
  759. end