]> git.pond.sub.org Git - empserver/blob - src/lib/commands/offe.c
reject: Replace getrejects() by nat_accepts()
[empserver] / src / lib / commands / offe.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  *  offe.c: Offer a loan
28  *
29  *  Known contributors to this file:
30  *     Pat Loney, 1992
31  *     Steve McClure, 1996
32  *     Markus Armbruster, 2005-2016
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "loan.h"
39 #include "optlist.h"
40
41 static int do_loan(void);
42
43 int
44 offe(void)
45 {
46     char *cp;
47
48     cp = player->argp[1] ? player->argp[1] : "loan";
49     switch (*cp) {
50     case 'l':
51         if (!opt_LOANS) {
52             pr("Loans are not enabled.\n");
53             return RET_FAIL;
54         }
55         return do_loan();
56     default:
57         pr("You must specify \"loan\".\n");
58         return RET_SYN;
59     }
60 }
61
62 static int
63 do_loan(void)
64 {
65     int amt, irate, dur, maxloan;
66     struct nstr_item nstr;
67     struct natstr *natp;
68     struct lonstr loan;
69     natid recipient;
70     int n;
71     char prompt[128];
72
73     if ((n = natarg(player->argp[2], "Lend to? ")) < 0)
74         return RET_SYN;
75     recipient = n;
76     if (recipient == player->cnum) {
77         pr("You can't loan yourself money!\n");
78         return RET_FAIL;
79     }
80     natp = getnatp(recipient);
81     if (!nat_accepts(natp, player->cnum, REJ_LOAN)) {
82         pr("%s is rejecting your loans.\n", cname(recipient));
83         return RET_SYN;
84     }
85     natp = getnatp(player->cnum);
86     if (natp->nat_money + 100 > MAXLOAN)
87         maxloan = MAXLOAN;
88     else
89         maxloan = natp->nat_money - 100;
90     if (maxloan < 0) {
91         pr("You don't have enough money to loan!\n");
92         return RET_FAIL;
93     }
94     sprintf(prompt, "Size of loan for country #%d? (max %d) ",
95             recipient, maxloan);
96     amt = onearg(player->argp[3], prompt);
97     if (amt <= 0)
98         return RET_FAIL;
99     if (amt > MAXLOAN) {
100         pr("You can only loan $%d at a time.\n", MAXLOAN);
101         return RET_FAIL;
102     }
103     if (amt > maxloan) {
104         pr("You can't afford that much.\n");
105         return RET_FAIL;
106     }
107     dur = onearg(player->argp[4], "Duration? (days, max 7) ");
108     if (dur <= 0)
109         return RET_FAIL;
110     if (dur > 7)
111         return RET_FAIL;
112     irate = onearg(player->argp[5], "Interest rate? (from 5 to 25%) ");
113     if (irate > 25)
114         return RET_FAIL;
115     if (irate < 5)
116         return RET_FAIL;
117     snxtitem_all(&nstr, EF_LOAN);
118     while (nxtitem(&nstr, &loan)) {
119         if ((loan.l_status == LS_SIGNED) && (loan.l_lonee == player->cnum)
120             && (loan.l_loner == recipient)) {
121             pr("You already owe HIM money - how about repaying your loan?\n");
122             return RET_FAIL;
123         }
124     }
125     snxtitem_all(&nstr, EF_LOAN);
126     while (nxtitem(&nstr, &loan)) {
127         if (loan.l_status == LS_FREE)
128             break;
129     }
130     ef_blank(EF_LOAN, nstr.cur, &loan);
131     loan.l_loner = player->cnum;
132     loan.l_lonee = recipient;
133     loan.l_status = LS_PROPOSED;
134     loan.l_irate = MIN(irate, 127);
135     loan.l_ldur = MIN(dur, 127);
136     loan.l_amtpaid = 0;
137     loan.l_amtdue = amt;
138     (void)time(&loan.l_lastpay);
139     loan.l_duedate = loan.l_ldur * SECS_PER_DAY + loan.l_lastpay;
140     if (!putloan(nstr.cur, &loan)) {
141         logerror("do_loan: can't save loan");
142         pr("Couldn't save loan; get help!\n");
143         return RET_FAIL;
144     }
145     pr("You have offered loan %d\n", nstr.cur);
146     wu(0, recipient, "Country #%d has offered you a loan (#%d)\n",
147        player->cnum, nstr.cur);
148     return RET_OK;
149 }