]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Simplify tests for presence of load
[empserver] / src / lib / subs / trdsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 "empobj.h"
39 #include "file.h"
40 #include "item.h"
41 #include "land.h"
42 #include "loan.h"
43 #include "misc.h"
44 #include "nat.h"
45 #include "nsc.h"
46 #include "nuke.h"
47 #include "optlist.h"
48 #include "plane.h"
49 #include "player.h"
50 #include "prototypes.h"
51 #include "sect.h"
52 #include "ship.h"
53 #include "trade.h"
54 #include "xy.h"
55
56 int
57 trade_check_ok(struct trdstr *tp, union empobj_storage *tgp)
58 {
59     return check_trade_ok(tp) && trade_check_item_ok(tgp);
60 }
61
62 int
63 trade_check_item_ok(union empobj_storage *tgp)
64 {
65     if (tgp->gen.ef_type == EF_LAND)
66         return check_land_ok(&tgp->land);
67     if (tgp->gen.ef_type == EF_PLANE)
68         return check_plane_ok(&tgp->plane);
69     if (tgp->gen.ef_type == EF_SHIP)
70         return check_ship_ok(&tgp->ship);
71     if (tgp->gen.ef_type == EF_NUKE)
72         return check_nuke_ok(&tgp->nuke);
73     CANT_REACH();
74     pr("Trade lot went bad!\n");
75     return 0;
76 }
77
78 char *
79 trade_nameof(struct trdstr *tp, union empobj_storage *tgp)
80 {
81     switch (tp->trd_type) {
82     case EF_NUKE:
83         return nchr[(int)tgp->nuke.nuk_type].n_name;
84     case EF_PLANE:
85         return plchr[(int)tgp->plane.pln_type].pl_name;
86     case EF_SHIP:
87         return mchr[(int)tgp->ship.shp_type].m_name;
88     case EF_LAND:
89         return lchr[(int)tgp->land.lnd_type].l_name;
90     }
91     return "Bad trade type, get help";
92 }
93
94 /*
95  * Describe an item up for sale.  "tgp" is a union containing
96  * the details of the generic item.
97  * Return 1 on success, 0 on error
98  */
99 int
100 trade_desc(struct trdstr *tp, union empobj_storage *tgp)
101 {
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 nstr_item ni;
109     struct plnstr plane;
110     struct lndstr land;
111     struct nukstr nuke;
112
113     switch (tp->trd_type) {
114     case EF_NUKE:
115         np = &tgp->nuke;
116         tp->trd_owner = np->nuk_own;
117         pr("(%3d)  tech %d %d%% %s #%d",
118            tp->trd_owner, np->nuk_tech, np->nuk_effic,
119            nchr[(int)np->nuk_type].n_name, tp->trd_unitid);
120         break;
121     case EF_SHIP:
122         sp = &tgp->ship;
123         tp->trd_owner = sp->shp_own;
124         pr("(%3d)  tech %d %d%% %s [",
125            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
126
127         for (it = I_NONE + 1; it <= I_MAX; ++it) {
128             if (sp->shp_item[it])
129                 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
130         }
131         pr("] #%d", tp->trd_unitid);
132         if (opt_SHOWPLANE) {
133             snxtitem_all(&ni, EF_PLANE);
134             while (nxtitem(&ni, &plane)) {
135                 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
136                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
137                        plane.pln_tech,
138                        plane.pln_effic,
139                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
140                     if (getnuke(nuk_on_plane(&plane), &nuke))
141                         pr("(%s)", nchr[nuke.nuk_type].n_name);
142                 }
143             }
144             snxtitem_all(&ni, EF_LAND);
145             while (nxtitem(&ni, &land)) {
146                 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
147                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
148                        land.lnd_tech,
149                        land.lnd_effic,
150                        lchr[(int)land.lnd_type].l_name, land.lnd_uid);
151                     if (pln_first_on_land(&land) >= 0) {
152                         snxtitem_all(&ni, EF_PLANE);
153                         while (nxtitem(&ni, &plane)) {
154                             if (plane.pln_land == land.lnd_uid) {
155                                 pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
156                                    plane.pln_tech,
157                                    plane.pln_effic,
158                                    plchr[(int)plane.pln_type].pl_name,
159                                    plane.pln_uid);
160                                 if (getnuke(nuk_on_plane(&plane), &nuke))
161                                     pr("(%s)", nchr[nuke.nuk_type].n_name);
162                             }
163                         }
164                     }
165                 }
166             }
167         }
168         getsect(sp->shp_x, sp->shp_y, &sect);
169         if (sect.sct_type != SCT_WATER)
170             pr(" in a %s %s",
171                cname(sect.sct_own), dchr[sect.sct_type].d_name);
172         else
173             pr(" at sea");
174         break;
175     case EF_LAND:
176         lp = &tgp->land;
177         tp->trd_owner = lp->lnd_own;
178         pr("(%3d)  tech %d %d%% %s [",
179            tp->trd_owner,
180            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
181         for (it = I_NONE + 1; it <= I_MAX; ++it) {
182             if (lp->lnd_item[it])
183                 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
184         }
185         pr("] #%d", tp->trd_unitid);
186         if (opt_SHOWPLANE) {
187             snxtitem_all(&ni, EF_PLANE);
188             while (nxtitem(&ni, &plane)) {
189                 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
190                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
191                        plane.pln_tech,
192                        plane.pln_effic,
193                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
194                     if (getnuke(nuk_on_plane(&plane), &nuke))
195                         pr("(%s)", nchr[nuke.nuk_type].n_name);
196                 }
197             }
198         }
199         getsect(lp->lnd_x, lp->lnd_y, &sect);
200         break;
201     case EF_PLANE:
202         pp = &tgp->plane;
203         tp->trd_owner = pp->pln_own;
204         pr("(%3d)  tech %d %d%% %s #%d",
205            tp->trd_owner,
206            pp->pln_tech,
207            pp->pln_effic,
208            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
209         if (getnuke(nuk_on_plane(pp), &nuke))
210             pr("(%s)", nchr[nuke.nuk_type].n_name);
211         break;
212     default:
213         pr("flaky unit type %d", tp->trd_type);
214         break;
215     }
216     return 1;
217 }
218
219 int
220 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
221 {
222     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
223         return 0;
224     return 1;
225 }
226
227 /*
228  * Return amount due for LOAN at time PAYTIME.
229  */
230 double
231 loan_owed(struct lonstr *loan, time_t paytime)
232 {
233     time_t rtime;               /* regular interest time */
234     time_t xtime;               /* double interest time */
235     double rate;
236     int dur;
237
238     /*
239      * Split interval paytime - l_lastpay into regular (up to
240      * l_duedate) and extended (beyond l_duedate) time.
241      */
242     rtime = loan->l_duedate - loan->l_lastpay;
243     xtime = paytime - loan->l_duedate;
244     if (rtime < 0) {
245         xtime += rtime;
246         rtime = 0;
247     }
248     if (xtime < 0) {
249         rtime += xtime;
250         xtime = 0;
251     }
252     if (CANT_HAPPEN(rtime < 0))
253         rtime = 0;
254
255     dur = loan->l_ldur;
256     if (CANT_HAPPEN(dur <= 0))
257         dur = 1;
258     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
259
260     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
261 }