Ok.... two questions on that one:
1. while (enum1.GetNext(u)!=0) should be changed to
while (enum1.GetNext(u)) because 0 is a false value, all other values are true. (The latter also generates more efficient machine code)
2. why do you need to check the type of each Unit that is returned? since the type is specified to the enumerator constructor. (So it would only return that specific type of unit).
I have some useful code too. Here's a couple functions.
__inline LOCATION GetMapSize() const
{
// if your map HAPPENS to be bigger than 10000*10000, increase this
MAP_RECT mr(0, 0, 10000, 10000);
mr.ClipToMap();
return LOCATION(mr.x2, mr.y2);
}
__inline LOCATION RandMapPt() const
{
return GetMapSize().RandPt();
}
Those two functions return the dimensions of the map, and get a random point in the map, respectively. (The 2nd one depends on the 1st, so you need both if you're going to use random map points). There's probably a more efficient way to do it, but this is the best way to do it without having to hack into memory.
I marked the functions as __inline since they are relatively short and might be called repeatedly. (__inline, in the MS compiler, tells it to include the code from the function "in line" with the calling code. This is faster because the machine code instructions for pushing parameters to the function, calling it, and setting up and cleaning up the stack don't have to be done, because in the generated machine code there is no "subroutine.")
Anyway, for people who aren't used to __inline: There's few times when you should use it. On large functions with many calls it should never be used: the code for the function gets inserted multiple times, and your final executable size is a lot bigger.
For short functions that are a little bit more powerful than macros, which probably execute just as quickly as it takes to call the function, __inline is a good tool to speed up your code.