bridge.lua 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. local modpath = minetest.get_modpath(minetest.get_current_modname())
  2. local S, NS = dofile(modpath.."/intllib.lua")
  3. if ropes.bridges_enabled then
  4. local bridge_on_place = function(itemstack, placer, pointed_thing)
  5. -- Shall place item and return the leftover itemstack.
  6. -- The placer may be any ObjectRef or nil.
  7. -- default: minetest.item_place
  8. if placer == nil then
  9. return minetest.item_place(itemstack, placer, pointed_thing)
  10. end
  11. local above = pointed_thing.above
  12. local under = pointed_thing.under
  13. if above.x == under.x and above.z == under.z and above.y > under.y then
  14. -- we're aimed downward at a buildable node from above.
  15. -- determine the direction the placer lies relative to this node.
  16. local new_under = vector.new(under)
  17. local placer_pos = placer:get_pos()
  18. local diff_x = placer_pos.x - under.x
  19. local diff_z = placer_pos.z - under.z
  20. if math.abs(diff_x) > math.abs(diff_z) then
  21. -- placer is displaced along the X axis relative to the target
  22. if diff_x > 0 then
  23. new_under.x = under.x - 1
  24. else
  25. new_under.x = under.x + 1
  26. end
  27. else
  28. -- placer is displaced along the Z axis relative to the target
  29. if diff_z > 0 then
  30. new_under.z = under.z - 1
  31. else
  32. new_under.z = under.z + 1
  33. end
  34. end
  35. if minetest.registered_nodes[minetest.get_node(new_under).name].buildable_to then
  36. local new_pointed_thing = {type="node", under=new_under, above={x=new_under.x, y=new_under.y+1, z=new_under.z}}
  37. return minetest.item_place(itemstack, placer, new_pointed_thing)
  38. end
  39. end
  40. return minetest.item_place(itemstack, placer, pointed_thing)
  41. end
  42. minetest.register_node("ropes:wood_bridge", {
  43. description = S("Wooden Bridge"),
  44. _doc_items_longdesc = ropes.doc.wooden_bridge_longdesc,
  45. _doc_items_usagehelp = ropes.doc.wooden_bridge_usagehelp,
  46. tiles = {
  47. "default_wood.png", "default_wood.png",
  48. "default_wood.png^[transformR270", "default_wood.png^[transformR90",
  49. "default_wood.png^[transformR270", "default_wood.png^[transformR90",
  50. },
  51. drawtype = "nodebox",
  52. paramtype = "light",
  53. paramtype2 = "facedir",
  54. groups = {choppy = 2, flammable = 2, oddly_breakable_by_hand = 1, flow_through = 1, fence = 1, wall = 1},
  55. sounds = default.node_sound_wood_defaults(),
  56. node_box = {
  57. type = "fixed",
  58. fixed = {
  59. {-0.5, 0.375, -0.5, 0.5, 0.5, 0.5}, -- Platform
  60. {-0.375, -0.5, -0.5, 0.375, -0.375, -0.4375}, -- x beam4
  61. {-0.375, -0.5, 0.4375, 0.375, -0.375, 0.5}, -- x beam3
  62. {0.375, -0.5, -0.4375, 0.5, -0.375, 0.4375}, -- z beam2
  63. {-0.5, -0.5, -0.4375, -0.375, -0.375, 0.4375}, -- z beam1
  64. {0.375, -0.5, -0.5, 0.5, 0.375, -0.4375}, -- upright4
  65. {0.375, -0.5, 0.4375, 0.5, 0.375, 0.5}, -- upright3
  66. {-0.5, -0.5, -0.5, -0.375, 0.375, -0.4375}, -- upright2
  67. {-0.5, -0.5, 0.4375, -0.375, 0.375, 0.5}, -- upright1
  68. }
  69. },
  70. on_place = bridge_on_place,
  71. })
  72. minetest.register_craft({
  73. output = "ropes:wood_bridge 5",
  74. recipe = {
  75. {"group:stick", "stairs:slab_wood", "group:stick"},
  76. {"group:stick", "", "group:stick"},
  77. {"group:stick", "group:stick", "group:stick"},
  78. }
  79. })
  80. end