]> git.pond.sub.org Git - empserver/blob - src/lib/commands/coll.c
collect: Don't disclose sector value when it exceeds loan
[empserver] / src / lib / commands / coll.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  coll.c: Collect on a loan
28  *
29  *  Known contributors to this file:
30  *     Pat Loney, 1992
31  *     Steve McClure, 1996-2000
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "item.h"
39 #include "loan.h"
40 #include "news.h"
41 #include "optlist.h"
42
43 int
44 coll(void)
45 {
46     int arg;
47     int i;
48     int val;
49     time_t now;
50     char *p;
51     struct lonstr loan;
52     struct sctstr sect;
53     coord x, y;
54     double owed;
55     double pay;
56     char buf[1024];
57
58     if (!opt_LOANS) {
59         pr("Loans are not enabled.\n");
60         return RET_FAIL;
61     }
62     if ((arg = onearg(player->argp[1], "Collect on loan # ")) < 0)
63         return RET_SYN;
64     /* Check if it's a valid loan.  That means, is it a valid loan,
65        owed to this player, with a valid duration and it's been signed. */
66     if (!getloan(arg, &loan) || (loan.l_loner != player->cnum) ||
67         (loan.l_ldur == 0) || (loan.l_status != LS_SIGNED)) {
68         pr("You aren't owed anything on that loan...\n");
69         return RET_FAIL;
70     }
71     /* If we got here, we check to see if it's been defaulted on.  We
72        already know it's owed to this player. */
73     owed = loan_owed(&loan, time(&now));
74     if (now <= loan.l_duedate) {
75         pr("There has been no default on loan %d\n", arg);
76         return RET_FAIL;
77     }
78
79     pr("You are owed $%.2f on that loan.\n", owed);
80     p = getstarg(player->argp[2],
81                  "What sector do you wish to confiscate? ", buf);
82     if (!p)
83         return RET_SYN;
84     if (!check_loan_ok(&loan))
85         return RET_FAIL;
86     if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
87         return RET_SYN;
88     if (!neigh(x, y, player->cnum)) {
89         pr("You are not adjacent to %s\n", xyas(x, y, player->cnum));
90         return RET_FAIL;
91     }
92     if (sect.sct_own != loan.l_lonee) {
93         pr("%s is not owned by %s.\n",
94            xyas(x, y, player->cnum), cname(loan.l_lonee));
95         return RET_FAIL;
96     }
97     pay = dchr[sect.sct_type].d_value * (sect.sct_effic + 100.0);
98     for (i = 0; ichr[i].i_name; i++) {
99         if (ichr[i].i_value == 0 || ichr[i].i_uid == I_NONE)
100             continue;
101         val = sect.sct_item[ichr[i].i_uid];
102         pay += val * ichr[i].i_value;
103     }
104     if (pay > owed * 1.2) {
105         pr("That sector (and its contents) is valued at more than %.2f.\n",
106            owed);
107         return RET_FAIL;
108     }
109     pr("That sector (and its contents) is valued at $%.2f\n", pay);
110
111     sect.sct_item[I_MILIT] = 1; /* FIXME now where did this guy come from? */
112
113     /*
114      * Used to call takeover() here a long time ago, but that does
115      * unwanted things, like generate che.
116      */
117     sect.sct_own = player->cnum;
118
119     memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
120     memset(sect.sct_del, 0, sizeof(sect.sct_del));
121     sect.sct_off = 1;
122     sect.sct_dist_x = sect.sct_x;
123     sect.sct_dist_y = sect.sct_y;
124
125     if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
126         caploss(&sect, loan.l_lonee, "that was %s's capital!\n");
127     putsect(&sect);
128     nreport(player->cnum, N_SEIZE_SECT, loan.l_lonee, 1);
129     owed = loan_owed(&loan, time(&now));
130     if (pay >= owed) {
131         loan.l_status = LS_FREE;
132         loan.l_ldur = 0;
133         nreport(loan.l_lonee, N_REPAY_LOAN, player->cnum, 1);
134         wu(0, loan.l_lonee,
135            "%s seized %s to satisfy loan #%d\n",
136            cname(player->cnum),
137            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
138         pr("That loan is now considered repaid.\n");
139     } else {
140         (void)time(&loan.l_lastpay);
141         owed -= pay;
142         loan.l_amtdue = (int)owed;
143         pay += loan.l_amtpaid;
144         loan.l_amtpaid = (int)pay;
145         wu(0, loan.l_lonee,
146            "%s seized %s in partial payment of loan %d.\n",
147            cname(player->cnum),
148            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
149         pr("You are still owed $%.2f on loan %d.\n", owed, arg);
150     }
151     putloan(arg, &loan);
152     return RET_OK;
153 }