]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Remove a bunch of redundant casts.
[empserver] / src / lib / subs / trdsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
37 #include "player.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "item.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "nuke.h"
44 #include "plane.h"
45 #include "trade.h"
46 #include "xy.h"
47 #include "nsc.h"
48 #include "file.h"
49 #include "loan.h"
50 #include "prototypes.h"
51 #include "optlist.h"
52
53 int
54 trade_check_ok(struct trdstr *tp, union trdgenstr *tgp)
55 {
56     union trdgenstr check;
57
58     if (!check_trade_ok(tp))
59         return 0;
60
61     trade_getitem(tp, &check);
62     if (tp->trd_type == EF_LAND)
63         return check_land_ok(&tgp->lnd);
64     if (tp->trd_type == EF_PLANE)
65         return check_plane_ok(&tgp->pln);
66     if (tp->trd_type == EF_SHIP)
67         return check_ship_ok(&tgp->shp);
68     if (tp->trd_type == EF_NUKE)
69         return check_nuke_ok(&tgp->nuk);
70     CANT_HAPPEN("Bad TRD_TYPE");
71     pr("Trade lot #%d went bad!\n", tp->trd_uid);
72     return 0;
73 }
74
75 s_char *
76 trade_nameof(struct trdstr *tp, union trdgenstr *tgp)
77 {
78     switch (tp->trd_type) {
79     case EF_NUKE:
80         return "nuclear stockpile";
81     case EF_PLANE:
82         return plchr[(int)tgp->pln.pln_type].pl_name;
83     case EF_SHIP:
84         return mchr[(int)tgp->shp.shp_type].m_name;
85     case EF_LAND:
86         return lchr[(int)tgp->lnd.lnd_type].l_name;
87     }
88     return "Bad trade type, get help";
89 }
90
91 /*
92  * Describe an item up for sale.  "tgp" is a union containing
93  * the details of the generic item.
94  * Return 1 on success, 0 on error
95  */
96 int
97 trade_desc(struct trdstr *tp, union trdgenstr *tgp)
98 {
99     int i;
100     struct sctstr sect;
101     struct nukstr *np;
102     struct shpstr *sp;
103     struct plnstr *pp;
104     struct lndstr *lp;
105     struct natstr *natp;
106     int needcomma;
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         if (!getsect(np->nuk_x, np->nuk_y, &sect))
115             return 0;
116         tp->trd_owner = sect.sct_own;
117         natp = getnatp(tp->trd_owner);
118         pr("(%3d)  ", sect.sct_own);
119         needcomma = 0;
120         for (i = 0; i < N_MAXNUKE; i++) {
121             if (np->nuk_types[i]) {
122                 if (needcomma)
123                     pr(",");
124                 pr("%dx%s", np->nuk_types[i], nchr[i].n_name);
125                 needcomma = 1;
126             }
127         }
128         break;
129     case EF_SHIP:
130         sp = &tgp->shp;
131         tp->trd_owner = sp->shp_own;
132         pr("(%3d)  tech %d %d%% %s [",
133            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
134
135         for (i = I_NONE + 1; i <= I_MAX; ++i) {
136             if (sp->shp_item[i])
137                 pr("%c:%d ", ichr[i].i_mnem, sp->shp_item[i]);
138         }
139         pr("] #%d", tp->trd_unitid);
140         if (opt_SHOWPLANE) {
141             snxtitem_all(&ni, EF_PLANE);
142             while (nxtitem(&ni, &plane)) {
143                 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
144                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
145                        plane.pln_tech,
146                        plane.pln_effic,
147                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
148                     if (plane.pln_nuketype != -1) {
149                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
150                     }
151                 }
152             }
153             snxtitem_all(&ni, EF_LAND);
154             while (nxtitem(&ni, &land)) {
155                 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
156                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
157                        land.lnd_tech,
158                        land.lnd_effic,
159                        lchr[(int)land.lnd_type].l_name, land.lnd_uid);
160                     if (land.lnd_nxlight) {
161                         snxtitem_all(&ni, EF_PLANE);
162                         while (nxtitem(&ni, &plane)) {
163                             if (plane.pln_land == land.lnd_uid) {
164                                 pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
165                                    plane.pln_tech,
166                                    plane.pln_effic,
167                                    plchr[(int)plane.pln_type].pl_name,
168                                    plane.pln_uid);
169                                 if (plane.pln_nuketype != -1) {
170                                     pr("(%s)",
171                                        nchr[(int)plane.pln_nuketype].
172                                        n_name);
173                                 }
174                             }
175                         }
176                     }
177                 }
178             }
179         }
180         getsect(sp->shp_x, sp->shp_y, &sect);
181         if (sect.sct_type != SCT_WATER)
182             pr(" in a %s %s", cname(sect.sct_own),
183                dchr[sect.sct_type].d_name);
184         else
185             pr(" at sea");
186         break;
187     case EF_LAND:
188         lp = &tgp->lnd;
189         tp->trd_owner = lp->lnd_own;
190         pr("(%3d)  tech %d %d%% %s [",
191            tp->trd_owner,
192            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
193         for (i = I_NONE + 1; i <= I_MAX; ++i) {
194             if (lp->lnd_item[i])
195                 pr("%c:%d ", ichr[i].i_mnem, lp->lnd_item[i]);
196         }
197         pr("] #%d", tp->trd_unitid);
198         if (opt_SHOWPLANE) {
199             snxtitem_all(&ni, EF_PLANE);
200             while (nxtitem(&ni, &plane)) {
201                 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
202                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
203                        plane.pln_tech,
204                        plane.pln_effic,
205                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
206                     if (plane.pln_nuketype != -1) {
207                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
208                     }
209                 }
210             }
211         }
212         getsect(lp->lnd_x, lp->lnd_y, &sect);
213         break;
214     case EF_PLANE:
215         pp = &tgp->pln;
216         tp->trd_owner = pp->pln_own;
217         pr("(%3d)  tech %d %d%% %s #%d",
218            tp->trd_owner,
219            pp->pln_tech,
220            pp->pln_effic,
221            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
222         if (pp->pln_nuketype != -1) {
223             pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
224         }
225         break;
226     default:
227         pr("flaky unit type %d", tp->trd_type);
228         break;
229     }
230     return 1;
231 }
232
233 int
234 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
235 {
236     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
237         return 0;
238     return 1;
239 }
240
241 long
242 get_couval(int cnum)
243 {
244     struct sctstr *sp;
245     int j, k, val;
246     long secttot = 0;
247
248     for (j = 0; NULL != (sp = getsectid(j)); j++) {
249         if (sp->sct_own != cnum)
250             continue;
251         secttot += (long)(dchr[sp->sct_type].d_value *
252                           ((float)sp->sct_effic + 100.0));
253         for (k = 0; ichr[k].i_name; k++) {
254             if (ichr[k].i_value == 0 || ichr[k].i_vtype == I_NONE)
255                 continue;
256             val = sp->sct_item[ichr[k].i_vtype];
257             secttot += val * ichr[k].i_value;
258         }
259     }
260     return secttot;
261 }
262
263 long
264 get_outstand(int cnum)
265 {
266     struct lonstr loan;
267     int j;
268     long loantot = 0;
269
270     for (j = 0; getloan(j, &loan); j++) {
271         if (loan.l_status == LS_FREE)
272             continue;
273         if (loan.l_lonee == cnum)
274             loantot += loan.l_amtdue;
275     }
276     return loantot;
277 }
278
279 /*
280  * Return amount due for LOAN at time PAYTIME.
281  */
282 double
283 loan_owed(struct lonstr *loan, time_t paytime)
284 {
285     time_t rtime;               /* regular interest time */
286     time_t xtime;               /* double interest time */
287     double rate;
288     int dur;
289
290     /*
291      * Split interval paytime - l_lastpay into regular (up to
292      * l_duedate) and extended (beyond l_duedate) time.
293      */
294     rtime = loan->l_duedate - loan->l_lastpay;
295     xtime = paytime - loan->l_duedate;
296     if (rtime < 0) {
297         xtime += rtime;
298         rtime = 0;
299     }
300     if (xtime < 0) {
301         rtime += xtime;
302         xtime = 0;
303     }
304     if (CANT_HAPPEN(rtime < 0))
305         rtime = 0;
306
307     dur = loan->l_ldur;
308     if (CANT_HAPPEN(dur <= 0))
309         dur = 1;
310     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
311
312     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
313 }