]> git.pond.sub.org Git - empserver/blob - src/lib/commands/cons.c
27725ce344f8cc19ad7ce7b1076b345da7d0fceb
[empserver] / src / lib / commands / cons.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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         pr("Bad operation %c from cons_choose; get help!\n", ltc.op);
94         break;
95     };
96
97     return rv;
98 }
99
100 /*
101  * Choose whether we want to accept, decline, or postpone a
102  * loan or treaty.  Put all the goodies in ltcp, and return
103  * RET_OK if all goes well, and anything else on error.
104  */
105 static int
106 cons_choose(struct ltcomstr *ltcp)
107 {
108     static int lon_or_trt[] = { EF_LOAN, EF_TREATY, EF_BAD };
109     char *p;
110     struct lonstr *lp;
111     struct trtstr *tp;
112     char prompt[128];
113     char buf[1024];
114
115     memset(ltcp, 0, sizeof(*ltcp));
116     if (!getstarg(player->argp[1], "loan or treaty? ", buf))
117         return RET_SYN;
118     ltcp->type = ef_byname_from(buf, lon_or_trt);
119     switch (ltcp->type) {
120     case EF_TREATY:
121         if (!opt_TREATIES) {
122             pr("Treaties are not enabled.\n");
123             return RET_FAIL;
124         }
125         ltcp->name = "treaty";
126         ltcp->Name = "Treaty";
127         break;
128     case EF_LOAN:
129         if (!opt_LOANS) {
130             pr("Loans are not enabled.\n");
131             return RET_FAIL;
132         }
133         ltcp->name = "loan";
134         ltcp->Name = "Loan";
135         break;
136     default:
137         pr("You must specify \"loan\" or \"treaty\".\n");
138         return RET_SYN;
139     }
140     sprintf(prompt, "%s number? ", ltcp->Name);
141     if ((ltcp->num = onearg(player->argp[2], prompt)) < 0)
142         return RET_SYN;
143     if (!ef_read(ltcp->type, ltcp->num, &ltcp->u) ||
144         !(ltcp->type == EF_TREATY
145           ? distrea(ltcp->num, &ltcp->u.t)
146           : disloan(ltcp->num, &ltcp->u.l))) {
147         pr("%s #%d is not being offered to you!\n", ltcp->Name, ltcp->num);
148         return RET_SYN;
149     }
150     switch (ltcp->type) {
151     case EF_LOAN:
152         lp = &ltcp->u.l;
153         if (lp->l_status == LS_SIGNED) {
154             pr("That loan has already been accepted!\n");
155             return RET_FAIL;
156         }
157         ltcp->proposer = lp->l_loner;
158         ltcp->proposee = lp->l_lonee;
159         break;
160     case EF_TREATY:
161         tp = &ltcp->u.t;
162         if (tp->trt_status == TS_SIGNED) {
163             pr("That treaty has already been accepted!\n");
164             return RET_FAIL;
165         }
166         ltcp->proposer = tp->trt_cna;
167         ltcp->proposee = tp->trt_cnb;
168         break;
169     }
170     ltcp->mailee = (ltcp->proposer == player->cnum)
171         ? ltcp->proposee : ltcp->proposer;
172     p = getstarg(player->argp[3], "Accept, decline or postpone? ", buf);
173     if (!p || (*p != 'a' && *p != 'd' && *p != 'p'))
174         return RET_SYN;
175     ltcp->op = *p;
176     return RET_OK;
177 }
178
179 /*
180  * Accept a loan.  If the offering country has too little money,
181  * leave him $100 left and offer the rest.  Return RET_OK on
182  * success, anything else on error.
183  */
184 static int
185 loan_accept(struct ltcomstr *ltcp)
186 {
187     struct lonstr *lp;
188     struct natstr *lender;
189     struct nstr_item nstr;
190     struct lonstr loan;
191
192     lp = &ltcp->u.l;
193     if (ltcp->proposee != player->cnum) {
194         pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
195         return RET_OK;
196     }
197     if (!getloan(ltcp->num, lp)) {
198         logerror("loan_accept: can't read loan");
199         pr("can't read loan; get help!\n");
200         return RET_FAIL;
201     }
202     if (lp->l_status == LS_FREE) {      /* other guy retratcted already */
203         late(ltcp);
204         return RET_OK;
205     }
206     if (lp->l_status == LS_SIGNED) {    /* already signed somehow */
207         prev_signed(ltcp);
208         return RET_OK;
209     }
210     /* check to see if a loan already exists */
211     snxtitem_all(&nstr, EF_LOAN);
212     while (nxtitem(&nstr, &loan)) {
213         if (loan.l_status == LS_SIGNED && loan.l_lonee == lp->l_loner
214             && (loan.l_loner == lp->l_lonee)) {
215             pr("He already owes you money - make him repay his loan!\n");
216             return RET_OK;
217         }
218     }
219     lender = getnatp(ltcp->proposer);
220     if (lender->nat_money < lp->l_amtdue) {     /* other guy is poor */
221         lp->l_amtdue = lender->nat_money - 100;
222         pr("%s no longer has the funds.\n", cname(ltcp->proposer));
223         if (lp->l_amtdue <= 0)
224             return RET_FAIL;
225         pr("You may borrow $%d at the same terms.\n", lp->l_amtdue);
226     }
227     lender->nat_money -= lp->l_amtdue;
228     putnat(lender);
229     player->dolcost -= lp->l_amtdue;
230     lp->l_amtpaid = 0;
231     (void)time(&lp->l_lastpay);
232     lp->l_duedate = lp->l_ldur * SECS_PER_DAY + lp->l_lastpay;
233     lp->l_status = LS_SIGNED;
234     if (!putloan(ltcp->num, lp)) {
235         pr("Problem writing lp->to disk; get help!\n");
236         return RET_FAIL;
237     }
238     accpt(ltcp);
239     pr("You are now $%d richer (sort of).\n", lp->l_amtdue);
240     return RET_OK;
241 }
242
243 /*
244  * Declne a loan.  Return RET_OK on success, anything else on error.
245  */
246 static int
247 loan_decline(struct ltcomstr *ltcp)
248 {
249     struct lonstr *lp;
250
251     lp = &ltcp->u.l;
252     if (!getloan(ltcp->num, lp)) {
253         logerror("loan_decline: can't read loan");
254         pr("can't read loan; get help!\n");
255         return RET_FAIL;
256     }
257     /* loan got accepted somehow between now and last time we checked */
258     if (lp->l_status == LS_SIGNED) {
259         late(ltcp);
260         return RET_OK;
261     }
262     lp->l_status = LS_FREE;
263     if (!putloan(ltcp->num, lp)) {
264         logerror("loan_decline: can't write loan");
265         pr("can't write loan; get help!\n");
266         return RET_FAIL;
267     }
268     decline(ltcp);
269     return RET_OK;
270 }
271
272 /*
273  * Accept a treaty.  Return RET_OK on success, anything else on error.
274  */
275 static int
276 treaty_accept(struct ltcomstr *ltcp)
277 {
278     struct trtstr *tp;
279
280     tp = &ltcp->u.t;
281     if (ltcp->proposee != player->cnum) {
282         pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
283         return RET_OK;
284     }
285     if (!gettre(ltcp->num, tp)) {
286         pr("treaty_accept: can't read treaty");
287         pr("can't read treaty; get help!\n");
288         return RET_FAIL;
289     }
290     if (tp->trt_status == TS_FREE) {    /* treaty offer withdrawn */
291         late(ltcp);
292         return RET_OK;
293     }
294     if (tp->trt_status == TS_SIGNED) {  /* somehow got accepted */
295         prev_signed(ltcp);
296         return RET_OK;
297     }
298     tp->trt_status = TS_SIGNED;
299     if (!puttre(ltcp->num, tp)) {
300         pr("treaty_accept: can't write treaty");
301         pr("Problem saving treaty; get help!\n");
302         return RET_FAIL;
303     }
304     accpt(ltcp);
305     pr("Treaty in effect until %s", ctime(&tp->trt_exp));
306     return RET_OK;
307 }
308
309 /*
310  * Decline a treaty.  Return RET_OK on success, anything else on error.
311  */
312 static int
313 treaty_decline(struct ltcomstr *ltcp)
314 {
315     struct trtstr *tp;
316
317     tp = &ltcp->u.t;
318     if (!gettre(ltcp->num, tp)) {
319         logerror("treaty_decline: can't read treaty");
320         pr("can't read treaty; get help!\n");
321         return RET_FAIL;
322     }
323     /* treaty got signed somehow between now and last time we read it */
324     if (tp->trt_status == TS_SIGNED) {
325         late(ltcp);
326         return RET_OK;
327     }
328     tp->trt_status = TS_FREE;
329     if (!puttre(ltcp->num, tp)) {
330         logerror("treaty_decline: can't write treaty");
331         pr("Problem saving treaty; get help!\n");
332         return RET_FAIL;
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 }