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