ID:176566
 
On a large map, how could I run a proc that selects a single piece of turf of a particualr type (assuming there are several of these turf types in the world (I just want one of them)) and manipulate that?
Example:
In a large city, I want a single house to burst into flames and set the usr.client.eye to that house.
Maybe adding a different tag to each turf? then u can locate it using that tag.
well if you know were that turf is located befor runtime, you can always just us its location to point some proc to it. I didnt understand your point all the way, so i might not be helping.
Krosse wrote:
On a large map, how could I run a proc that selects a single piece of turf of a particualr type (assuming there are several of these turf types in the world (I just want one of them)) and manipulate that?
Example:
In a large city, I want a single house to burst into flames and set the usr.client.eye to that house.

To select a random turf of a given type, you'd do this:
var/turf/mytype/T
var/count=0
// count turfs of this type
for(T) ++count
// pick a random number from 1 to count
count=rand(1,count)
// find the "count"th turf
for(T)
if(--count<=0) break
If you don't know the type going in, you have to elaborate a little more:
var/turf/T
var/count=0
// count turfs of this type
for(T)
if(istype(T,searchtype)) ++count
// pick a random number from 1 to count
count=rand(1,count)
// find the "count"th turf of searchtype
for(T)
if(!istype(T,searchtype)) continue
if(--count<=0) break
Or, if you don't want to loop through that many turfs every time, and there are a limited number to choose from, building a list might not be a bad idea.
var/turf/T
var/list/L=new
for(T) if(istype(T,searchtype)) L+=T
T=pick(L)

Lummox JR
In response to Weedman
Well, thank you all for your advice. I think I'll go with Lummox's suggestion. He's never steered me wrong before and it seems to fit the best. Sorry if my example was a bit cryptic.
In response to Lummox JR
By the way, I assumed that the "break" line was to avoid an infinite loop, but I soon discovered that if there is no turf of the specified type, it will result in an infinite loop. Just for future reference.

(Not a big deal as, if you write this into your code, obviously, you're going to have that turf type in the program.)
In response to Krosse
Krosse wrote:
By the way, I assumed that the "break" line was to avoid an infinite loop, but I soon discovered that if there is no turf of the specified type, it will result in an infinite loop. Just for future reference.

(Not a big deal as, if you write this into your code, obviously, you're going to have that turf type in the program.)

Ah--a way around that is to abort the proc if count==0, before picking a random value. (I hadn't thought about that; the count=rand(1,count) will set count=1.) Or you can use the list method. This shouldn't be an infinite loop because the second loop will end on its own when it runs out of turfs, though. It's just less desirable behavior than not going through the loop at all.

Lummox JR