[CODE] Help with Introduction/Memory Code?

From: Mathew Earle Reuther (graymere@zipcon.net)
Date: 08/14/02


I'm working on adapting Erwin's MERC introduction/memory system to circle
and have run into a number of questions.  Perhaps someone feels like
taking a shot at helping me out on this?  Goal of course is to create a
fully-functional version of an introduction/memory system which can then
be at least in part placed in the circle ftp site.  (Because it was
honestly quite a pain to track down even the MERC stuff!)

Changes made thusfar:

Added to struct player_special_data in structs.h:
     int *memory;        /* PC's memory for intro system         */


Added to db.c:
  (in load_char):
          } else if(!strcmp(tag, "Mem ")) {
            ch->player_specials->memory = fbgetstring(fl);
  (in save_char):
      if(ch->player_specials->memory && *ch->player_specials->memory) {
        strcpy(buf, ch->player_specials->memory);
        fbprintf(fl, "Mem : %s\n", buf);
      }


Created a memory.c file containing the following code:

  /* Does ch know victim? */
  bool knows_char(struct char_data *ch, struct char_data *victim)
  {
    int i;

    /* Characters always know NPCs and IMMORTALS and vice versa */
    if (IS_NPC(ch) || IS_NPC(victim) || (GET_LEVEL(ch) >= LVL_IMMORT)
                   || (GET_LEVEL(victim) >= LVL_IMMORT))
      return TRUE;


    /* Simple sequential search. If the data was sorted, a binary search
       could be attempted */
    for (i = 1; i < ch->player_specials->memory; i++)
      if (ch->player_specials->memory[i] == GET_IDNUM(victim))
        return TRUE;

    return FALSE;
  }

  /* Add victim to ch's know list */
  void add_know(struct char_data *ch, struct char_data *victim)
  {
    int *memory;

    if (knows_char(ch,victim))
      return;

    /*  Allocate space for the new array
     *  We need 1 int for the counter, 1 for the new addition and
     *  as many ints as the people that ch used to know
     *  Alternatively, the libc memory manager rather than MUD's own
     *  could be used, and with it, realloc
     */

    /* This function had slight problems -jun99 */
    memory = malloc(sizeof(int) * (ch->player_specials->memory[0] + 1));
    memory[0] = ch->player_specials->memory[0] + 1;
    memcpy(memory+1, ch->player_specials->memory+1,
      (ch->player_specials->memory[0]-1) *sizeof(int));

    /* This could be extended to insert in a sorted order */
    memory[ch->player_specials->memory[0]] = GET_IDNUM(victim);

    free(ch->player_specials->memory);
    ch->player_specials->memory = memory;
  }

Questions:

Is malloc() the correct method of memory allocation to use in add_know?
(And of course, free() afterwards?)

Why do I get a warning for comparing a pointer to an integer in this line
of knows_char():
    for (i = 1; i < ch->player_specials->memory; i++)

Did I add the int *memory to the right structure? (And is int* memory;
correct in the first place?)

Am I saving and loading the data to the pfile (ASCII, of course) in a
manner which will allow it to function in the intended manner?

-Mathew

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT