]> git.pond.sub.org Git - empserver/blob - src/lib/commands/shark.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / shark.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  shark.c: Transfer a loan by buying it out
29  * 
30  *  Known contributors to this file:
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <math.h>
36 #include "misc.h"
37 #include "var.h"
38 #include "file.h"
39 #include "sect.h"
40 #include "item.h"
41 #include "loan.h"
42 #include "news.h"
43 #include "nat.h"
44 #include "xy.h"
45 #include "player.h"
46 #include "commands.h"
47 #include "optlist.h"
48
49 int
50 shark(void)
51 {
52     register int arg;
53     time_t now;
54     char *p;
55     struct lonstr loan;
56     struct natstr *natp;
57     struct natstr *oldie;
58     long due;
59     long last;
60     long rdur;
61     long xdur;
62     double rate;
63     double owed;
64     long payment;
65     s_char buf[1024];
66
67     if (!opt_LOANS) {
68         pr("Loans are not enabled.\n");
69         return RET_FAIL;
70     }
71     p = getstarg(player->argp[1], "Transfer which loan #: ", buf);
72     if (p == 0)
73         return RET_SYN;
74     if (*p == 0)
75         return RET_SYN;
76     arg = atoi(p);
77     if (arg < 0)
78         return RET_SYN;
79     /* Check if it's a valid loan to shark.  That means, is it a valid loan,
80        not owed to this player, with a valid duration and it's been signed. */
81     if (!getloan(arg, &loan) || (loan.l_loner == player->cnum) ||
82         (loan.l_ldur == 0) || (loan.l_status != LS_SIGNED)) {
83         pr("Invalid loan\n");
84         return RET_FAIL;
85     }
86     /* If we got here, we check to see if it's been defaulted on. */
87     (void)time(&now);
88     due = loan.l_duedate;
89     if (now <= due) {
90         pr("There has been no default on loan %d\n", arg);
91         return RET_FAIL;
92     }
93     last = loan.l_lastpay;
94     if (last < due && due < now) {
95         rdur = due - last;
96         xdur = now - due;
97     } else if (due < last) {
98         rdur = 0;
99         xdur = now - last;
100     }
101     rate = loan.l_irate / (loan.l_ldur * 8.64e6);
102
103     owed = ((rdur * rate) + (xdur * rate * 2.0) + 1.0);
104     if (((1 << 30) / owed) < loan.l_amtdue)
105         owed = (1 << 30);
106     else
107         owed *= loan.l_amtdue;
108     pr("That loan is worth $%.2f.\n", owed);
109     natp = getnatp(player->cnum);
110     payment = owed * (1.0 + loan.l_irate / 100.0);
111     if (payment > natp->nat_money - 100.0) {
112         pr("You do not have enough to cover that loan\n");
113         return RET_FAIL;
114     } else {
115         wu(0, loan.l_lonee,
116            "%s bought loan #%d.  You now owe him!\n",
117            cname(player->cnum), arg);
118         wu(0, loan.l_loner,
119            "%s bought loan #%d out from under you for %d\n",
120            cname(player->cnum), arg, payment);
121         pr("You now own loan #%d.  Go break some legs.\n", arg);
122     }
123 /*      NAT_DELTA(natp->nat_money, loan.l_loner, payment);*/
124     oldie = getnatp(loan.l_loner);
125     oldie->nat_money += payment;
126     player->dolcost += payment;
127     loan.l_loner = player->cnum;
128     putloan(arg, &loan);
129     return RET_OK;
130 }