Advertisement
Guest User

miner

a guest
Jan 23rd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1.  
  2. local CIDFile = "/.cid";
  3. local locFile = "/.minerLocation";
  4. local controllerID = nil;
  5. local currentChunk = 1;
  6. local offset = 0;
  7. local mineFacing = 0;
  8. local importantDir = "";
  9.  
  10. -- load cID
  11. if fs.exists(CIDFile) then
  12.   local f = fs.open(CIDFile,"r");
  13.   controllerID = tonumber(f.readLine());
  14.   f.close();
  15. end
  16.  
  17. if fs.exists(locFile) then
  18.   local f = fs.open(locFile,"r");
  19.   currentChunk = tonumber(f.readLine());
  20.   offset = tonumber(f.readLine());
  21.   mineFacing = tonumber(f.readLine());
  22.   importantDir = f.readLine();
  23.   f.close();
  24.  
  25.   -- we're resuming. Get set up.
  26.   -- Get to the travel y...
  27.   while t.getPos().y < 80 do
  28.     t.up();
  29.   end
  30.   -- Face the right way
  31.   while mineFacing ~= t.getPos().f do
  32.     t.turnLeft();
  33.   end
  34.   -- and move back to the previous mining position
  35.   local out = 5 - ((t.getPos()[importantDir] - offset) % 5);
  36.   if out > 0 and out < 5 then
  37.     t.backward(out);
  38.   end
  39. else
  40.   -- first run, do setup
  41.   -- Which direction do we care about?
  42.   local f = t.getPos().f;
  43.   local dir;
  44.   if f == 0 or f == 2 then
  45.     dir = "z";
  46.   else
  47.     dir = "x";
  48.   end
  49.   importantDir = dir;
  50.  
  51.   -- Math out the values we need
  52.   currentChunk = math.floor(t.getPos()[dir] / 16);
  53.   offset = t.getPos()[dir] % 5;
  54.   mineFacing = t.getPos().f;
  55.   local f = fs.open(locFile,"w");
  56.   f.writeLine(currentChunk);
  57.   f.writeLine(offset);
  58.   f.writeLine(mineFacing);
  59.   f.writeLine(importantDir);
  60.   f.close();
  61. end
  62.  
  63. function ready()
  64.   local packet = {
  65.     ['type'] = "ROUTED_PACKET",
  66.     ['src']  = os.getComputerID(),
  67.     ['dest'] = controllerID or rednet.CHANNEL_BROADCAST,
  68.     ['data'] = "ready"
  69.   }
  70.   print("Send to " .. packet.dest);
  71.   rednet.sendPacket(packet);
  72.  
  73.   local recvd = false;
  74.   local timer = os.startTimer(5);
  75.   while true do
  76.     local evt = {os.pullEvent()};
  77.     if evt[1] == "timer" and evt[2] == timer then
  78.       timer = os.startTimer(5);
  79.       if not recvd then
  80.       print("resend");
  81.         rednet.sendPacket(packet);
  82.       end
  83.     end
  84.     if evt[1] == "packet_recv" then
  85.       if evt[2].type == "ACK" and evt[2].dest == os.getComputerID() then
  86.         recvd = true;
  87.         controllerID = evt[2].src;
  88.         local f = fs.open(CIDFile,"w");
  89.         f.writeLine(controllerID);
  90.         f.close();
  91.       end
  92.       if evt[2].type == "ROUTED_PACKET" and evt[2].data == "mine" and evt[2].src == controllerID then
  93.         local resp = {
  94.           ['type'] = "ACK",
  95.           ['src']  = os.getComputerID(),
  96.           ['dest'] = controllerID,
  97.           ['data'] = "mine"
  98.         }
  99.         print("Sending ack for mine");
  100.         rednet.sendPacket(resp);
  101.         return;
  102.       end
  103.     end
  104.   end
  105. end
  106.  
  107.  
  108. local fwd = 0;
  109. function mine()
  110.   local depth = 0;
  111.   while t.down(1, true) do
  112.     depth = depth + 1;
  113.     t.dig();
  114.     t.turnLeft();
  115.     t.dig();
  116.     t.turnLeft();
  117.     t.dig();
  118.     t.turnLeft();
  119.     t.dig();
  120.     t.turnLeft();
  121.   end
  122.   t.up(depth);
  123.   t.forward(5);
  124.   local cChunk = math.floor(t.getPos()[importantDir] / 16);
  125.   if cChunk > currentChunk then
  126.     local f = fs.open(locFile,"w");
  127.     f.writeLine(cChunk);
  128.     f.writeLine(offset);
  129.     f.writeLine(mineFacing);
  130.     f.writeLine(importantDir);
  131.     f.close();
  132.     return;
  133.   end
  134. end
  135.  
  136. while true do
  137.   ready();
  138.   mine();
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement