functions.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. --Mostly stolen code from Xdecor.
  2. local function top_face(pointed_thing)
  3. if not pointed_thing then return end
  4. return pointed_thing.above.y > pointed_thing.under.y
  5. end
  6. function furniture.sit(pos, node, clicker, pointed_thing, rotate)
  7. if not top_face(pointed_thing) then return end
  8. local player_name = clicker:get_player_name()
  9. local objs = minetest.get_objects_inside_radius(pos, 0.1)
  10. local vel = clicker:get_player_velocity()
  11. local ctrl = clicker:get_player_control()
  12. local fdir = node.param2 % 32
  13. for _, obj in pairs(objs) do
  14. if obj:is_player() and obj:get_player_name() ~= player_name then
  15. return
  16. end
  17. end
  18. if default.player_attached[player_name] then
  19. pos.y = pos.y - 0.5
  20. clicker:set_pos(pos)
  21. clicker:set_eye_offset(vector.new(), vector.new())
  22. clicker:set_physics_override(furniture.players[player_name])
  23. default.player_attached[player_name] = false
  24. default.player_set_animation(clicker, "stand", 30)
  25. elseif not default.player_attached[player_name] and fdir <= 3 and
  26. not ctrl.sneak and vector.equals(vel, vector.new()) then
  27. furniture.players[player_name] = clicker:get_physics_override()
  28. clicker:set_eye_offset({x = 0, y = -7, z = 2}, vector.new())
  29. clicker:set_physics_override({speed = 0, jump = 0, gravity = 1})
  30. clicker:set_pos(pos)
  31. default.player_attached[player_name] = true
  32. default.player_set_animation(clicker, "sit", 30)
  33. if rotate then
  34. if fdir == 0 then
  35. clicker:set_look_yaw(3.15)
  36. elseif fdir == 1 then
  37. clicker:set_look_yaw(7.9)
  38. elseif fdir == 2 then
  39. clicker:set_look_yaw(6.28)
  40. elseif fdir == 3 then
  41. clicker:set_look_yaw(4.75)
  42. end
  43. end
  44. end
  45. end
  46. function furniture.sort_inventory(inv) -- Copied from the Technic_chests mod.
  47. local inlist = inv:get_list("main")
  48. local typecnt = {}
  49. local typekeys = {}
  50. for _, st in ipairs(inlist) do
  51. if not st:is_empty() then
  52. local n = st:get_name()
  53. local w = st:get_wear()
  54. local m = st:get_metadata()
  55. local k = string.format("%s %05d %s", n, w, m)
  56. if not typecnt[k] then
  57. typecnt[k] = {
  58. name = n,
  59. wear = w,
  60. metadata = m,
  61. stack_max = st:get_stack_max(),
  62. count = 0,
  63. }
  64. table.insert(typekeys, k)
  65. end
  66. typecnt[k].count = typecnt[k].count + st:get_count()
  67. end
  68. end
  69. table.sort(typekeys)
  70. local outlist = {}
  71. for _, k in ipairs(typekeys) do
  72. local tc = typecnt[k]
  73. while tc.count > 0 do
  74. local c = math.min(tc.count, tc.stack_max)
  75. table.insert(outlist, ItemStack({
  76. name = tc.name,
  77. wear = tc.wear,
  78. metadata = tc.metadata,
  79. count = c,
  80. }))
  81. tc.count = tc.count - c
  82. end
  83. end
  84. if #outlist > #inlist then return end
  85. while #outlist < #inlist do
  86. table.insert(outlist, ItemStack(nil))
  87. end
  88. inv:set_list("main", outlist)
  89. end
  90. function furniture.inv_take_put(pos, listname, index, stack, player)
  91. local player_name = player:get_player_name()
  92. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  93. return 0
  94. else
  95. return 99
  96. end
  97. end
  98. function furniture.inv_manipulate(pos, from_list, from_index, to_list, to_index, count, player)
  99. local player_name = player:get_player_name()
  100. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  101. return 0
  102. else
  103. return 99
  104. end
  105. end
  106. function furniture.inv_take(pos, listname, index, stack, player)
  107. minetest.log("action", player:get_player_name() .. " takes " .. stack:get_name() ..
  108. " from storage at " .. minetest.pos_to_string(pos))
  109. end
  110. function furniture.inv_put(pos, listname, index, stack, player)
  111. minetest.log("action", player:get_player_name() .. " moves " .. stack:get_name() ..
  112. " to storage at " .. minetest.pos_to_string(pos))
  113. end