]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Sectors need space for items, deliveries and distribution thresholds.
[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     struct sctstr sect;
108     struct nukstr *np;
109     struct shpstr *sp;
110     struct plnstr *pp;
111     struct lndstr *lp;
112     struct natstr *natp;
113     int needcomma;
114     struct nstr_item ni;
115     struct plnstr plane;
116     struct lndstr land;
117
118     switch (tp->trd_type) {
119     case EF_NUKE:
120         np = &tgp->nuk;
121         if (!getsect(np->nuk_x, np->nuk_y, &sect))
122             return 0;
123         tp->trd_owner = sect.sct_own;
124         natp = getnatp(tp->trd_owner);
125         pr("(%3d)  ", sect.sct_own);
126         needcomma = 0;
127         for (i = 0; i < N_MAXNUKE; i++) {
128             if (np->nuk_types[i]) {
129                 if (needcomma)
130                     pr(",");
131                 pr("%dx%s", np->nuk_types[i], nchr[i].n_name);
132                 needcomma = 1;
133             }
134         }
135         break;
136     case EF_SHIP:
137         sp = &tgp->shp;
138         tp->trd_owner = sp->shp_own;
139         pr("(%3d)  tech %d %d%% %s [",
140            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
141
142         for (i = 1; i <= I_MAX; ++i) {
143             if (sp->shp_item[i])
144                 pr("%c:%d ", ichr[i].i_mnem, sp->shp_item[i]);
145         }
146         pr("] #%d", tp->trd_unitid);
147         if (opt_SHOWPLANE) {
148             snxtitem_all(&ni, EF_PLANE);
149             while (nxtitem(&ni, (s_char *)&plane)) {
150                 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
151                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
152                        plane.pln_tech,
153                        plane.pln_effic,
154                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
155                     if (plane.pln_nuketype != (s_char)-1) {
156                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
157                     }
158                 }
159             }
160             snxtitem_all(&ni, EF_LAND);
161             while (nxtitem(&ni, (s_char *)&land)) {
162                 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
163                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
164                        land.lnd_tech,
165                        land.lnd_effic,
166                        lchr[(int)land.lnd_type].l_name, land.lnd_uid);
167                     if (land.lnd_nxlight) {
168                         snxtitem_all(&ni, EF_PLANE);
169                         while (nxtitem(&ni, (s_char *)&plane)) {
170                             if (plane.pln_land == land.lnd_uid) {
171                                 pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
172                                    plane.pln_tech,
173                                    plane.pln_effic,
174                                    plchr[(int)plane.pln_type].pl_name,
175                                    plane.pln_uid);
176                                 if (plane.pln_nuketype != (s_char)-1) {
177                                     pr("(%s)",
178                                        nchr[(int)plane.pln_nuketype].
179                                        n_name);
180                                 }
181                             }
182                         }
183                     }
184                 }
185             }
186         }
187         getsect(sp->shp_x, sp->shp_y, &sect);
188         if (sect.sct_type != SCT_WATER)
189             pr(" in a %s %s", cname(sect.sct_own),
190                dchr[sect.sct_type].d_name);
191         else
192             pr(" at sea");
193         break;
194     case EF_LAND:
195         lp = &tgp->lnd;
196         tp->trd_owner = lp->lnd_own;
197         pr("(%3d)  tech %d %d%% %s [",
198            tp->trd_owner,
199            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
200         for (i = 1; i <= I_MAX; ++i) {
201             if (lp->lnd_item[i])
202                 pr("%c:%d ", ichr[i].i_mnem, lp->lnd_item[i]);
203         }
204         pr("] #%d", tp->trd_unitid);
205         if (opt_SHOWPLANE) {
206             snxtitem_all(&ni, EF_PLANE);
207             while (nxtitem(&ni, (s_char *)&plane)) {
208                 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
209                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
210                        plane.pln_tech,
211                        plane.pln_effic,
212                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
213                     if (plane.pln_nuketype != (s_char)-1) {
214                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
215                     }
216                 }
217             }
218         }
219         getsect(lp->lnd_x, lp->lnd_y, &sect);
220         break;
221     case EF_PLANE:
222         pp = &tgp->pln;
223         tp->trd_owner = pp->pln_own;
224         pr("(%3d)  tech %d %d%% %s #%d",
225            tp->trd_owner,
226            pp->pln_tech,
227            pp->pln_effic,
228            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
229         if (pp->pln_nuketype != (s_char)-1) {
230             pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
231         }
232         break;
233     default:
234         pr("flaky unit type %d", tp->trd_type);
235         break;
236     }
237     return 1;
238 }
239
240 int
241 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
242 {
243     if (!ef_read(tp->trd_type, tp->trd_unitid, (s_char *)tgp))
244         return 0;
245     return 1;
246 }
247
248 long
249 get_couval(int cnum)
250 {
251     struct sctstr *sp;
252     register int j, k, val;
253     long secttot = 0;
254
255     for (j = 0; NULL != (sp = getsectid(j)); j++) {
256         if (sp->sct_own != cnum)
257             continue;
258         secttot += (long)(dchr[sp->sct_type].d_value *
259                           ((float)sp->sct_effic + 100.0));
260         for (k = 0; ichr[k].i_name; k++) {
261             if (ichr[k].i_value == 0 || ichr[k].i_vtype == 0)
262                 continue;
263             val = sp->sct_item[ichr[k].i_vtype];
264             secttot += val * ichr[k].i_value;
265         }
266     }
267     return secttot;
268 }
269
270 long
271 get_outstand(int cnum)
272 {
273     struct lonstr loan;
274     int j;
275     long loantot = 0;
276
277     for (j = 0; getloan(j, &loan); j++) {
278         if (loan.l_status == LS_FREE)
279             continue;
280         if (loan.l_lonee == cnum)
281             loantot += loan.l_amtdue;
282     }
283     return loantot;
284 }
285
286 struct ichrstr *
287 whichitem(char p)
288 {
289     register int i;
290
291     if (p == 0)
292         return 0;
293     for (i = 1; ichr[i].i_mnem != 0; i++)
294         if (p == ichr[i].i_mnem)
295             return (struct ichrstr *)(&ichr[i]);
296     pr("Unrecognized item \"%c\"\n", p);
297     return 0;
298 }