init.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. doors = {}
  2. -- Registers a door - REDEFINITION ONLY | DOORS MOD MUST HAVE BEEN LOADED BEFORE
  3. -- name: The name of the door
  4. -- def: a table with the folowing fields:
  5. -- description
  6. -- inventory_image
  7. -- groups
  8. -- tiles_bottom: the tiles of the bottom part of the door {front, side}
  9. -- tiles_top: the tiles of the bottom part of the door {front, side}
  10. -- If the following fields are not defined the default values are used
  11. -- node_box_bottom
  12. -- node_box_top
  13. -- selection_box_bottom
  14. -- selection_box_top
  15. -- only_placer_can_open: if true only the player who placed the door can
  16. -- open it
  17. function doors:register_door(name, def)
  18. def.groups.not_in_creative_inventory = 1
  19. local box = {{-0.5, -0.5, -0.5, 0.5, 0.5, -0.5+1.5/16}}
  20. if not def.node_box_bottom then
  21. def.node_box_bottom = box
  22. end
  23. if not def.node_box_top then
  24. def.node_box_top = box
  25. end
  26. if not def.selection_box_bottom then
  27. def.selection_box_bottom= box
  28. end
  29. if not def.selection_box_top then
  30. def.selection_box_top = box
  31. end
  32. local tt = def.tiles_top
  33. local tb = def.tiles_bottom
  34. local function after_dig_node(pos, name)
  35. if minetest.env:get_node(pos).name == name then
  36. minetest.env:remove_node(pos)
  37. end
  38. end
  39. local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
  40. pos.y = pos.y+dir
  41. if not minetest.env:get_node(pos).name == check_name then
  42. return
  43. end
  44. local p2 = minetest.env:get_node(pos).param2
  45. p2 = params[p2+1]
  46. local meta = minetest.env:get_meta(pos):to_table()
  47. minetest.env:set_node(pos, {name=replace_dir, param2=p2})
  48. minetest.env:get_meta(pos):from_table(meta)
  49. pos.y = pos.y-dir
  50. meta = minetest.env:get_meta(pos):to_table()
  51. minetest.env:set_node(pos, {name=replace, param2=p2})
  52. minetest.env:get_meta(pos):from_table(meta)
  53. end
  54. local function on_mesecons_signal_open (pos, node)
  55. on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
  56. end
  57. local function on_mesecons_signal_close (pos, node)
  58. on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
  59. end
  60. local function check_player_priv(pos, player)
  61. if not def.only_placer_can_open then
  62. return true
  63. end
  64. local meta = minetest.env:get_meta(pos)
  65. local pn = player:get_player_name()
  66. return meta:get_string("doors_owner") == pn
  67. end
  68. minetest.register_node(":"..name.."_b_1", {
  69. tiles = {tb[2], tb[2], tb[2], tb[2], tb[1], tb[1].."^[transformfx"},
  70. paramtype = "light",
  71. paramtype2 = "facedir",
  72. drop = name,
  73. drawtype = "nodebox",
  74. node_box = {
  75. type = "fixed",
  76. fixed = def.node_box_bottom
  77. },
  78. selection_box = {
  79. type = "fixed",
  80. fixed = def.selection_box_bottom
  81. },
  82. groups = def.groups,
  83. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  84. pos.y = pos.y+1
  85. after_dig_node(pos, name.."_t_1")
  86. end,
  87. on_rightclick = function(pos, node, puncher)
  88. if check_player_priv(pos, puncher) then
  89. on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
  90. end
  91. end,
  92. mesecons = {effector = {
  93. action_on = on_mesecons_signal_open
  94. }},
  95. can_dig = check_player_priv,
  96. })
  97. minetest.register_node(":"..name.."_b_2", {
  98. tiles = {tb[2], tb[2], tb[2], tb[2], tb[1].."^[transformfx", tb[1]},
  99. paramtype = "light",
  100. paramtype2 = "facedir",
  101. drop = name,
  102. drawtype = "nodebox",
  103. node_box = {
  104. type = "fixed",
  105. fixed = def.node_box_bottom
  106. },
  107. selection_box = {
  108. type = "fixed",
  109. fixed = def.selection_box_bottom
  110. },
  111. groups = def.groups,
  112. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  113. pos.y = pos.y+1
  114. after_dig_node(pos, name.."_t_2")
  115. end,
  116. on_rightclick = function(pos, node, puncher)
  117. if check_player_priv(pos, puncher) then
  118. on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
  119. end
  120. end,
  121. mesecons = {effector = {
  122. action_off = on_mesecons_signal_close
  123. }},
  124. can_dig = check_player_priv,
  125. })
  126. end
  127. doors:register_door("doors:door_wood", {
  128. description = "Wooden Door",
  129. inventory_image = "door_wood.png",
  130. groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=2,door=1},
  131. tiles_bottom = {"door_wood_b.png", "door_brown.png"},
  132. tiles_top = {"door_wood_a.png", "door_brown.png"},
  133. })
  134. doors:register_door("doors:door_steel", {
  135. description = "Steel Door",
  136. inventory_image = "door_steel.png",
  137. groups = {snappy=1,bendy=2,cracky=1,melty=2,level=2,door=1},
  138. tiles_bottom = {"door_steel_b.png", "door_grey.png"},
  139. tiles_top = {"door_steel_a.png", "door_grey.png"},
  140. only_placer_can_open = true,
  141. })