Advertisement
DiamondQ2

OC_Gox

Sep 28th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local computer = require("computer")
  3. local robot = require("robot")
  4. local shell = require("shell")
  5. local term = require("term")
  6. local sides = require("sides")
  7.  
  8. local tArgs = { ... }
  9. local cldt = {}
  10. local mvfncs = {{robot.forward,robot.detect,robot.swing,robot.swing},
  11.         {robot.up,robot.detectUp,robot.swingUp,robot.swingUp},
  12.         {robot.down,robot.detectDown,robot.swingDown,robot.swingDown},}
  13. local function trply(mssgstr) -- i.e. "unbreakable block in path"
  14.     print(mssgstr)
  15.     if cldt.rcID then sleep(0.1) rednet.send(cldt.rcID,mssgstr) end
  16. end
  17. local function trply(mssgstr) -- i.e. "unbreakable block in path"
  18.     print(mssgstr)
  19.     if cldt.rcID then sleep(0.1) rednet.send(cldt.rcID,mssgstr) end
  20. end
  21. local function mvdgK(drctn) -- i.e.(1 = forward, 2 = up, 3 = down)
  22.     local itr8 = 0
  23.     while not mvfncs[drctn][1]() do
  24.         if mvfncs[drctn][2]() then
  25.             if not mvfncs[drctn][3]() then trply("unbreakable block in path") return false end
  26.         elseif computer.energy() == 0 then trply("Out of fuel" ) return false
  27.         else mvfncs[drctn][4]() end
  28.         if itr8 > 64 then trply("persistent impediment") return false end
  29.         itr8 = itr8+1
  30.     end
  31.     return true
  32. end
  33. rctfncs = {
  34. tnl = function() robot.swingUp() robot.swingDown() return mvdgK(1) end,
  35. dfd = function() return mvdgK(1) end,
  36. dup = function() return mvdgK(2) end,
  37. ddn = function() return mvdgK(3) end,
  38. bmb = function()
  39.     robot.placeDown()
  40.     redstone.setOutput("bottom",true)
  41.     sleep(0.5)
  42.     redstone.setOutput("bottom",false)
  43. end,
  44. pckup = function()
  45.     if not robot.suck() then
  46.         robot.suckUp()
  47.     else robot.suckDown() end
  48. end,
  49. slct = function()
  50.     cldt.slt = math.fmod(cldt.slt,16)+1
  51.     robot.select(cldt.slt)
  52.     trply("Slot "..cldt.slt.." selected, contains "..robot.count(cldt.slt)..", "..robot.space(cldt.slt).." from full")
  53. end,
  54. refuel = function()
  55.     trply("Current fuel level is "..computer.energy())
  56.     if not component.generator.insert(robot.count(cldt.slt)) then
  57.         trply("Slot "..cldt.slt.." does not contain fuel")
  58.     else trply("New fuel level is "..computer.energy()) end
  59. end,
  60. gox = function()
  61.     trply("send command string")
  62.     repeat cldt[2], cldt[3] = rednet.receive() until cldt[2] == cldt.rcID
  63.     tfnctns[6](cldt[3])
  64. end,
  65. gpslct = function() local x,y,z = gps.locate(2) trply("Robot ID "..os.computerID().." GPS = "..x..", "..y..", "..z) end,
  66. gpshst = function()
  67.     local rqstr,mssgrq = nil
  68.     local x,y,z = gps.locate(2)
  69.     repeat
  70.         rqstr,mssgrq = rednet.receive()
  71.         if mssgrq == "PING" then rednet.send(rqstr, textutils.serialize({x,y,z}))
  72.         elseif cldt.rcID == rqstr and mssgrq == "gpshst" then x = nil end
  73.     until not x
  74.     trply("Robot ID "..os.computerID().." GPS hosting terminated")
  75. end,
  76. rcvfl = function()
  77.     trply("send file name")
  78.     repeat cldt[2], cldt[3] = rednet.receive() until cldt[2] == cldt.rcID
  79.     local flnm = cldt[3]
  80.     trply("send file body")
  81.     repeat cldt[2], cldt[3] = rednet.receive() until cldt[2] == cldt.rcID
  82.     local flbdy = fs.open(flnm,"w") flbdy.write(cldt[3]) flbdy.close()
  83.     trply(flnm.." Updated")
  84. end,
  85. srvfl = function()
  86.     trply("send file path/name")
  87.     repeat cldt[2], cldt[3] = rednet.receive() until cldt[2] == cldt.rcID
  88.     local flbdy = fs.open(cldt[3],"r")
  89.     trply(flbdy.readAll()) flbdy.close()
  90. end,
  91. ldstrng = function()
  92.     trply("send lua code chunk")
  93.     repeat cldt[2], cldt[3] = rednet.receive() until cldt[2] == cldt.rcID
  94.     local tmpfnc,rrmssg = loadstring(cldt[3])
  95.     if tmpfnc then tmpfnc() else trply(rrmssg) end
  96. end,
  97. }
  98. cmdlst = {f=rctfncs.dfd,b=robot.back,u=rctfncs.dup,d=rctfncs.ddn,
  99.     l=robot.turnLeft,r=robot.turnRight,t= rctfncs.tnl,
  100.     p=robot.place,P=robot.placeUp,o=robot.placeDown,
  101.     x=robot.drop,X=robot.dropUp,z=robot.dropDown,
  102.     e=robot.swing,E=robot.swingUp,w=robot.swingDown,
  103.     v=rctfncs.pckup,V=robot.suckUp,c=robot.suckDown,
  104.     R=rctfncs.refuel,S=rctfncs.slct,B=rctfncs.bmb,
  105. }
  106. tfnctns = {
  107. function(psswrd,rtID)
  108.     cldt.rcID = rtID
  109.     rednet.open("right")
  110.     if psswrd then cldt[1] = psswrd cldt.slt = 1 robot.select(cldt.slt) else
  111.     print("Enter confirmation password")
  112.     cldt[1] = io.read() end
  113.     print("Send password from control terminal")
  114.     repeat cldt[2], cldt[3] = rednet.receive()
  115.         if not cldt.rcID and cldt[3] == cldt[1] then cldt.rcID = cldt[2] end
  116.     until cldt[2] == cldt.rcID and cldt[3] == cldt[1] trply("password accepted")
  117.     local hndl = fs.open("startup","w")
  118.     hndl.writeLine("shell.run(\"clfn\",1,\""..cldt[1].."\")") hndl.close()
  119.     while cldt.rcID do cldt[2],rnmssg = rednet.receive()
  120.         if cldt[2] == cldt.rcID then
  121.             _,_,cldt[3], cldt[4] = rnmssg:find("^(%a+)(.*)")
  122.             if robot[cldt[3]] then trply(tostring(robot[cldt[3]](tonumber(cldt[4]))))
  123.             elseif rctfncs[cldt[3]] then rctfncs[cldt[3]]()
  124.             elseif rnmssg == "trmn8RC" then cldt.rcID = nil end
  125.         end
  126.     end
  127.     print("Remote Control Terminated") fs.delete("startup")
  128. end,
  129. function()
  130.     cldt.slt = 16
  131.     robot.select(cldt.slt)
  132.     print("Place fuel in Slot 16 and enter")
  133.     io.read()
  134.     rctfncs.refuel()
  135. end,
  136. function()
  137.     print("Place material to be sifted in Slots 1-15\nLeave Slot 16 empty")
  138.     io.read()
  139.     repeat
  140.         robot.place()
  141.         if robot.count(cldt.slt) < 1 then rctfncs.slct() end
  142.         if not robot.swing() then print("Error") break end
  143.     until cldt.slt > 15
  144. end,
  145. function()
  146.     print("Place seeds in Slot 1\nBonemeal in Slots 2-15\nLeave Slot 16 empty")
  147.     io.read()
  148.     cldt.slt = 2
  149.     repeat
  150.         robot.select(1)
  151.         if not robot.place() then print("There must be tilled dirt to plant") break end
  152.         robot.select(cldt.slt)
  153.         if not robot.place() then print("Process requires Bonemeal to continue") break end
  154.         if robot.count(cldt.slt) < 1 then rctfncs.slct() end
  155.         robot.swing()
  156.     until cldt.slt > 15
  157. end,
  158. function()
  159.     local x1Ps,y1Ps,z1Ps,x2Ps,z2Ps = 0,255,0,1,0
  160.     local function gpsnode(xPos,yPos,zPos)
  161.         robot.select(2)
  162.         robot.place()
  163.         for i=4,7 do if robot.count(i) > 0 then robot.select(i) break end end
  164.         robot.drop()
  165.         strtfl = fs.open("disk/startup","w")
  166.         strtfl.writeLine("shell.run(\"gps\",\"host\","..xPos..","..yPos..","..zPos..")")
  167.         strtfl.close()
  168.         robot.up()
  169.         robot.forward()
  170.         robot.turnLeft()
  171.         robot.back()
  172.         robot.select(1)
  173.         robot.place()
  174.         robot.back()
  175.         robot.select(3)
  176.         robot.place()
  177.         robot.turnLeft()
  178.         robot.forward()
  179.         robot.turnRight()
  180.         robot.forward()
  181.         robot.forward()
  182.         robot.turnRight()
  183.         peripheral.call("front","turnOn")
  184.         robot.down()
  185.     end
  186.     local function plcaxis(xPos,yPos,zPos)
  187.         x1Ps,y1Ps,z1Ps = gps.locate(2,true)
  188.         robot.forward()
  189.         if x1Ps then --there is a GPS system, use it to determine coordinates and facing
  190.             x2Ps,yPos,z2Ps = gps.locate(2)
  191.             x2Ps,z2Ps = x2Ps-x1Ps,z2Ps-z1Ps
  192.         else x1Ps,y1Ps,z1Ps = xPos,yPos,zPos end
  193.         robot.down()
  194.         gpsnode(x1Ps+(x2Ps*2),y1Ps,z1Ps+(z2Ps*2))
  195.     -- Go back 2 and set the second node
  196.         robot.turnRight()
  197.         robot.turnRight()
  198.         for c=1,2 do robot.forward() end
  199.         gpsnode(x1Ps-(x2Ps*2),y1Ps,z1Ps-(z2Ps*2))
  200.     end
  201.     print([[Fill inventory as follows
  202. Slot 1 > 4+ Computers
  203. Slot 2 > 4+ Disk drives
  204. Slot 3 > 4+ Wireless Modems (4x)
  205. Slots 4-7 > Floppy disks
  206. Enter robot's current x]])
  207.     local x3Ps = tonumber(io.read())
  208.     print("Enter robot's current z")
  209.     local z3Ps = tonumber(io.read())
  210.     print("Enter robot's current facing")
  211.     for i=1, tonumber(io.read())+1 do robot.turnLeft() end
  212.     while computer.energy() < 500 do
  213.         print("Low Fuel Warning")
  214.         print("Place fuel in Slot 16 and press Enter")
  215.         io.read()
  216.         robot.select(16)
  217.         print("Current fuel level is"..computer.energy())
  218.         if not component.generator.insert(robot.count(16)) then
  219.             print("Slot "..cldt.slt.." does not contain fuel")
  220.         else print("New fuel level is"..computer.energy()) end
  221.     end
  222. -- launch robot
  223.     while robot.up() do end
  224.     plcaxis(x3Ps,255,z3Ps)
  225. -- Go back to the middle and turn
  226.     x2Ps,z2Ps = 0,1
  227.     robot.back()
  228.     robot.turnRight()
  229.     plcaxis(x3Ps,254,z3Ps)
  230.     while robot.down() do end
  231. end,
  232. function(cmndstrng) -- Go commands
  233. -- non-nil array length
  234. local function lngth(lst)
  235.     if lst then return #lst else return 0 end
  236. end
  237. local function gtNmbr(strg)
  238.     local _,_,nmbr,rmndr = strg:find("^(%d+)(.*)$")
  239.     return nmbr,lngth(strg)-lngth(rmndr)
  240. end
  241. local function gtLoop(strg)
  242.     local _,_,loop,rmndr = strg:find("^(%b[])(.*)$")
  243.     if loop then return loop:sub(2,-2),lngth(strg)-lngth(rmndr)
  244.     else return nil,0 end
  245. end
  246. local function gox(cmnds)
  247.     if not #cmnds then return false end
  248.     local ndx,prvCmnd,result = 1,""
  249.     while ndx <= #cmnds do
  250.         local char = cmnds:sub(ndx,ndx)
  251.         local nxtCh = cmnds:sub(ndx+1, ndx+1)
  252.         if cmdlst[char] then
  253.             ndx,result = ndx+1,cmdlst[char]()
  254.         else
  255.             local nmbr, nLen = gtNmbr(cmnds:sub(ndx))
  256.             local loop, lpLen = gtLoop(cmnds:sub(ndx))
  257.             if nmbr then
  258.                 for i = 2,tonumber(nmbr) do gox(prvCmnd) end
  259.                 ndx = ndx+nLen
  260.             elseif loop then
  261.                 local nmbr, nLen = gtNmbr(cmnds:sub(ndx+lpLen))
  262.                 if nmbr then
  263.                     for i = 1,tonumber(nmbr) do gox(loop) end
  264.                     ndx = ndx+nLen
  265.                 else while gox(loop) do end end
  266.                 ndx = ndx+lpLen
  267.         end end
  268.         prvCmnd = char
  269.     end
  270.     return result
  271. end
  272. if not cmndstrng then
  273.     print([[enter a string of commands/loops
  274. f b u d l r to move, t to tunnel
  275. p P o to place[U/Dn], x X z to drop
  276. e E w to dig [U/Dn], v V c to suck
  277. R to refuel, S to select next slot
  278. [...] loop while last command succeeds
  279. number for fixed loop]])
  280.     cmndstrng = io.read() end
  281.     gox(cmndstrng)
  282. end,
  283. }
  284. term.clear()
  285. cldt[1] = tonumber(tArgs[1])
  286. cldt.slt = 1
  287. robot.select(cldt.slt)
  288. if cldt[1] and tArgs[2] then tfnctns[cldt[1]](tArgs[2],tonumber(tArgs[3])) else
  289. repeat
  290.     cldt.slt = 1
  291.     robot.select(cldt.slt)
  292.     print("Select function by number")
  293.     print([[1: Remote Control
  294. 2: Refuel
  295. 3: Sift Gravel
  296. 4: Auto Farm
  297. 5: Launch GPS Array
  298. 6: GoX commands
  299. Or other to exit]])
  300.     sclfn = tfnctns[tonumber(io.read())]
  301.     if sclfn then sclfn() end
  302. until not sclfn
  303. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement