]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Remove unused variables.
[empserver] / src / lib / subs / trdsub.c
1 /*
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
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  trdsub.c: Trade helper functions
29  * 
30  *  Known contributors to this file:
31  *     St Phil, 1989
32  *     Pat Loney, 1992
33  *     Steve McClure, 1996
34  */
35
36 #include <config.h>
37
38 #include "misc.h"
39 #include "player.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "item.h"
43 #include "ship.h"
44 #include "land.h"
45 #include "nuke.h"
46 #include "plane.h"
47 #include "trade.h"
48 #include "xy.h"
49 #include "nsc.h"
50 #include "file.h"
51 #include "loan.h"
52 #include "prototypes.h"
53 #include "optlist.h"
54
55 int
56 trade_check_ok(struct trdstr *tp, union trdgenstr *tgp)
57 {
58     return check_trade_ok(tp) && trade_check_item_ok(tgp);
59 }
60
61 int
62 trade_check_item_ok(union trdgenstr *tgp)
63 {
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);
72     CANT_REACH();
73     pr("Trade lot went bad!\n");
74     return 0;
75 }
76
77 char *
78 trade_nameof(struct trdstr *tp, union trdgenstr *tgp)
79 {
80     switch (tp->trd_type) {
81     case EF_NUKE:
82         return nchr[(int)tgp->nuk.nuk_type].n_name;
83     case EF_PLANE:
84         return plchr[(int)tgp->pln.pln_type].pl_name;
85     case EF_SHIP:
86         return mchr[(int)tgp->shp.shp_type].m_name;
87     case EF_LAND:
88         return lchr[(int)tgp->lnd.lnd_type].l_name;
89     }
90     return "Bad trade type, get help";
91 }
92
93 /*
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
97  */
98 int
99 trade_desc(struct trdstr *tp, union trdgenstr *tgp)
100 {
101     i_type it;
102     struct sctstr sect;
103     struct nukstr *np;
104     struct shpstr *sp;
105     struct plnstr *pp;
106     struct lndstr *lp;
107     struct nstr_item ni;
108     struct plnstr plane;
109     struct lndstr land;
110
111     switch (tp->trd_type) {
112     case EF_NUKE:
113         np = &tgp->nuk;
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);
118         break;
119     case EF_SHIP:
120         sp = &tgp->shp;
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));
124
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]);
128         }
129         pr("] #%d", tp->trd_unitid);
130         if (opt_SHOWPLANE) {
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",
135                        plane.pln_tech,
136                        plane.pln_effic,
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);
140                     }
141                 }
142             }
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",
147                        land.lnd_tech,
148                        land.lnd_effic,
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",
155                                    plane.pln_tech,
156                                    plane.pln_effic,
157                                    plchr[(int)plane.pln_type].pl_name,
158                                    plane.pln_uid);
159                                 if (plane.pln_nuketype != -1) {
160                                     pr("(%s)",
161                                        nchr[(int)plane.pln_nuketype].n_name);
162                                 }
163                             }
164                         }
165                     }
166                 }
167             }
168         }
169         getsect(sp->shp_x, sp->shp_y, &sect);
170         if (sect.sct_type != SCT_WATER)
171             pr(" in a %s %s", cname(sect.sct_own),
172                dchr[sect.sct_type].d_name);
173         else
174             pr(" at sea");
175         break;
176     case EF_LAND:
177         lp = &tgp->lnd;
178         tp->trd_owner = lp->lnd_own;
179         pr("(%3d)  tech %d %d%% %s [",
180            tp->trd_owner,
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]);
185         }
186         pr("] #%d", tp->trd_unitid);
187         if (opt_SHOWPLANE) {
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",
192                        plane.pln_tech,
193                        plane.pln_effic,
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);
197                     }
198                 }
199             }
200         }
201         getsect(lp->lnd_x, lp->lnd_y, &sect);
202         break;
203     case EF_PLANE:
204         pp = &tgp->pln;
205         tp->trd_owner = pp->pln_own;
206         pr("(%3d)  tech %d %d%% %s #%d",
207            tp->trd_owner,
208            pp->pln_tech,
209            pp->pln_effic,
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);
213         }
214         break;
215     default:
216         pr("flaky unit type %d", tp->trd_type);
217         break;
218     }
219     return 1;
220 }
221
222 int
223 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
224 {
225     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
226         return 0;
227     return 1;
228 }
229
230 /*
231  * Return amount due for LOAN at time PAYTIME.
232  */
233 double
234 loan_owed(struct lonstr *loan, time_t paytime)
235 {
236     time_t rtime;               /* regular interest time */
237     time_t xtime;               /* double interest time */
238     double rate;
239     int dur;
240
241     /*
242      * Split interval paytime - l_lastpay into regular (up to
243      * l_duedate) and extended (beyond l_duedate) time.
244      */
245     rtime = loan->l_duedate - loan->l_lastpay;
246     xtime = paytime - loan->l_duedate;
247     if (rtime < 0) {
248         xtime += rtime;
249         rtime = 0;
250     }
251     if (xtime < 0) {
252         rtime += xtime;
253         xtime = 0;
254     }
255     if (CANT_HAPPEN(rtime < 0))
256         rtime = 0;
257
258     dur = loan->l_ldur;
259     if (CANT_HAPPEN(dur <= 0))
260         dur = 1;
261     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
262
263     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
264 }