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 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 * trdsub.c: Trade helper functions
30 * Known contributors to this file:
52 #include "prototypes.h"
56 trade_check_ok(struct trdstr *tp, union trdgenstr *tgp)
58 return check_trade_ok(tp) && trade_check_item_ok(tgp);
62 trade_check_item_ok(union trdgenstr *tgp)
64 if (tgp->gen.ef_type == EF_LAND)
65 return check_land_ok(&tgp->lnd);
66 if (tgp->gen.ef_type == EF_PLANE)
67 return check_plane_ok(&tgp->pln);
68 if (tgp->gen.ef_type == EF_SHIP)
69 return check_ship_ok(&tgp->shp);
70 if (tgp->gen.ef_type == EF_NUKE)
71 return check_nuke_ok(&tgp->nuk);
73 pr("Trade lot went bad!\n");
78 trade_nameof(struct trdstr *tp, union trdgenstr *tgp)
80 switch (tp->trd_type) {
82 return "nuclear stockpile";
84 return plchr[(int)tgp->pln.pln_type].pl_name;
86 return mchr[(int)tgp->shp.shp_type].m_name;
88 return lchr[(int)tgp->lnd.lnd_type].l_name;
90 return "Bad trade type, get help";
94 * Describe an item up for sale. "tgp" is a union containing
95 * the details of the generic item.
96 * Return 1 on success, 0 on error
99 trade_desc(struct trdstr *tp, union trdgenstr *tgp)
114 switch (tp->trd_type) {
117 if (!getsect(np->nuk_x, np->nuk_y, §))
119 tp->trd_owner = sect.sct_own;
120 natp = getnatp(tp->trd_owner);
121 pr("(%3d) ", sect.sct_own);
123 for (i = 0; i < N_MAXNUKE; i++) {
124 if (np->nuk_types[i]) {
127 pr("%dx%s", np->nuk_types[i], nchr[i].n_name);
134 tp->trd_owner = sp->shp_own;
135 pr("(%3d) tech %d %d%% %s [",
136 tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
138 for (it = I_NONE + 1; it <= I_MAX; ++it) {
139 if (sp->shp_item[it])
140 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
142 pr("] #%d", tp->trd_unitid);
144 snxtitem_all(&ni, EF_PLANE);
145 while (nxtitem(&ni, &plane)) {
146 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
147 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
150 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
151 if (plane.pln_nuketype != -1) {
152 pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
156 snxtitem_all(&ni, EF_LAND);
157 while (nxtitem(&ni, &land)) {
158 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
159 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
162 lchr[(int)land.lnd_type].l_name, land.lnd_uid);
163 if (land.lnd_nxlight) {
164 snxtitem_all(&ni, EF_PLANE);
165 while (nxtitem(&ni, &plane)) {
166 if (plane.pln_land == land.lnd_uid) {
167 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
170 plchr[(int)plane.pln_type].pl_name,
172 if (plane.pln_nuketype != -1) {
174 nchr[(int)plane.pln_nuketype].n_name);
182 getsect(sp->shp_x, sp->shp_y, §);
183 if (sect.sct_type != SCT_WATER)
184 pr(" in a %s %s", cname(sect.sct_own),
185 dchr[sect.sct_type].d_name);
191 tp->trd_owner = lp->lnd_own;
192 pr("(%3d) tech %d %d%% %s [",
194 lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
195 for (it = I_NONE + 1; it <= I_MAX; ++it) {
196 if (lp->lnd_item[it])
197 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
199 pr("] #%d", tp->trd_unitid);
201 snxtitem_all(&ni, EF_PLANE);
202 while (nxtitem(&ni, &plane)) {
203 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
204 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
207 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
208 if (plane.pln_nuketype != -1) {
209 pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
214 getsect(lp->lnd_x, lp->lnd_y, §);
218 tp->trd_owner = pp->pln_own;
219 pr("(%3d) tech %d %d%% %s #%d",
223 plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
224 if (pp->pln_nuketype != -1) {
225 pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
229 pr("flaky unit type %d", tp->trd_type);
236 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
238 if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
250 for (j = 0; NULL != (sp = getsectid(j)); j++) {
251 if (sp->sct_own != cnum)
253 secttot += (long)(dchr[sp->sct_type].d_value *
254 ((float)sp->sct_effic + 100.0));
255 for (k = 0; ichr[k].i_name; k++) {
256 if (ichr[k].i_value == 0 || ichr[k].i_uid == I_NONE)
258 val = sp->sct_item[ichr[k].i_uid];
259 secttot += val * ichr[k].i_value;
266 get_outstand(int cnum)
272 for (j = 0; getloan(j, &loan); j++) {
273 if (loan.l_status == LS_FREE)
275 if (loan.l_lonee == cnum)
276 loantot += loan.l_amtdue;
282 * Return amount due for LOAN at time PAYTIME.
285 loan_owed(struct lonstr *loan, time_t paytime)
287 time_t rtime; /* regular interest time */
288 time_t xtime; /* double interest time */
293 * Split interval paytime - l_lastpay into regular (up to
294 * l_duedate) and extended (beyond l_duedate) time.
296 rtime = loan->l_duedate - loan->l_lastpay;
297 xtime = paytime - loan->l_duedate;
306 if (CANT_HAPPEN(rtime < 0))
310 if (CANT_HAPPEN(dur <= 0))
312 rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
314 return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);