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

Turtle Station Program

Started by professorloz, 12 January 2016 - 04:34 PM
professorloz #1
Posted 12 January 2016 - 05:34 PM
Hi I am trying to create a program which can be used on a turtle to deploy a specific route/etc to it based on options selected. So atm I have 4 routes configured, the paths are of different coloured wool and consist of getting from the start of the route, to a square, goes around the outside, then back to the start again. The program so far has each route setup as a function, once the turtle's path colour is set-up and you get to the point where it should begin the route, it doesn't do anything (have checked it's refueled). I have put text before it kicks off the selected route function which does display, seems it just never actually runs the route function. :/ Any ideas:

(PS\ Am just learning atm, so any pointers on better ways to do things much appreciated)


--[[ KIERANs T-STATION CONTROL ]]--
--[[ VARIABLEs ]]--
a = 0
b = 0
x = 0
y = 0
z = 0
endless = "N"
endlessask = "Y"
shouldrun = "Y"
route = "NOT_SET"
--[[ FUNCTIONs ]]--
function VReset()
x = 0
y = 0
end
function Forward(y)
while x < y do
  turtle.forward()
  x = x + 1
end
VReset()
end
function TurnLeft(y)
while x < y do
  turtle.TurnLeft()
  x = x + 1
end
VReset()
end
function TurnRight(y)
while x < y do
  turtle.TurnRight()
  x = x + 1
end
VReset()
end
--[[ ROUTES ]]--
function AQUAroute(y)
VReset()
while x < y do
  --[[ AQUA ]]--
  --[[ Getting to Square {SP} ]]--
   Forward(9)
   TurnRight()
   Forward(3)
   TurnRight()
   Forward(1)
   TurnLeft()
   Forward(2)
  
  --[[ Getting to Left Bottom Corner ]]--
   TurnLeft()
   Forward(1)
   TurnRight()
  
  --[[ Round the Square ]]--
    --[[ @BottomLeft Square ]]--
   Forward(12)
   TurnRight()
    --[[ @TopLeft Square ]]--
   Forward(12)
   TurnRight()
    --[[ @TopRight Square ]]--
   Forward(12)
   TurnRight()
    --[[ @BottomRight Square ]]--
   Forward(12)
   TurnRight()
    --[[ @BottomLeft Square ]]--
  
  --[[ Geeting to Square {SP} ]]--
   TurnRight()
   Forward(1)
   TurnRight()
  
  --[[ Back to Station ]]--
   Forward(2)
   TurnLeft()
   Forward(1)
   TurnLeft()
   Forward(3)
   TurnRight()
   Forward(9)
end
VReset()
end
function PINKroute(y)
VReset()
while x < y do
  --[[ PINK ]]--
  --[[ Getting to Square {SP} ]]--
   Forward(11)
   TurnRight()
   Forward(5)
   TurnLeft()
   Forward(1)
   TurnRight()
   Forward(2)
  
  --[[ Getting to Left Bottom Corner ]]--
   TurnRight()
   Forward(1)
   TurnLeft()
  
  --[[ Round the Square ]]--
    --[[ @BottomRight Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @TopRight Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @TopLeft Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @BottomLeft Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @BottomRight Square ]]--
  
  --[[ Geeting to Square {SP} ]]--
   TurnLeft()
   Forward(1)
   TurnLeft()
  
  --[[ Back to Station ]]--
   Forward(2)
   TurnLeft()
   Forward(1)
   TurnRight()
   Forward(5)
   TurnLeft()
   Forward(11)
end
VReset()
end
function GREENroute(y)
VReset()
while x < y do
  --[[ GREEN ]]--
  --[[ Getting to Square {SP} ]]--
   Forward(11)
   TurnLeft()
   Forward(5)
   TurnRight()
   Forward(1)
   TurnLeft()
   Forward(2)
  
  --[[ Getting to Left Bottom Corner ]]--
   TurnLeft()
   Forward(1)
   TurnRight()
  
  --[[ Round the Square ]]--
    --[[ @BottomLeft Square ]]--
   Forward(12)
   TurnRight()
    --[[ @TopLeft Square ]]--
   Forward(12)
   TurnRight()
    --[[ @TopRight Square ]]--
   Forward(12)
   TurnRight()
    --[[ @BottomRight Square ]]--
   Forward(12)
   TurnRight()
    --[[ @BottomLeft Square ]]--
  
  --[[ Geeting to Square {SP} ]]--
   TurnRight()
   Forward(1)
   TurnRight()
  
  --[[ Back to Station ]]--
   Forward(2)
   TurnRight()
   Forward(1)
   TurnLeft()
   Forward(5)
   TurnRight()
   Forward(11)
end
VReset()
end
function YELLOWroute(y)
VReset()
while x < y do
  --[[ YELLOW ]]--
  --[[ Getting to Square {SP} ]]--
   Forward(9)
   TurnLeft()
   Forward(3)
   TurnLeft()
   Forward(1)
   TurnRight()
   Forward(2)
  
  --[[ Getting to Left Bottom Corner ]]--
   TurnRight()
   Forward(1)
   TurnLeft()
  
  --[[ Round the Square ]]--
    --[[ @BottomRight Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @TopRight Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @TopLeft Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @BottomLeft Square ]]--
   Forward(12)
   TurnLeft()
    --[[ @BottomRight Square ]]--
  
  --[[ Geeting to Square {SP} ]]--
   TurnLeft()
   Forward(1)
   TurnLeft()
  
  --[[ Back to Station ]]--
   Forward(2)
   TurnLeft()
   Forward(1)
   TurnRight()
   Forward(3)
   TurnRight()
   Forward(9)
end
VReset()
end
function PURPLEroute(y)
VReset()
while x < y do

end
VReset()
end
function REDroute(y)
VReset()
while x < y do

end
VReset()
end
function ORANGEroute(y)
VReset()
while x < y do

end
VReset()
end
function BLUEroute(y)
VReset()
while x < y do

end
VReset()
end
--[[ ESSENTIAL ]]--
function WhichRoute()
if route == "NOT_SET" then
  print("Please enter desired route to take?")
  print("(AQUA, PINK, GREEN, YELLOW, PURPLE, RED, ORANGE, BLUE)")
  route = read()
else
 
  if route == "AQUA" then
   print("AQUA Route Selected")
   AQUAroute(y)
  else
   if route == "PINK" then
   print("PINK Route Selected")
   PINKroute(y)
   else
    if route == "GREEN" then
    print("GREEN Route Selected")
    GREENroute(y)
    else
	 if route == "YELLOW" then
	 print("YELLOW Route Selected")
	 YELLOWroute(y)
	 else
	  if route == "PURPLE" then
	  print("PURPLE Route Selected")
	  PURPLEroute(y)
	  else
	   if route == "RED" then
	   print("RED Route Selected")
	   REDroute(y)
	   else
	    if route == "ORANGE" then
	    print("ORANGE Route Selected")
	    ORANGEroute(y)
	    else
		 if route == "BLUE" then
		 print("BLUE Route Selected")
		 BLUEroute(y)
		 else
		  print("INVALID ROUTE SELECTED")
		 end
	    end
	   end
	  end
	 end
    end
   end
  end
end
end

function EndLess(y)
if x > y then
   print("The input specified is a negative.")
   if endlessask == "Y" then
    print("*Warning* - This will put turtle into endless mode")
    print("Please confirm? (Y/N)")
    endless = read()
    if endless == "Y" then
	 print("Enabling endless mode on turtle. Ask for confirmation on this turtle again?")
	 print("Please confirm? (Y/N)")
	 endlessask = read()
	 if endlessask == "Y" then
	  print("Will continue to ask for confirmation.")
	 else
	  endlessask = "N"
	 end 
	 shouldrun = "Y"
    else
	 shouldrun = "N"
    end
   else
    print("Activating Endless Mode")
    shouldrun = "Y"
   end
end
if shouldrun == "Y" then
   WhichRoute(y)
else
  write("Something isn't right.")
end
end
function StartRoute()
print("Enter the number of times to repeat process:")
print("(Negative values enable Endless Mode)")
b = read()
b = tonumber(B)/>
EndLess(B)/>
end
function SetRoute()
if route == "NOT_SET" then
  print("Please enter desired route to take?")
  print("(AQUA, PINK, GREEN, YELLOW, PURPLE, RED, ORANGE, BLUE)")
  route = read()
else
  write("Route already set as: ")
  write(route)
  print("!")
  print("Please enter desired route to take?")
  print("(AQUA, PINK, GREEN, YELLOW, PURPLE, RED, ORANGE, BLUE)")
  route = read()
end
end

function GetRoute()
print(route)
end
function Intro()
print("WELCOME TO T-STATION CONTROL")
print("Please select from the following options:")
print("")
print("1. Start Route")
print("2. Check Route")
print("3. Set a Route")
print("4. Additional")
print("")
print("[1-4]")
a = read()
a = tonumber(a)
if a == 1 then
  StartRoute()
  Intro()
else
  if a == 2 then
  GetRoute()
  Intro()
  else
   if a == 3 then
   SetRoute()
   Intro()
   else
    if a == 4 then
	 print("None at this time.")
	 Intro()
    else
	 print("Please enter a correct option.")
	 Intro()
    end
   end
  end
end
a = 0
end

--[[ Program ]]--
Intro()
Bomb Bloke #2
Posted 13 January 2016 - 12:06 AM
All of your routes seem to be running this loop:

while x < y do

But before that, you're calling VReset(), which sets both x and y to 0. x is hence never less than y when the loops start, and so they never run their code at all.

It's worth noting that within the scope of your route functions, y refers to a different variable to the global one you defined at the top of the script - each has a local y, passed in as a parameter when the functions are called, and VReset() can't touch those ones. These are still 0, as that's what you defined y as at the start of the script, that global y's value is passed to the function's local y variables, and you never change it before attempting to start your loops. See here for more info on variable scope.

Another thing: elseif. This here:

Spoiler
if a == 1 then
  StartRoute()
  Intro()
else
  if a == 2 then
  GetRoute()
  Intro()
  else
   if a == 3 then
   SetRoute()
   Intro()
   else
    if a == 4 then
         print("None at this time.")
         Intro()
    else
         print("Please enter a correct option.")
         Intro()
    end
   end
  end
end

… can be written as:

Spoiler
if a == 1 then
  StartRoute()
  Intro()
elseif a == 2 then
  GetRoute()
  Intro()
elseif a == 3 then
   SetRoute()
   Intro()
elseif a == 4 then
   print("None at this time.")
   Intro()
else
   print("Please enter a correct option.")
   Intro()
end