Advertisement
Wojbie

Swarminer Main

Oct 13th, 2013
3,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 47.45 KB | None | 0 0
  1. --Wojbie's Swarm Miner Main Program
  2. --Controls Turtle and Handles all mining related parts of programing
  3. --This is part of set of programs
  4. --To get rest of them make instaler Floppy Disk by placing empty Floppy Disk in disk drive and using pastebin get LXLBZK25 disk/startup
  5.  
  6. local tArgs = {...}
  7.  
  8. --Standalone mode (Warning Unstable Can get funky on crashes and reboots)
  9.  
  10. local usegps=true
  11.  
  12. --Minimal level trutle can reach (default 5 - at 4 starts bedrock) - in case of flat bedrock set to 2 - added on HeffeD Request
  13.  
  14. local levelmin=5
  15.  
  16. --connecting to modem
  17. local channel=15151
  18. local modem=false
  19. if peripheral.isPresent("right") then
  20.     if peripheral.getType("right")=="modem" then
  21.         modem=peripheral.wrap("right")
  22.             if modem["isWireless"] then
  23.                 if modem.isWireless() then
  24.                     modem.open(channel)
  25.                 else
  26.                     modem=false
  27.                 end
  28.             else
  29.                 modem.open(channel)
  30.             end
  31.     end
  32. end
  33.  
  34.  
  35. --Creating tables for later use
  36.  
  37. local pos={}
  38. local settings={}
  39. local order={}
  40. local current={}
  41. local currents={} -- other brothers currents.
  42.  
  43. --Flags - Dont touch
  44.  
  45. local working=false  --if done any work in this loop.
  46. local movelock=false  --if moving (locks system stopping)
  47. local reboot=false --Ctrl+r/s detection
  48. local rebootimer --2 sec timer canceling ctrl+r/s
  49. local maplock=false  --map useage lock
  50. local run=true  --mining or waiting
  51. local terminate=false  --endig program
  52. local drawmap=true  --redrawing map
  53. local maintencegoto=false --if going to manual maintence point
  54. local status="Running"  --status info
  55. local monactive=false --if monitor is active
  56.  
  57. --debug Flag - You can change this one
  58.  
  59. local ldebug=false
  60.  
  61. --constans
  62.  
  63. local function Rev(A)  --reverses table function - keys for values and vice versa
  64.     local Revt={}
  65.     for i,j in pairs(A) do
  66.         Revt[j]=i
  67.     end
  68.     return Revt
  69. end
  70.  
  71. local function stamp(A)
  72. local logo
  73. if turtle then
  74. logo="__          __     _  _      _       \n\\ \\        / /    (_)| |    (_)      \n \\ \\  /\\  / /___   _ | |__   _   ___ \n  \\ \\/  \\/ // _ \\ | || '_ \\ | | / _ \\\n   \\  /\\  /| (_) || || |_) || ||  __/\n    \\/  \\/  \\___/ | ||_.__/ |_| \\___|\n                 _/ |                \n                |__/                 "
  75. else
  76. logo=" __      __                __                      \n/\\ \\  __/\\ \\           __ /\\ \\       __            \n\\ \\ \\/\\ \\ \\ \\    ___  /\\_\\\\ \\ \\____ /\\_\\      __   \n \\ \\ \\ \\ \\ \\ \\  / __'\\\\/\\ \\\\ \\ '__'\\\\/\\ \\   /'__'\\ \n  \\ \\ \\_/ \\_\\ \\/\\ \\L\\ \\\\ \\ \\\\ \\ \\L\\ \\\\ \\ \\ /\\  __/ \n   \\ '\\___x___/\\ \\____/_\\ \\ \\\\ \\_,__/ \\ \\_\\\\ \\____\\\n    '\\/__//__/  \\/___//\\ \\_\\ \\\\/___/   \\/_/ \\/____/\n                      \\ \\____/                     \n                       \\/___/                      "
  77. end
  78.     term.setCursorPos(1,A)
  79.     print(logo)
  80. end
  81.  
  82. --predefining functions that need it (one for now)
  83.  
  84. local maintence  --maintence function predefinition
  85. local fullcheack --fullcheack function predefinition
  86.  
  87. -- open peripherals chest definitions - left space for additional definitions
  88. -- one of chest definitions is for older version - other is for new one.
  89.  
  90. local chests={
  91. container_chest=true,
  92. chest=true,
  93. --drive=true, --for testing
  94. }
  95.  
  96. local mapmark={".","+","#"}
  97. local krampam=Rev(mapmark)
  98.  
  99. ----Utility Ones
  100.  
  101. -- File Useage
  102.  
  103. local function save(A,B) local file = fs.open(tostring(A),"w") file.write(B) file.close() end
  104. local function saveT(A,B) save(A,textutils.serialize(B)) end
  105. local function saveTH(A,B) save(A,string.gsub(textutils.serialize(B),"},","},\r\n")) end
  106. local function get(A) local file = fs.open(tostring(A),"r") if not file then return false end local data = file.readAll() file.close() if data then return data end end
  107. local function getT(A) local data = get(A) if data then data = textutils.unserialize(data) end if data then return data end end
  108.  
  109. --Distance Calculation
  110.  
  111. local function fromto(A,B) return math.abs(A.x-B.x)+math.abs(A.y-B.y)+math.abs(A.z-B.z) end
  112.  
  113. --Modem use (transiting if can transmit)
  114.  
  115. local function transmit(...)
  116.     if modem then modem.transmit(...) end
  117. end
  118.  
  119. --Comparing 2 orders
  120.  
  121. local function compOr(A,B)
  122.     if
  123.     A.size.x==B.size.x and
  124.     A.size.y==B.size.y and
  125.     A.size.z==B.size.z and
  126.     A.start.x==B.start.x and
  127.     A.start.y==B.start.y and
  128.     A.start.z==B.start.z and
  129.     A.start.xd==B.start.xd and
  130.     A.start.zd==B.start.zd and
  131.     A.repeats.x==B.repeats.x and
  132.     A.repeats.z==B.repeats.z
  133.     then
  134.         return true
  135.     else
  136.         return false
  137.     end
  138. end
  139.  
  140. --Merging maps (they are same size from definition)
  141.  
  142. local function merMap(A,B)
  143.     local Out={}
  144.     local kA,vA,kAi,vAi
  145.     for kA,vA in pairs(A) do
  146.         Out[kA]={}
  147.         for kAi,vAi in pairs(vA) do
  148.             if krampam[A[kA][kAi]] <= krampam[B[kA][kAi]] then
  149.                 Out[kA][kAi]=A[kA][kAi]
  150.             else
  151.                 Out[kA][kAi]=B[kA][kAi]
  152.             end
  153.         end
  154.     end
  155.     return Out
  156. end
  157.  
  158. -- Yes/No Questions
  159.  
  160. local function yn(A)
  161.     if not A then return false end
  162.     local key
  163.     write(A.."[y/n]:")
  164.     while true do
  165.         _,key = os.pullEvent("key")
  166.         if key==21 then write(keys.getName(key).."\n") sleep(0.01) return true
  167.         elseif key==49 then write(keys.getName(key).."\n") sleep(0.01) return false
  168.         end
  169.     end
  170. end
  171.  
  172. -- Compressing inventory -- used only if cobble dropping is online - otherwise there is never a hole in inventory
  173.  
  174. local function compress(A)
  175.     if not A then return false end
  176.     local i,k
  177.     for i=A,16 do
  178.         if turtle.getItemCount(i)==0 then
  179.             for k=i,16 do
  180.                 if turtle.getItemCount(k)>0 then turtle.select(k) turtle.transferTo(i) break end
  181.             end
  182.         end
  183.         if turtle.getItemCount(i)>0 then
  184.             turtle.select(i)
  185.             for k=i,16 do
  186.                 if turtle.compareTo(k) then turtle.select(k) turtle.transferTo(i,turtle.getItemSpace(i)) turtle.select(i) end
  187.             end
  188.         end
  189.     end
  190. end
  191.  
  192. --Char testing -- used for ctrl+r/s detection
  193.  
  194. local function testforchar(A)
  195.     local out=false
  196.     local test=keys.getName(A)
  197.     local tempT={}
  198.     os.queueEvent("testchar"..test) -- possible loose of event? Tested - 100% operational
  199.    
  200.     while true do
  201.         table.insert(tempT,{os.pullEvent()})
  202.         --print(unpack(tempT[#tempT]))
  203.         if tempT[#tempT][1]=="char" then
  204.             if tempT[#tempT][2]==test then out=true end
  205.         elseif tempT[#tempT][1]=="testchar"..test then
  206.             table.remove(tempT,#tempT) break
  207.         end
  208.     end
  209.  
  210.     while #tempT >0 do
  211.         os.queueEvent(unpack(table.remove(tempT)))
  212.     end
  213.  
  214.     return out
  215. end
  216.  
  217. -- normalizing zones (cause pepole are idiots sometimes)
  218.  
  219. local function normZone()
  220.     local mi=math.min
  221.     local ma=math.max
  222.     for k,i in pairs(order.zone) do
  223.         local j={xl=i.xl,yl=i.yl,zl=i.zl,xu=i.xu,yu=i.yu,zu=i.zu}
  224.         order.zone[k]={xl=mi(j.xl,j.xu),yl=mi(j.yl,j.yu),zl=mi(j.zl,j.zu),xu=ma(j.xl,j.xu),yu=ma(j.yl,j.yu),zu=ma(j.zl,j.zu)}
  225.     end
  226. end
  227.  
  228. -- face to xd,zd and back
  229.  
  230. local function facetx(A)
  231.     if A==0 then return 0,1
  232.     elseif A==1 then return -1,0
  233.     elseif A==2 then return 0,-1
  234.     elseif A==3 then return 1,0 end
  235.     return nil,nil
  236. end
  237.  
  238. local function xtface(A,B)
  239.     if A==0 and B==1 then return 0
  240.     elseif A==-1 and B==0 then return 1
  241.     elseif A==0 and B==-1 then return 2
  242.     elseif A==1 and B==0 then return 3 end
  243.     return -1
  244. end
  245.  
  246. -- getting User entered coords
  247.  
  248. local function getpos()
  249.     local temp={}
  250.     while true do
  251.         print("Enter data from F3 Screen")
  252.         write("Enter x coordinate:") temp.x=tonumber(read())
  253.         write("Enter y coordinate:") temp.y=tonumber(read())
  254.         write("Enter z coordinate:") temp.z=tonumber(read())
  255.         write("Enter f coordinate\n (where turtle is facing):") temp.xd,temp.zd=facetx(tonumber(read()))
  256.         if temp.x and temp.y and temp.z and temp.xd and temp.zd then
  257.             print("x:"..temp.x.." y:"..temp.y.." z:"..temp.z.." f:"..xtface(temp.xd,temp.zd))
  258.             if yn("Are thise correct?") then break end
  259.         end
  260.     end
  261.     return temp
  262. end
  263.  
  264. -- fuel checking
  265.  
  266. local function getFuelLevel()
  267.     local i = turtle.getFuelLevel()
  268.     sleep(0.01) --Wierd Yeld bug ugly fix -- unneded now but still left gere 0 just in case.
  269.     if i =="unlimited" then
  270.         return 10000000  --This should be enuf for radter large mine - still its mostly unused.
  271.     else
  272.         return i
  273.     end
  274. end
  275.  
  276.  
  277. -- Making Map
  278.  
  279. local function newMap(A,B)
  280.     local i,k
  281.     local Out={}
  282.     for i=1,A do
  283.         Out[i]={}
  284.         for k=1,B do
  285.             Out[i][k]=mapmark[3]  --Filling map with #
  286.         end
  287.     end
  288.     return Out
  289. end
  290.  
  291. ---- Movement
  292.  
  293. -- Zone testing
  294. local function inZone(A)
  295.     local i
  296.     for _,i in pairs(order.zone) do
  297.         --print(i.xl.." "..i.yl.." "..i.zl) print(A.x.." "..A.y.." "..A.z) print(i.xu.." "..i.yu.." "..i.zu)
  298.         if
  299.         i.xl<=A.x and i.xu>=A.x and
  300.         i.yl<=A.y and i.yu>=A.y and
  301.         i.zl<=A.z and i.zu>=A.z
  302.         then return true end
  303.     end
  304.     return false
  305. end
  306.  
  307. -- TArget testing
  308. local function BroLock(A)
  309.     local i
  310.     local unlock
  311.     local stat=status
  312.     local timeout=os.clock()
  313.     while true do
  314.         unlock=true
  315.         for _,i in pairs(currents) do
  316.             if i.pos then
  317.                 if i.pos.x==A.x and i.pos.y==A.y and i.pos.z==A.z and i.dist < 15 and (os.clock()-i.atime)<10 then unlock=false status="Queue" break end
  318.             end
  319.         end
  320.         if unlock or (os.clock()-timeout)>60 then status=stat return true  end
  321.         sleep(2)
  322.     end
  323. end
  324.  
  325.  
  326. -- Chest empting (OpenPeripheral Only) (also used for vacumming)
  327.  
  328. local function getAll(A)
  329.     while turtle["suck"..A]() do
  330.         if turtle.getItemCount(16) >= 1 and not maintencegoto then fullcheack() end
  331.     end
  332. end
  333.  
  334. -- Cleaner Function --works only on non-block situations.
  335.  
  336. local function vacuum(A)
  337.     if not turtle["detect"..A]() then
  338.         getAll(A)
  339.     end
  340. end
  341.  
  342. -- Brutality function
  343.  
  344. local function attack(A)
  345.     while turtle["attack"..A]() do sleep(0.4) turtle["attack"..A]() sleep(0.4) end
  346. end
  347.  
  348. --Combo Function
  349.  
  350. local function getAttackAll(A)
  351. getAll(A)
  352. attack(A)
  353. getAll(A)
  354. end
  355.  
  356. -- Mining
  357.  
  358. local function digUp()
  359.     if inZone({x=pos.x,y=pos.y+1,z=pos.z}) then return false end
  360.     if peripheral.isPresent("top") then
  361.         local A=peripheral.getType("top")
  362.         if not chests[A] then
  363.             sleep(1) return false
  364.         end
  365.     end
  366.     getAttackAll("Up")
  367.     return turtle.digUp()
  368. end
  369.  
  370. local function dig()
  371.     if inZone({x=pos.x+pos.xd,y=pos.y,z=pos.z+pos.zd}) then return false end
  372.     if peripheral.isPresent("front") then
  373.         local A=peripheral.getType("front")
  374.         if not chests[A] then
  375.             sleep(1) return false
  376.         end
  377.     end
  378.     getAttackAll("")
  379.     return turtle.dig()
  380. end
  381.  
  382. local function digDown()
  383.     if inZone({x=pos.x,y=pos.y-1,z=pos.z}) then return false end
  384.     if peripheral.isPresent("bottom") then
  385.         local A=peripheral.getType("bottom")
  386.         if not chests[A] then
  387.             sleep(1) return false
  388.         end
  389.     end
  390.     getAttackAll("Down")
  391.     return turtle.digDown()
  392. end
  393.  
  394. --Turning
  395.  
  396. local function turnRight()
  397.     while reboot do sleep(0.01) end
  398.     movelock=true
  399.     turtle.turnRight()
  400.     pos.xd, pos.zd = -pos.zd, pos.xd
  401.     if not usegps then saveT("/gps.log",pos) end
  402.     movelock=false
  403. end
  404.  
  405. local function turnLeft()
  406.     while reboot do sleep(0.01) end
  407.     movelock=true
  408.     turtle.turnLeft()
  409.     pos.xd, pos.zd = pos.zd, -pos.xd
  410.     if not usegps then saveT("/gps.log",pos) end
  411.     movelock=false
  412. end
  413.  
  414. --setting direction
  415. --old face -> local function face(A,B) while pos.xd ~= A or pos.zd ~= B do turnRight() end end
  416.  
  417. local function face(A,B)
  418.     if pos.xd==A and pos.zd==B then return end
  419.     if pos.zd==A and -pos.xd==B then turnLeft() return end
  420.     if -pos.zd==A and pos.xd==B then turnRight() return end
  421.     if -pos.xd==A or -pos.zd==B then turnRight() turnRight() return end
  422. end
  423.  
  424. -- main movement ones
  425.  
  426. local function up(B)
  427.     if not B then B=0 end
  428.     while true do
  429.         if not turtle.detectUp() then getAttackAll("Up") end
  430.         while reboot do sleep(0.01) end
  431.         movelock=true
  432.         if turtle.up() then
  433.             pos.y = pos.y+1
  434.             if not usegps then saveT("/gps.log",pos) end
  435.             movelock=false
  436.             return true
  437.         elseif B>0 then
  438.             digUp() B=B-1
  439.         else
  440.             movelock=false
  441.             return false
  442.         end
  443.     end
  444. end
  445.  
  446. local function down(B)
  447.     if not B then B=0 end
  448.     while true do
  449.         if not turtle.detectDown() then getAttackAll("Down") end
  450.         while reboot do sleep(0.01) end
  451.         movelock=true
  452.         if turtle.down() then
  453.             pos.y = pos.y-1
  454.             if not usegps then saveT("/gps.log",pos) end
  455.             movelock=false
  456.             return true
  457.         elseif B>0 then
  458.             digDown() B=B-1
  459.         else
  460.             movelock=false
  461.             return false
  462.         end
  463.     end
  464. end
  465.  
  466. local function forward(B)
  467. if not B then B=0 end
  468.     while true do
  469.         if not turtle.detect() then getAttackAll("") end
  470.         while reboot do sleep(0.01) end
  471.         movelock=true
  472.         if turtle.forward() then
  473.             pos.x = pos.x+pos.xd
  474.             pos.z = pos.z+pos.zd
  475.             if not usegps then saveT("/gps.log",pos) end
  476.             movelock=false
  477.             return true
  478.         elseif B>0 then
  479.             dig() B=B-1
  480.         else
  481.             movelock=false
  482.             return false
  483.         end
  484.     end
  485. end
  486.  
  487. local function back(B)  --unused in program but left for completition reasons (could be used in future)
  488.     if not B then B=0 end
  489.     while true do
  490.         while reboot do sleep(0.01) end
  491.         movelock=true
  492.         if turtle.back() then
  493.             pos.x = pos.x-pos.xd
  494.             pos.z = pos.z-pos.zd
  495.             if not usegps then saveT("/gps.log",pos) end
  496.             movelock=false
  497.             return true
  498.         else
  499.             movelock=false
  500.             return false
  501.         end
  502.     end
  503. end
  504.  
  505. -- movement random pathfinding addon
  506.  
  507. local function upP(B,C)
  508.     if not B then B=0 end
  509.     if not C then C=0 end
  510.     while true do
  511.         if up(B) then
  512.             return true
  513.         else
  514.             if turtle.detect() then
  515.                 if math.random(1,2) == 1 then
  516.                     turnRight(B)
  517.                 else
  518.                     turnLeft(B)
  519.                 end
  520.             end
  521.             forward(B)
  522.             if up(B) then return true end
  523.             if C>0 then
  524.                 C=C-1
  525.             else
  526.                 return false
  527.             end
  528.         end
  529.     end
  530. end
  531.  
  532.  
  533. local function downP(B,C)
  534.     if not B then B=0 end
  535.     if not C then C=0 end
  536.     while true do
  537.         if down(B) then
  538.             return true
  539.         else
  540.             if turtle.detect() then
  541.                 if math.random(1,2) == 1 then
  542.                     turnRight(B)
  543.                 else
  544.                     turnLeft(B)
  545.                 end
  546.             end
  547.                 forward(B)
  548.                 if down(B) then return true end
  549.             if C>0 then
  550.                 C=C-1
  551.             else
  552.                 return false
  553.             end
  554.         end
  555.     end
  556. end
  557.  
  558. local function forwardP(B,C)
  559.     if not B then B=0 end
  560.     if not C then C=0 end
  561.     while true do
  562.         if forward(B) then
  563.             return true
  564.         else
  565.             if math.random(1,2) == 1 then
  566.                 if not upP(B,math.min(C,3)) then downP(B,math.min(C,3)) end
  567.             else
  568.                 if not downP(B,math.min(C,3)) then upP(B,math.min(C,3)) end
  569.             end
  570.             if forward(B) then return true end
  571.             if C>0 then
  572.                 C=C-1
  573.             else
  574.                 return false
  575.             end
  576.         end
  577.     end
  578. end
  579.  
  580. local function backP(B,C) --unused in program but left for completition reasons (could be used in future)
  581.     if not B then B=0 end
  582.     if not C then C=0 end
  583.     while true do
  584.         if back(B) then
  585.             return true
  586.         else
  587.             if math.random(1,2) == 1 then
  588.                 if not upP(B,math.min(C,3)) then downP(B,math.min(C,3)) end
  589.             else
  590.                 if not downP(B,math.min(C,3)) then upP(B,math.min(C,3)) end
  591.             end
  592.             if back(B) then return true end
  593.             if C>0 then
  594.                 C=C-1
  595.             else
  596.                 return false
  597.             end
  598.         end
  599.     end
  600. end
  601.  
  602. ---- Advanced movement (goto)
  603.  
  604. local function goto(A,B,C)
  605.     if not B then B=0 end
  606.     if not C then C=0 end
  607.     --if (pos.x ~= A.x or pos.z ~= A.z) and pos.xd == pos.zd then getposdr(B) end -- get data at start not now.
  608.  
  609.     while A.x-pos.x ~= 0 or A.y-pos.y ~= 0 or A.z-pos.z ~= 0 do
  610.         if A.x-pos.x < 0 then face(-1,0) while A.x-pos.x ~= 0 and pos.xd==-1 and pos.zd==0 do if not forwardP(B,C) then break end BroLock(A) end BroLock(A) end
  611.         if A.x-pos.x > 0 then face(1,0) while A.x-pos.x ~= 0 and pos.xd==1 and pos.zd==0 do if not forwardP(B,C) then break end BroLock(A) end BroLock(A) end
  612.         if A.z-pos.z < 0 then face(0,-1) while A.z-pos.z ~= 0 and pos.xd==0 and pos.zd==-1 do if not forwardP(B,C) then break end BroLock(A) end BroLock(A) end
  613.         if A.z-pos.z > 0 then face(0,1) while A.z-pos.z ~= 0 and pos.xd==0 and pos.zd==1 do if not forwardP(B,C) then break end BroLock(A) end BroLock(A) end
  614.         if A.y-pos.y < 0 then while A.y-pos.y ~= 0 do if not downP(B,C) then break end BroLock(A) end BroLock(A) end
  615.         if A.y-pos.y > 0 then while A.y-pos.y ~= 0 do if not upP(B,C) then break end BroLock(A) end BroLock(A) end
  616.         if settings.manual then
  617.             local i=getFuelLevel()
  618.             if i<fromto(A,pos) and i>fromto(order.manpos,pos)+100 then maintence(true) end
  619.         else
  620.             if getFuelLevel()<fromto(A,pos) then maintence(true) end
  621.         end
  622.         sleep(0.01)
  623.     end
  624.     if A.xd==0 or A.zd==0 then
  625.         face(A.xd,A.zd)
  626.     end
  627.  
  628.     return true
  629. end
  630.  
  631. ---- Inventory menagement
  632.  
  633. local function findplace()
  634.     local i
  635.     if not turtle.detectDown() then return "Down" end
  636.     if not turtle.detectUp() then return "Up" end
  637.     for i=1,4 do
  638.         if not turtle.detect() then return "" end
  639.         turnLeft()
  640.     end
  641. end
  642.  
  643. --enderchest recovery (if it gets lost after crash/unload)
  644.  
  645. local function slotask()
  646.    
  647.     if settings.slot.stuff then
  648.         if turtle.getItemCount(settings.slot.stuff)==0 then
  649.             write("Enderchest - Loot in slot "..tostring(settings.slot.stuff))read()
  650.         end
  651.     end
  652.     if  settings.slot.fuel then
  653.         if turtle.getItemCount(settings.slot.fuel)==0 then
  654.             write("Enderchest - Fuel in slot "..tostring(settings.slot.fuel))read()
  655.         end
  656.     end
  657.     if  settings.slot.cobble then
  658.         if turtle.getItemCount(settings.slot.cobble)==0 then
  659.             write("1 cobble in slot "..tostring(settings.slot.cobble))read()
  660.         end
  661.     end
  662. end
  663.  
  664. local function slotcheck()
  665.     local function test(A)
  666.         local temp=peripheral.getType(A)
  667.         if not temp then return true end
  668.         if temp=="enderchest" then return true end
  669.         return false
  670.     end
  671.  
  672.     if not settings.manual then
  673.         local temp
  674.         if turtle.getItemCount(1)==0 then
  675.             turtle.select(1)
  676.             if turtle.detectDown() and test("bottom") and turtle.getItemCount(1)==0 then turtle.digDown() end
  677.             if turtle.detectUp() and test("top") and turtle.getItemCount(1)==0 then turtle.digUp() end
  678.             if turtle.detect() and test("front") and turtle.getItemCount(1)==0 then turtle.dig() end
  679.         end
  680.         if turtle.getItemCount(2)==0 then
  681.             turtle.select(2)
  682.             if turtle.detectDown() and test("bottom") and turtle.getItemCount(2)==0 then turtle.digDown() end
  683.             if turtle.detectUp() and test("top") and turtle.getItemCount(2)==0 then turtle.digUp() end
  684.             if turtle.detect() and test("front") and turtle.getItemCount(2)==0 then turtle.dig() end
  685.         end
  686.     end
  687. end
  688.  
  689. -- maintence
  690. local function refside(side,amount,ender,deb)
  691.     local state=getFuelLevel()
  692.     local value
  693.     turtle.select(settings.slot.todrop)
  694.     while true do
  695.         if ender then
  696.             turtle.select(settings.slot.fuel)
  697.             while not turtle["place"..side]() do attack(side) vacuum(side) sleep(0.01) end
  698.             turtle.select(settings.slot.todrop)
  699.         end
  700.         if  turtle["suck"..side]() then
  701.             if turtle.refuel(1) then
  702.                 value=getFuelLevel()-state
  703.                 state=getFuelLevel()
  704.                 if deb then print("Worth:"..value.." Need:"..math.max(math.ceil((amount-state)/value),0)) end
  705.                 if value ~= 0 then turtle.refuel(math.max(math.ceil((amount-state)/value),0)) end
  706.                 turtle["drop"..side](turtle.getItemCount(settings.slot.todrop))
  707.                 turtle.refuel(64)
  708.                 state=getFuelLevel()
  709.             end
  710.             turtle["drop"..side](turtle.getItemCount(settings.slot.todrop))
  711.         end
  712.         if ender then
  713.             turtle.select(settings.slot.fuel)
  714.             turtle["dig"..side]()
  715.             turtle.select(settings.slot.todrop)
  716.         end
  717.         if deb then print(state) end
  718.         sleep(1)
  719.         if state>=amount then break end
  720.     end
  721. end
  722.  
  723. function maintence(noback) --not local cause already defined
  724.     local i
  725.     saveT("/spot.log",pos)
  726.     if settings.manual then
  727.         if maintencegoto then return false end
  728.         local distance=fromto(pos,order.manpos)
  729.         maintencegoto=true
  730.         goto(order.manpos,0,10)
  731.         maintencegoto=false
  732.        
  733.         --drop stuff
  734.         for i=(settings.slot.todrop),16 do turtle.select(i) while not (turtle.drop() and turtle.getItemCount(i)==0) and turtle.getItemCount(i)>0 do sleep(0.01) end end
  735.  
  736.         if getFuelLevel() < 400+distance*2 then
  737.             refside("Up",500+distance*2,false,false)
  738.         end
  739.  
  740.     else
  741.         local distance=fromto(pos,order.start)
  742.         --find space for it
  743.         local side=findplace()
  744.        
  745.         --drop stuff
  746.         turtle.select(settings.slot.stuff)
  747.         while not turtle["place"..side]() do attack(side) vacuum(side) sleep(0.01) end
  748.         for i=(settings.slot.todrop),16 do turtle.select(i) while (not turtle["drop"..side]() and turtle.getItemCount(i)==0) and turtle.getItemCount(i)>0 do sleep(0.01) end end
  749.         turtle.select(settings.slot.stuff)
  750.         turtle["dig"..side]()
  751.        
  752.         if getFuelLevel() < 100+distance then
  753.             refside(side,200+distance,true,false)
  754.         end
  755.        
  756.     end
  757.     if not noback then goto(getT("/spot.log"),0,10) end
  758.     turtle.select(settings.slot.mine)
  759. end
  760.  
  761. --fast check
  762.  
  763. function fullcheack()
  764.  
  765.     local function needfuel()
  766.         if settings.manual then
  767.             return fromto(pos,order.manpos)+200
  768.         else
  769.             return fromto(pos,order.start)+100
  770.         end
  771.     end
  772.     local i
  773.    
  774.     if settings.cobbleaway then
  775.  
  776.         if turtle.getItemCount(settings.slot.cobble) > 60 then
  777.             turtle.select(settings.slot.cobble)
  778.             turtle.dropUp(turtle.getItemCount(settings.slot.cobble)-30)
  779.         end
  780.        
  781.         if turtle.getItemCount(16) >= 1 or getFuelLevel() < needfuel() then
  782.             for i=settings.slot.todrop,16 do
  783.                 if turtle.compareTo(i) then
  784.                     turtle.select(i)
  785.                     turtle.dropUp(turtle.getItemCount(i))
  786.                     turtle.select(settings.slot.cobble)            
  787.                 end
  788.             end
  789.         compress(settings.slot.todrop)
  790.         end
  791.        
  792.         turtle.select(settings.slot.mine)
  793.     end
  794.  
  795.     if  turtle.getItemCount(16) >= 1 or getFuelLevel() < needfuel() then maintence() return true end
  796.     return false
  797. end
  798.  
  799. ---- mining function
  800.  
  801. local function mine3()
  802.     local tu,tf,td
  803.     turtle.select(settings.slot.mine)
  804.     fullcheack()
  805.     while true do
  806.         if turtle.detectDown() then td=digDown() fullcheack()  else getAttackAll("Down") td=false end
  807.         if turtle.detect() then tf=dig() fullcheack()  else getAttackAll("") tf=false end
  808.         if turtle.detectUp() then tu=digUp() fullcheack()  else getAttackAll("Up") tu=false end
  809.         if not (tu or tf or td) then break end
  810.     end
  811.     fullcheack()
  812.     return true
  813. end
  814.  
  815. -- minepart function
  816.  
  817. -- plotting path - Creating table of positions to mine in this layer
  818.  
  819. local function plot(A,right) --A{x,z} current repeat -- plots list of coords that have to be reached to mine one layer.
  820.     --pos={x=0,y=0,z=0,xd=1,zd=0}
  821.     local i,j,k
  822.     local Out={}
  823.     local function mark() table.insert(Out,{x=pp.x,y=pp.y,z=pp.z,xd=pp.xd,zd=pp.zd}) end
  824.     local function omark() Out[#Out]={x=pp.x,y=pp.y,z=pp.z,xd=pp.xd,zd=pp.zd} end
  825.     local function f() pp.x = pp.x+pp.xd pp.z = pp.z+pp.zd mark() end
  826.     local function b() pp.x = pp.x-pp.xd pp.z = pp.z-pp.zd mark() end
  827.     local function u() pp.y = pp.y+1 mark() end
  828.     local function d() pp.y = pp.y-1 mark() end
  829.     local function r() pp.xd, pp.zd = -pp.zd, pp.xd omark() end
  830.     local function l() pp.xd, pp.zd = pp.zd, -pp.xd omark() end
  831.  
  832.     -- figure out coords for other mining directions
  833.  
  834.     if right then
  835.         if current.layer%2==0 or order.size.z%2==0 then
  836.             pp= {x=order.start.x+(A.x-1)*order.size.x*order.start.xd+(A.z-1)*order.size.z*(-order.start.zd),
  837.                 y=math.max(order.start.y-1-current.layer*3,levelmin,order.start.y-order.size.y+2),
  838.                 z=order.start.z+(A.x-1)*order.size.x*order.start.zd+(A.z-1)*order.size.z*order.start.xd,
  839.                 xd=order.start.xd,zd=order.start.zd}
  840.         else
  841.             pp= {x=order.start.x+(A.x-1)*order.size.x*order.start.xd+(A.z-1)*order.size.z*(-order.start.zd)+(order.size.z-1)*(-order.start.zd)+(order.size.x-1)*order.start.xd,
  842.                 y=math.max(order.start.y-1-current.layer*3,levelmin,order.start.y-order.size.y+2),
  843.                 z=order.start.z+(A.x-1)*order.size.x*order.start.zd+(A.z-1)*order.size.z*order.start.xd+(order.size.z-1)*order.start.xd+(order.size.x-1)*order.start.zd,
  844.                 xd=-order.start.xd,zd=-order.start.zd}
  845.         end
  846.     else
  847.         pp= {x=order.start.x+(A.x-1)*order.size.x*order.start.xd+(A.z-1)*order.size.z*(-order.start.zd)+(order.size.z-1)*(-order.start.zd),
  848.             y=math.max(order.start.y-1-current.layer*3,levelmin,order.start.y-order.size.y+2),
  849.             z=order.start.z+(A.x-1)*order.size.x*order.start.zd+(A.z-1)*order.size.z*order.start.xd+(order.size.z-1)*order.start.xd,
  850.             xd=order.start.xd,zd=order.start.zd}
  851.     end
  852.    
  853.     --creating path
  854.     mark()
  855.    
  856.     for i=1,order.size.z do
  857.         for j=1,order.size.x-1 do
  858.             f()
  859.         end
  860.         if i<order.size.z then
  861.         if right then r() f() r() right=false else l() f() l() right=true end
  862.         else r() r() end
  863.     end
  864.  
  865.     --removing parts inside zones -- so that turtle won't get stuck on wall trying to get in impossible place.
  866.  
  867.     for i = table.getn(Out), 1, -1 do
  868.         if inZone(Out[i]) then table.remove (Out,i) end
  869.     end
  870.  
  871.     --returtning path
  872.     return Out,right
  873. end
  874.  
  875. --executing plan
  876.  
  877. local function follow(A)
  878. status="Mining"
  879. local function nex() table.remove(A,1) if table.getn(A)>=1 and inZone(A[1]) then nex() end  end
  880. while table.getn(A)>=1 do
  881. if goto(A[1],10,10) then mine3() nex() end
  882. end
  883. end
  884.  
  885. --planning and executing controlrt
  886.  
  887. local function minepart(A) --A{x,z} current repeat
  888. working=true
  889.  
  890. while maplock do sleep(0.01) end
  891. maplock=true
  892. order.map[A.x][A.z]=mapmark[2]
  893. drawmap=true
  894. saveTH("/order.log",order)
  895. maplock=false
  896.  
  897. if not current.layer then current.layer = 0 end
  898. saveT("/current.log",current)
  899. --pos={x=0,y=0,z=0,xd=1,zd=0}
  900. local right=true
  901. local plan={}
  902.  
  903. while current.layer <= math.floor(order.size.y/3) do
  904. if current.layer==math.floor(order.size.y/3) and order.size.y%3==0 then break end
  905. --print("Layer "..current.layer.." Fuel left:"..turtle.getFuelLevel())
  906. plan,right=plot(A,right)
  907. follow(plan)
  908. current.layer=current.layer+1
  909. saveT("/current.log",current)
  910. if pos.y==levelmin then break end
  911. end
  912.  
  913.  
  914. current.layer=0
  915. saveT("/current.log",current)
  916.  
  917. while maplock do sleep(0.01) end
  918. maplock=true
  919. order.map[A.x][A.z]=mapmark[1]
  920. drawmap=true
  921. saveTH("/order.log",order)
  922. maplock=false
  923. end
  924.  
  925.  
  926. ----------------------------------Data Checks, Loading and Imputs-----------------------------
  927.  
  928. -- Cheecking and loading saved data
  929.  
  930. local function miniwire()
  931.     --transmit(number channel, number replyChannel, string message)
  932.     local small=os.startTimer(0)
  933.     local temp
  934.     while true do
  935.         event={os.pullEvent()}
  936.         if event[1]=="timer" then
  937.             if event[2]==small then
  938.                 small=os.startTimer(2)
  939.                 transmit(channel,1,textutils.serialize(current))
  940.             end
  941.         end
  942.         if event[1]=="modem_message" then
  943.             --print(event[4],":",event[5]," d:",event[6])
  944.             if event[3]==channel then
  945.                 if event[4]==1 then
  946.                     temp=textutils.unserialize(event[5])
  947.                     if temp then currents[temp.id]=temp currents[temp.id].dist=event[6] currents[temp.id].atime=os.clock() end -- saving brothers data.
  948.                 end
  949.             elseif event[3]==gps.CHANNEL_GPS and usegps and pos then transmit(event[4], gps.CHANNEL_GPS, textutils.serialize({pos.x,pos.y,pos.z}))
  950.             end
  951.         end
  952.     end
  953. end
  954.  
  955. local function curcheck()
  956. --reading current
  957.  
  958. current = getT("/current.log")
  959. if not current then current={} end
  960. current.id=os.getComputerID()
  961. saveT("/current.log",current)
  962. return true
  963.  
  964. end
  965.  
  966. local function setget()
  967.     local settings
  968.     term.clear()
  969.     term.setCursorPos(1,1)
  970.     print("No Settings File")
  971.     if yn("Do you want to run local setup?") then
  972.         while true do
  973.             settings={}
  974.             local temp=0
  975.             settings.manual= (not yn("Do you want to use Enderchests?"))
  976.             settings.cobbleaway=yn("Do you want me to trow away cobble?")
  977.             settings.slot={}
  978.             if not settings.manual then
  979.                 print("You will need to provide 2 enderchests")
  980.                 print("One to send loot, Second to refuel turtle")
  981.                 settings.slot.stuff=1
  982.                 settings.slot.fuel=2
  983.                 temp=temp+2
  984.             end
  985.             if settings.cobbleaway then
  986.                 settings.slot.cobble=temp+1
  987.                 settings.slot.mine=temp+1
  988.                 temp=temp+1
  989.             else
  990.                 settings.slot.mine=temp+1
  991.             end
  992.             settings.slot.todrop=temp+1
  993.             print("In this setup there are "..tostring(temp).." slots taken by items i need")
  994.             print("Slots from "..tostring(settings.slot.todrop).."-16 will be filled with loot mined")
  995.             if yn("Are thise Settings correct?") then
  996.                 print("Settings saved")
  997.                 return settings
  998.             end
  999.         end
  1000.     end
  1001.     return nil
  1002. end
  1003.  
  1004. local function setcheck()
  1005.     settings = getT("/settings.log")
  1006.  
  1007.     if not settings then
  1008.         settings=setget()
  1009.         saveTH("/settings.log",settings)
  1010.     end
  1011. end
  1012.  
  1013. local function poscheck()
  1014.  
  1015.     pos=nil
  1016.  
  1017.     if usegps then
  1018.         term.clear()
  1019.         term.setCursorPos(1,1)
  1020.         print("Determining position")
  1021.  
  1022.         local function loop()
  1023.             local i
  1024.             if turtle.forward() then return true end
  1025.             for i=1,3 do turtle.turnLeft() if turtle.forward() then return true end end
  1026.             return false
  1027.         end
  1028.        
  1029.         local pos1,pos2
  1030.         while not pos do
  1031.             if turtle.getFuelLevel()==0 then return false end
  1032.             pos1={gps.locate(2,true)}
  1033.             if pos1[1] then
  1034.                 local dir,wd,i="down",0,1
  1035.                 while true do
  1036.                     if loop() then break end
  1037.                     if turtle.detectDown() then dir="up" for i=1,wd do turtle.up() end end
  1038.                     if turtle.detectUp() and dir=="up" then break end
  1039.                     turtle[dir]() wd=wd+1
  1040.                 end
  1041.             end
  1042.             pos2={gps.locate(2,true)}
  1043.             if pos2[1] and pos1[1] then
  1044.                 if pos1[1]==pos2[1]
  1045.                 and pos1[2]==pos2[2]
  1046.                 and pos1[3]==pos2[3] then break end
  1047.                 pos={x=pos2[1],y=pos2[2],z=pos2[3],xd=pos2[1]-pos1[1],zd=pos2[3]-pos1[3]}
  1048.                 return true
  1049.             end
  1050.             sleep(5)
  1051.         end
  1052.     else
  1053.         pos = getT("/gps.log") -- Standalone mode - loading last known position
  1054.         if not pos then
  1055.             term.clear()
  1056.             term.setCursorPos(1,1)
  1057.             print("No Position File or could not determine position")
  1058.             if yn("Do you want to create one?") then
  1059.                 pos=getpos()
  1060.                 if pos then saveT("/gps.log",pos) print("Position Saved") return true else print("Error") return false end
  1061.             end
  1062.         end
  1063.     end
  1064. end
  1065.  
  1066.  
  1067. local function ordwireless()
  1068.     --print("Scanning for Remote Order")
  1069.     local event={}
  1070.     while true do
  1071.         event={os.pullEvent()}
  1072.         if event[1]=="modem_message" then
  1073.             if event[3]==channel then
  1074.                 if event[4]==7 and run==false then
  1075.                     temp=textutils.unserialize(event[5]) if temp then order=temp run=true status="Running"  current.loc=nil current.layer=nil saveT("/current.log",current) break end
  1076.                 end
  1077.             end
  1078.         end
  1079.     end
  1080. end
  1081.  
  1082. local function askfororder()
  1083.     transmit(channel,3,"Order?")
  1084.     local event={}
  1085.     local timeout=os.startTimer(5)
  1086.     while true do
  1087.         event={os.pullEvent()}
  1088.         if event[1]=="modem_message" then
  1089.             if event[4]==2 then return textutils.unserialize(event[5]) end
  1090.         elseif event[1]=="timer" then
  1091.             if event[2]==timeout then break end
  1092.         end
  1093.     end
  1094.     return nil
  1095. end
  1096.  
  1097.  
  1098. local function ordget()
  1099.     local order
  1100.     term.clear()
  1101.     term.setCursorPos(1,1)
  1102.     print("No Order File")
  1103.     if yn("Do you want me to download Order from other swarmites? I need adleas one in range to download")then
  1104.         print("Establishing Connection")
  1105.         order = askfororder()
  1106.         if order then
  1107.             term.clear()
  1108.             term.setCursorPos(1,1)
  1109.             print("Start Point:")
  1110.             print("x:"..order.start.x.." y:"..order.start.y.." z:"..order.start.z.." xd:"..order.start.xd.." zd:"..order.start.zd)
  1111.             print("Manual Dropoff Point:")
  1112.             print("x:"..order.manpos.x.." y:"..order.manpos.y.." z:"..order.manpos.z.." xd:"..order.manpos.xd.." zd:"..order.manpos.zd)
  1113.             print("Sector size: "..order.size.x.."x"..order.size.z.." ,Deapth: "..order.size.y)
  1114.             print("Repeats size: "..order.repeats.x.."x"..order.repeats.z)
  1115.             print("Will mine area total: "..order.size.x*order.repeats.x.."x"..order.size.z*order.repeats.z)
  1116.             if yn("Is this Order correct?") then
  1117.                 return order
  1118.             else
  1119.                 order=nil
  1120.             end
  1121.         else print("No Swarmites responded to my needs") end
  1122.     end
  1123.     if not order then
  1124.         if yn("Do you want to make Order?") then
  1125.             while true do
  1126.                 term.clear()
  1127.                 term.setCursorPos(1,1)
  1128.                 order={}
  1129.                 order.zone={{xl=0,yl=0,zl=0,xu=0,yu=0,zu=0},}
  1130.                 print("Enter Coords to of start point mining")
  1131.                 order.start={}
  1132.                 order.start=getpos()
  1133.                 if not order.start then print("Error") return false end
  1134.                
  1135.                 while true do
  1136.                     term.clear()
  1137.                     term.setCursorPos(1,1) 
  1138.                     order.size={}
  1139.                     order.repeats={},
  1140.                     write("Enter lenght size of Sector\n[2-10]:") order.size.x=tonumber(read())
  1141.                     write("Enter width  size of Sector\n[2-10]:") order.size.z=tonumber(read())
  1142.                     write("Enter how depth down to mine\n[min 3]:") order.size.y=tonumber(read())
  1143.                     write("Enter number of lenghtwise sector repeats:") order.repeats.x=tonumber(read())
  1144.                     write("Enter number of widthwise  sector repeats:") order.repeats.z=tonumber(read())
  1145.                     if order.size.y and order.size.y <3 then order.size.y=nil end
  1146.                     if order.size.x<2 or order.size.x>10 then order.size.x=nil end
  1147.                     if order.size.z<2 or order.size.z>10 then order.size.z=nil end
  1148.                     if order.size.x and order.size.y and order.size.z and order.repeats.x and order.repeats.z then
  1149.                         print("Sector size: "..order.size.x.."x"..order.size.z.." ,Deapth: "..order.size.y)
  1150.                         print("Repeats size: "..order.repeats.x.."x"..order.repeats.z)
  1151.                         print("Will mine area total: "..order.size.x*order.repeats.x.."x"..order.size.z*order.repeats.z)
  1152.                         if yn("Are thise correct?") then break end
  1153.                     end
  1154.                 end
  1155.                
  1156.                 term.clear()
  1157.                 term.setCursorPos(1,1) 
  1158.                 order.manpos={}
  1159.                 if yn("Do you want to manual dropoff point to ba same as start point?")
  1160.                 then
  1161.                     order.manpos={x=order.start.x,y=order.start.y,z=order.start.z,xd=-order.start.xd,zd=-order.start.zd}
  1162.                 else
  1163.                     print(" Specify costum drop off point")
  1164.                     order.manpos=getpos()
  1165.                 end
  1166.                 if not order.manpos then print("Error") return false end
  1167.                 term.clear()
  1168.                 term.setCursorPos(1,1)
  1169.                 print("Start Point:")
  1170.                 print("x:"..order.start.x.." y:"..order.start.y.." z:"..order.start.z.." xd:"..order.start.xd.." zd:"..order.start.zd)
  1171.                 print("Manual Dropoff Point:")
  1172.                 print("x:"..order.manpos.x.." y:"..order.manpos.y.." z:"..order.manpos.z.." xd:"..order.manpos.xd.." zd:"..order.manpos.zd)
  1173.                 print("Sector size: "..order.size.x.."x"..order.size.z.." ,Deapth: "..order.size.y)
  1174.                 print("Repeats size: "..order.repeats.x.."x"..order.repeats.z)
  1175.                 order.map=newMap(order.repeats.x,order.repeats.z)
  1176.                
  1177.                 if yn("Is this Order correct?") then
  1178.                     print("Order saved")
  1179.                     return order
  1180.                 end
  1181.  
  1182.             end
  1183.         end
  1184.     end
  1185. end
  1186.  
  1187. local function ordcheck()
  1188.  
  1189.     --order setting
  1190.  
  1191.     order = getT("/order.log")
  1192.  
  1193.     if not order then
  1194.         current.loc=nil current.layer=nil
  1195.         saveT("/current.log",current)
  1196.         order=ordget()
  1197.         saveTH("/order.log",order)
  1198.     end
  1199. end
  1200.  
  1201.  
  1202. ------------------------------MAIN 3-----------------------------
  1203.  
  1204.  
  1205. --Screen Controls (Added Screen Freeze code - there is no need to paint screen if noone is looking)
  1206.  
  1207. local function monitor()
  1208.     local i,k,j,l,event
  1209.     local deactive
  1210.     local deactivetime
  1211.     local update
  1212.  
  1213.     local function wmap(A,B)
  1214.         if current.loc and current.loc.x==A and current.loc.z==B then
  1215.             term.setTextColor(colors.black)
  1216.             term.setBackgroundColor(colors.white)
  1217.             write(order.map[A][B])
  1218.             term.setTextColor(colors.white)
  1219.             term.setBackgroundColor(colors.black)
  1220.         else
  1221.             write(order.map[A][B])
  1222.         end
  1223.     end
  1224.    
  1225.     local function stable()
  1226.         i=9
  1227.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1228.         write("Ctrl T-Terminate")
  1229.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1230.         write("P-Pause")
  1231.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1232.         write("O-Unpause")
  1233.         for i=1,13 do
  1234.             term.setCursorPos(14,i)
  1235.             write("|")
  1236.         end
  1237.     end
  1238.    
  1239.     term.clear()
  1240.     term.setCursorPos(1,1)
  1241.     if monactive then
  1242.     drawmap=true
  1243.     update=os.startTimer(0.2)
  1244.     deactive=os.startTimer(10)
  1245.     deactivetime=os.time()+10
  1246.     stable()
  1247.     else
  1248.     print("Screen Paused - press any key to reactivate screen. If Screen don't reactivate infrom Wojbie about bug")
  1249.     stamp(5)
  1250.     end
  1251.  
  1252. while true do
  1253.     event={os.pullEvent()}
  1254.         if event[1]=="key" and not monactive then monactive=true drawmap=true update=os.startTimer(0.2) deactive=os.startTimer(30) deactivetime=os.time()+30 term.clear() stable()
  1255.     elseif event[1]=="timer" then
  1256.         if event[2]==deactive then monactive=false term.clear() term.setCursorPos(1,1) print("Screen Paused - press any key to reactivate screen. If Screen don't reactivate infrom Wojbie about bug")  stamp(5)
  1257.     elseif event[2]==update and monactive then
  1258.         update=os.startTimer(0.2)
  1259.         if drawmap then
  1260.             drawmap=false
  1261.  
  1262.             if order.repeats.z < 13 or not current.loc then l=0 else l=math.max(math.min(order.repeats.z-13,current.loc.z-7),0) end
  1263.             if order.repeats.x < 13 or not current.loc then j=0 else j=math.max(math.min(order.repeats.x-13,current.loc.x-7),0) end
  1264.  
  1265.             term.setCursorPos(1,1) 
  1266.             for i=1,math.min(order.repeats.x,13) do
  1267.                 term.setCursorPos(1,14-i)
  1268.                 for k=1,math.min(order.repeats.z,13) do
  1269.                         wmap(i+j,k+l)
  1270.                 end
  1271.             end
  1272.         end
  1273.  
  1274.         i=1
  1275.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1276.         write("x:"..pos.x.." y:"..pos.y.." z:"..pos.z)
  1277.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1278.         write("id: "..os.getComputerID().." f:"..xtface(pos.xd,pos.zd)) if current.layer then write(" Layer:"..current.layer) end
  1279.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1280.         if current.loc then write("Current Sector:"..current.loc.x.."x"..current.loc.z) end
  1281.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1282.         write("Sector Size:"..order.size.x.."x"..order.size.z.." y: "..order.size.y)
  1283.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1284.         write("Repeat All:"..order.repeats.x.."x"..order.repeats.z)
  1285.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1286.         write("Area total:"..order.size.x*order.repeats.x.."x"..order.size.z*order.repeats.z)
  1287.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1288.         write("Fuel left:"..turtle.getFuelLevel())
  1289.  
  1290.         i=13
  1291.         term.setCursorPos(15,i) for l=0,39-15 do write(" ") end term.setCursorPos(15,i) i=i+1
  1292.         write(status)
  1293.         if ldebug then
  1294.             write("  ")
  1295.             write(deactivetime-os.time())
  1296.         end
  1297.     end
  1298.     end
  1299. end
  1300. print("I should not get printed - If i was inform Wojbie about it. Also Cheescake!!!!")
  1301. end
  1302.  
  1303. --Wireless Part (In Fact its most event handler but i like to call it wireless)
  1304.  
  1305. local function wireless()
  1306.     --modem.transmit(number channel, number replyChannel, string message)
  1307.     local event={}
  1308.     local temp
  1309.     local small=os.startTimer(0)
  1310.     local big=os.startTimer(math.random(0,10))
  1311.     local fiveminreset--=os.startTimer(300+math.random(0,100)) -- 5-6 Min Reboot on turtle - crude debug code but works. Do NOT UNCOMMENT!!
  1312.     while true do
  1313.         event={os.pullEvent()}
  1314.         if event[1]=="modem_message" then
  1315.             --print(event[4],":",event[5]," d:",event[6])
  1316.             if event[3]==channel then
  1317.                 if event[4]==1 then
  1318.                     temp=textutils.unserialize(event[5])
  1319.                     if temp then
  1320.                         currents[temp.id]=temp currents[temp.id].dist=event[6] currents[temp.id].atime=os.clock() -- saving brothers data.
  1321.                         if temp.loc and current.loc then
  1322.                             if temp.loc.x==current.loc.x and temp.loc.z==current.loc.z and temp.layer and current.layer then
  1323.                                 if temp.layer<current.layer then --nothing
  1324.                                 elseif temp.id>current.id then --nothing
  1325.                                 else
  1326.                                 current.loc=nil current.layer=0 run=true while movelock do sleep(0.01) end break --Break Mine if someone else is deeper than me or he is older than me.
  1327.                                 end
  1328.                             end
  1329.                         end
  1330.                     end
  1331.                 elseif event[4]==2 then
  1332.                     temp=textutils.unserialize(event[5])
  1333.                     if temp then
  1334.                         if compOr(order,temp) then
  1335.                             while maplock do sleep(0.01) end
  1336.                             maplock=true
  1337.                             order.map=merMap(order.map,temp.map)
  1338.                             drawmap=true
  1339.                             saveTH("/order.log",order)
  1340.                             maplock=false  
  1341.                             if current.loc then if order.map[current.loc.x][current.loc.z]==mapmark[1] then current.loc=nil current.layer=0 run=true while movelock do sleep(0.01) end break end end --Break Mine if someone reports that this area is mined.
  1342.                         end
  1343.                     end
  1344.                 elseif event[4]==3 and event[5]=="Order?" then big=os.startTimer(0)
  1345.                 elseif event[4]==4 and event[5]=="Pause" then run=false status="Paused" while movelock do sleep(0.01) end break -- pause
  1346.                 elseif event[4]==5 and event[5]=="UnPause" then run=true status="Running" while movelock do sleep(0.01) end break -- unpause
  1347.                 elseif event[4]==6 and run==false then
  1348.                     temp=textutils.unserialize(event[5]) if temp then status="Goto Active" parallel.waitForAny(miniwire,function() goto(temp,0,10) end)  status="Paused" end
  1349.                 elseif event[4]==7 and run==false then
  1350.                     temp=textutils.unserialize(event[5]) if temp then order=temp saveTH("/order.log",order) current.loc=nil current.layer=0 saveT("/current.log",current)  end
  1351.                 elseif event[4]==channel and event[5]=="Terminate" then os.queueEvent("terminate") run=false terminate=true end
  1352.             elseif event[3]==gps.CHANNEL_GPS and usegps then transmit(event[4], gps.CHANNEL_GPS, textutils.serialize({pos.x,pos.y,pos.z}))
  1353.             end
  1354.         elseif event[1]=="timer" then
  1355.             if event[2]==small then
  1356.                 small=os.startTimer(2)
  1357.                 current.pos=pos
  1358.                 current.fuel=getFuelLevel()
  1359.                 transmit(channel,1,textutils.serialize(current))
  1360.             elseif event[2]==big then
  1361.                 big=os.startTimer(10)
  1362.                 transmit(channel,2,textutils.serialize(order))
  1363.             elseif event[2]==rebootimer then --Canceling Ctrl-r/s alert
  1364.                 reboot=false status="Freeze Canceled"
  1365.             elseif event[2]==fiveminreset then --debug timer - unused in normal code - do not use!!
  1366.                 run=current.run while movelock do sleep(0.01) end break
  1367.             end
  1368.         elseif event[1]=="key" then
  1369.             if event[2]==19 or event[2]==31 then
  1370.                 if not testforchar(event[2]) then --detecting ctrl+r/s situation
  1371.                     rebootimer=os.startTimer(2) reboot=true status="Ctrl+"..keys.getName(event[2]).." Detected"
  1372.                 end
  1373.             end
  1374.         elseif event[1]=="char" then
  1375.             if event[2]=="p" or event[2]=="P" then run=false status="Paused" while movelock do sleep(0.01) end break -- pause
  1376.             elseif event[2]=="o" or  event[2]=="O" then run=true status="Running" while movelock do sleep(0.01) end break  end  -- unpause
  1377.         elseif event[1]=="terminate" then
  1378.             while movelock do sleep(0.01) end
  1379.             run=false terminate=true
  1380.             print("Terminate Event")
  1381.             break
  1382.         elseif event[1]=="toggle" then --old code - unused - left as template.
  1383.             while movelock do sleep(0.01) end break
  1384.         end
  1385.     end
  1386.  
  1387. end
  1388.  
  1389. --Main Program
  1390.  
  1391. local function work()
  1392.     transmit(channel,3,"Order?")
  1393.     sleep(2)
  1394.     fullcheack()
  1395.     local j,l=0,0
  1396.     local dist,tdist,i,k,find
  1397.     if current.loc then
  1398.         minepart(current.loc)
  1399.     else
  1400.         current.loc={x=0,z=0}--{x=math.random(1,math.min(order.repeats.x,10)),z=math.random(1,math.min(order.repeats.z,10))}
  1401.     end
  1402.     j=current.loc.x l=current.loc.z
  1403.     while true do
  1404.         find=false
  1405.         dist=order.repeats.x+order.repeats.z
  1406.         for i=1,order.repeats.x do
  1407.             for k=1,order.repeats.z do
  1408.                 if order.map[i][k] == mapmark[3] then
  1409.                         tdist=math.abs(j-i)+math.abs(l-k)
  1410.                         --print(i,"x",k," c:",tdist," d:",dist)
  1411.                         if dist > tdist or tdist==0 then find=true current.loc={x=i,z=k} dist=tdist end
  1412.                 end
  1413.             end
  1414.         end
  1415.         --print(current.loc.x,"x",current.loc.z," d:",dist) read()
  1416.         if not find then break end
  1417.         saveT("/current.log",current)
  1418.         j=current.loc.x l=current.loc.z minepart(current.loc)
  1419.     end
  1420.     current.loc=nil
  1421.     saveT("/current.log",current)
  1422.     if working then maintence(true)
  1423.         goto({x=order.start.x+math.random(0,order.size.x-1)*order.start.xd+math.random(0,order.size.z-1)*(-order.start.zd),
  1424.         y=order.start.y-math.random(0,math.min(order.size.y,9)),
  1425.         z=order.start.z+math.random(0,order.size.x-1)*order.start.zd+math.random(0,order.size.z-1)*order.start.xd,
  1426.         xd=order.start.xd,zd=order.start.zd},0,10)
  1427.         working=false
  1428.     end
  1429.     status="Work Done"
  1430. end
  1431.  
  1432.  
  1433.  
  1434.  
  1435. ------------------------------PROGRAM START-----------------------------
  1436.  
  1437.  
  1438. --sleep so it wont bug later
  1439. sleep(0.01+math.random(1,99)/100)
  1440.  
  1441. --pre setup setup
  1442. local label="Swarminer id:"..os.getComputerID()
  1443. if os.getComputerLabel~=(label) then os.setComputerLabel(label) end
  1444. if usegps then local temp temp={gps.locate(2)} pos={x=temp[1],y=temp[2],z=temp[3]} current.pos={x=temp[1],y=temp[2],z=temp[3]} print(textutils.serialize(pos)) end
  1445.  
  1446.  
  1447. --tArgs setup
  1448. local i,ni
  1449. for ni,i in pairs(tArgs) do
  1450.     if i=="instalation" then
  1451.         curcheck()
  1452.         fs.delete("/settings.log")
  1453.         if fs.exists(tArgs[ni+1].."settings.log") then fs.copy(tArgs[ni+1].."settings.log","/settings.log") end
  1454.         parallel.waitForAny(setcheck,miniwire)
  1455.         if settings then saveT("/settings.log",settings) saveTH(tArgs[ni+1].."settings.log",settings) end
  1456.        
  1457.         fs.delete("/order.log")
  1458.         if fs.exists(tArgs[ni+1].."order.log") then  fs.copy(tArgs[ni+1].."order.log","/order.log") end
  1459.         parallel.waitForAny(ordcheck,miniwire)
  1460.         if order then saveTH("/order.log",order) saveTH(tArgs[ni+1].."order.log",order) end
  1461.        
  1462.         fs.delete("/current.log")
  1463.        
  1464.         slotask()
  1465.         if getFuelLevel()<100+(order.size.x*order.repeats.x+order.size.z*order.repeats.z) then
  1466.             if settings.manual then
  1467.                 refside("Up",100+(order.size.x*order.repeats.x+order.size.z*order.repeats.z),false,true)
  1468.             else
  1469.                 refside("Up",100+(order.size.x*order.repeats.x+order.size.z*order.repeats.z),true,true)
  1470.             end
  1471.         end
  1472.        
  1473.         local function loop()
  1474.             local i
  1475.             if turtle.forward() then return true end
  1476.             for i=1,3 do turtle.turnLeft() if turtle.forward() then return true end end
  1477.             return false
  1478.         end
  1479.         loop()
  1480.        
  1481.         os.reboot()
  1482.        
  1483.         return true
  1484.     elseif i=="create" then
  1485.         if fs.exists(tArgs[ni+1].."settings.log") then print("Preformated Settings Detected") end
  1486.         if yn("Do you want to preformat a new settings file?") then
  1487.             saveTH(tArgs[ni+1].."settings.log",setget())
  1488.             print("Settings Saved on Floppy disk")
  1489.         end
  1490.         if fs.exists(tArgs[ni+1].."order.log") then print("Preformated Order Detected") end
  1491.         if yn("Do you want to preformat a new order file?") then
  1492.             saveTH(tArgs[ni+1].."order.log",ordget())
  1493.             print("Order Saved on Floppy disk")
  1494.         end
  1495.         return true
  1496.     elseif i=="debug" then ldebug=true
  1497.     elseif i=="?" or i=="help" then print("debug/pause/unpause/gotoStart/sendOrder/newMap") return true
  1498.     elseif i=="pause" then print("paused all") transmit(channel,4,"Pause") return true
  1499.     elseif i=="unpause" then print("unpaused all") transmit(channel,5,"UnPause") return true
  1500.     elseif i=="gotoStart" then print("Goto send to all paused") transmit(channel,6,textutils.serialize(getT("/order.log").start)) return true
  1501.     elseif i=="sendOrder" then print("Order send to all paused") transmit(channel,7,textutils.serialize(getT("/order.log"))) return true
  1502.     elseif i=="newMap" then print("New map generated") order = getT("/order.log") order.map=newMap(order.repeats.z,order.repeats.z) saveTH("/order.log",order) return true
  1503.     else return true end
  1504. end
  1505.  
  1506. curcheck()
  1507. parallel.waitForAny(setcheck,miniwire) -- if no settings asks for them
  1508. parallel.waitForAny(poscheck,miniwire) -- looks for position until finds it
  1509. parallel.waitForAny(ordcheck,ordwireless,miniwire)
  1510. if not pos or not order or not settings or not current then print("Somewing went wong") return false end
  1511. if usegps and modem then modem.open(gps.CHANNEL_GPS) else modem.close(gps.CHANNEL_GPS) end --starting up and shutting down gps channel
  1512.  
  1513. -- adding dropoff point to zone file
  1514. order.zone["dropoff"]={xl=order.manpos.x,yl=order.manpos.y,zl=order.manpos.z,xu=order.manpos.x+order.manpos.xd,yu=order.manpos.y+1,zu=order.manpos.z+order.manpos.zd}
  1515.  
  1516. normZone()  -- normalize Zonefile
  1517. slotcheck()
  1518. slotask()
  1519.  
  1520. --debug print
  1521.  
  1522. if ldebug then
  1523.     term.clear()
  1524.     term.setCursorPos(1,1)
  1525.     write("x:"..pos.x.." y:"..pos.y.." z:"..pos.z.." f:"..xtface(pos.xd,pos.zd))
  1526.     if current.layer then print(" Layer:"..current.layer) else print() end
  1527.     if settings.manual then print("Dropof to point") else print("Dropof to Enderchest") end
  1528.     if settings.cobbleaway then print("Trowing Cobble away") else print("Collecting all") end
  1529.     print("Sector size: "..order.size.x.."x"..order.size.z.." ,Deapth: "..order.size.y)
  1530.     print("Repeats size: "..order.repeats.x.."x"..order.repeats.z)
  1531.     print("Will mine area total: "..order.size.x*order.repeats.x.."x"..order.size.z*order.repeats.z)
  1532.     if not yn("Run Program Now?") then return end
  1533. end
  1534.  
  1535.  
  1536.  
  1537. --Non-termination point
  1538. local oldpullEvent=os.pullEvent
  1539. os.pullEvent=os.pullEventRaw
  1540.  
  1541. repeat
  1542. current.run = run
  1543. drawmap=true
  1544. slotcheck()
  1545.  
  1546. if run then
  1547.     run=false
  1548.     parallel.waitForAny(monitor,work,wireless) --working
  1549.     if terminate then break end
  1550. else
  1551.     parallel.waitForAny(monitor,wireless) --awaiting orders
  1552.     if terminate then break end
  1553. end
  1554.  
  1555. until terminate
  1556.  
  1557. --Restoring normal termination
  1558. os.pullEvent=oldpullEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement