ID:160612
 
Question spawning from id:656943

How would I go about creating a "smart" verb that chooses several objects based on said objects' stats?

I understand the basis would be a loop iterating as many times as a number input from the user, and checking a potential selection's variables, but I can't get it to work.
There are definitely lots of approaches to do this, but here's one. Filter your objects to categories, and when the first-priority one has nothing in it, check the 2nd priority one, etc. There may be a better method, but I'm too tired to think too much. :P Usually you'd use a list for each categoey, but in your case there's no difference between choosing one hungry guy or another, so you only need to have one reference for each category:
proc/get_free_survivor(list/survivors)
var/survivor/hungry
var/survivor/starving
for(var/survivor/S in survivors)
if(!S.hunger) //not hungry
return S //choose right away
switch(S.hunger)
if(1) //hungry value
if(hungry == null) //not assigned yet - this just ensures it keeps the first hungry guy it finds, this isn't necessary
hungry = S
if(2) //...starving value
if(starving == null)
starving = S
//if we reached here, it means everybody is either hungry or starving, otherwise 'return' would have stopped the proc

if(hungry) //if we found someone hungry
return hungry //pick him
//otherwise,
return starving //return the starving person
//you could switch the last part with this:
return hungry || starving


Also, you could move the !S.hunger part to the switch() as well, would probably be slightly faster:
switch(S.hunger)
if(null,0,"") //false -- and likely your game doesn't need to check for the last value
return S //he's not hungry, so return
#define SCOUT "scout" //just to make sure you don't type it wrong :P

mob/proc/Find_Criteria(var/N as num)
var/list/Found = new //create a list
for(var/mob/survivor/S in survivors) //for every survivor in the survivors list
if(N <=0) break //if the num is less than or 0 end the loop and return
Found += S //add the survivor to the list
return Found //self explantory :P

mob/verb/Have_People_Scout(N as num)
for(var/mob/survivor/S in Find_Criteria(N)) //Find_Criteria returns a list, so for everything in said list
S.action = SCOUT //change their action to scout


You can edit this as you like, adding more restriction to the Find_Criteria proc etc.


Keep in mind this could be used to use my idea!
In response to Jeff8500
Thanks a lot! This is working a treat now.