init.lua 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. -- our API object
  2. doors = {}
  3. doors.modpath = minetest.get_modpath("doors")
  4. -- Localize for performance.
  5. local math_random = math.random
  6. -- private data
  7. local _doors = {}
  8. _doors.registered_doors = {}
  9. _doors.registered_trapdoors = {}
  10. -- returns an object to a door object or nil
  11. function doors.get(pos)
  12. local node_name = minetest.get_node(pos).name
  13. if _doors.registered_doors[node_name] then
  14. -- A normal upright door
  15. return {
  16. pos = pos,
  17. open = function(self, player)
  18. if self:state() then
  19. return false
  20. end
  21. return _doors.door_toggle(self.pos, nil, player)
  22. end,
  23. close = function(self, player)
  24. if not self:state() then
  25. return false
  26. end
  27. return _doors.door_toggle(self.pos, nil, player)
  28. end,
  29. toggle = function(self, player)
  30. return _doors.door_toggle(self.pos, nil, player)
  31. end,
  32. state = function(self)
  33. local state = minetest.get_meta(self.pos):get_int("state")
  34. return state %2 == 1
  35. end
  36. }
  37. elseif _doors.registered_trapdoors[node_name] then
  38. -- A trapdoor
  39. return {
  40. pos = pos,
  41. open = function(self, player)
  42. if self:state() then
  43. return false
  44. end
  45. return _doors.trapdoor_toggle(self.pos, nil, player)
  46. end,
  47. close = function(self, player)
  48. if not self:state() then
  49. return false
  50. end
  51. return _doors.trapdoor_toggle(self.pos, nil, player)
  52. end,
  53. toggle = function(self, player)
  54. return _doors.trapdoor_toggle(self.pos, nil, player)
  55. end,
  56. state = function(self)
  57. return minetest.get_node(self.pos).name:sub(-5) == "_open"
  58. end
  59. }
  60. else
  61. return nil
  62. end
  63. end
  64. -- this hidden node is placed on top of the bottom, and prevents
  65. -- nodes from being placed in the top half of the door.
  66. minetest.register_node("doors:hidden", {
  67. description = "Hidden Door Segment",
  68. -- can't use airlike otherwise falling nodes will turn to entities
  69. -- and will be forever stuck until door is removed.
  70. drawtype = "nodebox",
  71. paramtype = "light",
  72. paramtype2 = "facedir",
  73. sunlight_propagates = true,
  74. -- has to be walkable for falling nodes to stop falling.
  75. walkable = true,
  76. pointable = false,
  77. diggable = false,
  78. buildable_to = false,
  79. floodable = false,
  80. drop = "",
  81. groups = {not_in_creative_inventory = 1},
  82. on_blast = function() end,
  83. tiles = {"doors_blank.png"},
  84. -- 1px transparent block inside door hinge near node top.
  85. nodebox = {
  86. type = "fixed",
  87. fixed = {-15/32, 13/32, -15/32, -13/32, 1/2, -13/32},
  88. },
  89. -- collision_box needed otherise selection box would be full node size
  90. collision_box = {
  91. type = "fixed",
  92. fixed = {-15/32, 13/32, -15/32, -13/32, 1/2, -13/32},
  93. },
  94. })
  95. -- table used to aid door opening/closing
  96. local transform = {
  97. {
  98. {v = "_a", param2 = 3},
  99. {v = "_a", param2 = 0},
  100. {v = "_a", param2 = 1},
  101. {v = "_a", param2 = 2},
  102. },
  103. {
  104. {v = "_b", param2 = 1},
  105. {v = "_b", param2 = 2},
  106. {v = "_b", param2 = 3},
  107. {v = "_b", param2 = 0},
  108. },
  109. {
  110. {v = "_b", param2 = 1},
  111. {v = "_b", param2 = 2},
  112. {v = "_b", param2 = 3},
  113. {v = "_b", param2 = 0},
  114. },
  115. {
  116. {v = "_a", param2 = 3},
  117. {v = "_a", param2 = 0},
  118. {v = "_a", param2 = 1},
  119. {v = "_a", param2 = 2},
  120. },
  121. }
  122. -- Force door at position to toggle, produce no sound, no ownership checks.
  123. -- Function cannot/must not fail.
  124. function _doors.door_toggle_force(pos, node)
  125. local meta = minetest.get_meta(pos)
  126. node = node or minetest.get_node(pos)
  127. local def = minetest.reg_ns_nodes[node.name]
  128. local name = def.door.name
  129. local state = meta:get_string("state")
  130. if state == "" then
  131. -- fix up lvm-placed right-hinged doors, default closed
  132. if node.name:sub(-2) == "_b" then
  133. state = 2
  134. else
  135. state = 0
  136. end
  137. else
  138. state = tonumber(state)
  139. end
  140. -- until Lua-5.2 we have no bitwise operators :(
  141. if state % 2 == 1 then
  142. state = state - 1
  143. else
  144. state = state + 1
  145. end
  146. local dir = node.param2
  147. minetest.swap_node(pos, {
  148. name = name .. transform[state + 1][dir+1].v,
  149. param2 = transform[state + 1][dir+1].param2
  150. })
  151. meta:set_int("state", state)
  152. end
  153. function _doors.have_matching_door(pos, node)
  154. local nn = minetest.get_node(pos)
  155. if nn.name == node.name and nn.param2 == node.param2 then
  156. return true
  157. end
  158. end
  159. -- The OFFICIAL door toggle function.
  160. function _doors.door_toggle(pos, node, clicker)
  161. local meta = minetest.get_meta(pos)
  162. node = node or minetest.get_node(pos)
  163. local def = minetest.reg_ns_nodes[node.name]
  164. local name = def.door.name
  165. local state = meta:get_string("state")
  166. if state == "" then
  167. -- fix up lvm-placed right-hinged doors, default closed
  168. if node.name:sub(-2) == "_b" then
  169. state = 2
  170. else
  171. state = 0
  172. end
  173. else
  174. state = tonumber(state)
  175. end
  176. if clicker and not minetest.check_player_privs(clicker, "protection_bypass") then
  177. -- is player wielding the right key?
  178. local item = clicker:get_wielded_item()
  179. local owner = meta:get_string("doors_owner")
  180. if item:get_name() == "key:key" or item:get_name() == "key:chain" then
  181. local key_meta = item:get_meta()
  182. local secret = meta:get_string("key_lock_secret")
  183. if key_meta:get_string("secret") == "" then
  184. local key_oldmeta = item:get_metadata()
  185. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  186. ambiance.sound_play("doors_locked", pos, 1.0, 20)
  187. return false
  188. end
  189. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  190. item:set_metadata("")
  191. end
  192. if secret ~= key_meta:get_string("secret") then
  193. minetest.chat_send_player(clicker:get_player_name(), "# Server: Key does not fit lock!")
  194. ambiance.sound_play("doors_locked", pos, 1.0, 20)
  195. return false
  196. end
  197. elseif owner ~= "" then
  198. if clicker:get_player_name() ~= owner then
  199. ambiance.sound_play("doors_locked", pos, 1.0, 20)
  200. return false
  201. end
  202. end
  203. end
  204. local door_above = _doors.have_matching_door({x=pos.x, y=pos.y + 2, z=pos.z}, node)
  205. local door_below = _doors.have_matching_door({x=pos.x, y=pos.y - 2, z=pos.z}, node)
  206. -- until Lua-5.2 we have no bitwise operators :(
  207. if state % 2 == 1 then
  208. state = state - 1
  209. else
  210. state = state + 1
  211. end
  212. local dir = node.param2
  213. -- Play door open/close sound (check if hinges are oiled, in which case door makes no sound).
  214. local last_oiled = meta:get_int("oiled_time")
  215. if (os.time() - last_oiled) > math_random(0, 60*60*24*7) then
  216. if state % 2 == 0 then
  217. minetest.sound_play(def.door.sounds[1],
  218. {pos = pos, gain = 0.3, max_hear_distance = 20})
  219. else
  220. minetest.sound_play(def.door.sounds[2],
  221. {pos = pos, gain = 0.3, max_hear_distance = 20})
  222. end
  223. end
  224. minetest.swap_node(pos, {
  225. name = name .. transform[state + 1][dir+1].v,
  226. param2 = transform[state + 1][dir+1].param2
  227. })
  228. meta:set_int("state", state)
  229. if door_above then
  230. _doors.door_toggle_force({x=pos.x, y=pos.y + 2, z=pos.z}, node)
  231. end
  232. if door_below then
  233. _doors.door_toggle_force({x=pos.x, y=pos.y - 2, z=pos.z}, node)
  234. end
  235. return true
  236. end
  237. local function on_place_node(place_to, newnode,
  238. placer, oldnode, itemstack, pointed_thing)
  239. -- Run script hook
  240. for _, callback in ipairs(minetest.registered_on_placenodes) do
  241. -- Deepcopy pos, node and pointed_thing because callback can modify them
  242. local place_to_copy = {x = place_to.x, y = place_to.y, z = place_to.z}
  243. local newnode_copy =
  244. {name = newnode.name, param1 = newnode.param1, param2 = newnode.param2}
  245. local oldnode_copy =
  246. {name = oldnode.name, param1 = oldnode.param1, param2 = oldnode.param2}
  247. local pointed_thing_copy = {
  248. type = pointed_thing.type,
  249. above = vector.new(pointed_thing.above),
  250. under = vector.new(pointed_thing.under),
  251. ref = pointed_thing.ref,
  252. }
  253. callback(place_to_copy, newnode_copy, placer,
  254. oldnode_copy, itemstack, pointed_thing_copy)
  255. end
  256. end
  257. local function can_dig_door(pos, digger)
  258. local digger_name = digger and digger:get_player_name()
  259. if digger_name and minetest.get_player_privs(digger_name).protection_bypass then
  260. return true
  261. end
  262. return minetest.get_meta(pos):get_string("doors_owner") == digger_name
  263. end
  264. function doors.register(name, def)
  265. if not name:find(":") then
  266. name = "doors:" .. name
  267. end
  268. local inv_image = def.inventory_image
  269. if def.protected then
  270. inv_image = inv_image .. "^protector_lock.png"
  271. end
  272. minetest.register_craftitem(":" .. name, {
  273. description = def.description,
  274. inventory_image = inv_image,
  275. groups = def.groups,
  276. on_place = function(itemstack, placer, pointed_thing)
  277. local pos
  278. if not pointed_thing.type == "node" then
  279. return itemstack
  280. end
  281. -- Pass through interactions to nodes that define them (like chests).
  282. local node = minetest.get_node(pointed_thing.under)
  283. local pdef = minetest.reg_ns_nodes[node.name]
  284. if pdef and pdef.on_rightclick and not placer:get_player_control().sneak then
  285. return pdef.on_rightclick(pointed_thing.under, node, placer, itemstack, pointed_thing)
  286. end
  287. if pdef and pdef.buildable_to then
  288. pos = pointed_thing.under
  289. else
  290. pos = pointed_thing.above
  291. node = minetest.get_node(pos)
  292. pdef = minetest.reg_ns_nodes[node.name]
  293. if not pdef or not pdef.buildable_to then
  294. return itemstack
  295. end
  296. end
  297. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  298. local top_node = minetest.get_node_or_nil(above)
  299. local topdef = top_node and minetest.reg_ns_nodes[top_node.name]
  300. if not topdef or not topdef.buildable_to then
  301. return itemstack
  302. end
  303. local pn = placer:get_player_name()
  304. if minetest.is_protected(pos, pn) or minetest.is_protected(above, pn) then
  305. return itemstack
  306. end
  307. local dir = minetest.dir_to_facedir(placer:get_look_dir())
  308. local ref = {
  309. {x = -1, y = 0, z = 0},
  310. {x = 0, y = 0, z = 1},
  311. {x = 1, y = 0, z = 0},
  312. {x = 0, y = 0, z = -1},
  313. }
  314. local aside = {
  315. x = pos.x + ref[dir + 1].x,
  316. y = pos.y + ref[dir + 1].y,
  317. z = pos.z + ref[dir + 1].z,
  318. }
  319. local state = 0
  320. if minetest.get_item_group(minetest.get_node(aside).name, "door") == 1 then
  321. state = state + 2
  322. minetest.add_node(pos, {name = name .. "_b", param2 = dir})
  323. minetest.add_node(above, {name = "doors:hidden", param2 = (dir + 3) % 4})
  324. else
  325. minetest.add_node(pos, {name = name .. "_a", param2 = dir})
  326. minetest.add_node(above, {name = "doors:hidden", param2 = dir})
  327. end
  328. local meta = minetest.get_meta(pos)
  329. meta:set_int("state", state)
  330. if def.protected then
  331. meta:set_string("doors_owner", pn)
  332. local dname = rename.gpn(pn)
  333. meta:set_string("rename", dname)
  334. meta:set_string("infotext", "Locked Door (Owned by <" .. dname .. ">!)")
  335. end
  336. if not minetest.setting_getbool("creative_mode") then
  337. itemstack:take_item()
  338. end
  339. on_place_node(pos, minetest.get_node(pos),
  340. placer, node, itemstack, pointed_thing)
  341. return itemstack
  342. end
  343. })
  344. def.inventory_image = nil
  345. if def.recipe then
  346. minetest.register_craft({
  347. output = name,
  348. recipe = def.recipe,
  349. })
  350. end
  351. def.recipe = nil
  352. if not def.sounds then
  353. def.sounds = default.node_sound_wood_defaults()
  354. end
  355. if not def.sound_open then
  356. def.sound_open = "doors_door_open"
  357. end
  358. if not def.sound_close then
  359. def.sound_close = "doors_door_close"
  360. end
  361. def.groups.not_in_creative_inventory = 1
  362. def.groups.door = 1
  363. def.drop = name
  364. def.door = {
  365. name = name,
  366. sounds = { def.sound_close, def.sound_open },
  367. }
  368. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  369. _doors.door_toggle(pos, node, clicker)
  370. return itemstack
  371. end
  372. def.after_dig_node = function(pos, node, meta, digger)
  373. minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
  374. minetest.check_for_falling({x = pos.x, y = pos.y + 1, z = pos.z})
  375. end
  376. def.on_rotate = function(pos, node, user, mode, new_param2)
  377. return false
  378. end
  379. if def.protected then
  380. def.can_dig = can_dig_door
  381. def.on_blast = function() end
  382. def.on_key_use = function(pos, player)
  383. local door = doors.get(pos)
  384. door:toggle(player)
  385. end
  386. def.on_skeleton_key_use = function(pos, player, newsecret)
  387. local meta = minetest.get_meta(pos)
  388. local owner = meta:get_string("doors_owner")
  389. local pname = player:get_player_name()
  390. -- verify placer is owner of lockable door
  391. if not gdac.player_is_admin(pname) then
  392. if owner ~= pname then
  393. minetest.record_protection_violation(pos, pname)
  394. minetest.chat_send_player(pname, "# Server: You do not own this locked door.")
  395. return nil
  396. end
  397. end
  398. local secret = meta:get_string("key_lock_secret")
  399. if secret == "" then
  400. secret = newsecret
  401. meta:set_string("key_lock_secret", secret)
  402. end
  403. return secret, "a locked door", owner
  404. end
  405. -- Called by rename LBM.
  406. def._on_rename_check = function(pos)
  407. local meta = minetest.get_meta(pos)
  408. local owner = meta:get_string("doors_owner")
  409. -- Nobody placed this block.
  410. if owner == "" then
  411. return
  412. end
  413. local dname = rename.gpn(owner)
  414. meta:set_string("rename", dname)
  415. meta:set_string("infotext", "Locked Door (Owned by <" .. dname .. ">!)")
  416. end
  417. -- Disable client dig prediction.
  418. def.node_dig_prediction = ""
  419. else
  420. def.on_blast = function(pos, intensity)
  421. minetest.remove_node(pos)
  422. -- hidden node doesn't get blasted away.
  423. minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
  424. return {name}
  425. end
  426. end
  427. def.on_destruct = function(pos)
  428. minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
  429. end
  430. def.drawtype = "mesh"
  431. def.paramtype = "light"
  432. def.paramtype2 = "facedir"
  433. def.sunlight_propagates = true
  434. def.walkable = true
  435. def.is_ground_content = false
  436. def.buildable_to = false
  437. def.selection_box = {type = "fixed", fixed = {-1/2,-1/2,-1/2,1/2,3/2,-6/16}}
  438. def.collision_box = {type = "fixed", fixed = {-1/2,-1/2,-1/2,1/2,3/2,-6/16}}
  439. def.mesh = "door_a.obj"
  440. minetest.register_node(":" .. name .. "_a", def)
  441. def.mesh = "door_b.obj"
  442. minetest.register_node(":" .. name .. "_b", def)
  443. _doors.registered_doors[name .. "_a"] = true
  444. _doors.registered_doors[name .. "_b"] = true
  445. end
  446. -- Capture mods using the old API as best as possible.
  447. function doors.register_door(name, def)
  448. if def.only_placer_can_open then
  449. def.protected = true
  450. end
  451. def.only_placer_can_open = nil
  452. local i = name:find(":")
  453. local modname = name:sub(1, i - 1)
  454. if not def.tiles then
  455. if def.protected then
  456. def.tiles = {{name = "doors_door_steel.png", backface_culling = true}}
  457. else
  458. def.tiles = {{name = "doors_door_wood.png", backface_culling = true}}
  459. end
  460. minetest.log("warning", modname .. " registered door \"" .. name .. "\" " ..
  461. "using deprecated API method \"doors.register_door()\" but " ..
  462. "did not provide the \"tiles\" parameter. A fallback tiledef " ..
  463. "will be used instead.")
  464. end
  465. doors.register(name, def)
  466. end
  467. ----trapdoor----
  468. function _doors.trapdoor_toggle(pos, node, clicker)
  469. node = node or minetest.get_node(pos)
  470. local meta = minetest.get_meta(pos)
  471. if clicker and not minetest.check_player_privs(clicker, "protection_bypass") then
  472. -- is player wielding the right key?
  473. local item = clicker:get_wielded_item()
  474. local owner = meta:get_string("doors_owner")
  475. if item:get_name() == "key:key" or item:get_name() == "key:chain" then
  476. local key_meta = item:get_meta()
  477. local secret = meta:get_string("key_lock_secret")
  478. if key_meta:get_string("secret") == "" then
  479. local key_oldmeta = item:get_metadata()
  480. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  481. ambiance.sound_play("doors_locked", pos, 1.0, 20)
  482. return false
  483. end
  484. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  485. item:set_metadata("")
  486. end
  487. if secret ~= key_meta:get_string("secret") then
  488. minetest.chat_send_player(clicker:get_player_name(), "# Server: Key does not fit lock!")
  489. ambiance.sound_play("doors_locked", pos, 1.0, 20)
  490. return false
  491. end
  492. elseif owner ~= "" then
  493. if clicker:get_player_name() ~= owner then
  494. ambiance.sound_play("doors_locked", pos, 1.0, 20)
  495. return false
  496. end
  497. end
  498. end
  499. local def = minetest.reg_ns_nodes[node.name]
  500. -- Play trapdoor open/close sound (check if hinges are oiled, in which case door makes no sound).
  501. local play_sound = false
  502. local last_oiled = meta:get_int("oiled_time")
  503. if (os.time() - last_oiled) > math_random(0, 60*60*24*7) then
  504. play_sound = true
  505. end
  506. if string.sub(node.name, -5) == "_open" then
  507. if play_sound then
  508. minetest.sound_play(def.sound_close,
  509. {pos = pos, gain = 0.3, max_hear_distance = 20})
  510. end
  511. minetest.swap_node(pos, {name = string.sub(node.name, 1,
  512. string.len(node.name) - 5), param1 = node.param1, param2 = node.param2})
  513. else
  514. if play_sound then
  515. minetest.sound_play(def.sound_open,
  516. {pos = pos, gain = 0.3, max_hear_distance = 20})
  517. end
  518. minetest.swap_node(pos, {name = node.name .. "_open",
  519. param1 = node.param1, param2 = node.param2})
  520. end
  521. end
  522. function doors.register_trapdoor(name, def)
  523. local origname = name
  524. local origdef = table.copy(def)
  525. if not name:find(":") then
  526. name = "doors:" .. name
  527. end
  528. def.groups = def.groups or {}
  529. def.groups.trapdoor = 1
  530. local name_closed = name
  531. local name_opened = name.."_open"
  532. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  533. _doors.trapdoor_toggle(pos, node, clicker)
  534. return itemstack
  535. end
  536. -- Common trapdoor configuration
  537. def.drawtype = "nodebox"
  538. def.paramtype = "light"
  539. def.paramtype2 = "facedir"
  540. def.is_ground_content = false
  541. if def.protected then
  542. def.can_dig = can_dig_door
  543. def.after_place_node = function(pos, placer, itemstack, pointed_thing)
  544. local pn = placer:get_player_name()
  545. local meta = minetest.get_meta(pos)
  546. meta:set_string("doors_owner", pn)
  547. local dname = rename.gpn(pn)
  548. meta:set_string("rename", dname)
  549. meta:set_string("infotext", "Locked Trapdoor (Owned by <" .. dname .. ">!)")
  550. return minetest.setting_getbool("creative_mode")
  551. end
  552. def.on_blast = function() end
  553. def.on_key_use = function(pos, player)
  554. local door = doors.get(pos)
  555. door:toggle(player)
  556. end
  557. def.on_skeleton_key_use = function(pos, player, newsecret)
  558. local meta = minetest.get_meta(pos)
  559. local owner = meta:get_string("doors_owner")
  560. local pname = player:get_player_name()
  561. -- verify placer is owner of lockable door
  562. if not gdac.player_is_admin(pname) then
  563. if owner ~= pname then
  564. minetest.record_protection_violation(pos, pname)
  565. minetest.chat_send_player(pname, "# Server: You do not own this trapdoor.")
  566. return nil
  567. end
  568. end
  569. local secret = meta:get_string("key_lock_secret")
  570. if secret == "" then
  571. secret = newsecret
  572. meta:set_string("key_lock_secret", secret)
  573. end
  574. return secret, "a locked trapdoor", owner
  575. end
  576. -- Called by rename LBM.
  577. def._on_rename_check = function(pos)
  578. local meta = minetest.get_meta(pos)
  579. local owner = meta:get_string("doors_owner")
  580. -- Nobody placed this block.
  581. if owner == "" then
  582. return
  583. end
  584. local dname = rename.gpn(owner)
  585. meta:set_string("rename", dname)
  586. meta:set_string("infotext", "Locked Trapdoor (Owned by <" .. dname .. ">!)")
  587. end
  588. -- Disable client dig prediction.
  589. def.node_dig_prediction = ""
  590. else
  591. def.on_blast = function(pos, intensity)
  592. minetest.remove_node(pos)
  593. return {name}
  594. end
  595. end
  596. if not def.sounds then
  597. def.sounds = default.node_sound_wood_defaults()
  598. end
  599. if not def.sound_open then
  600. def.sound_open = "doors_door_open"
  601. end
  602. if not def.sound_close then
  603. def.sound_close = "doors_door_close"
  604. end
  605. local def_opened = table.copy(def)
  606. local def_closed = table.copy(def)
  607. def_closed.node_box = {
  608. type = "fixed",
  609. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  610. }
  611. def_closed.selection_box = {
  612. type = "fixed",
  613. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  614. }
  615. def_closed.tiles = {def.tile_front,
  616. def.tile_front .. '^[transformFY',
  617. def.tile_side, def.tile_side,
  618. def.tile_side, def.tile_side}
  619. def_opened.node_box = {
  620. type = "fixed",
  621. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  622. }
  623. def_opened.selection_box = {
  624. type = "fixed",
  625. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  626. }
  627. def_opened.tiles = {def.tile_side, def.tile_side,
  628. def.tile_side .. '^[transform3',
  629. def.tile_side .. '^[transform1',
  630. def.tile_front .. '^[transform46',
  631. def.tile_front .. '^[transform6'}
  632. def_opened.drop = name_closed
  633. def_opened.groups.not_in_creative_inventory = 1
  634. if def.protected then
  635. def_closed.inventory_image = def_closed.inventory_image .. "^protector_lock.png"
  636. def_opened.inventory_image = def_opened.inventory_image .. "^protector_lock.png"
  637. end
  638. minetest.register_node(name_opened, def_opened)
  639. minetest.register_node(name_closed, def_closed)
  640. if def.recipeitem then
  641. if def.protected then
  642. minetest.register_craft({
  643. output = name,
  644. recipe = {
  645. {'', 'default:padlock', ''},
  646. {def.recipeitem, def.recipeitem, 'group:stick'},
  647. {def.recipeitem, def.recipeitem, 'group:stick'},
  648. }
  649. })
  650. else
  651. minetest.register_craft({
  652. output = name,
  653. recipe = {
  654. {def.recipeitem, def.recipeitem, 'group:stick'},
  655. {def.recipeitem, def.recipeitem, 'group:stick'},
  656. }
  657. })
  658. end
  659. end
  660. _doors.registered_trapdoors[name_opened] = true
  661. _doors.registered_trapdoors[name_closed] = true
  662. doors.register_trapdoor_climbable("climbable_" .. origname, origdef)
  663. end
  664. function doors.register_trapdoor_climbable(name, def)
  665. if not name:find(":") then
  666. name = "doors:" .. name
  667. end
  668. def.groups = def.groups or {}
  669. def.groups.trapdoor = 1
  670. local name_closed = name
  671. local name_opened = name.."_open"
  672. def.description = def.description .. " With Ladder"
  673. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  674. _doors.trapdoor_toggle(pos, node, clicker)
  675. return itemstack
  676. end
  677. -- Common trapdoor configuration
  678. def.drawtype = "nodebox"
  679. def.paramtype = "light"
  680. def.paramtype2 = "facedir"
  681. def.climbable = true
  682. def.is_ground_content = false
  683. if def.protected then
  684. def.can_dig = can_dig_door
  685. def.after_place_node = function(pos, placer, itemstack, pointed_thing)
  686. local pn = placer:get_player_name()
  687. local meta = minetest.get_meta(pos)
  688. meta:set_string("doors_owner", pn)
  689. local dname = rename.gpn(pn)
  690. meta:set_string("rename", dname)
  691. meta:set_string("infotext", "Locked Trapdoor (Owned by <" .. dname .. ">!)")
  692. return minetest.setting_getbool("creative_mode")
  693. end
  694. def.on_blast = function() end
  695. def.on_key_use = function(pos, player)
  696. local door = doors.get(pos)
  697. door:toggle(player)
  698. end
  699. def.on_skeleton_key_use = function(pos, player, newsecret)
  700. local meta = minetest.get_meta(pos)
  701. local owner = meta:get_string("doors_owner")
  702. local pname = player:get_player_name()
  703. -- verify placer is owner of lockable door
  704. if not gdac.player_is_admin(pname) then
  705. if owner ~= pname then
  706. minetest.record_protection_violation(pos, pname)
  707. minetest.chat_send_player(pname, "# Server: You do not own this trapdoor.")
  708. return nil
  709. end
  710. end
  711. local secret = meta:get_string("key_lock_secret")
  712. if secret == "" then
  713. secret = newsecret
  714. meta:set_string("key_lock_secret", secret)
  715. end
  716. return secret, "a locked trapdoor", owner
  717. end
  718. -- Called by rename LBM.
  719. def._on_rename_check = function(pos)
  720. local meta = minetest.get_meta(pos)
  721. local owner = meta:get_string("doors_owner")
  722. -- Nobody placed this block.
  723. if owner == "" then
  724. return
  725. end
  726. local dname = rename.gpn(owner)
  727. meta:set_string("rename", dname)
  728. meta:set_string("infotext", "Locked Trapdoor (Owned by <" .. dname .. ">!)")
  729. end
  730. -- Disable client dig prediction.
  731. def.node_dig_prediction = ""
  732. else
  733. def.on_blast = function(pos, intensity)
  734. minetest.remove_node(pos)
  735. return {name}
  736. end
  737. end
  738. if not def.sounds then
  739. def.sounds = default.node_sound_wood_defaults()
  740. end
  741. if not def.sound_open then
  742. def.sound_open = "doors_door_open"
  743. end
  744. if not def.sound_close then
  745. def.sound_close = "doors_door_close"
  746. end
  747. local def_opened = table.copy(def)
  748. local def_closed = table.copy(def)
  749. def_closed.node_box = {
  750. type = "fixed",
  751. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  752. }
  753. def_closed.selection_box = {
  754. type = "fixed",
  755. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  756. }
  757. def_closed.tiles = {def.tile_front,
  758. def.tile_front .. '^[transformFY',
  759. def.tile_side, def.tile_side,
  760. def.tile_side, def.tile_side}
  761. def_opened.node_box = {
  762. type = "fixed",
  763. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  764. }
  765. def_opened.selection_box = {
  766. type = "fixed",
  767. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  768. }
  769. def_opened.tiles = {def.tile_side, def.tile_side,
  770. def.tile_side .. '^[transform3',
  771. def.tile_side .. '^[transform1',
  772. def.tile_front .. '^[transform46',
  773. def.tile_front .. '^[transform6'}
  774. def_opened.drop = name_closed
  775. def_opened.groups.not_in_creative_inventory = 1
  776. if def.protected then
  777. def_closed.inventory_image = def_closed.inventory_image .. "^protector_lock.png"
  778. def_opened.inventory_image = def_opened.inventory_image .. "^protector_lock.png"
  779. end
  780. def_closed.inventory_image = def_closed.inventory_image .. "^doors_ladder.png"
  781. def_opened.inventory_image = def_opened.inventory_image .. "^doors_ladder.png"
  782. minetest.register_node(name_opened, def_opened)
  783. minetest.register_node(name_closed, def_closed)
  784. if def.recipeitem then
  785. if def.protected then
  786. minetest.register_craft({
  787. output = name,
  788. recipe = {
  789. {'', 'default:padlock', ''},
  790. {def.recipeitem, def.recipeitem, 'default:ladder_wood'},
  791. {def.recipeitem, def.recipeitem, 'default:ladder_wood'},
  792. }
  793. })
  794. else
  795. minetest.register_craft({
  796. output = name,
  797. recipe = {
  798. {def.recipeitem, def.recipeitem, 'default:ladder_wood'},
  799. {def.recipeitem, def.recipeitem, 'default:ladder_wood'},
  800. }
  801. })
  802. end
  803. end
  804. _doors.registered_trapdoors[name_opened] = true
  805. _doors.registered_trapdoors[name_closed] = true
  806. end
  807. ----fence gate----
  808. function doors.register_fencegate(name, def)
  809. local fence = {
  810. description = def.description,
  811. drawtype = "mesh",
  812. tiles = {def.texture},
  813. paramtype = "light",
  814. paramtype2 = "facedir",
  815. sunlight_propagates = true,
  816. is_ground_content = false,
  817. drop = name .. "_closed",
  818. connect_sides = {"left", "right"},
  819. groups = def.groups,
  820. sounds = def.sounds,
  821. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  822. local node_def = minetest.reg_ns_nodes[node.name]
  823. minetest.swap_node(pos, {name = node_def.gate, param2 = node.param2})
  824. minetest.sound_play(node_def.sound, {pos = pos, gain = 0.3,
  825. max_hear_distance = 20})
  826. return itemstack
  827. end,
  828. selection_box = {
  829. type = "fixed",
  830. fixed = {-1/2, -1/2, -1/4, 1/2, 1/2, 1/4},
  831. },
  832. }
  833. if not fence.sounds then
  834. fence.sounds = default.node_sound_wood_defaults()
  835. end
  836. fence.groups.fence = 1
  837. local fence_closed = table.copy(fence)
  838. fence_closed.mesh = "doors_fencegate_closed.obj"
  839. fence_closed.gate = name .. "_open"
  840. fence_closed.sound = "doors_fencegate_open"
  841. fence_closed.collision_box = {
  842. type = "fixed",
  843. fixed = {-1/2, -1/2, -1/4, 1/2, 1/2, 1/4},
  844. }
  845. local fence_open = table.copy(fence)
  846. fence_open.mesh = "doors_fencegate_open.obj"
  847. fence_open.gate = name .. "_closed"
  848. fence_open.sound = "doors_fencegate_close"
  849. fence_open.groups.not_in_creative_inventory = 1
  850. fence_open.collision_box = {
  851. type = "fixed",
  852. fixed = {{-1/2, -1/2, -1/4, -3/8, 1/2, 1/4},
  853. {-1/2, -3/8, -1/2, -3/8, 3/8, 0}},
  854. }
  855. minetest.register_node(":" .. name .. "_closed", fence_closed)
  856. minetest.register_node(":" .. name .. "_open", fence_open)
  857. minetest.register_craft({
  858. output = name .. "_closed",
  859. recipe = {
  860. {"group:stick", def.material, "group:stick"},
  861. {"group:stick", def.material, "group:stick"}
  862. }
  863. })
  864. end
  865. dofile(doors.modpath .. "/doors.lua")
  866. dofile(doors.modpath .. "/trapdoors.lua")
  867. dofile(doors.modpath .. "/gates.lua")