Essentially what I need to do is to be able to pick out mobs/objs with certain characteristics fairly easily from a fairly large group.
Lets say there are 50 people bunched together. 40 of them have blue hats, 5 have green hats, and 5 have red hats. They are all the same type and can change their hats at anytime. If I wanted to go and punch all people with red hats (for some reason...) is there anyway I could locate them easily without walking up to each person and looking at their hat?
I know how to loop through them all and find what I need but it seemslike a really inefficient to find a minority within a majority.
Any ideas?
ID:154190
Apr 15 2002, 7:58 pm
|
|
Apr 15 2002, 8:17 pm
|
|
Why go through the trouble of looking for things when you can just keep track of where they are? In coding terms, maintain smaller lists corresponding to each characteristic you need "indexed." You could put some code inside the New() proc that checks to see if the object being created has any indexed traits, and add them to the appropriate list(s).
|
Well I wish we could do stuff like for (var/mob/M with hat="red" in world) but that's more of a feature request than any help to you.
Z |
In response to Lesbian Assassin
|
|
The main problem is that I need to find people with red hats within my view and ignore ones not in my view.
Incase it helps, the application is actually ants patrolling arround for enemies. They look at all the ants in their view and if they find an ant that isn't from their colony they attack it. I guess my little analogy wasn't completely accurate, sorry for the vagueness. |
Check which have the "red hat" var. When you change the persons hat, change their hat variable. and do a for() check for the mobs that have the red hat var.
|
In response to Sariat
|
|
Sariat wrote:
Check which have the "red hat" var. When you change the persons hat, change their hat variable. and do a for() check for the mobs that have the red hat var. I think he was aware of this option. He wanted to know if there was another way to do it that was more efficient (in terms of processing time). |
In response to English
|
|
I wonder how easily and efficiently you could check for overlap between two given lists... then you could combine Lexy's idea with use of view() and check for ants in the intersection of the enemy ant list(s) and view().
|
In response to Leftley
|
|
Leftley wrote:
I wonder how easily and efficiently you could check for overlap between two given lists... then you could combine Lexy's idea with use of view() and check for ants in the intersection of the enemy ant list(s) and view(). Hmmm... how about: proc/intersection(list/L1, list/L2) var list/combined list/exclusion1 list/exclusion2 list/overlap combined = L1 + L2 exclusion1 = L1 - L2 exclusion2 = L2 - L1 overlap = combined - exclusion1 - exclusion2 return overlap Or less readably, but maybe more efficient: proc/intersection(list/L1, list/L2) return (L1 + L2) - (L1 - L2) - (L2 - L1) Untested, and I'm sure you're better at this kind of stuff. It would be nice to have some functions like this for lists, though. |
In response to Leftley
|
|
Leftley wrote:
I wonder how easily and efficiently you could check for overlap between two given lists... then you could combine Lexy's idea with use of view() and check for ants in the intersection of the enemy ant list(s) and view(). Interesting idea. I'm not sure if there is a simple way to find an overlap of two lists, but you could easily subtract the list of ants on your team from the list returned by view. var/list/filtered_view = view() - list_of_ants_on_my_team |
In response to Shadowdarke
|
|
That's probably the best idea that's been posted here...
My only question is, why are the ants wearing hats? |
In response to Lesbian Assassin
|
|
Have you ever looked at ants? They're practically all identical. Sure, you might find one variety of big black spiky ones and one variety of little beady red ones, but all the ants coming out of the same cluster of anthills--usually plenty of seperate nests spread over a pretty good area--are all going to look exactly the same. And if we can't tell the difference, how do you expect a bunch of stupid ants to tell each other apart without visual cues? Hence, hats.
|
In response to Leftley
|
|
LOL, that's the best theory for ant wearing hats I've ever heard :) (not that I've heard many :p)
The ants aren't actually wearing hats, that was just part of my poor analogy :p Anyway, I'll look into subtracting the lists because each colony has a list of all its ants. There are some complications such as multiple colonies per player but they'll mostly be seeing ants from their own colony so it should cut down on the looping by a considerable amount. This leaves one more question then. How efficient are subtracting lists? It seems like when you subtract them it would have to do a search for each item in one list and remove it from the second list. That might eat up just as much CPU as just looping through them all in the first place. Unless someone has a quick answer I'll run a test program when I get home to see which is faster. Thanks for the input! |
In response to English
|
|
English wrote:
This leaves one more question then. How efficient are subtracting lists? It seems like when you subtract them it would have to do a search for each item in one list and remove it from the second list. That might eat up just as much CPU as just looping through them all in the first place. Subtracting lists would require all that, but the good news is that list subtraction is part of the BYOND engine. It need not be interpretted to machine code at runtime and will execute much faster than if we coded it manually in DM. The only way to know for sure is to test it for yourself. ;) |