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