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