Advertisement
SirSheepe

STQ Server

May 15th, 2023 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.40 KB | None | 0 0
  1. local args = { ... }
  2. local argument = args[1]
  3.  
  4. local TurtleRegistry = {}
  5. TurtleRegistry.__index = TurtleRegistry
  6.  
  7. function TurtleRegistry.new()
  8. local self = setmetatable({}, TurtleRegistry)
  9.  
  10. self.register = {}
  11.  
  12. local registry = settings.get("registry") or {}
  13.  
  14. for id, entry in pairs(registry) do
  15. self.register[id] = {
  16. uuid = entry.uuid,
  17. channel = entry.channel,
  18. }
  19. end
  20.  
  21. return self
  22. end
  23.  
  24. -- https://gist.github.com/jrus/3197011
  25.  
  26. function TurtleRegistry:GenerateRegisterUUID()
  27. local template = "STIDxxxx-xxxx-xxxx"
  28. return string.gsub(template, "[x]", function(c)
  29. return string.format("%x", math.random(0, 0xf))
  30. end)
  31. end
  32.  
  33. function TurtleRegistry:Register(id)
  34. if self.register[id] then
  35. return false
  36. end
  37.  
  38. local uuid = self:GenerateRegisterUUID()
  39.  
  40. self.register[id] = {
  41. uuid = uuid,
  42. channel = (os.getComputerID() * id) % 65536,
  43. }
  44.  
  45. settings.set("registry", self.register)
  46. settings.save()
  47.  
  48. return uuid
  49. end
  50.  
  51. function TurtleRegistry:GetEntryByUUID(uuid)
  52. for id, entry in pairs(self.register) do
  53. if entry.uuid == uuid then
  54. return entry, id
  55. end
  56. end
  57. end
  58.  
  59. function TurtleRegistry:GetEntryByID(id)
  60. return self.register[id]
  61. end
  62.  
  63. function TurtleRegistry:GetEntryByChannel(channel)
  64. for id, entry in pairs(self.register) do
  65. if entry.channel == channel then
  66. return entry, id
  67. end
  68. end
  69. end
  70.  
  71. function TurtleRegistry:Delete(id)
  72. self.register[id] = nil
  73. settings.set("registry", self.register)
  74. settings.save()
  75. end
  76.  
  77. function TurtleRegistry:Export(id)
  78. if not self.register[id] then
  79. return ""
  80. end
  81.  
  82. local list = {}
  83.  
  84. for i, v in pairs(self.register[id]) do
  85. if type(v) == "string" then
  86. table.insert(list, i .. "='" .. v .. "'")
  87. else
  88. table.insert(list, i .. "=" .. v)
  89. end
  90. end
  91.  
  92. return "{" .. table.concat(list, ",") .. ",cid=" .. os.getComputerID() .. "}"
  93. end
  94.  
  95. local Network = {}
  96. Network.__index = Network
  97.  
  98. function Network.new(registry, modem)
  99. local self = setmetatable({}, Network)
  100.  
  101. if registry == nil then
  102. error("Unable to create network, no turtle registry provided", 2)
  103. end
  104.  
  105. if modem == nil then
  106. error("Unable to create network, no modem provided", 2)
  107. end
  108.  
  109. self.registry = registry
  110. self.DEFAULT_CHANNEL = 42424
  111. self.expectedPackets = {}
  112. self.outgoingPackets = {}
  113. self.connections = {}
  114. self.responseTime = 3
  115. self.modems = { modem }
  116.  
  117. return self
  118. end
  119.  
  120. function Network:Listen()
  121. while true do
  122. local event, id, channel, reply, message = os.pullEvent()
  123.  
  124. if argument == "debug" then
  125. print("l", event, id, channel, message)
  126. end
  127.  
  128. if event == "network_disconnect" then
  129. self.expectedPackets = {}
  130. self.outgoingPackets = {}
  131. break
  132. end
  133.  
  134. if event == "timer" or event == "modem_message" then
  135. local closed = {}
  136.  
  137. for i = 1, #self.expectedPackets do
  138. local packet = self.expectedPackets[i]
  139.  
  140. if event == "timer" and id == packet.timeout then
  141. table.remove(self.expectedPackets, i)
  142. i = i - 1
  143. end
  144.  
  145. if event == "modem_message" and channel == packet.channel and message == packet.message then
  146. table.remove(self.expectedPackets, i)
  147. table.insert(closed, channel)
  148. os.queueEvent(packet.uuid)
  149. os.cancelTimer(packet.timeout)
  150. i = i - 1
  151. end
  152. end
  153.  
  154. for i = 1, #self.connections do
  155. local connection = self.connections[i]
  156.  
  157. if event == "timer" and connection.timeoutID == id then
  158. table.remove(self.connections, i)
  159. os.queueEvent("connection_end", connection.targetUUID)
  160. self:CancelRepeatedPacket(connection.signalUUID)
  161. i = i - 1
  162. connection.status = 0
  163. if connection.timeoutListener then
  164. connection.timeoutListener(connection)
  165. end
  166. table.insert(closed, channel)
  167. elseif event == "modem_message" and channel == connection.channel then
  168. os.cancelTimer(connection.timeoutID)
  169. connection.timeoutID = os.startTimer(self.responseTime)
  170. if connection.status == 0 and message == connection.message then
  171. connection.status = 1
  172. elseif connection.status == 1 and message == connection.message then
  173. connection.status = 2
  174. connection.timeout = self.responseTime
  175. os.queueEvent("network_connect", connection.UUID)
  176. if connection.connectedListener then
  177. connection.connectedListener(connection)
  178. end
  179. elseif connection.status == 2 and message ~= connection.message then
  180. os.queueEvent("connection_update", connection.targetUUID, message)
  181. end
  182. end
  183. end
  184.  
  185. for i = 1, #closed do
  186. local required = false
  187.  
  188. for _, packet in pairs(self.expectedPackets) do
  189. local channel = closed[i]
  190.  
  191. if packet.channel == channel then
  192. required = true
  193. break
  194. end
  195. end
  196.  
  197. if not required then
  198. self:CloseChannel(channel)
  199. end
  200. end
  201.  
  202. if event == "timer" then
  203. for i, packet in pairs(self.outgoingPackets) do
  204. if packet.timerID == id then
  205. if self:Transmit(packet.channel, packet.message, packet.wireless) then
  206. local timer = os.startTimer(packet.period)
  207. packet.timerID = timer
  208.  
  209. if argument == "debug" then
  210. print("u", "send", packet.channel, packet.message, packet.wireless)
  211. end
  212. else
  213. table.remove(self.outgoingPackets, i)
  214. end
  215. break
  216. end
  217. end
  218. end
  219. end
  220. end
  221. end
  222.  
  223. -- https://gist.github.com/jrus/3197011
  224.  
  225. function Network:GenerateEventUUID()
  226. local template = "STNPxxxx-xxxx-xxxx"
  227. return string.gsub(template, "[x]", function(c)
  228. return string.format("%x", math.random(0, 0xf))
  229. end)
  230. end
  231.  
  232. function Network:Stop()
  233. os.queueEvent("network_disconnect")
  234. end
  235.  
  236. function Network:WaitForAll(timeout)
  237. local timerID = os.startTimer(timeout or 60)
  238.  
  239. while #self.expectedPackets > 0 do
  240. local event, id = os.pullEvent()
  241.  
  242. if event == "timer" and id == timerID then
  243. break
  244. end
  245. end
  246.  
  247. return #self.expectedPackets == 0
  248. end
  249.  
  250. function Network:WaitForBatch(batch, timeout)
  251. local events = {}
  252. local result = {}
  253.  
  254. for i, packet in pairs(batch) do
  255. table.insert(
  256. events,
  257. Network:Expect(packet.channel, packet.message, timeout, function(success)
  258. result[i] = success
  259. return packet.callback(success)
  260. end)
  261. )
  262. end
  263.  
  264. parallel.waitForAll(unpack(events))
  265.  
  266. return result
  267. end
  268.  
  269. function Network:Wait(channel, message, timeout, callback)
  270. return self:Expect(channel, message, timeout, callback)()
  271. end
  272.  
  273. function Network:Expect(channel, message, timeout, callback)
  274. local packetUUID = self:GenerateEventUUID()
  275. local timerID = timeout == false and -1 or os.startTimer(timeout or 30)
  276.  
  277. self:OpenChannel(channel)
  278.  
  279. table.insert(self.expectedPackets, {
  280. channel = channel,
  281. message = message,
  282. timeout = timerID,
  283. uuid = packetUUID,
  284. })
  285.  
  286. return function()
  287. while true do
  288. local event, id = os.pullEvent()
  289.  
  290. if event == "timer" and id == timerID then
  291. if callback then
  292. return callback(false)
  293. end
  294. return false
  295. end
  296.  
  297. if event == packetUUID then
  298. if callback then
  299. return callback(false)
  300. end
  301. return true
  302. end
  303. end
  304. end
  305. end
  306.  
  307. function Network:HasModem(otherModem)
  308. for i, modem in pairs(self.modems) do
  309. for channel = 1, 65535 do
  310. if not (modem.isOpen(channel) or otherModem.isOpen(channel)) then
  311. modem.open(channel)
  312.  
  313. local newOpen = otherModem.isOpen(channel)
  314. modem.close(channel)
  315.  
  316. return newOpen
  317. end
  318. end
  319. end
  320. return false
  321. end
  322.  
  323. function Network:AddModem(newModem)
  324. if self:HasModem(newModem) then
  325. return false
  326. end
  327.  
  328. table.insert(self.modems, newModem)
  329.  
  330. return true
  331. end
  332.  
  333. function Network:OpenChannel(channel, wireless)
  334. if wireless == nil then
  335. wireless = true
  336. end
  337.  
  338. for _, modem in pairs(self.modems) do
  339. if
  340. ((modem.isWireless() and wireless) or (not modem.isWireless() and not wireless)) and modem.isOpen(channel)
  341. then
  342. return true
  343. end
  344. end
  345.  
  346. for _, modem in pairs(self.modems) do
  347. if
  348. ((modem.isWireless() and wireless) or (not modem.isWireless() and not wireless))
  349. and pcall(modem.open, channel)
  350. then
  351. return true
  352. end
  353. end
  354.  
  355. error("Too many channels open, no modems available", 2)
  356. end
  357.  
  358. function Network:CloseChannel(channel)
  359. for _, modem in pairs(self.modems) do
  360. if modem.isOpen(channel) then
  361. modem.close(channel)
  362. end
  363. end
  364. end
  365.  
  366. function Network:Transmit(channel, message, wireless)
  367. if #self.modems == 0 then
  368. error("No modem available to transmit message", 2)
  369. end
  370.  
  371. if wireless == nil then
  372. wireless = true
  373. end
  374.  
  375. for _, modem in pairs(self.modems) do
  376. local isWireless = select(2, pcall(modem.isWireless))
  377. if (isWireless and wireless) or not (isWireless or wireless) then
  378. modem.transmit(channel, channel, message)
  379. return true
  380. end
  381. end
  382.  
  383. return false
  384. end
  385.  
  386. function Network:SendRepeatedPacket(channel, message, period, wireless)
  387. local uuid = self:GenerateEventUUID()
  388. local timerID = os.startTimer(period)
  389.  
  390. if self:Transmit(channel, message, wireless) then
  391. table.insert(self.outgoingPackets, {
  392. uuid = uuid,
  393. timerID = timerID,
  394. channel = channel,
  395. message = message,
  396. period = period,
  397. wireless = wireless,
  398. })
  399. else
  400. os.cancelTimer(timerID)
  401. return
  402. end
  403.  
  404. return uuid
  405. end
  406.  
  407. function Network:CancelRepeatedPacket(uuid)
  408. for i, packet in pairs(self.outgoingPackets) do
  409. if packet.uuid == uuid then
  410. os.cancelTimer(packet.timerID)
  411. table.remove(self.outgoingPackets, i)
  412. return true
  413. end
  414. end
  415. return false
  416. end
  417.  
  418. function Network:IsConnected(turtleUUID)
  419. for _, con in pairs(self.connections) do
  420. if con.targetUUID == turtleUUID and con.status == 2 then
  421. return true, con.UUID
  422. end
  423. end
  424. return false, nil
  425. end
  426.  
  427. function Network:ConnectToTurtle(turtleUUID, timeout, timeoutListener, connectedListener)
  428. local connected, uid = self:IsConnected(turtleUUID)
  429.  
  430. if connected then
  431. return uid
  432. end
  433.  
  434. local entry, id = self.registry:GetEntryByUUID(turtleUUID)
  435.  
  436. if entry == nil then
  437. error("Turtle UUID not registered to this system", 2)
  438. end
  439.  
  440. local message = self:Obfuscate(entry.uuid .. "-CONNECT", id)
  441. local timerID = os.startTimer(timeout or 300)
  442. local UUID = self:GenerateEventUUID()
  443.  
  444. self:OpenChannel(entry.channel, true)
  445.  
  446. table.insert(self.connections, {
  447. status = 0,
  448. timeoutID = timerID,
  449. timeout = timeout or 300,
  450. channel = entry.channel,
  451. message = message,
  452. signalUUID = self:SendRepeatedPacket(entry.channel, message, 1, true),
  453. targetUUID = entry.uuid,
  454. timeoutListener = timeoutListener,
  455. connectedListener = connectedListener,
  456. UUID = UUID,
  457. })
  458.  
  459. return function()
  460. while true do
  461. local event, id = os.pullEvent()
  462.  
  463. if event == "timer" and id == timerID then
  464. return nil
  465. end
  466.  
  467. if event == "network_connect" and id == UUID then
  468. return UUID
  469. end
  470. end
  471. end
  472. end
  473.  
  474. function Network:GetListenerHandle()
  475. return function()
  476. self:Listen()
  477. end
  478. end
  479.  
  480. function Network:Obfuscate(str, id, reverse)
  481. local lid = os.getComputerID()
  482. math.randomseed(lid >= id and (lid * lid + lid + id) or (lid + id * id))
  483.  
  484. if #str % 2 == 1 then
  485. str = str .. ";"
  486. end
  487.  
  488. local keys = {}
  489. local mul = reverse and 2 or 1
  490. local len = #str / (2 * mul)
  491.  
  492. for i = 1, 16 do
  493. local key = {}
  494. for i = 1, len do
  495. table.insert(key, math.random(0, 255))
  496. end
  497. keys[i] = key
  498. end
  499.  
  500. local function xor8(x, y)
  501. return (math.floor(x / 0x80) == math.floor(y / 0x80) and 0 or 0x80)
  502. + (math.floor(x / 0x40) % 2 == math.floor(y / 0x40) % 2 and 0x00 or 0x40)
  503. + (math.floor(x / 0x20) % 2 == math.floor(y / 0x20) % 2 and 0x00 or 0x20)
  504. + (math.floor(x / 0x10) % 2 == math.floor(y / 0x10) % 2 and 0x00 or 0x10)
  505. + (math.floor(x / 0x08) % 2 == math.floor(y / 0x08) % 2 and 0x00 or 0x08)
  506. + (math.floor(x / 0x04) % 2 == math.floor(y / 0x04) % 2 and 0x00 or 0x04)
  507. + (math.floor(x / 0x02) % 2 == math.floor(y / 0x02) % 2 and 0x00 or 0x02)
  508. + (x % 2 == y % 2 and 0x00 or 0x01)
  509. end
  510.  
  511. local function xor(x, y)
  512. local o = {}
  513. for i = 1, #x do
  514. table.insert(o, xor8(x[i], y[i]))
  515. end
  516. return o
  517. end
  518.  
  519. local L, R = {}, {}
  520.  
  521. for i = 1, #str, mul do
  522. local chunk = reverse and tonumber(str:sub(i, i + 1), 16) or str:byte(i)
  523. if i <= #str / 2 then
  524. table.insert(L, chunk)
  525. else
  526. table.insert(R, chunk)
  527. end
  528. end
  529.  
  530. if not reverse then
  531. for i = 1, #keys do
  532. L, R = R, xor(L, xor(keys[i], R))
  533. end
  534. else
  535. for i = #keys, 1, -1 do
  536. L, R = R, xor(L, xor(keys[i], R))
  537. end
  538. end
  539.  
  540. local out = ""
  541.  
  542. for i = 1, len do
  543. if reverse then
  544. out = out .. string.char(R[i])
  545. else
  546. out = out .. ("%02x"):format(R[i])
  547. end
  548. end
  549.  
  550. for i = 1, len do
  551. if reverse then
  552. if not (L[i] == 59 and i == len) then
  553. out = out .. string.char(L[i])
  554. end
  555. else
  556. out = out .. ("%02x"):format(L[i])
  557. end
  558. end
  559.  
  560. return out
  561. end
  562.  
  563. function Network:ReadData(s)
  564. --[[
  565. data format
  566. entries split by :
  567.  
  568. status=1:key=value:ins=1,3,2#scan|
  569. until the ins key, where we
  570. split by #
  571. ]]
  572. local info = {}
  573.  
  574. for chunk in s:gmatch("[^:]+") do
  575. local bits = {}
  576. for bit in chunk:gmatch("[^=]+") do
  577. table.insert(bits, bit)
  578. end
  579.  
  580. if #bits ~= 2 or not bits[1]:match("%a") then
  581. return
  582. end
  583.  
  584. local key = bits[1]
  585.  
  586. if key == "ins" then
  587. local ins = {}
  588.  
  589. for i in bits[2]:gmatch("[^/]+") do
  590. local dat = {}
  591. for k in i:gmatch("[^#]+") do
  592. table.insert(dat, k)
  593. end
  594.  
  595. if #dat ~= 2 or not dat[2]:match("%a") then
  596. return
  597. end
  598.  
  599. local coord = {}
  600. for dir in dat[1]:gmatch("[^,]+") do
  601. local n = tonumber(dir)
  602. if n == nil then
  603. return
  604. end
  605. table.insert(coord, n)
  606. end
  607.  
  608. if #coord ~= 3 then
  609. return
  610. end
  611.  
  612. table.insert(ins, {
  613. pos = coord,
  614. action = dat[2],
  615. })
  616. end
  617.  
  618. info.ins = ins
  619. else
  620. local anum = tonumber(bits[2])
  621. info[key] = anum ~= nil and anum or bits[2]
  622. end
  623. end
  624.  
  625. return info
  626. end
  627.  
  628. function Network:SendCommand(turtleUUID, data)
  629. if not self:IsConnected(turtleUUID) then
  630. return false
  631. end
  632.  
  633. local chunks = {}
  634.  
  635. for i, v in pairs(data) do
  636. if type(v) ~= "table" then
  637. table.insert(chunks, tostring(i) .. "=" .. tostring(v))
  638. else
  639. local ins = {}
  640. for k, n in pairs(v) do
  641. if n.direction == nil then
  642. table.insert(ins, table.concat(n.position, ",") .. ",~#" .. n.action)
  643. else
  644. local dstr = ""
  645. if n.direction[1] == 0 then
  646. if n.direction[3] == -1 then
  647. dstr = "N"
  648. else
  649. dstr = "S"
  650. end
  651. else
  652. if n.direction[1] == -1 then
  653. dstr = "W"
  654. else
  655. dstr = "E"
  656. end
  657. end
  658. table.insert(ins, table.concat(n.position, ",") .. "," .. dstr .. "#" .. n.action)
  659. end
  660. end
  661. table.insert(chunks, "ins=" .. table.concat(ins, "/"))
  662. end
  663. end
  664.  
  665. local entry, id = self.registry:GetEntryByUUID(turtleUUID)
  666. local msg = self:Obfuscate(table.concat(chunks, ":"), id)
  667.  
  668. self:Transmit(entry.channel, msg, true)
  669. end
  670.  
  671. function Network:ConnectionExpect(turtleUUID, callback)
  672. if not self:IsConnected(turtleUUID) then
  673. return
  674. end
  675.  
  676. local entry, id = self.registry:GetEntryByUUID(turtleUUID)
  677.  
  678. return function()
  679. while true do
  680. local event, uuid, message = os.pullEvent()
  681.  
  682. if event == "connection_end" and uuid == turtleUUID then
  683. callback(false)
  684. return false
  685. elseif event == "connection_update" and uuid == turtleUUID then
  686. local data = self:Obfuscate(message, id, true)
  687. local read = self:ReadData(data)
  688.  
  689. if read ~= nil then
  690. callback(true, read)
  691. return true, read
  692. end
  693. end
  694. end
  695. end
  696. end
  697.  
  698. function Network:ConnectionWait(turtleUUID, callback)
  699. return self:ConnectionExpect(turtleUUID, callback)()
  700. end
  701.  
  702. function Network:EndConnection(turtleUUID)
  703. for i, v in pairs(self.connections) do
  704. if v.targetUUID == turtleUUID then
  705. table.remove(self.connections, i)
  706. return true
  707. end
  708. end
  709. return false
  710. end
  711.  
  712. local modem = peripheral.find("modem", function(_, o)
  713. return o.isWireless()
  714. end)
  715. local registry = TurtleRegistry.new()
  716. local network = Network.new(registry, modem)
  717.  
  718. local function registrationSetup(process)
  719. local lan = peripheral.find("modem", function(_, o)
  720. return not o.isWireless()
  721. end)
  722.  
  723. if not lan then
  724. error("Please connect the computer to the registration network", 2)
  725. end
  726.  
  727. network:AddModem(lan)
  728.  
  729. local drive = peripheral.find("drive")
  730.  
  731. if not drive then
  732. error("Make sure a drive is connected to the network, did you right click it?", 2)
  733. end
  734.  
  735. if not drive.isDiskPresent() then
  736. error("Please place a floppy disk into the drive", 2)
  737. end
  738.  
  739. network:OpenChannel(network.DEFAULT_CHANNEL, false)
  740. print("Searching for a turtle...")
  741.  
  742. while true do
  743. local event, name = os.pullEvent()
  744.  
  745. if event == "peripheral" then
  746. local t = peripheral.getType(name)
  747.  
  748. if lan.isPresentRemote(name) then
  749. if t == "turtle" then
  750. process(lan, drive, name)
  751. end
  752. end
  753. elseif event == "peripheral_detach" then
  754. local t = peripheral.getType(name)
  755.  
  756. if t == "drive" then
  757. drive = peripheral.find("drive")
  758.  
  759. if not drive then
  760. error("Connection lost to the registration drive", 2)
  761. end
  762. elseif t == "modem" then
  763. lan = peripheral.find("modem", function(_, o)
  764. return not o.isWireless()
  765. end)
  766.  
  767. if not lan then
  768. error("Lost connection to the registration network", 2)
  769. end
  770. end
  771. end
  772. end
  773. end
  774.  
  775. local function registrationProcess()
  776. registrationSetup(function(lan, drive, name)
  777. local id = lan.callRemote(name, "getID")
  778.  
  779. if not registry:GetEntryByID(id) then
  780. print("Unregistered turtle found, attempting connection...")
  781.  
  782. local conUUID = network:SendRepeatedPacket(network.DEFAULT_CHANNEL, "ST-REG", 1, false)
  783. local success = network:Wait(network.DEFAULT_CHANNEL, "ST-REG-CONFIRM", 60)
  784. network:CancelRepeatedPacket(conUUID)
  785.  
  786. if not drive.isDiskPresent() then
  787. error("Floppy disk not present, unable to continue registration", 2)
  788. end
  789.  
  790. if success then
  791. print("Connection successful, transferring data...")
  792.  
  793. local turtleUUID = registry:Register(id)
  794. local entry = registry:GetEntryByUUID(turtleUUID)
  795.  
  796. local disk = fs.open("disk/reg", "w")
  797. disk.write(registry:Export(id))
  798. disk.close()
  799.  
  800. network:OpenChannel(entry.channel, false)
  801.  
  802. local regConfirmUUID = network:SendRepeatedPacket(network.DEFAULT_CHANNEL, "ST-REG-FINISH", 1, false)
  803. local success = network:Wait(entry.channel, "ST-REG-SUCCESS", 20)
  804. network:CancelRepeatedPacket(regConfirmUUID)
  805.  
  806. if success then
  807. print(("Turtle %s successfully registered"):format(turtleUUID))
  808. else
  809. registry:Delete(id)
  810. print("Unable to confirm successful registration, please try again")
  811. end
  812. else
  813. print("Failed to connect to the turtle, remove the turtle and try again")
  814. end
  815. else
  816. print("Turtle already registered with this network")
  817. end
  818.  
  819. print("Searching for a turtle...")
  820. end)
  821. end
  822.  
  823. local function deregistrationProcess()
  824. registrationSetup(function(lan, drive, name)
  825. local id = lan.callRemote(name, "getID")
  826. local entry = registry:GetEntryByID(id)
  827.  
  828. if entry then
  829. print("Registered turtle found, attempting connection...")
  830.  
  831. network:OpenChannel(entry.channel, false)
  832.  
  833. local conUUID = network:SendRepeatedPacket(entry.channel, "ST-DEREG", 1, false)
  834. local success = network:Wait(entry.channel, "ST-DEREG-CONFIRM", 60)
  835. network:CancelRepeatedPacket(conUUID)
  836.  
  837. if success then
  838. network:Transmit(entry.channel, "ST-DEREG-SUCCESS", false)
  839. print("Deregistration successful")
  840. registry:Delete(id)
  841. else
  842. print("Failed to connect to the turtle, remove the turtle and try again")
  843. end
  844. else
  845. print("Turtle is not registered with this network")
  846. end
  847.  
  848. print("Searching for a turtle...")
  849. end)
  850. end
  851.  
  852. parallel.waitForAll(network:GetListenerHandle(), function()
  853. if argument == "register" then
  854. registrationProcess()
  855. return
  856. elseif argument == "deregister" then
  857. deregistrationProcess()
  858. return
  859. end
  860.  
  861. if
  862. not (tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]))
  863. then
  864. error("Insufficient arguments, ensure the chunk size x and z, as well as the computer position is provided", 2)
  865. end
  866.  
  867. local cpos = { tonumber(args[3]), tonumber(args[4]), tonumber(args[5]) }
  868. local size = { tonumber(args[1]), tonumber(args[2]) }
  869.  
  870. local connected = {}
  871. local events = {}
  872. local total = 0
  873.  
  874. print("Attempting connection to turtles...")
  875.  
  876. for i, v in pairs(registry.register) do
  877. total = total + 1
  878. table.insert(
  879. events,
  880. network:ConnectToTurtle(v.uuid, 60, nil, function()
  881. table.insert(connected, v.uuid)
  882. end)
  883. )
  884. end
  885.  
  886. parallel.waitForAll(unpack(events))
  887.  
  888. print("Connected " .. #connected .. " / " .. total)
  889.  
  890. network.responseTime = 10
  891.  
  892. print("Locating turtles...")
  893.  
  894. local ready = 0
  895. local done = {}
  896. local locations = {}
  897.  
  898. for i, v in pairs(network.connections) do
  899. network:SendCommand(v.targetUUID, { locate = true, cx = cpos[1], cy = cpos[2], cz = cpos[3] })
  900. table.insert(
  901. done,
  902. network:ConnectionExpect(v.targetUUID, function(success, data)
  903. if success then
  904. ready = ready + 1
  905. for i, v in pairs(data) do
  906. print(i, v)
  907. end
  908. locations[v.targetUUID] = {
  909. position = { x = data.x, y = data.y, z = data.z },
  910. direction = { x = data.dx, y = data.dy, z = data.dz },
  911. }
  912. else
  913. network:EndConnection(v.targetUUID)
  914. end
  915. end)
  916. )
  917. end
  918.  
  919. parallel.waitForAll(unpack(done))
  920.  
  921. print("Starting process with " .. ready .. " / " .. total .. " turtles")
  922.  
  923. for i, v in pairs(locations) do
  924. print(i, v.position.x, v.position.y, v.position.z, v.direction.x, v.direction.y, v.direction.z)
  925. end
  926.  
  927. if ready == 0 then
  928. error("No turtles to do job, try again", 2)
  929. end
  930.  
  931. local pos = { { -73, 96, -25 }, { -56, 96, -25 } }
  932.  
  933. for i, v in pairs(network.connections) do
  934. network:SendCommand(
  935. v.targetUUID,
  936. { ins = { {
  937. action = "move",
  938. position = pos[i],
  939. } }, status = "start" }
  940. )
  941. end
  942.  
  943. -- local success = network:ConnectToTurtle(registry:GetEntryByID(13).uuid, 60, function()
  944. -- print("timed out sadge")
  945. -- end, function()
  946. -- print("connect wooo")
  947. -- end)()
  948.  
  949. -- if success then
  950. -- print("sending")
  951. -- network:SendCommand(registry:GetEntryByID(13).uuid, { fuel = true })
  952. -- print("waiting")
  953. -- network:ConnectionWait(registry:GetEntryByID(13).uuid, function(success, data)
  954. -- if success then
  955. -- print("returned data")
  956. -- print(data.fuel)
  957. -- end
  958. -- end)
  959. -- end
  960. -- if settings.get("")
  961. end)
  962.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement