(loan_owed): New, factored out common code.

(coll, fina, repa, shark, disloan): Use it.
This commit is contained in:
Markus Armbruster 2004-08-20 08:16:34 +00:00
parent e502056dab
commit e7c9da21e5
6 changed files with 49 additions and 147 deletions

View file

@ -57,9 +57,6 @@ coll(void)
struct lonstr loan;
struct sctstr sect;
coord x, y;
long rdur;
long xdur;
double rate;
double owed;
double pay;
s_char buf[1024];
@ -79,33 +76,12 @@ coll(void)
}
/* If we got here, we check to see if it's been defaulted on. We
already know it's owed to this player. */
(void)time(&now);
/*
* split duration now - l_lastpay into regular (up to l_duedate)
* and extended (beyond l_duedate)
*/
rdur = loan.l_duedate - loan.l_lastpay;
xdur = now - loan.l_duedate;
if (rdur < 0) {
xdur += rdur;
rdur = 0;
}
if (xdur <= 0) {
owed = loan_owed(&loan, time(&now));
if (now <= loan.l_duedate) {
pr("There has been no default on loan %d\n", arg);
return RET_FAIL;
}
rate = loan.l_irate / (loan.l_ldur * 8.64e6);
/* changed following to avoid overflow 3/27/89 bailey@math-cs.kent.edu
owed = ((rdur * rate) + (xdur * rate * 2.0) + 1.0) * loan.l_amtdue;
Begin overflow fix */
owed = ((rdur * rate) + (xdur * rate * 2.0) + 1.0);
if (((1 << 30) / owed) < loan.l_amtdue)
owed = (1 << 30);
else
owed *= loan.l_amtdue;
/* End overflow fix */
pr("You are owed $%.2f on that loan.\n", owed);
if (!(p = getstarg(player->argp[2],
"What sector do you wish to confiscate? ", buf)))
@ -137,7 +113,7 @@ coll(void)
}
if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
caploss(&sect, sect.sct_own, "that was %s's capital!\n");
sect.sct_item[I_MILIT] = 1; /* FIXME no where did this guy come from? */
sect.sct_item[I_MILIT] = 1; /* FIXME now where did this guy come from? */
/* Consider modifying takeover to take a "no che" argument and
putting using it here again. */

View file

@ -52,10 +52,6 @@ fina(void)
struct lonstr loan;
struct nstr_item ni;
time_t now;
int rdur;
int xdur;
double rate;
double amt;
if (!opt_LOANS) {
pr("Loans are not enabled.\n");
@ -71,40 +67,12 @@ fina(void)
while (nxtitem(&ni, (s_char *)&loan)) {
if (loan.l_status != LS_SIGNED)
continue;
/*
* split duration now - l_lastpay into regular (up to l_duedate)
* and extended (beyond l_duedate)
*/
rdur = loan.l_duedate - loan.l_lastpay;
xdur = now - loan.l_duedate;
if (rdur < 0) {
xdur += rdur;
rdur = 0;
}
if (xdur < 0) {
rdur += xdur;
xdur = 0;
}
if (CANT_HAPPEN(loan.l_ldur == 0))
continue;
rate = loan.l_irate / (loan.l_ldur * 8640000.0);
/* changed following to avoid overflow 3/27/89 bailey@math-cs.kent.edu
amt = (rdur * rate + xdur * rate * 2.0 + 1.0) * loan.l_amtdue;
Begin overflow fix */
amt = (rdur * rate + xdur * rate * 2.0 + 1.0);
if (((1 << 30) / amt) < loan.l_amtdue)
amt = (1 << 30);
else
amt *= loan.l_amtdue;
/* End overflow fix */
pr(" %-2d (%3d) %-8.8s (%3d) %-8.8s ", ni.cur,
loan.l_loner, cname(loan.l_loner),
loan.l_lonee, cname(loan.l_lonee));
pr("%3d%% %3d %5ld %7d",
loan.l_irate, loan.l_ldur, loan.l_amtpaid, (int)amt);
pr("%3d%% %3d %5ld %7ld",
loan.l_irate, loan.l_ldur, loan.l_amtpaid,
(long)loan_owed(&loan, now));
if (now > loan.l_duedate)
pr(" (in arrears)\n");
else

View file

@ -50,11 +50,7 @@ repa(void)
int loan_num;
long payment;
long owe;
long normaltime;
long doubletime;
double rate_per_sec, amt;
s_char *cp;
time_t now;
s_char buf[1024];
if (!opt_LOANS) {
@ -73,36 +69,7 @@ repa(void)
pr("You don't owe anything on that loan.\n");
return RET_FAIL;
}
(void)time(&now);
/*
* split duration now - l_lastpay into regular (up to l_duedate)
* and extended (beyond l_duedate)
*/
normaltime = loan.l_duedate - loan.l_lastpay;
doubletime = now - loan.l_duedate;
if (normaltime < 0) {
doubletime += normaltime;
normaltime = 0;
}
if (doubletime < 0) {
normaltime += doubletime;
doubletime = 0;
}
rate_per_sec = loan.l_irate /
((double)loan.l_ldur * SECS_PER_DAY * 100.0);
owe = (long)(loan.l_amtdue *
((double)normaltime * rate_per_sec + 1.0 +
(double)doubletime * rate_per_sec * 2.0) + 0.5);
amt = ((double)normaltime * rate_per_sec + 1.0 +
(double)doubletime * rate_per_sec * 2.0);
if (((1 << 30) / amt) < loan.l_amtdue)
owe = (1 << 30);
else
owe = (long)(loan.l_amtdue *
((double)normaltime * rate_per_sec + 1.0 +
(double)doubletime * rate_per_sec * 2.0) + 0.5);
owe = (long)loan_owed(&loan, time(NULL));
if ((cp = getstarg(player->argp[2], "amount? ", buf)) == 0)
return RET_SYN;
if (!check_loan_ok(&loan))

View file

@ -55,9 +55,6 @@ shark(void)
struct lonstr loan;
struct natstr *natp;
struct natstr *oldie;
long rdur;
long xdur;
double rate;
double owed;
long payment;
s_char buf[1024];
@ -82,29 +79,11 @@ shark(void)
return RET_FAIL;
}
/* If we got here, we check to see if it's been defaulted on. */
(void)time(&now);
/*
* split duration now - l_lastpay into regular (up to l_duedate)
* and extended (beyond l_duedate)
*/
rdur = loan.l_duedate - loan.l_lastpay;
xdur = now - loan.l_duedate;
if (rdur < 0) {
xdur += rdur;
rdur = 0;
}
if (xdur <= 0) {
owed = loan_owed(&loan, time(&now));
if (now <= loan.l_duedate) {
pr("There has been no default on loan %d\n", arg);
return RET_FAIL;
}
rate = loan.l_irate / (loan.l_ldur * 8.64e6);
owed = ((rdur * rate) + (xdur * rate * 2.0) + 1.0);
if (((1 << 30) / owed) < loan.l_amtdue)
owed = (1 << 30);
else
owed *= loan.l_amtdue;
pr("That loan is worth $%.2f.\n", owed);
natp = getnatp(player->cnum);
payment = owed * (1.0 + loan.l_irate / 100.0);
@ -120,7 +99,6 @@ shark(void)
cname(player->cnum), arg, payment);
pr("You now own loan #%d. Go break some legs.\n", arg);
}
/* NAT_DELTA(natp->nat_money, loan.l_loner, payment);*/
oldie = getnatp(loan.l_loner);
oldie->nat_money += payment;
player->dolcost += payment;

View file

@ -51,14 +51,10 @@
#include "prototypes.h"
int
disloan(int n, register struct lonstr *loan)
disloan(int n, struct lonstr *loan)
{
time_t now;
time_t normaltime;
time_t doubletime;
time_t accept;
double rate;
double owe;
if (loan->l_status == LS_FREE)
return 0;
@ -84,30 +80,11 @@ disloan(int n, register struct lonstr *loan)
return 1;
}
/*
* split duration now - l_lastpay into regular (up to l_duedate)
* and extended (beyond l_duedate)
*/
normaltime = loan->l_duedate - loan->l_lastpay;
doubletime = now - loan->l_duedate;
if (normaltime < 0) {
doubletime += normaltime;
normaltime = 0;
}
if (doubletime < 0) {
normaltime += doubletime;
doubletime = 0;
}
rate = ((double)loan->l_irate / 100.0) / (loan->l_ldur * SECS_PER_DAY);
owe = ((double)loan->l_amtdue *
(((double)normaltime * rate + 1.0) +
((double)doubletime * rate * 2.0))) + 0.5;
pr("Amount paid to date $%ld\n", loan->l_amtpaid);
pr("Amount due (if paid now) $%.2f", owe);
if (doubletime == 0) {
pr("Amount due (if paid now) $%.2f", loan_owed(loan, now));
if (now <= loan->l_duedate) {
pr(" (if paid on due date) $%.2f\n",
loan->l_amtdue * ((loan->l_duedate - loan->l_lastpay) * rate + 1.0));
loan_owed(loan, loan->l_duedate));
pr("Due date is %s", ctime(&loan->l_duedate));
} else
pr(" ** In Arrears **\n");

View file

@ -282,3 +282,39 @@ get_outstand(int cnum)
}
return loantot;
}
/*
* Return amount due for LOAN at time PAYTIME.
*/
double
loan_owed(struct lonstr *loan, time_t paytime)
{
time_t rtime; /* regular interest time */
time_t xtime; /* double interest time */
double rate;
int dur;
/*
* Split interval paytime - l_lastpay into regular (up to
* l_duedate) and extended (beyond l_duedate) time.
*/
rtime = loan->l_duedate - loan->l_lastpay;
xtime = paytime - loan->l_duedate;
if (rtime < 0) {
xtime += rtime;
rtime = 0;
}
if (xtime < 0) {
rtime += xtime;
xtime = 0;
}
if (CANT_HAPPEN(rtime < 0))
rtime = 0;
dur = loan->l_ldur;
if (CANT_HAPPEN(dur <= 0))
dur = 1;
rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
}