Advertisement
nagoL2015

stocker.lua

Jul 26th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.04 KB | None | 0 0
  1. modem1 = 6565
  2. modem2 = 6566
  3. modem3 = 6567
  4. modem4 = 6568
  5.  
  6. local channel = 1
  7.  
  8. local modems = { peripheral.find("modem") }
  9. if #modems == 0 then error("No modems found") end
  10.  
  11. local x = gps.locate()
  12. if x == nil then while true do print("Failed to connect to GPS servers") local x = gps.locate() if x ~= nil then break end end end
  13.  
  14. local modem = modems[1]
  15.  
  16. local map = {} -- Store chests
  17.  
  18. local location = {}
  19.  
  20. if not fs.exists(".map") then
  21.     print("Map file does not exist.")
  22.     print("Give chest location:")
  23.     print("X: ")
  24.     local x = read()
  25.     print("Y: ")
  26.     local y = read()
  27.     print("Z: ")
  28.     local z = read()
  29.    
  30.    
  31.     if tonumber(x) ~= nil and tonumber(y) ~= nil and tonumber(z) ~= nil then
  32.         map.chest = {}
  33.         map.chest.x = tonumber(x)
  34.         map.chest.y = tonumber(y)
  35.         map.chest.z = tonumber(z)
  36.         map.chest.type = "inputChest"
  37.         print("Ensure all 'row' turtles are ON. Press enter")
  38.         read()
  39.         print("Transmitting...")
  40.         modem.transmit(modem4,7547,"commence")
  41.         modem.open(7547)
  42.         os.startTimer(4)
  43.         while true do
  44.             local e,side,chnl,rChnl,msg,dist = os.pullEvent()
  45.             if e == "modem_message" and type(msg) == "table" and msg.type == "setupResponse" then
  46.                 if msg.success then
  47.                     id = msg.id
  48.                     if id == "" then
  49.                         for i = 1,100 do --Generate random ID to be shown for this
  50.                             if map["empty"..i] == nil then
  51.                                 id = "empty"..i
  52.                                 break
  53.                             end
  54.                         end
  55.                     end
  56.                    
  57.                     print("Success: "..id.." is at ("..msg.x..", "..msg.y..", "..msg.z..")")
  58.                    
  59.                     map[id] = {}
  60.                     map[id].x = msg.x
  61.                     map[id].y = msg.y
  62.                     map[id].z = msg.z
  63.                     map[id].type = "chest"
  64.                 else
  65.                     print("Fail: "..msg.error)
  66.                 end
  67.             elseif e == "timer" then break end
  68.         end
  69.         print("Completed")
  70.         local mapFile = fs.open(".map","w")
  71.         mapFile.write(textutils.serialize(map))
  72.         mapFile.close()
  73.     else
  74.         print("One or more coords is not a number. Try again.") --Redo this with a better message
  75.         sleep(5)
  76.         return
  77.     end
  78. else
  79.     local mapFile = fs.open(".map","r")
  80.     local nmap = textutils.unserialize(mapFile.readAll())
  81.     mapFile.close()
  82.     if nmap == nil then error("failed to unserialize .map")end
  83.     map = nmap
  84. end
  85.  
  86. local pos1x,pos1y,pos1z = gps.locate()
  87. local pos2x,pos2y,pos2z
  88. while true do
  89.     if turtle.forward() then
  90.         pos2x,pos2y,pos2z = gps.locate()
  91.         break
  92.     else
  93.         turtle.turnRight()
  94.     end
  95. end
  96.  
  97. location.x = pos2x
  98. location.y = pos2y
  99. location.z = pos2z
  100.  
  101. local xdif = pos2x-pos1x
  102. local zdif = pos2z-pos1z
  103.  
  104. if zdif == -1 then
  105.     location.facing = "north"
  106. elseif xdif == 1 then
  107.     location.facing = "east"
  108. elseif zdif == 1 then
  109.     location.facing = "south"
  110. elseif xdif == -1 then
  111.     location.facing = "west"
  112. end
  113.  
  114. print("Currently at ("..location.x..", "..location.y..", "..location.z..") facing "..location.facing)
  115.  
  116. local function selSlot(slot)
  117.     if turtle.getSelectedSlot() ~= slot then turtle.select(slot) end
  118. end
  119.  
  120. local oldForward = turtle.forward
  121. local oldBack = turtle.back
  122. local oldRight = turtle.turnRight
  123. local oldLeft = turtle.turnLeft
  124.  
  125. function turtle.forward(...)
  126.     local f = oldForward(unpack({...}))
  127.     local lx,ly,lz = gps.locate()
  128.     location.x = lx
  129.     location.y = ly
  130.     location.z = lz
  131.     return f
  132. end
  133.  
  134. function turtle.back(...)
  135.     local b = oldBack(unpack({...}))
  136.     local lx,ly,lz = gps.locate()
  137.     location.x = lx
  138.     location.y = ly
  139.     location.z = lz
  140.     return b
  141. end
  142.  
  143. function turtle.turnRight(...)
  144.     local r = oldRight(unpack({...}))
  145.     if location.facing == "north" then
  146.         location.facing = "east"
  147.     elseif location.facing == "east" then
  148.         location.facing = "south"
  149.     elseif location.facing == "south" then
  150.         location.facing = "west"
  151.     elseif location.facing == "west" then
  152.         location.facing = "north"
  153.     end
  154.     return r
  155. end
  156.  
  157. function turtle.turnLeft(...)
  158.     local l = oldLeft(unpack({...}))
  159.     if location.facing == "north" then
  160.         location.facing = "west"
  161.     elseif location.facing == "west" then
  162.         location.facing = "south"
  163.     elseif location.facing == "south" then
  164.         location.facing = "east"
  165.     elseif location.facing == "east" then
  166.         location.facing = "north"
  167.     end
  168.     return l
  169. end
  170.  
  171. local ttNum = {north=1,east=2,south=3,west=4}--Instead of doing ttNum[1] to get north, I wanna do ttNum.north to get 1
  172.  
  173. function turnTo(direction)
  174.     if location.facing == direction then return end
  175.     if ttNum[direction]-ttNum[location.facing] == -1 or (location.facing == "north" and direction == "west") then
  176.         turtle.turnLeft() return
  177.     end
  178.     if ttNum[direction]-ttNum[location.facing] == 1 or (location.facing == "west" and direction == "north") then
  179.         turtle.turnRight() return
  180.     end
  181.     if math.abs(ttNum[direction]-ttNum[location.facing]) == 2 then
  182.         turtle.turnRight() turtle.turnRight() return
  183.     end
  184. end
  185.  
  186. local xConv = {[-1]="west",[1]="east"}
  187. local zConv = {[-1]="north",[1]="south"}
  188.  
  189. function go(x,y,z)
  190.     if location.z ~= z then
  191.         for nz = location.z,z,(z-location.z >= 0 and 1 or -1) do
  192.             if z-location.z ~= 0 then
  193.                 turnTo(zConv[(z-location.z >= 0 and 1 or -1)])
  194.                 while not turtle.forward() do end
  195.             end
  196.         end
  197.     end
  198.     if location.x ~= x then
  199.         for nx = location.x,x,(x-location.x >= 0 and 1 or -1) do
  200.             if x-location.x ~= 0 then
  201.                 turnTo(xConv[(x-location.x >= 0 and 1 or -1)])
  202.                 while not turtle.forward() do end
  203.             end
  204.         end
  205.     end
  206. end
  207.  
  208.  
  209. function normalOperation()
  210.     while true do
  211.         for slot = 1,16 do
  212.             if turtle.getItemDetail(slot) ~= nil and map[turtle.getItemDetail(slot).name] ~= nil then
  213.                 print("Going to "..turtle.getItemDetail(slot).name)
  214.                 loc = map[turtle.getItemDetail(slot).name]
  215.                 go(loc.x,loc.y,loc.z)
  216.                 print("Going to "..loc.x..", "..loc.y..", "..loc.z)
  217.                 print("Ended at "..location.x..", "..location.y..", "..location.z)
  218.                
  219.                 local name = turtle.getItemDetail(slot).name
  220.                
  221.                 local bool,itm = turtle.inspectDown()
  222.                 if bool and (itm.name == "minecraft:chest" or itm.name == "minecraft:trapped_chest") then
  223.                     for slot2 = 1,16 do
  224.                         if turtle.getItemDetail(slot2) ~= nil and turtle.getItemDetail(slot2).name == name then
  225.                             turtle.select(slot2)
  226.                             turtle.dropDown()
  227.                         end
  228.                     end
  229.                 end
  230.             end
  231.         end
  232.        
  233.         go(map.chest.x,map.chest.y,map.chest.z)
  234.        
  235.         local b,item = turtle.inspect()
  236.         if not b or not (item.name == "minecraft:chest" or item.name == "minecraft:trapped_chest") then
  237.             while not b or not (item.name == "minecraft:chest" or item.name == "minecraft:trapped_chest") do
  238.                 turtle.turnRight()
  239.                 b,item = turtle.inspect()
  240.             end
  241.         end
  242.        
  243.         while turtle.getItemCount(16) == 0 do selSlot(1) turtle.suck() end
  244.        
  245.         sleep(3)
  246.     end
  247. end
  248.  
  249. function modemListener()
  250.     while true do
  251.         local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
  252.         if chnl == modem1 then
  253.             local x,y,z = gps.locate()
  254.            
  255.             local inv = {}
  256.            
  257.             for slot = 1,16 do
  258.                 local detail = turtle.getItemDetail(slot)
  259.                 if detail == nil then
  260.                     inv[slot] = {}
  261.                 else
  262.                     inv[slot] = detail
  263.                 end
  264.             end
  265.            
  266.             modem.transmit(rChnl,chnl,{type="infoStockerResponse",fuel=turtle.getFuelLevel(),location={x=x,y=y,z=z},inventory=inv})
  267.             print("Sent info")
  268.         end
  269.     end
  270. end
  271.  
  272. modem.open(modem1)
  273.  
  274. parallel.waitForAny(normalOperation,modemListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement