]> git.pond.sub.org Git - empserver/blob - src/lib/commands/coll.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / coll.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  *  coll.c: Collet on a loan
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 "player.h"
38 #include "var.h"
39 #include "file.h"
40 #include "sect.h"
41 #include "item.h"
42 #include "loan.h"
43 #include "news.h"
44 #include "nat.h"
45 #include "xy.h"
46 #include "commands.h"
47 #include "optlist.h"
48
49 int
50 coll(void)
51 {
52     register int arg;
53     register int i;
54     register int val;
55     time_t now;
56     s_char *p;
57     struct lonstr loan;
58     struct sctstr sect;
59     coord x, y;
60     long due;
61     long last;
62     long rdur;
63     long xdur;
64     double rate;
65     double owed;
66     double pay;
67     s_char buf[1024];
68     int vec[I_MAX + 1];
69
70     if (!opt_LOANS) {
71         pr("Loans are not enabled.\n");
72         return RET_FAIL;
73     }
74     if ((arg = onearg(player->argp[1], "Collect on loan #")) < 0)
75         return RET_SYN;
76     /* Check if it's a valid loan.  That means, is it a valid loan,
77        owed to this player, with a valid duration and it's been signed. */
78     if (!getloan(arg, &loan) || (loan.l_loner != player->cnum) ||
79         (loan.l_ldur == 0) || (loan.l_status != LS_SIGNED)) {
80         pr("You aren't owed anything on that loan...\n");
81         return RET_FAIL;
82     }
83     /* If we got here, we check to see if it's been defaulted on.  We
84        already know it's owed to this player. */
85     (void)time(&now);
86     due = loan.l_duedate;
87     if (now <= due) {
88         pr("There has been no default on loan %d\n", arg);
89         return RET_FAIL;
90     }
91     last = loan.l_lastpay;
92     if (last < due && due < now) {
93         rdur = due - last;
94         xdur = now - due;
95     } else if (due < last) {
96         rdur = 0;
97         xdur = now - last;
98     }
99     rate = loan.l_irate / (loan.l_ldur * 8.64e6);
100
101 /* changed following to avoid overflow 3/27/89 bailey@math-cs.kent.edu
102         owed = ((rdur * rate) + (xdur * rate * 2.0) + 1.0) * loan.l_amtdue;
103    Begin overflow fix */
104     owed = ((rdur * rate) + (xdur * rate * 2.0) + 1.0);
105     if (((1 << 30) / owed) < loan.l_amtdue)
106         owed = (1 << 30);
107     else
108         owed *= loan.l_amtdue;
109 /* End overflow fix */
110     pr("You are owed $%.2f on that loan.\n", owed);
111     if (!
112         (p =
113          getstarg(player->argp[2],
114                   "What sector do you wish to confiscate? ", buf)))
115         return RET_SYN;
116     if (!check_loan_ok(&loan))
117         return RET_FAIL;
118     if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
119         return RET_SYN;
120     if (!neigh(x, y, player->cnum)) {
121         pr("You are not adjacent to %s\n", xyas(x, y, player->cnum));
122         return RET_FAIL;
123     }
124     if (sect.sct_own != loan.l_lonee) {
125         pr("%s is not owned by %s.\n",
126            xyas(x, y, player->cnum), cname(loan.l_lonee));
127         return RET_FAIL;
128     }
129     pay = dchr[sect.sct_type].d_value * ((float)sect.sct_effic + 100.0);
130     for (i = 0; ichr[i].i_name; i++) {
131         if (ichr[i].i_value == 0 || ichr[i].i_vtype == 0)
132             continue;
133         val = getvar(ichr[i].i_vtype, (s_char *)&sect, EF_SECTOR);
134         pay += val * ichr[i].i_value;
135     }
136     pr("That sector (and its contents) is valued at $%.2f\n", pay);
137     if (pay > owed * 1.2) {
138         pr("That is more than is owed!\n");
139         return RET_FAIL;
140     }
141     if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
142         caploss(&sect, sect.sct_own, "that was %s's capital!\n");
143     putvar(V_MILIT, 1, (s_char *)&sect, EF_SECTOR);
144
145 /* Consider modifying takeover to take a "no che" argument and
146    putting using it here again. */
147 /*      (void) takeover(&sect, player->cnum);*/
148     makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
149     makenotlost(EF_SECTOR, player->cnum, 0, sect.sct_x, sect.sct_y);
150     sect.sct_own = player->cnum;
151
152     bzero((s_char *)vec, sizeof(vec));
153     putvec(VT_DIST, vec, (s_char *)&sect, EF_SECTOR);
154     putvec(VT_DEL, vec, (s_char *)&sect, EF_SECTOR);
155     sect.sct_off = 1;
156     sect.sct_dist_x = sect.sct_x;
157     sect.sct_dist_y = sect.sct_y;
158
159     putsect(&sect);
160     nreport(player->cnum, N_SEIZE_SECT, loan.l_lonee, 1);
161     if (pay >= owed) {
162         loan.l_ldur = 0;
163         nreport(loan.l_lonee, N_REPAY_LOAN, player->cnum, 1);
164         wu(0, loan.l_lonee,
165            "%s seized %s to satisfy loan #%d\n",
166            cname(player->cnum),
167            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
168         pr("That loan is now considered repaid.\n");
169     } else {
170         (void)time(&loan.l_lastpay);
171         owed -= pay;
172         loan.l_amtdue = (long)owed;
173         pay += loan.l_amtpaid;
174         loan.l_amtpaid = pay;
175         wu(0, loan.l_lonee,
176            "%s seized %s in partial payment of loan %d.\n",
177            cname(player->cnum),
178            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
179         pr("You are still owed $%.2f on loan %d.\n", owed, arg);
180     }
181     putloan(arg, &loan);
182     return RET_OK;
183 }