ID:1489987
 
(See the best response by Kaiochao.)
Problem description: Hey everybody, now I'm looking to place random turfs on the map at run-time.

I was wondering how would I check to make sure there isn't another turf in that spot so it won't place it there? To be honest you're probably gonna laugh at how bad this piece of code is, but hey I'm trying to learn!

Code:
proc
clouds(var/turf/O = /turf/clouds/cloud1/)
for(var/T in 1 to 50) //what i believe makes 50, unsure right now.
var/X = rand(1,14)
var/Y = rand(1,100)
new O (locate(X,Y,1))
return



var/turf/T = locate(X,Y,1)
if(isnull(T)) new O locate(X,Y,1)
else world << "Turf already here"

This would be my first try. I'm not sure if turfs are null at a location or not, so it might not work.
In response to Lugia319
Nah didn't work, did help me find isobj() and isturf() though, thanks. Changed it up a little bit but this one create about 2 clouds.

var/clouds = 50

proc
clouds(var/O = /obj/clouds/cloud1/)
while(clouds > 0)
var/X = rand(1,14)
var/Y = rand(1,100)
//var/turf/T = locate(X,Y,1)
var/obj/clouds/T = locate(X,Y,1)
if(!isobj(T))
new O (locate(X,Y,1))
clouds--
return 0
sleep(10)
In response to Lugia319
So I eventually got it to work. Just makes the game a little sluggish.

var/clouds = 50

proc
cloudcheck()
if(clouds <=0)
return
while(clouds > 0)
spawn(-1) clouds()
sleep(20)

clouds(var/O = /obj/clouds/cloud1/)
if(clouds <= 0)
return
var/X = rand(1,14)
var/Y = rand(1,100)
//var/turf/T = locate(X,Y,1)
var/obj/clouds/T = locate(X,Y,1)
if(!isobj(T))
new O (locate(X,Y,1))
clouds-- </dm<
Why not make your clouds objs? I'm pretty sure you want to avoid creating turfs at runtime unless it's absolutely necessary.

Also, more times than not, bad things come from using spawn in a loop like that (especially since it's not sleeping - indentation error, maybe?).

Off-top, I can't really tell you what the "best" way is, but I'd say using areas and changing their icon_state should do what you're trying to do and do it a lot more smoother than what you have there.
In response to Lugia319
Best response
Having a turf be null at a location would mean the X or Y are outside the map's bounds (world.maxx×world.maxy). It doesn't make sense otherwise.

You can't have a position on the map that doesn't have a turf. It's fundamentally nonsense, since turfs are map positions. It's not "each position on the map has a turf," it's "every position on the map is a turf."

You can create turfs on other turfs to replace them. If you want the previous turf to appear behind the new turf, you're thinking about it incorrectly and should use a movable atom instead, or use an overlay (which is what the map editor does when you try to stack turfs).
So when you delete turfs, they are replaced by a null turf? Some kind of placeholder turf?
In response to Lugia319
world.turf
In response to FKI
@FKI - Not sure how I would go about doing it the area way. Wouldn't that make all the icon_states in that area the same?

Also, this seems to be the working code. Clouds will be created next to each other but, they don't overlap anymore. For some reason if I tried isobj(X+2,X-2) and X-2, X+2 etc, it would just make the overlap again. Probably misinterpreting something there.

var/clouds = 50

proc
cloudcheck()
set background = 1

while(clouds > 0)
if(clouds <= 0)
return
clouds()
sleep(20)

clouds(var/O = /obj/clouds/cloud1/)
var/X = rand(1,14)
var/Y = rand(20,100)

if(clouds <= 0)
return

if(isobj(X,Y) || isobj(X+1,Y+1) || isobj(X-1,Y-1) || isturf(X,Y))
//check for overlapping objs and turfs. Due to cloud being 64x64 and without isturf will go over turfs
return

else if(!isobj(X,Y))
clouds --
new O (locate(X,Y,1))
In response to FKI
FKI wrote:
Off-top, I can't really tell you what the "best" way is, but I'd say using areas and changing their icon_state should do what you're trying to do and do it a lot more smoother than what you have there.

Area updates trigger the same map refresh that turf updates do. Thus, they have the same negative impact as runtime turf creation.
So for anyone that's interested (probably no one). For like the third time I've got it. I'm English if you're wondering why it took so long I went to bed, fresh mind and all that jazz.

So here goes, if anyone wants to add any pointers on making this more efficient or any tips at all would be most appreciated.

var/clouds = 40

proc
cloudcheck()
set background = 1

while(clouds > 0)
if(clouds <= 0)
return
clouds()
sleep(2)

clouds(type = /obj/clouds/cloud1/)
var
X = rand(1,14)
Y = rand(20,100)
check_y

if(clouds <= 0)
return

for(var/turf/plat/p)
locate(p.y)

check_y = p.y //Only works when i check for y, if I check for X it puts all on the 1,Y,1

if(check_y == Y)
return

if(!isobj(check_y)) //&& isturf(check_y))
clouds --
new type (locate(X,Y,1))