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

[Ask A Pro] Torus Troubles.

Started by JustIan12, 26 October 2016 - 03:06 PM
JustIan12 #1
Posted 26 October 2016 - 05:06 PM
So, I can't Build a torus without making mistakes so I get a handy dandy turtle to help and I am getting a error. It is quite complex because I could not find a way to redefine the function t later on in the code.


So, the code works in sections

1A, 1B, 2A, 2B and 1AA

The order it goes in now is 1A > 1B > 2A > 2B (Should go to 1AA then 1B) but it goes to 2B again… And I don't know why…
please halp also any improvements or tips would be greatly appreciated.

INSTALLER

term.clear()
term.setCursorPos(1,1)
print("Installing Very Complex Base Structure")
term.clear()
term.setCursorPos(1,1)
shell.run("pastebin get 07Z3mV6z 1A")
term.clear()
term.setTextColor(colors.green)
term.setCursorPos(1,1)
print("Done (1A)")
term.setCursorPos(1,5)
print("20%")
sleep(1)
shell.run("pastebin get m23gNpWJ 1B")
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.green)
print("Done (1B)")
term.setCursorPos(1,5)
term.clearLine()
print("40%")
sleep(1)
shell.run("pastebin get mCqfNDNp 2A")
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.green)
print("Done (2A)")
term.setCursorPos(1,5)
term.clearLine()
print("60%")
sleep(1)
shell.run("pastebin get HS8pPMam 2B")
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.green)
print("Done (2B)")
term.setCursorPos(1,5)
term.clearLine()
print("80%")
sleep(1)
shell.run("pastebin get HS8pPMam 1AA")
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.green)
print("Done (1A (Modified))")
term.setCursorPos(1,5)
term.clearLine()
print("100%")
sleep(1)
-- Refuel
term.setCursorPos(1,1)
term.clear()
term.setTextColor(colors.white)
print("Please Insert Fuel Into My First Slot")
sleep(5)
turtle.refuel()
term.clear()
term.setCursorPos(1,1)
print("Please Insert Building Material Into My Slots")
sleep(15)
-- Run
shell.run("1A")

1A

term.clear()
term.setCursorPos(1,1)
print("Building of Torus starting...")
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
-- Functions
local function t()
turtle.turnLeft()
turtle.back()
turtle.turnRight()
end

local function five()
local n = 5
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function four()
local n = 4
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function three()
local n = 3
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function two()
local n = 2
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function one()
local n = 1
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
-- 1A
five()
four()
three()
three()
one()
two()
one()
two()
one()
one()
one()
one()
one()
shell.run("1B")

1B

term.clear()
term.setCursorPos(1,1)
print("1B")
-- Functions
local function t()
turtle.turnRight()
turtle.back()
turtle.turnLeft()
end

local function five()
local n = 5
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function four()
local n = 4
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function three()
local n = 3
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function two()
local n = 2
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function one()
local n = 1
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
-- 1B
turtle.turnLeft()
two()
one()
two()
one()
three()
three()
four()
five()
turtle.turnRight()
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.select(2)
shell.run("2A")

2A

term.clear()
term.setCursorPos(1,1)
print("Second Quarter 2A")
-- Functions
local function t()
turtle.turnLeft()
turtle.back()
turtle.turnRight()
end

local function five()
local n = 5
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function four()
local n = 4
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function three()
local n = 3
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function two()
local n = 2
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function one()
local n = 1
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
-- 2A

five()
four()
three()
three()
one()
two()
one()
two()
one()
one()
one()
one()
one()
shell.run("2B")

2B

term.clear()
term.setCursorPos(1,1)
print("Second Quarter 2B")
-- Functions
local function t()
turtle.turnRight()
turtle.back()
turtle.turnLeft()
end

local function five()
local n = 5
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function four()
local n = 4
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function three()
local n = 3
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function two()
local n = 2
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function one()
local n = 1
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
-- 2B

turtle.turnLeft()
two()
one()
two()
one()
three()
three()
four()
five()
turtle.back()
turtle.place()
turtle.select(3)
shell.run("1AA")

1AA

term.clear()
term.setCursorPos(1,1)
-- Functions
local function t()
turtle.turnLeft()
turtle.back()
turtle.turnRight()
end

local function five()
local n = 5
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function four()
local n = 4
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function three()
local n = 3
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function two()
local n = 2
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
local function one()
local n = 1
for x = 1,n do
  turtle.back()
  turtle.place()
end
t()
end
-- 1A
five()
four()
three()
three()
one()
two()
one()
two()
one()
one()
one()
one()
one()
shell.run("1B")

Here is a pastebin if you need it: http://pastebin.com/sN8ebhvb

And this is what I am trying to automate…

Diameter : 75
Thickness: 11
Precision :1

http://www.plotz.co.uk/plotz-model.php?model=Torus

Thanks

- Ian
KingofGamesYami #2
Posted 26 October 2016 - 05:18 PM
You could replace five(), four(), three(), etc. with a function & argument


local function moveBackAndPlace( n )
  for i = 1, n do
    turtle.back()
    turtle.place()
  end
  t()
end

This would eliminate a large number of lines in your program, making it a little more readable.

I would also recommend writing functions for 1A etc. instead of entire programs. This, again, would make your code more readable. It would also eliminate redundant declarations and remove the need for an installer.

Note: You can't use 1A as a function name, as it starts with a number. Instead, you could name it either 'a1' or 'oneA'.
JustIan12 #3
Posted 26 October 2016 - 05:30 PM
Ok, I will have a look also could you explain in a bit more detail what you mean by 'You could replace five(), four(), three(), etc. with a function & argument'
Edited on 26 October 2016 - 03:35 PM
KingofGamesYami #4
Posted 26 October 2016 - 05:51 PM
Sure!

Functions can take arguments (eg 1, 2, 3, 4). The only difference between the functions I listed is the number of times the loop iterates.

Instead, you would write a function similar to the one I posted:

local function moveBackAndPlace( n )
  for i = 1, n do
    turtle.back()
    turtle.place()
  end
  t()
end

As you can see, it accepts an argument (n) which determines how many times it will move back and place. n is specified when you call the function, for example:


moveBackAndPlace( 1 )

Is the equivalent of your

one()

Specifying 2 or 3 would be equivalent to calling two() or three().
JustIan12 #5
Posted 26 October 2016 - 06:16 PM
Solved the issue thanks
Edited on 27 October 2016 - 09:51 AM