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