This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SodaSpartan's profile picture

My turtle likes going in circles for some reason!

Started by SodaSpartan, 12 September 2017 - 10:13 PM
SodaSpartan #1
Posted 13 September 2017 - 12:13 AM
I've been working on a 3x3 mining program for my turtle. and yes i know there are plenty out there i could just pastebin but it is sorta a personal goal of mine to make and use my own program. so basically the main function i use to "mine a layer" hence the function mineLayer() lol, should work as far as i can tell but for some reason it just moves in a circle.

its probably something really obvious but i just cant see it.
so here is the code


function refuel()
  turtle.select(1)
  turtle.refuel(1)
end
function digUp()
  while turtle.detectUp() do
	turtle.digUp()
  end
end
function moveUp()
  if turtle.getFuelLevel() < 10 then
	refuel()
  end
  while turtle.up == false do
	digUp()
	turtle.attackUp()
  end
end	
function digForward()
  while turtle.detect() do
	turtle.dig()
  end
end
function moveForward()
  if turtle.getFuelLevel() < 10 then
	refuel()
  end
  while turtle.forward() == false do
	digForward()
	turtle.attack()
  end
end
function turnAround()
  turtle.turnRight(2)
end
function mineLayer()
  moveForward()
  sleep(.5)
  turtle.turnRight()
  moveForward(2)
  turnAround()
  moveUp()
  moveForward(2)
  turnAround()
  moveUp()
  moveForward(2)
  turnAround()
  turtle.down(2)
  moveForward(2)
  turtle.turnRight()
end
while true do
  mineLayer()
end
  

thank you for any input, this is my first attempt at a program so constructive criticism is welcomed :)/>
Bomb Bloke #2
Posted 13 September 2017 - 01:58 AM
Missing brackets here:

  while turtle.up == false do

These functions don't expect arguments:

turtle.turnRight(2)
moveForward(2)
turtle.down(2)

… and will ignore them if they're passed in.

If you want to repeat something a set amount of times, the usual manner is with a for loop:

for i = 1, 2 do turtle.turnRight() end

You could furthermore build functions that do accept arguments, and use them to loop:

local function moveForward( amount )       --# Argument is assigned to "amount" variable.
	if not amount then amount = 1 end  --# If an argument wasn't passed, assume a default value.
	
	while turtle.getFuelLevel() < amount do refuel() end
	
	for i = 1, amount do
		while not turtle.forward() do
			digForward()
			turtle.attack()
		end
	end
end

.
.
.

moveForward(2)  --# Argument is 2: moves forward two spaces.
moveForward()   --# No argument: moves forward one space.
SodaSpartan #3
Posted 13 September 2017 - 02:19 AM
while waiting for my topic to appear in the forum i kept doing research and ended up with this


function refuel()
  turtle.select(1)
  turtle.refuel(1)
end
function digUp()
  while turtle.detectUp() do
	turtle.digUp()
  end
end
function moveUp()
  if turtle.getFuelLevel() < 10 then
	refuel()
  end
  while turtle.up() == false do
	digUp()
	turtle.attackUp()
  end
end	
function digForward()
  while turtle.detect() do
	turtle.dig()
  end
end
function moveForward()
  if turtle.getFuelLevel() < 10 then
	refuel()
  end
  while turtle.forward() == false do
	digForward()
	turtle.attack()
  end
end
function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end
function mineLayer()
  moveForward()
  turtle.turnRight()
  moveForward()
  moveForward()
  turnAround()
  moveUp()
  moveForward()
  moveForward()
  turnAround()
  moveUp()
  moveForward()
  moveForward()
  turnAround()
  turtle.down()
  turtle.down()
  moveForward()
  moveForward()
  turtle.turnRight()
end
print("Dig forward how many layers?")
local x = tonumber( read())
function mine()
for i = 1,(x) do
  mineLayer()
end
end
mine()

also i noticed the missing brackets immediately after posting but i couldn't find my post lol.
i will definitely use your suggestions to improve upon it. thank you very much

also i notice in front of the function you put local. is there a difference between doing that and just putting function like i did? just trying to understand some of this better
Edited on 13 September 2017 - 12:24 AM
KingofGamesYami #4
Posted 13 September 2017 - 03:14 AM
local functions (and variables in general) don't end up in the global scope. This means they are deleted after the program's execution, and can't be replaced by other scripts which may be running at the same time. It's generally considered good practice to use local when declaring variables, however you should read up on how scope works before using it a lot.
SodaSpartan #5
Posted 13 September 2017 - 04:13 AM
thank you. one more thing, i know this is unrelated to the topic (sorry) but I'm trying to make it check to see if there is something in the 16th inventory slot, if true put down a chest which will be in slot 4 and drop everything in slot 5 -16 into said chest. I know I'm asking a lot on this one but i greatly appreciate the help :)/>

function refuel()
  turtle.select(1)
  turtle.refuel(1)
end
function digUp()
  while turtle.detectUp() do
    turtle.digUp()
  end
end
function moveUp()
  if turtle.getFuelLevel() < 10 then
    refuel()
  end
  while not turtle.up() do
    digUp()
    turtle.attackUp()
  end
end	 
function digForward()
  while turtle.detect() do
    turtle.dig()
  end
end
local function moveForward(amount)
if not amount then amount = 1 end
  if turtle.getFuelLevel() < 10 then
    refuel()
  end
  for i = 1, amount do
  while not turtle.forward() do
    digForward()
    turtle.attack()
  end
end
end
function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end
function placeBlock()
while not turtle.detect() do
  turtle.select(3)
  turtle.place()
end
end
function placeBlockUp()
while not turtle.detectUp() do
  turtle.select(3)
  turtle.placeUp()
