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