]> git.pond.sub.org Git - empserver/blob - src/lib/commands/cons.c
614ce7f9c91d177f550ddc36b17696536000cfeb
[empserver] / src / lib / commands / cons.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  *  cons.c: Consider a loan or treaty
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "loan.h"
39 #include "nat.h"
40 #include "treaty.h"
41 #include "news.h"
42 #include "xy.h"
43 #include "nsc.h"
44 #include "file.h"
45 #include "commands.h"
46 #include "optlist.h"
47
48 /*
49  * Things common to a loan or treaty.
50  */
51 struct ltcomstr {
52     int type;                   /* EF_LOAN or EF_TREATY */
53     int num;                    /* number */
54     s_char *name;               /* "loan" or "treaty" */
55     s_char *Name;               /* "Loan" or "Treaty" */
56     natid proposer;             /* country offering */
57     natid proposee;             /* country offered to */
58     natid mailee;               /* who gets mail about it */
59     s_char op;                  /* 'a', 'd', or 'p' */
60     union {
61         struct lonstr l;        /* the loan */
62         struct trtstr t;        /* the treaty */
63     } u;
64 };
65
66 static int cons_choose(struct ltcomstr *ltcp);
67 static int treaty_accept(struct ltcomstr *ltcp);
68 static int treaty_decline(struct ltcomstr *ltcp);
69 static int loan_accept(struct ltcomstr *ltcp);
70 static int loan_decline(struct ltcomstr *ltcp);
71 static int postpone(struct ltcomstr *ltcp);
72 static void accpt(struct ltcomstr *ltcp);
73 static void decline(struct ltcomstr *ltcp);
74 static void late(struct ltcomstr *ltcp);
75 static void prev_signed(struct ltcomstr *ltcp);
76
77 int
78 cons(void)
79 {
80     int rv;
81     struct ltcomstr ltc;
82
83     rv = cons_choose(&ltc);
84     if (rv != RET_OK)
85         return rv;
86
87     switch (ltc.op) {
88     case 'a':
89         rv = (ltc.type == EF_TREATY) ? treaty_accept(&ltc)
90             : loan_accept(&ltc);
91         break;
92     case 'd':
93         rv = (ltc.type == EF_TREATY) ? treaty_decline(&ltc)
94             : loan_decline(&ltc);
95         break;
96     case 'p':
97         rv = postpone(&ltc);
98         break;
99     default:
100         pr("Bad operation %c from cons_choose; get help!\n", ltc.op);
101         break;
102     };
103
104     return rv;
105 }
106
107 /*
108  * Choose whether we want to accept, decline, or postpone a
109  * loan or treaty.  Put all the goodies in ltcp, and return
110  * RET_OK if all goes well, and anything else on error.
111  */
112 static int
113 cons_choose(struct ltcomstr *ltcp)
114 {
115     static int lon_or_trt[] = { EF_LOAN, EF_TREATY, EF_BAD };
116     s_char *p;
117     struct lonstr *lp;
118     struct trtstr *tp;
119     s_char prompt[128];
120     s_char buf[1024];
121
122     memset(ltcp, 0, sizeof(*ltcp));
123     if (getstarg(player->argp[1], "loan or treaty? ", buf) == 0)
124         return RET_SYN;
125     ltcp->type = ef_byname_from(buf, lon_or_trt);
126     switch (ltcp->type) {
127     case EF_TREATY:
128         if (!opt_TREATIES) {
129             pr("Treaties are not enabled.\n");
130             return RET_FAIL;
131         }
132         ltcp->name = "treaty";
133         ltcp->Name = "Treaty";
134         break;
135     case EF_LOAN:
136         if (!opt_LOANS) {
137             pr("Loans are not enabled.\n");
138             return RET_FAIL;
139         }
140         ltcp->name = "loan";
141         ltcp->Name = "Loan";
142         break;
143     default:
144         pr("You must specify \"loan\" or \"treaty\".\n");
145         return RET_SYN;
146     }
147     sprintf(prompt, "%s number? ", ltcp->Name);
148     if ((ltcp->num = onearg(player->argp[2], prompt)) < 0)
149         return RET_SYN;
150     if (!ef_read(ltcp->type, ltcp->num, &ltcp->u) ||
151         !(ltcp->type == EF_TREATY
152           ? distrea(ltcp->num, &ltcp->u.t)
153           : disloan(ltcp->num, &ltcp->u.l))) {
154         pr("%s #%d is not being offered to you!\n", ltcp->Name, ltcp->num);
155         return RET_SYN;
156     }
157     switch (ltcp->type) {
158     case EF_LOAN:
159         lp = &ltcp->u.l;
160         if (lp->l_status == LS_SIGNED) {
161             pr("That loan has already been accepted!\n");
162             return RET_FAIL;
163         }
164         ltcp->proposer = lp->l_loner;
165         ltcp->proposee = lp->l_lonee;
166         break;
167     case EF_TREATY:
168         tp = &ltcp->u.t;
169         if (tp->trt_status == TS_SIGNED) {
170             pr("That treaty has already been accepted!\n");
171             return RET_FAIL;
172         }
173         ltcp->proposer = tp->trt_cna;
174         ltcp->proposee = tp->trt_cnb;
175         break;
176     }
177     ltcp->mailee = (ltcp->proposer == player->cnum)
178         ? ltcp->proposee : ltcp->proposer;
179     p = getstarg(player->argp[3], "Accept, decline or postpone? ", buf);
180     if (!p || (*p != 'a' && *p != 'd' && *p != 'p'))
181         return RET_SYN;
182     ltcp->op = *p;
183     return RET_OK;
184 }
185
186 /*
187  * Accept a loan.  If the offering country has too little money,
188  * leave him $100 left and offer the rest.  Return RET_OK on
189  * success, anything else on error.
190  */
191 static int
192 loan_accept(struct ltcomstr *ltcp)
193 {
194     struct lonstr *lp;
195     struct natstr *lender;
196     struct nstr_item nstr;
197     struct lonstr loan;
198
199     lp = &ltcp->u.l;
200     if (ltcp->proposee != player->cnum) {
201         pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
202         return RET_OK;
203     }
204     if (!getloan(ltcp->num, lp)) {
205         pr("loan_accept: can't read loan; get help!\n");
206         return RET_SYS;
207     }
208     if (lp->l_status == LS_FREE) {      /* other guy retratcted already */
209         late(ltcp);
210         return RET_OK;
211     }
212     if (lp->l_status == LS_SIGNED) {    /* already signed somehow */
213         prev_signed(ltcp);
214         return RET_OK;
215     }
216     /* check to see if a loan already exists */
217     snxtitem_all(&nstr, EF_LOAN);
218     while (nxtitem(&nstr, &loan)) {
219         if (loan.l_status == LS_SIGNED && loan.l_lonee == lp->l_loner
220             && (loan.l_loner == lp->l_lonee)) {
221             pr("He already owes you money - make him repay his loan!\n");
222             return RET_OK;
223         }
224     }
225     lender = getnatp(ltcp->proposer);
226     if (lender->nat_money < lp->l_amtdue) {     /* other guy is poor */
227         lp->l_amtdue = lender->nat_money - 100;
228         pr("%s no longer has the funds.\n", cname(ltcp->proposer));
229         if (lp->l_amtdue <= 0)
230             return RET_FAIL;
231         pr("You may borrow $%ld at the same terms.\n", lp->l_amtdue);
232     }
233     lender->nat_money -= lp->l_amtdue;
234     putnat(lender);
235     player->dolcost -= lp->l_amtdue;
236     lp->l_amtpaid = 0;
237     (void)time(&lp->l_lastpay);
238     lp->l_duedate = lp->l_ldur * 86400 + lp->l_lastpay;
239     lp->l_status = LS_SIGNED;
240     if (!putloan(ltcp->num, lp)) {
241         pr("Problem writing lp->to disk; get help!\n");
242         return RET_FAIL;
243     }
244     accpt(ltcp);
245     pr("You are now $%ld richer (sort of).\n", lp->l_amtdue);
246     return RET_OK;
247 }
248
249 /*
250  * Declne a loan.  Return RET_OK on success, anything else on error.
251  */
252 static int
253 loan_decline(struct ltcomstr *ltcp)
254 {
255     struct lonstr *lp;
256
257     lp = &ltcp->u.l;
258     if (!getloan(ltcp->num, lp)) {
259         pr("Decline: can't read loan; get help!\n");
260         return RET_SYS;
261     }
262     /* loan got accepted somehow between now and last time we checked */
263     if (lp->l_status == LS_SIGNED) {
264         late(ltcp);
265         return RET_OK;
266     }
267     lp->l_status = LS_FREE;
268     if (!putloan(ltcp->num, lp)) {
269         pr("loan_decline: can't write loan; get help!\n");
270         return RET_SYS;
271     }
272     decline(ltcp);
273     return RET_OK;
274 }
275
276 /*
277  * Accept a treaty.  Return RET_OK on success, anything else on error.
278  */
279 static int
280 treaty_accept(struct ltcomstr *ltcp)
281 {
282     struct trtstr *tp;
283
284     tp = &ltcp->u.t;
285     if (ltcp->proposee != player->cnum) {
286         pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
287         return RET_OK;
288     }
289     if (!gettre(ltcp->num, tp)) {
290         pr("Accept: can't read treaty; get help!\n");
291         return RET_SYS;
292     }
293     if (tp->trt_status == TS_FREE) {    /* treaty offer withdrawn */
294         late(ltcp);
295         return RET_OK;
296     }
297     if (tp->trt_status == TS_SIGNED) {  /* somehow got accepted */
298         prev_signed(ltcp);
299         return RET_OK;
300     }
301     tp->trt_status = TS_SIGNED;
302     if (!puttre(ltcp->num, tp)) {
303         pr("Problem saving treaty; get help!\n");
304         return RET_SYS;
305     }
306     accpt(ltcp);
307     pr("Treaty in effect until %s", ctime(&tp->trt_exp));
308     return RET_OK;
309 }
310
311 /*
312  * Decline a treaty.  Return RET_OK on success, anything else on error.
313  */
314 static int
315 treaty_decline(struct ltcomstr *ltcp)
316 {
317     struct trtstr *tp;
318
319     tp = &ltcp->u.t;
320     if (!gettre(ltcp->num, tp)) {
321         pr("Decline: can't read treaty; get help!\n");
322         return RET_SYS;
323     }
324     /* treaty got signed somehow between now and last time we read it */
325     if (tp->trt_status == TS_SIGNED) {
326         late(ltcp);
327         return RET_OK;
328     }
329     tp->trt_status = TS_FREE;
330     if (!puttre(ltcp->num, tp)) {
331         pr("Problem saving treaty; get help!\n");
332         return RET_SYS;
333     }
334     decline(ltcp);
335     return RET_OK;
336 }
337
338 /*
339  * Postpone a treaty; always succeeds.
340  */
341 static int
342 postpone(struct ltcomstr *ltcp)
343 {
344     pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
345     if (ltcp->proposee == player->cnum)
346         wu(0, ltcp->proposer, "%s %d considered by %s\n",
347            ltcp->name, ltcp->num, cname(player->cnum));
348     return RET_OK;
349 }
350
351 /*
352  * Somebody tried to accept a loan/treaty that was retracted,
353  * or to decline a loan/treaty they already signed.
354  */
355 static void
356 late(struct ltcomstr *ltcp)
357 {
358     pr("Too late; that %s %s!\n", ltcp->name,
359        (ltcp->op == 'a') ? "is no longer being offered"
360        : "has already been accepted");
361 }
362
363 /*
364  * Loan or treaty was previously signed.
365  */
366 static void
367 prev_signed(struct ltcomstr *ltcp)
368 {
369     pr("%s #%d is already in effect.\n", ltcp->Name, ltcp->num);
370 }
371
372 /*
373  * Post-processing after successful declination of loan or treaty.
374  * Notify the folks involved.
375  */
376 static void
377 decline(struct ltcomstr *ltcp)
378 {
379     if (ltcp->proposee == player->cnum) {
380         wu(0, ltcp->proposer, "%s %d refused by %s\n",
381            ltcp->Name, ltcp->num, cname(player->cnum));
382         pr("%s %d refused.\n", ltcp->Name, ltcp->num);
383     } else {
384         wu(0, ltcp->proposee,
385            "%s offer %d retracted by %s\n",
386            ltcp->Name, ltcp->num, cname(player->cnum));
387         pr("%s offer %d retracted.\n", ltcp->Name, ltcp->num);
388     }
389 }
390
391 /*
392  * Post-processing after successful acceptance of loan or treaty.
393  * Notify the press, and the folks involved.
394  * (Weird spelling is to avoid accept(2)).
395  */
396 static void
397 accpt(struct ltcomstr *ltcp)
398 {
399     if (ltcp->type == EF_LOAN)
400         nreport(ltcp->proposer, N_MAKE_LOAN, player->cnum, 1);
401     else
402         nreport(player->cnum, N_SIGN_TRE, ltcp->mailee, 1);
403     wu(0, ltcp->mailee, "%s #%d accepted by %s\n",
404        ltcp->Name, ltcp->num, cname(player->cnum));
405 }