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