]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Update copyright notice
[empserver] / src / lib / subs / trdsub.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  trdsub.c: Trade helper functions
28  *
29  *  Known contributors to this file:
30  *     St Phil, 1989
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996
33  */
34
35 #include <config.h>
36
37 #include "empobj.h"
38 #include "item.h"
39 #include "land.h"
40 #include "loan.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "nsc.h"
44 #include "nuke.h"
45 #include "plane.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49 #include "trade.h"
50 #include "unit.h"
51 #include "xy.h"
52
53 int
54 trade_check_ok(struct trdstr *tp, struct empobj *tgp)
55 {
56     return check_trade_ok(tp) && check_obj_ok(tgp);
57 }
58
59 char *
60 trade_nameof(struct trdstr *tp, struct empobj *tgp)
61 {
62     switch (tp->trd_type) {
63     case EF_NUKE:
64         return nchr[tgp->type].n_name;
65     case EF_PLANE:
66         return plchr[tgp->type].pl_name;
67     case EF_SHIP:
68         return mchr[tgp->type].m_name;
69     case EF_LAND:
70         return lchr[tgp->type].l_name;
71     }
72     return "Bad trade type, get help";
73 }
74
75 /*
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
79  */
80 int
81 trade_desc(struct empobj *tgp)
82 {
83     i_type it;
84     struct sctstr sect;
85     struct nukstr *np;
86     struct shpstr *sp;
87     struct plnstr *pp;
88     struct lndstr *lp;
89     struct nstr_item ni;
90     struct plnstr plane;
91     struct lndstr land;
92     struct nukstr nuke;
93
94     switch (tgp->ef_type) {
95     case EF_NUKE:
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);
100         break;
101     case EF_SHIP:
102         sp = (struct shpstr *)tgp;
103         pr("(%3d)  tech %d %d%% %s [",
104            sp->shp_own, sp->shp_tech, sp->shp_effic, prship(sp));
105
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]);
109         }
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",
114                plane.pln_tech,
115                plane.pln_effic,
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);
119         }
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",
123                land.lnd_tech,
124                land.lnd_effic,
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",
130                        plane.pln_tech,
131                        plane.pln_effic,
132                        plchr[(int)plane.pln_type].pl_name,
133                        plane.pln_uid);
134                     if (getnuke(nuk_on_plane(&plane), &nuke))
135                         pr("(%s)", nchr[nuke.nuk_type].n_name);
136                 }
137             }
138         }
139         getsect(sp->shp_x, sp->shp_y, &sect);
140         if (sect.sct_type != SCT_WATER)
141             pr(" in a %s %s",
142                cname(sect.sct_own), dchr[sect.sct_type].d_name);
143         else
144             pr(" at sea");
145         break;
146     case EF_LAND:
147         lp = (struct lndstr *)tgp;
148         pr("(%3d)  tech %d %d%% %s [",
149            lp->lnd_own,
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]);
154         }
155         pr("] #%d", lp->lnd_uid);
156         break;
157     case EF_PLANE:
158         pp = (struct plnstr *)tgp;
159         pr("(%3d)  tech %d %d%% %s #%d",
160            pp->pln_own,
161            pp->pln_tech,
162            pp->pln_effic,
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);
166         break;
167     default:
168         pr("flaky unit type %d", tgp->uid);
169         break;
170     }
171     return 1;
172 }
173
174 int
175 trade_has_unsalable_cargo(struct empobj *tgp, int noisy)
176 {
177     int ret, i, type;
178     short *item;
179     struct nstr_item ni;
180     union empobj_storage cargo;
181
182     ret = 0;
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) {
189                 if (noisy)
190                     pr("%s carries %s, which you can't sell.\n",
191                        unit_nameof(tgp), ichr[i].i_name);
192                 ret = 1;
193             }
194         }
195     }
196
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);
201     }
202
203     return ret;
204 }
205
206 int
207 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
208 {
209     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
210         return 0;
211     return 1;
212 }
213
214 /*
215  * Return amount due for @loan at time @paytime.
216  */
217 double
218 loan_owed(struct lonstr *loan, time_t paytime)
219 {
220     time_t rtime;               /* regular interest time */
221     time_t xtime;               /* double interest time */
222     double rate;
223     int dur;
224
225     /*
226      * Split interval paytime - l_lastpay into regular (up to
227      * l_duedate) and extended (beyond l_duedate) time.
228      */
229     rtime = loan->l_duedate - loan->l_lastpay;
230     xtime = paytime - loan->l_duedate;
231     if (rtime < 0) {
232         xtime += rtime;
233         rtime = 0;
234     }
235     if (xtime < 0) {
236         rtime += xtime;
237         xtime = 0;
238     }
239     if (CANT_HAPPEN(rtime < 0))
240         rtime = 0;
241
242     dur = loan->l_ldur;
243     if (CANT_HAPPEN(dur <= 0))
244         dur = 1;
245     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
246
247     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
248 }