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

making a math formula?

Started by Avous, 11 January 2013 - 09:41 AM
Avous #1
Posted 11 January 2013 - 10:41 AM
I am trying to make a tree felling turtle but I want a user defined area for example,

I start my turtle up and he asks me something like, "how many trees are in a row?" It will use that input to run said section of code X amount of times to chop the/go to the next tree.

Finally, it will ask "how many rows are in this tree farm?" Based on the input i need it to do a formula to figure out how many spots it needs to move to go the first tree of the second row ect and then back home after it is done.

I have a limited understanding of lua so the plainer put the easier for me lol.

Edit: I think i just thought of a work around to avoid this but it would be good to know in case it fails or for future reference.

Edit to clarify: So, simply put. It has to use both numbers. (how many trees? how many rows?) then it has to be able to go to the first tree of the next row and then back to its starting position based on that input after its finished the last defined row.
Doyle3694 #2
Posted 11 January 2013 - 10:45 AM

print("How many times")
looptimes = tonumber(read())
for i = 1,looptimes do
   -- code
end

For the other one, I cant help you because you have not provided enough information
Avous #3
Posted 11 January 2013 - 10:48 AM

print("How many times")
looptimes = tonumber(read())
for i = 1,looptimes do
   -- code
end

For the other one, I cant help you because you have not provided enough information

Thanks, I mean that for example a turtle has to move twice between trees so that's simple to do. However, when I define a row (say three rows in this farm) it has to go to the beginning of the next row and then continue to do so after its done with that row until its to the end and then be recalled home because it has reached the end of the defined row, so it needs to know how many times to move.

So, simply put. It has to use both numbers. (how many trees? how many rows?) then it has to be able to go to the first tree of the next row and then back to its starting position based on that input after its finished the last defined row.
Doyle3694 #4
Posted 11 January 2013 - 10:51 AM
maybe

for i = 1,looptimes*3 do
   turtle.forward()
end
Avous #5
Posted 11 January 2013 - 10:53 AM
maybe

for i = 1,looptimes*3 do
   turtle.forward()
end
thanks i'll try that
Orwell #6
Posted 11 January 2013 - 10:54 AM
I understanded that you have x trees per row and y rows. Is that right? And you want to know when you're at the end of the row and need to move back?

If that's the case, you can do this:

print("How many trees?")
local nTrees = tonumber(read())
print("How long is a row?")
local rowLen = tonumber(read())

for i=0,nTrees-1 do
  local x = math.floor(nTrees/rowLen)
  local y = i%rowLen

  -- example1:
  -- do something at position x of row y

  -- example2: detect when you need to switch rows:
  if x==0 then
	-- switch rows
  end
end
theoriginalbit #7
Posted 11 January 2013 - 10:54 AM
in lua there are these operators
+ plus
- minus
/ divide
* multiply
% modulo
^ power

as well as everything in the math library detailed here
Avous #8
Posted 11 January 2013 - 11:04 AM
Thanks for your help guys I'm sure I can get this to work now!