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
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.
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, see <http://www.gnu.org/licenses/>.
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.
27 * cons.c: Consider a loan or treaty
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2014
42 * Things common to a loan or treaty.
45 int type; /* EF_LOAN or EF_TREATY */
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' */
54 struct lonstr l; /* the loan */
55 struct trtstr t; /* the treaty */
59 static int cons_choose(struct ltcomstr *ltcp);
60 static int cons_display(struct ltcomstr *ltcp);
61 static int cons_accept(struct ltcomstr *ltcp);
62 static int cons_decline(struct ltcomstr *ltcp);
63 static int cons_postpone(struct ltcomstr *ltcp);
64 static int treaty_accept(struct ltcomstr *ltcp);
65 static int treaty_decline(struct ltcomstr *ltcp);
66 static int loan_accept(struct ltcomstr *ltcp);
67 static int loan_decline(struct ltcomstr *ltcp);
68 static void accpt(struct ltcomstr *ltcp);
69 static void decline(struct ltcomstr *ltcp);
70 static void late(struct ltcomstr *ltcp);
71 static void prev_signed(struct ltcomstr *ltcp);
79 rv = cons_choose(<c);
85 return cons_accept(<c);
87 return cons_decline(<c);
89 return cons_postpone(<c);
97 * Choose whether we want to accept, decline, or postpone a
98 * loan or treaty. Put all the goodies in ltcp, and return
99 * RET_OK if all goes well, and anything else on error.
102 cons_choose(struct ltcomstr *ltcp)
104 static int lon_or_trt[] = { EF_LOAN, EF_TREATY, EF_BAD };
111 memset(ltcp, 0, sizeof(*ltcp));
112 if (!getstarg(player->argp[1], "loan or treaty? ", buf))
114 ltcp->type = ef_byname_from(buf, lon_or_trt);
115 switch (ltcp->type) {
118 pr("Treaties are not enabled.\n");
121 ltcp->name = "treaty";
122 ltcp->Name = "Treaty";
126 pr("Loans are not enabled.\n");
133 pr("You must specify \"loan\" or \"treaty\".\n");
136 sprintf(prompt, "%s number? ", ltcp->Name);
137 if ((ltcp->num = onearg(player->argp[2], prompt)) < 0)
139 if (!ef_read(ltcp->type, ltcp->num, <cp->u) || !cons_display(ltcp)) {
140 pr("%s #%d is not being offered to you!\n", ltcp->Name, ltcp->num);
143 switch (ltcp->type) {
146 if (lp->l_status == LS_SIGNED) {
147 pr("That loan has already been accepted!\n");
150 ltcp->proposer = lp->l_loner;
151 ltcp->proposee = lp->l_lonee;
155 if (tp->trt_status == TS_SIGNED) {
156 pr("That treaty has already been accepted!\n");
159 ltcp->proposer = tp->trt_cna;
160 ltcp->proposee = tp->trt_cnb;
163 ltcp->mailee = (ltcp->proposer == player->cnum)
164 ? ltcp->proposee : ltcp->proposer;
165 p = getstarg(player->argp[3], "Accept, decline or postpone? ", buf);
166 if (!p || (*p != 'a' && *p != 'd' && *p != 'p'))
173 cons_display(struct ltcomstr *ltcp)
175 switch (ltcp->type) {
177 return disloan(ltcp->num, <cp->u.l);
179 return distrea(ltcp->num, <cp->u.t);
187 cons_accept(struct ltcomstr *ltcp)
189 switch (ltcp->type) {
191 return loan_accept(ltcp);
193 return treaty_accept(ltcp);
201 cons_decline(struct ltcomstr *ltcp)
203 switch (ltcp->type) {
205 return loan_decline(ltcp);
207 return treaty_decline(ltcp);
215 * Postpone a treaty; always succeeds.
218 cons_postpone(struct ltcomstr *ltcp)
220 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
221 if (ltcp->proposee == player->cnum)
222 wu(0, ltcp->proposer, "%s %d considered by %s\n",
223 ltcp->name, ltcp->num, cname(player->cnum));
228 * Accept a loan. If the offering country has too little money,
229 * leave him $100 left and offer the rest. Return RET_OK on
230 * success, anything else on error.
233 loan_accept(struct ltcomstr *ltcp)
236 struct natstr *lender;
237 struct nstr_item nstr;
241 if (ltcp->proposee != player->cnum) {
242 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
245 if (!getloan(ltcp->num, lp)) {
246 logerror("loan_accept: can't read loan");
247 pr("can't read loan; get help!\n");
250 if (lp->l_status == LS_FREE) { /* other guy retratcted already */
254 if (lp->l_status == LS_SIGNED) { /* already signed somehow */
258 /* check to see if a loan already exists */
259 snxtitem_all(&nstr, EF_LOAN);
260 while (nxtitem(&nstr, &loan)) {
261 if (loan.l_status == LS_SIGNED && loan.l_lonee == lp->l_loner
262 && (loan.l_loner == lp->l_lonee)) {
263 pr("He already owes you money - make him repay his loan!\n");
267 lender = getnatp(ltcp->proposer);
268 if (lender->nat_money < lp->l_amtdue) { /* other guy is poor */
269 lp->l_amtdue = lender->nat_money - 100;
270 pr("%s no longer has the funds.\n", cname(ltcp->proposer));
271 if (lp->l_amtdue <= 0)
273 pr("You may borrow $%d at the same terms.\n", lp->l_amtdue);
275 lender->nat_money -= lp->l_amtdue;
277 player->dolcost -= lp->l_amtdue;
279 (void)time(&lp->l_lastpay);
280 lp->l_duedate = lp->l_ldur * SECS_PER_DAY + lp->l_lastpay;
281 lp->l_status = LS_SIGNED;
282 if (!putloan(ltcp->num, lp)) {
283 pr("Problem writing lp->to disk; get help!\n");
287 pr("You are now $%d richer (sort of).\n", lp->l_amtdue);
292 * Declne a loan. Return RET_OK on success, anything else on error.
295 loan_decline(struct ltcomstr *ltcp)
300 if (!getloan(ltcp->num, lp)) {
301 logerror("loan_decline: can't read loan");
302 pr("can't read loan; get help!\n");
305 /* loan got accepted somehow between now and last time we checked */
306 if (lp->l_status == LS_SIGNED) {
310 lp->l_status = LS_FREE;
311 if (!putloan(ltcp->num, lp)) {
312 logerror("loan_decline: can't write loan");
313 pr("can't write loan; get help!\n");
321 * Accept a treaty. Return RET_OK on success, anything else on error.
324 treaty_accept(struct ltcomstr *ltcp)
329 if (ltcp->proposee != player->cnum) {
330 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
333 if (!gettre(ltcp->num, tp)) {
334 pr("treaty_accept: can't read treaty");
335 pr("can't read treaty; get help!\n");
338 if (tp->trt_status == TS_FREE) { /* treaty offer withdrawn */
342 if (tp->trt_status == TS_SIGNED) { /* somehow got accepted */
346 tp->trt_status = TS_SIGNED;
347 if (!puttre(ltcp->num, tp)) {
348 pr("treaty_accept: can't write treaty");
349 pr("Problem saving treaty; get help!\n");
353 pr("Treaty in effect until %s", ctime(&tp->trt_exp));
358 * Decline a treaty. Return RET_OK on success, anything else on error.
361 treaty_decline(struct ltcomstr *ltcp)
366 if (!gettre(ltcp->num, tp)) {
367 logerror("treaty_decline: can't read treaty");
368 pr("can't read treaty; get help!\n");
371 /* treaty got signed somehow between now and last time we read it */
372 if (tp->trt_status == TS_SIGNED) {
376 tp->trt_status = TS_FREE;
377 if (!puttre(ltcp->num, tp)) {
378 logerror("treaty_decline: can't write treaty");
379 pr("Problem saving treaty; get help!\n");
387 * Somebody tried to accept a loan/treaty that was retracted,
388 * or to decline a loan/treaty they already signed.
391 late(struct ltcomstr *ltcp)
393 pr("Too late; that %s %s!\n", ltcp->name,
394 (ltcp->op == 'a') ? "is no longer being offered"
395 : "has already been accepted");
399 * Loan or treaty was previously signed.
402 prev_signed(struct ltcomstr *ltcp)
404 pr("%s #%d is already in effect.\n", ltcp->Name, ltcp->num);
408 * Post-processing after successful declination of loan or treaty.
409 * Notify the folks involved.
412 decline(struct ltcomstr *ltcp)
414 if (ltcp->proposee == player->cnum) {
415 wu(0, ltcp->proposer, "%s %d refused by %s\n",
416 ltcp->Name, ltcp->num, cname(player->cnum));
417 pr("%s %d refused.\n", ltcp->Name, ltcp->num);
419 wu(0, ltcp->proposee,
420 "%s offer %d retracted by %s\n",
421 ltcp->Name, ltcp->num, cname(player->cnum));
422 pr("%s offer %d retracted.\n", ltcp->Name, ltcp->num);
427 * Post-processing after successful acceptance of loan or treaty.
428 * Notify the press, and the folks involved.
429 * (Weird spelling is to avoid accept(2)).
432 accpt(struct ltcomstr *ltcp)
434 switch (ltcp->type) {
436 nreport(ltcp->proposer, N_MAKE_LOAN, player->cnum, 1);
439 nreport(player->cnum, N_SIGN_TRE, ltcp->mailee, 1);
444 wu(0, ltcp->mailee, "%s #%d accepted by %s\n",
445 ltcp->Name, ltcp->num, cname(player->cnum));