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:
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 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);
76 rv = cons_choose(<c);
82 rv = (ltc.type == EF_TREATY) ? treaty_accept(<c)
86 rv = (ltc.type == EF_TREATY) ? treaty_decline(<c)
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.
105 cons_choose(struct ltcomstr *ltcp)
107 static int lon_or_trt[] = { EF_LOAN, EF_TREATY, EF_BAD };
114 memset(ltcp, 0, sizeof(*ltcp));
115 if (!getstarg(player->argp[1], "loan or treaty? ", buf))
117 ltcp->type = ef_byname_from(buf, lon_or_trt);
118 switch (ltcp->type) {
121 pr("Treaties are not enabled.\n");
124 ltcp->name = "treaty";
125 ltcp->Name = "Treaty";
129 pr("Loans are not enabled.\n");
136 pr("You must specify \"loan\" or \"treaty\".\n");
139 sprintf(prompt, "%s number? ", ltcp->Name);
140 if ((ltcp->num = onearg(player->argp[2], prompt)) < 0)
142 if (!ef_read(ltcp->type, ltcp->num, <cp->u) ||
143 !(ltcp->type == EF_TREATY
144 ? distrea(ltcp->num, <cp->u.t)
145 : disloan(ltcp->num, <cp->u.l))) {
146 pr("%s #%d is not being offered to you!\n", ltcp->Name, ltcp->num);
149 switch (ltcp->type) {
152 if (lp->l_status == LS_SIGNED) {
153 pr("That loan has already been accepted!\n");
156 ltcp->proposer = lp->l_loner;
157 ltcp->proposee = lp->l_lonee;
161 if (tp->trt_status == TS_SIGNED) {
162 pr("That treaty has already been accepted!\n");
165 ltcp->proposer = tp->trt_cna;
166 ltcp->proposee = tp->trt_cnb;
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'))
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.
184 loan_accept(struct ltcomstr *ltcp)
187 struct natstr *lender;
188 struct nstr_item nstr;
192 if (ltcp->proposee != player->cnum) {
193 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
196 if (!getloan(ltcp->num, lp)) {
197 logerror("loan_accept: can't read loan");
198 pr("can't read loan; get help!\n");
201 if (lp->l_status == LS_FREE) { /* other guy retratcted already */
205 if (lp->l_status == LS_SIGNED) { /* already signed somehow */
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");
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)
224 pr("You may borrow $%d at the same terms.\n", lp->l_amtdue);
226 lender->nat_money -= lp->l_amtdue;
228 player->dolcost -= lp->l_amtdue;
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");
238 pr("You are now $%d richer (sort of).\n", lp->l_amtdue);
243 * Declne a loan. Return RET_OK on success, anything else on error.
246 loan_decline(struct ltcomstr *ltcp)
251 if (!getloan(ltcp->num, lp)) {
252 logerror("loan_decline: can't read loan");
253 pr("can't read loan; get help!\n");
256 /* loan got accepted somehow between now and last time we checked */
257 if (lp->l_status == LS_SIGNED) {
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");
272 * Accept a treaty. Return RET_OK on success, anything else on error.
275 treaty_accept(struct ltcomstr *ltcp)
280 if (ltcp->proposee != player->cnum) {
281 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
284 if (!gettre(ltcp->num, tp)) {
285 pr("treaty_accept: can't read treaty");
286 pr("can't read treaty; get help!\n");
289 if (tp->trt_status == TS_FREE) { /* treaty offer withdrawn */
293 if (tp->trt_status == TS_SIGNED) { /* somehow got accepted */
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");
304 pr("Treaty in effect until %s", ctime(&tp->trt_exp));
309 * Decline a treaty. Return RET_OK on success, anything else on error.
312 treaty_decline(struct ltcomstr *ltcp)
317 if (!gettre(ltcp->num, tp)) {
318 logerror("treaty_decline: can't read treaty");
319 pr("can't read treaty; get help!\n");
322 /* treaty got signed somehow between now and last time we read it */
323 if (tp->trt_status == TS_SIGNED) {
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");
338 * Postpone a treaty; always succeeds.
341 postpone(struct ltcomstr *ltcp)
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));
351 * Somebody tried to accept a loan/treaty that was retracted,
352 * or to decline a loan/treaty they already signed.
355 late(struct ltcomstr *ltcp)
357 pr("Too late; that %s %s!\n", ltcp->name,
358 (ltcp->op == 'a') ? "is no longer being offered"
359 : "has already been accepted");
363 * Loan or treaty was previously signed.
366 prev_signed(struct ltcomstr *ltcp)
368 pr("%s #%d is already in effect.\n", ltcp->Name, ltcp->num);
372 * Post-processing after successful declination of loan or treaty.
373 * Notify the folks involved.
376 decline(struct ltcomstr *ltcp)
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);
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);
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)).
396 accpt(struct ltcomstr *ltcp)
398 if (ltcp->type == EF_LOAN)
399 nreport(ltcp->proposer, N_MAKE_LOAN, player->cnum, 1);
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));