ID:827689
 
(See the best response by DarkCampainger.)
Code:
world
{
New()
{
for(var/i = 1 to 15)
{
while(1)
{
var/x = rand(223, 259)
var/y = rand(303, 277)

var/turf/t = locate(x,y,1)
if(t.density) continue
new /turf/tree(t)
t=locate(x+1,y,1)
new /turf/tree/tree_top(t)
break
}
}
}
}


Problem description:
Well, I would like for it to be created on top of the grass turf. I'm not sure how to go about that (thanks Forum_Account for the tutorial on NPCs (i used it as references for this)) This is what it looks like ingame;

http://oi45.tinypic.com/fa3lfb.jpg

This is how the tree is created
turf/tree
{
icon = 'treeic.dmi'
density=1
name = " "
icon_state="base"
tree_top
{
icon_state="top"
density=0
layer=MOB_LAYER+2
}
}

Would I change the layer of the turf...or?
Best response
The issue is that there can only be one turf per tile, so when you create a new one, the old one is replaced. The easiest way to get around this is to make your trees /obj instead of /turf.

If you need them to be turfs for some reason, you can also use the same method that stacked turfs in the map editor use: create an image of the existing turf, and add it as an underlay to the new turf.

Also, instead of (x+1,y,1) for the tree_top, you want (x,y+1,1). But that said, with big icon support, you can just make a 32x64 icon and have your tree use a single icon and ditch the second turf/obj.


Also, it's worth mentioning that you're relying on chance for your spawning loop to find a valid location. It may work fine for a small number of tree in a large, open area; but if you use it in a denser area for many trees, it may begin to take excessive amounts of time to place all of them. Instead, I would suggest using block() to grab all of the turfs in the area as a list and store it. Then, for each tree you want to place, pick a turf from that list. If it's invalid, remove the turf from the list and try again. If it's valid, place the tree and remove the turf from the list. Just for safety, you may want to break out of the loop if the list becomes empty (fewer open spaces than trees to place). This way, it will always be able to find the remaining valid locations.
Hmm, I see, thanks.