init.lua 28 KB

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