[CODE] Saving Non-Native Shop Items

From: Edward Felch (dustlos@hotmail.com)
Date: 06/29/01


Hello all, as a spur of the moment thing I decided to work on getting items
which shops don't "produce" to save to a file (just the vnum), which is
loaded at a reset, zone reboot, mud crash/reboot, etc. However I've hit a
roadblock and am not thinking very straight at the moment and could use a
bit of assistance... I'm submitting this here as it is (very new, tested and
crashed, early). Any help is appreciated:

** CODE **
These two functions are in shop.c:
/* Function to take a shop and mob and save any non-unique items to it
* It saves any object vnums which aren't native producing, good for crashs
*/
void save_shop_nonnative(shop_vnum shop_num, struct char_data *keeper)
{
  FILE   *cfile = NULL;
  struct obj_data *obj, *next_obj;

  sprintf(buf, "shop_item/%ld", shop_num);

  if ((cfile = fopen(buf, "w")) == NULL) {
    /* Create the File Then */
    sprintf(buf2, "shop_item/%ld", shop_num);
    if (!(cfile = fopen(buf2, "w"))) {
      mudlog("SYSERR: SHP: Can't write new shop_item file.", BRF, LVL_IMPL,
TRUE);
      exit(0);
    }
    fprintf(cfile, "#%ld\n"
                   "$\n", shop_num);
  }

  /* Loops through the keeper's item list, checks if its native or not
   * if it isnt, it writes the item's VNUM to the file */
  *buf = '\0';
  *buf2 = '\0';
  sprintf(buf2, "#%ld\n", shop_num);
  for (obj = keeper->carrying; obj; obj = next_obj) {
     next_obj = obj->next;
     if (!shop_producing(obj, shop_num))
       sprintf(buf2+strlen(buf2), "%ld\n", GET_OBJ_VNUM(obj));
  }
  fprintf(cfile, buf2);
  fclose(cfile);
  return;
}

void load_shop_nonnative(shop_vnum shop_num, struct char_data *keeper)
{
  FILE *cfile = NULL;
  struct obj_data *obj;
  int line_num = 0, placer = 0;

  sprintf(buf, "shop_item/%ld", shop_num);

  /* Check to see if we have a file for this shop number */
  if ((cfile = fopen(buf, "r")) == NULL)
     return;

  line_num += get_line(cfile, buf);
  if (sscanf(buf, "#%d", &placer) != 1) {
    fprintf(stderr, "Format error in shop_item %ld, line %d.\n", shop_num,
line_num);
    exit(0);
  }
  while (buf && *buf != '$') {
    line_num += get_line(cfile, buf);
    if (sscanf(buf, "#%d", &placer) != 1) {
      fprintf(stderr, "Format error in shop_item %ld, line %d.\n", shop_num,
line_num);
      exit(0);
    }
    obj = read_object(placer, VIRTUAL);
    if (!obj || GET_OBJ_VNUM(obj) == NOTHING) {
      fprintf(stderr, "Bad Object in shop_item %ld, line %d.\n", shop_num,
line_num);
      exit(0);
    }
   if (!shop_producing(obj, shop_num))
      slide_obj(obj, keeper, shop_num);
  }
  return;
}


In db.c I added in all the needed includes, extern'd the shop_index and
top_shop so they would be available.

In reset_zone right after the end of the switch statement for checking for
mob, obj, etc loads I added this:
    /* Parts for loading non-native items in shops, if mob
     * load was true, see if this mob is a shopkeeper, if it
     * is then we should try the load function */
    if (mob_load == TRUE) {
       for (shop_nr = 0; shop_nr <= top_shop; shop_nr++) {
          if (SHOP_KEEPER(shop_nr) == mob->nr)
             break;
       }
       if (!(shop_nr > top_shop)) {
          load_shop_nonnative(shop_nr, mob);
       }
    }


It compiles and reboots correctly, in fact it even makes a file, however
this is what is within the file:

#3
401
400
28000
28000
23702
23702
23703
more numbers...

Item 400 is a long sword which is produced by the shop
Item 401 is a club which isn't produced (correct)
The rest is bla

And when I actually reboot the zone, it crashs right away. Something weird
in the save func must make all the gibberish, and the load function is bleh
too... Take a look at it and thanks for any help at all :)
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST