/* ************************************************************************ * File: db.c Part of CircleMUD * * Usage: Loading/saving chars, booting/resetting world, internal funcs * * * * All rights reserved. See license.doc for complete information. * * * * Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University * * CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991. * ************************************************************************ */ #define __DB_C__ #include "conf.h" #include "sysdep.h" #include "structs.h" #include "utils.h" #include "db.h" #include "character.h" #include "comm.h" #include "command.h" #include "handler.h" #include "spells.h" #include "mail.h" #include "interpreter.h" #include "house.h" #include "constants.h" #include "log.h" #include "hashMap.h" #include "zone.h" #include "item.h" #include "room.h" #include "mobile.h" #include "alias.h" /************************************************************************** * declarations of most of the 'global' variables * **************************************************************************/ /* * Globals being added */ hashMap_t *zones; /**< hashMap of Zones in game */ /* * Globals being kept... for now */ itemData_t *object_list = NULL; /* global linked list of objs */ charData_t *character_list = NULL; /* global linked list of chars */ messageList_t fight_messages[MAX_MESSAGES]; /* fighting messages */ playerIndex_t *player_table = NULL; /* index to plr file */ FILE *player_fl = NULL; /* file desc of player file */ int top_of_p_table = 0; /* ref to top of table */ long top_idnum = 0; /* highest idnum in use */ int no_mail = 0; /* mail disabled? */ int mini_mud = 0; /* mini-mud mode? */ int no_rent_check = 0; /* skip rent check on boot? */ time_t boot_time = 0; /* time of mud boot */ int circle_restrict = 0; /* level of game restriction */ char *credits = NULL; /* game credits */ char *news = NULL; /* mud news */ char *motd = NULL; /* message of the day - mortals */ char *imotd = NULL; /* message of the day - immorts */ char *GREETINGS = NULL; /* opening credits screen */ char *help = NULL; /* help screen */ char *info = NULL; /* info page */ char *wizlist = NULL; /* list of higher gods */ char *immlist = NULL; /* list of peon gods */ char *background = NULL; /* background story */ char *handbook = NULL; /* handbook for new immortals */ char *policies = NULL; /* policies page */ helpData_t *help_table = 0; /* the help table */ int top_of_helpt = 0; /* top of help index table */ timeInfoData_t time_info;/* the infomation about the time */ weatherData_t weather_info; /* the infomation about the weather */ playerSpecials_t dummy_mob; /* dummy spec area for mobs */ roomData_t *rMortalStart = NULL; roomData_t *rImmortalStart = NULL; roomData_t *rFrozenStart = NULL; roomData_t *rLimbo = NULL; roomData_t *rVoid = NULL; roomData_t *rIdle = NULL; /* local functions */ int check_bitvector_names(bitvector_t bits, size_t namecount, const char *whatami, const char *whatbits); int check_object_spell_number(itemData_t *obj, int val); int check_object_level(itemData_t *obj, int val); void setup_dir(FILE *fl, int room, int dir); void index_boot(int mode); int check_object(itemData_t *); void load_help(FILE *fl); void assign_mobiles(void); void assign_objects(void); void assign_rooms(void); void assign_the_shopkeepers(void); void build_player_index(void); int file_to_string(const char *name, char *buf); int file_to_string_alloc(const char *name, char **buf); void reboot_wizlists(void); int count_alias_records(FILE *fl); void get_one_line(FILE *fl, char *buf); void save_etext(charData_t *ch); void check_start_rooms(void); void renum_world(void); void log_zone_error(zoneVnum_t zone, int cmd_no, const char *message); void reset_time(void); long get_ptable_by_name(const char *name); /* external functions */ int level_exp(int chclass, int level); void paginate_string(char *str, descriptorData_t *d); timeInfoData_t *mud_time_passed(time_t t2, time_t t1); void free_alias(aliasData_t *a); void load_messages(void); void weather_and_time(int mode); void mag_assign_spells(void); void boot_social_messages(void); void update_obj_file(void); /* In objsave.c */ void sort_commands(void); void sort_spells(void); void load_banned(void); void Read_Invalid_List(void); void boot_the_shops(FILE *shop_f, char *filename, int rec_count); int hsort(const void *a, const void *b); void prune_crlf(char *txt); void destroy_shops(void); /* external vars */ extern int no_specials; extern int scheck; extern descriptorData_t *descriptor_list; extern const char *unused_spellname; /* spell_parser.c */ extern const char *mortalStartRoom; extern const char *immortalStartRoom; extern const char *frozenStartRoom; extern const char *limboRoom; extern const char *voidRoom; extern const char *rentIdleRoom; /************************************************************************* * routines for booting the system * *************************************************************************/ /* this is necessary for the autowiz system */ void reboot_wizlists(void) { file_to_string_alloc(WIZLIST_FILE, &wizlist); file_to_string_alloc(IMMLIST_FILE, &immlist); } /* Wipe out all the loaded text files, for shutting down. */ void free_text_files(void) { char **textfiles[] = { &wizlist, &immlist, &news, &credits, &motd, &imotd, &help, &info, &policies, &handbook, &background, &GREETINGS, NULL }; int rf; for (rf = 0; textfiles[rf]; rf++) if (*textfiles[rf]) { free(*textfiles[rf]); *textfiles[rf] = NULL; } } /* * Too bad it doesn't check the return values to let the user * know about -1 values. This will result in an 'Okay.' to a * 'reload' command even when the string was not replaced. * To fix later, if desired. -gg 6/24/99 */ ACMD(do_reboot) { char arg[MAX_INPUT_LENGTH]; one_argument(argument, arg); if (!str_cmp(arg, "all") || *arg == '*') { if (file_to_string_alloc(GREETINGS_FILE, &GREETINGS) == 0) prune_crlf(GREETINGS); file_to_string_alloc(WIZLIST_FILE, &wizlist); file_to_string_alloc(IMMLIST_FILE, &immlist); file_to_string_alloc(NEWS_FILE, &news); file_to_string_alloc(CREDITS_FILE, &credits); file_to_string_alloc(MOTD_FILE, &motd); file_to_string_alloc(IMOTD_FILE, &imotd); file_to_string_alloc(HELP_PAGE_FILE, &help); file_to_string_alloc(INFO_FILE, &info); file_to_string_alloc(POLICIES_FILE, &policies); file_to_string_alloc(HANDBOOK_FILE, &handbook); file_to_string_alloc(BACKGROUND_FILE, &background); } else if (!str_cmp(arg, "wizlist")) file_to_string_alloc(WIZLIST_FILE, &wizlist); else if (!str_cmp(arg, "immlist")) file_to_string_alloc(IMMLIST_FILE, &immlist); else if (!str_cmp(arg, "news")) file_to_string_alloc(NEWS_FILE, &news); else if (!str_cmp(arg, "credits")) file_to_string_alloc(CREDITS_FILE, &credits); else if (!str_cmp(arg, "motd")) file_to_string_alloc(MOTD_FILE, &motd); else if (!str_cmp(arg, "imotd")) file_to_string_alloc(IMOTD_FILE, &imotd); else if (!str_cmp(arg, "help")) file_to_string_alloc(HELP_PAGE_FILE, &help); else if (!str_cmp(arg, "info")) file_to_string_alloc(INFO_FILE, &info); else if (!str_cmp(arg, "policy")) file_to_string_alloc(POLICIES_FILE, &policies); else if (!str_cmp(arg, "handbook")) file_to_string_alloc(HANDBOOK_FILE, &handbook); else if (!str_cmp(arg, "background")) file_to_string_alloc(BACKGROUND_FILE, &background); else if (!str_cmp(arg, "greetings")) { if (file_to_string_alloc(GREETINGS_FILE, &GREETINGS) == 0) prune_crlf(GREETINGS); } else if (!str_cmp(arg, "xhelp")) { if (help_table) free_help(); index_boot(DB_BOOT_HLP); } else { send_to_char(ch, "Unknown reload option.\r\n"); return; } send_to_char(ch, "%s", OK); } void free_extra_descriptions(extraDescData_t *edesc) { extraDescData_t *enext; for (; edesc; edesc = enext) { enext = edesc->next; free(edesc->keyword); free(edesc->description); free(edesc); } } /* Free the world, in a memory allocation sense. */ void destroy_db(void) { charData_t *chtmp; itemData_t *objtmp; /* Active Mobiles & Players */ while (character_list) { chtmp = character_list; character_list = character_list->next; free_char(chtmp); } /* Active Objects */ while (object_list) { objtmp = object_list; object_list = object_list->next; free_obj(objtmp); } /* Free The World! */ hashMap_free(zones); } /* body of the booting system */ void boot_db(void) { /* Declare a hashMap iterator */ hashMapIterator_t *iter = NULL; log("Boot db -- BEGIN."); log("Resetting the game time:"); reset_time(); log("Reading news, credits, help, bground, info & motds."); file_to_string_alloc(NEWS_FILE, &news); file_to_string_alloc(CREDITS_FILE, &credits); file_to_string_alloc(MOTD_FILE, &motd); file_to_string_alloc(IMOTD_FILE, &imotd); file_to_string_alloc(HELP_PAGE_FILE, &help); file_to_string_alloc(INFO_FILE, &info); file_to_string_alloc(WIZLIST_FILE, &wizlist); file_to_string_alloc(IMMLIST_FILE, &immlist); file_to_string_alloc(POLICIES_FILE, &policies); file_to_string_alloc(HANDBOOK_FILE, &handbook); file_to_string_alloc(BACKGROUND_FILE, &background); if (file_to_string_alloc(GREETINGS_FILE, &GREETINGS) == 0) prune_crlf(GREETINGS); log("Loading spell definitions."); mag_assign_spells(); /* Load the new hashMap based world */ log("Loading world from dao files."); zoneData_loadWorld(); log("Checking start rooms."); check_start_rooms(); log("Loading help entries."); index_boot(DB_BOOT_HLP); log("Generating player index."); build_player_index(); log("Loading fight messages."); load_messages(); log("Loading social messages."); boot_social_messages(); log("Assigning function pointers:"); if (!no_specials) { log(" Mobiles."); assign_mobiles(); /*log(" Shopkeepers.");*/ /*assign_the_shopkeepers();*/ log(" Objects."); assign_objects(); log(" Rooms."); assign_rooms(); } log("Assigning spell and skill levels."); init_spell_levels(); log("Sorting command list and spells."); sort_commands(); sort_spells(); log("Booting mail system."); if (!scan_file()) { log(" Mail boot failed -- Mail system disabled"); no_mail = 1; } log("Reading banned site and invalid-name list."); load_banned(); Read_Invalid_List(); if (!no_rent_check) { log("Deleting timed-out crash and rent files:"); update_obj_file(); log(" Done."); } /* Moved here so the object limit code works. -gg 6/24/98 */ if (!mini_mud) { log("Booting houses."); House_boot(); } /* New zone resetting -- hashMaps */ iter = hashMapIterator_create(zones); while (iter && hashMapIterator_getValue(iter)) { zoneData_t *zone = (zoneData_t *)hashMapIterator_getValue(iter); if (zone) { log("Resetting: %-20s : %-30s", zone->keyword, zone->name); zoneData_reset(zone); } zone = NULL; hashMapIterator_next(iter); } hashMapIterator_free(iter); /* Done */ boot_time = time(0); log("Boot db -- DONE."); } /* reset the time in the game from file */ void reset_time(void) { time_t beginning_of_time = 0; FILE *bgtime; if ((bgtime = fopen(TIME_FILE, "r")) == NULL) log("SYSERR: Can't read from '%s' time file.", TIME_FILE); else { fscanf(bgtime, "%ld\n", &beginning_of_time); fclose(bgtime); } if (beginning_of_time == 0) beginning_of_time = 650336715; time_info = *mud_time_passed(time(0), beginning_of_time); if (time_info.hours <= 4) weather_info.sunlight = SUN_DARK; else if (time_info.hours == 5) weather_info.sunlight = SUN_RISE; else if (time_info.hours <= 20) weather_info.sunlight = SUN_LIGHT; else if (time_info.hours == 21) weather_info.sunlight = SUN_SET; else weather_info.sunlight = SUN_DARK; log(" Current Gametime: %dH %dD %dM %dY.", time_info.hours, time_info.day, time_info.month, time_info.year); weather_info.pressure = 960; if ((time_info.month >= 7) && (time_info.month <= 12)) weather_info.pressure += dice(1, 50); else weather_info.pressure += dice(1, 80); weather_info.change = 0; if (weather_info.pressure <= 980) weather_info.sky = SKY_LIGHTNING; else if (weather_info.pressure <= 1000) weather_info.sky = SKY_RAINING; else if (weather_info.pressure <= 1020) weather_info.sky = SKY_CLOUDY; else weather_info.sky = SKY_CLOUDLESS; } /* Write the time in 'when' to the MUD-time file. */ void save_mud_time(timeInfoData_t *when) { FILE *bgtime; if ((bgtime = fopen(TIME_FILE, "w")) == NULL) log("SYSERR: Can't write to '%s' time file.", TIME_FILE); else { fprintf(bgtime, "%ld\n", mud_time_to_secs(when)); fclose(bgtime); } } void free_player_index(void) { int tp; if (!player_table) return; for (tp = 0; tp <= top_of_p_table; tp++) if (player_table[tp].name) free(player_table[tp].name); free(player_table); player_table = NULL; top_of_p_table = 0; } /* generate index table for the player file */ void build_player_index(void) { int nr = -1, i; long size, recs; pfileElement_t dummy; if (!(player_fl = fopen(PLAYER_FILE, "r+b"))) { if (errno != ENOENT) { perror("SYSERR: fatal error opening playerfile"); exit(1); } else { log("No playerfile. Creating a new one."); touch(PLAYER_FILE); if (!(player_fl = fopen(PLAYER_FILE, "r+b"))) { perror("SYSERR: fatal error opening playerfile"); exit(1); } } } fseek(player_fl, 0L, SEEK_END); size = ftell(player_fl); rewind(player_fl); if (size % sizeof(pfileElement_t)) log("\aWARNING: PLAYERFILE IS PROBABLY CORRUPT!"); recs = size / sizeof(pfileElement_t); if (recs) { log(" %ld players in database.", recs); CREATE(player_table, playerIndex_t, recs); } else { player_table = NULL; top_of_p_table = -1; return; } for (;;) { fread(&dummy, sizeof(pfileElement_t), 1, player_fl); if (feof(player_fl)) break; /* new record */ nr++; CREATE(player_table[nr].name, char, strlen(dummy.name) + 1); for (i = 0; (*(player_table[nr].name + i) = LOWER(*(dummy.name + i))); i++) ; player_table[nr].id = dummy.char_specials_saved.idnum; top_idnum = MAX(top_idnum, dummy.char_specials_saved.idnum); } top_of_p_table = nr; } /* * Thanks to Andrey (andrey@alex-ua.com) for this bit of code, although I * did add the 'goto' and changed some "while()" into "do { } while()". * -gg 6/24/98 (technically 6/25/98, but I care not.) */ int count_alias_records(FILE *fl) { char key[READ_SIZE], next_key[READ_SIZE]; char line[READ_SIZE], *scan; int total_keywords = 0; /* get the first keyword line */ get_one_line(fl, key); while (*key != '$') { /* skip the text */ do { get_one_line(fl, line); if (feof(fl)) goto ackeof; } while (*line != '#'); /* now count keywords */ scan = key; do { scan = one_word(scan, next_key); if (*next_key) ++total_keywords; } while (*next_key); /* get next keyword line (or $) */ get_one_line(fl, key); if (feof(fl)) goto ackeof; } return (total_keywords); /* No, they are not evil. -gg 6/24/98 */ ackeof: log("SYSERR: Unexpected end of help file."); exit(1); /* Some day we hope to handle these things better... */ } /* This is only here until help files are moved to DAO */ void index_boot(int mode) { const char *index_filename, *prefix = NULL; /* NULL or egcs 1.1 complains */ FILE *db_index, *db_file; int rec_count = 0, size[2]; char buf2[PATH_MAX], buf1[MAX_STRING_LENGTH]; switch (mode) { case DB_BOOT_HLP: prefix = HLP_PREFIX; break; default: log("SYSERR: Unknown subcommand %d to index_boot!", mode); exit(1); } if (mini_mud) index_filename = MINDEX_FILE; else index_filename = INDEX_FILE; snprintf(buf2, sizeof(buf2), "%s%s", prefix, index_filename); if (!(db_index = fopen(buf2, "r"))) { log("SYSERR: opening index file '%s': %s", buf2, strerror(errno)); exit(1); } /* first, count the number of records in the file so we can malloc */ fscanf(db_index, "%s\n", buf1); while (*buf1 != '$') { snprintf(buf2, sizeof(buf2), "%s%s", prefix, buf1); if (!(db_file = fopen(buf2, "r"))) { log("SYSERR: File '%s' listed in '%s/%s': %s", buf2, prefix, index_filename, strerror(errno)); fscanf(db_index, "%s\n", buf1); continue; } else { if (mode == DB_BOOT_HLP) rec_count += count_alias_records(db_file); } fclose(db_file); fscanf(db_index, "%s\n", buf1); } /* * NOTE: "bytes" does _not_ include strings or other later malloc'd things. */ switch (mode) { case DB_BOOT_HLP: CREATE(help_table, helpData_t, rec_count); size[0] = sizeof(helpData_t) * rec_count; log(" %d entries, %d bytes.", rec_count, size[0]); break; } rewind(db_index); fscanf(db_index, "%s\n", buf1); while (*buf1 != '$') { snprintf(buf2, sizeof(buf2), "%s%s", prefix, buf1); if (!(db_file = fopen(buf2, "r"))) { log("SYSERR: %s: %s", buf2, strerror(errno)); exit(1); } switch (mode) { case DB_BOOT_HLP: /* * If you think about it, we have a race here. Although, this is the * "point-the-gun-at-your-own-foot" type of race. */ load_help(db_file); break; } fclose(db_file); fscanf(db_index, "%s\n", buf1); } fclose(db_index); /* sort the help index */ if (mode == DB_BOOT_HLP) { qsort(help_table, top_of_helpt, sizeof(helpData_t), hsort); top_of_helpt--; } } /** * Make sure start rooms exist, resovle them to pointers * * @param none * @return none */ void check_start_rooms(void) { if ((rMortalStart = roomData_find((char *)mortalStartRoom)) == NULL) { log("SYSERR: Mortal start room does not exist. Change in config.c."); exit(1); } if ((rImmortalStart = roomData_find((char *)immortalStartRoom)) == NULL) { if (!mini_mud) log("SYSERR: Warning: Immort start room does not exist. Change in config.c."); rImmortalStart = rMortalStart; } if ((rFrozenStart = roomData_find((char *)frozenStartRoom)) == NULL) { if (!mini_mud) log("SYSERR: Warning: Frozen start room does not exist. Change in config.c."); rFrozenStart = rMortalStart; } if ((rLimbo = roomData_find((char *)limboRoom)) == NULL) { log("WARNING: Limbo room does not exist. Change in config.c."); rLimbo = rMortalStart; } if ((rVoid = roomData_find((char *)voidRoom)) == NULL) { log("WARNING: Void room does not exist. Change in config.c."); rVoid = rMortalStart; } if ((rIdle = roomData_find((char *)rentIdleRoom)) == NULL) { log("WARNING: Idle room does not exist. Change in config.c."); rIdle = rMortalStart; } } void get_one_line(FILE *fl, char *buf) { if (fgets(buf, READ_SIZE, fl) == NULL) { log("SYSERR: error reading help file: not terminated with $?"); exit(1); } buf[strlen(buf) - 1] = '\0'; /* take off the trailing \n */ } void free_help(void) { int hp; if (!help_table) return; for (hp = 0; hp <= top_of_helpt; hp++) { if (help_table[hp].keyword) free(help_table[hp].keyword); if (help_table[hp].entry && !help_table[hp].duplicate) free(help_table[hp].entry); } free(help_table); help_table = NULL; top_of_helpt = 0; } void load_help(FILE *fl) { #if defined(CIRCLE_MACINTOSH) static char key[READ_SIZE + 1], next_key[READ_SIZE + 1], entry[32384]; /* too big for stack? */ #else char key[READ_SIZE + 1], next_key[READ_SIZE + 1], entry[32384]; #endif size_t entrylen; char line[READ_SIZE + 1], *scan; helpData_t el; /* get the first keyword line */ get_one_line(fl, key); while (*key != '$') { strcat(key, "\r\n"); /* strcat: OK (READ_SIZE - "\n" + "\r\n" == READ_SIZE + 1) */ entrylen = strlcpy(entry, key, sizeof(entry)); /* read in the corresponding help entry */ get_one_line(fl, line); while (*line != '#' && entrylen < sizeof(entry) - 1) { entrylen += strlcpy(entry + entrylen, line, sizeof(entry) - entrylen); if (entrylen + 2 < sizeof(entry) - 1) { strcpy(entry + entrylen, "\r\n"); /* strcpy: OK (size checked above) */ entrylen += 2; } get_one_line(fl, line); } if (entrylen >= sizeof(entry) - 1) { int keysize; const char *truncmsg = "\r\n*TRUNCATED*\r\n"; strcpy(entry + sizeof(entry) - strlen(truncmsg) - 1, truncmsg); /* strcpy: OK (assuming sane 'entry' size) */ keysize = strlen(key) - 2; log("SYSERR: Help entry exceeded buffer space: %.*s", keysize, key); /* If we ran out of buffer space, eat the rest of the entry. */ while (*line != '#') get_one_line(fl, line); } /* now, add the entry to the index with each keyword on the keyword line */ el.duplicate = 0; el.entry = strdup(entry); scan = one_word(key, next_key); while (*next_key) { el.keyword = strdup(next_key); help_table[top_of_helpt++] = el; el.duplicate++; scan = one_word(scan, next_key); } /* get next keyword line (or $) */ get_one_line(fl, key); } } int hsort(const void *a, const void *b) { const helpData_t *a1, *b1; a1 = (const helpData_t *) a; b1 = (const helpData_t *) b; return (str_cmp(a1->keyword, b1->keyword)); } /************************************************************************* * procedures for resetting, both play-time and boot-time * *************************************************************************/ /* create a character, and add it to the char list */ charData_t *create_char(void) { charData_t *ch; CREATE(ch, charData_t, 1); clear_char(ch); ch->next = character_list; character_list = ch; return (ch); } /** * Create a new mobile instance from prototype * * @param mobProto Pointer to prototype of mobile to load * @return Pointer to new instance of mobile */ charData_t *read_mobile(charData_t *mobProto) { charData_t *mob = NULL; if (mobProto == NULL) { log("read_mobile(): Invalid charData_t 'mobProto'."); } else if (charData_isPrototype(mobProto) == FALSE) { log("read_mobile(): Passed non mobile-prototype charData_t."); } else { /* We have a valid charData_t passed, let's load the new instance */ /* alloc and clear */ CREATE(mob, charData_t, 1); clear_char(mob); /* set */ *mob = *mobProto; /* add to character_list */ mob->next = character_list; character_list = mob; /* set prototype pointer */ mob->prototype = mobProto; /* set misc other values for sanity's sake */ mob->room = NULL; mob->wasInRoom = NULL; mob->char_specials.position = mob->mob_specials.default_pos; /* Set max points */ if (!mob->points.max_hit) { mob->points.max_hit = dice(mob->points.hit, mob->points.mana) + mob->points.move; } else mob->points.max_hit = rand_number(mob->points.hit, mob->points.mana); /* set base */ mob->points.hit = mob->points.max_hit; mob->points.mana = mob->points.max_mana; mob->points.move = mob->points.max_move; /* set times */ mob->player.time.birth = time(0); mob->player.time.played = 0; mob->player.time.logon = time(0); /* increment the count of this mob */ mobProto->number++; } return (mob); } /* create an object, and add it to the object list */ itemData_t *create_obj(void) { itemData_t *obj; CREATE(obj, itemData_t, 1); clear_object(obj); obj->next = object_list; object_list = obj; return (obj); } /** * Create a new item instance from prototype * * @param itemProto Pointer to prototype of item to load * @return Pointer to new instance of item */ itemData_t *read_item(itemData_t *itemProto) { itemData_t *item = NULL; if (itemProto == NULL) { log("read_item(): Invalid itemData_t 'itemProto'."); } else if (itemData_isPrototype(itemProto) == FALSE) { log("read_item(): Passed non item-prototype itemData_t."); } else { /* Valid prototype passed, let's load */ /* alloc and clear */ CREATE(item, itemData_t, 1); clear_object(item); /* set */ *item = *itemProto; /* set prototype pointer */ item->prototype = itemProto; /* clear some values for sanity's sake */ item->room = NULL; /* add to object_list */ item->next = object_list; object_list = item; /* increment the count if this item */ itemProto->number++; } return (item); } /************************************************************************* * stuff related to the save/load player system * *************************************************************************/ long get_ptable_by_name(const char *name) { int i; for (i = 0; i <= top_of_p_table; i++) if (!str_cmp(player_table[i].name, name)) return (i); return (-1); } long get_id_by_name(const char *name) { int i; for (i = 0; i <= top_of_p_table; i++) if (!str_cmp(player_table[i].name, name)) return (player_table[i].id); return (-1); } char *get_name_by_id(long id) { int i; for (i = 0; i <= top_of_p_table; i++) if (player_table[i].id == id) return (player_table[i].name); return (NULL); } /* Load a char, TRUE if loaded, FALSE if not */ int load_char(const char *name, pfileElement_t *char_element) { int player_i; if ((player_i = get_ptable_by_name(name)) >= 0) { fseek(player_fl, player_i * sizeof(pfileElement_t), SEEK_SET); fread(char_element, sizeof(pfileElement_t), 1, player_fl); return (player_i); } else return (-1); } /* * write the vital data of a player to the player file * * And that's it! No more fudging around with the load room. * Unfortunately, 'host' modifying is still here due to lack * of that variable in the char_data structure. */ void save_char(charData_t *ch) { pfileElement_t st; if (IS_NPC(ch) || !ch->desc || GET_PFILEPOS(ch) < 0) return; char_to_store(ch, &st); strncpy(st.host, ch->desc->host, HOST_LENGTH); /* strncpy: OK (s.host:HOST_LENGTH+1) */ st.host[HOST_LENGTH] = '\0'; fseek(player_fl, GET_PFILEPOS(ch) * sizeof(pfileElement_t), SEEK_SET); fwrite(&st, sizeof(pfileElement_t), 1, player_fl); } /* copy data from the file structure to a char struct */ void store_to_char(pfileElement_t *st, charData_t *ch) { int i; /* to save memory, only PC's -- not MOB's -- have player_specials */ if (ch->player_specials == NULL) CREATE(ch->player_specials, playerSpecials_t, 1); GET_SEX(ch) = st->sex; GET_CLASS(ch) = st->chclass; GET_LEVEL(ch) = st->level; GET_AUTH(ch) = st->auth; ch->player.short_descr = NULL; ch->player.long_descr = NULL; ch->player.title = strdup(st->title); ch->player.description = strdup(st->description); ch->player.hometown = st->hometown; ch->player.time.birth = st->birth; ch->player.time.played = st->played; ch->player.time.logon = time(0); ch->player.weight = st->weight; ch->player.height = st->height; ch->real_abils = st->abilities; ch->aff_abils = st->abilities; ch->points = st->points; ch->char_specials.saved = st->char_specials_saved; ch->player_specials->saved = st->player_specials_saved; POOFIN(ch) = NULL; POOFOUT(ch) = NULL; GET_LAST_TELL(ch) = NOBODY; if (ch->points.max_mana < 100) ch->points.max_mana = 100; ch->char_specials.carry_weight = 0; ch->char_specials.carry_items = 0; ch->points.armor = 100; ch->points.hitroll = 0; ch->points.damroll = 0; if (ch->player.name) free(ch->player.name); ch->player.name = strdup(st->name); strlcpy(ch->player.passwd, st->pwd, sizeof(ch->player.passwd)); /* Add all spell effects */ for (i = 0; i < MAX_AFFECT; i++) { if (st->affected[i].type) effectData_toChar(ch, &st->affected[i]); } /* * If you're not poisioned and you've been away for more than an hour of * real time, we'll set your HMV back to full */ if (!AFF_FLAGGED(ch, AFF_POISON) && time(0) - st->last_logon >= SECS_PER_REAL_HOUR) { GET_HIT(ch) = GET_MAX_HIT(ch); GET_MOVE(ch) = GET_MAX_MOVE(ch); GET_MANA(ch) = GET_MAX_MANA(ch); } } /* store_to_char */ /* copy vital data from a players char-structure to the file structure */ void char_to_store(charData_t *ch, pfileElement_t *st) { int i; effectData_t *af; itemData_t *char_eq[NUM_WEARS]; /* Unaffect everything a character can be affected by */ for (i = 0; i < NUM_WEARS; i++) { if (GET_EQ(ch, i)) char_eq[i] = char_unequipItem(ch, i); else char_eq[i] = NULL; } for (af = ch->affected, i = 0; i < MAX_AFFECT; i++) { if (af) { st->affected[i] = *af; st->affected[i].next = 0; af = af->next; } else { st->affected[i].type = 0; /* Zero signifies not used */ st->affected[i].duration = 0; st->affected[i].modifier = 0; st->affected[i].location = 0; st->affected[i].bitvector = 0; st->affected[i].next = 0; } } /* * remove the affections so that the raw values are stored; otherwise the * effects are doubled when the char logs back in. */ while (ch->affected) effectData_remove(ch, ch->affected); if ((i >= MAX_AFFECT) && af && af->next) log("SYSERR: WARNING: OUT OF STORE ROOM FOR AFFECTED TYPES!!!"); ch->aff_abils = ch->real_abils; st->birth = ch->player.time.birth; st->played = ch->player.time.played; st->played += time(0) - ch->player.time.logon; st->last_logon = time(0); ch->player.time.played = st->played; ch->player.time.logon = time(0); st->hometown = ch->player.hometown; st->weight = GET_WEIGHT(ch); st->height = GET_HEIGHT(ch); st->sex = GET_SEX(ch); st->chclass = GET_CLASS(ch); st->level = GET_LEVEL(ch); st->auth = GET_AUTH(ch); st->abilities = ch->real_abils; st->points = ch->points; st->char_specials_saved = ch->char_specials.saved; st->player_specials_saved = ch->player_specials->saved; st->points.armor = 100; st->points.hitroll = 0; st->points.damroll = 0; if (GET_TITLE(ch)) strlcpy(st->title, GET_TITLE(ch), MAX_TITLE_LENGTH); else *st->title = '\0'; if (ch->player.description) { if (strlen(ch->player.description) >= sizeof(st->description)) { log("SYSERR: char_to_store: %s's description length: %d, max: %d! " "Truncated.", GET_PC_NAME(ch), strlen(ch->player.description), sizeof(st->description)); ch->player.description[sizeof(st->description) - 3] = '\0'; strcat(ch->player.description, "\r\n"); /* strcat: OK (previous line makes room) */ } strcpy(st->description, ch->player.description); /* strcpy: OK (checked above) */ } else *st->description = '\0'; strcpy(st->name, GET_NAME(ch)); /* strcpy: OK (that's what GET_NAME came from) */ strcpy(st->pwd, GET_PASSWD(ch)); /* strcpy: OK (that's what GET_PASSWD came from) */ /* add spell and eq affections back in now */ for (i = 0; i < MAX_AFFECT; i++) { if (st->affected[i].type) effectData_toChar(ch, &st->affected[i]); } for (i = 0; i < NUM_WEARS; i++) { if (char_eq[i]) char_equipItem(ch, char_eq[i], i); } /* effectData_total(ch); unnecessary, I think !?! */ } /* Char to store */ void save_etext(charData_t *ch) { /* this will be really cool soon */ } /* * Create a new entry in the in-memory index table for the player file. * If the name already exists, by overwriting a deleted character, then * we re-use the old position. */ int create_entry(char *name) { int i, pos; if (top_of_p_table == -1) { /* no table */ CREATE(player_table, playerIndex_t, 1); pos = top_of_p_table = 0; } else if ((pos = get_ptable_by_name(name)) == -1) { /* new name */ i = ++top_of_p_table + 1; RECREATE(player_table, playerIndex_t, i); pos = top_of_p_table; } CREATE(player_table[pos].name, char, strlen(name) + 1); /* copy lowercase equivalent of name to table field */ for (i = 0; (player_table[pos].name[i] = LOWER(name[i])); i++) /* Nothing */; return (pos); } /************************************************************************ * funcs of a (more or less) general utility nature * ************************************************************************/ /* read and allocate space for a '~'-terminated string from a given file */ char *fread_string(FILE *fl, const char *error) { char buf[MAX_STRING_LENGTH], tmp[513]; char *point; int done = 0, length = 0, templength; *buf = '\0'; do { if (!fgets(tmp, 512, fl)) { log("SYSERR: fread_string: format error at or near %s", error); exit(1); } /* If there is a '~', end the string; else put an "\r\n" over the '\n'. */ if ((point = strchr(tmp, '~')) != NULL) { *point = '\0'; done = 1; } else { point = tmp + strlen(tmp) - 1; *(point++) = '\r'; *(point++) = '\n'; *point = '\0'; } templength = strlen(tmp); if (length + templength >= MAX_STRING_LENGTH) { log("SYSERR: fread_string: string too large (db.c)"); log("%s", error); exit(1); } else { strcat(buf + length, tmp); /* strcat: OK (size checked above) */ length += templength; } } while (!done); /* allocate space for the new string and copy it */ return (strlen(buf) ? strdup(buf) : NULL); } /* release memory allocated for a char struct */ void free_char(charData_t *ch) { aliasData_t *a; if (ch->player_specials != NULL && ch->player_specials != &dummy_mob) { while ((a = GET_ALIASES(ch)) != NULL) { GET_ALIASES(ch) = (GET_ALIASES(ch))->next; free_alias(a); } if (ch->player_specials->poofin) free(ch->player_specials->poofin); if (ch->player_specials->poofout) free(ch->player_specials->poofout); free(ch->player_specials); if (IS_NPC(ch)) log("SYSERR: Mob %s (%s:%d) had player_specials allocated!", GET_NAME(ch), ch->zone->keyword, ch->vnum); } if (!IS_NPC(ch) || (IS_NPC(ch) && ch->prototype == NULL)) { /* if this is a player, or a non-prototyped non-player, free all */ if (GET_NAME(ch)) free(GET_NAME(ch)); if (ch->player.title) free(ch->player.title); if (ch->player.short_descr) free(ch->player.short_descr); if (ch->player.long_descr) free(ch->player.long_descr); if (ch->player.description) free(ch->player.description); } else if (ch->prototype != NULL) { /* otherwise, free strings only if the string is not pointing at proto */ if (ch->player.name && DIFFERS(ch, player.name)) free(ch->player.name); if (ch->player.title && DIFFERS(ch, player.title)) free(ch->player.title); if (ch->player.short_descr && DIFFERS(ch, player.short_descr)) free(ch->player.short_descr); if (ch->player.long_descr && DIFFERS(ch, player.long_descr)) free(ch->player.long_descr); if (ch->player.description && DIFFERS(ch, player.description)) free(ch->player.description); } while (ch->affected) effectData_remove(ch, ch->affected); if (ch->desc) ch->desc->character = NULL; free(ch); } /* release memory allocated for an obj struct */ void free_obj(itemData_t *obj) { if (obj->prototype == NULL) { if (obj->name) free(obj->name); if (obj->description) free(obj->description); if (obj->shortDescription) free(obj->shortDescription); if (obj->actionDescription) free(obj->actionDescription); if (obj->exDescription) free_extra_descriptions(obj->exDescription); } else { if (obj->name && DIFFERS(obj, name)) free(obj->name); if (obj->description && DIFFERS(obj, description)) free(obj->description); if (obj->shortDescription && DIFFERS(obj, shortDescription)) free(obj->shortDescription); if (obj->actionDescription && DIFFERS(obj, actionDescription)) free(obj->actionDescription); if (obj->exDescription && DIFFERS(obj, exDescription)) free_extra_descriptions(obj->exDescription); } free(obj); } /* * Steps: * 1: Read contents of a text file. * 2: Make sure no one is using the pointer in paging. * 3: Allocate space. * 4: Point 'buf' to it. * * We don't want to free() the string that someone may be * viewing in the pager. page_string() keeps the internal * strdup()'d copy on ->showstr_head and it won't care * if we delete the original. Otherwise, strings are kept * on ->showstr_vector but we'll only match if the pointer * is to the string we're interested in and not a copy. * * If someone is reading a global copy we're trying to * replace, give everybody using it a different copy so * as to avoid special cases. */ int file_to_string_alloc(const char *name, char **buf) { int temppage; char temp[MAX_STRING_LENGTH]; descriptorData_t *in_use; for (in_use = descriptor_list; in_use; in_use = in_use->next) if (in_use->showstr_vector && *in_use->showstr_vector == *buf) return (-1); /* Lets not free() what used to be there unless we succeeded. */ if (file_to_string(name, temp) < 0) return (-1); for (in_use = descriptor_list; in_use; in_use = in_use->next) { if (!in_use->showstr_count || *in_use->showstr_vector != *buf) continue; /* Let's be nice and leave them at the page they were on. */ temppage = in_use->showstr_page; paginate_string((in_use->showstr_head = strdup(*in_use->showstr_vector)), in_use); in_use->showstr_page = temppage; } if (*buf) free(*buf); *buf = strdup(temp); return (0); } /* read contents of a text file, and place in buf */ int file_to_string(const char *name, char *buf) { FILE *fl; char tmp[READ_SIZE + 3]; int len; *buf = '\0'; if (!(fl = fopen(name, "r"))) { log("SYSERR: reading %s: %s", name, strerror(errno)); return (-1); } for (;;) { if (!fgets(tmp, READ_SIZE, fl)) /* EOF check */ break; if ((len = strlen(tmp)) > 0) tmp[len - 1] = '\0'; /* take off the trailing \n */ strcat(tmp, "\r\n"); /* strcat: OK (tmp:READ_SIZE+3) */ if (strlen(buf) + strlen(tmp) + 1 > MAX_STRING_LENGTH) { log("SYSERR: %s: string too big (%d max)", name, MAX_STRING_LENGTH); *buf = '\0'; fclose(fl); return (-1); } strcat(buf, tmp); /* strcat: OK (size checked above) */ } fclose(fl); return (0); } /* clear some of the the working variables of a char */ void reset_char(charData_t *ch) { int i; for (i = 0; i < NUM_WEARS; i++) GET_EQ(ch, i) = NULL; ch->followers = NULL; ch->master = NULL; IN_ROOM(ch) = NULL; ch->carrying = NULL; ch->next = NULL; ch->next_fighting = NULL; ch->next_in_room = NULL; FIGHTING(ch) = NULL; ch->char_specials.position = POS_STANDING; ch->mob_specials.default_pos = POS_STANDING; ch->char_specials.carry_weight = 0; ch->char_specials.carry_items = 0; if (GET_HIT(ch) <= 0) GET_HIT(ch) = 1; if (GET_MOVE(ch) <= 0) GET_MOVE(ch) = 1; if (GET_MANA(ch) <= 0) GET_MANA(ch) = 1; GET_LAST_TELL(ch) = NOBODY; } /* clear ALL the working variables of a char; do NOT free any space alloc'ed */ void clear_char(charData_t *ch) { memset((char *) ch, 0, sizeof(charData_t)); IN_ROOM(ch) = NULL; GET_AUTH(ch) = AUTH_NONE; GET_PFILEPOS(ch) = -1; GET_WAS_IN(ch) = NULL; GET_POS(ch) = POS_STANDING; ch->mob_specials.default_pos = POS_STANDING; ch->prototype = NULL; ch->func = NULL; ch->zone = NULL; ch->shop = NULL; GET_AC(ch) = 100; /* Basic Armor */ if (ch->points.max_mana < 100) ch->points.max_mana = 100; } void clear_object(itemData_t *obj) { memset((char *) obj, 0, sizeof(itemData_t)); obj->vnum = -1; obj->zone = NULL; IN_ROOM(obj) = NULL; obj->wornOn = -1; } /* * Called during character creation after picking character class * (and then never again for that character). */ void init_char(charData_t *ch) { int i; /* create a player_special structure */ if (ch->player_specials == NULL) CREATE(ch->player_specials, playerSpecials_t, 1); /* *** if this is our first player --- he be God *** */ if (top_of_p_table == 0) { GET_LEVEL(ch) = NUM_LEVELS; GET_AUTH(ch) = AUTH_OWNER; GET_EXP(ch) = level_exp(GET_CLASS(ch), NUM_LEVELS); /* Set some default flags for the owner. */ SET_BIT(PRF_FLAGS(ch), PRF_LOG1 | PRF_LOG2 | PRF_NOHASSLE | PRF_HOLYLIGHT | PRF_ROOMFLAGS); /* The implementor never goes through do_start(). */ GET_MAX_HIT(ch) = 500; GET_MAX_MANA(ch) = 100; GET_MAX_MOVE(ch) = 82; GET_HIT(ch) = GET_MAX_HIT(ch); GET_MANA(ch) = GET_MAX_MANA(ch); GET_MOVE(ch) = GET_MAX_MOVE(ch); } set_title(ch, NULL); ch->player.short_descr = NULL; ch->player.long_descr = NULL; ch->player.description = NULL; ch->player.time.birth = time(0); ch->player.time.logon = time(0); ch->player.time.played = 0; GET_HOME(ch) = 1; GET_AC(ch) = 100; for (i = 0; i < MAX_TONGUE; i++) GET_TALK(ch, i) = 0; /* * make favors for sex -- or in English, we bias the height and weight of the * character depending on what gender they've chosen for themselves. While it * is possible to have a tall, heavy female it's not as likely as a male. * * Height is in centimeters. Weight is in pounds. The only place they're * ever printed (in stock code) is SPELL_IDENTIFY. */ if (GET_SEX(ch) == SEX_MALE) { GET_WEIGHT(ch) = rand_number(120, 180); GET_HEIGHT(ch) = rand_number(160, 200); /* 5'4" - 6'8" */ } else { GET_WEIGHT(ch) = rand_number(100, 160); GET_HEIGHT(ch) = rand_number(150, 180); /* 5'0" - 6'0" */ } if ((i = get_ptable_by_name(GET_NAME(ch))) != -1) player_table[i].id = GET_IDNUM(ch) = ++top_idnum; else log("SYSERR: init_char: Character '%s' not found in player table.", GET_NAME(ch)); for (i = 1; i <= MAX_SKILLS; i++) { if (GET_AUTH(ch) < AUTH_WIZARD) SET_SKILL(ch, i, 0); else SET_SKILL(ch, i, 100); } AFF_FLAGS(ch) = 0; for (i = 0; i < 5; i++) GET_SAVE(ch, i) = 0; ch->real_abils.intel = 25; ch->real_abils.wis = 25; ch->real_abils.dex = 25; ch->real_abils.str = 25; ch->real_abils.str_add = 100; ch->real_abils.con = 25; ch->real_abils.cha = 25; for (i = 0; i < 3; i++) { GET_COND(ch, i) = (GET_AUTH(ch) >= AUTH_WIZARD ? -1 : 24); } GET_LOADROOM(ch) = NULL; } /* * Extend later to include more checks. * * TODO: Add checks for unknown bitvectors. */ int check_object(itemData_t *obj) { char objname[MAX_INPUT_LENGTH + 32]; int error = FALSE; if (GET_ITEM_WEIGHT(obj) < 0 && (error = TRUE)) log("SYSERR: Object %s:%d (%s) has negative weight (%d).", obj->zone->keyword, obj->vnum, obj->shortDescription, GET_ITEM_WEIGHT(obj)); if (GET_ITEM_RENT(obj) < 0 && (error = TRUE)) log("SYSERR: Object %s:%d (%s) has negative cost/day (%d).", obj->zone->keyword, obj->vnum, obj->shortDescription, GET_ITEM_RENT(obj)); snprintf(objname, sizeof(objname), "Object %s:%d (%s)", obj->zone->keyword, obj->vnum, obj->shortDescription); error |= check_bitvector_names(GET_ITEM_WEAR(obj), wear_bits_count, objname, "object wear"); error |= check_bitvector_names(GET_ITEM_EXTRA(obj), extra_bits_count, objname, "object extra"); error |= check_bitvector_names(GET_ITEM_AFFECT(obj), affected_bits_count, objname, "object affect"); switch (GET_ITEM_TYPE(obj)) { case ITEM_DRINKCON: { char onealias[MAX_INPUT_LENGTH], *space = strrchr(obj->name, ' '); strlcpy(onealias, space ? space + 1 : obj->name, sizeof(onealias)); if (search_block(onealias, drinknames, TRUE) < 0 && (error = TRUE)) log("SYSERR: Object %s:%d (%s) doesn't have drink type as last alias. (%s)", obj->zone->keyword, obj->vnum, obj->shortDescription, obj->name); } /* Fall through. */ case ITEM_FOUNTAIN: if (GET_ITEM_VAL(obj, 1) > GET_ITEM_VAL(obj, 0) && (error = TRUE)) log("SYSERR: Object %s:%d (%s) contains (%d) more than maximum (%d).", obj->zone->keyword, obj->vnum, obj->shortDescription, GET_ITEM_VAL(obj, 1), GET_ITEM_VAL(obj, 0)); break; case ITEM_SCROLL: case ITEM_POTION: error |= check_object_level(obj, 0); error |= check_object_spell_number(obj, 1); error |= check_object_spell_number(obj, 2); error |= check_object_spell_number(obj, 3); break; case ITEM_WAND: case ITEM_STAFF: error |= check_object_level(obj, 0); error |= check_object_spell_number(obj, 3); if (GET_ITEM_VAL(obj, 2) > GET_ITEM_VAL(obj, 1) && (error = TRUE)) log("SYSERR: Object %s:%d (%s) has more charges (%d) than maximum (%d).", obj->zone->keyword, obj->vnum, obj->shortDescription, GET_ITEM_VAL(obj, 2), GET_ITEM_VAL(obj, 1)); break; } return (error); } int check_object_spell_number(itemData_t *obj, int val) { int error = FALSE; const char *spellname; if (GET_ITEM_VAL(obj, val) == -1) /* i.e.: no spell */ return (error); /* * Check for negative spells, spells beyond the top define, and any * spell which is actually a skill. */ if (GET_ITEM_VAL(obj, val) < 0) error = TRUE; if (GET_ITEM_VAL(obj, val) > TOP_SPELL_DEFINE) error = TRUE; if (GET_ITEM_VAL(obj, val) > MAX_SPELLS && GET_ITEM_VAL(obj, val) <= MAX_SKILLS) error = TRUE; if (error) log("SYSERR: Object %s:%d (%s) has out of range spell #%d.", obj->zone->keyword, obj->vnum, obj->shortDescription, GET_ITEM_VAL(obj, val)); /* * This bug has been fixed, but if you don't like the special behavior... */ #if 0 if (GET_ITEM_TYPE(obj) == ITEM_STAFF && HAS_SPELL_ROUTINE(GET_ITEM_VAL(obj, val), MAG_AREAS | MAG_MASSES)) log("... '%s' (%s:%d) uses %s spell '%s'.", obj->shortDescription, obj->zone->keyword, obj->vnum, HAS_SPELL_ROUTINE(GET_ITEM_VAL(obj, val), MAG_AREAS) ? "area" : "mass", skill_name(GET_ITEM_VAL(obj, val))); #endif if (scheck) /* Spell names don't exist in syntax check mode. */ return (error); /* Now check for unnamed spells. */ spellname = skill_name(GET_ITEM_VAL(obj, val)); if ((spellname == unused_spellname || !str_cmp("UNDEFINED", spellname)) && (error = TRUE)) log("SYSERR: Object %s:%d (%s) uses '%s' spell #%d.", obj->zone->keyword, obj->vnum, obj->shortDescription, spellname, GET_ITEM_VAL(obj, val)); return (error); } int check_object_level(itemData_t *obj, int val) { int error = FALSE; if ((GET_ITEM_VAL(obj, val) < 0 || GET_ITEM_VAL(obj, val) > NUM_LEVELS) && (error = TRUE)) log("SYSERR: Object %s:%d (%s) has out of range level #%d.", obj->zone->keyword, obj->vnum, obj->shortDescription, GET_ITEM_VAL(obj, val)); return (error); } int check_bitvector_names(bitvector_t bits, size_t namecount, const char *whatami, const char *whatbits) { unsigned int flagnum; bool error = FALSE; /* See if any bits are set above the ones we know about. */ if (bits <= (~(bitvector_t)0 >> (sizeof(bitvector_t) * 8 - namecount))) return (FALSE); for (flagnum = namecount; flagnum < sizeof(bitvector_t) * 8; flagnum++) if ((1 << flagnum) & bits) { log("SYSERR: %s has unknown %s flag, bit %d (0 through %d known).", whatami, whatbits, flagnum, namecount - 1); error = TRUE; } return (error); }