]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Replace trade_check_item_ok() by check_obj_ok()
[empserver] / src / lib / subs / trdsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, 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) && check_obj_ok(&tgp->gen);
57 }
58
59 char *
60 trade_nameof(struct trdstr *tp, union empobj_storage *tgp)
61 {
62     switch (tp->trd_type) {
63     case EF_NUKE:
64         return nchr[(int)tgp->nuke.nuk_type].n_name;
65     case EF_PLANE:
66         return plchr[(int)tgp->plane.pln_type].pl_name;
67     case EF_SHIP:
68         return mchr[(int)tgp->ship.shp_type].m_name;
69     case EF_LAND:
70         return lchr[(int)tgp->land.lnd_type].l_name;
71     }
72     return "Bad trade type, get help";
73 }
74
75 /*
76  * Describe an item up for sale.  "tgp" is a union containing
77  * the details of the generic item.
78  * Return 1 on success, 0 on error
79  */
80 int
81 trade_desc(struct trdstr *tp, union empobj_storage *tgp)
82 {
83     i_type it;
84     struct sctstr sect;
85     struct nukstr *np;
86     struct shpstr *sp;
87     struct plnstr *pp;
88     struct lndstr *lp;
89     struct nstr_item ni;
90     struct plnstr plane;
91     struct lndstr land;
92     struct nukstr nuke;
93
94     switch (tp->trd_type) {
95     case EF_NUKE:
96         np = &tgp->nuke;
97         tp->trd_owner = np->nuk_own;
98         pr("(%3d)  tech %d %d%% %s #%d",
99            tp->trd_owner, np->nuk_tech, np->nuk_effic,
100            nchr[(int)np->nuk_type].n_name, tp->trd_unitid);
101         break;
102     case EF_SHIP:
103         sp = &tgp->ship;
104         tp->trd_owner = sp->shp_own;
105         pr("(%3d)  tech %d %d%% %s [",
106            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
107
108         for (it = I_NONE + 1; it <= I_MAX; ++it) {
109             if (sp->shp_item[it])
110                 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
111         }
112         pr("] #%d", tp->trd_unitid);
113         snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
114         while (nxtitem(&ni, &plane)) {
115             pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
116                plane.pln_tech,
117                plane.pln_effic,
118                plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
119             if (getnuke(nuk_on_plane(&plane), &nuke))
120                 pr("(%s)", nchr[nuke.nuk_type].n_name);
121         }
122         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
123         while (nxtitem(&ni, &land)) {
124             pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
125                land.lnd_tech,
126                land.lnd_effic,
127                lchr[(int)land.lnd_type].l_name, land.lnd_uid);
128             if (pln_first_on_land(&land) >= 0) {
129                 snxtitem_cargo(&ni, EF_PLANE, EF_LAND, land.lnd_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,
135                        plane.pln_uid);
136                     if (getnuke(nuk_on_plane(&plane), &nuke))
137                         pr("(%s)", nchr[nuke.nuk_type].n_name);
138                 }
139             }
140         }
141         getsect(sp->shp_x, sp->shp_y, &sect);
142         if (sect.sct_type != SCT_WATER)
143             pr(" in a %s %s",
144                cname(sect.sct_own), dchr[sect.sct_type].d_name);
145         else
146             pr(" at sea");
147         break;
148     case EF_LAND:
149         lp = &tgp->land;
150         tp->trd_owner = lp->lnd_own;
151         pr("(%3d)  tech %d %d%% %s [",
152            tp->trd_owner,
153            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
154         for (it = I_NONE + 1; it <= I_MAX; ++it) {
155             if (lp->lnd_item[it])
156                 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
157         }
158         pr("] #%d", tp->trd_unitid);
159         break;
160     case EF_PLANE:
161         pp = &tgp->plane;
162         tp->trd_owner = pp->pln_own;
163         pr("(%3d)  tech %d %d%% %s #%d",
164            tp->trd_owner,
165            pp->pln_tech,
166            pp->pln_effic,
167            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
168         if (getnuke(nuk_on_plane(pp), &nuke))
169             pr("(%s)", nchr[nuke.nuk_type].n_name);
170         break;
171     default:
172         pr("flaky unit type %d", tp->trd_type);
173         break;
174     }
175     return 1;
176 }
177
178 int
179 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
180 {
181     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
182         return 0;
183     return 1;
184 }
185
186 /*
187  * Return amount due for LOAN at time PAYTIME.
188  */
189 double
190 loan_owed(struct lonstr *loan, time_t paytime)
191 {
192     time_t rtime;               /* regular interest time */
193     time_t xtime;               /* double interest time */
194     double rate;
195     int dur;
196
197     /*
198      * Split interval paytime - l_lastpay into regular (up to
199      * l_duedate) and extended (beyond l_duedate) time.
200      */
201     rtime = loan->l_duedate - loan->l_lastpay;
202     xtime = paytime - loan->l_duedate;
203     if (rtime < 0) {
204         xtime += rtime;
205         rtime = 0;
206     }
207     if (xtime < 0) {
208         rtime += xtime;
209         xtime = 0;
210     }
211     if (CANT_HAPPEN(rtime < 0))
212         rtime = 0;
213
214     dur = loan->l_ldur;
215     if (CANT_HAPPEN(dur <= 0))
216         dur = 1;
217     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
218
219     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
220 }