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