2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * cons.c: Consider a loan or treaty
30 * Known contributors to this file:
43 * Things common to a loan or treaty.
46 int type; /* EF_LOAN or EF_TREATY */
48 char *name; /* "loan" or "treaty" */
49 char *Name; /* "Loan" or "Treaty" */
50 natid proposer; /* country offering */
51 natid proposee; /* country offered to */
52 natid mailee; /* who gets mail about it */
53 char op; /* 'a', 'd', or 'p' */
55 struct lonstr l; /* the loan */
56 struct trtstr t; /* the treaty */
60 static int cons_choose(struct ltcomstr *ltcp);
61 static int treaty_accept(struct ltcomstr *ltcp);
62 static int treaty_decline(struct ltcomstr *ltcp);
63 static int loan_accept(struct ltcomstr *ltcp);
64 static int loan_decline(struct ltcomstr *ltcp);
65 static int postpone(struct ltcomstr *ltcp);
66 static void accpt(struct ltcomstr *ltcp);
67 static void decline(struct ltcomstr *ltcp);
68 static void late(struct ltcomstr *ltcp);
69 static void prev_signed(struct ltcomstr *ltcp);
77 rv = cons_choose(<c);
83 rv = (ltc.type == EF_TREATY) ? treaty_accept(<c)
87 rv = (ltc.type == EF_TREATY) ? treaty_decline(<c)
94 pr("Bad operation %c from cons_choose; get help!\n", ltc.op);
102 * Choose whether we want to accept, decline, or postpone a
103 * loan or treaty. Put all the goodies in ltcp, and return
104 * RET_OK if all goes well, and anything else on error.
107 cons_choose(struct ltcomstr *ltcp)
109 static int lon_or_trt[] = { EF_LOAN, EF_TREATY, EF_BAD };
116 memset(ltcp, 0, sizeof(*ltcp));
117 if (getstarg(player->argp[1], "loan or treaty? ", buf) == 0)
119 ltcp->type = ef_byname_from(buf, lon_or_trt);
120 switch (ltcp->type) {
123 pr("Treaties are not enabled.\n");
126 ltcp->name = "treaty";
127 ltcp->Name = "Treaty";
131 pr("Loans are not enabled.\n");
138 pr("You must specify \"loan\" or \"treaty\".\n");
141 sprintf(prompt, "%s number? ", ltcp->Name);
142 if ((ltcp->num = onearg(player->argp[2], prompt)) < 0)
144 if (!ef_read(ltcp->type, ltcp->num, <cp->u) ||
145 !(ltcp->type == EF_TREATY
146 ? distrea(ltcp->num, <cp->u.t)
147 : disloan(ltcp->num, <cp->u.l))) {
148 pr("%s #%d is not being offered to you!\n", ltcp->Name, ltcp->num);
151 switch (ltcp->type) {
154 if (lp->l_status == LS_SIGNED) {
155 pr("That loan has already been accepted!\n");
158 ltcp->proposer = lp->l_loner;
159 ltcp->proposee = lp->l_lonee;
163 if (tp->trt_status == TS_SIGNED) {
164 pr("That treaty has already been accepted!\n");
167 ltcp->proposer = tp->trt_cna;
168 ltcp->proposee = tp->trt_cnb;
171 ltcp->mailee = (ltcp->proposer == player->cnum)
172 ? ltcp->proposee : ltcp->proposer;
173 p = getstarg(player->argp[3], "Accept, decline or postpone? ", buf);
174 if (!p || (*p != 'a' && *p != 'd' && *p != 'p'))
181 * Accept a loan. If the offering country has too little money,
182 * leave him $100 left and offer the rest. Return RET_OK on
183 * success, anything else on error.
186 loan_accept(struct ltcomstr *ltcp)
189 struct natstr *lender;
190 struct nstr_item nstr;
194 if (ltcp->proposee != player->cnum) {
195 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
198 if (!getloan(ltcp->num, lp)) {
199 logerror("loan_accept: can't read loan");
200 pr("can't read loan; get help!\n");
203 if (lp->l_status == LS_FREE) { /* other guy retratcted already */
207 if (lp->l_status == LS_SIGNED) { /* already signed somehow */
211 /* check to see if a loan already exists */
212 snxtitem_all(&nstr, EF_LOAN);
213 while (nxtitem(&nstr, &loan)) {
214 if (loan.l_status == LS_SIGNED && loan.l_lonee == lp->l_loner
215 && (loan.l_loner == lp->l_lonee)) {
216 pr("He already owes you money - make him repay his loan!\n");
220 lender = getnatp(ltcp->proposer);
221 if (lender->nat_money < lp->l_amtdue) { /* other guy is poor */
222 lp->l_amtdue = lender->nat_money - 100;
223 pr("%s no longer has the funds.\n", cname(ltcp->proposer));
224 if (lp->l_amtdue <= 0)
226 pr("You may borrow $%ld at the same terms.\n", lp->l_amtdue);
228 lender->nat_money -= lp->l_amtdue;
230 player->dolcost -= lp->l_amtdue;
232 (void)time(&lp->l_lastpay);
233 lp->l_duedate = lp->l_ldur * 86400 + lp->l_lastpay;
234 lp->l_status = LS_SIGNED;
235 if (!putloan(ltcp->num, lp)) {
236 pr("Problem writing lp->to disk; get help!\n");
240 pr("You are now $%ld richer (sort of).\n", lp->l_amtdue);
245 * Declne a loan. Return RET_OK on success, anything else on error.
248 loan_decline(struct ltcomstr *ltcp)
253 if (!getloan(ltcp->num, lp)) {
254 logerror("loan_decline: can't read loan");
255 pr("can't read loan; get help!\n");
258 /* loan got accepted somehow between now and last time we checked */
259 if (lp->l_status == LS_SIGNED) {
263 lp->l_status = LS_FREE;
264 if (!putloan(ltcp->num, lp)) {
265 logerror("loan_decline: can't write loan");
266 pr("can't write loan; get help!\n");
274 * Accept a treaty. Return RET_OK on success, anything else on error.
277 treaty_accept(struct ltcomstr *ltcp)
282 if (ltcp->proposee != player->cnum) {
283 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
286 if (!gettre(ltcp->num, tp)) {
287 pr("treaty_accept: can't read treaty");
288 pr("can't read treaty; get help!\n");
291 if (tp->trt_status == TS_FREE) { /* treaty offer withdrawn */
295 if (tp->trt_status == TS_SIGNED) { /* somehow got accepted */
299 tp->trt_status = TS_SIGNED;
300 if (!puttre(ltcp->num, tp)) {
301 pr("treaty_accept: can't write treaty");
302 pr("Problem saving treaty; get help!\n");
306 pr("Treaty in effect until %s", ctime(&tp->trt_exp));
311 * Decline a treaty. Return RET_OK on success, anything else on error.
314 treaty_decline(struct ltcomstr *ltcp)
319 if (!gettre(ltcp->num, tp)) {
320 logerror("treaty_decline: can't read treaty");
321 pr("can't read treaty; get help!\n");
324 /* treaty got signed somehow between now and last time we read it */
325 if (tp->trt_status == TS_SIGNED) {
329 tp->trt_status = TS_FREE;
330 if (!puttre(ltcp->num, tp)) {
331 logerror("treaty_decline: can't write treaty");
332 pr("Problem saving treaty; get help!\n");
340 * Postpone a treaty; always succeeds.
343 postpone(struct ltcomstr *ltcp)
345 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
346 if (ltcp->proposee == player->cnum)
347 wu(0, ltcp->proposer, "%s %d considered by %s\n",
348 ltcp->name, ltcp->num, cname(player->cnum));
353 * Somebody tried to accept a loan/treaty that was retracted,
354 * or to decline a loan/treaty they already signed.
357 late(struct ltcomstr *ltcp)
359 pr("Too late; that %s %s!\n", ltcp->name,
360 (ltcp->op == 'a') ? "is no longer being offered"
361 : "has already been accepted");
365 * Loan or treaty was previously signed.
368 prev_signed(struct ltcomstr *ltcp)
370 pr("%s #%d is already in effect.\n", ltcp->Name, ltcp->num);
374 * Post-processing after successful declination of loan or treaty.
375 * Notify the folks involved.
378 decline(struct ltcomstr *ltcp)
380 if (ltcp->proposee == player->cnum) {
381 wu(0, ltcp->proposer, "%s %d refused by %s\n",
382 ltcp->Name, ltcp->num, cname(player->cnum));
383 pr("%s %d refused.\n", ltcp->Name, ltcp->num);
385 wu(0, ltcp->proposee,
386 "%s offer %d retracted by %s\n",
387 ltcp->Name, ltcp->num, cname(player->cnum));
388 pr("%s offer %d retracted.\n", ltcp->Name, ltcp->num);
393 * Post-processing after successful acceptance of loan or treaty.
394 * Notify the press, and the folks involved.
395 * (Weird spelling is to avoid accept(2)).
398 accpt(struct ltcomstr *ltcp)
400 if (ltcp->type == EF_LOAN)
401 nreport(ltcp->proposer, N_MAKE_LOAN, player->cnum, 1);
403 nreport(player->cnum, N_SIGN_TRE, ltcp->mailee, 1);
404 wu(0, ltcp->mailee, "%s #%d accepted by %s\n",
405 ltcp->Name, ltcp->num, cname(player->cnum));