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

Code Cleanup --> Moving To More Advanced Stuff.

Started by MonsterTKE, 28 February 2012 - 01:49 AM
MonsterTKE #1
Posted 28 February 2012 - 02:49 AM
Hey guys, Im not really to much of a coder but I do know the basics and how to use google. So far ComputerCraft has really gotten me thinking along some new lines. I have a few programs cobbled together from other sources and modified but now I am seeking some help in moving them to the next level of complexity.

I have a wireless turtle program to recieve commands from a computer, a computer program to send those commands, and a thrid program that I am trying to figure out how to integrate.

here is the computer.

-- computer
print("type down to run the down program")
print("type dig to run the dig program")
print("type move to run the move program")
digBot = 0
digBot2 = 1
digBot3 = 20
digBot4 = 3
digBot5 = 19
digBot6 = 5

direction = 0
distance = 0

rednet.open('right') -- open wifi port
while true do -- start infinite loop
key = read()
if key == 'down' then
rednet.send(digBot, 'down')
print('sent down')
rednet.send(digBot2, 'down')
print('sent down')
rednet.send(digBot3, 'down')
print('sent down')
rednet.send(digBot4, 'down')
print('sent down')
rednet.send(digBot5, 'down')
print('sent down')
rednet.send(digBot6, 'down')
print('sent down')
end
if key == 'dig' then
rednet.send(digBot, 'dig')
print('sent dig')
rednet.send(digBot2, 'dig')
print('sent dig')
rednet.send(digBot3, 'dig')
print('sent dig')
rednet.send(digBot4, 'dig')
print('sent dig')
rednet.send(digBot5, 'dig')
print('sent dig')
rednet.send(digBot6, 'dig')
print('sent dig')
turtle.forward()
turtle.forward()
turtle.down()
turtle.down()
end
end
here is the turtle.

-- turtle
write("host id #")
sender = read()
--ask for host id

shell.run('clear') --clear the screen
rednet.open("right") --open the wifi port
x=1
while x == 1 do --start loop
action, senderID, text = os.pullEvent() --wait for input
if action == "rednet_message" then --if its a wireless message do
if text == 'down' then
shell.setDir('rom')
shell.run('/rom/down')-- go forward
print(working)
end

  if text == 'dig' then -- if the up button is pressed
  shell.setDir('rom')
  shell.run('/rom/dig', '30')
  print(working)
end
end
end
and here is the last program I am trying to remotely control from the computer. Basically I am stuck on using rednet to send the variables to the client turtle before it runs the program. I would like to type move, then have it prompt me for the direction then the distance. I am a little confused with the whole wireless rednet api.

args = {...}
args2 = tonumber(args[2])
while true do

if args[1] == 'left' then
turtle.turnLeft()
while args2 > 0 do
turtle.forward()
args2 = args2 - 1
end
turtle.turnRight()
break
end

if args[1] == 'right' then
turtle.turnRight()
while args2 > 0 do
turtle.forward()
args2 = args2 - 1
end
turtle.turnLeft()
break
end

if args[1] == 'forward' then
while args2 > 0 do
turtle.forward()
args2 = args2 - 1
end
break
end

  if args[1] == 'back' then
while args2 > 0 do
turtle.back()
args2 = args2 - 1
end
break
end

if args[1] == 'up' then
while args2 > 0 do
turtle.up()
args2 = args2 - 1
end
break
end

  if args[1] == 'down' then
while args2 > 0 do
turtle.down()
args2 = args2 - 1
end
break
end
end

here is the little bit of code I have so far for the host to recieve and store the direction and distance to move.

if key == 'move' then
print("what direction")
direction = read()
print("how far")
distance = tonumber(read())
print(distance)
print(direction)
end

this works just fine, however I am totally stuck on sending this to the client.
Advert #2
Posted 28 February 2012 - 03:53 AM
I'll start with the computer program:


-- computer
print("type down to run the down program")
print("type dig to run the dig program")
print("type move to run the move program")
digBot = 0
digBot2 = 1
digBot3 = 20
digBot4 = 3
digBot5 = 19
digBot6 = 5

direction = 0
distance = 0

rednet.open('right') -- open wifi port
while true do -- start infinite loop
key = read()
if key == 'down' then
rednet.send(digBot, 'down')
print('sent down')
rednet.send(digBot2, 'down')
print('sent down')
rednet.send(digBot3, 'down')
print('sent down')
rednet.send(digBot4, 'down')
print('sent down')
rednet.send(digBot5, 'down')
print('sent down')
rednet.send(digBot6, 'down')
print('sent down')
end
if key == 'dig' then
rednet.send(digBot, 'dig')
print('sent dig')
rednet.send(digBot2, 'dig')
print('sent dig')
rednet.send(digBot3, 'dig')
print('sent dig')
rednet.send(digBot4, 'dig')
print('sent dig')
rednet.send(digBot5, 'dig')
print('sent dig')
rednet.send(digBot6, 'dig')
print('sent dig')
turtle.forward()
turtle.forward()
turtle.down()
turtle.down()
end
end

You can store all your bots in a table, which will make it much easier to add new ones, as well:

digbots = {}
digbot[1] = 0
digbot[2] = 1
digbot[3] = 20
-- and so on
-- Then, you can get your digbot's ID like this:
print(digbot[3]) -- Will print 20
print(digbot[tonumber(read())]) -- Type a number; if a digbot exists with that number, then it'll print it's ID:
-- Let's say we typed in 2; it would then return 1 to us, since digbot[2] is 1.
Read more here: http://www.lua.org/pil/2.5.html
You could also give your bots names, if you wanted to.
What the table will do, is allow you to code movement/sending stuff once, then use this to find out what the ID of the bot is, using a name or easy-to-remember number.

About sending the direction: You can use string.sub(), like this:

print("Direction?")
local dir = read()
print("Distance?")
local dist = read() -- We don't need to convert it to a number, since it's being sent as a string over rednet anyway:
rednet.send(targetbot, dir .. ";" .. dist) -- We're using the ; as a deltimer, on the turtle, we'll search for it, and split it there. Alternatively, you could just send the first letter of the command, as they're all different (I'd reccomend doing this for now, since it's easier):
rednet.send(targetbot, string.sub(dir, 1, 1) .. dist) -- This sends the first letter of the direction, and the distance, e.g: 'u20' 'b4' would be up 20, back 4, respectively.

On the turtle, assuming you used the recommended route:


local id, message = rednet.recieve()
local dir = message:sub(1, 1) -- get the first letter, which we'll use to determine the command
local dist = tonumber(message:sub(2)) -- Get the number part of the message, and turn it into a number

-- Put your stuff here to check what to do, but remember, dir is only one letter; so for down, check if dir == 'd'
if dir == 'd' then
 -- We're going down
 ...
end

Of course, the code I've provided is by no means complete; you'll need to add some stuff here and there, but I hope it'll help you. <_</>/>
MonsterTKE #3
Posted 28 February 2012 - 05:56 AM
I'll start with the computer program:

Im liking the table based method of storing the ids. Ill have to play around with that more.

My solution to the sending issue.
computer:

if key == 'move' then
print("what direction")
local direction = read()
print("how far")
local distance = read()
rednet.send(digBot, 'move')
rednet.send(digBot, direction)
rednet.send(digBot, distance)
rednet.send(digBot2, 'move')
rednet.send(digBot2, direction)
rednet.send(digBot2, distance)
rednet.send(digBot3, 'move')
rednet.send(digBot3, direction)
rednet.send(digBot3, distance)
rednet.send(digBot4, 'move')
rednet.send(digBot4, direction)
rednet.send(digBot4, distance)
rednet.send(digBot5, 'move')
rednet.send(digBot5, direction)
rednet.send(digBot5, distance)
rednet.send(digBot6, 'move')
rednet.send(digBot6, direction)
rednet.send(digBot6, distance)
print("done: type down, dig or move")
end

turtle:

if text == 'move' then
print("working")
action2, senderID, dir = os.pullEvent()
action3, senderID, dis = os.pullEvent()
dis2 = tonumber(dis)
print(dis2, dir, dis)
shell.setDir('rom')
shell.run('/rom/move', dir, dis2)

I see you used some local variables, Im still not 100% sure of the reasons to do this, or not to do this.
Advert #4
Posted 28 February 2012 - 06:00 AM
I'll start with the computer program:

Im liking the table based method of storing the ids. Ill have to play around with that more.

My solution to the sending issue.
computer:

if key == 'move' then
print("what direction")
local direction = read()
print("how far")
local distance = read()
rednet.send(digBot, 'move')
rednet.send(digBot, direction)
rednet.send(digBot, distance)
rednet.send(digBot2, 'move')
rednet.send(digBot2, direction)
rednet.send(digBot2, distance)
rednet.send(digBot3, 'move')
rednet.send(digBot3, direction)
rednet.send(digBot3, distance)
rednet.send(digBot4, 'move')
rednet.send(digBot4, direction)
rednet.send(digBot4, distance)
rednet.send(digBot5, 'move')
rednet.send(digBot5, direction)
rednet.send(digBot5, distance)
rednet.send(digBot6, 'move')
rednet.send(digBot6, direction)
rednet.send(digBot6, distance)
print("done: type down, dig or move")
end

turtle:

if text == 'move' then
print("working")
action2, senderID, dir = os.pullEvent()
action3, senderID, dis = os.pullEvent()
dis2 = tonumber(dis)
print(dis2, dir, dis)
shell.setDir('rom')
shell.run('/rom/move', dir, dis2)

I see you used some local variables, Im still not 100% sure of the reasons to do this, or not to do this.
Global variables are bad: They persist, meaning that in subsequent runs of your program, old variables may be kept; they may conflict with other pieces of code, etc. There isn't much reason not to use locals, they are even slightly faster :(/>/>