ID:1493686
 
(See the best response by Koshigia.)
Problem description: I know I should be using objects to do this. But here goes, I'm randomly generating platforms at run time for a mini game.

I'm just having a couple issues with turfs being created north or south of each other. With my limited DM knowledge I'm unsure of how to go any further.



Code:
    plat_spawn()
for(var/P in 1 to 50)
var/X = rand(1,14)
var/Y = rand(1,95)
var/T = /turf/plat/
if(isobj(locate(X,Y,1)) || isturf(T, (locate(X,Y,1))))
//make sure nothing is at the location.
return

else if(isturf(T, (locate(X,Y,1)) in get_step(T, NORTH)))
return

else if(isturf(T, (locate(X,Y,1)) in get_step(T, SOUTH)))
return

new T (locate(X,Y,1))




Broken link for the image
Fixed, sorry about that.
Best response
Ok, let's examine this line:
else if(isturf(T, (locate(X,Y,1)) in get_step(T, NORTH)))


If I understand what you are trying to do, isturf() is the wrong procedure for this. isturf() will return true if all arguments are valid turfs. var/T, by definition, is not a turf. It is a type path. You would want to go back to using istype() as you did in the first if statement.

The second thing you would want to change is the get_step() part. Instead of trying to use that where it is unnecessary, you can simply use locate with offset coordinates like so:

else if(istype(locate(X, Y+1, 1), T))
return


furthermore, you could eliminate the lengthy if-then statement by using loops for the coordinates you would like to use. Lets say you would like to make sure there is nothing directly above or below (by one space), and nothing to either side by at least 2 spaces, you could do something like this.

...

for(var/x_off = -2, x_off <= 2, x_off++)
for(var/y_off = -1, y_off <= 1, y_off++)
var/turf/this_turf = locate(X + x_off, Y + y_off, 1)
if(istype(this_turf, /turf/plat))
return

//if none of those turfs are platforms, then go ahead and create a new platform...


Those are called nested loops and can be very useful.


*edit*
I noticed that your arguments are backwards in the first istype(). Please refer to the documentation (F1 from dreammaker)
Thanks! Some really useful information here and I wondered why istype() wasn't working properly.
In response to Rickoshay
No problem =)