2
0

init.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. -- doors/init.lua
  2. -- our API object
  3. doors = {}
  4. doors.registered_doors = {}
  5. doors.registered_trapdoors = {}
  6. -- Load support for MT game translation.
  7. local S = minetest.get_translator("doors")
  8. local function replace_old_owner_information(pos)
  9. local meta = minetest.get_meta(pos)
  10. local owner = meta:get_string("doors_owner")
  11. if owner and owner ~= "" then
  12. meta:set_string("owner", owner)
  13. meta:set_string("doors_owner", "")
  14. end
  15. end
  16. local function is_doors_upper_node(pos)
  17. return minetest.get_node(pos).name == "doors:hidden"
  18. end
  19. -- returns an object to a door object or nil
  20. function doors.get(pos)
  21. local node_name = minetest.get_node(pos).name
  22. if doors.registered_doors[node_name] then
  23. -- A normal upright door
  24. return {
  25. pos = pos,
  26. open = function(self, player)
  27. if self:state() then
  28. return false
  29. end
  30. return doors.door_toggle(self.pos, nil, player)
  31. end,
  32. close = function(self, player)
  33. if not self:state() then
  34. return false
  35. end
  36. return doors.door_toggle(self.pos, nil, player)
  37. end,
  38. toggle = function(self, player)
  39. return doors.door_toggle(self.pos, nil, player)
  40. end,
  41. state = function(self)
  42. local state = minetest.get_meta(self.pos):get_int("state")
  43. return state %2 == 1
  44. end
  45. }
  46. elseif doors.registered_trapdoors[node_name] then
  47. -- A trapdoor
  48. return {
  49. pos = pos,
  50. open = function(self, player)
  51. if self:state() then
  52. return false
  53. end
  54. return doors.trapdoor_toggle(self.pos, nil, player)
  55. end,
  56. close = function(self, player)
  57. if not self:state() then
  58. return false
  59. end
  60. return doors.trapdoor_toggle(self.pos, nil, player)
  61. end,
  62. toggle = function(self, player)
  63. return doors.trapdoor_toggle(self.pos, nil, player)
  64. end,
  65. state = function(self)
  66. return minetest.get_node(self.pos).name:sub(-5) == "_open"
  67. end
  68. }
  69. else
  70. return nil
  71. end
  72. end
  73. -- this hidden node is placed on top of the bottom, and prevents
  74. -- nodes from being placed in the top half of the door.
  75. minetest.register_node("doors:hidden", {
  76. description = S("Hidden Door Segment"),
  77. inventory_image = "doors_hidden_segment.png^default_invisible_node_overlay.png",
  78. wield_image = "doors_hidden_segment.png^default_invisible_node_overlay.png",
  79. drawtype = "airlike",
  80. paramtype = "light",
  81. paramtype2 = "facedir",
  82. sunlight_propagates = true,
  83. -- has to be walkable for falling nodes to stop falling.
  84. walkable = true,
  85. pointable = false,
  86. diggable = false,
  87. buildable_to = false,
  88. floodable = false,
  89. drop = "",
  90. groups = {not_in_creative_inventory = 1},
  91. on_blast = function() end,
  92. -- 1px block inside door hinge near node top
  93. collision_box = {
  94. type = "fixed",
  95. fixed = {-15/32, 13/32, -15/32, -13/32, 1/2, -13/32},
  96. },
  97. })
  98. -- table used to aid door opening/closing
  99. local transform = {
  100. {
  101. {v = "_a", param2 = 3},
  102. {v = "_a", param2 = 0},
  103. {v = "_a", param2 = 1},
  104. {v = "_a", param2 = 2},
  105. },
  106. {
  107. {v = "_c", param2 = 1},
  108. {v = "_c", param2 = 2},
  109. {v = "_c", param2 = 3},
  110. {v = "_c", param2 = 0},
  111. },
  112. {
  113. {v = "_b", param2 = 1},
  114. {v = "_b", param2 = 2},
  115. {v = "_b", param2 = 3},
  116. {v = "_b", param2 = 0},
  117. },
  118. {
  119. {v = "_d", param2 = 3},
  120. {v = "_d", param2 = 0},
  121. {v = "_d", param2 = 1},
  122. {v = "_d", param2 = 2},
  123. },
  124. }
  125. function doors.door_toggle(pos, node, clicker)
  126. local meta = minetest.get_meta(pos)
  127. node = node or minetest.get_node(pos)
  128. local def = minetest.registered_nodes[node.name]
  129. local name = def.door.name
  130. local state = meta:get_string("state")
  131. if state == "" then
  132. -- fix up lvm-placed right-hinged doors, default closed
  133. if node.name:sub(-2) == "_b" then
  134. state = 2
  135. else
  136. state = 0
  137. end
  138. else
  139. state = tonumber(state)
  140. end
  141. replace_old_owner_information(pos)
  142. if clicker and not default.can_interact_with_node(clicker, pos) then
  143. return false
  144. end
  145. -- until Lua-5.2 we have no bitwise operators :(
  146. if state % 2 == 1 then
  147. state = state - 1
  148. else
  149. state = state + 1
  150. end
  151. local dir = node.param2
  152. -- It's possible param2 is messed up, so, validate before using
  153. -- the input data. This indicates something may have rotated
  154. -- the door, even though that is not supported.
  155. if not transform[state + 1] or not transform[state + 1][dir + 1] then
  156. return false
  157. end
  158. if state % 2 == 0 then
  159. minetest.sound_play(def.door.sounds[1],
  160. {pos = pos, gain = def.door.gains[1], max_hear_distance = 10}, true)
  161. else
  162. minetest.sound_play(def.door.sounds[2],
  163. {pos = pos, gain = def.door.gains[2], max_hear_distance = 10}, true)
  164. end
  165. minetest.swap_node(pos, {
  166. name = name .. transform[state + 1][dir+1].v,
  167. param2 = transform[state + 1][dir+1].param2
  168. })
  169. meta:set_int("state", state)
  170. return true
  171. end
  172. local function on_place_node(place_to, newnode,
  173. placer, oldnode, itemstack, pointed_thing)
  174. -- Run script hook
  175. for _, callback in ipairs(minetest.registered_on_placenodes) do
  176. -- Deepcopy pos, node and pointed_thing because callback can modify them
  177. local place_to_copy = {x = place_to.x, y = place_to.y, z = place_to.z}
  178. local newnode_copy =
  179. {name = newnode.name, param1 = newnode.param1, param2 = newnode.param2}
  180. local oldnode_copy =
  181. {name = oldnode.name, param1 = oldnode.param1, param2 = oldnode.param2}
  182. local pointed_thing_copy = {
  183. type = pointed_thing.type,
  184. above = vector.new(pointed_thing.above),
  185. under = vector.new(pointed_thing.under),
  186. ref = pointed_thing.ref,
  187. }
  188. callback(place_to_copy, newnode_copy, placer,
  189. oldnode_copy, itemstack, pointed_thing_copy)
  190. end
  191. end
  192. local function can_dig_door(pos, digger)
  193. replace_old_owner_information(pos)
  194. return default.can_interact_with_node(digger, pos)
  195. end
  196. function doors.register(name, def)
  197. if not name:find(":") then
  198. name = "doors:" .. name
  199. end
  200. -- replace old doors of this type automatically
  201. minetest.register_lbm({
  202. name = ":doors:replace_" .. name:gsub(":", "_"),
  203. nodenames = {name.."_b_1", name.."_b_2"},
  204. action = function(pos, node)
  205. local l = tonumber(node.name:sub(-1))
  206. local meta = minetest.get_meta(pos)
  207. local h = meta:get_int("right") + 1
  208. local p2 = node.param2
  209. local replace = {
  210. {{type = "a", state = 0}, {type = "a", state = 3}},
  211. {{type = "b", state = 1}, {type = "b", state = 2}}
  212. }
  213. local new = replace[l][h]
  214. -- retain infotext and doors_owner fields
  215. minetest.swap_node(pos, {name = name .. "_" .. new.type, param2 = p2})
  216. meta:set_int("state", new.state)
  217. -- properly place doors:hidden at the right spot
  218. local p3 = p2
  219. if new.state >= 2 then
  220. p3 = (p3 + 3) % 4
  221. end
  222. if new.state % 2 == 1 then
  223. if new.state >= 2 then
  224. p3 = (p3 + 1) % 4
  225. else
  226. p3 = (p3 + 3) % 4
  227. end
  228. end
  229. -- wipe meta on top node as it's unused
  230. minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z},
  231. {name = "doors:hidden", param2 = p3})
  232. end
  233. })
  234. minetest.register_craftitem(":" .. name, {
  235. description = def.description,
  236. inventory_image = def.inventory_image,
  237. groups = table.copy(def.groups),
  238. on_place = function(itemstack, placer, pointed_thing)
  239. local pos
  240. if pointed_thing.type ~= "node" then
  241. return itemstack
  242. end
  243. local doorname = itemstack:get_name()
  244. local node = minetest.get_node(pointed_thing.under)
  245. local pdef = minetest.registered_nodes[node.name]
  246. if pdef and pdef.on_rightclick and
  247. not (placer and placer:is_player() and
  248. placer:get_player_control().sneak) then
  249. return pdef.on_rightclick(pointed_thing.under,
  250. node, placer, itemstack, pointed_thing)
  251. end
  252. if pdef and pdef.buildable_to then
  253. pos = pointed_thing.under
  254. else
  255. pos = pointed_thing.above
  256. node = minetest.get_node(pos)
  257. pdef = minetest.registered_nodes[node.name]
  258. if not pdef or not pdef.buildable_to then
  259. return itemstack
  260. end
  261. end
  262. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  263. local top_node = minetest.get_node_or_nil(above)
  264. local topdef = top_node and minetest.registered_nodes[top_node.name]
  265. if not topdef or not topdef.buildable_to then
  266. return itemstack
  267. end
  268. local pn = placer and placer:get_player_name() or ""
  269. if minetest.is_protected(pos, pn) or minetest.is_protected(above, pn) then
  270. return itemstack
  271. end
  272. local dir = placer and minetest.dir_to_facedir(placer:get_look_dir()) or 0
  273. local ref = {
  274. {x = -1, y = 0, z = 0},
  275. {x = 0, y = 0, z = 1},
  276. {x = 1, y = 0, z = 0},
  277. {x = 0, y = 0, z = -1},
  278. }
  279. local aside = {
  280. x = pos.x + ref[dir + 1].x,
  281. y = pos.y + ref[dir + 1].y,
  282. z = pos.z + ref[dir + 1].z,
  283. }
  284. local state = 0
  285. if minetest.get_item_group(minetest.get_node(aside).name, "door") == 1 then
  286. state = state + 2
  287. minetest.set_node(pos, {name = doorname .. "_b", param2 = dir})
  288. minetest.set_node(above, {name = "doors:hidden", param2 = (dir + 3) % 4})
  289. else
  290. minetest.set_node(pos, {name = doorname .. "_a", param2 = dir})
  291. minetest.set_node(above, {name = "doors:hidden", param2 = dir})
  292. end
  293. local meta = minetest.get_meta(pos)
  294. meta:set_int("state", state)
  295. if def.protected then
  296. meta:set_string("owner", pn)
  297. meta:set_string("infotext", def.description .. "\n" .. S("Owned by @1", pn))
  298. end
  299. if not minetest.is_creative_enabled(pn) then
  300. itemstack:take_item()
  301. end
  302. minetest.sound_play(def.sounds.place, {pos = pos}, true)
  303. on_place_node(pos, minetest.get_node(pos),
  304. placer, node, itemstack, pointed_thing)
  305. return itemstack
  306. end
  307. })
  308. def.inventory_image = nil
  309. if def.recipe then
  310. minetest.register_craft({
  311. output = name,
  312. recipe = def.recipe,
  313. })
  314. end
  315. def.recipe = nil
  316. if not def.sounds then
  317. def.sounds = default.node_sound_wood_defaults()
  318. end
  319. if not def.sound_open then
  320. def.sound_open = "doors_door_open"
  321. end
  322. if not def.sound_close then
  323. def.sound_close = "doors_door_close"
  324. end
  325. if not def.gain_open then
  326. def.gain_open = 0.3
  327. end
  328. if not def.gain_close then
  329. def.gain_close = 0.3
  330. end
  331. def.groups.not_in_creative_inventory = 1
  332. def.groups.door = 1
  333. def.drop = name
  334. def.door = {
  335. name = name,
  336. sounds = {def.sound_close, def.sound_open},
  337. gains = {def.gain_close, def.gain_open},
  338. }
  339. if not def.on_rightclick then
  340. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  341. doors.door_toggle(pos, node, clicker)
  342. return itemstack
  343. end
  344. end
  345. def.after_dig_node = function(pos, node, meta, digger)
  346. local above = pos:offset(0, 1, 0)
  347. if is_doors_upper_node(above) then
  348. minetest.remove_node(above)
  349. end
  350. minetest.check_for_falling(above)
  351. end
  352. def.on_rotate = function(pos, node, user, mode, new_param2)
  353. return false
  354. end
  355. if def.protected then
  356. def.can_dig = can_dig_door
  357. def.on_blast = function() end
  358. def.on_key_use = function(pos, player)
  359. local door = doors.get(pos)
  360. door:toggle(player)
  361. end
  362. def.on_skeleton_key_use = function(pos, player, newsecret)
  363. replace_old_owner_information(pos)
  364. local meta = minetest.get_meta(pos)
  365. local owner = meta:get_string("owner")
  366. local pname = player:get_player_name()
  367. -- verify placer is owner of lockable door
  368. if owner ~= pname then
  369. minetest.record_protection_violation(pos, pname)
  370. minetest.chat_send_player(pname, S("You do not own this locked door."))
  371. return nil
  372. end
  373. local secret = meta:get_string("key_lock_secret")
  374. if secret == "" then
  375. secret = newsecret
  376. meta:set_string("key_lock_secret", secret)
  377. end
  378. return secret, S("a locked door"), owner
  379. end
  380. def.node_dig_prediction = ""
  381. else
  382. def.on_blast = function(pos, intensity)
  383. minetest.remove_node(pos)
  384. local above = pos:offset(0, 1, 0)
  385. -- hidden node doesn't get blasted away.
  386. if is_doors_upper_node(above) then
  387. minetest.remove_node(above)
  388. end
  389. return {name}
  390. end
  391. end
  392. def.on_destruct = function(pos)
  393. local above = pos:offset(0, 1, 0)
  394. if is_doors_upper_node(above) then
  395. minetest.remove_node(above)
  396. end
  397. end
  398. def.drawtype = "mesh"
  399. def.paramtype = "light"
  400. def.paramtype2 = "facedir"
  401. def.sunlight_propagates = true
  402. def.walkable = true
  403. def.is_ground_content = false
  404. def.buildable_to = false
  405. def.selection_box = {type = "fixed", fixed = {-1/2,-1/2,-1/2,1/2,3/2,-6/16}}
  406. def.collision_box = {type = "fixed", fixed = {-1/2,-1/2,-1/2,1/2,3/2,-6/16}}
  407. def.use_texture_alpha = def.use_texture_alpha or "clip"
  408. def.mesh = "door_a.b3d"
  409. minetest.register_node(":" .. name .. "_a", table.copy(def))
  410. def.mesh = "door_b.b3d"
  411. minetest.register_node(":" .. name .. "_b", table.copy(def))
  412. def.mesh = "door_b.b3d"
  413. minetest.register_node(":" .. name .. "_c", table.copy(def))
  414. def.mesh = "door_a.b3d"
  415. minetest.register_node(":" .. name .. "_d", table.copy(def))
  416. doors.registered_doors[name .. "_a"] = true
  417. doors.registered_doors[name .. "_b"] = true
  418. doors.registered_doors[name .. "_c"] = true
  419. doors.registered_doors[name .. "_d"] = true
  420. end
  421. doors.register("door_wood", {
  422. tiles = {{ name = "doors_door_wood.png", backface_culling = true }},
  423. description = S("Wooden Door"),
  424. inventory_image = "doors_item_wood.png",
  425. groups = {node = 1, choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
  426. gain_open = 0.06,
  427. gain_close = 0.13,
  428. recipe = {
  429. {"group:wood", "group:wood"},
  430. {"group:wood", "group:wood"},
  431. {"group:wood", "group:wood"},
  432. }
  433. })
  434. doors.register("door_steel", {
  435. tiles = {{name = "doors_door_steel.png", backface_culling = true}},
  436. description = S("Steel Door"),
  437. inventory_image = "doors_item_steel.png",
  438. protected = true,
  439. groups = {node = 1, cracky = 1, level = 2},
  440. sounds = default.node_sound_metal_defaults(),
  441. sound_open = "doors_steel_door_open",
  442. sound_close = "doors_steel_door_close",
  443. gain_open = 0.2,
  444. gain_close = 0.2,
  445. recipe = {
  446. {"default:steel_ingot", "default:steel_ingot"},
  447. {"default:steel_ingot", "default:steel_ingot"},
  448. {"default:steel_ingot", "default:steel_ingot"},
  449. }
  450. })
  451. doors.register("door_glass", {
  452. tiles = {"doors_door_glass.png"},
  453. description = S("Glass Door"),
  454. inventory_image = "doors_item_glass.png",
  455. groups = {node = 1, cracky=3, oddly_breakable_by_hand=3},
  456. sounds = default.node_sound_glass_defaults(),
  457. sound_open = "doors_glass_door_open",
  458. sound_close = "doors_glass_door_close",
  459. gain_open = 0.3,
  460. gain_close = 0.25,
  461. recipe = {
  462. {"default:glass", "default:glass"},
  463. {"default:glass", "default:glass"},
  464. {"default:glass", "default:glass"},
  465. }
  466. })
  467. doors.register("door_obsidian_glass", {
  468. tiles = {"doors_door_obsidian_glass.png"},
  469. description = S("Obsidian Glass Door"),
  470. inventory_image = "doors_item_obsidian_glass.png",
  471. groups = {node = 1, cracky=3},
  472. sounds = default.node_sound_glass_defaults(),
  473. sound_open = "doors_glass_door_open",
  474. sound_close = "doors_glass_door_close",
  475. gain_open = 0.3,
  476. gain_close = 0.25,
  477. recipe = {
  478. {"default:obsidian_glass", "default:obsidian_glass"},
  479. {"default:obsidian_glass", "default:obsidian_glass"},
  480. {"default:obsidian_glass", "default:obsidian_glass"},
  481. },
  482. })
  483. -- Capture mods using the old API as best as possible.
  484. function doors.register_door(name, def)
  485. if def.only_placer_can_open then
  486. def.protected = true
  487. end
  488. def.only_placer_can_open = nil
  489. local i = name:find(":")
  490. local modname = name:sub(1, i - 1)
  491. if not def.tiles then
  492. if def.protected then
  493. def.tiles = {{name = "doors_door_steel.png", backface_culling = true}}
  494. else
  495. def.tiles = {{name = "doors_door_wood.png", backface_culling = true}}
  496. end
  497. minetest.log("warning", modname .. " registered door \"" .. name .. "\" " ..
  498. "using deprecated API method \"doors.register_door()\" but " ..
  499. "did not provide the \"tiles\" parameter. A fallback tiledef " ..
  500. "will be used instead.")
  501. end
  502. doors.register(name, def)
  503. end
  504. ----trapdoor----
  505. function doors.trapdoor_toggle(pos, node, clicker)
  506. node = node or minetest.get_node(pos)
  507. replace_old_owner_information(pos)
  508. if clicker and not default.can_interact_with_node(clicker, pos) then
  509. return false
  510. end
  511. local def = minetest.registered_nodes[node.name]
  512. if string.sub(node.name, -5) == "_open" then
  513. minetest.sound_play(def.sound_close,
  514. {pos = pos, gain = def.gain_close, max_hear_distance = 10}, true)
  515. minetest.swap_node(pos, {name = string.sub(node.name, 1,
  516. string.len(node.name) - 5), param1 = node.param1, param2 = node.param2})
  517. else
  518. minetest.sound_play(def.sound_open,
  519. {pos = pos, gain = def.gain_open, max_hear_distance = 10}, true)
  520. minetest.swap_node(pos, {name = node.name .. "_open",
  521. param1 = node.param1, param2 = node.param2})
  522. end
  523. end
  524. function doors.register_trapdoor(name, def)
  525. if not name:find(":") then
  526. name = "doors:" .. name
  527. end
  528. local name_closed = name
  529. local name_opened = name.."_open"
  530. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  531. doors.trapdoor_toggle(pos, node, clicker)
  532. return itemstack
  533. end
  534. -- Common trapdoor configuration
  535. def.drawtype = "nodebox"
  536. def.paramtype = "light"
  537. def.paramtype2 = "facedir"
  538. def.is_ground_content = false
  539. def.use_texture_alpha = def.use_texture_alpha or "clip"
  540. if def.protected then
  541. def.can_dig = can_dig_door
  542. def.after_place_node = function(pos, placer, itemstack, pointed_thing)
  543. local pn = placer:get_player_name()
  544. local meta = minetest.get_meta(pos)
  545. meta:set_string("owner", pn)
  546. meta:set_string("infotext", def.description .. "\n" .. S("Owned by @1", pn))
  547. return minetest.is_creative_enabled(pn)
  548. end
  549. def.on_blast = function() end
  550. def.on_key_use = function(pos, player)
  551. local door = doors.get(pos)
  552. door:toggle(player)
  553. end
  554. def.on_skeleton_key_use = function(pos, player, newsecret)
  555. replace_old_owner_information(pos)
  556. local meta = minetest.get_meta(pos)
  557. local owner = meta:get_string("owner")
  558. local pname = player:get_player_name()
  559. -- verify placer is owner of lockable door
  560. if owner ~= pname then
  561. minetest.record_protection_violation(pos, pname)
  562. minetest.chat_send_player(pname, S("You do not own this trapdoor."))
  563. return nil
  564. end
  565. local secret = meta:get_string("key_lock_secret")
  566. if secret == "" then
  567. secret = newsecret
  568. meta:set_string("key_lock_secret", secret)
  569. end
  570. return secret, S("a locked trapdoor"), owner
  571. end
  572. def.node_dig_prediction = ""
  573. else
  574. def.on_blast = function(pos, intensity)
  575. minetest.remove_node(pos)
  576. return {name}
  577. end
  578. end
  579. if not def.sounds then
  580. def.sounds = default.node_sound_wood_defaults()
  581. end
  582. if not def.sound_open then
  583. def.sound_open = "doors_door_open"
  584. end
  585. if not def.sound_close then
  586. def.sound_close = "doors_door_close"
  587. end
  588. if not def.gain_open then
  589. def.gain_open = 0.3
  590. end
  591. if not def.gain_close then
  592. def.gain_close = 0.3
  593. end
  594. local def_opened = table.copy(def)
  595. local def_closed = table.copy(def)
  596. if def.nodebox_closed and def.nodebox_opened then
  597. def_closed.node_box = def.nodebox_closed
  598. else
  599. def_closed.node_box = {
  600. type = "fixed",
  601. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  602. }
  603. end
  604. def_closed.selection_box = {
  605. type = "fixed",
  606. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  607. }
  608. def_closed.tiles = {
  609. def.tile_front,
  610. def.tile_front .. '^[transformFY',
  611. def.tile_side,
  612. def.tile_side,
  613. def.tile_side,
  614. def.tile_side
  615. }
  616. if def.nodebox_opened and def.nodebox_closed then
  617. def_opened.node_box = def.nodebox_opened
  618. else
  619. def_opened.node_box = {
  620. type = "fixed",
  621. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  622. }
  623. end
  624. def_opened.selection_box = {
  625. type = "fixed",
  626. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  627. }
  628. def_opened.tiles = {
  629. def.tile_side,
  630. def.tile_side .. '^[transform2',
  631. def.tile_side .. '^[transform3',
  632. def.tile_side .. '^[transform1',
  633. def.tile_front .. '^[transform46',
  634. def.tile_front .. '^[transform6'
  635. }
  636. def_opened.drop = name_closed
  637. def_opened.groups.not_in_creative_inventory = 1
  638. minetest.register_node(name_opened, def_opened)
  639. minetest.register_node(name_closed, def_closed)
  640. doors.registered_trapdoors[name_opened] = true
  641. doors.registered_trapdoors[name_closed] = true
  642. end
  643. doors.register_trapdoor("doors:trapdoor", {
  644. description = S("Wooden Trapdoor"),
  645. inventory_image = "doors_trapdoor.png",
  646. wield_image = "doors_trapdoor.png",
  647. tile_front = "doors_trapdoor.png",
  648. tile_side = "doors_trapdoor_side.png",
  649. gain_open = 0.06,
  650. gain_close = 0.13,
  651. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2, door = 1},
  652. })
  653. doors.register_trapdoor("doors:trapdoor_steel", {
  654. description = S("Steel Trapdoor"),
  655. inventory_image = "doors_trapdoor_steel.png",
  656. wield_image = "doors_trapdoor_steel.png",
  657. tile_front = "doors_trapdoor_steel.png",
  658. tile_side = "doors_trapdoor_steel_side.png",
  659. protected = true,
  660. sounds = default.node_sound_metal_defaults(),
  661. sound_open = "doors_steel_door_open",
  662. sound_close = "doors_steel_door_close",
  663. gain_open = 0.2,
  664. gain_close = 0.2,
  665. groups = {cracky = 1, level = 2, door = 1},
  666. })
  667. minetest.register_craft({
  668. output = "doors:trapdoor 2",
  669. recipe = {
  670. {"group:wood", "group:wood", "group:wood"},
  671. {"group:wood", "group:wood", "group:wood"},
  672. {"", "", ""},
  673. }
  674. })
  675. minetest.register_craft({
  676. output = "doors:trapdoor_steel",
  677. recipe = {
  678. {"default:steel_ingot", "default:steel_ingot"},
  679. {"default:steel_ingot", "default:steel_ingot"},
  680. }
  681. })
  682. ----fence gate----
  683. local fence_collision_extra = minetest.settings:get_bool("enable_fence_tall") and 3/8 or 0
  684. function doors.register_fencegate(name, def)
  685. local fence = {
  686. description = def.description,
  687. drawtype = "mesh",
  688. tiles = {},
  689. paramtype = "light",
  690. paramtype2 = "facedir",
  691. sunlight_propagates = true,
  692. is_ground_content = false,
  693. drop = name .. "_closed",
  694. connect_sides = {"left", "right"},
  695. groups = def.groups,
  696. sounds = def.sounds,
  697. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  698. local node_def = minetest.registered_nodes[node.name]
  699. minetest.swap_node(pos, {name = node_def._gate, param2 = node.param2})
  700. minetest.sound_play(node_def._gate_sound, {pos = pos, gain = 0.15,
  701. max_hear_distance = 8}, true)
  702. return itemstack
  703. end,
  704. selection_box = {
  705. type = "fixed",
  706. fixed = {-1/2, -1/2, -1/4, 1/2, 1/2, 1/4}
  707. },
  708. }
  709. if type(def.texture) == "string" then
  710. fence.tiles[1] = {name = def.texture, backface_culling = true}
  711. elseif def.texture.backface_culling == nil then
  712. fence.tiles[1] = table.copy(def.texture)
  713. fence.tiles[1].backface_culling = true
  714. else
  715. fence.tiles[1] = def.texture
  716. end
  717. if not fence.sounds then
  718. fence.sounds = default.node_sound_wood_defaults()
  719. end
  720. fence.groups.fence = 1
  721. local fence_closed = table.copy(fence)
  722. fence_closed.mesh = "doors_fencegate_closed.obj"
  723. fence_closed._gate = name .. "_open"
  724. fence_closed._gate_sound = "doors_fencegate_open"
  725. fence_closed.collision_box = {
  726. type = "fixed",
  727. fixed = {-1/2, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  728. }
  729. local fence_open = table.copy(fence)
  730. fence_open.mesh = "doors_fencegate_open.obj"
  731. fence_open._gate = name .. "_closed"
  732. fence_open._gate_sound = "doors_fencegate_close"
  733. fence_open.groups.not_in_creative_inventory = 1
  734. fence_open.collision_box = {
  735. type = "fixed",
  736. fixed = {{-1/2, -1/2, -1/8, -3/8, 1/2 + fence_collision_extra, 1/8},
  737. {-1/2, -3/8, -1/2, -3/8, 3/8, 0 }}
  738. }
  739. minetest.register_node(":" .. name .. "_closed", fence_closed)
  740. minetest.register_node(":" .. name .. "_open", fence_open)
  741. minetest.register_craft({
  742. output = name .. "_closed",
  743. recipe = {
  744. {"group:stick", def.material, "group:stick"},
  745. {"group:stick", def.material, "group:stick"}
  746. }
  747. })
  748. end
  749. doors.register_fencegate("doors:gate_wood", {
  750. description = S("Apple Wood Fence Gate"),
  751. texture = "default_wood.png",
  752. material = "default:wood",
  753. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2}
  754. })
  755. doors.register_fencegate("doors:gate_acacia_wood", {
  756. description = S("Acacia Wood Fence Gate"),
  757. texture = "default_acacia_wood.png",
  758. material = "default:acacia_wood",
  759. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2}
  760. })
  761. doors.register_fencegate("doors:gate_junglewood", {
  762. description = S("Jungle Wood Fence Gate"),
  763. texture = "default_junglewood.png",
  764. material = "default:junglewood",
  765. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2}
  766. })
  767. doors.register_fencegate("doors:gate_pine_wood", {
  768. description = S("Pine Wood Fence Gate"),
  769. texture = "default_pine_wood.png",
  770. material = "default:pine_wood",
  771. groups = {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3}
  772. })
  773. doors.register_fencegate("doors:gate_aspen_wood", {
  774. description = S("Aspen Wood Fence Gate"),
  775. texture = "default_aspen_wood.png",
  776. material = "default:aspen_wood",
  777. groups = {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3}
  778. })
  779. ----fuels----
  780. minetest.register_craft({
  781. type = "fuel",
  782. recipe = "doors:trapdoor",
  783. burntime = 7,
  784. })
  785. minetest.register_craft({
  786. type = "fuel",
  787. recipe = "doors:door_wood",
  788. burntime = 14,
  789. })
  790. minetest.register_craft({
  791. type = "fuel",
  792. recipe = "doors:gate_wood_closed",
  793. burntime = 7,
  794. })
  795. minetest.register_craft({
  796. type = "fuel",
  797. recipe = "doors:gate_acacia_wood_closed",
  798. burntime = 8,
  799. })
  800. minetest.register_craft({
  801. type = "fuel",
  802. recipe = "doors:gate_junglewood_closed",
  803. burntime = 9,
  804. })
  805. minetest.register_craft({
  806. type = "fuel",
  807. recipe = "doors:gate_pine_wood_closed",
  808. burntime = 6,
  809. })
  810. minetest.register_craft({
  811. type = "fuel",
  812. recipe = "doors:gate_aspen_wood_closed",
  813. burntime = 5,
  814. })