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

My first Lua script. (turtle floor replacement program)

Started by scarecrow569, 03 August 2012 - 02:43 PM
scarecrow569 #1
Posted 03 August 2012 - 04:43 PM
Ok so bascially as my title said this is my first 'real' lua script. Im sure there are alot of bugs and ways that things could be written better
so basically what im looking for is some pointers and some feedback on what is obviously wrong and what can be done better.

This program is designed to ask for the dimentions of a ground to replace and then of course replace it.

I'm also sure their is a way to make lines 14-22 can be written more effectivly any pointers would go along way.


x = 0
y = 0
tf = true
print("place x value (how far ahead of turtle?)")
x = io.read()
print ("Place y value (how far to the side?) ")
y = io.read()
value = 0
x ^ y = value
if value > 576 then
print("This is too big fo rthe turtle.")
else
a = turtle.getItemCount(1)
b = turtle.getItemCount(2)
c = turtle.getItemCount(3)
d = turtle.getItemCount(4)
e = turtle.getItemCount(5)
f = turtle.getItemCount(6)
g = turtle.getItemCount(7)
h = turtle.getItemCount(8)
i = turtle.getItemCount(9)
end
total = a + b + c + d + e + f + g + h + i
if value > total then
print("You dont have enough blocks")
else
while y > 0 do
y = y - 1
z = x
  while z > 0 then
   z = z - 1
   turtle.digDown()
   turtle.placeDown()
   turtle.forward()
   z = 0
  end
  if tf = true then
   turtle.turnRight()
   turtle.forward()
   turtle.turnRight()
   tf = false
  else
   turtle.turnLeft()
   turtle.forward()
   turle.turnLeft()
   tf = true
  end
end
end
print("Task Complete!")

3rdly. How awesome is Lua!
OmegaVest #2
Posted 03 August 2012 - 07:58 PM
So, first, you might want to put tonumber around the reads at the top. Not sure if its necessary, but it might help.

So, for the item counter, use a table.


itemCounts = {}
total = 0
for i = 1, 9 do
   itemCount[i] = turtle.getItemCount(i)
   total = total + itemCount[i]
end

It won't use letters, but it will make manipulation much easier in the end, if you decide to do anything larger. Although, I don't guess you actually need the table at all. You could just use the "total" line inside the for loop.

And, I don't know about this, but line 9 should probably be value = x^y, not the other way around. Lua is not stack-based, obviously, and it might make x into 0 instead of making value into x^y.

Other than those few things, it looks good. (On the other hand, I've never gotten this kind of program to work personally, so good luck!)
scarecrow569 #3
Posted 04 August 2012 - 11:33 AM
thanks mate ill let you know how I go!
Pharap #4
Posted 04 August 2012 - 02:44 PM
I can think of several ways to improve this, do you want to hear them?
scarecrow569 #5
Posted 04 August 2012 - 03:38 PM
Sure pharap!
This is still very much in the 'wtf am i doing' stage.
Also are their any online/downloadable applications to check if it will complie/work? without having to put it into the game?
Lua is also my first coding languge outside html/css and so far im loving this commmunity!

Scare.
Pharap #6
Posted 04 August 2012 - 10:08 PM
Sure pharap!
This is still very much in the 'wtf am i doing' stage.
Also are their any online/downloadable applications to check if it will complie/work? without having to put it into the game?
Lua is also my first coding languge outside html/css and so far im loving this commmunity!

Scare.

Well, if you're html/css, that certainly explains why you'd have a bit of an issue getting your head around things.

You see, html and css are declarative languages - they describe how to arrange things
languages like lua, visualbasic, c#, c++ are all imperative programming languages. - they perform processes.

Every language is useful and has a place, but some are very different to others.

I don't know of any online validators (I'm assuming you've used an html validator before?), and tbh, with or without IDEs (integrated development environments, fancy code editors/managers) programming is still mostly writing code, it's not very easy to escape from.

I have a few tips I could give you and I could run you through some basic concepts by message if you wish to do so, but for now I will just attempt to improve your program, here's an edit with comments marked by – (in html/css, I think you have something like // to make a line that won't be read by the program, not sure, it's been a while since I've done webdev. In lua, – does the same thing)



