]> git.pond.sub.org Git - empserver/blob - src/lib/commands/offe.c
Update copyright notice
[empserver] / src / lib / commands / offe.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  offe.c: Offer a loan or treaty
29  *
30  *  Known contributors to this file:
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "loan.h"
39 #include "optlist.h"
40 #include "treaty.h"
41
42 static int do_treaty(void);
43 static int do_loan(void);
44
45 int
46 offe(void)
47 {
48     char *cp;
49     char buf[1024];
50
51     cp = getstarg(player->argp[1], "loan or treaty? ", buf);
52     if (!cp || !*cp)
53         return RET_SYN;
54
55     switch (*cp) {
56     case 'l':
57         if (!opt_LOANS) {
58             pr("Loans are not enabled.\n");
59             return RET_FAIL;
60         }
61         return do_loan();
62     case 't':
63         if (!opt_TREATIES) {
64             pr("Treaties are not enabled.\n");
65             return RET_FAIL;
66         }
67         return do_treaty();
68     default:
69         pr("You must specify \"loan\" as there are no treaties.\n");
70         return RET_SYN;
71     }
72 }
73
74 static int
75 do_treaty(void)
76 {
77     char *cp;
78     int ourcond, theircond;
79     struct symbol *tfp;
80     struct trtstr trty;
81     struct nstr_item nstr;
82     natid recipient;
83     time_t now;
84     int j, n;
85     struct natstr *natp;
86     char prompt[128];
87     char buf[1024];
88
89     if ((n = natarg(player->argp[2], "Treaty offered to? ")) < 0)
90         return RET_SYN;
91     recipient = n;
92     if (recipient == player->cnum) {
93         pr("You can't sign a treaty with yourself!\n");
94         return RET_FAIL;
95     }
96     natp = getnatp(recipient);
97     if (player->cnum && (getrejects(player->cnum, natp) & REJ_TREA)) {
98         pr("%s is rejecting your treaties.\n", cname(recipient));
99         return RET_SYN;
100     }
101     pr("Terms for %s:\n", cname(recipient));
102     theircond = 0;
103     for (tfp = treaty_flags; tfp && tfp->name; tfp++) {
104         sprintf(prompt, "%s? ", tfp->name);
105         if (!(cp = getstring(prompt, buf)))
106             return RET_FAIL;
107         if (*cp == 'y')
108             theircond |= tfp->value;
109     }
110     pr("Terms for you:\n");
111     ourcond = 0;
112     for (tfp = treaty_flags; tfp && tfp->name; tfp++) {
113         sprintf(prompt, "%s? ", tfp->name);
114         if (!(cp = getstring(prompt, buf)))
115             return RET_FAIL;
116         if (*cp == 'y')
117             ourcond |= tfp->value;
118     }
119     if (ourcond == 0 && theircond == 0) {
120         pr("Treaties with no clauses aren't very useful, boss!\n");
121         return RET_SYN;
122     }
123     cp = getstring("Proposed treaty duration? (days) ", buf);
124     if (!cp)
125         return RET_FAIL;
126     j = atoi(cp);
127     if (j <= 0) {
128         pr("Bad treaty duration.\n");
129         return RET_SYN;
130     }
131     (void)time(&now);
132     snxtitem_all(&nstr, EF_TREATY);
133     while (nxtitem(&nstr, &trty)) {
134         if (trty.trt_status == TS_FREE) {
135             break;
136         }
137     }
138     ef_blank(EF_TREATY, nstr.cur, &trty);
139     trty.trt_acond = ourcond;
140     trty.trt_bcond = theircond;
141     trty.trt_status = TS_PROPOSED;
142     trty.trt_cna = player->cnum;
143     trty.trt_cnb = recipient;
144     trty.trt_exp = j * SECS_PER_DAY + now;
145     if (!puttre(nstr.cur, &trty)) {
146         logerror("do_treaty: can't write treaty");
147         pr("Couldn't save treaty; get help.\n");
148         return RET_FAIL;
149     }
150     wu(0, recipient, "Treaty #%d proposed to you by %s\n",
151        nstr.cur, cname(player->cnum));
152     pr("You have proposed treaty #%d\n", nstr.cur);
153     return RET_OK;
154 }
155
156 static int
157 do_loan(void)
158 {
159     int amt, irate, dur, maxloan;
160     struct nstr_item nstr;
161     struct natstr *natp;
162     struct lonstr loan;
163     natid recipient;
164     int n;
165     char prompt[128];
166
167     if ((n = natarg(player->argp[2], "Lend to? ")) < 0)
168         return RET_SYN;
169     recipient = n;
170     if (recipient == player->cnum) {
171         pr("You can't loan yourself money!\n");
172         return RET_FAIL;
173     }
174     natp = getnatp(recipient);
175     if (player->cnum && (getrejects(player->cnum, natp) & REJ_LOAN)) {
176         pr("%s is rejecting your loans.\n", cname(recipient));
177         return RET_SYN;
178     }
179     natp = getnatp(player->cnum);
180     if (natp->nat_money + 100 > MAXLOAN)
181         maxloan = MAXLOAN;
182     else
183         maxloan = natp->nat_money - 100;
184     if (maxloan < 0) {
185         pr("You don't have enough money to loan!\n");
186         return RET_FAIL;
187     }
188     sprintf(prompt, "Size of loan for country #%d? (max %d) ",
189             recipient, maxloan);
190     amt = onearg(player->argp[3], prompt);
191     if (amt <= 0)
192         return RET_FAIL;
193     if (amt > MAXLOAN) {
194         pr("You can only loan $%d at a time.\n", MAXLOAN);
195         return RET_FAIL;
196     }
197     if (amt > maxloan) {
198         pr("You can't afford that much.\n");
199         return RET_FAIL;
200     }
201     dur = onearg(player->argp[4], "Duration? (days, max 7) ");
202     if (dur <= 0)
203         return RET_FAIL;
204     if (dur > 7)
205         return RET_FAIL;
206     irate = onearg(player->argp[5], "Interest rate? (from 5 to 25%) ");
207     if (irate > 25)
208         return RET_FAIL;
209     if (irate < 5)
210         return RET_FAIL;
211     snxtitem_all(&nstr, EF_LOAN);
212     while (nxtitem(&nstr, &loan)) {
213         if ((loan.l_status == LS_SIGNED) && (loan.l_lonee == player->cnum)
214             && (loan.l_loner == recipient)) {
215             pr("You already owe HIM money - how about repaying your loan?\n");
216             return RET_FAIL;
217         }
218     }
219     snxtitem_all(&nstr, EF_LOAN);
220     while (nxtitem(&nstr, &loan)) {
221         if (loan.l_status == LS_FREE)
222             break;
223     }
224     ef_blank(EF_LOAN, nstr.cur, &loan);
225     loan.l_loner = player->cnum;
226     loan.l_lonee = recipient;
227     loan.l_status = LS_PROPOSED;
228     loan.l_irate = MIN(irate, 127);
229     loan.l_ldur = MIN(dur, 127);
230     loan.l_amtpaid = 0;
231     loan.l_amtdue = amt;
232     (void)time(&loan.l_lastpay);
233     loan.l_duedate = loan.l_ldur * SECS_PER_DAY + loan.l_lastpay;
234     if (!putloan(nstr.cur, &loan)) {
235         logerror("do_loan: can't save loan");
236         pr("Couldn't save loan; get help!\n");
237         return RET_FAIL;
238     }
239     pr("You have offered loan %d\n", nstr.cur);
240     wu(0, recipient, "Country #%d has offered you a loan (#%d)\n",
241        player->cnum, nstr.cur);
242     return RET_OK;
243 }