Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local CIDFile = "/.cid";
- local locFile = "/.minerLocation";
- local controllerID = nil;
- local currentChunk = 1;
- local offset = 0;
- local mineFacing = 0;
- local importantDir = "";
- -- load cID
- if fs.exists(CIDFile) then
- local f = fs.open(CIDFile,"r");
- controllerID = tonumber(f.readLine());
- f.close();
- end
- if fs.exists(locFile) then
- local f = fs.open(locFile,"r");
- currentChunk = tonumber(f.readLine());
- offset = tonumber(f.readLine());
- mineFacing = tonumber(f.readLine());
- importantDir = f.readLine();
- f.close();
- -- we're resuming. Get set up.
- -- Get to the travel y...
- while t.getPos().y < 80 do
- t.up();
- end
- -- Face the right way
- while mineFacing ~= t.getPos().f do
- t.turnLeft();
- end
- -- and move back to the previous mining position
- local out = 5 - ((t.getPos()[importantDir] - offset) % 5);
- if out > 0 and out < 5 then
- t.backward(out);
- end
- else
- -- first run, do setup
- -- Which direction do we care about?
- local f = t.getPos().f;
- local dir;
- if f == 0 or f == 2 then
- dir = "z";
- else
- dir = "x";
- end
- importantDir = dir;
- -- Math out the values we need
- currentChunk = math.floor(t.getPos()[dir] / 16);
- offset = t.getPos()[dir] % 5;
- mineFacing = t.getPos().f;
- local f = fs.open(locFile,"w");
- f.writeLine(currentChunk);
- f.writeLine(offset);
- f.writeLine(mineFacing);
- f.writeLine(importantDir);
- f.close();
- end
- function ready()
- local packet = {
- ['type'] = "ROUTED_PACKET",
- ['src'] = os.getComputerID(),
- ['dest'] = controllerID or rednet.CHANNEL_BROADCAST,
- ['data'] = "ready"
- }
- print("Send to " .. packet.dest);
- rednet.sendPacket(packet);
- local recvd = false;
- local timer = os.startTimer(5);
- while true do
- local evt = {os.pullEvent()};
- if evt[1] == "timer" and evt[2] == timer then
- timer = os.startTimer(5);
- if not recvd then
- print("resend");
- rednet.sendPacket(packet);
- end
- end
- if evt[1] == "packet_recv" then
- if evt[2].type == "ACK" and evt[2].dest == os.getComputerID() then
- recvd = true;
- controllerID = evt[2].src;
- local f = fs.open(CIDFile,"w");
- f.writeLine(controllerID);
- f.close();
- end
- if evt[2].type == "ROUTED_PACKET" and evt[2].data == "mine" and evt[2].src == controllerID then
- local resp = {
- ['type'] = "ACK",
- ['src'] = os.getComputerID(),
- ['dest'] = controllerID,
- ['data'] = "mine"
- }
- print("Sending ack for mine");
- rednet.sendPacket(resp);
- return;
- end
- end
- end
- end
- local fwd = 0;
- function mine()
- local depth = 0;
- while t.down(1, true) do
- depth = depth + 1;
- t.dig();
- t.turnLeft();
- t.dig();
- t.turnLeft();
- t.dig();
- t.turnLeft();
- t.dig();
- t.turnLeft();
- end
- t.up(depth);
- t.forward(5);
- local cChunk = math.floor(t.getPos()[importantDir] / 16);
- if cChunk > currentChunk then
- local f = fs.open(locFile,"w");
- f.writeLine(cChunk);
- f.writeLine(offset);
- f.writeLine(mineFacing);
- f.writeLine(importantDir);
- f.close();
- return;
- end
- end
- while true do
- ready();
- mine();
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement