Megaddd

Megaddd - robot quarry client - OpenComputers

Jun 6th, 2014
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.94 KB | None | 0 0
  1. local FILENAME = "qData.dat" --savefile name
  2.  
  3. local floorHeight = 0
  4. local homePos = {0,floorHeight,0} --robot initial placement offset in regards to everything else
  5. local dumpPos = {0,floorHeight,-5}
  6. local refuelPos = {0,floorHeight,5}
  7.  
  8. comp = require 'component'
  9. event = require 'event'
  10. term = require 'term'
  11. robot = require 'robot'
  12. computer = require 'computer'
  13. net = comp.proxy(comp.modem.address)
  14. port = 1337 --the port on your server
  15. str = 500 --500's generally enough to reach anything within player chunk loading radius
  16. home_address = nil --leave nil
  17.  
  18. patience = 11 --for how long to not bash the head of someone/something standing in fronth of the robot, in seconds.
  19.  
  20. _x = 0
  21. _y = floorHeight
  22. _z = 0
  23.  
  24. _facing = 3
  25. dir = {["west"] = 1, ["north"] = 2, ["east"] = 3, ["south"] = 4, ["up"] = 5, ["down"] = 6}
  26.  
  27. --[[
  28. function pw()
  29.     return math.floor(32768*2 - math.sin(math.ceil((os.time())*10))*32768)
  30. end
  31. --]]
  32.  
  33. function listen(timeout)
  34.     term.write("lsn call: ")
  35.     if timeout == nil then
  36.         local arg = {}
  37.         _, arg["address"], _, arg["port"], arg["distance"], _,
  38.         arg[1], arg[2], arg[3], arg[4] = event.pull("modem_message")
  39.         local a = arg[1] local b = arg[2] local c = arg[3] local d = arg[4]
  40.         if a == nil then a = "nil" end
  41.         if b == nil then b = "nil" end
  42.         if c == nil then c = "nil" end
  43.         if d == nil then d = "nil" end
  44.         term.write("returned_a "..a.." "..b.." "..c.." "..d.."\n")
  45.         return arg
  46.     else
  47.         local arg = {}
  48.         _, arg["address"], _, arg["port"], arg["distance"], _,
  49.         arg[1], arg[2], arg[3], arg[4] = event.pull(timeout,"modem_message")
  50.         local a = arg[1] local b = arg[2] local c = arg[3] local d = arg[4]
  51.         if a == nil then a = "nil" end
  52.         if b == nil then b = "nil" end
  53.         if c == nil then c = "nil" end
  54.         if d == nil then d = "nil" end
  55.         term.write("returned_a "..a.." "..b.." "..c.." "..d.."\n")
  56.         return arg
  57.     end
  58. end
  59.  
  60. function init()
  61.     net.open(port)
  62.     net.setStrength(str)
  63.     net.broadcast(port,"init")
  64.     local dissatisfied = true
  65.     while dissatisfied do
  66.         local lsn = listen()
  67.         print("init: lsn-1 = "..lsn[1])
  68.         if lsn[1] == "shake" then
  69.             term.write("shake received. \n")
  70.             home_address = lsn["address"]
  71.             dissatisfied = false
  72.         end
  73.     end
  74. end
  75.  
  76. function forward()
  77.     if robot.forward() then
  78.         if _facing == 1 then _x = _x - 1 end
  79.         if _facing == 2 then _z = _z - 1 end
  80.         if _facing == 3 then _x = _x + 1 end
  81.         if _facing == 4 then _z = _z + 1 end
  82.         --send(sr.serialize({_x,_y,_z,_facing}))
  83.         return true
  84.     else
  85.         return false
  86.     end
  87. end
  88.  
  89. function back()
  90.     if robot.back() then
  91.         if _facing == 1 then _x = _x + 1 end
  92.         if _facing == 2 then _z = _z + 1 end
  93.         if _facing == 3 then _x = _x - 1 end
  94.         if _facing == 4 then _z = _z - 1 end
  95.         --send(sr.serialize({_x,_y,_z,_facing}))
  96.         return true
  97.     else
  98.         return false
  99.     end
  100. end
  101.  
  102. function up()
  103.     if robot.up() then
  104.         _y = _y + 1
  105.         --send(sr.serialize({_x,_y,_z,_facing}))
  106.         return true
  107.     else
  108.         return false
  109.     end
  110. end
  111.  
  112. function down()
  113.     if robot.down() then
  114.         _y = _y - 1
  115.         --send(sr.serialize({_x,_y,_z,_facing}))
  116.         return true
  117.     else
  118.         return false
  119.     end
  120. end
  121.  
  122. function turnLeft()
  123.     _facing = _facing - 1
  124.     if _facing == 0 then _facing = 4 end
  125.     robot.turnLeft()
  126.     --send(sr.serialize({_x,_y,_z,_facing}))
  127. end
  128.  
  129. function turnRight()
  130.     _facing = _facing + 1
  131.     if _facing == 5 then _facing = 1 end
  132.     robot.turnRight()
  133.     --send(sr.serialize({_x,_y,_z,_facing}))
  134. end
  135.  
  136. function send(p1, p2, p3, p4, p5, p6, p7, p8, p9)
  137.     net.broadcast(port,p1, p2, p3, p4, p5, p6, p7, p8, p9)
  138. end
  139.  
  140. function faceWest()
  141.     if _facing == 1 then return true end
  142.     if _facing == 2 then turnLeft() return true end
  143.     if _facing == 3 then turnLeft() turnLeft() return true end
  144.     if _facing == 4 then turnRight() return true end
  145. end
  146.  
  147. function faceNorth()
  148.     if _facing == 1 then turnRight() return true end
  149.     if _facing == 2 then return true end
  150.     if _facing == 3 then turnLeft() return true end
  151.     if _facing == 4 then turnLeft() turnLeft() return true end
  152. end
  153.  
  154. function faceEast()
  155.     if _facing == 1 then turnRight() turnRight() return true end
  156.     if _facing == 2 then turnRight() return true end
  157.     if _facing == 3 then return true end
  158.     if _facing == 4 then turnLeft() end
  159. end
  160.  
  161. function faceSouth()
  162.     if _facing == 1 then turnLeft() return true end
  163.     if _facing == 2 then turnLeft() turnLeft() return true end
  164.     if _facing == 3 then turnRight() return true end
  165.     if _facing == 4 then return true end
  166. end
  167.  
  168. function faceSet(n)
  169.     if n == 1 then faceWest() end
  170.     if n == 2 then faceNorth() end
  171.     if n == 3 then faceEast() end
  172.     if n == 4 then faceSouth() end
  173.     if n == 5 or n == 6 then return true end
  174.     return true
  175. end
  176.  
  177. function getPos()
  178.     return _x, _y, _z
  179. end
  180.  
  181. function dodge(obstacleDir,maxtries)
  182.     print("dodging "..obstacleDir.."!\n")
  183.     local dpm = {0,0,0} --Total displacement after dodge {+East/-West, +Up/-Down, +South/-North}
  184.     local initFacing = _facing
  185.     if tonumber(maxtries) == nil then maxtries = 64 end
  186.     if obstacleDir == "up" then
  187.         local tries = 0
  188.         while robot.detectUp() do
  189.             local rand = math.random(4)
  190.             if rand % 2 == 1 then
  191.                 faceEast()
  192.                 if rand < 3 then
  193.                     if forward(1) then dpm[1] = dpm[1] + 1 end
  194.                 else
  195.                     if back(1) then dpm[1] = dpm[1] - 1 end
  196.                 end
  197.                 faceSet(initFacing)
  198.             else
  199.                 faceSouth()
  200.                 if rand < 3 then
  201.                     if forward(1) then dpm[3] = dpm[3] + 1 end
  202.                 else
  203.                     if back(1) then dpm[3] = dpm[3] - 1 end
  204.                 end
  205.                 faceSet(initFacing)
  206.             end
  207.             tries = tries + 1
  208.             if tries >= maxtries then return false end
  209.         end
  210.     end
  211.     if obstacleDir == "down" then
  212.         local tries = 0
  213.         while robot.detectDown() do
  214.             local rand = math.random(4)
  215.             if rand % 2 == 1 then
  216.                 faceEast()
  217.                 if rand < 3 then
  218.                     if forward(1) then dpm[1] = dpm[1] + 1 end
  219.                 else
  220.                     if back(1) then dpm[1] = dpm[1] - 1 end
  221.                 end
  222.                 faceSet(initFacing)
  223.             else
  224.                 faceSouth()
  225.                 if rand < 3 then
  226.                     if forward(1) then dpm[3] = dpm[3] + 1 end
  227.                 else
  228.                     if back(1) then dpm[3] = dpm[3] - 1 end
  229.                 end
  230.                 faceSet(initFacing)
  231.             end
  232.             tries = tries + 1
  233.             if tries >= maxtries then return false end
  234.         end
  235.     end
  236.     if obstacleDir == "south" or obstacleDir == "north" then
  237.         local tries = 0
  238.         while robot.detect() do
  239.             local rand = math.random(4)
  240.             if rand % 2 == 1 then
  241.                 faceEast()
  242.                 if rand < 3 then
  243.                     if forward(1) then dpm[1] = dpm[1] + 1 end
  244.                 else
  245.                     if back(1) then dpm[1] = dpm[1] - 1 end
  246.                 end
  247.                 faceSet(initFacing)
  248.             else
  249.                 if rand < 3 then
  250.                     if up(1) then dpm[2] = dpm[2] + 1 end
  251.                 else
  252.                     if down(1) then dpm[2] = dpm[2] - 1 end
  253.                 end
  254.             end
  255.             tries = tries + 1
  256.             if tries >= maxtries then return false end
  257.         end
  258.     end
  259.     if obstacleDir == "east" or obstacleDir == "west" then
  260.         local tries = 0
  261.         while robot.detect() do
  262.             local rand = math.random(4)
  263.             if rand % 2 == 1 then
  264.                 faceSouth()
  265.                 if rand < 3 then
  266.                     if forward(1) then dpm[3] = dpm[3] + 1 end
  267.                 else
  268.                     if back(1) then dpm[3] = dpm[3] - 1 end
  269.                 end
  270.                 faceSet(initFacing)
  271.             else
  272.                 if rand < 3 then
  273.                     if up(1) then dpm[2] = dpm[2] + 1 end
  274.                 else
  275.                     if down(1) then dpm[2] = dpm[2] - 1 end
  276.                 end
  277.             end
  278.             tries = tries + 1
  279.             if tries >= maxtries then return false end
  280.         end
  281.     end
  282.     --{+East/-West, +Up/-Down, +South/-North}
  283.     if obstacleDir == "up" then
  284.         if up() then dpm[2] = dpm[2] + 1 end
  285.         if up() then dpm[2] = dpm[2] + 1 end
  286.     end
  287.     if obstacleDir == "down" then
  288.         if down() then dpm[2] = dpm[2] - 1 end
  289.         if down() then dpm[2] = dpm[2] - 1 end
  290.     end
  291.     if obstacleDir == "south" then
  292.         faceSouth()
  293.         if forward() then dpm[3] = dpm[3] + 1 end
  294.         if forward() then dpm[3] = dpm[3] + 1 end
  295.     end
  296.     if obstacleDir == "north" then
  297.         faceNorth()
  298.         if forward() then dpm[3] = dpm[3] - 1 end
  299.         if forward() then dpm[3] = dpm[3] - 1 end
  300.     end
  301.     if obstacleDir == "east" then
  302.         faceEast()
  303.         if forward() then dpm[1] = dpm[1] + 1 end
  304.         if forward() then dpm[1] = dpm[1] + 1 end
  305.     end
  306.     if obstacleDir == "west" then
  307.         faceWest()
  308.         if forward() then dpm[1] = dpm[1] - 1 end
  309.         if forward() then dpm[1] = dpm[1] - 1 end
  310.     end
  311.     return dpm[1],dpm[2],dpm[3]
  312. end
  313.  
  314. function dist(x1, y1, z1, x2, y2, z2)
  315.     return math.sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)
  316. end
  317.  
  318. function goToPos(a, b, c, mode, stopdist)
  319.     if stopdist == nil then stopdist = 0 end
  320.     print("goToPos("..a..","..b..","..c..")\n")
  321.     if tonumber(stopdist) == nil then stopdist = 0 end
  322.     local initFacing = _facing
  323.     while a ~= _x or b ~= _y or c ~= _z do
  324.         while a ~= _x do
  325.             if a>=_x then
  326.                 if a ~= _x then
  327.                     faceEast()
  328.                     while mode and robot.detect() do robot.swing() os.sleep(0.2) end
  329.                     if not forward(1) then
  330.                         if dist(_x,_y,_z,a,b,c) > stopdist then
  331.                             local a1, b1, c1 = dodge("east")
  332.                             --a = a + a1
  333.                             --b = b + b1
  334.                             --c = c + c1
  335.                         else
  336.                             return "stopdist"
  337.                         end
  338.                     end
  339.                 end
  340.             else
  341.                 faceWest()
  342.                 while mode and robot.detect() do robot.swing() os.sleep(0.2) end
  343.                 if not forward(1) then
  344.                     if dist(_x,_y,_z,a,b,c) > stopdist then
  345.                         local a1, b1, c1 = dodge("west")
  346.                         --a = a + a1
  347.                         --b = b + b1
  348.                         --c = c + c1
  349.                     else
  350.                         return "stopdist"
  351.                     end
  352.                 end
  353.             end
  354.         end
  355.         while c ~= _z do
  356.             if c>=_z then
  357.                 if c ~= _z then
  358.                     faceSouth()
  359.                     while mode and robot.detect() do robot.swing() os.sleep(0.2) end
  360.                     if not forward(1) then
  361.                         if dist(_x,_y,_z,a,b,c) > stopdist then
  362.                             local a1, b1, c1 = dodge("south")
  363.                             --a = a + a1
  364.                             --b = b + b1
  365.                             --c = c + c1
  366.                         else
  367.                             return "stopdist"
  368.                         end
  369.                     end
  370.                 end
  371.             else
  372.                 faceNorth()
  373.                 while mode and robot.detect() do robot.swing() os.sleep(0.2) end
  374.                 if not forward(1) then
  375.                     if dist(_x,_y,_z,a,b,c) > stopdist then
  376.                         local a1, b1, c1 = dodge("north")
  377.                         --a = a + a1
  378.                         --b = b + b1
  379.                         --c = c + c1
  380.                     else
  381.                         return "stopdist"
  382.                     end
  383.                 end
  384.             end
  385.         end
  386.         while b ~= _y do
  387.             if b>=_y then
  388.                 if b ~= _y then
  389.                     while mode and robot.detectUp() do turtle.swingUp() os.sleep(0.2) end
  390.                     if not up() then
  391.                         local a1, b1, c1 = dodge("up")
  392.                         --a = a + a1
  393.                         --b = b + b1
  394.                         --c = c + c1
  395.                     end
  396.                 end
  397.             else
  398.                 while mode and robot.detectDown() do robot.swingDown() os.sleep(0.2) end
  399.                 if not down() then
  400.                     local a1, b1, c1 = dodge("down")
  401.                     --a = a + a1
  402.                     --b = b + b1
  403.                     --c = c + c1
  404.                 end
  405.             end
  406.         end
  407.         faceSet(initFacing)
  408.     end
  409.     return true
  410. end
  411.  
  412. local function printError(cIn)
  413.     print("  Err: "..cIn)
  414. end
  415.  
  416. local function mineForward(xLen)
  417.     for i=1,xLen do
  418.         while robot.detect() do
  419.             robot.swing()
  420.         end
  421.         robot.swingUp()
  422.         robot.swingDown()
  423.         forward()
  424.     end
  425.     return true
  426. end
  427.  
  428.  
  429. function refuel(n)
  430.     gen = comp.proxy(comp.generator.address)
  431.     if gen ~= nil then
  432.         local sel = robot.select()
  433.         robot.select(16)
  434.         gen.insert(n)
  435.         robot.select(sel)
  436.         return true
  437.     else
  438.         return false
  439.     end
  440. end
  441.  
  442. function getBlockDir(x,y,z) --Gets the major cardinal direction towards Pos. (West/North/Up..)
  443.     if tonumber(x) and tonumber(y) and tonumber(z) then else return false end
  444.     x = x - _x
  445.     y = y - _y
  446.     z = z - _z
  447.     local m = math.max(math.abs(x),math.abs(y),math.abs(z))
  448.     if m == math.abs(x) and m ~= 0 then
  449.         if x>0 then return "east" else return "west" end
  450.     else
  451.         if m == math.abs(z) and m ~= 0 then
  452.             if z>0 then return "south" else return "north" end
  453.         else
  454.             if m == math.abs(y) and m ~= 0 then
  455.                 if y>0 then return "up" else return "down" end
  456.             end
  457.         end
  458.     end
  459.     return false,"YouJustBrokeTheGame"
  460. end
  461.  
  462. function facePos(x,y,z)
  463.     faceSet(dir[getBlockDir(x,y,z)])
  464. end
  465.    
  466. function dumpToPos(x,y,z)
  467.     while goToPos(x,y,z,false,4) == "stopdist" do
  468.         if dist(x,y,z,_x,_y,_z) == 1 then break end
  469.         os.sleep(2)
  470.     end
  471.     local blockDir = getBlockDir(x,y,z)
  472.     local sel = robot.select()
  473.     if blockDir ~= "up" and blockDir ~= "down" then
  474.         facePos(x,y,z)
  475.         for i=1,15 do robot.select(i) robot.drop() end
  476.     else
  477.         if blockDir == "up" then
  478.             for i=1,15 do robot.select(i) robot.dropUp() end
  479.         end
  480.         if blockDir == "down" then
  481.             for i=1,15 do robot.select(i) robot.dropDown() end
  482.         end
  483.     end
  484.     robot.select(sel)
  485. end
  486.  
  487. function recharge(x,y,z)
  488.     while goToPos(x,y,z,false,4) == "stopdist" do
  489.         if dist(x,y,z,_x,_y,_z) == 1 then break end
  490.         os.sleep(2)
  491.     end
  492.     local uncharged = true
  493.     while uncharged do
  494.         local eLeft = computer.maxEnergy() - computer.energy()
  495.         if eLeft < 10 then uncharged = false
  496.         else os.sleep(1) end
  497.     end
  498. end
  499.  
  500. function resupply()
  501.     local x, y, z = getPos()
  502.     local cFacing = _facing
  503.     goToPos(x,floorHeight+1,z,false,0)
  504.     dumpToPos(dumpPos[1],dumpPos[2],dumpPos[3])
  505.     recharge(refuelPos[1],refuelPos[2],refuelPos[3])
  506.     goToPos(x,floorHeight+1,z,false,0)
  507.     goToPos(x,y,z,false,0)
  508.     faceSet(cFacing)
  509. end
  510.  
  511. function override(minfuel)
  512.     if computer.energy() < minfuel then
  513.         refuel()
  514.     end
  515.     if robot.count(15) ~= 0 then
  516.         resupply()
  517.     end
  518. end
  519.    
  520. local function turn(i,invert)
  521.     local test
  522.     if invert == true then test = "true" else test = "false" end
  523.     print("turn("..i..","..test..")")
  524.     if invert == nil then invert = false end
  525.     if invert == false then
  526.         print("turn: invert - false")
  527.         if i % 2 == 1 then
  528.             turnRight()
  529.         else
  530.             turnLeft()
  531.         end
  532.     else
  533.         print("turn: invert - true")
  534.         if i % 2 == 1 then
  535.             turnLeft()
  536.         else
  537.             turnRight()
  538.         end
  539.     end
  540. end
  541.  
  542. function minePlane(xLen, yLen, pinvert) --Brace yourself for lots of redundancy, re-using updated old code. "Dis gon' get ugly."
  543.     robot.swingDown()
  544.     for i=0,yLen-1 do
  545.         local xInvert
  546.         local notxInvert
  547.         if pinvert == true then
  548.             xInvert = true
  549.             notxInvert = false
  550.         else
  551.             xInvert = false
  552.             notxInvert = true
  553.         end
  554.         if xLen % 2 == 1 and yLen % 2 == 1 then
  555.             xInvert = false
  556.             notxInvert = true
  557.         end
  558.         turn(i,xInvert)
  559.         mineForward(xLen-1)
  560.         turn(i,notxInvert)
  561.         if i ~= yLen - 1 then
  562.             mineForward(1)
  563.         end
  564.     end
  565. end
  566.  
  567. function mineQuarry(xLen, yLen, zLen, invert)
  568.     local zrem = 0
  569.     while robot.detectDown() == false do
  570.         down()
  571.         zrem = zrem + 1
  572.     end
  573.     zLen = zLen - zrem
  574.     zLenTrunc = math.floor(zLen/3)
  575.     robot.swingDown()
  576.     down()
  577.     robot.swingDown()
  578.     down()
  579.     local xMirror = invert
  580.     local xInvert
  581.     for i=0,zLenTrunc-1 do
  582.         if xMirror then
  583.             if i % 2 == 1 then
  584.                 xInvert = true
  585.             else
  586.                 xInvert = false
  587.             end
  588.         else
  589.             if i % 2 == 1 then
  590.                 xInvert = false
  591.             else
  592.                 xInvert = true
  593.             end
  594.         end
  595.         minePlane(xLen, yLen, xInvert)
  596.         if i < zLenTrunc-1 then
  597.             robot.swingDown()
  598.             down()
  599.             robot.swingDown()
  600.             down()
  601.             robot.swingDown()
  602.             down()
  603.             turnRight()
  604.             turnRight()
  605.         end
  606.     end
  607. end
  608.  
  609. function deploy(x,y,z,dir,xq,yq,zq) --Notice: (1,*,*) = forward, (*,*,-1) = right
  610.     if tonumber(x) ~= nil and tonumber(y) ~= nil and tonumber(z) ~= nil and dir ~= nil and tonumber(xq) ~= nil and tonumber(yq) ~= nil and tonumber(zq) ~= nil
  611.     then
  612.         local xc, yc, zc = getPos()
  613.         goToPos(x,y,z,false,0)
  614.         if dir == "left" then faceWest() else faceEast() end
  615.         mineQuarry(xq, yq, zq, false, minfuel)
  616.         local xc2, yc2, zc2 = getPos()
  617.         goToPos(xc2,floorHeight+1,zc2,false,0)
  618.         dumpToPos(dumpPos[1],dumpPos[2],dumpPos[3])
  619.         recharge(refuelPos[1],refuelPos[2],refuelPos[3])
  620.         send("done")
  621.     else
  622.         printError("deploy: missing/incorrect vars.")
  623.     end
  624. end
  625.  
  626.  
  627. sr = require 'serialization'
  628. function main()
  629.     init()
  630.     while true do
  631.         if net.isOpen(port) == false then net.open(port) end
  632.         lsn = listen()
  633.         print("from "..lsn["address"]..":")
  634.         print("  "..lsn[1])
  635.         if lsn["address"] == home_address then --deploy(x,y,z,dir,xq,yq,zq)
  636.             if lsn[1] == "deploy" then
  637.                 send("deployed")
  638.                 local aTable = sr.unserialize(lsn[2])
  639.                 if lsn[6] == "true" then bool = true else bool = false end
  640.                 local x = tonumber(aTable[1])
  641.                 local y = tonumber(aTable[2])
  642.                 local z = tonumber(aTable[3])
  643.                 local xq = tonumber(aTable[5])
  644.                 local yq = tonumber(aTable[6])
  645.                 local zq = tonumber(aTable[7])
  646.                 print("Deploying to: ("..x..","..y..","..z..")")
  647.                 deploy(x,y,z,aTable[4],xq,yq,zq)
  648.             end
  649.             if lsn[1] == "park" then
  650.                 send("parking")
  651.                 goToPos(x,y,z,false,0)
  652.                 faceEast()
  653.             end
  654.         end
  655.     end
  656. end
  657.  
  658. main()
Advertisement
Add Comment
Please, Sign In to add comment