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