end
end
function placeBlockDown()
while not turtle.detectDown() do
  turtle.select(3)
  turtle.placeDown()
end
end
function placeTorch()
turtle.select(2)
turtle.turnRight()
while not turtle.detect() do
  turtle.place()
end
turtle.turnLeft()
end
local z = 0
local function mineLayer()
  moveForward()
  turtle.turnRight()
  placeBlockDown()
  moveForward()
  placeBlockDown()
  moveForward()
  placeBlockDown()
  placeBlock()
  moveUp()
  placeBlock()
  turnAround()
  moveForward(2)
  placeBlock()
  moveUp()
  placeBlock()
  turnAround()
  placeBlockUp()
  moveForward()
  placeBlockUp()
  moveForward()
  placeBlockUp()
  placeBlock()
  turtle.down()
  turtle.down()
  turnAround()
  moveForward(2)
  placeBlock()
  turtle.turnRight()
  z = z + 1
if z == 7 then
  placeTorch()
  z = 0
end
end
function layerStack(amount)
amount = 7
for i=1, (amount) do
  mineLayer()
end
end
print("Dig forward how many iterations? (iterations of 7)")
local x = tonumber( read())
function mine()
for i = 1,(x) do
  layerStack()
end
end
mine()

also that scope thing helped alot with getting the torch placing interval to work thank you KingofGamesYami
Edited on 13 September 2017 - 06:02 AM
SodaSpartan #6
Posted 13 September 2017 - 08:52 AM
i uhh.. kept working on this while waiting for replies and uhh sorta finished it more or less.

here is the final code


function refuel()
  turtle.select(1)
  turtle.refuel(1)
end
function digUp()
  while turtle.detectUp() do
    turtle.digUp()
  end
end
function moveUp()
  if turtle.getFuelLevel() < 10 then
    refuel()
  end
  while not turtle.up() do
    digUp()
    turtle.attackUp()
  end
end	 
function digForward()
  while turtle.detect() do
    turtle.dig()
  end
end
local function moveForward(amount)
if not amount then amount = 1 end
  if turtle.getFuelLevel() < 10 then
    refuel()
  end
  for i = 1, amount do
  while not turtle.forward() do
    digForward()
    turtle.attack()
  end
end
end
function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end
function placeBlock()
while not turtle.detect() do
  turtle.select(3)
  turtle.place()
end
end
function placeBlockUp()
while not turtle.detectUp() do
  turtle.select(3)
  turtle.placeUp()
end
end
function placeBlockDown()
while not turtle.detectDown() do
  turtle.select(3)
  turtle.placeDown()
end
end
function placeTorch()
turtle.select(2)
turtle.turnRight()
while not turtle.detect() do
  turtle.place()
end
turtle.turnLeft()
end
function placeChest()
turtle.select(4)
turnAround()
turtle.place()
end
function invDump()
  placeChest()
for i = 16,5,-1 do
  turtle.select(i)
  turtle.drop()
end
turnAround()
end
local z = 0
local function mineLayer()
  moveForward()
  turtle.turnRight()
  placeBlockDown()
  moveForward()
  placeBlockDown()
  moveForward()
  placeBlockDown()
  placeBlock()
  moveUp()
  placeBlock()
  turnAround()
  moveForward(2)
  placeBlock()
  moveUp()
  placeBlock()
  turnAround()
  placeBlockUp()
  moveForward()
  placeBlockUp()
  moveForward()
  placeBlockUp()
  placeBlock()
  turtle.down()
  turtle.down()
  turnAround()
  moveForward(2)
  placeBlock()
  turtle.turnRight()
  z = z + 1
if z == 7 then
  placeTorch()
  z = 0
end
if turtle.getItemCount(16) >= 1 then
  invDump()
end
end
function layerStack(amount)
amount = 7
for i=1, (amount) do
  mineLayer()
end
end
print("Dig forward how many iterations? (iterations of 7)")
local x = tonumber( read())
function mine()
for i = 1,(x) do
  layerStack()
end
end
mine()

there is a few touch ups i can do but for now as long as i have coal, torches, cobblestone and chests in slots 1,2,3 and 4 respectively it runs great! i will change out the chest for an ender chest later and have it place the chest, drop inventory 5 through 16 then collect chest again

thanks again for the help before, the purpose of making this program was to learn how to use lua better in computercraft and i learned alot!
Dave-ee Jones #7
Posted 15 September 2017 - 06:33 AM
Wait. Does that mean only the turtle can see what's in the ender chest if it's the one to place down the chest, not you? :blink:/> *mind blown*
DevelopedLogic #8
Posted 15 September 2017 - 07:04 AM
Wait. Does that mean only the turtle can see what's in the ender chest if it's the one to place down the chest, not you? :blink:/> *mind blown*

I assume not, as I believe turtles take actions on behalf of the player who placed them, meaning if a turtle breaks a block it is usually logged on a server as the player, and not the turtle. This, I assume, means they access your ender chest too.
Dave-ee Jones #9
Posted 15 September 2017 - 07:45 AM
Wait. Does that mean only the turtle can see what's in the ender chest if it's the one to place down the chest, not you? :blink:/> *mind blown*

I assume not, as I believe turtles take actions on behalf of the player who placed them, meaning if a turtle breaks a block it is usually logged on a server as the player, and not the turtle. This, I assume, means they access your ender chest too.

Yea, I was joking around but still curious as to whether it was viable or not. It's a weird issue that might not have been thought about before. Although I do remember this vague thing where things couldn't place ender chests, but they could place normal chests. This was perhaps because of this issue. Not sure.
Lupus590 #10
Posted 15 September 2017 - 10:44 AM
Turtles use a fake player and are not bound to the player who placed them in any way.

If turtles can access (vanilla) ender chests then they will access the fake players chest.