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