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

[Question] modifying custom [API], use of multiple [arg], and improving program interaction.

Started by C3H7COOH, 04 January 2013 - 06:34 AM
C3H7COOH #1
Posted 04 January 2013 - 07:34 AM
the API in question actually came from rondouglas Minecraft ComputerCraft Tutorial 11 - Custom Turtle API. Before moving on I want to modify the API to better suit my personal needs

—Modifying a custom API—
I do not like how the function draw.Line() places blocks.


function drawLine(dir,length)
--turn x number of times to face dir
  for i=1,dir do
	 turtle.turnRight()
  end
-- loop from 1 to length
  for j=1,length do
-- move forward and place block
	placeUp()
  if j<length then
turtle.forward()
  end
  end
end

--before placing a block check inventory--
function placeUp()
  for i=1,16 do
   if turtle.getItemCount(i) > 0 then
	  turtle.select(i)
	  break
   end
  end
	turtle.placeUp()
end

I would prefer the turtle use the turtle.placeDown() command under him, in the same plane he starts in, than simply putting them above him. The best i could come up with on my own was a modified turtle leap frog that made my drawLine() work. But rending my one if not all of my other functions to make squares, rectangles, or triangles useless.

Entire API here:

function drawLine(dir,length)
-- turn x number of times to face dir
  for i=1,dir do
	turtle.turnRight()
  end
-- loop from 1 to length
  for j=1,length do
-- move forward and place block
	turtle.placeUp()
  if j<length then
	turtle.forward()
	end
  end
end
function drawSquare(length,plane)
  drawRect(length,length,plane)
end
function drawRect(length,width,plane)
  drawLine(0,length)
  drawLine(1,width)
  drawLine(1,length)
  drawLine(1,width)
	turtle.turnRight()
end
function drawTriangle(length,width,plane)
  drawLine(0,length)
	local s = width/length
  curS = 0
  for i=1,length-1 do
	turtle.placeUp()
	turtle.back()
	curS = curS + s
  if curS >= 0.5 then
		turtle.turnRight()
		turtle.forward()
		turtle.turnLeft()
	  curS = curS - 1
	end
  end
	drawLine(3,width)
	 turtle.turnRight()
end
function drawCircle(radius)
  for i=1,radius do
	turtle.forward()
  end

  for j=1,4 do
	x= -radius
	y= 0
	err =2-(2*radius)
  
  while x < 0 do
		placeUp()
	  r = err
	 if r<=y then
	  y=y+1
	  turtle.turnRight()
	  turtle.forward()
	  turtle.turnLeft()
	  err = err + y*2 + 1
	end
	if r > x or err > y then
	  x=x+1
	  turtle.back()
	  err = err + x*2+1
	end
  end
  turtle.turnRight()
  end
end
function placeUp()
  for i=1,16 do
   if turtle.getItemCount(i) > 0 then
	  turtle.select(i)
	  break
   end
  end
	turtle.placeUp()
end  


—Using Multiple [arg]—
@ the 31:58 mark he start using arguments to supliment going into the code everytime to edit the variable for his circle program


os.loadAPI("ts")
arg = {...}
ts.drawCircle(arg[1])

instead of the previous ts.drawCircle(<radius>)

So that said. How do I use arguments in code that have several variables to account for?

An example would be my drawRectangle() function:


function drawRect(length,width,plane)
	drawLine(0,length)
	drawLine(1,width)
	drawLine(1,length)
	drawLine(1,width)
	turtle.turnRight()
end

has 3 variables that need to be established length, width, plane

How can i utilize [arg] to establish these instead of having to go in and edit it every single time.

—How to Make a Better User interaction–
I would like to be able to house all these programs into one file (Perhaps simply called TurtleShapes)
Where the program ask the user what shape they want to build
Then ask for the specific dimentions:
What width would you like?
What length?
How far up/down should I build?
I then would like it to know if its possible to somehow use this API to also fill in a shape after it has constructed the border?
Then it could simply ask the user:
Do you want me to fill it in when finished?

