clan/
class/
data/
doc/ideas/
doc/mobprogs/
gods/
log/
player/
time/
/*
 *  The unique portions of SunderMud code as well as the integration efforts
 *  for code from other sources is based on the efforts of:
 *
 *  Lotherius (elfren@aros.net)
 *
 *  This code can only be used under the terms of the DikuMud, Merc,
 *  and ROM licenses. The same requirements apply to the changes that
 *  have been made.
 *
 * All other copyrights remain in place and in force.
*/

/* routines related to leasing. Entire file 100% unique coding by Lotherius (elfren@aros.net)*/

#include "everything.h"

/* local functions */

void save_leases();

void do_lease( CHAR_DATA *ch, char *argument )
{
    char buf[MAX_STRING_LENGTH];

    /* is room rentable at all? */
    if (IS_SET(ch->in_room->room_flags, ROOM_RENT) )
    {
	if (ch->in_room->rented_by)  /* if room is currently being rented */
	{
	    if (ch->in_room->rented_by != ch->name)  /* if room is rented by ch */
	    {
		send_to_char("This room is not currently for rent.\n\r",ch);
		return; /* if not, return with no further action. */
	    } /* end of currently rented*/
	} /* end of rented_by */

	sprintf(buf,"{CThe monthly rent on this room is ${Y%ld{x.\n\r",
		ch->in_room->room_rent); /* how much is the rent per month */
	send_to_char( buf , ch );

	if (ch->in_room->rented_by) /* if it is being rented (by ch) then when is rent due */
	{
	    sprintf(buf,"{CYour rent is paid until Month %d of Year %d.\n\r", /* month/dd/yyyy */
		    ch->in_room->paid_month,
		    ch->in_room->paid_year );
	    send_to_char(buf,ch);
	}
    
	if (argument[0] != '\0') /* if there's an argument, we're going to rent for longer */
	{
	    int timeleft; /* time left on lease */
	    int months;

	    if (!is_number(argument))
	    {
		send_to_char("{RInvalid argument. Syntax: lease <number>.{x\n\r",ch);
		return;
	    }

	    months = atoi(argument);

	    if (months < 1 ) /* trying to unrent? */
	    {
		send_to_char("{MYou must rent for at least 1 full month.{x\n\r",ch);
		return;
	    } /* end of unrent */

	    if (months > 17 ) /* can't rent for longer than 1 mud year */
	    {
		send_to_char("{MYou can only rent for a maximum of 1 year.{x\n\r",ch);
		return;
	    } /* end of too long rent */

	    timeleft = 0;

	    if (ch->in_room->paid_year < time_info.year)
	    {
		timeleft = 0;
	    }
	    else if (ch->in_room->paid_year == time_info.year)
	    {
		timeleft = ch->in_room->paid_month - time_info.month;
	    }
	    else if (ch->in_room->paid_year > time_info.year)
	    {
		timeleft = ch->in_room->paid_month - time_info.month;
		timeleft += 17;
	    }

	    if (timeleft >= 17)
	    {
		send_to_char("{YYou are currently paid for a full year.{x\n\r",ch);
		return;
	    }

	    if ( (timeleft + months) > 17 )
	    {
		send_to_char("{MYou can't rent past 1 year. Try fewer months.{x\n\r",ch);
		return;
	    } /* now all that's out of the way, we have an almost valid number of months to rent. */

	    if ( (months * ch->in_room->room_rent) > ch->gold )
	    {
		send_to_char("{RYou don't have enough gold.{x\n\r",ch);
/* 		if ( (ch->gold / (months * ch->in_room->room_rent) ) >= 1 )
		    sprintf(buf,"You have enough gold for {Y%d{x months.\n\r",
			    (ch->gold / (months * ch->in_room->room_rent)) ); */
		return;
	    } /* end of gold check, now we know if there's enough gold or not. */
	       /* rentable room, okay time, enough gold. */

	    ch->gold -= months * ch->in_room->room_rent;
	    ch->in_room->paid_month = time_info.month;
	    ch->in_room->paid_year = time_info.year;

	    if ( (timeleft + months) > 17 ) 
	    {
		++ch->in_room->paid_year;
	    }
	    else
	    {
		if ( ( time_info.month + (months + timeleft) ) > 17 )
		{
		    ++ch->in_room->paid_year;
		    ch->in_room->paid_month += ( (months + timeleft) - 17 );
		}
		else
		{
		ch->in_room->paid_month += (months + timeleft);
		}
	    }
	    sprintf(buf,"{CYou have now rented this room for ${Y%ld{C gold coins for a total of %d months.\n\r{x",
		    months * ch->in_room->room_rent,
		    months );
	    send_to_char(buf,ch);
	    sprintf(buf,"{CThe rent is NOW paid until Month %d of Year %d.{x\n\r",
		    ch->in_room->paid_month,
		    ch->in_room->paid_year );
	    send_to_char(buf,ch);
	    act( "$n has signed a lease on this room.", ch, NULL, NULL, TO_ROOM );
	    free_string( ch->in_room->rented_by );
            ch->in_room->rented_by = str_dup( ch->name );
	    save_leases();
	    return;
	} /* end of rental code! */

    } /* end of able to rent */

    send_to_char("This room cannot be rented.\n\r",ch);
    return;

} /* end of do_lease */

