Changing who; race sections

Posted by Zeno on Wed 06 Sep 2006 12:16 AM — 5 posts, 19,616 views.

USA #0
What would be a good way to change the who code so that it splits up each race into a section? I can think of a way to do this, but it involves looping through all players and checking their race, thus making it 3 loops (for each race). I'm sure there's a better way to do this.

Example who output (not exact):
=====Race1=====
Bob
Joe
=====Race2=====
James
=====Race3=====
Gin
Rob
=====Immortals=====
Zeno
USA #1
I think that looping through the players 3 times isn't such a bad solution; you won't ever have enough players for it to really make things inefficient. (You'd need several thousand before it would start mattering.)

To be fast, you'd need to do complex things like keep your player list sorted into three buckets, one per race. That's way too complicated for what you're doing, though, I think.
USA #2
A relatively simple way to accomplish the "sorting into three buckets" would be to use linked lists.

You could make three local linked lists (one for each race) and loop through everyone once, adding them to their respective local linked list. You could also then sort by level, class, or whatever else you wanted.

Then, for output, you just print out your "=====Race=====" header and print out each person in your presorted order.

Thus you eliminate the need for a third iteration of each person (each person would get processed twice) and you get the added bennefit of sorting.
USA #3
That would be a good way of doing it. It would also make it very easy to sort as you inserted, even though the easy sort (insertion sort) is a fair bit slower than quicksort. Insertion sort is O(n^2), I believe, whereas Quicksort is O(n log n), which is a quite a lot better.

Again, though, for these efficiency concerns to be relevant, you need at least 4-digit numbers of players.
USA #4
Well, who was sort of complicated but not really. Turns out I just had to add a WT_RACE, a new who->type and bam.