Re: [CODE] Saving Non-Native Shop Items

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


OK, I somehow fixed it up, I think the problem was silly, something about
the load or save wanting something which wasn't there. Currently it works
except it reads the file in twice which means that the location I put the
load_shop_nonnative in db.c was a bad place to put it. I'm not sure where
would be a good location, we need to put it after a mob has loaded but where
I have it it does it twice... But I'm happy for now, I can purge the shop
keeper, reset the zone and he has the items (albeit double for nonnative), I
rebooted my mud and he had them and so forth :)

shop_item file:
#3
Thomas the Shopkeeper
402
$


log file after doing a zone reboot:
Reading in shop number.
Reading in shop keeper name.
Reading in shop item list.
Reading in shop number.
Reading in shop keeper name.
Reading in shop item list.


Code for the shop saving/loading:
/* 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"
                   "%s\n"
                   "$\n", shop_num, GET_NAME(keeper));
  }

  /* 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"
                "%s\n", shop_num, GET_NAME(keeper));
  for (obj = keeper->carrying; obj; obj = next_obj) {
     next_obj = obj->next_content;
     if (!next_obj)
        break;
     if (!shop_producing(obj, shop_num))
       sprintf(buf2+strlen(buf2), "%ld\n", GET_OBJ_VNUM(obj));
  }
  fprintf(cfile, buf2);
  fprintf(cfile, "$\n");
  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;
  obj_vnum v_this;

  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;

  // Shop number
  fprintf(stderr, "Reading in shop number.\n");
  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);
  }

  fprintf(stderr, "Reading in shop keeper name.\n");
  // Name of shopkeeper
  line_num += get_line(cfile, buf);

  fprintf(stderr, "Reading in shop item list.\n");
  // Item list
  while (buf && *buf != '$') {
    line_num += get_line(cfile, buf);
    if (!buf || !*buf || *buf == '$')
       break;
    if (sscanf(buf, "%d", &placer) != 1) {
      fprintf(stderr, "Format error in shop_item %ld, line %d.\n", shop_num,
line_num);
      exit(0);
    }
    v_this = placer;
    obj = read_object(v_this, 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;
}

The save function is used after an item is sold or bought (after the object
is transfered).


In db.c here is a view of where the load is:
    default:
      ZONE_ERROR("unknown cmd in reset table; cmd disabled");
      ZCMD.command = '*';
      break;
    }

    /* 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);
       }
    }
  }

  zone_table[zone].age = 0;
_________________________________________________________________
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