]> git.pond.sub.org Git - empserver/blob - src/lib/commands/coll.c
667c2e1f4c9046ce70972527c8818f071fdb81de
[empserver] / src / lib / commands / coll.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 "commands.h"
38 #include "item.h"
39 #include "loan.h"
40 #include "lost.h"
41 #include "news.h"
42 #include "optlist.h"
43
44 int
45 coll(void)
46 {
47     int arg;
48     int i;
49     int val;
50     time_t now;
51     char *p;
52     struct lonstr loan;
53     struct sctstr sect;
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     pr("You are owed $%.2f on that loan.\n", owed);
81     p = getstarg(player->argp[2],
82                  "What sector do you wish to confiscate? ", buf);
83     if (!p)
84         return RET_SYN;
85     if (!check_loan_ok(&loan))
86         return RET_FAIL;
87     if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
88         return RET_SYN;
89     if (!neigh(x, y, player->cnum)) {
90         pr("You are not adjacent to %s\n", xyas(x, y, player->cnum));
91         return RET_FAIL;
92     }
93     if (sect.sct_own != loan.l_lonee) {
94         pr("%s is not owned by %s.\n",
95            xyas(x, y, player->cnum), cname(loan.l_lonee));
96         return RET_FAIL;
97     }
98     pay = dchr[sect.sct_type].d_value * (sect.sct_effic + 100.0);
99     for (i = 0; ichr[i].i_name; i++) {
100         if (ichr[i].i_value == 0 || ichr[i].i_uid == I_NONE)
101             continue;
102         val = sect.sct_item[ichr[i].i_uid];
103         pay += val * ichr[i].i_value;
104     }
105     pr("That sector (and its contents) is valued at $%.2f\n", pay);
106     if (pay > owed * 1.2) {
107         pr("That is more than is owed!\n");
108         return RET_FAIL;
109     }
110     sect.sct_item[I_MILIT] = 1; /* FIXME now where did this guy come from? */
111
112     /*
113      * Used to call takeover() here a long time ago, but that does
114      * unwanted things, like generate che.
115      */
116     sect.sct_own = player->cnum;
117
118     memset(sect.sct_dist, 0, sizeof(sect.sct_dist));
119     memset(sect.sct_del, 0, sizeof(sect.sct_del));
120     sect.sct_off = 1;
121     sect.sct_dist_x = sect.sct_x;
122     sect.sct_dist_y = sect.sct_y;
123
124     if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
125         caploss(&sect, loan.l_lonee, "that was %s's capital!\n");
126     putsect(&sect);
127     nreport(player->cnum, N_SEIZE_SECT, loan.l_lonee, 1);
128     owed = loan_owed(&loan, time(&now));
129     if (pay >= owed) {
130         loan.l_status = LS_FREE;
131         loan.l_ldur = 0;
132         nreport(loan.l_lonee, N_REPAY_LOAN, player->cnum, 1);
133         wu(0, loan.l_lonee,
134            "%s seized %s to satisfy loan #%d\n",
135            cname(player->cnum),
136            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
137         pr("That loan is now considered repaid.\n");
138     } else {
139         (void)time(&loan.l_lastpay);
140         owed -= pay;
141         loan.l_amtdue = (long)owed;
142         pay += loan.l_amtpaid;
143         loan.l_amtpaid = pay;
144         wu(0, loan.l_lonee,
145            "%s seized %s in partial payment of loan %d.\n",
146            cname(player->cnum),
147            xyas(sect.sct_x, sect.sct_y, loan.l_lonee), arg);
148         pr("You are still owed $%.2f on loan %d.\n", owed, arg);
149     }
150     putloan(arg, &loan);
151     return RET_OK;
152 }