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

AutoBase

Started by CtrlAltElite, 19 May 2013 - 04:11 PM
CtrlAltElite #1
Posted 19 May 2013 - 06:11 PM
Hello! I am writing a program that will build your base to whatever scale you want. I really am not good with lua, but I know java and C++ so it will come fast. I was wondering if someone here could point me to a tutorial, or demonstrate a way to make a turtle return to where it started. I was thinking something like this:

--[SkidProof]--
xpos = 0
ypos = 0
zpos = 0
repeat
if turtle.turnsRight() then
xxxxxxxx
end
if turtle.goesup() then
ypos = ypos + 1
end
if turtle.goesForward()  then
xpos = xpos + 1
end
until programEnds()

I was wondering how I would make it switch to the zpos variable and not add to the xpos. I am sure there is an easier way, if there is please let me know! I really dont want to make a GPS system, id rather be able to code it into the turtle for if I want to use it w/o much resources.

Thank you for your time, CtrlAltElite

P.S. IF you are a trusted rank and want to help me in game, Ill start up a server and PM you the address.
W00dyR #2
Posted 19 May 2013 - 06:21 PM
You have to make a new variable that reads what way you are facing.

Most simple way of doing it that I can think of pretty much is:

- Make a variable with a numeric value (1, 2, 3 and 4 = all four sides it can possibly face, one represents north, other one east, etc.)
- Depending on that value, it changes the variable to its new facing side when it turns right or left
- Depending on what side its facing (reading the variable when it does turtle.forward() ) it will either add to xpos, substract from xpos, add to zpos, or substract from zpos.

I could code the entire thing, but I'm sure you are talented enough to figure that out yourself with these simple instructions :)/>
CtrlAltElite #3
Posted 19 May 2013 - 06:36 PM
You have to make a new variable that reads what way you are facing.

Most simple way of doing it that I can think of pretty much is:

- Make a variable with a numeric value (1, 2, 3 and 4 = all four sides it can possibly face, one represents north, other one east, etc.)
- Depending on that value, it changes the variable to its new facing side when it turns right or left
- Depending on what side its facing (reading the variable when it does turtle.forward() ) it will either add to xpos, substract from xpos, add to zpos, or substract from zpos.

I could code the entire thing, but I'm sure you are talented enough to figure that out yourself with these simple instructions :)/>


Great Idea! So maybe something like this?

local 1  --[  north  ]--
local 2  --[  east   ]--
local 3  --[  south ]--
local 4  --[  west  ]--
local xPos
local yPos
local zPos
x = 1
repeat
if turtle.turnRight() then
   x = x + 1
end
if x > 4 then
   x = 1
end
if turtle.forward() and x ==1 then
   xpos = xpos + 1
end
if turtle.forward() and x ==2 then
   zPos = xPos + 1
end
if turtle.forward() and x ==3 then
   xPos = xPos - 1
end
if turtle.forward() and x == 4 then
   zPos = zPos - 1
until programEnds()

--[ Note It only turns Right ]--


Now my only problem would be having it return back to where it came. I have no clue how I might go about coding that… Any ideas on your side?
W00dyR #4
Posted 19 May 2013 - 07:11 PM
You have to make a new variable that reads what way you are facing.

Most simple way of doing it that I can think of pretty much is:

- Make a variable with a numeric value (1, 2, 3 and 4 = all four sides it can possibly face, one represents north, other one east, etc.)
- Depending on that value, it changes the variable to its new facing side when it turns right or left
- Depending on what side its facing (reading the variable when it does turtle.forward() ) it will either add to xpos, substract from xpos, add to zpos, or substract from zpos.

I could code the entire thing, but I'm sure you are talented enough to figure that out yourself with these simple instructions :)/>


Great Idea! So maybe something like this?
Spoiler

local 1  --[  north  ]--
local 2  --[  east   ]--
local 3  --[  south ]--
local 4  --[  west  ]--
local xPos
local yPos
local zPos
x = 1
repeat
if turtle.turnRight() then
   x = x + 1
end
if x > 4 then
   x = 1
end
if turtle.forward() and x ==1 then
   xpos = xpos + 1
end
if turtle.forward() and x ==2 then
   zPos = xPos + 1
end
if turtle.forward() and x ==3 then
   xPos = xPos - 1
end
if turtle.forward() and x == 4 then
   zPos = zPos - 1
until programEnds()

--[ Note It only turns Right ]--


Now my only problem would be having it return back to where it came. I have no clue how I might go about coding that… Any ideas on your side?

Yes that is the basic of what I ment, just make sure you do declare your variables, because if you dont declare that xPos = 0, then it will error when you say xPos = xPos + 1 . Also make sure you dont do something like on the 2nd check, saying zPos = xPos + 1, but I'm sure thats a quick typo made when typing an example :)/>

For the returning part, you could make another check.

- Check its yPos, then make it go up or down untill it reaches 0 (where it started).
- Check its xPos + what way it faces, then make it turn the way it needs to go (so if it faces west, and it needs to go east to make xPos = 0, turn twice etc)
- Same as above goes for zPos.

Pretty much with the variable that tells you what way it faces, you can perform checks and make it move, and when it moves ofcourse make it edit the position. You could probably make a function for the forward part that makes it easiest.

Something like:


local function moveForward()
  turtle.forward()
  if x == 1 then
	 xPos = xPos + 1
  elseif x == 2 then
	 zPos = zPos + 1
  elseif x == 3 then
	 xPos == xPos - 1
  elseif x = 4 then
	 zPos = zPos - 1
  end
end

That is quick typing, you would have to make sure all the coordinates work and stuff (play around with it inside Minecraft itself works best to figure out the positioning ;)/> )

Also, for the returning a simple loop would do for each position:


repeat
   moveForward()
until xPos == 0

And then ofcourse the check that I mentioned to make it turn the right way, then repeat the thing for yPos