2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 * trdsub.c: Trade helper functions
30 * Known contributors to this file:
51 #include "prototypes.h"
55 trade_check_ok(struct trdstr *tp, union trdgenstr *tgp)
57 union trdgenstr check;
59 if (!check_trade_ok(tp))
62 trade_getitem(tp, &check);
63 if (tp->trd_type == EF_LAND)
64 return check_land_ok(&tgp->lnd);
65 if (tp->trd_type == EF_PLANE)
66 return check_plane_ok(&tgp->pln);
67 if (tp->trd_type == EF_SHIP)
68 return check_ship_ok(&tgp->shp);
69 if (tp->trd_type == EF_NUKE)
70 return check_nuke_ok(&tgp->nuk);
71 CANT_HAPPEN("Bad TRD_TYPE");
72 pr("Trade lot #%d went bad!\n", tp->trd_uid);
77 trade_nameof(struct trdstr *tp, union trdgenstr *tgp)
79 switch (tp->trd_type) {
81 return "nuclear stockpile";
83 return plchr[(int)tgp->pln.pln_type].pl_name;
85 return mchr[(int)tgp->shp.shp_type].m_name;
87 return lchr[(int)tgp->lnd.lnd_type].l_name;
89 return "Bad trade type, get help";
93 * Describe an item up for sale. "tgp" is a union containing
94 * the details of the generic item.
95 * Return 1 on success, 0 on error
98 trade_desc(struct trdstr *tp, union trdgenstr *tgp)
112 switch (tp->trd_type) {
115 if (!getsect(np->nuk_x, np->nuk_y, §))
117 tp->trd_owner = sect.sct_own;
118 natp = getnatp(tp->trd_owner);
119 pr("(%3d) ", sect.sct_own);
121 for (i = 0; i < N_MAXNUKE; i++) {
122 if (np->nuk_types[i]) {
125 pr("%dx%s", np->nuk_types[i], nchr[i].n_name);
132 tp->trd_owner = sp->shp_own;
133 pr("(%3d) tech %d %d%% %s [",
134 tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
136 for (i = I_NONE + 1; i <= I_MAX; ++i) {
138 pr("%c:%d ", ichr[i].i_mnem, sp->shp_item[i]);
140 pr("] #%d", tp->trd_unitid);
142 snxtitem_all(&ni, EF_PLANE);
143 while (nxtitem(&ni, (s_char *)&plane)) {
144 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
145 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
148 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
149 if (plane.pln_nuketype != (s_char)-1) {
150 pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
154 snxtitem_all(&ni, EF_LAND);
155 while (nxtitem(&ni, (s_char *)&land)) {
156 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
157 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
160 lchr[(int)land.lnd_type].l_name, land.lnd_uid);
161 if (land.lnd_nxlight) {
162 snxtitem_all(&ni, EF_PLANE);
163 while (nxtitem(&ni, (s_char *)&plane)) {
164 if (plane.pln_land == land.lnd_uid) {
165 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
168 plchr[(int)plane.pln_type].pl_name,
170 if (plane.pln_nuketype != (s_char)-1) {
172 nchr[(int)plane.pln_nuketype].
181 getsect(sp->shp_x, sp->shp_y, §);
182 if (sect.sct_type != SCT_WATER)
183 pr(" in a %s %s", cname(sect.sct_own),
184 dchr[sect.sct_type].d_name);
190 tp->trd_owner = lp->lnd_own;
191 pr("(%3d) tech %d %d%% %s [",
193 lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
194 for (i = I_NONE + 1; i <= I_MAX; ++i) {
196 pr("%c:%d ", ichr[i].i_mnem, lp->lnd_item[i]);
198 pr("] #%d", tp->trd_unitid);
200 snxtitem_all(&ni, EF_PLANE);
201 while (nxtitem(&ni, (s_char *)&plane)) {
202 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
203 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
206 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
207 if (plane.pln_nuketype != (s_char)-1) {
208 pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
213 getsect(lp->lnd_x, lp->lnd_y, §);
217 tp->trd_owner = pp->pln_own;
218 pr("(%3d) tech %d %d%% %s #%d",
222 plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
223 if (pp->pln_nuketype != (s_char)-1) {
224 pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
228 pr("flaky unit type %d", tp->trd_type);
235 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
237 if (!ef_read(tp->trd_type, tp->trd_unitid, (s_char *)tgp))
246 register int j, k, val;
249 for (j = 0; NULL != (sp = getsectid(j)); j++) {
250 if (sp->sct_own != cnum)
252 secttot += (long)(dchr[sp->sct_type].d_value *
253 ((float)sp->sct_effic + 100.0));
254 for (k = 0; ichr[k].i_name; k++) {
255 if (ichr[k].i_value == 0 || ichr[k].i_vtype == I_NONE)
257 val = sp->sct_item[ichr[k].i_vtype];
258 secttot += val * ichr[k].i_value;
265 get_outstand(int cnum)
271 for (j = 0; getloan(j, &loan); j++) {
272 if (loan.l_status == LS_FREE)
274 if (loan.l_lonee == cnum)
275 loantot += loan.l_amtdue;
281 * Return amount due for LOAN at time PAYTIME.
284 loan_owed(struct lonstr *loan, time_t paytime)
286 time_t rtime; /* regular interest time */
287 time_t xtime; /* double interest time */
292 * Split interval paytime - l_lastpay into regular (up to
293 * l_duedate) and extended (beyond l_duedate) time.
295 rtime = loan->l_duedate - loan->l_lastpay;
296 xtime = paytime - loan->l_duedate;
305 if (CANT_HAPPEN(rtime < 0))
309 if (CANT_HAPPEN(dur <= 0))
311 rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
313 return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);