]> git.pond.sub.org Git - empserver/blob - src/lib/commands/coll.c
commands: Rename the command functions
[empserver] / src / lib / commands / coll.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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 static double appraise_sect(struct sctstr *);
44
45 int
46 c_collect(void)
47 {
48     int arg;
49     time_t now;
50     char *p;
51     struct lonstr loan;
52     struct sctstr sect;
53     struct natstr *lonee_np;
54     coord x, y;
55     double owed;
56     double pay;
57     char buf[1024];
58
59     if (!opt_LOANS) {
60         pr("Loans are not enabled.\n");
61         return RET_FAIL;
62     }
63     if ((arg = onearg(player->argp[1], "Collect on loan # ")) < 0)
64         return RET_SYN;
65     /* Check if it's a valid loan.  That means, is it a valid loan,
66        owed to this player, with a valid duration and it's been signed. */
67     if (!getloan(arg, &loan) || (loan.l_loner != player->cnum) ||
68         (loan.l_ldur == 0) || (loan.l_status != LS_SIGNED)) {
69         pr("You aren't owed anything on that loan...\n");
70         return RET_FAIL;
71     }
72     /* If we got here, we check to see if it's been defaulted on.  We
73        already know it's owed to this player. */
74     owed = loan_owed(&loan, time(&now));
75     if (now <= loan.l_duedate) {
76         pr("There has been no default on loan %d\n", arg);
77         return RET_FAIL;
78     }
79
80     lonee_np = getnatp(loan.l_lonee);
81     pr("You are owed $%.2f on that loan.\n", owed);
82     p = getstarg(player->argp[2],
83                  "What sector do you wish to confiscate? ", buf);
84     if (!p)
85         return RET_SYN;
86     if (!check_loan_ok(&loan))
87         return RET_FAIL;
88     if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
89         return RET_SYN;
90     if (!neigh(x, y, player->cnum)) {
91         pr("You are not adjacent to %s\n", xyas(x, y, player->cnum));
92         return RET_FAIL;
93     }
94     if (sect.sct_own != loan.l_lonee) {
95         pr("%s is not owned by %s.\n",
96            xyas(x, y, player->cnum), cname(loan.l_lonee));
97         return RET_FAIL;
98     }
99     pay = appraise_sect(&sect);
100     if (pay > owed * 1.2) {
101         pr("That sector (and its contents) is valued at more than %.2f.\n",
102            owed);
103         return RET_FAIL;
104     }
105     if (!influx(lonee_np)
106         && sect.sct_x == lonee_np->nat_xcap
107         && sect.sct_y == lonee_np->nat_ycap) {
108         pr("%s's capital cannot be confiscated.\n", cname(loan.l_lonee));
109         return RET_FAIL;
110     }
111     pr("That sector (and its contents) is valued at $%.2f\n", pay);
112
113     sect.sct_item[I_MILIT] = 1; /* FIXME now where did this guy come from? */
114
115     /*
116      * Used to call takeover() here a long time ago, but that does
117      * unwanted things, like generate che.
118      */
119     sect.sct_own = player->cnum;
120     memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
121     memset(sect.sct_del, 0, sizeof(sect.sct_del));
122     sect.sct_off = 1;
123     sect.sct_dist_x = sect.sct_x;
124     sect.sct_dist_y = sect.sct_y;
125     putsect(&sect);
126     nreport(player->cnum, N_SEIZE_SECT, loan.l_lonee, 1);
127     owed = loan_owed(&loan, time(&now));
128     if (pay >= owed) {
129         loan.l_status = LS_FREE;
130         loan.l_ldur = 0;
131         nreport(loan.l_lonee, N_REPAY_LOAN, player->cnum, 1);
132         wu(0, loan.l_lonee,
133            "%s seized %s to satisfy loan #%d\n",
134            cname(player->cnum),
135            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
136         pr("That loan is now considered repaid.\n");
137     } else {
138         (void)time(&loan.l_lastpay);
139         owed -= pay;
140         loan.l_amtdue = (int)owed;
141         pay += loan.l_amtpaid;
142         loan.l_amtpaid = (int)pay;
143         wu(0, loan.l_lonee,
144            "%s seized %s in partial payment of loan %d.\n",
145            cname(player->cnum),
146            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
147         pr("You are still owed $%.2f on loan %d.\n", owed, arg);
148     }
149     putloan(arg, &loan);
150     return RET_OK;
151 }
152
153 static double
154 appraise_items(short item[])
155 {
156     double total, val;
157     int i;
158
159     total = 0.0;
160     for (i = I_NONE + 1; i <= I_MAX; i++) {
161         val = ichr[i].i_power / 10.0;
162         if (i == I_MILIT || i == I_CIVIL || i == I_UW)
163             val /= 5.0;         /* collect-specific fudge factor */
164         total += item[i] * val;
165     }
166     return total;
167 }
168
169 static double
170 appraise_sect(struct sctstr *sp)
171 {
172     struct dchrstr *dcp = &dchr[sp->sct_type];
173     double bld_val = appraise_items(dcp->d_mat) + dcp->d_cost;
174
175     return bld_val * sp->sct_effic / 100.0 + dcp->d_maxpop
176         + appraise_items(sp->sct_item);
177 }