Advertisement
Guest User

clfn update 11/18/12

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