tubecrowbar.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. --[[
  2. Hyperloop Mod
  3. =============
  4. Copyright (C) 2017-2019 Joachim Stolberg
  5. LGPLv2.1+
  6. See LICENSE.txt for more information
  7. ]]--
  8. -- for lazy programmers
  9. local SP = function(pos) if pos then return minetest.pos_to_string(pos) end end
  10. local P = minetest.string_to_pos
  11. local M = minetest.get_meta
  12. -- Load support for intllib.
  13. local S = vents.S
  14. local NS = vents.NS
  15. local Shaft = vents.Shaft
  16. local Tube = vents.Tube
  17. local function chat_message(dir, cnt, peer_pos, peer_dir)
  18. local sdir = tubelib2.dir_to_string(dir)
  19. if Shaft:is_secondary_node(peer_pos, peer_dir) then
  20. local npos, node = Shaft:get_node(peer_pos, peer_dir)
  21. return "[Vents] To the "..sdir..": "..cnt.." tube nodes to "..node.name.." at "..SP(npos)
  22. else
  23. return "[Vents] To the "..sdir..": "..cnt.." tube nodes to "..SP(peer_pos)
  24. end
  25. end
  26. local function repair_tubes(itemstack, placer, pointed_thing)
  27. if pointed_thing.type == "node" then
  28. local pos = pointed_thing.under
  29. local dir1, dir2, fpos1, fpos2, fdir1, fdir2, cnt1, cnt2 =
  30. Shaft:tool_repair_tube(pos, placer, pointed_thing)
  31. if fpos1 and fpos2 then
  32. if cnt1 + cnt2 >= Shaft.max_tube_length then
  33. minetest.chat_send_player(placer:get_player_name(), string.char(0x1b) ..
  34. "(c@#ff0000)" .. S("[Hyperloop] Error: Tube is too long!"))
  35. end
  36. minetest.chat_send_player(placer:get_player_name(), chat_message(dir1, cnt1, fpos1, fdir1))
  37. minetest.chat_send_player(placer:get_player_name(), chat_message(dir2, cnt2, fpos2, fdir2))
  38. minetest.sound_play({
  39. name="hyperloop_crowbar"},{
  40. pos = pos,
  41. gain=2,
  42. max_hear_distance=5,
  43. loop=false})
  44. else
  45. local dir1, dir2, fpos1, fpos2, fdir1, fdir2, cnt1, cnt2 =
  46. Tube:tool_repair_tube(pos, placer, pointed_thing)
  47. if fpos1 and fpos2 then
  48. if cnt1 + cnt2 >= Shaft.max_tube_length then
  49. minetest.chat_send_player(placer:get_player_name(), string.char(0x1b) ..
  50. "(c@#ff0000)" .. S("[Hyperloop] Error: Tube is too long!"))
  51. end
  52. minetest.chat_send_player(placer:get_player_name(), chat_message(dir1, cnt1, fpos1, fdir1))
  53. minetest.chat_send_player(placer:get_player_name(), chat_message(dir2, cnt2, fpos2, fdir2))
  54. minetest.sound_play({
  55. name="hyperloop_crowbar"},{
  56. pos = pos,
  57. gain=2,
  58. max_hear_distance=5,
  59. loop=false})
  60. end
  61. end
  62. else
  63. minetest.chat_send_player(placer:get_player_name(),
  64. S("[Crowbar Help]\n")..
  65. S(" left: remove node\n")..
  66. S(" right: repair tube/shaft line\n"))
  67. end
  68. end
  69. local function add_to_inventory(placer, item_name)
  70. local inv = placer:get_inventory()
  71. local item = ItemStack(item_name)
  72. if inv and item and inv:room_for_item("main", item) then
  73. inv:add_item("main", item)
  74. end
  75. end
  76. local function remove_tube(itemstack, placer, pointed_thing)
  77. if minetest.check_player_privs(placer:get_player_name(), "hyperloop") then
  78. if pointed_thing.type == "node" then
  79. local pos = pointed_thing.under
  80. if Shaft:tool_remove_tube(pos, "default_break_glass") then
  81. add_to_inventory(placer, "vents:shaft")
  82. elseif Tube:tool_remove_tube(pos, "default_break_glass") then
  83. add_to_inventory(placer, "vents:tubeS")
  84. end
  85. end
  86. else
  87. minetest.chat_send_player(placer:get_player_name(), S("You don't have the necessary privs!"))
  88. end
  89. end
  90. local function dump_data_base(pos)
  91. print(dump(vents.tDatabase))
  92. end
  93. -- Tool for tube workers to crack a protected tube line
  94. minetest.register_node("vents:tube_crowbar", {
  95. description = S("Hyperloop Tube Crowbar"),
  96. inventory_image = "hyperloop_tubecrowbar.png",
  97. wield_image = "hyperloop_tubecrowbar.png",
  98. use_texture_alpha = true,
  99. groups = {breakable=1, book=1, not_in_creative_inventory=1},
  100. on_use = remove_tube,
  101. on_place = repair_tubes,
  102. on_secondary_use = repair_tubes,
  103. node_placement_prediction = "",
  104. stack_max = 1,
  105. })
  106. minetest.register_privilege("hyperloop",
  107. {description = S("Rights to remove tube nodes by means of the crowbar"),
  108. give_to_singleplayer = false})
  109. if(minetest.get_modpath("worldedit")) ~= nil then
  110. minetest.register_chatcommand("hyperloop_repair_tubes", {
  111. description = S("Repair via WorldEdit placed Hyperloop tubes by reusing WorldEdit pos1/pos2"),
  112. privs = {worldedit=true},
  113. func = function(name, param)
  114. local pos1 = worldedit.pos1[name]
  115. local pos2 = worldedit.pos2[name]
  116. Tube:replace_tube_line(pos1, pos2)
  117. end,
  118. })
  119. end