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

Turtle Control V2.0

Started by cheekycharlie101, 21 October 2012 - 01:50 PM
cheekycharlie101 #1
Posted 21 October 2012 - 03:50 PM
Welcome to turtle control

ok so i have been working on a program that will let you control turtles wirelessly. it will do the following things.

turn left
turn right
go left
go right
go forward
go back
dig around its self
go up
go down
and will even drop tnt

Note to drop tnt place the tnt in the bottom right slot of the turtle.
i will soon add more features like outputting redstone signals placing blocks ete. how ever im limited because the gui is in a menu form. so i can only do the ammount of options that fit on the screen.
anyway, to use its quite easy. just put the turtle code on a wireless mining turtle and run it.
for the client you have to make 2 modifications. you must enter the id of the turtle. and type in where the modem is on the computer
(left, right, top, bottom, front, back)

heres the code:
client:
Spoiler

id = 15 --change to turtle id
n=1
options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
"Dig",
"Turn Left",
"Turn Right",
}
local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
while true do
term.clear()
rednet.open("side") --Change to the side of your modem
local input = opt(options,0)
if input == 1 then
rednet.send(id,"payload,1")
elseif input == 2 then
rednet.send(id,"up,1")
elseif input == 3 then
rednet.send(id,"down,1")
elseif input == 4 then
rednet.send(id,"left,1")
elseif input == 5 then
rednet.send(id,"right,1")
elseif input == 6 then
rednet.send(id,"forward,1")
elseif input == 7 then
rednet.send(id,"back,1")
elseif input == 8 then
rednet.send(id,"dig,1")
elseif input == 9 then
rednet.send(id,"lLeft,1")
elseif input == 10 then
rednet.send(id,"lRight,1")
end
end

turtle:
Spoiler

rednet.open("right")
function LookLeft(distance)
for i = 1,distance do
turtle.turnLeft()
end
end
function LookRight(distance)
for i = 1,distance do
turtle.turnRight()
end
end
function forward(distance)
for i = 1,distance do
turtle.forward()
end
end
function back(distance)
for i = 1,distance do
turtle.back()
end
end
function mine(distance)
for i = 1,distance do
turtle.dig()
sleep(1.5)
turtle.digUp()
sleep(1.5)
turtle.digDown()
end
end
function up(distance)
for i = 1,distance do
turtle.up()
end
end
function left(distance)
turtle.turnLeft()
for i = 1,distance do
turtle.forward()
end
turtle.turnRight()
end
function down(distance)
for i = 1,distance do
turtle.down()
end
end
function right(distance)
turtle.turnRight()
for i = 1,distance do
turtle.forward()
end
turtle.turnLeft()
end
function payload(distance)
redstone.setOutput("bottom", true)
turtle.select(9)
for i = 1,distance do
turtle.placeDown()
sleep(1)
end
redstone.setOutput("bottom", false)
turtle.select(1)
end
while true do
local id,msg,dist = rednet.receive()
local command,dist = string.match(msg, "(%w+),(%w+)")
if command == "forward" then
forward(tonumber(dist))
elseif command == "back" then
back(tonumber(dist))
elseif command == "dig" then
mine(tonumber(dist))
elseif command == "up" then
up(tonumber(dist))
elseif command == "down" then
down(tonumber(dist))
elseif command == "left" then
left(tonumber(dist))
elseif command == "right" then
right(tonumber(dist))
elseif command == "payload" then
payload(tonumber(dist))
elseif command == "lRight" then
LookRight(tonumber(dist))
elseif command == "lLeft" then
LookLeft(tonumber(dist))
end
end


all suggestions are welcome and please let me know if you have errors and what part the errors are in!
thanks -Cheeky

p.s A MAJOR THANKS TO CRANIUM FOR HELPING ME WITH THE MENUE!!!!
ChunLing #2
Posted 21 October 2012 - 09:28 PM
You'll be wanting to put the functions in a table so that you can replace that long series of "if then elseif then … end" with a single compare (to see if the specified function is in the table) and call of the function.

You'll also want to use a table of messages to send, indexed by the input value that calls them.

The opt() function is interesting, but it seems a little inappropriate for control of a turtle…like you say, it is very limiting in how many options you can accommodate. Using the arrow keys (and the other navigation keys, pgUp/Dn, Home/End/Ins/Del) directly gives you more options and more intuitive controls (arrow keys for forward/back and turning, PgUp/Dn to go up/down, Ins/Del to place/dig, and Home/End to...drop/suck, maybe).
cheekycharlie101 #3
Posted 30 October 2012 - 09:19 PM
You'll be wanting to put the functions in a table so that you can replace that long series of "if then elseif then … end" with a single compare (to see if the specified function is in the table) and call of the function.

You'll also want to use a table of messages to send, indexed by the input value that calls them.

The opt() function is interesting, but it seems a little inappropriate for control of a turtle…like you say, it is very limiting in how many options you can accommodate. Using the arrow keys (and the other navigation keys, pgUp/Dn, Home/End/Ins/Del) directly gives you more options and more intuitive controls (arrow keys for forward/back and turning, PgUp/Dn to go up/down, Ins/Del to place/dig, and Home/End to...drop/suck, maybe).
thanks for your advice, but i really have no idea at all of how to go about doing this. this is probaly the best thing ive made in CC and i want to improve it but my coding is limited by my skill level:P could you possibly point me int he right direction for this?
thanks -Cheeky
ChunLing #4
Posted 31 October 2012 - 08:22 AM
When you have more than about 4 elseif statements that do essentially similar things in response to essentially similar conditionals, what you can do is put the parts of what you want to do in a table that is indexed by the things you are using as conditionals.

In this example, we have the following snippet of code, an if then elseif then…(etc.)…end structure.
if command == "forward" then forward(tonumber(dist))
elseif command == "back" then back(tonumber(dist))
elseif command == "dig" then mine(tonumber(dist))
elseif command == "up" then up(tonumber(dist))
elseif command == "down" then down(tonumber(dist))
elseif command == "left" then left(tonumber(dist))
elseif command == "right" then right(tonumber(dist))
elseif command == "payload" then payload(tonumber(dist))
elseif command == "lRight" then LookRight(tonumber(dist))
elseif command == "lLeft" then LookLeft(tonumber(dist))
end 
In each case, we just execute a function (with tonumber(dist) as the argument, though I'm not sure why) if command == the message commanding that function. Instead, we could put all the functions in a table, keyed to the commands that call them.
t_commands = {forward = forward,back = back,dig = mine,up = up,down = down, left = left,right = right,
   payload = payload,lRight = LookRight,lLeft = LookLeft,}
Now, in each identifier1 = identifier2 pair, identifier1 the index for a pointer to the contents of identifier2, which has been defined before the table setup. If we wanted to simplify our lives a bit, we'd just define the functions within the table itself rather than outside the table, but there are legitimate uses for doing it this way too.

Anyway, now that 'pointers' to the functions are stored in the table, keyed by the commands we want to use, we just say:
if t_commands[command] then t_commands[command](tonumber(dist)) end
That is to say, if the string in command is the index of something in our table, then run the table entry with tonumber(dist) as an argument (still not sure why you want to do that). This code is generally going to be faster and a bit more maintenance friendly than the previous, as well as being a good bit shorter.