Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local runtimeArgs = { ... };
- local isReturningHome = false;
- local isTerminating = false;
- xPos = 0;
- yPos = 0;
- zPos = 0;
- curDir = 0;
- directions = {
- [0] = "forward",
- [1] = "right",
- [2] = "back",
- [3] = "left"
- }
- turnDirs = {
- right = turtle.turnRight,
- left = turtle.turnLeft
- }
- --[[function digDownSpecial()
- turtle.digDown();
- end]]
- digDirs = {
- up = turtle.digUp,
- down = turtle.digDown,
- forward = turtle.dig,
- back = turtle.dig,
- left = turtle.dig,
- right = turtle.dig
- }
- moveDirs = {
- forward = turtle.forward,
- back = turtle.back,
- up = turtle.up,
- down = turtle.down,
- }
- function printCurDir()
- --print( "[+] Current Direction: " .. directions[curDir] .. " [-]");
- end
- function printCurPos()
- --print( "[+] Current Pos:");
- --print( " XPos- " .. xPos );
- --print( " YPos- " .. yPos );
- --print( "[-]" );
- end
- function printCurrentFuel()
- --print( "[+] Current Fuel Level: " .. turtle.getFuelLevel() .. " [-]");
- end
- function selectItem( itemID )
- --print( "[+] Selecting Item..." );
- for i = 1, 16, 1 do
- curItem = turtle.getItemDetail( i ) or { name = "Empty" };
- --print( "Checking Slot: " .. i );
- --print( " - " .. curItem.name );
- if( curItem.name == itemID ) then
- turtle.select( i );
- --print( "[-] Selected Item." );
- return true;
- end
- end
- --print( "[-!] Failed Select Item." );
- return false;
- end
- function terminateProgram()
- if( isTerminating ) then return end
- isTerminating = true;
- print( "!!! Terminating Program !!!" );
- print( "!!! Returning Home !!!" );
- returnHome();
- print( "!!! Terminated Program !!!" );
- sleep(1);
- os.queueEvent( "terminate" );
- sleep(1);
- os.queueEvent( "terminate" );
- end
- local fuelPerCoal = 0;
- function refuel()
- --printCurrentFuel();
- --print( "[+] Refueling..." );
- local returnHomeFuelAmount = math.abs( xPos ) + math.abs( yPos ) + math.abs( zPos ) + 5;
- local numCoal = 0;
- if( selectItem( "minecraft:coal" ) ) then
- numCoal = turtle.getItemCount();
- end
- local curFuel = turtle.getFuelLevel();
- local shouldRefuel = curFuel <= returnHomeFuelAmount;
- if( fuelPerCoal == 0 and turtle.refuel(1) ) then
- fuelPerCoal = turtle.getFuelLevel() - curFuel;
- curFuel = turtle.getFuelLevel();
- end
- if( numCoal * fuelPerCoal <= returnHomeFuelAmount ) then
- terminateProgram();
- end
- if( shouldRefuel ) then
- if( turtle.refuel(1) ) then
- --print( "[-] Refueled" );
- else
- --print( "[-!] Failed Refuel" );
- while( not turtle.refuel(1) and ( not isReturningHome or curFuel <= 0 ) ) do
- sleep( 0.5 );
- print( "!!! OUT OF FUEL !!!" );
- selectItem( "minecraft:coal" );
- end
- --terminateProgram();
- end
- else
- --print( "[-] Refuel Not Needed" );
- end
- --printCurrentFuel()
- end
- function directionValFix( dir )
- return math.abs( dir % 4 );
- end
- function turn( dir )
- --printCurDir()
- --print( "[+] Turning: " .. dir .. "..." );
- dirVal = 1;
- if( dir == "left" ) then
- dirVal = -dirVal;
- end
- curDir = directionValFix( curDir + dirVal );
- turnDirs[dir]();
- --print( "[-] Turned: " .. dir .. "." );
- --printCurDir()
- end
- local stopAddingToHistory = false;
- local moveHistory = {};
- function move( dir )
- refuel();
- dropOffMaterials();
- --printCurPos();
- --print( "[+] Moveing: " .. dir .. "..." );
- function actualMove( direct )
- digDirs[dir]();
- if( dir ~= "up" and dir ~= "down" ) then
- if( moveDirs[direct]() ) then
- --Forward
- if( curDir == 0 ) then
- zPos = zPos + 1;
- end
- -- Right
- if( curDir == 1 ) then
- xPos = xPos + 1;
- end
- -- Back
- if( curDir == 2 ) then
- zPos = zPos - 1;
- end
- --Left
- if( curDir == 3 ) then
- xPos = xPos - 1;
- end
- --print( "[-] Moved: " .. dir .. "." );
- else
- --print( "[-!] Failed Moved: " .. dir .. "." );
- end
- else
- if( moveDirs[direct]() ) then
- if( dir == "up" ) then
- yPos = yPos + 1;
- end
- if ( dir == "down" ) then
- yPos = yPos - 1;
- end
- --print( "[-] Moved: " .. dir .. "." );
- else
- --print( "[-!] Failed Moved: " .. dir .. "." );
- end
- end
- end
- if( dir ~= "up" and dir ~= "down" ) then
- if( dir == "back" ) then
- turn( "right" );
- turn( "right" );
- end
- if( dir == "right" or dir == "left" ) then
- turn( dir );
- end
- actualMove( "forward" );
- else
- actualMove( dir );
- end
- if( not stopAddingToHistory ) then
- table.insert( moveHistory, { x = xPos, y = yPos, z = zPos } );
- end
- --printCurPos()
- end
- placeDirs = {
- forward = turtle.place,
- up = turtle.placeUp,
- down = turtle.placeDown
- }
- function build( dir, blockID )
- selectItem( blockID );
- return placeDirs[dir]();
- end
- function faceDirection( dir )
- if( directions[directionValFix( curDir-1 )] == dir ) then
- turn( "left" );
- return;
- end
- for i = 0, 3, 1 do
- if( directions[curDir] == dir ) then
- return;
- end
- turn( "right" );
- end
- end
- function moveDirection( dir )
- if( dir ~= "up" and dir ~= "down" ) then
- faceDirection( dir );
- move( "forward" );
- else
- move( dir );
- end
- end
- function moveTo( x, y, z )
- function axisMove( axis, curVal, toVal )
- if( curVal ~= toVal ) then
- changeVal = 1;
- if( curVal > toVal ) then
- changeVal = -1;
- end
- for curAxisVal = curVal, toVal-changeVal, changeVal do
- if( axis == "x" ) then
- if( changeVal > 0 ) then
- moveDirection( "right" );
- else
- moveDirection( "left" );
- end
- end
- if( axis == "y" ) then
- if( changeVal > 0 ) then
- moveDirection( "up" );
- else
- moveDirection( "down" );
- end
- end
- if( axis == "z" ) then
- if( changeVal > 0 ) then
- moveDirection( "forward" );
- else
- moveDirection( "back" );
- end
- end
- end
- end
- end
- axisMove( "y", yPos, y );
- axisMove( "x", xPos, x );
- axisMove( "z", zPos, z );
- end
- function reverseMove()
- if( #moveHistory == 0 ) then return end
- local lastMove = moveHistory[#moveHistory];
- moveHistory[#moveHistory] = nil;
- moveTo( lastMove.x, lastMove.y, lastMove.z );
- end
- function reverseAllMoves()
- stopAddingToHistory = true;
- while #moveHistory > 0 do
- reverseMove();
- end
- end
- function toBool( val )
- return val == "true" or val == true;
- end
- local buildingBlocks = {
- ["minecraft:cobblestone"] = true,
- ["minecraft:cobbled_deepslate"] = true,
- ["minecraft:netherrack"] = true
- }
- local importantItems = {
- ["minecraft:coal"] = true
- }
- for k, v in pairs(buildingBlocks) do table.insert(importantItems, v) end
- function placeBuildingBlock( dir )
- for key, value in pairs(buildingBlocks) do
- if( build( dir, key ) ) then
- return true;
- end
- end
- return false;
- end
- function returnHome()
- if( isReturningHome ) then return end
- isReturningHome = true;
- reverseAllMoves();
- moveTo( 0, 0, 0 );
- faceDirection( "forward" );
- isReturningHome = false;
- end
- local currentlyDroppingOffMaterials = false;
- function dropOffMaterials()
- if( currentlyDroppingOffMaterials ) then return false end
- local isInventoryFilled = true;
- for i = 1, 16, 1 do
- local curItem = turtle.getItemDetail( i ) or { name = "Empty" };
- if( curItem.name == "Empty" ) then
- isInventoryFilled = false;
- break;
- end
- end
- if( isInventoryFilled ) then
- currentlyDroppingOffMaterials = true;
- local startData = { dir = curDir, x = xPos, y = yPos, z = zPos };
- returnHome();
- local skippedBuildMat = false;
- local skippedCoal = false;
- for i = 1, 16, 1 do
- local curItem = turtle.getItemDetail( i ) or { name = "Empty" };
- local curID = curItem.name;
- local isImportant = importantItems[curID];
- local shouldDrop = isImportant;
- if( shouldDrop ) then
- turtle.select( i );
- turtle.dropDown();
- end
- end
- currentlyDroppingOffMaterials = false;
- moveTo( xPos, yPos, startData.z );
- moveTo( startData.x, yPos, zPos );
- moveTo( xPos, startData.y, zPos );
- faceDirection( directions[startData.dir] );
- end
- end
- -- Start of Program
- local deep = tonumber( runtimeArgs[1] );
- local type = runtimeArgs[2];
- -- Builds stairs down in a spiral pattern
- local function BuildSpiralStairs(deep)
- turn( "right" );
- for i = 1, deep, 1 do
- build( "down", "minecraft:cobblestone" );
- move( "forward" );
- turtle.digUp();
- turn( "right" );
- build( "forward", "minecraft:cobblestone" );
- turn("left")
- turn("left")
- build( "forward", "minecraft:cobblestone" );
- move( "down" );
- build( "down", "minecraft:cobblestone" );
- build( "forward", "minecraft:cobblestone" );
- turn( "right" );
- turn( "right" );
- build( "forward", "minecraft:cobblestone" );
- turn("left")
- move( "forward" );
- turtle.digUp();
- build( "forward", "minecraft:cobblestone" );
- turn( "right" );
- build( "forward", "minecraft:cobblestone" );
- move( "down" );
- build( "forward", "minecraft:cobblestone" );
- build( "down", "minecraft:cobblestone" );
- turn("left")
- build( "forward", "minecraft:cobblestone" );
- turn( "left" );
- end
- end
- local function BuildStrightStairs(deep)
- for i = 1, deep, 1 do
- turtle.digUp();
- build( "down", "minecraft:cobblestone" );
- if(i == 1) then
- turn( "left" );
- end
- build( "forward", "minecraft:cobblestone" );
- turn( "left" );
- turn( "left" );
- stopAddingToHistory = true;
- move( "forward" );
- turtle.digUp();
- build( "down", "minecraft:cobblestone" );
- build( "forward", "minecraft:cobblestone" );
- turn( "left" );
- move( "forward" );
- turtle.digUp();
- turn( "left" );
- move( "forward" );
- turtle.digUp();
- stopAddingToHistory = false;
- move( "down" );
- end
- end
- local StrctureTypes = {
- ["spiral"] = BuildSpiralStairs,
- ["straight"] = BuildStrightStairs
- }
- StrctureTypes[type](deep);
- -- End of Program
- dropOffMaterials();
- returnHome();
- --[[
- - Turns once to either direction
- turn( "left" );
- turn( "right" );
- - Fully turns til it reaches that direction based on the original orientation of the turtle when the program started
- faceDirection( "forward" );
- faceDirection( "back" );
- faceDirection( "right" );
- faceDirection( "left" );
- - Moves turtle based on its current orientation
- move( "forward" );
- move( "back" );
- move( "right" );
- move( "left" );
- move( "up" );
- move( "down" );
- - Moves turtle to inputed cords based on/to the original pos and direction the turtle was when program started
- (Will move on y axis first, then x, and last z.)
- moveTo( x, y, z );
- - Moves turtle in direction based on the original orientation of the turtle when the program started
- moveDirection( "forward" );
- moveDirection( "back" );
- moveDirection( "right" );
- moveDirection( "left" );
- moveDirection( "up" );
- moveDirection( "down" );
- - Searhes in inventory and selects inputed item, if the item doesn't exist it will terminate the program so things don't break.
- selectItem( itemID );
- - places the block in whatever direction based on the turtle's current orientation
- build( "forward", blockID );
- build( "up", blockID );
- build( "down", blockID );
- pastebin run Ymze4E42 5 straight
- pastebin run Ymze4E42 5 spiral
- - To do
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement