2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 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.
28 * cons.c: Consider a loan or treaty
30 * Known contributors to this file:
49 * Things common to a loan or treaty.
52 int type; /* EF_LOAN or EF_TREATY */
54 s_char *name; /* "loan" or "treaty" */
55 s_char *Name; /* "Loan" or "Treaty" */
56 natid proposer; /* country offering */
57 natid proposee; /* country offered to */
58 natid mailee; /* who gets mail about it */
59 s_char op; /* 'a', 'd', or 'p' */
61 struct lonstr l; /* the loan */
62 struct trtstr t; /* the treaty */
66 static int cons_choose(struct ltcomstr *ltcp);
67 static int treaty_accept(struct ltcomstr *ltcp);
68 static int treaty_decline(struct ltcomstr *ltcp);
69 static int loan_accept(struct ltcomstr *ltcp);
70 static int loan_decline(struct ltcomstr *ltcp);
71 static int postpone(struct ltcomstr *ltcp);
72 static void accpt(struct ltcomstr *ltcp);
73 static void decline(struct ltcomstr *ltcp);
74 static void late(struct ltcomstr *ltcp);
75 static void prev_signed(struct ltcomstr *ltcp);
83 rv = cons_choose(<c);
89 rv = (ltc.type == EF_TREATY) ? treaty_accept(<c)
93 rv = (ltc.type == EF_TREATY) ? treaty_decline(<c)
100 pr("Bad operation %c from cons_choose; get help!\n", ltc.op);
108 * Choose whether we want to accept, decline, or postpone a
109 * loan or treaty. Put all the goodies in ltcp, and return
110 * RET_OK if all goes well, and anything else on error.
113 cons_choose(struct ltcomstr *ltcp)
115 static int lon_or_trt[] = { EF_LOAN, EF_TREATY, EF_BAD };
122 memset(ltcp, 0, sizeof(*ltcp));
123 if (getstarg(player->argp[1], "loan or treaty? ", buf) == 0)
125 ltcp->type = ef_byname_from(buf, lon_or_trt);
126 switch (ltcp->type) {
129 pr("Treaties are not enabled.\n");
132 ltcp->name = "treaty";
133 ltcp->Name = "Treaty";
137 pr("Loans are not enabled.\n");
144 pr("You must specify \"loan\" or \"treaty\".\n");
147 sprintf(prompt, "%s number? ", ltcp->Name);
148 if ((ltcp->num = onearg(player->argp[2], prompt)) < 0)
150 if (!ef_read(ltcp->type, ltcp->num, <cp->u) ||
151 !(ltcp->type == EF_TREATY
152 ? distrea(ltcp->num, <cp->u.t)
153 : disloan(ltcp->num, <cp->u.l))) {
154 pr("%s #%d is not being offered to you!\n", ltcp->Name, ltcp->num);
157 switch (ltcp->type) {
160 if (lp->l_status == LS_SIGNED) {
161 pr("That loan has already been accepted!\n");
164 ltcp->proposer = lp->l_loner;
165 ltcp->proposee = lp->l_lonee;
169 if (tp->trt_status == TS_SIGNED) {
170 pr("That treaty has already been accepted!\n");
173 ltcp->proposer = tp->trt_cna;
174 ltcp->proposee = tp->trt_cnb;
177 ltcp->mailee = (ltcp->proposer == player->cnum)
178 ? ltcp->proposee : ltcp->proposer;
179 p = getstarg(player->argp[3], "Accept, decline or postpone? ", buf);
180 if (!p || (*p != 'a' && *p != 'd' && *p != 'p'))
187 * Accept a loan. If the offering country has too little money,
188 * leave him $100 left and offer the rest. Return RET_OK on
189 * success, anything else on error.
192 loan_accept(struct ltcomstr *ltcp)
195 struct natstr *lender;
196 struct nstr_item nstr;
200 if (ltcp->proposee != player->cnum) {
201 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
204 if (!getloan(ltcp->num, lp)) {
205 pr("loan_accept: can't read loan; get help!\n");
208 if (lp->l_status == LS_FREE) { /* other guy retratcted already */
212 if (lp->l_status == LS_SIGNED) { /* already signed somehow */
216 /* check to see if a loan already exists */
217 snxtitem_all(&nstr, EF_LOAN);
218 while (nxtitem(&nstr, &loan)) {
219 if (loan.l_status == LS_SIGNED && loan.l_lonee == lp->l_loner
220 && (loan.l_loner == lp->l_lonee)) {
221 pr("He already owes you money - make him repay his loan!\n");
225 lender = getnatp(ltcp->proposer);
226 if (lender->nat_money < lp->l_amtdue) { /* other guy is poor */
227 lp->l_amtdue = lender->nat_money - 100;
228 pr("%s no longer has the funds.\n", cname(ltcp->proposer));
229 if (lp->l_amtdue <= 0)
231 pr("You may borrow $%ld at the same terms.\n", lp->l_amtdue);
233 lender->nat_money -= lp->l_amtdue;
235 player->dolcost -= lp->l_amtdue;
237 (void)time(&lp->l_lastpay);
238 lp->l_duedate = lp->l_ldur * 86400 + lp->l_lastpay;
239 lp->l_status = LS_SIGNED;
240 if (!putloan(ltcp->num, lp)) {
241 pr("Problem writing lp->to disk; get help!\n");
245 pr("You are now $%ld richer (sort of).\n", lp->l_amtdue);
250 * Declne a loan. Return RET_OK on success, anything else on error.
253 loan_decline(struct ltcomstr *ltcp)
258 if (!getloan(ltcp->num, lp)) {
259 pr("Decline: can't read loan; get help!\n");
262 /* loan got accepted somehow between now and last time we checked */
263 if (lp->l_status == LS_SIGNED) {
267 lp->l_status = LS_FREE;
268 if (!putloan(ltcp->num, lp)) {
269 pr("loan_decline: can't write loan; get help!\n");
277 * Accept a treaty. Return RET_OK on success, anything else on error.
280 treaty_accept(struct ltcomstr *ltcp)
285 if (ltcp->proposee != player->cnum) {
286 pr("%s %d is still pending.\n", ltcp->Name, ltcp->num);
289 if (!gettre(ltcp->num, tp)) {
290 pr("Accept: can't read treaty; get help!\n");
293 if (tp->trt_status == TS_FREE) { /* treaty offer withdrawn */
297 if (tp->trt_status == TS_SIGNED) { /* somehow got accepted */
301 tp->trt_status = TS_SIGNED;
302 if (!puttre(ltcp->num, tp)) {
303 pr("Problem saving treaty; get help!\n");
307 pr("Treaty in effect until %s", ctime(&tp->trt_exp));
312 * Decline a treaty. Return RET_OK on success, anything else on error.
315 treaty_decline(struct ltcomstr *ltcp)
320 if (!gettre(ltcp->num, tp)) {
321 pr("Decline: 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 pr("Problem saving treaty; get help!\n");
339 * Postpone a treaty; always succeeds.
342 postpone(struct ltcomstr *ltcp)
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));
352 * Somebody tried to accept a loan/treaty that was retracted,
353 * or to decline a loan/treaty they already signed.
356 late(struct ltcomstr *ltcp)
358 pr("Too late; that %s %s!\n", ltcp->name,
359 (ltcp->op == 'a') ? "is no longer being offered"
360 : "has already been accepted");
364 * Loan or treaty was previously signed.
367 prev_signed(struct ltcomstr *ltcp)
369 pr("%s #%d is already in effect.\n", ltcp->Name, ltcp->num);
373 * Post-processing after successful declination of loan or treaty.
374 * Notify the folks involved.
377 decline(struct ltcomstr *ltcp)
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);
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);
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)).
397 accpt(struct ltcomstr *ltcp)
399 if (ltcp->type == EF_LOAN)
400 nreport(ltcp->proposer, N_MAKE_LOAN, player->cnum, 1);
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));