Advertisement
Guest User

ComputerCraft turtle utility remote control

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