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.
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:
—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
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:
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.
—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.