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

Creating multiple objects in different coordinates

Started by killer95533, 22 November 2018 - 04:21 AM
killer95533 #1
Posted 22 November 2018 - 05:21 AM
What if you wanted to spawn 2 trees? but, you don't want to have the trees to spawn
in the exact same spot. So you mark the 1st tree in a way to 1. ID the tree 2.
get the coordinates of that tree, you do this because you want to be able to pull
up the ID, check if the ID's coordinates are matching the same coordinates your
putting in for the tree your currently making, (the 2nd tree) and if they match
then you want to change those coordinates until they do not match the 1st Tree's
coordinates. Now the actual goal is to spawn a large number of trees to the point
where you wouldn't want to make this code by hand but more like a repeatable code
that goes on until you get the desired amount maybe 10, 20, or 200 who knows.
How would you organize this in a way to do that? keeping in mind that each tree
needs its own coordinates and ID.
Now I know to some of you this will most likely seem to be very easy to do but for
some reason I'm struggling to comprehend it so it would help if when you respond explain it
in a simple way to where a child would understand that's all and thank you for your time.

P.S. If the Trees separation could be a random distance apart that would also be ideal.
Edited on 02 December 2018 - 06:10 AM
KingofGamesYami #2
Posted 22 November 2018 - 12:38 PM
Well, I'd generate position based on ID. Lets say you want to make (approx) a square.

-- where to start from
offsetx, offsety = 10, 10 
-- how many trees in a row
sideLen = math.ceil(math.sqrt(number_of_tree))
-- spacing
spacing = 5
for id = 1, number_of_tree
  x = (id%sideLen * spacing) + offsetx
  y = (math.floor(id/sideLen) * spacing) + offsety
end

This assumes you're counting up in both x and y. It can be modified to do generate in the negative direction as well. You can also easily make a formula to get id from coordinates.
killer95533 #3
Posted 22 November 2018 - 03:38 PM
Well, I'd generate position based on ID. Lets say you want to make (approx) a square.

-- where to start from
offsetx, offsety = 10, 10
-- how many trees in a row
sideLen = math.ceil(math.sqrt(number_of_tree))
-- spacing
spacing = 5
for id = 1, number_of_tree
  x = (id%sideLen * spacing) + offsetx
  y = (math.floor(id/sideLen) * spacing) + offsety
end

This assumes you're counting up in both x and y. It can be modified to do generate in the negative direction as well. You can also easily make a formula to get id from coordinates.

I'm sorry but I still don't quite understand could you explain in greater detail and go more in-depth on what all parts of this program does? It may seem simple to you but it feels overly complicated from my view and thank you for your help.
KingofGamesYami #4
Posted 22 November 2018 - 04:25 PM
I can't go into great detail right now b/c im on my phone, but essentially I generate an x and y coordinate every 5 with an offset like (15,15), (20,15) … up to the number of trees requested. The coordinates are also placed inside the smallest square that can contain that many trees, by finding the side length of a square of that area and rounding up. So requesting eg. 20 trees would get 4 < sqrt(20) < 5 and would round up to a 5x5 square.

It's mostly just basic algebra, tho modulus isn't really taught in school. Basically it returns the remainder of dividing, so 8 % 7 would return 1, or 8 % 4 would return 0.
killer95533 #5
Posted 22 November 2018 - 04:53 PM
oh ok, that makes sense thank you but how would the trees be randomly spawned in then if there are 5 apart between each tree if that's the case how could it be random?
KingofGamesYami #6
Posted 22 November 2018 - 05:25 PM
Its not random. Nowhere in the original post did you specify any degree of randomness. You could add a random number between -2 and 2 to each x and y coordinate to make it appear somewhat random, or you could get into actual procedural generation which is a bit more complicated.
killer95533 #7
Posted 02 December 2018 - 07:09 AM
Oh, my apologies I thought I mentioned randomness in the original post and actual procedural generation is the goal although I'm not that skill yet I wouldn't mind learning how to do procedural generation and thank you for your help.