2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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:
47 #include "prototypes.h"
55 trade_check_ok(struct trdstr *tp, struct empobj *tgp)
57 return check_trade_ok(tp) && check_obj_ok(tgp);
61 trade_nameof(struct trdstr *tp, struct empobj *tgp)
63 switch (tp->trd_type) {
65 return nchr[tgp->type].n_name;
67 return plchr[tgp->type].pl_name;
69 return mchr[tgp->type].m_name;
71 return lchr[tgp->type].l_name;
73 return "Bad trade type, get help";
77 * Describe an item up for sale. "tgp" is a union containing
78 * the details of the generic item.
79 * Return 1 on success, 0 on error
82 trade_desc(struct empobj *tgp)
95 switch (tgp->ef_type) {
97 np = (struct nukstr *)tgp;
98 pr("(%3d) tech %d %d%% %s #%d",
99 np->nuk_own, np->nuk_tech, np->nuk_effic,
100 nchr[(int)np->nuk_type].n_name, np->nuk_uid);
103 sp = (struct shpstr *)tgp;
104 pr("(%3d) tech %d %d%% %s [",
105 sp->shp_own, sp->shp_tech, sp->shp_effic, prship(sp));
107 for (it = I_NONE + 1; it <= I_MAX; ++it) {
108 if (sp->shp_item[it])
109 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
111 pr("] #%d", sp->shp_uid);
112 snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
113 while (nxtitem(&ni, &plane)) {
114 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
117 plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
118 if (getnuke(nuk_on_plane(&plane), &nuke))
119 pr("(%s)", nchr[nuke.nuk_type].n_name);
121 snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
122 while (nxtitem(&ni, &land)) {
123 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
126 lchr[(int)land.lnd_type].l_name, land.lnd_uid);
127 if (pln_first_on_land(&land) >= 0) {
128 snxtitem_cargo(&ni, EF_PLANE, EF_LAND, land.lnd_uid);
129 while (nxtitem(&ni, &plane)) {
130 pr("\n\t\t\t\t tech %3d %3d%% %s #%d",
133 plchr[(int)plane.pln_type].pl_name,
135 if (getnuke(nuk_on_plane(&plane), &nuke))
136 pr("(%s)", nchr[nuke.nuk_type].n_name);
140 getsect(sp->shp_x, sp->shp_y, §);
141 if (sect.sct_type != SCT_WATER)
143 cname(sect.sct_own), dchr[sect.sct_type].d_name);
148 lp = (struct lndstr *)tgp;
149 pr("(%3d) tech %d %d%% %s [",
151 lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
152 for (it = I_NONE + 1; it <= I_MAX; ++it) {
153 if (lp->lnd_item[it])
154 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
156 pr("] #%d", lp->lnd_uid);
159 pp = (struct plnstr *)tgp;
160 pr("(%3d) tech %d %d%% %s #%d",
164 plchr[(int)pp->pln_type].pl_name, pp->pln_uid);
165 if (getnuke(nuk_on_plane(pp), &nuke))
166 pr("(%s)", nchr[nuke.nuk_type].n_name);
169 pr("flaky unit type %d", tgp->uid);
176 trade_has_unsalable_cargo(struct empobj *tgp, int noisy)
181 union empobj_storage cargo;
184 if (tgp->ef_type == EF_SHIP || tgp->ef_type == EF_LAND) {
185 item = tgp->ef_type == EF_SHIP
186 ? ((struct shpstr *)tgp)->shp_item
187 : ((struct lndstr *)tgp)->lnd_item;
188 for (i = I_NONE + 1; i <= I_MAX; i++) {
189 if (item[i] && !ichr[i].i_sell) {
191 pr("%s carries %s, which you can't sell.\n",
192 unit_nameof(tgp), ichr[i].i_name);
198 for (type = EF_PLANE; type <= EF_NUKE; type++) {
199 snxtitem_cargo(&ni, type, tgp->ef_type, tgp->uid);
200 while (nxtitem(&ni, &cargo))
201 ret |= trade_has_unsalable_cargo(&cargo.gen, noisy);
208 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
210 if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
216 * Return amount due for LOAN at time PAYTIME.
219 loan_owed(struct lonstr *loan, time_t paytime)
221 time_t rtime; /* regular interest time */
222 time_t xtime; /* double interest time */
227 * Split interval paytime - l_lastpay into regular (up to
228 * l_duedate) and extended (beyond l_duedate) time.
230 rtime = loan->l_duedate - loan->l_lastpay;
231 xtime = paytime - loan->l_duedate;
240 if (CANT_HAPPEN(rtime < 0))
244 if (CANT_HAPPEN(dur <= 0))
246 rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
248 return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);