]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / subs / trdsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "misc.h"
37 #include "player.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "nat.h"
41 #include "item.h"
42 #include "ship.h"
43 #include "land.h"
44 #include "nuke.h"
45 #include "plane.h"
46 #include "trade.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "file.h"
50 #include "loan.h"
51 #include "prototypes.h"
52 #include "optlist.h"
53
54 int
55 trade_check_ok(int lot, struct trdstr *tp, union trdgenstr *tgp)
56 {
57     union trdgenstr check;
58     struct trdstr trade;
59     int result = 0;
60
61     trade_getitem(tp, &check);
62     if (tp->trd_type == EF_LAND)
63         result = memcmp(&(check.lnd), &(tgp->lnd), sizeof(struct lndstr));
64     else if (tp->trd_type == EF_PLANE)
65         result = memcmp(&(check.pln), &(tgp->pln), sizeof(struct plnstr));
66     else if (tp->trd_type == EF_SHIP)
67         result = memcmp(&(check.shp), &(tgp->shp), sizeof(struct shpstr));
68     else
69         result = memcmp(&(check.nuk), &(tgp->nuk), sizeof(struct nukstr));
70     if (result) {
71         pr("That item has changed!\n");
72         return 0;
73     }
74     gettrade(lot, &trade);
75     if (memcmp((s_char *)&trade, (s_char *)tp, sizeof(struct trdstr))) {
76         pr("That item has changed!\n");
77         return 0;
78     }
79     return 1;
80 }
81
82 s_char *
83 trade_nameof(struct trdstr *tp, union trdgenstr *tgp)
84 {
85     switch (tp->trd_type) {
86     case EF_NUKE:
87         return "nuclear stockpile";
88     case EF_PLANE:
89         return plchr[(int)tgp->pln.pln_type].pl_name;
90     case EF_SHIP:
91         return mchr[(int)tgp->shp.shp_type].m_name;
92     case EF_LAND:
93         return lchr[(int)tgp->lnd.lnd_type].l_name;
94     }
95     return "Bad trade type, get help";
96 }
97
98 /*
99  * Describe an item up for sale.  "tgp" is a union containing
100  * the details of the generic item.
101  * Return 1 on success, 0 on error
102  */
103 int
104 trade_desc(struct trdstr *tp, union trdgenstr *tgp)
105 {
106     int i;
107     float price;
108     struct sctstr sect;
109     struct nukstr *np;
110     struct shpstr *sp;
111     struct plnstr *pp;
112     struct lndstr *lp;
113     struct natstr *natp;
114     int needcomma;
115     struct nstr_item ni;
116     struct plnstr plane;
117     struct lndstr land;
118
119     price = multread(tp->trd_owner, player->cnum) * tp->trd_price;
120     switch (tp->trd_type) {
121     case EF_NUKE:
122         np = &tgp->nuk;
123         if (!getsect(np->nuk_x, np->nuk_y, &sect))
124             return 0;
125         tp->trd_owner = sect.sct_own;
126         natp = getnatp(tp->trd_owner);
127         pr("(%3d)  ", sect.sct_own);
128         needcomma = 0;
129         for (i = 0; i < N_MAXNUKE; i++) {
130             if (np->nuk_types[i]) {
131                 if (needcomma)
132                     pr(",");
133                 pr("%dx%s", np->nuk_types[i], nchr[i].n_name);
134                 needcomma = 1;
135             }
136         }
137         break;
138     case EF_SHIP:
139         sp = &tgp->shp;
140         tp->trd_owner = sp->shp_own;
141         pr("(%3d)  tech %d %d%% %s [",
142            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
143
144         for (i = 0; i < sp->shp_nv; i++) {
145             pr("%c:%d ",
146                ichr[sp->shp_vtype[i] & ~VT_TYPE].i_mnem, sp->shp_vamt[i]);
147         }
148         pr("] #%d", tp->trd_unitid);
149         if (opt_SHOWPLANE) {
150             snxtitem_all(&ni, EF_PLANE);
151             while (nxtitem(&ni, (s_char *)&plane)) {
152                 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
153                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
154                        plane.pln_tech,
155                        plane.pln_effic,
156                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
157                     if (plane.pln_nuketype != (s_char)-1) {
158                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
159                     }
160                 }
161             }
162             snxtitem_all(&ni, EF_LAND);
163             while (nxtitem(&ni, (s_char *)&land)) {
164                 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
165                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
166                        land.lnd_tech,
167                        land.lnd_effic,
168                        lchr[(int)land.lnd_type].l_name, land.lnd_uid);
169                     if (land.lnd_nxlight) {
170                         snxtitem_all(&ni, EF_PLANE);
171                         while (nxtitem(&ni, (s_char *)&plane)) {
172                             if (plane.pln_land == land.lnd_uid) {
173                                 pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
174                                    plane.pln_tech,
175                                    plane.pln_effic,
176                                    plchr[(int)plane.pln_type].pl_name,
177                                    plane.pln_uid);
178                                 if (plane.pln_nuketype != (s_char)-1) {
179                                     pr("(%s)",
180                                        nchr[(int)plane.pln_nuketype].
181                                        n_name);
182                                 }
183                             }
184                         }
185                     }
186                 }
187             }
188         }
189         getsect(sp->shp_x, sp->shp_y, &sect);
190         if (sect.sct_type != SCT_WATER)
191             pr(" in a %s %s", cname(sect.sct_own),
192                dchr[sect.sct_type].d_name);
193         else
194             pr(" at sea");
195         break;
196     case EF_LAND:
197         lp = &tgp->lnd;
198         tp->trd_owner = lp->lnd_own;
199         pr("(%3d)  tech %d %d%% %s [",
200            tp->trd_owner,
201            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
202         for (i = 0; i < lp->lnd_nv; i++) {
203             pr("%c:%d ",
204                ichr[lp->lnd_vtype[i] & ~VT_TYPE].i_mnem, lp->lnd_vamt[i]);
205         }
206         pr("] #%d", tp->trd_unitid);
207         if (opt_SHOWPLANE) {
208             snxtitem_all(&ni, EF_PLANE);
209             while (nxtitem(&ni, (s_char *)&plane)) {
210                 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
211                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
212                        plane.pln_tech,
213                        plane.pln_effic,
214                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
215                     if (plane.pln_nuketype != (s_char)-1) {
216                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
217                     }
218                 }
219             }
220         }
221         getsect(lp->lnd_x, lp->lnd_y, &sect);
222         break;
223     case EF_PLANE:
224         pp = &tgp->pln;
225         tp->trd_owner = pp->pln_own;
226         pr("(%3d)  tech %d %d%% %s #%d",
227            tp->trd_owner,
228            pp->pln_tech,
229            pp->pln_effic,
230            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
231         if (pp->pln_nuketype != (s_char)-1) {
232             pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
233         }
234         break;
235     default:
236         pr("flaky unit type %d", tp->trd_type);
237         break;
238     }
239     return 1;
240 }
241
242 int
243 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
244 {
245     if (!ef_read(tp->trd_type, tp->trd_unitid, (s_char *)tgp))
246         return 0;
247     return 1;
248 }
249
250 long
251 get_couval(int cnum)
252 {
253     struct sctstr *sp;
254     register int j, k, val;
255     long secttot = 0;
256
257     for (j = 0; NULL != (sp = getsectid(j)); j++) {
258         if (sp->sct_own != cnum)
259             continue;
260         secttot += (long)(dchr[sp->sct_type].d_value *
261                           ((float)sp->sct_effic + 100.0));
262         for (k = 0; ichr[k].i_name; k++) {
263             if (ichr[k].i_value == 0 || ichr[k].i_vtype == 0)
264                 continue;
265             val = getvar(ichr[k].i_vtype, (s_char *)sp, EF_SECTOR);
266             secttot += val * ichr[k].i_value;
267         }
268     }
269     return secttot;
270 }
271
272 long
273 get_outstand(int cnum)
274 {
275     struct lonstr loan;
276     int j;
277     long loantot = 0;
278
279     for (j = 0; getloan(j, &loan); j++) {
280         if (loan.l_status == LS_FREE)
281             continue;
282         if (loan.l_lonee == cnum)
283             loantot += loan.l_amtdue;
284     }
285     return loantot;
286 }
287
288 struct ichrstr *
289 whichitem(char p)
290 {
291     register int i;
292
293     if (p == 0)
294         return 0;
295     for (i = 1; ichr[i].i_mnem != 0; i++)
296         if (p == ichr[i].i_mnem)
297             return (struct ichrstr *)(&ichr[i]);
298     pr("Unrecognized item \"%c\"\n", p);
299     return 0;
300 }