I'd also like to maybe set this whole thing up with an additional refueling function that utlizes the 16th slot when to turtle.refuel() when the value drops below a particular value (I can probably do this myself).


I realize this what an extremely long post, but as all subjects mentioned all have to do with eachother it seemed wrong to post multiple threads reguarding the same topic. I hope the format is acceptable, and the code is clean enough to not warrant any "you need to clean your code up it is making my eyes bleed comments". Because as I mentioned before its rondouglas and I wrote it exactly how he did in the tutorial. It makes sense to me because i have never used lua or any programming before, that's how he taught me, that's how I learned it, so it make sense to me (constructive mentoring on how to improve would be acceptable).

I'll probably end up posting this program in the Turtle Program thread once I'm satisfied that it fits all my particular needs. I will of course credit those who help me make the desired changes. Thou I'd prefer it not make it to other forum just yet as I don't think it's ready.
Lyqyd #2
Posted 04 January 2013 - 08:57 AM
For the second question, args[1] is using the first index in the table. Use higher indexes to get the values of the subsequent arguments. For instance, args[3] is the third argument. The shell separates the arguments by spaces.
C3H7COOH #3
Posted 04 January 2013 - 11:28 AM
For the second question, args[1] is using the first index in the table. Use higher indexes to get the values of the subsequent arguments. For instance, args[3] is the third argument. The shell separates the arguments by spaces.

Can you give an example how that might appear?

Say I wanted to create a rectangle with a width = 3 as arg[1], a length = 5 as arg[2] in it's current plane = 0 as arg[3]

I realize that when I run the program I will see rectangle 3 5 0

What does that look like when my current method is


os.loadAPI("ts")
ts.drawRect(3,5,0]
ChunLing #4
Posted 04 January 2013 - 02:40 PM
Don't use arg, use args, tArgs, tCP, or whatever else. And make it local rather than global. Other than that, it should look about how you expect.
Lyqyd #5
Posted 04 January 2013 - 02:44 PM
Probably something vaguely like:


os.loadAPI("ts")
local args = {...}
ts.drawRect(tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
C3H7COOH #6
Posted 04 January 2013 - 05:39 PM
Don't use arg, use args, tArgs, tCP, or whatever else. And make it local rather than global. Other than that, it should look about how you expect.

That would be great but no one has explained what tArgs, tCP, or args (and args differ from arg) are. What I know i've gotten from Caspers tutorial, and a few turtorials i've read online. Most don't explain what the command does.example I see read= i.o (or something like that or shell.<whatever> I know that they can be useful, I see them all the time. But I'm limited in my lua vocabulary.
C3H7COOH #7
Posted 04 January 2013 - 05:40 PM
Probably something vaguely like:


os.loadAPI("ts")
local args = {...}
ts.drawRect(tonumber(args[1]), tonumber(args[2]), tonumber(args[3])

I must of tried every number combination of that except adding tonumber. Does tonumber have a particular meaning, or does it just always go before args when using multipule args int he same line?
ChunLing #8
Posted 04 January 2013 - 05:51 PM
tonumber is a basic Lua function that attempts to convert a string into a number. It doesn't work on other types.
C3H7COOH #9
Posted 04 January 2013 - 06:36 PM
tonumber is a basic Lua function that attempts to convert a string into a number. It doesn't work on other types.

So really tonumber[arg] exist solely for the reason I want to use it. Neat, I like having the right tool for the job. Thank you.
ChunLing #10
Posted 05 January 2013 - 03:15 AM
Um…just a nit, but tonumber[arg] would signify something different. It would be an attempt to index a table tonumber using the value of arg, which is the default identifier for the argument table passed to a function that receives a variable number of arguments.

If it worked, that would mean that you overwrote both the tonumber function and the arg table, which you probably shouldn't do.