/*This snippet alows for corpses to save through copyovers.
 *Pretty simple, and easy to install. Probably the more difficult
 *stuff is in fwrite_obj and fread_obj for making it able to save
 *without CHAR_DATA, but here we go!
 *
 *First off, we'll convert fread_obj. In case 'E': under the END str_cmp, there's
 *a few lines near the bottom of the call that look like */

 if ( iNest == 0 || rgObjNest[iNest] == NULL )
	obj_to_char( obj, ch );
 else
	obj_to_obj( obj, rgObjNest[iNest-1] );

/*That all has to be changed to this->*/
if ( iNest == 0 || rgObjNest[iNest] == NULL )
{   if(ch)
		obj_to_char( obj, ch );
	else
		obj_to_room(obj, obj->in_room );
}
else
	obj_to_obj( obj, rgObjNest[iNest-1] );

/*Right below that, add this to case 'I':*/

if(!str_cmp(word, "Inrm" ) )
{   obj->in_room = get_room_index(fread_number(fp) );
	fMatch = TRUE;
	break;
}

/*Now to fwrite_obj. The lines near the top of the function that look like this*/

if ( (ch->level < obj->level - 15 && obj->item_type != ITEM_CONTAINER)
    ||   obj->item_type == ITEM_KEY
    ||   (obj->item_type == ITEM_MAP && !obj->value[0]))
        return;

/* make it look like */
	if(ch)//<--- This was all that was added. Just to make sure ch isn't null.
    if ( (ch->level < obj->level - 15 && obj->item_type != ITEM_CONTAINER)
    ||   obj->item_type == ITEM_KEY
    ||   (obj->item_type == ITEM_MAP && !obj->value[0]))
        return;
/*Somewhere in there, throw this in*/

    if(!ch && obj->in_room)
        fprintf(fp, "Inrm %d\n", obj->in_room->vnum );

/*Thats it for save.c. Go into act_wiz.c or wherever do_copyover is.
 *Now, I use all my file_open stuff that Chil wrote up (Also avialable here look for file_open) And
 *I don't know where all the calls to fpReserve are *blush* Sooo, I'll just give you my whole do_copyover
 *For those who dun want the whole thing, or can convert the file_open stuff, I labled the object saving
 *code. As well, your gunna have to add *obj *fpObj, has_reset and *pReset to the variable declarations.*/

 void do_copyover (CHAR_DATA *ch, char * argument)
{
	FILE *fp = NULL, *fpObj;
	DESCRIPTOR_DATA *d, *d_next;
	char buf [100], buf2[100];
	extern int port,control; /* db.c */
	bool asave = TRUE, has_reset = FALSE;
	char arg[MSL];
	CHAR_DATA *wch;
	OBJ_DATA *obj;
    RESET_DATA *pReset;
	arg[0] = '\0';

	argument = one_argument(argument,arg);

	if(!str_cmp(arg, "no_asave") )
	    asave = FALSE;

	fp = file_open(COPYOVER_FILE, "w");

        while (auction_list != NULL)
           reset_auc (auction_list, TRUE);

        save_gquest_data();

        for(d = descriptor_list; d; d = d->next )
	{
	   wch = CH(d);
	   if( d->pString )
	   {
		sprintf(buf, "%s is currently in a string editor.\r\n", wch->name);
		send_to_char(buf, ch);
		return;
	   }
	}
          
	/* Consider changing all saved areas here, if you use OLC */
	if(asave)
        do_function(NULL, &do_asave, "changed" );   
	
	sprintf (buf, "\n\r *** COPYOVER by %s - please remain seated!\n\r", ch->name);

	/* For each playing descriptor, save its state */
	for (d = descriptor_list; d ; d = d_next)
	{
		CHAR_DATA * och = CH (d);
		d_next = d->next; /* We delete from the list , so need to save this */
		
		if ( d->connected > CON_PLAYING ) /* drop those logging on */
		{
			send_to_char("Nuh uh! No copying over while people arn't playing. Check your sockets!",ch);
			return;
		}
		else
		{
			fprintf (fp, "%d %s %s\n", d->descriptor, och->name, d->host);

#if 0			 /* This is not necessary for ROM */
			if (och->level == 1)
			{
				write_to_descriptor (d->descriptor, "Since you are level one, and level one characters do not save, you gain a free level!\n\r", 0);
				advance_level (och);
				och->level++; /* Advance_level doesn't do that */
			}
#endif			
			save_char_obj (och);
			
			write_to_descriptor (d->descriptor, buf, 0);
		}
	}
	
	fprintf (fp, "-1\n");
	file_close(fp);
	fpObj = file_open("../area/objcopy.txt", "w" );
    /***** BEGINING OF OBJECT SAVING FOR COPYOVERS *****/
    for ( obj = object_list; obj != NULL; obj = obj->next )
	{   has_reset = FALSE;
		if(obj->carried_by )
			continue;
		if(obj->item_type != ITEM_CORPSE_PC )
			continue;
		for ( pReset = obj->in_room->reset_first; pReset; pReset = pReset->next )
		{   if(( (pReset->command == 'O' || pReset->command == 'P') && pReset->arg3 == obj->pIndexData->vnum ) ||
				( pReset->command == 'E' && pReset->arg1 == obj->pIndexData->vnum ) )
			{	has_reset = TRUE;
				break;
			}
		}
		if(has_reset)
			continue;
		fwrite_obj(NULL, obj, fpObj, 0 );
	}
	fprintf(fpObj, "#END\n");
	file_close(fpObj);
	/**** END OF OBJECT SAVING FOR COPYOVERS *****/

	/* exec - descriptors are inherited */
	
	sprintf (buf, "%d", port);
	sprintf (buf2, "%d", control);
	execl (EXE_FILE, "rom", buf, "copyover", buf2, (char *) NULL);

	/* Failed - sucessful exec will not return */
	
	perror ("do_copyover: execl");
	send_to_char ("Copyover FAILED!\n\r",ch);
	
}

/*Now right below do_copyover should be copyover_recover! Now, to that we add this to 
 *The apsolute bottom of the function, add */

 	load_copyover_obj();

/*Then, add this function */

void load_copyover_obj(void)
{	FILE *fpObj;
	char letter;
    char *word;
	
	if(!file_exists("../area/objcopy.txt" ) )
		return;
	fpObj = file_open("../area/objcopy.txt", "r" );
	unlink("../area/objcopy.txt"); //To prevent from char's doubling corpses.
	logf("Loading pc corpses.");
	for(;;)
	{	

        letter = fread_letter( fpObj );
        if ( letter == '*' )
        {	fread_to_eol( fpObj );
            continue;
        }
	    if ( letter != '#' )
        {	bug( "Load_char_obj: # not found.", 0 );
            break;
        }
		logf2("Freading word.");
        word = fread_word( fpObj );
			 if ( !str_cmp( word, "OBJECT" ) ) fread_obj  ( NULL, fpObj );
        else if ( !str_cmp( word, "O"      ) ) fread_obj  ( NULL, fpObj );
        else if ( !str_cmp( word, "END"    ) ) break;
        else
        {	bug( "Load_copyover_obj: bad section.", 0 );
            break;
        }
	}
	file_close(fpObj);
	return;
}

/*Thats all you need. Just run a clean make, add some protocals and your set!
 *Note: I will not help you to convert my code to the fopen and such, however
 *if you switch to file_open, I shall aid you in adding it to all your code.

 *Please send comments/critisizm/questions to davionkalhen@hotmail.com
 *Have fun with this, and love your builders!

 Davion*/
