tubaboy26

mainProg (extended version)

Jun 21st, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. rednet.open("right")
  2.     local miners = {}
  3.     local loaders = {}
  4.     local fuelBosses = {}
  5.     local needFuel = {}
  6.     needFuel[2] = {}
  7.     local next = next
  8.     local powerStations = {}
  9.     local reverseStations = {}
  10.     local tArgs = {...}
  11.      
  12.      
  13.     function findTurtles()
  14.        local gotMsg = true
  15.        local id, msg, dist
  16.        rednet.broadcast("checkIn_"..os.getComputerID())
  17.        while gotMsg do
  18.           id,msg,dist = rednet.receive(1)
  19.           if msg == "Miner" then
  20.              print(id..":"..msg)
  21.              miners[#miners+1] = id
  22.           elseif msg == "chunkloader" then
  23.              print(id..":"..msg)
  24.              loaders[#loaders+1] = id
  25.           elseif msg == "fuelBoss" then
  26.              print(id..":"..msg)
  27.              fuelBosses[#fuelBosses+1] = id
  28.              miners[#miners+1] = id
  29.       elseif msg == "powerStation" then
  30.          print(id..":"..msg)
  31.              powerStations[dist] = id
  32.           elseif msg == "Done" then
  33.           else
  34.              print("Done")
  35.              gotMsg = false
  36.           end
  37.        end      
  38.     end
  39.      
  40.     function checkFuel()
  41.       for x,y in pairs(powerStations) do
  42.         rednet.send(y, "checkStationFuel_"..os.getComputerID())
  43.         reverseStations[y] = x
  44.       end  
  45.       local gotmsg = true
  46.       while gotmsg do
  47.         local id, msg, dist = rednet.receive(1)
  48.         if msg == "true" then
  49.           needFuel[2][#needFuel[2]+1] = reverseStations[id]
  50.         elseif msg == "Done" then
  51.         else
  52.           gotmsg = false
  53.         end
  54.       end
  55.       if turtle.getFuelLevel() < 600 then
  56.         needFuel[1] = 1
  57.       end
  58.       if needFuel[1] ~= nil or next(needFuel[2]) ~= nil then
  59.         rednet.send(fuelBosses[1], "fuelBoss_"..textutils.serialize(needFuel))
  60.         rednet.receive()
  61.         if needFuel[1] ~= nil then
  62.           turtle.select(3)
  63.           turtle.refuel()
  64.           turtle.select(1)
  65.         end
  66.         if next(needFuel[2]) ~= nil then
  67.           for x,y in pairs(needFuel[2]) do
  68.             rednet.send(powerStations[y], "eatFuel")
  69.           end
  70.           local q = 0
  71.           while q < #needFuel[2] do
  72.             rednet.receive()
  73.             q=q+1
  74.           end
  75.         end
  76.         needFuel = {}
  77.         needFuel[2] ={}  
  78.       end
  79.     end
  80.      
  81.     function place()
  82.       for x,y in pairs(powerStations) do
  83.         rednet.send(y, "placePower")
  84.       end
  85.       local t = 0
  86.       while t < #powerStations do
  87.         local id,msg,dist=rednet.receive()
  88.         t=t+1
  89.       end
  90.       turtle.select(1)
  91.       turtle.place()
  92.       turtle.down()
  93.       turtle.select(2)
  94.       turtle.place()
  95.       turtle.attack()
  96.     end
  97.      
  98.     function remove()
  99.       for x,y in pairs(powerStations) do
  100.         rednet.send(y, "removePower")
  101.       end
  102.       local t = 0
  103.       while t < #powerStations do
  104.         local id,msg,dist=rednet.receive()
  105.         t=t+1
  106.       end
  107.       turtle.select(2)
  108.       turtle.dig()
  109.       turtle.up()
  110.       turtle.select(1)
  111.       turtle.dig()
  112.     end
  113.      
  114.     function minersGo()
  115.        for x,y in pairs(miners) do
  116.           if tArgs[2] ~= nil then
  117.              rednet.send(y, "cycle_"..tostring(tArgs[2]))
  118.           else
  119.              rednet.send(y, "cycle")
  120.           end
  121.        end
  122.        place()
  123.        local total = 0
  124.        while total < #miners do
  125.           local id,msg,dist=rednet.receive()
  126.           total = total+1
  127.        end  
  128.        remove()
  129.     end
  130.      
  131.     function moveLoaders()
  132.        for x,y in pairs(loaders) do
  133.           rednet.send(y,"chunkLoad")
  134.           rednet.receive()
  135.           sleep(0.5)
  136.        end
  137.     end
  138.  
  139.     findTurtles()
  140.     if tArgs[1] == nil then tArgs[1] = 1 end
  141.     for i = 1,tArgs[1] do
  142.        print("Interation: "..tostring(i).." of "..tostring(tArgs[1]))
  143.        minersGo()
  144.        turtle.forward()
  145.        checkFuel()
  146.        moveLoaders()
  147.     end
Advertisement
Add Comment
Please, Sign In to add comment