2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, 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 * trdsub.c: Trade helper functions
29 * Known contributors to this file:
46 #include "prototypes.h"
54 trade_check_ok(struct trdstr *tp, struct empobj *tgp)
56 return check_trade_ok(tp) && check_obj_ok(tgp);
60 trade_nameof(struct trdstr *tp, struct empobj *tgp)
62 switch (tp->trd_type) {
64 return nchr[tgp->type].n_name;
66 return plchr[tgp->type].pl_name;
68 return mchr[tgp->type].m_name;
70 return lchr[tgp->type].l_name;
72 return "Bad trade type, get help";
76 * Describe an item up for sale. "tgp" is a union containing
77 * the details of the generic item.
78 * Return 1 on success, 0 on error
81 trade_desc(struct empobj *tgp)
94 switch (tgp->ef_type) {
96 np = (struct nukstr *)tgp;
97 pr("(%3d) tech %d %d%% %s #%d",
98 np->nuk_own, np->nuk_tech, np->nuk_effic,
99 nchr[(int)np->nuk_type].n_name, np->nuk_uid);
102 sp = (struct shpstr *)tgp;
103 pr("(%3d) tech %d %d%% %s [",
104 sp->shp_own, sp->shp_tech, sp->shp_effic, prship(sp));
106 for (it = I_NONE + 1; it <= I_MAX; ++it) {
107 if (sp->shp_item[it])
108 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
110 pr("] #%d", sp->shp_uid);
111 snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
112 while (nxtitem(&ni, &plane)) {
113 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
116 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
117 if (getnuke(nuk_on_plane(&plane), &nuke))
118 pr("(%s)", nchr[nuke.nuk_type].n_name);
120 snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
121 while (nxtitem(&ni, &land)) {
122 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
125 lchr[(int)land.lnd_type].l_name, land.lnd_uid);
126 if (pln_first_on_land(&land) >= 0) {
127 snxtitem_cargo(&ni, EF_PLANE, EF_LAND, land.lnd_uid);
128 while (nxtitem(&ni, &plane)) {
129 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
132 plchr[(int)plane.pln_type].pl_name,
134 if (getnuke(nuk_on_plane(&plane), &nuke))
135 pr("(%s)", nchr[nuke.nuk_type].n_name);
139 getsect(sp->shp_x, sp->shp_y, §);
140 if (sect.sct_type != SCT_WATER)
142 cname(sect.sct_own), dchr[sect.sct_type].d_name);
147 lp = (struct lndstr *)tgp;
148 pr("(%3d) tech %d %d%% %s [",
150 lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
151 for (it = I_NONE + 1; it <= I_MAX; ++it) {
152 if (lp->lnd_item[it])
153 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
155 pr("] #%d", lp->lnd_uid);
158 pp = (struct plnstr *)tgp;
159 pr("(%3d) tech %d %d%% %s #%d",
163 plchr[(int)pp->pln_type].pl_name, pp->pln_uid);
164 if (getnuke(nuk_on_plane(pp), &nuke))
165 pr("(%s)", nchr[nuke.nuk_type].n_name);
168 pr("flaky unit type %d", tgp->uid);
175 trade_has_unsalable_cargo(struct empobj *tgp, int noisy)
180 union empobj_storage cargo;
183 if (tgp->ef_type == EF_SHIP || tgp->ef_type == EF_LAND) {
184 item = tgp->ef_type == EF_SHIP
185 ? ((struct shpstr *)tgp)->shp_item
186 : ((struct lndstr *)tgp)->lnd_item;
187 for (i = I_NONE + 1; i <= I_MAX; i++) {
188 if (item[i] && !ichr[i].i_sell) {
190 pr("%s carries %s, which you can't sell.\n",
191 unit_nameof(tgp), ichr[i].i_name);
197 for (type = EF_PLANE; type <= EF_NUKE; type++) {
198 snxtitem_cargo(&ni, type, tgp->ef_type, tgp->uid);
199 while (nxtitem(&ni, &cargo))
200 ret |= trade_has_unsalable_cargo(&cargo.gen, noisy);
207 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
209 if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
215 * Return amount due for @loan at time @paytime.
218 loan_owed(struct lonstr *loan, time_t paytime)
220 time_t rtime; /* regular interest time */
221 time_t xtime; /* double interest time */
226 * Split interval paytime - l_lastpay into regular (up to
227 * l_duedate) and extended (beyond l_duedate) time.
229 rtime = loan->l_duedate - loan->l_lastpay;
230 xtime = paytime - loan->l_duedate;
239 if (CANT_HAPPEN(rtime < 0))
243 if (CANT_HAPPEN(dur <= 0))
245 rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
247 return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);