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