Advertisement
No_Zen

LinearTransmitter

Apr 21st, 2024 (edited)
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.28 KB | None | 0 0
  1. local modem = peripheral.wrap("back");
  2.  
  3. local ids = {1069, 1070, 1071, 1072, 1073, 1074}
  4.  
  5. local delay = 3.8
  6.  
  7. local addX = function()
  8.     modem.transmit(25810, 25810, textutils.serialize({id=ids[3], side="bottom", state=true}));
  9.     os.sleep(0.1);
  10.     modem.transmit(25810, 25810, textutils.serialize({id=ids[3], side="back", state=true}));
  11.     os.sleep(delay);
  12.     modem.transmit(25810, 25810, textutils.serialize({id=ids[3], side="back", state=false}));
  13.     os.sleep(0.1);
  14.     modem.transmit(25810, 25810, textutils.serialize({id=ids[3], side="bottom", state=false}));
  15.     os.sleep(0.1);
  16.  
  17.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=true}));
  18.     os.sleep(delay);
  19.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=false}));
  20.     os.sleep(0.1);
  21.  
  22.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=true}));
  23.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=true}));
  24.     os.sleep(0.1);
  25.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=false}));
  26.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=false}));
  27.     os.sleep(0.1);
  28.  
  29.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="bottom", state=true}));
  30.     os.sleep(0.1);
  31.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=true}));
  32.     os.sleep(delay);
  33.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=false}));
  34.     os.sleep(0.1);
  35.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="bottom", state=false}));
  36.     os.sleep(0.1);
  37.  
  38.  
  39.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=true}));
  40.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=true}));
  41.     os.sleep(0.1);
  42.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=false}));
  43.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=false}));
  44.     os.sleep(0.1);
  45. end
  46.  
  47. local subX = function()
  48.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=true}));
  49.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=true}));
  50.     os.sleep(0.1);
  51.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=false}));
  52.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=false}));
  53.     os.sleep(0.1);
  54.  
  55.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=true}));
  56.     os.sleep(delay);
  57.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=false}));
  58.     os.sleep(0.1);
  59.  
  60.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=true}));
  61.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=true}));
  62.     os.sleep(0.1);
  63.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="left", state=false}));
  64.     modem.transmit(25810, 25810, textutils.serialize({id=ids[2], side="right", state=false}));
  65.     os.sleep(0.1);
  66.  
  67.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="bottom", state=true}));
  68.     os.sleep(0.1);
  69.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=true}));
  70.     os.sleep(delay);
  71.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="back", state=false}));
  72.     os.sleep(0.1);
  73.     modem.transmit(25810, 25810, textutils.serialize({id=ids[1], side="bottom", state=false}));
  74.     os.sleep(0.1);
  75.  
  76.     modem.transmit(25810, 25810, textutils.serialize({id=ids[3], side="back", state=true}));
  77.     os.sleep(delay);
  78.     modem.transmit(25810, 25810, textutils.serialize({id=ids[3], side="back", state=false}));
  79.     os.sleep(0.1);
  80. end
  81.  
  82. local addZ = function()
  83.     modem.transmit(25810, 25810, textutils.serialize({id=ids[6], side="bottom", state=true}));
  84.     os.sleep(0.1);
  85.     modem.transmit(25810, 25810, textutils.serialize({id=ids[6], side="back", state=true}));
  86.     os.sleep(delay);
  87.     modem.transmit(25810, 25810, textutils.serialize({id=ids[6], side="back", state=false}));
  88.     os.sleep(0.1);
  89.     modem.transmit(25810, 25810, textutils.serialize({id=ids[6], side="bottom", state=false}));
  90.     os.sleep(0.1);
  91.  
  92.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=true}));
  93.     os.sleep(delay);
  94.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=false}));
  95.     os.sleep(0.1);
  96.  
  97.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=true}));
  98.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=true}));
  99.     os.sleep(0.1);
  100.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=false}));
  101.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=false}));
  102.     os.sleep(0.1);
  103.  
  104.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="bottom", state=true}));
  105.     os.sleep(0.1);
  106.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=true}));
  107.     os.sleep(delay);
  108.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=false}));
  109.     os.sleep(0.1);
  110.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="bottom", state=false}));
  111.     os.sleep(0.1);
  112.  
  113.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=true}));
  114.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=true}));
  115.     os.sleep(0.1);
  116.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=false}));
  117.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=false}));
  118.     os.sleep(0.1);
  119. end
  120.  
  121. local subZ = function()
  122.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=true}));
  123.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=true}));
  124.     os.sleep(0.1);
  125.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=false}));
  126.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=false}));
  127.     os.sleep(0.1);
  128.  
  129.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=true}));
  130.     os.sleep(delay);
  131.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=false}));
  132.     os.sleep(0.1);
  133.  
  134.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=true}));
  135.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=true}));
  136.     os.sleep(0.1);
  137.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="left", state=false}));
  138.     modem.transmit(25810, 25810, textutils.serialize({id=ids[5], side="right", state=false}));
  139.     os.sleep(0.1);
  140.  
  141.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="bottom", state=true}));
  142.     os.sleep(0.1);
  143.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=true}));
  144.     os.sleep(delay);
  145.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="back", state=false}));
  146.     os.sleep(0.1);
  147.     modem.transmit(25810, 25810, textutils.serialize({id=ids[4], side="bottom", state=false}));
  148.     os.sleep(0.1);
  149.  
  150.     modem.transmit(25810, 25810, textutils.serialize({id=ids[6], side="back", state=true}));
  151.     os.sleep(delay);
  152.     modem.transmit(25810, 25810, textutils.serialize({id=ids[6], side="back", state=false}));
  153.     os.sleep(0.1);
  154. end
  155.  
  156. local filename = "data.txt"
  157.  
  158. -- Function to initialize or reset data.txt with default values for current and target coordinates
  159. local function initializeFile()
  160.     local file = fs.open(filename, "w")
  161.     file.writeLine("0")  -- Current x
  162.     file.writeLine("0")  -- Current z
  163.     file.writeLine("0")  -- Target x
  164.     file.writeLine("0")  -- Target z
  165.     file.close()
  166. end
  167.  
  168. -- Read coordinates from data.txt
  169. local function readCoordinates()
  170.     if not fs.exists(filename) then
  171.         initializeFile()
  172.     end
  173.     local file = fs.open(filename, "r")
  174.     local currentX = tonumber(file.readLine())
  175.     local currentZ = tonumber(file.readLine())
  176.     local targetX = tonumber(file.readLine())
  177.     local targetZ = tonumber(file.readLine())
  178.     file.close()
  179.     return currentX, currentZ, targetX, targetZ
  180. end
  181.  
  182. -- Write coordinates to data.txt
  183. local function writeCoordinates(currentX, currentZ, targetX, targetZ)
  184.     local file = fs.open(filename, "w")
  185.     file.writeLine(tostring(currentX))
  186.     file.writeLine(tostring(currentZ))
  187.     file.writeLine(tostring(targetX))
  188.     file.writeLine(tostring(targetZ))
  189.     file.close()
  190. end
  191.  
  192. -- Movement functions that handle modem communication and update current coordinates
  193. local currentX, currentZ, targetX, targetZ = readCoordinates()
  194.  
  195. -- Main function to handle logic and control flow
  196. local function main()
  197.     -- Check if stepping is needed
  198.     if currentX ~= targetX or currentZ ~= targetZ then
  199.         print("C: "..currentX.." "..currentZ.." T: "..targetX.." "..targetZ)
  200.         if currentX < targetX then
  201.             addX()
  202.             currentX = currentX + 2
  203.         elseif currentX > targetX then
  204.             subX()
  205.             currentX = currentX - 2
  206.         elseif currentZ < targetZ then
  207.             addZ()
  208.             currentZ = currentZ + 2
  209.         elseif currentZ > targetZ then
  210.             subZ()
  211.             currentZ = currentZ - 2
  212.         end
  213.  
  214.         writeCoordinates(currentX, currentZ, targetX, targetZ)
  215.         return "continue"
  216.     else
  217.         -- Wait for new input when target coordinates are reached
  218.         term.clear()
  219.         term.setCursorPos(1, 1)
  220.         print("C: "..currentX.." "..currentZ.." T: "..targetX.." "..targetZ)
  221.         print("Enter new target:")
  222.         local input = io.read()
  223.         local args = {}
  224.         for arg in string.gmatch(input, "%S+") do
  225.             table.insert(args, arg)
  226.         end
  227.  
  228.         -- Helper function to round a number towards zero to the nearest even number
  229. local function roundToEven(n)
  230.     return n % 2 == 0 and n or (n > 0 and (n - 1) or (n + 1))
  231. end
  232.  
  233. -- Parsing and adjusting target coordinates
  234. if args[1] == "r" and #args == 3 then
  235.     -- Relative coordinates: adjust inputs to be even if necessary
  236.     local deltaX = tonumber(args[2])
  237.     local deltaZ = tonumber(args[3])
  238.     targetX = currentX + roundToEven(deltaX)
  239.     targetZ = currentZ + roundToEven(deltaZ)
  240. elseif #args == 2 then
  241.     -- Absolute coordinates: round to the nearest even number towards zero
  242.     targetX = roundToEven(tonumber(args[1]))
  243.     targetZ = roundToEven(tonumber(args[2]))
  244. else
  245.     print("Invalid command. Please use the format '123 456' for absolute or 'r 4 -2' for relative with even values only.")
  246.     return "continue"
  247. end
  248.  
  249.  
  250.         writeCoordinates(currentX, currentZ, targetX, targetZ)
  251.         return "continue"
  252.     end
  253. end
  254.  
  255. term.clear()
  256. term.setCursorPos(1, 1)
  257.  
  258. -- Main loop to execute the main function and handle its status
  259. while true do
  260.     local status = main()
  261.     if status == "continue" then
  262.         -- Continue executing
  263.     elseif status == "break" then
  264.         break
  265.     else
  266.         print("Unknown return status.")
  267.     end
  268. end
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement