Clan Integration issues with save clan and load clan

  • Nero
  • Nero's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 month 3 weeks ago #10084 by Nero
Hello,

I am trying to integrate Clans into the game. I have it working for the most part but there seems to be an issue where if the game crashes/boots it is not reading the clan file properly. I believe its how the load_clan and save_clan functions are and how its not actually creating new lines in the file like it should. But I am not really sure how to correct it. I hope this is the reason anyway.
 /* Loads the clans from the text file */
 void load_clans(void)
 {
   FILE   *fl;
   int    clannum = 0, line_num = 0, i = 0, tmp, curmem = 0;
   char   name[80];
   char   *ptr;
   struct clan_type *cptr = NULL;
   bool   newc = FALSE;
   char buf[MAX_STRING_LENGTH];

   if ((fl = fopen(CLAN_FILE, "rt")) == NULL) {
     fprintf(stderr, "SYSERR: Unable to open clan file!");
     exit(0);
   }

   line_num += get_line(fl, buf);
   if (sscanf(buf, "%d", clannum) != 1) {
     fprintf(stderr, "Format error in clan, line %d (number of clans)\n", line_num);
     exit(0);
   }
   /* Setup the global total number of clans */
   cnum = clannum;
   
   /* process each clan in order */
   for (clannum = 0; clannum < cnum; clannum++) {
     /* Get the info for the individual clans */
     line_num += get_line(fl, buf);
     if (sscanf(buf, "#%d", &tmp) != 1) {
       fprintf(stderr, "Format error in clan (No Unique GID), line %d\n", line_num);
       exit(0);
     }
     /* create some clan shaped memory space */
     if ((cptr = enqueue_clan()) != NULL) {
       cptr->number = tmp;

       /* setup the global number of next new clan number */
       if (!newc) {
         if (cptr->number != clannum) {
           newclan = clannum;
           newc = TRUE;
         }
       }

       if (newc) {
         if (newclan == cptr->number) {
           newclan = cnum;
           newc = FALSE;
         }
       } else
         newclan = cptr->number + 1;
 
       /* allocate space for applicants */
       for (i=0; i< 20; i++)
         cptr->applicants[i] = NULL;

       for (i=0; i< 100; i++)
         cptr->members[i] = NULL;
     
      /* Now get the name of the clan */
       line_num += get_line(fl, buf);
       if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
         *ptr = '\0';
       cptr->name = strdup(buf);

       /* Now get the look member string */
       line_num += get_line(fl, buf);
       if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
         *ptr = '\0';

       cptr->member_look_str = strdup(buf);
       /* Now get entrance room and direction and guard */
       line_num += get_line(fl, buf);
       if (sscanf(buf, "%d", &tmp) != 1) {
         fprintf(stderr, "Format error in clan, line %d (entrance room)\n", line_num);
         exit(0);
       }
       cptr->clan_entr_room = tmp;

       /* Skip this line it's just a header */
       line_num += get_line(fl, buf); // password header

       line_num += get_line(fl, buf);
       if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
         *ptr = '\0';
       cptr->clan_password = strdup(buf);

       line_num += get_line(fl, buf); // gold header
       
       line_num += get_line(fl, buf);

       if (sscanf(buf, "%d", &(cptr->gold)) != 1)
       {
         fprintf(stderr, "Format error in clan (No Gold), line %d\n", line_num);
         exit(0);
       }
        

        
       line_num += get_line(fl, buf); //leader header

       /* Loop to get Members' Names */
       for (;;)
       {
         line_num += get_line(fl, buf);
         if (*buf == ';')
           break;
         sscanf(buf, "%s %d", name, &tmp);
         if (tmp == CLAN_LEADER)
           cptr->leadersname = strdup(name);
       }

       //line_num += get_line(fl, buf); // member Header

       /* Loop to get Members' Names */
       for (;;)
       {
         line_num += get_line(fl, buf);
         if (*buf == ';')
           break;
         else if ((ptr = strchr(buf, '~')) != NULL)
           *ptr = '\0';
         cptr->members[curmem++] = strdup(buf);   
       }
       curmem = 0;

       // header caught by break statement

       /* Okay we have the leader's name ... now for the applicants */
       for (i = 0; i < 20; i++) {
         line_num += get_line(fl, buf);
         /* We're done when we hit the $ character */
         if (*buf == '$')
           break;
         else if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
           *ptr = '\0';
         cptr->applicants[i] = strdup(buf);
       }
     } else break;
     /* process the next clan */
   }
   /* done processing clans -- close the file */
   fclose(fl);
 }


 void save_clans(void)
 {
   FILE   *cfile;
   int    clannum = 0, i;
   struct clan_type *cptr = clan_info;

   if (cptr == NULL) {
     fprintf(stderr, "SYSERR: No clans to save!!\n");
     return;
   }
   if ((cfile = fopen(CLAN_FILE, "wt")) == NULL) {
     fprintf(stderr, "SYSERR: Cannot save clans!\n");
     exit(0);
   }

   order_clans();

   /* The total number of clans */
   fprintf(cfile, "%d\r\n", cnum);
   /* Save each clan */
   while (clannum < cnum && cptr != NULL) {
     fprintf(cfile,  "#%d\r\n"
             "%s~\r\n"
         "%s~\r\n"
         "%d\r\n"
         "; Password\r\n"
         "%s~\r\n"
         "; Gold\r\n"
         "%d\r\n"
         "; Leader\r\n"
         "%s %d\r\n",
         cptr->number,   cptr->name,
         cptr->member_look_str,
         cptr->clan_entr_room,
         cptr->clan_password,
         cptr->gold,
         cptr->leadersname, CLAN_LEADER);
    fprintf(cfile, "; Members (Max of 100)\r\n");

     if (cptr->members[0] != NULL)
     {
       for (i = 0; i < 100; i++) {
         if (cptr->members[i] == NULL)
           break;
         else
           fprintf(cfile, "%s~\r\n", cptr->members[i]);
       }
     }

     fprintf(cfile, "; Applicants (Max of 20)\r\n");

     if (cptr->applicants[0] != NULL) {
       for (i = 0; i < 20; i++) {
         if (cptr->applicants[i] == NULL)
           break;
         else
           fprintf(cfile, "%s~\r\n", cptr->applicants[i]);
       }
     }
     fprintf(cfile, "$\r\n");
     /* process the next clan */
     cptr = cptr->next;
     clannum++;
   }
   /* done processing clans */
   fclose(cfile);
 }
Attachments:

Please Log in or Create an account to join the conversation.

More
1 month 3 weeks ago #10085 by thomas
I think if you open this file in notepad, atom or vscode instead of word (or wordpad or whatever this is), you'll see the correct newlines in it.

Please Log in or Create an account to join the conversation.

  • Nero
  • Nero's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
1 month 3 weeks ago - 1 month 3 weeks ago #10086 by Nero
Okay thank you. I am not sure why I am still having an issue with getting the clans to load up after crash. If the game crashes and I load back in, all the clans are still in the save file but if I do cedit, option L it acts like it isn't as it seems to be starting back at clan 0 which causes the game to crash and deletes/overwrites what was already in the file.

I am sure that most of the odd behavior stems from the fact that I am trying to integrate github.com/ryantm/deimos-mud/blob/master/src/cedit.c into TBA mud's version of cedit.c

*-------------------------------------------------------------------*/

void cedit_clan_menu(struct descriptor_data * d)
 {
 
   if (!OLC_CLAN(d))
     cedit_clan_new(d);

   get_char_colors(d->character);

   write_to_output(d,
     "-- Clan number : [%s%d%s]\r\n"
     "%s1%s) Name        : %s%s\r\n"
     "%s2%s) Leader      : %s%s\r\n"
     "%s4%s) Clan Abbrev : %s%s\r\n"
     "%sP%s) Purge this Clan\r\n"
     "%sQ%s) Exit To The Main Menu\r\n"
     "Enter choice : ",
     
     cyn, OLC_CLAN(d)->number, nrm,
     grn, nrm, yel, OLC_CLAN(d)->name,
     grn, nrm, yel, OLC_CLAN(d)->leadersname,
     grn, nrm, yel, OLC_CLAN(d)->member_look_str ?
     OLC_CLAN(d)->member_look_str : OLC_CLAN(d)->name,
     grn, nrm,
     grn, nrm
     );

   OLC_MODE(d) = CEDIT_CLAN_MENU;
 }

  void cedit_free_clan(struct clan_type *cptr)
 {
   dequeue_clan(cptr->number);
 }



 //confirm clan save
  case CEDIT_CONFIRM_SAVE:
    switch (*arg) {
     case 'y':
    case 'Y':
     if (OLC_VAL(d)) {
       save_clans();
      // sprintf(buf, "OLC: %s edits clan %d", GET_NAME(d->character), OLC_CLAN(d)->number);
        mudlog(CMP, MAX(LVL_BUILDER, GET_INVIS_LEV(d->character)), TRUE,
                 "OLC: %s edits clans", GET_NAME(d->character));
       write_to_output(d,"Clan saved to disk.\r\n");
       cleanup_olc(d, CLEANUP_CONFIG);
        } else
           cleanup_olc(d, CLEANUP_CONFIG);
       return;
     case 'n':
     case 'N':
       /* free everything up, including strings etc */
       cleanup_olc(d, CLEANUP_CONFIG);
       return;
   default:
      write_to_output(d, "\r\nThat is an invalid choice!\r\n");
     write_to_output(d, "Do you wish to save the configuration? (y/n) : ");
      return;
    }

case CEDIT_CLAN_MENU:
       switch (*arg) {
       case '1':
         write_to_output(d,"Enter clan name:-\r\n| ");
         OLC_MODE(d) = CEDIT_NAME;
         break;
       case '2':
         write_to_output(d,"Enter clan leader's name: ");
         OLC_MODE(d) = CEDIT_LEADERSNAME;
         break;
       case '4':
         write_to_output(d,"Enter clan member who string:-\r\n| ");
         OLC_MODE(d) = CEDIT_MBR_LOOK_STR;
         break;
       case 'p':
       case 'P':
         if (GET_LEVEL(d->character) >= LVL_IMPL) {
           newclan = OLC_CLAN(d)->number;  /* next new clan will get this one's number  */
           /* free everything up, including strings etc */
           cleanup_olc(d, CLEANUP_ALL);
           cnum--;
           write_to_output(d,"Clan purged.\r\n");
         } else {
           write_to_output(d,"Sorry you are not allowed to do that at this time.\r\n");
           cedit_clan_menu(d);
           }
         return;
        case 'q':
       case 'Q':
           /*. Something has been modified .*/
          write_to_output(d, "Do you wish to save the clan configuration? (y/n) : ");
          OLC_MODE(d) = CEDIT_CONFIRM_SAVE;
         return;
        default:
         write_to_output(d,"Invalid choice!");
         cedit_clan_menu(d);
         break;
       }
       return;

     case CEDIT_NAME:
         if (OLC_CLAN(d)->name)
           free(OLC_CLAN(d)->name);
         OLC_CLAN(d)->name = strdup(arg);
         cedit_clan_menu(d);
         break;

       case CEDIT_LEADERSNAME:
         if (OLC_CLAN(d)->leadersname)
           free(OLC_CLAN(d)->leadersname);
         OLC_CLAN(d)->leadersname = strdup(arg);
         cedit_clan_menu(d);
         break;

       case CEDIT_MBR_LOOK_STR:
         if (OLC_CLAN(d)->member_look_str)
           free(OLC_CLAN(d)->member_look_str);
         OLC_CLAN(d)->member_look_str = strdup(arg);
         cedit_clan_menu(d);
         break;

 default:
      /*
       * We should never get here, but just in case...
       */
      cleanup_olc(d, CLEANUP_CONFIG);
      mudlog(BRF, LVL_BUILDER, TRUE, "SYSERR: OLC: cedit_parse(): Reached default case!");
      write_to_output(d, "Oops...\r\n");
      break;
  }
  OLC_VAL(d) = 1;
}

/*
 * End of parse_cedit()  
 */
void reassign_rooms(void)
{
  void assign_rooms(void);
  int i;

  /* remove old funcs */
  for (i = 0; i < top_of_world; i++)
    world[i].func = NULL;
        
  /* reassign spec_procs */
  assign_rooms();
}


 
  //Create a new clan with some default strings.
 
 void cedit_clan_new(struct descriptor_data *d)
 {
   int i;

   if ((OLC_CLAN(d) = enqueue_clan()) != NULL) {
     OLC_CLAN(d)->name = strdup("Unfinished Clan");
     OLC_CLAN(d)->number = newclan;
     OLC_CLAN(d)->leadersname = strdup("NoOne");
     OLC_CLAN(d)->member_look_str = NULL;
     OLC_CLAN(d)->clan_entr_room = 1;
     OLC_CLAN(d)->clan_password = strdup("none");
     OLC_CLAN(d)->gold = 0;
     for (i=0; i<20; i++)
       OLC_CLAN(d)->applicants[i] = NULL;
   } else
     fprintf(stderr, "SYSERR: Unable to create new clan!\r\n");
   cnum++;
   if (newclan == cnum)
     newclan++;
   else
     newclan = cnum;
   cedit_clan_menu(d);
   OLC_VAL(d) = 0;
 }

void cedit_string_cleanup(struct descriptor_data *d, int terminator)
{
  switch (OLC_MODE(d)) {
  case CEDIT_MENU:
  case CEDIT_WELC_MESSG:
  case CEDIT_START_MESSG:
    cedit_disp_operation_options(d);
    break;
  }
}[/i][/i]

 
Attachments:
Last edit: 1 month 3 weeks ago by Nero.

Please Log in or Create an account to join the conversation.

Time to create page: 0.132 seconds