x = 0
y = 0
tf = true
print("place x value (how far ahead of turtle?)")
x = io.read() --note: read does work on its own, you do not need io
print ("Place y value (how far to the side?) ")
y = io.read()
value = 0
value = x * y
--read as variable = data to assign, not data to assign = variable.
--Also, ^ means powers of, * is multiplication, which is what you want for area of a rectangle
if value > 576 then
print("This is too big for the turtle.") --minor typo
total = 0
else
a = turtle.getItemCount(1)
b = turtle.getItemCount(2)
c = turtle.getItemCount(3)
d = turtle.getItemCount(4)
e = turtle.getItemCount(5)
f = turtle.getItemCount(6)
g = turtle.getItemCount(7)
h = turtle.getItemCount(8)
i = turtle.getItemCount(9)
total = a + b + c + d + e + f + g + h + i
-- variables are local, after end, the letters will no longer exit
--total will remain because it was declared beforehand
end
if value > total then
print("You dont have enough blocks")
else
tf = true
--tf had not been set, it makes most sense here, what is it supposed to do?
while y > 0 do
  while x > 0 then
   turtle.digDown()
   turtle.placeDown()
   turtle.forward()
   z = z - 1
   -- ideally a for loop would be better here, but I will just correct your while for now
  end
  if tf = true then
   turtle.turnRight()
   turtle.forward()
   turtle.turnRight()
   tf = false
  else
   turtle.turnLeft()
   turtle.forward()
   turle.turnLeft()
   tf = true
  end
  y = y - 1
end
end
print("Task Complete!")

It's not brilliant, but I made a few alterations to your code to improve it. It's a bit hard to read/understand, but I can make out what you were thinking and you did well to manage the nested while loop. Nested loops are always handy for processing coords.

If you want me to run through a more fully optimised version with you or some of the basic commands, feel free to note me.

For now, however, I just leave you with this edited script. I can't see why it wouldn't run now, but feel free to bring it back if it doesn't run, there is a chance I've screwed up somewhere. After all, programming is one thing, reading someone else's code is a whole other story.
scarecrow569 #7
Posted 05 August 2012 - 06:14 PM
Thanks Pharap for checking through my code. Some little gems of knowledge in there.

tf had not been set, it makes most sense here, what is it supposed to do?

Just to make sure on its first cycle it turns the correct way.

I'll write it up now and see how we go.
scarecrow569 #8
Posted 05 August 2012 - 07:21 PM
Ok this is where we are at,


x = 0
y = 0
tf = true
print("place x value (how far ahead of turtle?)")
x = io.read() --note: read does work on its own, you do not need io
print ("Place y value (how far to the side?) ")
y = io.read()
value = 0
value = x * y
--read as variable = data to assign, not data to assign = variable.
--Also, ^ means powers of, * is multiplication, which is what you want for area of a rectangle
if value > 576 then
print("This is too big for the turtle.") --minor typo
total = 0
else
a = turtle.getItemCount(1)
b = turtle.getItemCount(2)
c = turtle.getItemCount(3)
d = turtle.getItemCount(4)
e = turtle.getItemCount(5)
f = turtle.getItemCount(6)
g = turtle.getItemCount(7)
h = turtle.getItemCount(8)
i = turtle.getItemCount(9)
total = a + b + c + d + e + f + g + h + i
-- variables are local, after end, the letters will no longer exit
--total will remain because it was declared beforehand
end
if value > total then
print("You dont have enough blocks")
else
tf = true
--tf had not been set, it makes most sense here, what is it supposed to do?
while y > 0 do
  while x > 0 do
   turtle.digDown()
   turtle.placeDown()
   turtle.forward()
   z = z - 1
   -- ideally a for loop would be better here, but I will just correct your while for now
  end
  if tf == "true" then
   turtle.turnRight()
   turtle.forward()
   turtle.turnRight()
   tf = false
  else
   turtle.turnLeft()
   turtle.forward()
   turle.turnLeft()
   tf = true
  end
  y = y - 1
end
end
print("Task Complete!")

It compiles after a couple of bug fixes,
- Line 34 needed to be do instead of then
- line 42 needed to read… if tf == "true" then instead of…. if tf = true then

Now after running the program and placing a 'x' and 'y' value I receive this message.

floor:34: attempt to compare string
with number expected, got string

This error has me a little stumped.

I've tried putting in a… print(y) just before the line 34 and it returns the correct value/variable.

Edit: Is this error saying that its not seeing my y value as a number? Has this to do with this 'tonumber' ?

Any guidance?

PS to earlier posters. Your advice has not been forgotten, I've chosen to proceed with Pharap's
version until I have a working program, from there I will try these better ways of coding.
(sorry I really don't know the coding lingo, so please point out dumb phrases I use)
Pharap #9
Posted 06 August 2012 - 01:52 AM
Ok this is where we are at,

It compiles after a couple of bug fixes,
- Line 34 needed to be do instead of then
- line 42 needed to read… if tf == "true" then instead of…. if tf = true then

Now after running the program and placing a 'x' and 'y' value I receive this message.

floor:34: attempt to compare string
with number expected, got string

This error has me a little stumped.

I've tried putting in a… print(y) just before the line 34 and it returns the correct value/variable.

Edit: Is this error saying that its not seeing my y value as a number? Has this to do with this 'tonumber' ?

Any guidance?

PS to earlier posters. Your advice has not been forgotten, I've chosen to proceed with Pharap's
version until I have a working program, from there I will try these better ways of coding.
(sorry I really don't know the coding lingo, so please point out dumb phrases I use)

Sorry about those errors. I used the visual basic terms out of habit, I am new to lua, and the syntax is really similar but there's just a tiny difference which means I get a lot of errors due to typos and trying to use VB syntax.

As for your code, I believe changing x = io.read() and y = io.read() to x = tonumber(io.read()) and y = tonumber(io.read()) should fix things.
Yet again, that was down to my habits. In visual basic, it changes strings to number automatically if they are valid numbers.

I'm guessing most mathematical operators (+ - = / *) in lua can be used with strings as well, but if you use y < 0 and y is a string, lua thinks you are trying to compare the string with a 0 and gets annoyed.

try it now:


x = 0
y = 0
tf = true
print("place x value (how far ahead of turtle?)")
x = tonumber(io.read()) --note: read does work on its own, you do not need io
print ("Place y value (how far to the side?) ")
y = tonumber(io.read())
value = 0 -- note: probably not needed, lua should figure out x * y then set value as the results of the equation
value = x * y
--read as variable = data to assign, not data to assign = variable.
--Also, ^ means powers of, * is multiplication, which is what you want for area of a rectangle
if value > 576 then
print("This is too big for the turtle.") --minor typo
total = 0
else
a = turtle.getItemCount(1)
b = turtle.getItemCount(2)
c = turtle.getItemCount(3)
d = turtle.getItemCount(4)
e = turtle.getItemCount(5)
f = turtle.getItemCount(6)
g = turtle.getItemCount(7)
h = turtle.getItemCount(8)
i = turtle.getItemCount(9)
total = a + b + c + d + e + f + g + h + i
-- variables are local, after end, the letters will no longer exit
--total will remain because it was declared beforehand
end
if value > total then
print("You dont have enough blocks")
else
tf = true
--tf had not been set, it makes most sense here, what is it supposed to do?
while y > 0 do
  while x > 0 then
   turtle.digDown()
   turtle.placeDown()
   turtle.forward()
   z = z - 1
   -- ideally a for loop would be better here, but I will just correct your while for now
  end
  if tf = true then
   turtle.turnRight()
   turtle.forward()
   turtle.turnRight()
   tf = false
  else
   turtle.turnLeft()
   turtle.forward()
   turle.turnLeft()
   tf = true
  end
  y = y - 1
end
end
print("Task Complete!")

without typing it all in myself and checking if it compiles, I can only say I can't see why it wouldn't. But then again, I am only human, so there may still be something I've overlooked. Once you've got this working, I can show you some better methods. (After all, it looks like you worked hard on learning this code, so it would be a shame to see it go to waste)
scarecrow569 #10
Posted 06 August 2012 - 07:27 AM
It works!

But buggy. bugs i fixed as follows
- Line 45… if tf = true then to… if tf == true then
- Line 36 replaced then with do
- Changed the order of duty's to prevent the funky shapes it was first creating.
- Made the turtle dig infront of itself and above it to cut into a mountain/hill
- Above will also destroy obstacles like grass and stray blocks

The issue I'm now having is that after its used its first stack of 64 it starts picking up what it digs and puts it into
slot one, and then of course places what it picks up. and continues to do this until its finished its cycle.

what would be ideal is a dig but do not collect function… however I do not think it exists…

Anyway here's the working code, any help appreciated.


x = 0
y = 0
tf = true
print("place x value (how far ahead of turtle?)")
x = tonumber(io.read()) --note: read does work on its own, you do not need io
print ("Place y value (how far to the side?) ")
y = tonumber(io.read())
value = 0 -- note: probably not needed, lua should figure out x * y then set value as the results of the equation
value = x * y
--read as variable = data to assign, not data to assign = variable.
--Also, ^ means powers of, * is multiplication, which is what you want for area of a rectangle
if value > 576 then
print("This is too big for the turtle.") --minor typo
total = 0
else
a = turtle.getItemCount(1)
b = turtle.getItemCount(2)
c = turtle.getItemCount(3)
d = turtle.getItemCount(4)
e = turtle.getItemCount(5)
f = turtle.getItemCount(6)
g = turtle.getItemCount(7)
h = turtle.getItemCount(8)
i = turtle.getItemCount(9)
total = a + b + c + d + e + f + g + h + i
-- variables are local, after end, the letters will no longer exit
--total will remain because it was declared beforehand
end
if value > total then
print("You dont have enough blocks")
else
tf = true
--tf had not been set, it makes most sense here, what is it supposed to do?
while y > 0 do
  z = x - 1
  while z > 0 do
turtle.digDown()
turtle.digUp()
turtle.placeDown()
turtle.dig()
turtle.forward()
z = z - 1
   -- ideally a for loop would be better here, but I will just correct your while for now
  end
  if tf == true then
   turtle.digDown()
   turtle.digUp()
   turtle.placeDown()
   turtle.turnRight()
   turtle.dig()
   turtle.forward()
   turtle.turnRight()
   tf = false
  else
   turtle.digDown()
   turtle.digUp()
   turtle.placeDown()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   tf = true
  end
  y = y - 1
end
end
print("Task Complete!")
Pharap #11
Posted 06 August 2012 - 07:48 AM
It works!

But buggy. bugs i fixed as follows
- Line 45… if tf = true then to… if tf == true then
- Line 36 replaced then with do
- Changed the order of duty's to prevent the funky shapes it was first creating.
- Made the turtle dig infront of itself and above it to cut into a mountain/hill
- Above will also destroy obstacles like grass and stray blocks

The issue I'm now having is that after its used its first stack of 64 it starts picking up what it digs and puts it into
slot one, and then of course places what it picks up. and continues to do this until its finished its cycle.

what would be ideal is a dig but do not collect function… however I do not think it exists…

Anyway here's the working code, any help appreciated.

Sorry, both those errors were missed because of my VB syntax habits ^^'

But on the bright side, this is what I was hoping for, getting you to use your brain to figure out the problems and sort them.
Programming is about a lot of things, one of those key things is problem solving.
Another one is understanding - if you understand your code, it's easier to understand what's going wrong.

That function doesn't exist, but there is almost certainly a way around it. I only recently got a non-tekkit version of computercraft and found out how to externally edit code, so I'm finally getting round to making the pseudo-apis (really more like libraries of programs) to help get stuff done. The first one I'm working on is wall building (kind of ironic considering you're doing floors).

Right now I can't fix your inventory issue, but since I'm almost certain to come across this issue in my code, I'll post you the fix when I've done it. Hopefully that will be before the day is out

Until then, see if you can build a wall program. Since you have a working floor program, it shouldn't be too hard to figure out. When I've finished making my wall program and an optimised floor program, I'll send you the code and explain the optimisation. In the mean time, I suggest getting to know the programs and apis. use the help program and it should tell you a decent amount, then just fiddle around with things until you figure it out.

Good luck and have fun programming.
WeeHeeHee #12
Posted 20 November 2012 - 08:10 PM
Damn it guys I typed out this code three times, the first two times realizing that I was typing a broken draft.
remiX #13
Posted 21 November 2012 - 01:25 AM
The issue I'm now having is that after its used its first stack of 64 it starts picking up what it digs and puts it into
slot one, and then of course places what it picks up. and continues to do this until its finished its cycle.

Somewhere you will need to make it check if the slot it currently using is empty, and if it is then changed the slot to the next slot which has blocks available.

Damn it guys I typed out this code three times, the first two times realizing that I was typing a broken draft.

:(/>/> Nice one :(/>/>