Pipeworks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

272 lines
8.6 KiB

  1. local S = minetest.get_translator("pipeworks")
  2. local filename=minetest.get_worldpath() .. "/teleport_tubes"
  3. local tp_tube_db = nil -- nil forces a read
  4. local tp_tube_db_version = 2.0
  5. local function hash(pos)
  6. return string.format("%.30g", minetest.hash_node_position(pos))
  7. end
  8. local function save_tube_db()
  9. local file, err = io.open(filename, "w")
  10. if file then
  11. tp_tube_db.version = tp_tube_db_version
  12. file:write(minetest.serialize(tp_tube_db))
  13. tp_tube_db.version = nil
  14. io.close(file)
  15. else
  16. error(err)
  17. end
  18. end
  19. local function migrate_tube_db()
  20. local tmp_db = {}
  21. tp_tube_db.version = nil
  22. for key, val in pairs(tp_tube_db) do
  23. if(val.channel ~= "") then -- skip unconfigured tubes
  24. tmp_db[hash(val)] = val
  25. end
  26. end
  27. tp_tube_db = tmp_db
  28. save_tube_db()
  29. end
  30. local function read_tube_db()
  31. local file = io.open(filename, "r")
  32. if file ~= nil then
  33. local file_content = file:read("*all")
  34. io.close(file)
  35. if file_content and file_content ~= "" then
  36. tp_tube_db = minetest.deserialize(file_content)
  37. if(not tp_tube_db.version or tonumber(tp_tube_db.version) < tp_tube_db_version) then
  38. migrate_tube_db()
  39. end
  40. tp_tube_db.version = nil -- we add it back when saving
  41. return tp_tube_db -- we read sucessfully
  42. end
  43. end
  44. tp_tube_db = {}
  45. return tp_tube_db
  46. end
  47. -- debug formatter for coordinates used below
  48. local fmt = function(pos)
  49. return pos.x..", "..pos.y..", "..pos.z
  50. end
  51. -- updates or adds a tube
  52. local function set_tube(pos, channel, can_receive)
  53. local tubes = tp_tube_db or read_tube_db()
  54. local hash = hash(pos)
  55. local tube = tubes[hash]
  56. if tube then
  57. tube.channel = channel
  58. tube.cr = can_receive
  59. save_tube_db()
  60. return
  61. end
  62. -- we haven't found any tp tube to update, so lets add it
  63. -- but sanity check that the hash has not already been inserted.
  64. -- if so, complain very loudly and refuse the update so the player knows something is amiss.
  65. -- to catch regressions of https://github.com/minetest-mods/pipeworks/issues/166
  66. local existing = tp_tube_db[hash]
  67. if existing ~= nil then
  68. local e = "error"
  69. minetest.log(e, "pipeworks teleport tube update refused due to position hash collision")
  70. minetest.log(e, "collided hash: "..hash)
  71. minetest.log(e, "tried-to-place tube: "..fmt(pos))
  72. minetest.log(e, "existing tube: "..fmt(existing))
  73. return
  74. end
  75. tp_tube_db[hash] = {x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=can_receive}
  76. save_tube_db()
  77. end
  78. local function remove_tube(pos)
  79. local tubes = tp_tube_db or read_tube_db()
  80. tubes[hash(pos)] = nil
  81. save_tube_db()
  82. end
  83. local function read_node_with_vm(pos)
  84. local vm = VoxelManip()
  85. local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
  86. local data = vm:get_data()
  87. local area = VoxelArea:new({MinEdge = MinEdge, MaxEdge = MaxEdge})
  88. return minetest.get_name_from_content_id(data[area:index(pos.x, pos.y, pos.z)])
  89. end
  90. local function get_receivers(pos, channel)
  91. local tubes = tp_tube_db or read_tube_db()
  92. local receivers = {}
  93. local dirty = false
  94. for key, val in pairs(tubes) do
  95. -- skip all non-receivers and the tube that it came from as early as possible, as this is called often
  96. if (val.cr == 1 and val.channel == channel and (val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z)) then
  97. local is_loaded = (minetest.get_node_or_nil(val) ~= nil)
  98. local node_name = is_loaded and minetest.get_node(pos).name or read_node_with_vm(val)
  99. if minetest.registered_nodes[node_name] and minetest.registered_nodes[node_name].is_teleport_tube then
  100. table.insert(receivers, val)
  101. else
  102. tp_tube_db[key] = nil
  103. dirty = true
  104. end
  105. end
  106. end
  107. if dirty then
  108. save_tube_db()
  109. end
  110. return receivers
  111. end
  112. local function update_meta(meta, can_receive)
  113. meta:set_int("can_receive", can_receive and 1 or 0)
  114. local cr_state = can_receive and "on" or "off"
  115. meta:set_string("formspec","size[8.6,2.2]"..
  116. "field[0.6,0.6;7,1;channel;"..S("Channel")..";${channel}]"..
  117. "label[7.3,0;"..S("Receive").."]"..
  118. "image_button[7.3,0.3;1,0.6;pipeworks_button_" .. cr_state .. ".png;cr" .. (can_receive and 0 or 1) .. ";;;false;pipeworks_button_interm.png]"..
  119. "image[0.3,1.3;1,1;pipeworks_teleport_tube_inv.png]"..
  120. "label[1.6,1.2;"..S("channels are public by default").."]" ..
  121. "label[1.6,1.5;"..S("use <player>:<channel> for fully private channels").."]" ..
  122. "label[1.6,1.8;"..S("use <player>\\;<channel> for private receivers").."]" ..
  123. default.gui_bg..
  124. default.gui_bg_img)
  125. end
  126. pipeworks.register_tube("pipeworks:teleport_tube", {
  127. description = S("Teleporting Pneumatic Tube Segment"),
  128. inventory_image = "pipeworks_teleport_tube_inv.png",
  129. noctr = { "pipeworks_teleport_tube_noctr.png" },
  130. plain = { "pipeworks_teleport_tube_plain.png" },
  131. ends = { "pipeworks_teleport_tube_end.png" },
  132. short = "pipeworks_teleport_tube_short.png",
  133. node_def = {
  134. is_teleport_tube = true,
  135. tube = {
  136. can_go = function(pos,node,velocity,stack)
  137. velocity.x = 0
  138. velocity.y = 0
  139. velocity.z = 0
  140. local channel = minetest.get_meta(pos):get_string("channel")
  141. if channel == "" then return {} end
  142. local target = get_receivers(pos, channel)
  143. if target[1] == nil then return {} end
  144. local d = math.random(1,#target)
  145. pos.x = target[d].x
  146. pos.y = target[d].y
  147. pos.z = target[d].z
  148. return pipeworks.meseadjlist
  149. end
  150. },
  151. on_construct = function(pos)
  152. local meta = minetest.get_meta(pos)
  153. update_meta(meta, true)
  154. meta:set_string("infotext", S("unconfigured Teleportation Tube"))
  155. end,
  156. on_receive_fields = function(pos,formname,fields,sender)
  157. if not fields.channel -- ignore escaping or clientside manipulation of the form
  158. or not pipeworks.may_configure(pos, sender) then
  159. return
  160. end
  161. local new_channel = tostring(fields.channel):trim()
  162. local meta = minetest.get_meta(pos)
  163. local can_receive = meta:get_int("can_receive")
  164. -- check for private channels each time before actually changing anything
  165. -- to not even allow switching between can_receive states of private channels
  166. if new_channel ~= "" then
  167. local sender_name = sender:get_player_name()
  168. local name, mode = new_channel:match("^([^:;]+)([:;])")
  169. if name and mode and name ~= sender_name then
  170. --channels starting with '[name]:' can only be used by the named player
  171. if mode == ":" then
  172. minetest.chat_send_player(sender_name, S("Sorry, channel '@1' is reserved for exclusive use by @2",
  173. new_channel, name))
  174. return
  175. --channels starting with '[name];' can be used by other players, but cannot be received from
  176. elseif mode == ";" and (fields.cr1 or (can_receive ~= 0 and not fields.cr0)) then
  177. minetest.chat_send_player(sender_name, S("Sorry, receiving from channel '@1' is reserved for @2",
  178. new_channel, name))
  179. return
  180. end
  181. end
  182. end
  183. local dirty = false
  184. -- was the channel changed?
  185. local channel = meta:get_string("channel")
  186. if new_channel ~= channel then
  187. channel = new_channel
  188. meta:set_string("channel", channel)
  189. dirty = true
  190. end
  191. -- test if a can_receive button was pressed
  192. if fields.cr0 and can_receive ~= 0 then
  193. can_receive = 0
  194. update_meta(meta, false)
  195. dirty = true
  196. elseif fields.cr1 and can_receive ~= 1 then
  197. can_receive = 1
  198. update_meta(meta, true)
  199. dirty = true
  200. end
  201. -- save if we changed something, handle the empty channel while we're at it
  202. if dirty then
  203. if channel ~= "" then
  204. set_tube(pos, channel, can_receive)
  205. local cr_description = (can_receive == 1) and "sending and receiving" or "sending"
  206. meta:set_string("infotext", S("Teleportation Tube @1 on '@2'", cr_description, channel))
  207. else
  208. -- remove empty channel tubes, to not have to search through them
  209. remove_tube(pos)
  210. meta:set_string("infotext", S("unconfigured Teleportation Tube"))
  211. end
  212. end
  213. end,
  214. on_destruct = function(pos)
  215. remove_tube(pos)
  216. end
  217. },
  218. })
  219. minetest.register_craft( {
  220. output = "pipeworks:teleport_tube_1 2",
  221. recipe = {
  222. { "basic_materials:plastic_sheet", "basic_materials:plastic_sheet", "basic_materials:plastic_sheet" },
  223. { "default:desert_stone", "default:mese", "default:desert_stone" },
  224. { "basic_materials:plastic_sheet", "basic_materials:plastic_sheet", "basic_materials:plastic_sheet" }
  225. },
  226. })
  227. if minetest.get_modpath("mesecons_mvps") ~= nil then
  228. mesecon.register_on_mvps_move(function(moved_nodes)
  229. for _, n in ipairs(moved_nodes) do
  230. if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then
  231. local meta = minetest.get_meta(n.pos)
  232. set_tube(n.pos, meta:get_string("channel"), meta:get_int("can_receive"))
  233. end
  234. end
  235. end)
  236. end
  237. -- Expose teleport tube database API for other mods
  238. pipeworks.tptube = {
  239. hash = hash,
  240. save_tube_db = save_tube_db,
  241. get_db = function() return tp_tube_db or read_tube_db() end,
  242. tp_tube_db_version = tp_tube_db_version
  243. }