init.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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}, true)
  219. else
  220. minetest.sound_play(def.door.sounds[2],
  221. {pos = pos, gain = 0.3, max_hear_distance = 20}, true)
  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}, true)
  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}, true)
  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. local hinge = "techcrafts:hinge"
  642. if def.recipehinge then
  643. hinge = def.recipehinge
  644. end
  645. if def.protected then
  646. minetest.register_craft({
  647. output = name,
  648. recipe = {
  649. {'', 'default:padlock', ''},
  650. {def.recipeitem, def.recipeitem, 'group:stick'},
  651. {def.recipeitem, def.recipeitem, hinge},
  652. }
  653. })
  654. else
  655. minetest.register_craft({
  656. output = name,
  657. recipe = {
  658. {def.recipeitem, def.recipeitem, 'group:stick'},
  659. {def.recipeitem, def.recipeitem, hinge},
  660. }
  661. })
  662. end
  663. end
  664. _doors.registered_trapdoors[name_opened] = true
  665. _doors.registered_trapdoors[name_closed] = true
  666. doors.register_trapdoor_climbable("climbable_" .. origname, origdef)
  667. end
  668. function doors.register_trapdoor_climbable(name, def)
  669. if not name:find(":") then
  670. name = "doors:" .. name
  671. end
  672. def.groups = def.groups or {}
  673. def.groups.trapdoor = 1
  674. local name_closed = name
  675. local name_opened = name.."_open"
  676. def.description = def.description .. " With Ladder"
  677. def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  678. _doors.trapdoor_toggle(pos, node, clicker)
  679. return itemstack
  680. end
  681. -- Common trapdoor configuration
  682. def.drawtype = "nodebox"
  683. def.paramtype = "light"
  684. def.paramtype2 = "facedir"
  685. def.climbable = true
  686. def.is_ground_content = false
  687. if def.protected then
  688. def.can_dig = can_dig_door
  689. def.after_place_node = function(pos, placer, itemstack, pointed_thing)
  690. local pn = placer:get_player_name()
  691. local meta = minetest.get_meta(pos)
  692. meta:set_string("doors_owner", pn)
  693. local dname = rename.gpn(pn)
  694. meta:set_string("rename", dname)
  695. meta:set_string("infotext", "Locked Trapdoor (Owned by <" .. dname .. ">!)")
  696. return minetest.setting_getbool("creative_mode")
  697. end
  698. def.on_blast = function() end
  699. def.on_key_use = function(pos, player)
  700. local door = doors.get(pos)
  701. door:toggle(player)
  702. end
  703. def.on_skeleton_key_use = function(pos, player, newsecret)
  704. local meta = minetest.get_meta(pos)
  705. local owner = meta:get_string("doors_owner")
  706. local pname = player:get_player_name()
  707. -- verify placer is owner of lockable door
  708. if not gdac.player_is_admin(pname) then
  709. if owner ~= pname then
  710. minetest.record_protection_violation(pos, pname)
  711. minetest.chat_send_player(pname, "# Server: You do not own this trapdoor.")
  712. return nil
  713. end
  714. end
  715. local secret = meta:get_string("key_lock_secret")
  716. if secret == "" then
  717. secret = newsecret
  718. meta:set_string("key_lock_secret", secret)
  719. end
  720. return secret, "a locked trapdoor", owner
  721. end
  722. -- Called by rename LBM.
  723. def._on_rename_check = function(pos)
  724. local meta = minetest.get_meta(pos)
  725. local owner = meta:get_string("doors_owner")
  726. -- Nobody placed this block.
  727. if owner == "" then
  728. return
  729. end
  730. local dname = rename.gpn(owner)
  731. meta:set_string("rename", dname)
  732. meta:set_string("infotext", "Locked Trapdoor (Owned by <" .. dname .. ">!)")
  733. end
  734. -- Disable client dig prediction.
  735. def.node_dig_prediction = ""
  736. else
  737. def.on_blast = function(pos, intensity)
  738. minetest.remove_node(pos)
  739. return {name}
  740. end
  741. end
  742. if not def.sounds then
  743. def.sounds = default.node_sound_wood_defaults()
  744. end
  745. if not def.sound_open then
  746. def.sound_open = "doors_door_open"
  747. end
  748. if not def.sound_close then
  749. def.sound_close = "doors_door_close"
  750. end
  751. local def_opened = table.copy(def)
  752. local def_closed = table.copy(def)
  753. def_closed.node_box = {
  754. type = "fixed",
  755. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  756. }
  757. def_closed.selection_box = {
  758. type = "fixed",
  759. fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
  760. }
  761. def_closed.tiles = {def.tile_front,
  762. def.tile_front .. '^[transformFY',
  763. def.tile_side, def.tile_side,
  764. def.tile_side, def.tile_side}
  765. def_opened.node_box = {
  766. type = "fixed",
  767. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  768. }
  769. def_opened.selection_box = {
  770. type = "fixed",
  771. fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
  772. }
  773. def_opened.tiles = {def.tile_side, def.tile_side,
  774. def.tile_side .. '^[transform3',
  775. def.tile_side .. '^[transform1',
  776. def.tile_front .. '^[transform46',
  777. def.tile_front .. '^[transform6'}
  778. def_opened.drop = name_closed
  779. def_opened.groups.not_in_creative_inventory = 1
  780. if def.protected then
  781. def_closed.inventory_image = def_closed.inventory_image .. "^protector_lock.png"
  782. def_opened.inventory_image = def_opened.inventory_image .. "^protector_lock.png"
  783. end
  784. def_closed.inventory_image = def_closed.inventory_image .. "^doors_ladder.png"
  785. def_opened.inventory_image = def_opened.inventory_image .. "^doors_ladder.png"
  786. minetest.register_node(name_opened, def_opened)
  787. minetest.register_node(name_closed, def_closed)
  788. if def.recipeitem then
  789. local hinge = "techcrafts:hinge"
  790. if def.recipehinge then
  791. hinge = def.recipehinge
  792. end
  793. if def.protected then
  794. minetest.register_craft({
  795. output = name,
  796. recipe = {
  797. {'', 'default:padlock', ''},
  798. {def.recipeitem, def.recipeitem, 'default:ladder_wood'},
  799. {def.recipeitem, def.recipeitem, hinge},
  800. }
  801. })
  802. else
  803. minetest.register_craft({
  804. output = name,
  805. recipe = {
  806. {def.recipeitem, def.recipeitem, 'default:ladder_wood'},
  807. {def.recipeitem, def.recipeitem, hinge},
  808. }
  809. })
  810. end
  811. end
  812. _doors.registered_trapdoors[name_opened] = true
  813. _doors.registered_trapdoors[name_closed] = true
  814. end
  815. ----fence gate----
  816. function doors.register_fencegate(name, def)
  817. local fence = {
  818. description = def.description,
  819. drawtype = "mesh",
  820. tiles = {def.texture},
  821. paramtype = "light",
  822. paramtype2 = "facedir",
  823. sunlight_propagates = true,
  824. is_ground_content = false,
  825. drop = name .. "_closed",
  826. connect_sides = {"left", "right"},
  827. groups = def.groups,
  828. sounds = def.sounds,
  829. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  830. local node_def = minetest.reg_ns_nodes[node.name]
  831. minetest.swap_node(pos, {name = node_def.gate, param2 = node.param2})
  832. minetest.sound_play(node_def.sound, {pos = pos, gain = 0.3,
  833. max_hear_distance = 20}, true)
  834. return itemstack
  835. end,
  836. selection_box = {
  837. type = "fixed",
  838. fixed = {-1/2, -1/2, -1/4, 1/2, 1/2, 1/4},
  839. },
  840. }
  841. if not fence.sounds then
  842. fence.sounds = default.node_sound_wood_defaults()
  843. end
  844. fence.groups.fence = 1
  845. local fence_closed = table.copy(fence)
  846. fence_closed.mesh = "doors_fencegate_closed.obj"
  847. fence_closed.gate = name .. "_open"
  848. fence_closed.sound = "doors_fencegate_open"
  849. fence_closed.collision_box = {
  850. type = "fixed",
  851. fixed = {-1/2, -1/2, -1/4, 1/2, 1/2, 1/4},
  852. }
  853. local fence_open = table.copy(fence)
  854. fence_open.mesh = "doors_fencegate_open.obj"
  855. fence_open.gate = name .. "_closed"
  856. fence_open.sound = "doors_fencegate_close"
  857. fence_open.groups.not_in_creative_inventory = 1
  858. fence_open.collision_box = {
  859. type = "fixed",
  860. fixed = {{-1/2, -1/2, -1/4, -3/8, 1/2, 1/4},
  861. {-1/2, -3/8, -1/2, -3/8, 3/8, 0}},
  862. }
  863. minetest.register_node(":" .. name .. "_closed", fence_closed)
  864. minetest.register_node(":" .. name .. "_open", fence_open)
  865. do
  866. local hinge = "techcrafts:hinge"
  867. if minetest.get_item_group(def.material, "wood") ~= 0 then
  868. hinge = "techcrafts:hinge_wood"
  869. end
  870. minetest.register_craft({
  871. output = name .. "_closed",
  872. recipe = {
  873. {hinge, def.material, "group:stick"},
  874. {"group:stick", def.material, "group:stick"},
  875. }
  876. })
  877. end
  878. end
  879. dofile(doors.modpath .. "/doors.lua")
  880. dofile(doors.modpath .. "/trapdoors.lua")
  881. dofile(doors.modpath .. "/gates.lua")