Re: [CODE] Level-sorted do_who

From: Patrick Dughi (dughi@imaxx.net)
Date: 04/07/99


> My coders are attempting to order out wholist by level, higher levs being
> on top, sorting down to lower levs. We're unsure of how to do it really,
> and we're just screwing around with stuff that works, forgetting about
> efficiency (yes, i know, *shudder*). It's not my project, really, so I
> can't modify the files, but the constant crashing of the mud is beginnning
> to annoy me greatly...so I'm trying to help out. Here it is, hacky as can be.
>

        That code you posted was really rather scary.  As much as I
dislike people jumping on the list saying something along the lines of
'Use a linked list' as if they were the end-all-be-all construct, I
believe that may be what would work best for you.  First, just make a nice
struct:

struct your_list = {
  int level;
  struct char_data *ch;
  struct your_list *next;
};

        Now, find all your characters online at the time simple enough:
    struct your_list *your_list = NULL,*temp=NULL,*temp2=NULL;
    struct descriptor_data *d;
    int found_slot=0;

    for(d = descriptor_list;d;d=d->next) {
      /* stuff */
    }

        And within those for loops, you're going to do two things, 1)
determine who's going in that who list (invis, etc), and 2) put them in
the 'your_list' ..sorting for your insertion.

        Step 1 is up to you, step two is easy:
      CREATE(temp,struct your_list, 1)
      temp->ch = d->character;
      temp->level = GET_LEVEL(d->character);

      if(!your_list)
        your_list=temp;
      else {
        for(temp2=your_list;temp2 && !found_slot;temp2=temp2->next) {
          if(temp2->level >= temp->level &&
                (!(temp2->next) || (temp2->next)->level < temp->level)) {
            temp->next=temp2->next;
            temp2->next = temp;
            found_slot++;
          }
        }
      }

      Which should insert it in order.

        Then, at the end, you iterate down your 'your_list' list, and
make the buffer;
        sprintf(buf,"Your who list.\r\n");
        while(your_list) {
          sprintf(buf + strlen(buf),"%s the level %d adventurer.\r\n",
                GET_NAME(your_list->ch), your_list->level);
          your_list=your_list->next;
        }

       And then you send your buffer to the person.

        Of course, and as usual, I may be wrong, and all the above is
mailer code, so examine it before just plopping it in.

                                                        PjD


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST