]> git.pond.sub.org Git - empserver/blob - src/lib/commands/offe.c
ee94e3134a45719ad3abf270f9a033e3b9d227bd
[empserver] / src / lib / commands / offe.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, 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     if (!nat_accepts(recipient, player->cnum, REJ_LOAN)) {
81         pr("%s is rejecting your loans.\n", cname(recipient));
82         return RET_SYN;
83     }
84     natp = getnatp(player->cnum);
85     if (natp->nat_money + 100 > MAXLOAN)
86         maxloan = MAXLOAN;
87     else
88         maxloan = natp->nat_money - 100;
89     if (maxloan < 0) {
90         pr("You don't have enough money to loan!\n");
91         return RET_FAIL;
92     }
93     sprintf(prompt, "Size of loan for country #%d? (max %d) ",
94             recipient, maxloan);
95     amt = onearg(player->argp[3], prompt);
96     if (amt <= 0)
97         return RET_FAIL;
98     if (amt > MAXLOAN) {
99         pr("You can only loan $%d at a time.\n", MAXLOAN);
100         return RET_FAIL;
101     }
102     if (amt > maxloan) {
103         pr("You can't afford that much.\n");
104         return RET_FAIL;
105     }
106     dur = onearg(player->argp[4], "Duration? (days, max 7) ");
107     if (dur <= 0)
108         return RET_FAIL;
109     if (dur > 7)
110         return RET_FAIL;
111     irate = onearg(player->argp[5], "Interest rate? (from 5 to 25%) ");
112     if (irate > 25)
113         return RET_FAIL;
114     if (irate < 5)
115         return RET_FAIL;
116     snxtitem_all(&nstr, EF_LOAN);
117     while (nxtitem(&nstr, &loan)) {
118         if ((loan.l_status == LS_SIGNED) && (loan.l_lonee == player->cnum)
119             && (loan.l_loner == recipient)) {
120             pr("You already owe HIM money - how about repaying your loan?\n");
121             return RET_FAIL;
122         }
123     }
124     snxtitem_all(&nstr, EF_LOAN);
125     while (nxtitem(&nstr, &loan)) {
126         if (loan.l_status == LS_FREE)
127             break;
128     }
129     ef_blank(EF_LOAN, nstr.cur, &loan);
130     loan.l_loner = player->cnum;
131     loan.l_lonee = recipient;
132     loan.l_status = LS_PROPOSED;
133     loan.l_irate = MIN(irate, 127);
134     loan.l_ldur = MIN(dur, 127);
135     loan.l_amtpaid = 0;
136     loan.l_amtdue = amt;
137     (void)time(&loan.l_lastpay);
138     loan.l_duedate = loan.l_ldur * SECS_PER_DAY + loan.l_lastpay;
139     if (!putloan(nstr.cur, &loan)) {
140         logerror("do_loan: can't save loan");
141         pr("Couldn't save loan; get help!\n");
142         return RET_FAIL;
143     }
144     pr("You have offered loan %d\n", nstr.cur);
145     wu(0, recipient, "Country #%d has offered you a loan (#%d)\n",
146        player->cnum, nstr.cur);
147     return RET_OK;
148 }