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 nchr[(int)tgp->nuk.nuk_type].n_name;
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)
111 switch (tp->trd_type) {
114 tp->trd_owner = np->nuk_own;
115 pr("(%3d) tech %d %d%% %s #%d",
116 tp->trd_owner, np->nuk_tech, np->nuk_effic,
117 nchr[(int)np->nuk_type].n_name, tp->trd_unitid);
121 tp->trd_owner = sp->shp_own;
122 pr("(%3d) tech %d %d%% %s [",
123 tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
125 for (it = I_NONE + 1; it <= I_MAX; ++it) {
126 if (sp->shp_item[it])
127 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
129 pr("] #%d", tp->trd_unitid);
131 snxtitem_all(&ni, EF_PLANE);
132 while (nxtitem(&ni, &plane)) {
133 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
134 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
137 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
138 if (plane.pln_nuketype != -1) {
139 pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
143 snxtitem_all(&ni, EF_LAND);
144 while (nxtitem(&ni, &land)) {
145 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
146 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
149 lchr[(int)land.lnd_type].l_name, land.lnd_uid);
150 if (land.lnd_nxlight) {
151 snxtitem_all(&ni, EF_PLANE);
152 while (nxtitem(&ni, &plane)) {
153 if (plane.pln_land == land.lnd_uid) {
154 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
157 plchr[(int)plane.pln_type].pl_name,
159 if (plane.pln_nuketype != -1) {
161 nchr[(int)plane.pln_nuketype].n_name);
169 getsect(sp->shp_x, sp->shp_y, §);
170 if (sect.sct_type != SCT_WATER)
171 pr(" in a %s %s", cname(sect.sct_own),
172 dchr[sect.sct_type].d_name);
178 tp->trd_owner = lp->lnd_own;
179 pr("(%3d) tech %d %d%% %s [",
181 lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
182 for (it = I_NONE + 1; it <= I_MAX; ++it) {
183 if (lp->lnd_item[it])
184 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
186 pr("] #%d", tp->trd_unitid);
188 snxtitem_all(&ni, EF_PLANE);
189 while (nxtitem(&ni, &plane)) {
190 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
191 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
194 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
195 if (plane.pln_nuketype != -1) {
196 pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
201 getsect(lp->lnd_x, lp->lnd_y, §);
205 tp->trd_owner = pp->pln_own;
206 pr("(%3d) tech %d %d%% %s #%d",
210 plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
211 if (pp->pln_nuketype != -1) {
212 pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
216 pr("flaky unit type %d", tp->trd_type);
223 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
225 if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
231 * Return amount due for LOAN at time PAYTIME.
234 loan_owed(struct lonstr *loan, time_t paytime)
236 time_t rtime; /* regular interest time */
237 time_t xtime; /* double interest time */
242 * Split interval paytime - l_lastpay into regular (up to
243 * l_duedate) and extended (beyond l_duedate) time.
245 rtime = loan->l_duedate - loan->l_lastpay;
246 xtime = paytime - loan->l_duedate;
255 if (CANT_HAPPEN(rtime < 0))
259 if (CANT_HAPPEN(dur <= 0))
261 rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
263 return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);