objsave.cc
AwakeMUD-0.6Beta/src/objsave.cc · 41.6 KB
From the archive AwakeMUD-0.60_Beta.tgz
Download the archive « Back to the archive contents
/* ************************************************************************
* File: objsave.c Part of CircleMUD *
* Usage: loading/saving player objects for rent and crash-save *
* *
* 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. *
************************************************************************ */
/* now with auto-equip - BK */
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include "structs.h"
#include "awake.h"
#include "comm.h"
#include "handler.h"
#include "db.h"
#include "interpreter.h"
#include "utils.h"
#include "spells.h"
#include "newmagic.h"
/* these factors should be unique integers */
#define RENT_FACTOR 1
#define CRYO_FACTOR 4
int Crash_write_rentcode(struct char_data *ch, FILE *fl, struct rent_info *rent);
int find_hotel_cost(struct char_data *ch);
extern const struct str_app_type str_app[];
extern struct room_data *world;
extern struct index_data *mob_index;
extern struct index_data *obj_index;
extern struct descriptor_data *descriptor_list;
extern struct char_data *character_list;
extern struct player_index_element *player_table;
extern struct obj_data *obj_proto;
extern int top_of_p_table;
extern int min_rent_cost;
extern int fixers_need_save;
extern void obj_from_cyberware(struct obj_data * cyber);
extern void obj_to_cyberware(struct obj_data * object, struct char_data * ch);
extern void obj_from_bioware(struct obj_data *bio);
extern void obj_to_bioware(struct obj_data * object, struct char_data * ch);
extern void save_etext(struct char_data *ch);
extern void write_spells(struct char_data *ch);
extern void calc_weight(struct char_data *ch);
int fixers_need_save = 0;
/* Extern functions */
ACMD(do_action);
SPECIAL(receptionist);
SPECIAL(cryogenicist);
/*
For setup ASCII based eq files we need to write these 2 functions
The interesting Part is gona be seperating the data out
The first entry in the file should be rent info, since it is all ints
We can make a tag:
RENT: # # # # # # # # # # #
STRT:
Object's actual Data
END :
*/
/*
struct obj_data *Obj_from_store_to(struct obj_file_elem object, int *locate)
*/
/*
int Obj_to_store_from(struct obj_data * obj, FILE * fl, int locate)
*/
/*
void write_rent_info(struct rent_info, FILE *fl);
*/
/*
struct rent_info read_rent_info(FILE * fl);
*/
struct obj_data *Obj_from_store_to(struct obj_file_elem object, int *locate)
{
struct obj_data *obj;
int j;
if (real_object(object.item_number) > -1) {
obj = read_object(object.item_number, VIRTUAL);
*locate = (int) object.locate;
GET_OBJ_VAL(obj, 0) = object.value[0];
GET_OBJ_VAL(obj, 1) = object.value[1];
GET_OBJ_VAL(obj, 2) = object.value[2];
GET_OBJ_VAL(obj, 3) = object.value[3];
GET_OBJ_VAL(obj, 4) = object.value[4];
GET_OBJ_VAL(obj, 5) = object.value[5];
GET_OBJ_VAL(obj, 6) = object.value[6];
GET_OBJ_VAL(obj, 7) = object.value[7];
GET_OBJ_VAL(obj, 8) = object.value[8];
GET_OBJ_VAL(obj, 9) = object.value[9];
GET_OBJ_EXTRA(obj) = object.extra_flags;
GET_OBJ_WEIGHT(obj) = object.weight;
GET_OBJ_TIMER(obj) = object.timer;
obj->obj_flags.bitvector = object.bitvector;
obj->obj_flags.material = object.material;
obj->obj_flags.condition = object.condition;
for (j = 0; j < MAX_OBJ_AFFECT; j++)
obj->affected[j] = object.affected[j];
return obj;
} else
return NULL;
}
/* this function used in house.c */
struct obj_data *Obj_from_store(struct obj_file_elem object)
{
int locate;
return Obj_from_store_to(object, &locate);
}
void load_fixer_data(struct char_data *ch)
{
SPECIAL(fixer);
struct obj_data *obj;
struct obj_file_elem object;
FILE *fl;
char fname[40];
if (!IS_NPC(ch) || !GET_MOB_SPEC(ch) || GET_MOB_SPEC(ch) != fixer ||
mob_index[ch->nr].number != 1)
return;
sprintf(fname, "fixer_data/%d.fixer", GET_MOB_VNUM(ch));
if (!(fl = fopen(fname, "rb")))
return;
while (!feof(fl)) {
fread(&object, sizeof(struct obj_file_elem), 1, fl);
if (ferror(fl))
break;
if (!feof(fl))
if ((obj = Obj_from_store(object)))
obj_to_char(obj, ch);
}
fclose(fl);
}
int Obj_to_store_from(struct obj_data * obj, FILE * fl, int locate)
{
int j;
struct obj_file_elem object;
object.item_number = GET_OBJ_VNUM(obj);
object.locate = (sh_int) locate; /* where worn or inventory? */
object.value[0] = GET_OBJ_VAL(obj, 0);
object.value[1] = GET_OBJ_VAL(obj, 1);
object.value[2] = GET_OBJ_VAL(obj, 2);
object.value[3] = GET_OBJ_VAL(obj, 3);
object.value[4] = GET_OBJ_VAL(obj, 4);
object.value[5] = GET_OBJ_VAL(obj, 5);
object.value[6] = GET_OBJ_VAL(obj, 6);
object.value[7] = GET_OBJ_VAL(obj, 7);
object.value[8] = GET_OBJ_VAL(obj, 8);
object.value[9] = GET_OBJ_VAL(obj, 9);
object.extra_flags = GET_OBJ_EXTRA(obj);
object.weight = GET_OBJ_WEIGHT(obj);
object.timer = GET_OBJ_TIMER(obj);
object.material = GET_OBJ_MATERIAL(obj);
object.condition = GET_OBJ_CONDITION(obj);
object.bitvector = obj->obj_flags.bitvector;
for (j = 0; j < MAX_OBJ_AFFECT; j++)
object.affected[j] = obj->affected[j];
if (fwrite(&object, sizeof(struct obj_file_elem), 1, fl) < 1) {
perror("Error writing object in Obj_to_store");
return 0;
}
return 1;
}
int Obj_to_store(struct obj_data * obj, FILE * fl)
{
return Obj_to_store_from(obj, fl, 0);
}
void save_fixer_data(struct char_data *ch)
{
SPECIAL(fixer);
struct obj_data *obj;
FILE *fl;
char fname[40];
if (!fixers_need_save)
return;
if (IS_NPC(ch) && GET_MOB_SPEC(ch) && GET_MOB_SPEC(ch) == fixer) {
sprintf(fname, "fixer_data/%d.fixer", GET_MOB_VNUM(ch));
if (!(fl = fopen(fname, "wb+")))
return;
for (obj = ch->carrying; obj; obj = obj->next_content)
if (GET_OBJ_TIMER(obj) > 0)
Obj_to_store(obj, fl);
fclose(fl);
}
}
void display_rent_status(struct descriptor_data *d)
{
FILE *fl;
char fname[MAX_STRING_LENGTH];
struct rent_info rent;
float num_of_days;
int nuyen, account, cost;
if (!d->character || !GET_NAME(d->character) ||
!get_filename(GET_NAME(d->character), fname, CRASH_FILE)) {
SEND_TO_Q("Error opening rent file.\r\n", d);
return;
}
if (!(fl = fopen(fname, "r+b"))) {
if (errno != ENOENT) { /* if it fails, NOT because of no file */
sprintf(buf1, "SYSERR: READING OBJECT FILE %s (5)", fname);
perror(buf1);
}
SEND_TO_Q("Error opening rent file.\r\n", d);
return;
}
if (!feof(fl))
fread(&rent, sizeof(struct rent_info), 1, fl);
if (GET_LEVEL(d->character) >= LVL_LEGEND)
SEND_TO_Q("You're worried about rent?!\r\n", d);
else {
num_of_days = (float)(time(0) - rent.time) / SECS_PER_REAL_DAY;
num_of_days = MIN(num_of_days, 10.0);
cost = (int)(rent.net_cost_per_diem * num_of_days);
nuyen = rent.gold;
account = rent.account;
if (rent.net_cost_per_diem < 1) {
SEND_TO_Q("You're not paying for any services.\r\n", d);
return;
}
sprintf(buf, "You have %d ¥ cash and %d ¥ in the bank.\r\n"
"Total charge is currently %d ¥. "
"Room fees are %d ¥/day.\r\n", nuyen, account,
cost, rent.net_cost_per_diem);
if (cost > (nuyen + account))
strcat(buf, "You can't afford your rent!\r\n");
else if ((int)(rent.net_cost_per_diem * (10.0 - num_of_days)) <
(nuyen + account - cost))
strcat(buf, "You can (theoretically) afford to rent forever.\r\n");
else sprintf(buf + strlen(buf), "You can afford to rent for "
"%d more days.\r\n", (int)((nuyen + account - cost) /
rent.net_cost_per_diem));
SEND_TO_Q(buf, d);
}
}
int Crash_delete_file(char *name)
{
char filename[50], newfilename[50];
FILE *fl, *newfl = NULL;
struct rent_info rent;
struct obj_file_elem object;
bool found = FALSE;
int rnum;
if (!get_filename(name, filename, CRASH_FILE))
return 0;
if (!(fl = fopen(filename, "rb"))) {
if (errno != ENOENT) { /* if it fails but NOT because of no file */
sprintf(buf1, "SYSERR: deleting crash file %s (1)", filename);
perror(buf1);
}
return 0;
}
sprintf(newfilename, "%s.tmp", filename);
while (!feof(fl))
{
fread(&object, sizeof(struct obj_file_elem), 1, fl);
if (ferror(fl))
{
perror("Reading crash file: Crash_delete_file().");
break;
}
rnum = real_object(object.item_number);
if (!feof(fl) && rnum > -1)
{
if ((obj_proto[rnum].obj_flags.type_flag == ITEM_CYBERWARE ||
obj_proto[rnum].obj_flags.type_flag == ITEM_BIOWARE) &&
object.value[4] == 1)
{
if (!found)
{
if (!(newfl = fopen(newfilename, "wb")))
break;
rent.rentcode = RENT_CRASH;
rent.time = time(0);
if (!Crash_write_rentcode(NULL, newfl, &rent))
{
fclose(newfl);
break;
}
found = TRUE;
}
if (newfl && fwrite(&object, sizeof(struct obj_file_elem), 1, newfl) < 1)
{
perror("Error writing object in Crash_delete_file()");
break;
}
}
}
}
fclose(fl);
if (!found) {
if (unlink(filename) < 0) {
if (errno != ENOENT) { /* if it fails, NOT because of no file */
sprintf(buf1, "SYSERR: deleting crash file %s (2)", filename);
perror(buf1);
}
}
} else {
fclose(newfl);
if (rename(newfilename, filename) < 0) {
sprintf(buf1, "SYSERR: renaming crash file %s (2)", filename);
perror(buf1);
}
}
return 1;
}
int Crash_delete_crashfile(struct char_data * ch)
{
char fname[MAX_INPUT_LENGTH];
struct rent_info rent;
FILE *fl;
if (!get_filename(GET_NAME(ch), fname, CRASH_FILE))
return 0;
if (!(fl = fopen(fname, "rb"))) {
if (errno != ENOENT) { /* if it fails, NOT because of no file */
sprintf(buf1, "SYSERR: checking for crash file %s (3)", fname);
perror(buf1);
}
return 0;
}
if (!feof(fl))
fread(&rent, sizeof(struct rent_info), 1, fl);
fclose(fl);
if (rent.rentcode == RENT_CRASH)
Crash_delete_file(GET_NAME(ch));
return 1;
}
int Crash_clean_file(char *name)
{
char fname[MAX_STRING_LENGTH], filetype[20];
struct rent_info rent;
extern int rent_file_timeout, crash_file_timeout;
FILE *fl;
if (!get_filename(name, fname, CRASH_FILE))
return 0;
/*
* open for write so that permission problems will be flagged now, at boot
* time.
*/
if (!(fl = fopen(fname, "r+b"))) {
if (errno != ENOENT) { /* if it fails, NOT because of no file */
sprintf(buf1, "SYSERR: OPENING OBJECT FILE %s (4)", fname);
perror(buf1);
}
return 0;
}
if (!feof(fl))
fread(&rent, sizeof(struct rent_info), 1, fl);
fclose(fl);
if ((rent.rentcode == RENT_CRASH) ||
(rent.rentcode == RENT_FORCED) || (rent.rentcode == RENT_TIMEDOUT)) {
if (rent.time < time(0) - (crash_file_timeout * SECS_PER_REAL_DAY)) {
Crash_delete_file(name);
switch (rent.rentcode) {
case RENT_CRASH:
strcpy(filetype, "crash");
break;
case RENT_FORCED:
strcpy(filetype, "forced rent");
break;
case RENT_TIMEDOUT:
strcpy(filetype, "idlesave");
break;
default:
strcpy(filetype, "UNKNOWN!");
break;
}
sprintf(buf, " Deleting %s's %s file.", name, filetype);
log(buf);
return 1;
}
/* Must retrieve rented items w/in 30 days */
} else if (rent.rentcode == RENT_RENTED)
if (rent.time < time(0) - (rent_file_timeout * SECS_PER_REAL_DAY)) {
Crash_delete_file(name);
sprintf(buf, " Deleting %s's rent file.", name);
log(buf);
return 1;
}
return (0);
}
void update_obj_file(void)
{
int i;
for (i = 0; i <= top_of_p_table; i++)
Crash_clean_file((player_table + i)->name);
return;
}
void Crash_listrent(struct char_data * ch, char *name)
{
FILE *fl;
char fname[MAX_INPUT_LENGTH], buf[MAX_STRING_LENGTH];
struct obj_file_elem object;
struct obj_data *obj;
struct rent_info rent;
if (!get_filename(name, fname, CRASH_FILE))
return;
if (!(fl = fopen(fname, "rb"))) {
sprintf(buf, "%s has no rent file.\r\n", name);
send_to_char(buf, ch);
return;
}
sprintf(buf, "%s\r\n", fname);
if (!feof(fl))
fread(&rent, sizeof(struct rent_info), 1, fl);
switch (rent.rentcode) {
case RENT_RENTED:
strcat(buf, "Rent\r\n");
break;
case RENT_CRASH:
strcat(buf, "Crash\r\n");
break;
case RENT_CRYO:
strcat(buf, "Cryo\r\n");
break;
case RENT_TIMEDOUT:
case RENT_FORCED:
strcat(buf, "TimedOut\r\n");
break;
default:
strcat(buf, "Undef\r\n");
break;
}
while (!feof(fl)) {
fread(&object, sizeof(struct obj_file_elem), 1, fl);
if (ferror(fl)) {
fclose(fl);
return;
}
if (!feof(fl))
if (real_object(object.item_number) > -1) {
obj = read_object(object.item_number, VIRTUAL);
sprintf(buf, "%s [%5d] (%5dau) <%2d> %-20s\r\n", buf,
object.item_number, GET_OBJ_RENT(obj),
object.locate, obj->short_description);
extract_obj(obj);
}
}
send_to_char(buf, ch);
fclose(fl);
}
int Crash_write_rentcode(struct char_data *ch, FILE *fl, struct rent_info *rent)
{
rent->net_cost_per_diem = 0;
if (fwrite(rent, sizeof(struct rent_info), 1, fl) < 1) {
perror("Writing rent code.");
return 0;
}
return 1;
}
/* so this is gonna be the auto equip (hopefully) */
void auto_equip(struct char_data *ch, struct obj_data *obj, int locate)
{
int j;
if (locate > 0) { /* was worn */
switch (j = locate-1) {
case WEAR_LIGHT:
break;
case WEAR_FINGER_R:
case WEAR_FINGER_L:
if (!CAN_WEAR(obj,ITEM_WEAR_FINGER)) /* not fitting :( */
locate = 0;
break;
case WEAR_NECK_1:
case WEAR_NECK_2:
if (!CAN_WEAR(obj,ITEM_WEAR_NECK))
locate = 0;
break;
case WEAR_BODY:
if (!CAN_WEAR(obj,ITEM_WEAR_BODY))
locate = 0;
break;
case WEAR_HEAD:
if (!CAN_WEAR(obj,ITEM_WEAR_HEAD))
locate = 0;
break;
case WEAR_LEGS:
if (!CAN_WEAR(obj,ITEM_WEAR_LEGS))
locate = 0;
break;
case WEAR_FEET:
if (!CAN_WEAR(obj,ITEM_WEAR_FEET))
locate = 0;
break;
case WEAR_HANDS:
if (!CAN_WEAR(obj,ITEM_WEAR_HANDS))
locate = 0;
break;
case WEAR_ARMS:
if (!CAN_WEAR(obj,ITEM_WEAR_ARMS))
locate = 0;
break;
case WEAR_SHIELD:
if (!CAN_WEAR(obj,ITEM_WEAR_SHIELD))
locate = 0;
break;
case WEAR_ABOUT:
if (!CAN_WEAR(obj,ITEM_WEAR_ABOUT))
locate = 0;
break;
case WEAR_WAIST:
if (!CAN_WEAR(obj,ITEM_WEAR_WAIST))
locate = 0;
break;
case WEAR_WRIST_R:
case WEAR_WRIST_L:
if (!CAN_WEAR(obj,ITEM_WEAR_WRIST))
locate = 0;
break;
case WEAR_WIELD:
if ((!GET_WIELDED(ch, 0) && !CAN_WEAR(obj,ITEM_WEAR_HOLD)) ||
(GET_WIELDED(ch, 0) && !CAN_WEAR(obj, ITEM_WEAR_WIELD)))
locate = 0;
break;
case WEAR_HOLD:
if ((!GET_WIELDED(ch, 1) && !CAN_WEAR(obj,ITEM_WEAR_HOLD)) ||
(GET_WIELDED(ch, 1) && !CAN_WEAR(obj, ITEM_WEAR_WIELD)))
locate = 0;
break;
case WEAR_EYES:
if (!CAN_WEAR(obj,ITEM_WEAR_EYES))
locate = 0;
break;
case WEAR_EAR:
if (!CAN_WEAR(obj,ITEM_WEAR_EAR))
locate = 0;
break;
default:
locate = 0;
}
if (locate > 0)
if (!GET_EQ(ch,j))
equip_char(ch, obj, j);
else /* oops - saved player with double equipment[j]? */
locate = 0;
}
if (locate <= 0)
obj_to_char(obj, ch);
}
#define MAX_BAG_ROW 5
/* should be enough - who would carry a bag in a bag in a bag in a
bag in a bag in a bag ?!? */
int Crash_load(struct char_data * ch)
/* return values:
0 - successful load, keep char in rent room.
1 - load failure or load of crash items -- put char in temple.
2 - rented equipment lost (no $)
*/
{
void Crash_crashsave(struct char_data * ch);
FILE *fl;
char fname[MAX_STRING_LENGTH];
struct obj_file_elem object;
struct rent_info rent;
int cost, orig_rent_code;
float num_of_days;
struct obj_data *obj, *obj1, *cont_row[MAX_BAG_ROW];
int locate, j;
if (!get_filename(GET_NAME(ch), fname, CRASH_FILE))
return 1;
if (!(fl = fopen(fname, "r+b"))) {
if (errno != ENOENT) { /* if it fails, NOT because of no file */
sprintf(buf1, "SYSERR: READING OBJECT FILE %s (5)", fname);
perror(buf1);
send_to_char("\r\n********************* NOTICE *********************\r\n"
"There was a problem loading your objects from disk.\r\n"
"Contact a God for assistance.\r\n", ch);
}
sprintf(buf, "%s entering game with no equipment.", GET_NAME(ch));
mudlog(buf, ch, LOG_CONNLOG, FALSE);
return 1;
}
if (!feof(fl))
fread(&rent, sizeof(struct rent_info), 1, fl);
#if !RENT_ENABLED
rent.net_cost_per_diem = 0;
#endif
if ( (rent.net_cost_per_diem > 0) && (GET_LEVEL(ch) < LVL_LEGEND) && !(PLR_FLAGGED(ch, PLR_NEWBIE)) ) {
num_of_days = (float) (time(0) - rent.time) / SECS_PER_REAL_DAY;
num_of_days = MIN(num_of_days, 10.0);
cost = (int) (rent.net_cost_per_diem * num_of_days);
if (cost > GET_NUYEN(ch) + GET_BANK(ch)) {
fclose(fl);
sprintf(buf, "%s entering game, RENT LOSS: %.2f * %d = %d > %d + %d.",
GET_NAME(ch), num_of_days, rent.net_cost_per_diem,
cost, GET_NUYEN(ch), GET_BANK(ch));
mudlog(buf, ch, LOG_CONNLOG, TRUE);
#if RENT_ENABLED
Crash_crashsave(ch);
#endif
return 2;
}
else {
#if RENT_ENABLED
GET_BANK(ch) -= MAX(cost - GET_NUYEN(ch), 0);
GET_NUYEN(ch) = MAX(GET_NUYEN(ch) - cost, 0);
#endif
save_char(ch, GET_LOADROOM(ch));
}
}
switch (orig_rent_code = rent.rentcode) {
case RENT_RENTED:
sprintf(buf, "%s un-renting and entering game.", GET_NAME(ch));
mudlog(buf, ch, LOG_CONNLOG, FALSE);
break;
case RENT_CRASH:
sprintf(buf, "%s retrieving crash-saved items and entering game.", GET_NAME(ch));
mudlog(buf, ch, LOG_CONNLOG, FALSE);
break;
case RENT_CRYO:
sprintf(buf, "%s un-cryo'ing and entering game.", GET_NAME(ch));
mudlog(buf, ch, LOG_CONNLOG, FALSE);
break;
case RENT_FORCED:
case RENT_TIMEDOUT:
sprintf(buf, "%s retrieving force-saved items and entering game.", GET_NAME(ch));
mudlog(buf, ch, LOG_CONNLOG, FALSE);
break;
default:
sprintf(buf, "WARNING: %s entering game with undefined rent code.", GET_NAME(ch));
mudlog(buf, ch, LOG_CONNLOG, TRUE);
break;
}
for (j = 0;j < MAX_BAG_ROW;j++)
cont_row[j] = NULL; /* empty all cont lists (you never know ...) */
while (!feof(fl)) {
fread(&object, sizeof(struct obj_file_elem), 1, fl);
if (ferror(fl)) {
perror("Reading crash file: Crash_load.");
fclose(fl);
return 1;
}
if (!feof(fl)) {
if ((obj = Obj_from_store_to(object, &locate))) {
// first I gotta check for cyberware, and equip it if it's there
if (GET_OBJ_TYPE(obj) == ITEM_CYBERWARE && (GET_OBJ_VAL(obj, 4) == 1))
obj_to_cyberware(obj, ch);
else if (GET_OBJ_TYPE(obj) == ITEM_BIOWARE && GET_OBJ_VAL(obj, 4) == 1)
obj_to_bioware(obj, ch);
else { // then I run it through the other routines if it's not cyberware
auto_equip(ch, obj, locate);
if (locate > 0) { /* item equipped */
for (j = MAX_BAG_ROW-1;j > 0;j--)
if (cont_row[j]) { /* no container -> back to ch's inventory */
for (;cont_row[j];cont_row[j] = obj1) {
obj1 = cont_row[j]->next_content;
obj_to_char(cont_row[j], ch);
}
cont_row[j] = NULL;
}
if (cont_row[0]) { /* content list existing */
if (GET_OBJ_TYPE(obj) == ITEM_CONTAINER ||
GET_OBJ_TYPE(obj) == ITEM_CYBERDECK) {
/* rem item ; fill ; equip again */
obj = unequip_char(ch, locate-1);
obj->contains = NULL; /* should be empty - but who knows */
for (;cont_row[0];cont_row[0] = obj1) {
obj1 = cont_row[0]->next_content;
obj_to_obj(cont_row[0], obj);
}
equip_char(ch, obj, locate-1);
} else { /* object isn't container -> empty content list */
for (;cont_row[0];cont_row[0] = obj1) {
obj1 = cont_row[0]->next_content;
obj_to_char(cont_row[0], ch);
}
cont_row[0] = NULL;
}
}
} else { /* locate <= 0 */
for (j = MAX_BAG_ROW-1;j > -locate;j--)
if (cont_row[j]) { /* no container -> back to ch's inventory */
for (;cont_row[j];cont_row[j] = obj1) {
obj1 = cont_row[j]->next_content;
obj_to_char(cont_row[j], ch);
}
cont_row[j] = NULL;
}
if (j == -locate && cont_row[j]) { /* content list existing */
if (GET_OBJ_TYPE(obj) == ITEM_CONTAINER ||
GET_OBJ_TYPE(obj) == ITEM_CYBERDECK) {
/* take item ; fill ; give to char again */
obj_from_char(obj);
obj->contains = NULL;
for (;cont_row[j];cont_row[j] = obj1) {
obj1 = cont_row[j]->next_content;
obj_to_obj(cont_row[j], obj);
}
obj_to_char(obj, ch); /* add to inv first ... */
} else { /* object isn't container -> empty content list */
for (;cont_row[j];cont_row[j] = obj1) {
obj1 = cont_row[j]->next_content;
obj_to_char(cont_row[j], ch);
}
cont_row[j] = NULL;
}
}
if (locate < 0 && locate >= -MAX_BAG_ROW) {
// let obj be part of content list
// but put it at the list's end thus having the items
// in the same order as before renting
obj_from_char(obj);
if ((obj1 = cont_row[-locate-1])) {
while (obj1->next_content)
obj1 = obj1->next_content;
obj1->next_content = obj;
} else cont_row[-locate-1] = obj;
}
}
}
}
}
}
/* turn this into a crash file by re-writing the control block */
rent.rentcode = RENT_CRASH;
rent.time = time(0);
rewind(fl);
Crash_write_rentcode(ch, fl, &rent);
fclose(fl);
if ((orig_rent_code == RENT_RENTED) || (orig_rent_code == RENT_CRYO))
return 0;
else return 1;
}
int Crash_save(struct obj_data * obj, FILE * fp, int locate)
{
struct obj_data *tmp;
int result;
if (obj) {
Crash_save(obj->next_content, fp, locate);
// don't save quest objs to rent...
if (obj->obj_flags.quest_id > 0) {
Crash_save(obj->contains, fp, locate);
result = 1;
} else {
Crash_save(obj->contains, fp, MIN(0,locate)-1);
result = Obj_to_store_from(obj, fp, locate);
}
for (tmp = obj->in_obj; tmp; tmp = tmp->in_obj)
GET_OBJ_WEIGHT(tmp) -= GET_OBJ_WEIGHT(obj);
if (!result)
return 0;
}
return TRUE;
}
// recursion! yeck!!! --cjd --gotta rewrite this stuff sometime
void Crash_restore_weight(struct obj_data * obj)
{
if (obj) {
Crash_restore_weight(obj->contains);
Crash_restore_weight(obj->next_content);
if (obj->in_obj)
GET_OBJ_WEIGHT(obj->in_obj) += GET_OBJ_WEIGHT(obj);
}
}
void Crash_extract_objs(struct obj_data * obj)
{
if (obj) {
Crash_extract_objs(obj->contains);
Crash_extract_objs(obj->next_content);
extract_obj(obj);
}
}
int Crash_is_unrentable(struct obj_data * obj)
{
if (!obj)
return 0;
if (IS_OBJ_STAT(obj, ITEM_NORENT) || GET_OBJ_RENT(obj) < 0 ||
GET_OBJ_RNUM(obj) <= NOTHING || GET_OBJ_TYPE(obj) == ITEM_KEY || obj->worn_on == WEAR_PATCH)
return 1;
return 0;
}
void Crash_extract_norents(struct obj_data * obj)
{
if (obj) {
Crash_extract_norents(obj->contains);
Crash_extract_norents(obj->next_content);
if (Crash_is_unrentable(obj))
extract_obj(obj);
}
}
/* get norent items from eq to inventory and
extract norents out of worn containers */
void Crash_extract_norents_from_equipped(struct char_data * ch)
{
int j;
for (j = 0;j < NUM_WEARS;j++) {
if (GET_EQ(ch,j)) {
if (IS_OBJ_STAT(GET_EQ(ch,j), ITEM_NORENT) ||
GET_OBJ_RENT(GET_EQ(ch,j)) < 0 ||
GET_OBJ_RNUM(GET_EQ(ch,j)) <= NOTHING ||
GET_OBJ_TYPE(GET_EQ(ch,j)) == ITEM_KEY)
obj_to_char(unequip_char(ch,j),ch);
else Crash_extract_norents(GET_EQ(ch,j));
}
}
}
void Crash_extract_expensive(struct obj_data * obj)
{
struct obj_data *tobj, *max;
max = obj;
for (tobj = obj; tobj; tobj = tobj->next_content)
if (GET_OBJ_RENT(tobj) > GET_OBJ_RENT(max))
max = tobj;
extract_obj(max);
}
void Crash_calculate_rent(struct obj_data * obj, int *cost)
{
if (obj) {
*cost += MAX(0, GET_OBJ_RENT(obj));
Crash_calculate_rent(obj->contains, cost);
Crash_calculate_rent(obj->next_content, cost);
}
}
void Crash_crashsave(struct char_data * ch)
{
char buf[MAX_INPUT_LENGTH];
struct rent_info rent;
int j, wield[2];
FILE *fp;
struct obj_data *obj, *next_obj;
if (IS_NPC(ch))
return;
if (!get_filename(GET_NAME(ch), buf, CRASH_FILE))
return;
if (!(fp = fopen(buf, "wb")))
return;
rent.rentcode = RENT_CRASH;
rent.time = time(0);
if (!Crash_write_rentcode(ch, fp, &rent)) {
fclose(fp);
return;
}
/* place cyberware into their inventory in case of a crash -cjd */
for (obj = ch->cyberware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_cyberware(obj);
obj_to_char(obj, ch);
}
for (obj = ch->bioware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_bioware(obj);
obj_to_char(obj, ch);
}
if (!PLR_FLAGGED(ch, PLR_LOADROOM) && GET_LOADROOM(ch) < 2000 && GET_LEVEL(ch) < LVL_LEGEND)
{
if (PLR_FLAGGED(ch, PLR_NEWBIE))
GET_LOADROOM(ch) = 8039;
else GET_LOADROOM(ch) = 2127;
}
wield[0] = GET_WIELDED(ch, 0);
wield[1] = GET_WIELDED(ch, 1);
for (j = 0; j < NUM_WEARS; j++)
if (GET_EQ(ch,j)) {
if (!Crash_save(GET_EQ(ch,j), fp, j+1)) {
fclose(fp);
return;
}
Crash_restore_weight(GET_EQ(ch,j));
}
GET_WIELDED(ch, 0) = wield[0];
GET_WIELDED(ch, 1) = wield[1];
if (!Crash_save(ch->carrying, fp, 0)) {
fclose(fp);
return;
}
// after we've saved their inventory, we restore cyberware to proper
// position
for (obj = ch->carrying; obj; obj = next_obj) {
next_obj = obj->next_content;
if (GET_OBJ_TYPE(obj) == ITEM_CYBERWARE && GET_OBJ_VAL(obj, 4) == 1) {
obj_from_char(obj);
obj_to_cyberware(obj, ch);
}
}
for (obj = ch->carrying; obj; obj = next_obj) {
next_obj = obj->next_content;
if (GET_OBJ_TYPE(obj) == ITEM_BIOWARE && GET_OBJ_VAL(obj, 4) == 1) {
obj_from_char(obj);
obj_to_bioware(obj, ch);
}
}
Crash_restore_weight(ch->carrying);
//calc_weight(ch);
fclose(fp);
REMOVE_BIT(PLR_FLAGS(ch), PLR_CRASH);
}
void Crash_idlesave(struct char_data * ch)
{
char buf[MAX_INPUT_LENGTH];
struct rent_info rent;
int j, wield[2], cost, cost_eq, numitems = 0;
extern int max_obj_save;
FILE *fp;
struct obj_data *obj, *next_obj, *temp;
if (IS_NPC(ch))
return;
if (!get_filename(GET_NAME(ch), buf, CRASH_FILE))
return;
if (!(fp = fopen(buf, "wb")))
return;
/*
* Added code for people who want to just idle out so they don't
* have to worry about the item limit
*/
for (obj = ch->carrying; obj; obj = obj->next_content) {
numitems++;
for (temp = obj->contains; temp; temp = temp->next_content)
numitems++;
}
while ( GET_LEVEL(ch) < LVL_LEGEND && max_obj_save < numitems-- )
obj_from_char(ch->carrying);
Crash_extract_norents_from_equipped(ch);
Crash_extract_norents(ch->carrying);
// remove cyberware
for (obj = ch->cyberware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_cyberware(obj);
obj_to_char(obj, ch);
}
for (obj = ch->bioware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_bioware(obj);
obj_to_char(obj, ch);
}
if (!PLR_FLAGGED(ch, PLR_LOADROOM) && GET_LOADROOM(ch) < 2000 && GET_LEVEL(ch) < LVL_LEGEND)
{
if (PLR_FLAGGED(ch, PLR_NEWBIE))
GET_LOADROOM(ch) = 8039;
else GET_LOADROOM(ch) = 2127;
}
cost = find_hotel_cost(ch);
cost_eq = 0;
cost <<= 1; /* forcerent cost is 2x normal rent */
wield[0] = GET_WIELDED(ch, 0);
wield[1] = GET_WIELDED(ch, 1);
#if RENT_ENABLED
if (cost+cost_eq > GET_NUYEN(ch) + GET_BANK(ch)) {
for (j = 0; j < NUM_WEARS; j++) /* unequip player with low money */
if (GET_EQ(ch,j))
obj_to_char(unequip_char(ch, j), ch);
cost += cost_eq;
cost_eq = 0;
while ((cost > GET_NUYEN(ch) + GET_BANK(ch)) && ch->carrying) {
Crash_extract_expensive(ch->carrying);
cost = 0;
Crash_calculate_rent(ch->carrying, &cost);
cost <<= 1;
}
}
#endif
if (!ch->carrying) {
for (j = 0; j < NUM_WEARS && !(GET_EQ(ch,j)); j++)
;
if (j == NUM_WEARS) { /* no eq nor inv */
fclose(fp);
Crash_delete_file(GET_NAME(ch));
return;
}
}
rent.net_cost_per_diem = cost;
rent.rentcode = RENT_TIMEDOUT;
rent.time = time(0);
rent.gold = GET_NUYEN(ch);
rent.account = GET_BANK(ch);
if (!Crash_write_rentcode(ch, fp, &rent)) {
fclose(fp);
return;
}
for (j = 0; j < NUM_WEARS; j++)
if (GET_EQ(ch,j)) {
if (!Crash_save(GET_EQ(ch,j), fp, j+1)) {
fclose(fp);
return;
}
Crash_restore_weight(GET_EQ(ch,j));
Crash_extract_objs(GET_EQ(ch,j));
}
GET_WIELDED(ch, 0) = wield[0];
GET_WIELDED(ch, 1) = wield[1];
//calc_weight(ch);
if (!Crash_save(ch->carrying, fp, 0)) {
fclose(fp);
return;
}
fclose(fp);
Crash_extract_objs(ch->carrying);
}
void Crash_rentsave(struct char_data * ch, int cost)
{
char buf[MAX_INPUT_LENGTH];
struct rent_info rent;
int j, wield[2];
FILE *fp;
struct obj_data *obj, *next_obj;
if (IS_NPC(ch) || !get_filename(GET_NAME(ch), buf, CRASH_FILE))
return;
if (!(fp = fopen(buf, "wb")))
return;
cost = find_hotel_cost(ch);
Crash_extract_norents_from_equipped(ch);
for (obj = ch->cyberware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_cyberware(obj);
obj_to_char(obj, ch);
}
for (obj = ch->bioware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_bioware(obj);
obj_to_char(obj, ch);
}
Crash_extract_norents(ch->carrying);
rent.net_cost_per_diem = cost;
rent.rentcode = RENT_RENTED;
rent.time = time(0);
rent.gold = GET_NUYEN(ch);
rent.account = GET_BANK(ch);
if (!Crash_write_rentcode(ch, fp, &rent)) {
fclose(fp);
return;
}
wield[0] = GET_WIELDED(ch, 0);
wield[1] = GET_WIELDED(ch, 1);
for (j = 0; j < NUM_WEARS; j++)
if (GET_EQ(ch,j)) {
if (!Crash_save(GET_EQ(ch,j), fp, j+1)) {
fclose(fp);
return;
}
Crash_restore_weight(GET_EQ(ch,j));
Crash_extract_objs(GET_EQ(ch,j));
}
GET_WIELDED(ch, 0) = wield[0];
GET_WIELDED(ch, 1) = wield[1];
//calc_weight(ch);
if (!Crash_save(ch->carrying, fp, 0)) {
fclose(fp);
return;
}
fclose(fp);
Crash_extract_objs(ch->carrying);
}
void Crash_cryosave(struct char_data * ch, int cost)
{
char buf[MAX_INPUT_LENGTH];
struct rent_info rent;
int j, wield[2];
FILE *fp;
struct obj_data *obj, *next_obj;
if (IS_NPC(ch))
return;
if (!get_filename(GET_NAME(ch), buf, CRASH_FILE))
return;
if (!(fp = fopen(buf, "wb")))
return;
cost = (int)(find_hotel_cost(ch) * 7.5);
/* remove cyberware into inventory for saving -cjd */
for (obj = ch->cyberware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_cyberware(obj);
obj_to_char(obj, ch);
}
for (obj = ch->bioware; obj; obj = next_obj) {
next_obj = obj->next_content;
obj_from_bioware(obj);
obj_to_char(obj, ch);
}
Crash_extract_norents_from_equipped(ch);
Crash_extract_norents(ch->carrying);
#if RENT_ENABLED
GET_NUYEN(ch) = MAX(0, GET_NUYEN(ch) - cost);
#endif
rent.rentcode = RENT_CRYO;
rent.time = time(0);
rent.gold = GET_NUYEN(ch);
rent.account = GET_BANK(ch);
rent.net_cost_per_diem = 0;
if (!Crash_write_rentcode(ch, fp, &rent)) {
fclose(fp);
return;
}
wield[0] = GET_WIELDED(ch, 0);
wield[1] = GET_WIELDED(ch, 1);
for (j = 0; j < NUM_WEARS; j++)
if (GET_EQ(ch,j)) {
if (!Crash_save(GET_EQ(ch,j), fp, j+1)) {
fclose(fp);
return;
}
Crash_restore_weight(GET_EQ(ch,j));
Crash_extract_objs(GET_EQ(ch,j));
}
GET_WIELDED(ch, 0) = wield[0];
GET_WIELDED(ch, 1) = wield[1];
//calc_weight(ch);
if (!Crash_save(ch->carrying, fp, 0)) {
fclose(fp);
return;
}
fclose(fp);
Crash_extract_objs(ch->carrying);
SET_BIT(PLR_FLAGS(ch), PLR_CRYO);
}
/* ************************************************************************
* Routines used for the receptionist *
************************************************************************* */
void Crash_rent_deadline(struct char_data * ch, struct char_data * recep,
long cost)
{
long rent_deadline;
if (!cost)
return;
rent_deadline = ((GET_NUYEN(ch) + GET_BANK(ch)) / cost);
if (rent_deadline >= 10)
sprintf(buf, "$n tells you, \"You can rent forever with the nuyen you have.\"");
else sprintf(buf, "$n tells you, \"You can rent for %ld night%s with the nuyen you have.\r\n",
rent_deadline, (rent_deadline > 1) ? "s" : "");
act(buf, FALSE, recep, 0, ch, TO_VICT);
}
int Crash_report_unrentables(struct char_data * ch, struct char_data * recep,
struct obj_data * obj)
{
char buf[128];
int has_norents = 0;
if (obj) {
if (Crash_is_unrentable(obj)) {
has_norents = 1;
sprintf(buf, "$n tells you, 'You cannot store %s.'", OBJS(obj, ch));
act(buf, FALSE, recep, 0, ch, TO_VICT);
}
has_norents += Crash_report_unrentables(ch, recep, obj->contains);
has_norents += Crash_report_unrentables(ch, recep, obj->next_content);
}
return (has_norents);
}
void Crash_report_rent(struct char_data * ch, struct char_data * recep,
struct obj_data * obj, long *cost, long *nitems, int display,
int factor)
{
static char buf[256];
if (obj) {
if (!Crash_is_unrentable(obj)) {
(*nitems)++;
*cost += MAX(0, (GET_OBJ_RENT(obj) * factor));
if (display) {
sprintf(buf, "$n tells you, '%5d nuyen for %s..'",
(GET_OBJ_RENT(obj) * factor), OBJS(obj, ch));
act(buf, FALSE, recep, 0, ch, TO_VICT);
}
}
Crash_report_rent(ch, recep, obj->contains, cost, nitems, display, factor);
Crash_report_rent(ch, recep, obj->next_content, cost, nitems, display, factor);
}
}
int find_hotel_cost(struct char_data *ch)
{
float cost = 0.0;
int val;
switch (GET_LOADROOM(ch)) {
case 1939: case 1940: cost = 11.0;
case 2126: case 2127: cost = 6.25;
case 8038: case 8039: cost = 1.4;
case 9990: case 9991: cost = 5.5;
}
val = (int)(cost * MAX(1, (GET_REP(ch) + number(-1, 1)) / 10));
return val;
}
int find_hotel_room(int room)
{
switch (room) {
case 1939: return 1940;
case 2126: return 2127;
case 8038: return 8039;
case 9990: return 9991;
}
return room;
}
int Crash_offer_rent(struct char_data * ch, struct char_data * receptionist,
int display, int factor)
{
extern int max_obj_save; /* change in config.c */
char buf[MAX_INPUT_LENGTH];
struct obj_data *obj, *temp;
int i;
long totalcost = 0, numitems = 0, norent = 0;
norent = Crash_report_unrentables(ch, receptionist, ch->carrying);
for (i = 0; i < NUM_WEARS; i++)
norent += Crash_report_unrentables(ch, receptionist, GET_EQ(ch,i));
if (norent)
return 0;
i = GET_LOADROOM(ch);
GET_LOADROOM(ch) = world[ch->in_room].number;
totalcost = find_hotel_cost(ch);
GET_LOADROOM(ch) = i;
// Crash_report_rent(ch, receptionist, ch->carrying, &totalcost, &numitems, display, factor);
for (i = 0; i < NUM_WEARS; i++)
// Crash_report_rent(ch, receptionist, GET_EQ(ch,i), &totalcost, &numitems, display, factor);
if (GET_EQ(ch, i)) {
numitems++;
for (temp = GET_EQ(ch, i)->contains; temp; temp = temp->next_content)
numitems++;
}
for (obj = ch->carrying; obj; obj = obj->next_content) {
numitems++;
for (temp = obj->contains; temp; temp = temp->next_content)
numitems++;
}
// if (!numitems) {
// act("$n tells you, \"But you are not carrying anything rentable!\"",
// FALSE, receptionist, 0, ch, TO_VICT);
// return (0);
// }
if (numitems > max_obj_save) {
sprintf(buf, "$n tells you, \"Sorry, but you are only allowed %d items.\"",
max_obj_save);
act(buf, FALSE, receptionist, 0, ch, TO_VICT);
return (0);
}
if (display) {
sprintf(buf, "$n tells you, \"Plus, my %d credit fee..\"", min_rent_cost * factor);
act(buf, FALSE, receptionist, 0, ch, TO_VICT);
sprintf(buf, "$n tells you, \"For a total of %ld nuyen%s.\"",
totalcost, (factor == RENT_FACTOR ? " per day" : ""));
act(buf, FALSE, receptionist, 0, ch, TO_VICT);
if (totalcost > GET_NUYEN(ch)) {
act("$n tells you, '...which I see you can't afford.'",
FALSE, receptionist, 0, ch, TO_VICT);
return (0);
} else if (factor == RENT_FACTOR)
Crash_rent_deadline(ch, receptionist, totalcost);
}
return (totalcost);
}
int gen_receptionist(struct char_data * ch, struct char_data * recep,
int cmd, char *arg, int mode)
{
int cost = 0;
extern int free_rent;
sh_int save_room;
char *action_table[] = {"smile", "dance", "sigh", "blush", "burp", "cough",
"fart", "twiddle", "yawn"};
ACMD(do_say);
if (!ch->desc || IS_NPC(ch))
return FALSE;
if (!cmd && !number(0, 5)) {
do_action(recep, "", find_command(action_table[number(0, 8)]), 0);
return FALSE;
}
if (!CMD_IS("offer") && !CMD_IS("rent"))
return FALSE;
if (!AWAKE(recep)) {
send_to_char("Sleeping receptionists aren't much help, I'm afraid...\r\n", ch);
return TRUE;
}
if (!CAN_SEE(recep, ch)) {
do_say(recep, "I don't deal with people I can't see!", 0, 0);
return TRUE;
}
if (free_rent) {
act("$n tells you, \"Rent is free here. Just quit, and your objects will be saved!\"",
FALSE, recep, 0, ch, TO_VICT);
return 1;
}
if (!(cost = Crash_offer_rent(ch, recep, FALSE, mode)))
return TRUE;
if (CMD_IS("offer")) {
sprintf(buf, "$n tells you, \"Rooms here are %d nuyen per night.\"", cost);
act(buf, FALSE, recep, 0, ch, TO_VICT);
return TRUE;
}
if (mode == RENT_FACTOR)
sprintf(buf, "$n tells you, \"Rooms are %d nuyen per night.\"", cost);
else if (mode == CRYO_FACTOR)
sprintf(buf, "$n tells you, \"It will cost you %d nuyen to be frozen.\"", cost);
act(buf, FALSE, recep, 0, ch, TO_VICT);
if (cost > GET_NUYEN(ch)) {
act("$n tells you, \"...which I see you can't afford.\"", FALSE, recep, 0, ch, TO_VICT);
return TRUE;
}
if (cost && (mode == RENT_FACTOR))
Crash_rent_deadline(ch, recep, cost);
save_room = find_hotel_room(world[ch->in_room].number);
GET_LOADROOM(ch) = save_room;
if (mode == RENT_FACTOR) {
act("$n gives you a key and shows you to your room.", FALSE, recep, 0, ch, TO_VICT);
Crash_rentsave(ch, cost);
sprintf(buf, "%s has rented at %d", GET_NAME(ch), world[ch->in_room].number);
act("$n helps $N into $S room.", FALSE, recep, 0, ch, TO_NOTVICT);
} else { /* cryo */
act("$n stores your belongings and helps you into your private chamber.\r\n"
"A white mist appears in the room, chilling you to the bone...\r\n"
"You begin to lose consciousness...", FALSE, recep, 0, ch, TO_VICT);
Crash_cryosave(ch, cost);
sprintf(buf, "%s has cryo-rented.", GET_NAME(ch));
SET_BIT(PLR_FLAGS(ch), PLR_CRYO);
act("$n helps $N into $S private chamber.", FALSE, recep, 0, ch, TO_NOTVICT);
}
mudlog(buf, ch, LOG_CONNLOG, FALSE);
save_etext(ch);
write_spells(ch);
if (ch->desc && GET_LEVEL(ch) < LVL_LEGEND)
STATE(ch->desc) = CON_QMENU;
extract_char(ch);
ch->in_room = real_room(save_room);
save_char(ch, GET_LOADROOM(ch));
return TRUE;
}
SPECIAL(receptionist)
{
return (gen_receptionist(ch, (struct char_data *)me, cmd, argument, RENT_FACTOR));
}
SPECIAL(cryogenicist)
{
return (gen_receptionist(ch, (struct char_data *)me, cmd, argument, CRYO_FACTOR));
}
void save_fixers(void)
{
SPECIAL(fixer);
struct char_data *ch;
if (!fixers_need_save)
return;
for (ch = character_list; ch; ch = ch->next)
if (IS_NPC(ch) && GET_MOB_SPEC(ch) && GET_MOB_SPEC(ch) == fixer)
save_fixer_data(ch);
}
void Crash_save_all(void)
{
struct descriptor_data *d;
struct char_data *ch;
for (d = descriptor_list; d; d = d->next) {
if (!(ch = d->original))
ch = d->character;
if (STATE(d) != CON_PLAYING && ch) {
Crash_crashsave(ch);
save_char(ch, GET_LOADROOM(ch));
write_spells(ch);
save_etext(ch);
REMOVE_BIT(PLR_FLAGS(ch), PLR_CRASH);
}
}
}