Re: [Code] Looping through Mobs. <Pine.BSI.3.95.980715212428.24079A-100000@shell1.cybercom.net>

From: Kevin Doherty (kdoherty@JURAI.NET)
Date: 07/15/98


Thus spake Ryan Gasper or Steven Arnold <dalamar@CYBERCOM.NET>:
>  Could someone please tell me why the below code is not working? I have
>gone over it and put in some log checks, but cannot find out why its not
>working. I believe that after a mob vnum is equal to masters[i].master,
>it does not kick out of the loop correctly and mob is changed.
>
>  for (mob = world[ch->in_room].people; mob || !found; mob = mob->next_in_room
>)
>    for (i = 0; masters[i].master != -1; i++) {
>      sprintf(buf, "MOB Found, VNUM = %d", GET_MOB_VNUM(mob));
>      log(buf);
>      if (GET_MOB_VNUM(mob) == masters[i].master)
>      {
>        found = TRUE;
>        log("Second Step Complete");
>        break;
>      }
>    }
>log("Beginning of Step 3");
>  if (!mob) {
>    send_to_char("There is no one here to train you.\r\n", ch);
>    return;
>  }
>
>I get the message "There is no one here to train you" even though above I
>get Second Step complete meaning mob cannot be !mob. Also, if I add in a
>Check for the mobs vnum right after Beginning of step 3 log, it crashes
>with a seg fault. Anyone have any ideas?

In your outermost loop, the loop's conditional should be mob && !found,
not mob || !found. As it is, the value of found is only checked if mob
is NULL. As a result, presumably the master is always found, so it loops
until mob is NULL. Therefore any checks of the mob's vnum after the loop
will produce a segfault, and !mob will always be true. Of course, if the
master isn't found, you get an infinite loop. Yay.

--
Kevin Doherty, kdoherty@jurai.net
"Multiple exclamation marks are a sure sign of a diseased mind."
       -- Rincewind (from _Eric_)


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



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