RedPuma

test1

Apr 1st, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.06 KB | None | 0 0
  1. -- Computercraft Turtle-Quarry-Program
  2. -- By Marius Graefe
  3.  
  4. -- default values:
  5. local width = 16;
  6. local height = 16;
  7. local depth = 16;
  8. local noCobble = false;
  9.  
  10.  
  11. --------------------------
  12. -- DO NOT EDIT BELOW
  13. --------------------------
  14.  
  15. local chestPos = {x = 0, y = 0, z = 0};
  16. local moveHistory = {};
  17.  
  18.  
  19. local Move = {
  20.     ["Up"] = 0;
  21.     ["Forward"] = 1;
  22.     ["Down"] = 2;
  23.     ["Backward"] = 3;
  24.     ["TurnRight"] = 4;
  25.     ["TurnLeft"] = 5;
  26. };
  27.  
  28. -- copy a table by value
  29. function table.copy(t)
  30.     local t2 = {};
  31.     for k,v in pairs(t) do
  32.         t2[k] = v;
  33.     end
  34.     return t2;
  35. end
  36.  
  37. -- terminate the program
  38. function quit(msg)
  39.     if msg ~= nil then
  40.         print(msg);
  41.     end
  42.     os.exit();
  43. end
  44.  
  45.  
  46. -- call mommy
  47. function callForHelp(msg)
  48.     print( "Help me I'm stuck here. Please free me and type \"continue\". To terminate the program type \"stop\" or \"exit\". To refuel the turtle type \"refuel <n>\"" );
  49.            
  50.     if msg ~= nil then
  51.         print("Error Message was:\n" .. msg);
  52.     end
  53.    
  54.     local ok = false;
  55.     while not ok do
  56.         input = io.read();
  57.         r = input:match("refuel (%d+)");
  58.         if input == "continue" then
  59.             if turtle.getFuelLevel() <= 0 then
  60.                 print("Error: no fuel, put some fuel-items into the inventory and type \"refuel <n>\"");
  61.             else
  62.                 ok = true;
  63.             end
  64.         elseif input == "stop" or input == "exit" then
  65.             quit();
  66.         elseif r ~= nil then
  67.             turtle.refuel(tonumber(r));
  68.             print("Fuel level is now " .. turtle.getFuelLevel());
  69.         else
  70.             print("Unknown Command.");
  71.         end
  72.     end
  73. end
  74.  
  75.  
  76. -- rotate a vector with 3 components to the right/left
  77. function rotateVector(vec, direction)
  78.     local res = {z = vec.z};
  79.     if direction == Move.TurnLeft then
  80.         res.x = -vec.y;
  81.         res.y = vec.x;
  82.     elseif direction == Move.TurnRight then
  83.         res.x = vec.y;
  84.         res.y = -vec.x;
  85.     else
  86.         error("rotateVector: no turn direction: " .. direction);
  87.     end
  88.     return res;
  89. end
  90.  
  91.  
  92. -- move vector by offset
  93. function moveVector(vec, offset)
  94.     local res = {};
  95.     res.x = vec.x + offset.x;
  96.     res.y = vec.y + offset.y;
  97.     res.z = vec.z + offset.z;
  98.     return res;
  99. end
  100.  
  101.  
  102. -- throw all items that are equal to the first inventory slot out
  103. function clearInventory()
  104.     turtle.select(1);
  105.     for i = 2, 16 do
  106.         if turtle.compareTo(i) then
  107.             turtle.select(i);
  108.             turtle.dropDown();
  109.             turtle.select(1);
  110.         end
  111.     end
  112. end
  113.  
  114.  
  115. -- dig forward, does NOT guarrantee that there is no block in front afterwards
  116. function digAll(bForce)
  117.     if bForce ~= nil and bForce == true then
  118.         turtle.dig();
  119.     end
  120.     while turtle.detect() do turtle.dig(); end
  121. end
  122.  
  123.  
  124. -- dig up, does NOT guarrantee that there is no block upwards afterwards
  125. function digUpAll(bForce)
  126.     if bForce ~= nil and bForce == true then
  127.         turtle.digUp();
  128.     end
  129.     while turtle.detectUp() do turtle.digUp(); end
  130. end
  131.  
  132.  
  133. -- dig down, does NOT guarrantee that there is no block downwards afterwards
  134. function digDownAll(bForce)
  135.     if bForce ~= nil and bForce == true then
  136.         turtle.digDown();
  137.     end
  138.     while turtle.detectDown() do turtle.digDown(); end
  139. end
  140.  
  141.  
  142. -- call digUpAll() / digAll() / digDownAll() for direction equal to 1/2/3
  143. function digDir(direction, bForce)
  144.     if direction == Move.Up then
  145.         digUpAll(bForce);
  146.     elseif direction == Move.Forward then
  147.         digAll(bForce);
  148.     elseif direction == Move.Down then
  149.         digDownAll(bForce);
  150.     elseif direction == Move.Backward then
  151.         turnLeft();
  152.         turnLeft();
  153.         digAll(bForce);
  154.         turnRight();
  155.         turnRight();
  156.     else
  157.         error("digDir: no such direction: " .. direction);
  158.     end
  159. end
  160.  
  161.  
  162. -- call attack functions
  163. function attackDir(direction)
  164.     for i = 1, 10 do
  165.         if direction == Move.Up then
  166.             turtle.attackUp();
  167.         elseif direction == Move.Forward then
  168.             turtle.attack();
  169.         elseif direction == Move.Down then
  170.             turtle.attachDown();
  171.         elseif direction == Move.Backward then
  172.             turnLeft();
  173.             turnLeft();
  174.             turtle.attack();
  175.             turnRight();
  176.             turnRight();
  177.         else
  178.             error("attackDir: no such direction: " .. direction);
  179.         end
  180.     end
  181. end
  182.  
  183.  
  184. -- add to history
  185. function addHistory(move)
  186.     table.insert(moveHistory, move);
  187. end
  188.  
  189.  
  190. -- get the appropriate turtle function
  191. function getMoveFunction(direction)
  192.     local moveFunction = nil;
  193.     if direction == Move.Up then
  194.         moveFunction = turtle.up;
  195.     elseif direction == Move.Forward then
  196.         moveFunction = turtle.forward;
  197.     elseif direction == Move.Down then
  198.         moveFunction = turtle.down;
  199.     elseif direction == Move.Backward then
  200.         moveFunction = turtle.back;
  201.     else
  202.         error("getMoveFunction: No such direction: " .. direction);
  203.     end
  204.     return moveFunction;
  205. end
  206.  
  207.  
  208. -- move the chest position locally
  209. function moveChestPos(direction)
  210.     if direction == Move.Up then
  211.         chestPos = moveVector(chestPos, {x = 0, y = 0, z = -1});
  212.     elseif direction == Move.Down then
  213.         chestPos = moveVector(chestPos, {x = 0, y = 0, z = 1});
  214.     elseif direction == Move.Forward then
  215.         chestPos = moveVector(chestPos, {x = -1, y = 0, z = 0});
  216.     elseif direction == Move.Backward then
  217.         chestPos = moveVector(chestPos, {x = 1, y = 0, z = 0});
  218.     elseif direction == Move.TurnRight then
  219.         chestPos = rotateVector(chestPos, Move.TurnLeft);
  220.     elseif direction == Move.TurnLeft then
  221.         chestPos = rotateVector(chestPos, Move.TurnRight);
  222.     else
  223.         error("Unknown moveChest command: " .. direction);
  224.     end
  225.     -- print("chestPos is " .. chestPos.x .. " " .. chestPos.y .. " " .. chestPos.z .. ".");
  226. end
  227.  
  228.  
  229. -- move the turtle and update history
  230. function move(direction, dist)
  231.     local dist = dist or 1;
  232.     local moveFunction = getMoveFunction(direction);
  233.     for i = 1, dist do
  234.         local digCount = 0;
  235.         while not moveFunction() do
  236.             digCount = digCount + 1;
  237.             digDir(direction, i > 10);
  238.             if digCount%10 == 0 then
  239.                 attackDir(direction);
  240.             end
  241.             if digCount > 256 then
  242.                 callForHelp("Cannot move anymore...");
  243.             end
  244.         end
  245.         addHistory(direction);
  246.         moveChestPos(direction);
  247.     end
  248. end
  249.  
  250.  
  251. -- move the turtle up (guaranteed) and update history
  252. function up(dist)
  253.     move(Move.Up, dist);
  254. end
  255.  
  256.  
  257. -- move the turtle down (guaranteed) and update history
  258. function down(dist)
  259.     move(Move.Down, dist);
  260. end
  261.  
  262.  
  263. -- move the turtle forward (guaranteed) and update history
  264. function forward(dist)
  265.     move(Move.Forward, dist);
  266. end
  267.  
  268.  
  269. -- move the turtle forward (guaranteed) and update history
  270. function backward(dist)
  271.     move(Move.Backward, dist);
  272. end
  273.  
  274.  
  275. -- get the appropriate turtle function
  276. function getTurnFunction(direction)
  277.     if direction == Move.TurnRight then
  278.         return turtle.turnRight;
  279.     elseif direction == Move.TurnLeft then
  280.         return turtle.turnLeft;
  281.     else
  282.         error("No such turning direction: " .. direction);
  283.         return nil;
  284.     end
  285. end
  286.  
  287.  
  288. -- turn and update history
  289. function turn(direction)
  290.     local turnFunction = getTurnFunction(direction);
  291.     turnFunction();
  292.     addHistory(direction);
  293.     moveChestPos(direction);
  294. end
  295.  
  296.  
  297. -- turn left and update history
  298. function turnLeft()
  299.     turn(Move.TurnLeft);
  300. end
  301.  
  302.  
  303. -- turn right and update history
  304. function turnRight()
  305.     turn(Move.TurnRight);
  306. end
  307.  
  308.  
  309. -- dig the three blocks up/front/down and move forward
  310. function dig3()
  311.     digUpAll();
  312.     digAll();
  313.     digDownAll();
  314.     forward();
  315. end
  316.  
  317.  
  318. -- is the inventory full?
  319. function isInventoryFull()
  320.     for i = 1, 16 do
  321.         if turtle.getItemCount(i) == 0 then
  322.             return false;
  323.         end
  324.     end
  325.     return true;
  326. end
  327.  
  328.  
  329. -- 180 degree turn, does not move if bNoMove is true
  330. function turn180(direction, bNoMove)
  331.     turn(direction);
  332.     if not bNoMove then
  333.         dig3();
  334.     end
  335.     turn(direction);
  336. end
  337.  
  338.  
  339. -- move to position in local coordinates
  340. function moveToPos(vec)
  341.     -- move along z
  342.     if vec.z > 0 then
  343.         up(vec.z);
  344.     else
  345.         down(-vec.z);
  346.     end
  347.        
  348.     -- move along x
  349.     if vec.x < 0 then
  350.         turnLeft();
  351.         turnLeft();
  352.     end
  353.     forward(math.abs(vec.x));
  354.     if vec.x < 0 then
  355.         turnLeft();
  356.         turnLeft();
  357.     end
  358.    
  359.     -- move along y
  360.     if vec.y < 0 then
  361.         turnRight();
  362.     else
  363.         turnLeft();
  364.     end
  365.    
  366.     forward(math.abs(vec.y));
  367. end
  368.  
  369.  
  370. -- vomit inventory upwards
  371. function vomitInventory()
  372.     local start = noCobble and 2 or 1;
  373.     for i = start, 16 do
  374.         if turtle.getItemCount(i) > 0 then
  375.             turtle.select(i);
  376.             if not turtle.dropUp() then
  377.                 callForHelp("Chest is full");
  378.             end
  379.         end
  380.     end
  381.     turtle.select(1);
  382. end
  383.  
  384.  
  385. -- move along history
  386. function moveHistoryBackwards(history)
  387.     for i = #history, 1, -1 do
  388.         if history[i] == Move.Up then
  389.             down();
  390.         elseif history[i] == Move.Down then
  391.             up();
  392.         elseif history[i] == Move.Forward then
  393.             backward();
  394.         elseif history[i] == Move.Backward then
  395.             forward();
  396.         elseif history[i] == Move.TurnRight then
  397.             turnLeft();
  398.         elseif history[i] == Move.TurnLeft then
  399.             turnRight();
  400.         else
  401.             error("Unknown history command: " .. history[i]);
  402.         end
  403.     end
  404. end
  405.  
  406.  
  407. -- move to chest, unload inventory and return to quarry
  408. function unload(bNoReturnToQuarry)
  409.     print("Inventory Full, moving to chest");
  410.     moveHistory = {}; -- reset move history
  411.    
  412.     local oldChestPos = table.copy(chestPos);
  413.     moveToPos(oldChestPos);
  414.    
  415.     vomitInventory();
  416.    
  417.     if bNoReturnToQuarry == nil or bNoReturnToQuarry == false then
  418.         print("Moving back to quarry");
  419.         local oldHistory = table.copy(moveHistory);
  420.         moveHistoryBackwards(oldHistory);
  421.     end
  422. end
  423.  
  424.  
  425. function getFuelForUnload()
  426.     return math.abs(chestPos.x) + math.abs(chestPos.y) + math.abs(chestPos.z) + 1;
  427. end
  428.  
  429.  
  430. function mainProg(x, y, z)
  431.     print("starting quarry size " .. x .. " " .. y .. " " .. z .. ".");
  432.    
  433.     local lastTurnLeft = true;
  434.     cobbleCounter = 0;
  435.    
  436.     for i = 1, z/3 do
  437.         down((i==1) and 2 or 3);
  438.         for j = 1, y do
  439.             for k = 1, x do
  440.                 if isInventoryFull() then
  441.                     unload();
  442.                 end
  443.                 dig3();
  444.                 cobbleCounter = cobbleCounter + 3;
  445.                 if noCobble and cobbleCounter >= 45 then
  446.                     clearInventory();
  447.                     cobbleCounter = 0;
  448.                 end
  449.                 if turtle.getFuelLevel() <= getFuelForUnload() then
  450.                     unload(true);
  451.                     callForHelp("No more fuel.");
  452.                 end
  453.             end
  454.             turn180(lastTurnLeft and Move.TurnRight or Move.TurnLeft, j == n);
  455.             lastTurnLeft = not lastTurnLeft;
  456.         end
  457.         lastTurnLeft = not lastTurnLeft;
  458.     end
  459.     digUpAll();
  460.     digDownAll();
  461.     unload(true);
  462. end
  463.  
  464.  
  465. function startProgram()
  466.     print("Program by Marius Graefe");
  467.     print("Type help for help\n");
  468.    
  469.     while true do
  470.         input = io.read();
  471.         local w = input:match("width (%d+)");
  472.         local h = input:match("height (%d+)");
  473.         local d = input:match("depth (%d+)");
  474.         local c = input:match("nocobble (%a+)");
  475.         local r = input:match("refuel (%d+)");
  476.         if input == "help" then
  477.             displayHelp();
  478.         elseif input == "status" then
  479.             displayStatus();
  480.         elseif w ~= nil then
  481.             width = tonumber(w);
  482.             print("width is now " .. width);
  483.         elseif h ~= nil then
  484.             height = tonumber(h);
  485.             print("height is now " .. height);
  486.         elseif d ~= nil then
  487.             depth = d;
  488.             print("depth is now " .. depth);
  489.         elseif r ~= nil then
  490.             turtle.refuel(tonumber(r));
  491.             print("Fuel level is now " .. turtle.getFuelLevel());
  492.         elseif input == "start" then
  493.             if turtle.getFuelLevel() <= 0 then
  494.                 print("Error: no fuel, put some fuel-items into the inventory and type \"refuel <n>\"");
  495.             else
  496.                 mainProg(width, height, depth);
  497.             end
  498.         elseif input == "stop" or input == "exit" then
  499.             quit();
  500.         elseif c ~= nil and (c == "on" or c == "off") then
  501.             noCobble = c == "on";
  502.             print(noCobble and "No-Cobble-Mode ON" or "No-Cobble-Mode OFF");
  503.         else
  504.             print("unknown command, type help for help");
  505.         end
  506.     end
  507. end
  508.  
  509.  
  510. function displayStatus()
  511.     print("Quarry width: " .. width);
  512.     print("Quarry height: " .. height);
  513.     print("Quarry depth: " .. depth);
  514.     print("No Cobble Mode " .. (noCobble and "ON" or "OFF"));
  515. end
  516.  
  517.  
  518. function pressEnter()
  519.     io.write("[Enter]");
  520.     io.read();
  521. end
  522.  
  523.  
  524. function displayHelp()
  525.     print("Commands:");
  526.     print("  status\ndisplay status");
  527.     print("  start\nstarts digging");
  528.     print("  stop/exit\nterminates program");
  529.     print("  refuel <n>\nrefuel the turtle");
  530.     pressEnter();
  531.     print("  width <number>\nspecify width of quarry, i. e. how much the turtle digs forward");
  532.     print("  height <number>\nspecify height of quarry, i.e. how much the turtle digs right");
  533.     print("  depth <number>\nspecify depth of quarry, i.e. how much the turtle digs downwards");
  534.     pressEnter();
  535.     print("  nocobble on/off\nspecify if the turtle should not gather items equal to the item in the first slot");
  536. end
  537.  
  538.  
  539. startProgram();
Advertisement
Add Comment
Please, Sign In to add comment