]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
License upgrade to GPL version 3 or later
[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 "optlist.h"
47 #include "plane.h"
48 #include "player.h"
49 #include "prototypes.h"
50 #include "sect.h"
51 #include "ship.h"
52 #include "trade.h"
53 #include "xy.h"
54
55 int
56 trade_check_ok(struct trdstr *tp, union empobj_storage *tgp)
57 {
58     return check_trade_ok(tp) && trade_check_item_ok(tgp);
59 }
60
61 int
62 trade_check_item_ok(union empobj_storage *tgp)
63 {
64     if (tgp->gen.ef_type == EF_LAND)
65         return check_land_ok(&tgp->land);
66     if (tgp->gen.ef_type == EF_PLANE)
67         return check_plane_ok(&tgp->plane);
68     if (tgp->gen.ef_type == EF_SHIP)
69         return check_ship_ok(&tgp->ship);
70     if (tgp->gen.ef_type == EF_NUKE)
71         return check_nuke_ok(&tgp->nuke);
72     CANT_REACH();
73     pr("Trade lot went bad!\n");
74     return 0;
75 }
76
77 char *
78 trade_nameof(struct trdstr *tp, union empobj_storage *tgp)
79 {
80     switch (tp->trd_type) {
81     case EF_NUKE:
82         return nchr[(int)tgp->nuke.nuk_type].n_name;
83     case EF_PLANE:
84         return plchr[(int)tgp->plane.pln_type].pl_name;
85     case EF_SHIP:
86         return mchr[(int)tgp->ship.shp_type].m_name;
87     case EF_LAND:
88         return lchr[(int)tgp->land.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 empobj_storage *tgp)
100 {
101     i_type it;
102     struct sctstr sect;
103     struct nukstr *np;
104     struct shpstr *sp;
105     struct plnstr *pp;
106     struct lndstr *lp;
107     struct nstr_item ni;
108     struct plnstr plane;
109     struct lndstr land;
110     struct nukstr nuke;
111
112     switch (tp->trd_type) {
113     case EF_NUKE:
114         np = &tgp->nuke;
115         tp->trd_owner = np->nuk_own;
116         pr("(%3d)  tech %d %d%% %s #%d",
117            tp->trd_owner, np->nuk_tech, np->nuk_effic,
118            nchr[(int)np->nuk_type].n_name, tp->trd_unitid);
119         break;
120     case EF_SHIP:
121         sp = &tgp->ship;
122         tp->trd_owner = sp->shp_own;
123         pr("(%3d)  tech %d %d%% %s [",
124            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
125
126         for (it = I_NONE + 1; it <= I_MAX; ++it) {
127             if (sp->shp_item[it])
128                 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
129         }
130         pr("] #%d", tp->trd_unitid);
131         snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
132         while (nxtitem(&ni, &plane)) {
133             pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
134                plane.pln_tech,
135                plane.pln_effic,
136                plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
137             if (getnuke(nuk_on_plane(&plane), &nuke))
138                 pr("(%s)", nchr[nuke.nuk_type].n_name);
139         }
140         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
141         while (nxtitem(&ni, &land)) {
142             pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
143                land.lnd_tech,
144                land.lnd_effic,
145                lchr[(int)land.lnd_type].l_name, land.lnd_uid);
146             if (pln_first_on_land(&land) >= 0) {
147                 snxtitem_cargo(&ni, EF_PLANE, EF_LAND, land.lnd_uid);
148                 while (nxtitem(&ni, &plane)) {
149                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
150                        plane.pln_tech,
151                        plane.pln_effic,
152                        plchr[(int)plane.pln_type].pl_name,
153                        plane.pln_uid);
154                     if (getnuke(nuk_on_plane(&plane), &nuke))
155                         pr("(%s)", nchr[nuke.nuk_type].n_name);
156                 }
157             }
158         }
159         getsect(sp->shp_x, sp->shp_y, &sect);
160         if (sect.sct_type != SCT_WATER)
161             pr(" in a %s %s",
162                cname(sect.sct_own), dchr[sect.sct_type].d_name);
163         else
164             pr(" at sea");
165         break;
166     case EF_LAND:
167         lp = &tgp->land;
168         tp->trd_owner = lp->lnd_own;
169         pr("(%3d)  tech %d %d%% %s [",
170            tp->trd_owner,
171            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
172         for (it = I_NONE + 1; it <= I_MAX; ++it) {
173             if (lp->lnd_item[it])
174                 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
175         }
176         pr("] #%d", tp->trd_unitid);
177         break;
178     case EF_PLANE:
179         pp = &tgp->plane;
180         tp->trd_owner = pp->pln_own;
181         pr("(%3d)  tech %d %d%% %s #%d",
182            tp->trd_owner,
183            pp->pln_tech,
184            pp->pln_effic,
185            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
186         if (getnuke(nuk_on_plane(pp), &nuke))
187             pr("(%s)", nchr[nuke.nuk_type].n_name);
188         break;
189     default:
190         pr("flaky unit type %d", tp->trd_type);
191         break;
192     }
193     return 1;
194 }
195
196 int
197 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
198 {
199     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
200         return 0;
201     return 1;
202 }
203
204 /*
205  * Return amount due for LOAN at time PAYTIME.
206  */
207 double
208 loan_owed(struct lonstr *loan, time_t paytime)
209 {
210     time_t rtime;               /* regular interest time */
211     time_t xtime;               /* double interest time */
212     double rate;
213     int dur;
214
215     /*
216      * Split interval paytime - l_lastpay into regular (up to
217      * l_duedate) and extended (beyond l_duedate) time.
218      */
219     rtime = loan->l_duedate - loan->l_lastpay;
220     xtime = paytime - loan->l_duedate;
221     if (rtime < 0) {
222         xtime += rtime;
223         rtime = 0;
224     }
225     if (xtime < 0) {
226         rtime += xtime;
227         xtime = 0;
228     }
229     if (CANT_HAPPEN(rtime < 0))
230         rtime = 0;
231
232     dur = loan->l_ldur;
233     if (CANT_HAPPEN(dur <= 0))
234         dur = 1;
235     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
236
237     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
238 }