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