]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Clean up superfluous includes
[empserver] / src / lib / subs / trdsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  trdsub.c: Trade helper functions
28  *
29  *  Known contributors to this file:
30  *     St Phil, 1989
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996
33  */
34
35 #include <config.h>
36
37 #include "empobj.h"
38 #include "file.h"
39 #include "item.h"
40 #include "land.h"
41 #include "loan.h"
42 #include "misc.h"
43 #include "nat.h"
44 #include "nsc.h"
45 #include "nuke.h"
46 #include "plane.h"
47 #include "prototypes.h"
48 #include "sect.h"
49 #include "ship.h"
50 #include "trade.h"
51 #include "xy.h"
52
53 int
54 trade_check_ok(struct trdstr *tp, union empobj_storage *tgp)
55 {
56     return check_trade_ok(tp) && trade_check_item_ok(tgp);
57 }
58
59 int
60 trade_check_item_ok(union empobj_storage *tgp)
61 {
62     if (tgp->gen.ef_type == EF_LAND)
63         return check_land_ok(&tgp->land);
64     if (tgp->gen.ef_type == EF_PLANE)
65         return check_plane_ok(&tgp->plane);
66     if (tgp->gen.ef_type == EF_SHIP)
67         return check_ship_ok(&tgp->ship);
68     if (tgp->gen.ef_type == EF_NUKE)
69         return check_nuke_ok(&tgp->nuke);
70     CANT_REACH();
71     pr("Trade lot went bad!\n");
72     return 0;
73 }
74
75 char *
76 trade_nameof(struct trdstr *tp, union empobj_storage *tgp)
77 {
78     switch (tp->trd_type) {
79     case EF_NUKE:
80         return nchr[(int)tgp->nuke.nuk_type].n_name;
81     case EF_PLANE:
82         return plchr[(int)tgp->plane.pln_type].pl_name;
83     case EF_SHIP:
84         return mchr[(int)tgp->ship.shp_type].m_name;
85     case EF_LAND:
86         return lchr[(int)tgp->land.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 empobj_storage *tgp)
98 {
99     i_type it;
100     struct sctstr sect;
101     struct nukstr *np;
102     struct shpstr *sp;
103     struct plnstr *pp;
104     struct lndstr *lp;
105     struct nstr_item ni;
106     struct plnstr plane;
107     struct lndstr land;
108     struct nukstr nuke;
109
110     switch (tp->trd_type) {
111     case EF_NUKE:
112         np = &tgp->nuke;
113         tp->trd_owner = np->nuk_own;
114         pr("(%3d)  tech %d %d%% %s #%d",
115            tp->trd_owner, np->nuk_tech, np->nuk_effic,
116            nchr[(int)np->nuk_type].n_name, tp->trd_unitid);
117         break;
118     case EF_SHIP:
119         sp = &tgp->ship;
120         tp->trd_owner = sp->shp_own;
121         pr("(%3d)  tech %d %d%% %s [",
122            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
123
124         for (it = I_NONE + 1; it <= I_MAX; ++it) {
125             if (sp->shp_item[it])
126                 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
127         }
128         pr("] #%d", tp->trd_unitid);
129         snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
130         while (nxtitem(&ni, &plane)) {
131             pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
132                plane.pln_tech,
133                plane.pln_effic,
134                plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
135             if (getnuke(nuk_on_plane(&plane), &nuke))
136                 pr("(%s)", nchr[nuke.nuk_type].n_name);
137         }
138         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
139         while (nxtitem(&ni, &land)) {
140             pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
141                land.lnd_tech,
142                land.lnd_effic,
143                lchr[(int)land.lnd_type].l_name, land.lnd_uid);
144             if (pln_first_on_land(&land) >= 0) {
145                 snxtitem_cargo(&ni, EF_PLANE, EF_LAND, land.lnd_uid);
146                 while (nxtitem(&ni, &plane)) {
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,
151                        plane.pln_uid);
152                     if (getnuke(nuk_on_plane(&plane), &nuke))
153                         pr("(%s)", nchr[nuke.nuk_type].n_name);
154                 }
155             }
156         }
157         getsect(sp->shp_x, sp->shp_y, &sect);
158         if (sect.sct_type != SCT_WATER)
159             pr(" in a %s %s",
160                cname(sect.sct_own), dchr[sect.sct_type].d_name);
161         else
162             pr(" at sea");
163         break;
164     case EF_LAND:
165         lp = &tgp->land;
166         tp->trd_owner = lp->lnd_own;
167         pr("(%3d)  tech %d %d%% %s [",
168            tp->trd_owner,
169            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
170         for (it = I_NONE + 1; it <= I_MAX; ++it) {
171             if (lp->lnd_item[it])
172                 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
173         }
174         pr("] #%d", tp->trd_unitid);
175         break;
176     case EF_PLANE:
177         pp = &tgp->plane;
178         tp->trd_owner = pp->pln_own;
179         pr("(%3d)  tech %d %d%% %s #%d",
180            tp->trd_owner,
181            pp->pln_tech,
182            pp->pln_effic,
183            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
184         if (getnuke(nuk_on_plane(pp), &nuke))
185             pr("(%s)", nchr[nuke.nuk_type].n_name);
186         break;
187     default:
188         pr("flaky unit type %d", tp->trd_type);
189         break;
190     }
191     return 1;
192 }
193
194 int
195 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
196 {
197     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
198         return 0;
199     return 1;
200 }
201
202 /*
203  * Return amount due for LOAN at time PAYTIME.
204  */
205 double
206 loan_owed(struct lonstr *loan, time_t paytime)
207 {
208     time_t rtime;               /* regular interest time */
209     time_t xtime;               /* double interest time */
210     double rate;
211     int dur;
212
213     /*
214      * Split interval paytime - l_lastpay into regular (up to
215      * l_duedate) and extended (beyond l_duedate) time.
216      */
217     rtime = loan->l_duedate - loan->l_lastpay;
218     xtime = paytime - loan->l_duedate;
219     if (rtime < 0) {
220         xtime += rtime;
221         rtime = 0;
222     }
223     if (xtime < 0) {
224         rtime += xtime;
225         xtime = 0;
226     }
227     if (CANT_HAPPEN(rtime < 0))
228         rtime = 0;
229
230     dur = loan->l_ldur;
231     if (CANT_HAPPEN(dur <= 0))
232         dur = 1;
233     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
234
235     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
236 }