void do_checklease ( CHAR_DATA *ch, char *argument ) /* immortal command to show lease info */
{
    char buf[MAX_STRING_LENGTH];

    if (IS_SET(ch->in_room->room_flags, ROOM_RENT) ) /* if rentable */
    {
	if (ch->in_room->rented_by) /* if currently being rented */
	{
	    sprintf (buf,"{GRented By:     {W%s\n\r", /* who */
		     ch->in_room->rented_by );
	    send_to_char(buf,ch);
	    sprintf (buf,"{GMonthly Rent:  {W%ld\n\r", /* how much */
		     ch->in_room->room_rent );
	    send_to_char(buf,ch);
	    sprintf (buf,"{GLease Expires: {WMonth %d of Year %d.{x\n\r", /* until when */
		    ch->in_room->paid_month,
		    ch->in_room->paid_year );
	    send_to_char(buf,ch);
	    return;
	} /* end of display for currently rented */
	else /* not currently being rented */
	{
	    send_to_char("{GNot currently being rented.\n\r",ch);
	    sprintf (buf,"Monthly Rent:  {W%ld{x\n\r", /* how much */
		     ch->in_room->room_rent);
	    send_to_char(buf,ch);
	    return;
	} /* end of not currently rented */
    } /* end of rentable */

    send_to_char("{GThis is a non-rentable room.{x\n\r",ch);
    return;
} /* end of do_checklease */


void save_leases( )
{
    FILE *fp = NULL;
    ROOM_INDEX_DATA *pRoomIndex; 

    int iHash;
    char buf[MAX_STRING_LENGTH];

#if defined(DEBUGINFO)
    log_string("DEBUG: do_save_leases: begin");
#endif

    sprintf (buf, "%sLease.DAT", DATA_DIR);

    fp = fopen (buf, "w");

    if (!fp)
    {
	bug ("Could not open Lease.DAT for writing!",0);
	return;
    }

    for ( iHash = 0; iHash < MAX_KEY_HASH; iHash++ )
    {
	for ( pRoomIndex = room_index_hash[iHash]; pRoomIndex; pRoomIndex = pRoomIndex->next )
	{
	    if (IS_SET(pRoomIndex->room_flags, ROOM_RENT) )
	    {
		fprintf (fp, "L %d %ld %d %d %d %s~\n",
			 pRoomIndex->vnum,
			 pRoomIndex->room_rent,
			 pRoomIndex->paid_month,
			 pRoomIndex->paid_day,
			 pRoomIndex->paid_year,
			 pRoomIndex->rented_by);
	    } /* end of is rentable */
	} /* end of proomindex for loop */
    } /* end of iHash for */

    fprintf( fp, "END\n" );

    fclose ( fp );

    return;
}

void load_leases( )
{
    FILE *fp = NULL;
    char buf[MAX_STRING_LENGTH];
    char *word;
    bool fMatch;
    bool done=FALSE;
    ROOM_INDEX_DATA *pRoomIndex; 

    sprintf (buf, "%sLease.DAT", DATA_DIR);

    fp = fopen (buf, "r");

    if (!fp)
    {
	bug ("Could not open Lease.DAT for Reading!",0);
	return;
    }

    fMatch = FALSE;

    while (!done)
    {
	word = fread_word( fp );
	if (!str_cmp(word,"L"))
	{
	    pRoomIndex = get_room_index ( fread_number( fp ) );
	    pRoomIndex->room_rent	= fread_number( fp );
	    pRoomIndex->paid_month	= fread_number( fp );
	    pRoomIndex->paid_day	= fread_number( fp );
	    pRoomIndex->paid_year	= fread_number( fp );
	    free_string(pRoomIndex->rented_by);
	    pRoomIndex->rented_by	= fread_string( fp );
	}
	if (!str_cmp(word,"END"))
	{
	    done = TRUE;
	    break;
	}
    } /* end of while !done */

    fclose (fp);
    return;
}