pick.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. local modname = minetest.get_current_modname()
  2. local S = minetest.get_translator(modname)
  3. local get_prospect = _G[modname].get_prospect
  4. reading_message_chart =
  5. {
  6. {-3, "Found traces of traces of @1."},
  7. {-2, "Found traces of @1."},
  8. {-0.3, "Found large traces of @1."},
  9. {0, "Found @1."},
  10. }
  11. local function get_message_from_reading(strength)
  12. local ret
  13. for k, v in ipairs(reading_message_chart)
  14. do
  15. print(k, type(k))
  16. if strength >= v[1]
  17. then
  18. ret = v[2]
  19. end
  20. end
  21. return ret
  22. end
  23. minetest.register_tool(modname .. ":pick",
  24. {
  25. description = S("Prospector pick"),
  26. inventory_image = modname .. "_pick.png",
  27. on_use = function(itemstack, user, pointed_thing)
  28. if pointed_thing.type == "node"
  29. then
  30. local prospect = get_prospect(pointed_thing.under)
  31. local highest_name
  32. local highest_val = -math.huge
  33. for k, v in pairs(prospect)
  34. do
  35. if v > highest_val
  36. then
  37. highest_name = k
  38. highest_val = v
  39. end
  40. end
  41. if highest_name
  42. then
  43. local msg =
  44. highest_val < -3 and S("Found nothing.") or
  45. S(get_message_from_reading(highest_val), highest_name)
  46. minetest.chat_send_player(user:get_player_name(), msg)
  47. else
  48. minetest.chat_send_player(user:get_player_name(), S("Found nothing.") )
  49. end
  50. end
  51. end,
  52. })