Advertisement
hkbruvold

minerbot01.lua

Apr 29th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.35 KB | None | 0 0
  1. os.loadAPI("navigation")
  2. local tnav = navigation.newNav()
  3.  
  4. local id = os.getComputerID()
  5. local serverID = nil
  6.  
  7. local home = {}
  8. local group = {}
  9.  
  10. rednet.open("right")
  11.  
  12. function save(name, data)
  13.     local file = fs.open(name, "w")
  14.     file.write(textutils.serialize(data))
  15.     file.close()
  16. end
  17.    
  18. function load(name)
  19.     local file = fs.open(name, "r")
  20.     local data = file.readAll()
  21.     file.close()
  22.     return textutils.unserialize(data)
  23. end
  24.  
  25. function copyTable(inTable)
  26.     local outTable = {}
  27.     for key, value in pairs(inTable) do
  28.         outTable[key] = value
  29.     end
  30.     return outTable
  31. end
  32.  
  33. function createCoordinateTable(a, b)
  34.     local moveTable = {}
  35.     local vector = {b[1] - a[1], b[2] - a[2], b[3] - a[3]}
  36.     local unitVector = {}
  37.     for i = 1,3 do
  38.         unitVector[i] = vector[i]/math.abs(vector[i])
  39.     end
  40.    
  41.     -- direction of length and width
  42.     local mLen = nil
  43.     local mWid = nil
  44.     if math.abs(vector[1]) > math.abs(vector[3]) then
  45.         mLen = 1
  46.         mWid = 3
  47.     else
  48.         mLen = 3
  49.         mWid = 1
  50.     end
  51.    
  52.     local curDir = 1 -- direction relative to vector to add next coordinate
  53.     local curWDir = 1 -- number to keep track of direction on height layers
  54.     local lastCoord = copyTable(a)
  55.     local nextCoord = {}
  56.     for height = a[2], b[2], unitVector[2] do -- loop through height layers
  57.         for width = a[mWid], b[mWid], unitVector[mWid] do -- loop through width lines
  58.             local realWidth = nil
  59.             if curWDir == 1 then realWidth = width
  60.             else realWidth = a[mWid] + b[mWid] - width end
  61.            
  62.             nextCoord[mWid] = realWidth
  63.             nextCoord[mLen] = lastCoord[mLen]
  64.             nextCoord[2] = height
  65.            
  66.             table.insert(moveTable, copyTable(nextCoord))
  67.             lastCoord = copyTable(nextCoord)
  68.            
  69.             nextCoord[mWid] = realWidth
  70.             nextCoord[mLen] = lastCoord[mLen] + curDir * vector[mLen]
  71.             nextCoord[2] = height
  72.            
  73.             table.insert(moveTable, copyTable(nextCoord))
  74.             lastCoord = copyTable(nextCoord)
  75.            
  76.             curDir = curDir * -1
  77.         end
  78.         curWDir = curWDir * -1
  79.     end
  80.     table.insert(moveTable, home)
  81.     return moveTable
  82. end
  83.  
  84. function fullInv()
  85.     group = load("invgroups")
  86.     local curType = 0
  87.     local curSum = 64
  88.     for i = 1, 16 do
  89.         if curType == group[i] then
  90.             curSum = curSum + turtle.getItemSpace(i)
  91.         else
  92.             if curSum < 5 then
  93.                 return true
  94.             end
  95.             curType = group[i]
  96.             curSum = turtle.getItemSpace(i)
  97.         end
  98.     end
  99.     return false
  100. end
  101.  
  102. function emptyInventory()
  103.     tnav.moveTo(home)
  104.     for i = 1, 16 do
  105.         turtle.select(i)
  106.         if turtle.getItemCount(i) > 1 then
  107.             turtle.dropDown(turtle.getItemCount(i)-1)
  108.         end
  109.     end
  110.     turtle.select(1)
  111. end
  112.  
  113. function needRefuel()
  114.     if turtle.getFuelLevel() < 5000 then
  115.         return true
  116.     end
  117.     return false
  118. end
  119.  
  120. function refuel()
  121.     for i = 1, 3 do
  122.         turtle.select(i)
  123.         turtle.refuel(turtle.getItemCount(i)-1)
  124.     end
  125.     turtle.select(1)
  126. end
  127.  
  128. function mineArea(a, b)
  129.     local areaData = {}
  130.     local progress = 0
  131.     local moveTable = {}
  132.     local tableSize = 0
  133.     if a == nil then -- resume
  134.         areaData = load("areadata")
  135.         progress = areaData.progress
  136.         moveTable = areaData.moveTable
  137.     else -- start new
  138.         moveTable = createCoordinateTable(a, b)
  139.     end
  140.     tableSize = table.getn(moveTable)
  141.    
  142.     while progress < tableSize do
  143.         areaData.progress = progress
  144.         areaData.moveTable = moveTable
  145.         save("areadata", areaData)
  146.        
  147.         tnav.moveTo(moveTable[progress+1])
  148.         progress = progress + 1
  149.        
  150.         if progress % 50 == 0 then
  151.             if needRefuel() then
  152.                 refuel()
  153.             end
  154.         end
  155.        
  156.         if progress % 10 == 0 then
  157.             if fullInv() then
  158.                 emptyInventory()
  159.             end
  160.         end
  161.     end
  162.     emptyInventory()
  163. end
  164.  
  165. function setHome()
  166.     home = tnav.getPos()
  167.     save("home", home)
  168. end
  169.  
  170. if fs.exists("home") then
  171.     home = load("home")
  172. end
  173.  
  174. if fs.exists("serverID") then
  175.     serverID = load("serverID")
  176. else
  177.     write("Type in server ID: ")
  178.     serverID = tonumber(read())
  179.     save("serverID", serverID)
  180. end
  181.  
  182. if fs.exists("invgroups") then
  183.     groups = load("invgroups")
  184. else
  185.     groups = {1,1,1,2,3,3,3,3,4,4,5,5,6,7,8,9}
  186.     save("invgroups", groups)
  187. end
  188.  
  189. -- connect to server
  190. rednet.send(serverID, "me turtle, you server")
  191. print("sent handshake message to server")
  192.  
  193. if fs.exists("areadata") then
  194.     mineArea()
  195. end
  196.  
  197. while true do
  198.     id, message = rednet.receive()
  199.     print("received from "..tostring(id)..": "..message)
  200.     local command = {}
  201.     if id == serverID then
  202.         for word in message:gmatch("%S+") do
  203.             table.insert(command, word)
  204.         end
  205.        
  206.         if command[1] == "move" then
  207.             if command[2] == "home" then -- go home
  208.                 tnav.moveTo(home)
  209.             elseif tonumber(command[2]) ~= nil then -- go to coordinate
  210.                 local x = tonumber(command[2])
  211.                 local y = tonumber(command[3])
  212.                 local z = tonumber(command[4])
  213.                 if x ~= nil and y ~= nil and z ~= nil then
  214.                     tnav.moveTo(x, y, z)
  215.                 end
  216.             end
  217.        
  218.         elseif command[1] == "mine" then
  219.             local a = {tonumber(command[2]), tonumber(command[3]), tonumber(command[4])}
  220.             local b = {tonumber(command[5]), tonumber(command[6]), tonumber(command[7])}
  221.            
  222.             if a[1] ~= nil and a[2] ~= nil and a[3] ~= nil and
  223.               b[1] ~= nil and b[2] ~= nil and b[3] ~= nil then
  224.                 mineArea(a, b)
  225.             end
  226.        
  227.         elseif command[1] == "sethome" then
  228.             setHome()
  229.            
  230.         elseif command[1] == "resume" then
  231.             tnav.resumeMove()
  232.             mineArea()
  233.        
  234.         elseif command[1] == "setposition" then
  235.             local p = {}
  236.             for i = 2,5 do
  237.                 table.insert(p, tonumber(command[i]))
  238.             end
  239.             tnav.setPos(p[1], p[2], p[3], p[4])
  240.         end
  241.     end
  242. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement