]> git.pond.sub.org Git - empserver/blob - src/lib/subs/trdsub.c
Import of Empire 4.2.12
[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,
143                    sp->shp_tech,
144                    sp->shp_effic,
145                    prship(sp));
146
147                 for (i = 0; i < sp->shp_nv; i++) {
148                         pr("%c:%d ",
149                                 ichr[sp->shp_vtype[i] & ~VT_TYPE].i_mnem,
150                                 sp->shp_vamt[i]);
151                 }
152                 pr("] #%d", tp->trd_unitid);
153                 if (opt_SHOWPLANE) {
154                   snxtitem_all(&ni, EF_PLANE);
155                   while (nxtitem(&ni, (s_char *)&plane))
156                     {
157                       if (plane.pln_ship == sp->shp_uid &&
158                           plane.pln_own != 0)
159                         {
160                           pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
161                              plane.pln_tech,
162                              plane.pln_effic,
163                              plchr[(int)plane.pln_type].pl_name,
164                              plane.pln_uid);
165                           if (plane.pln_nuketype != (s_char)-1)
166                             {
167                               pr("(%s)",nchr[(int)plane.pln_nuketype].n_name);
168                             }
169                         }
170                     }
171                   snxtitem_all(&ni, EF_LAND);
172                   while (nxtitem(&ni, (s_char *)&land))
173                     {
174                       if (land.lnd_ship == sp->shp_uid &&
175                           land.lnd_own != 0)
176                         {
177                           pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
178                              land.lnd_tech,
179                              land.lnd_effic,
180                              lchr[(int)land.lnd_type].l_name,
181                              land.lnd_uid);
182                           if (land.lnd_nxlight)
183                             {
184                               snxtitem_all(&ni, EF_PLANE);
185                               while (nxtitem(&ni, (s_char *)&plane))
186                                 {
187                                   if (plane.pln_land == land.lnd_uid)
188                                     {
189                                       pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
190                                          plane.pln_tech,
191                                          plane.pln_effic,
192                                          plchr[(int)plane.pln_type].pl_name,
193                                          plane.pln_uid);
194                                       if (plane.pln_nuketype != (s_char)-1)
195                                         {
196                                           pr("(%s)",
197                                              nchr[(int)plane.pln_nuketype].n_name);
198                                         }
199                                     }
200                                 }
201                             }
202                         }
203                     }
204                 }
205                 getsect(sp->shp_x, sp->shp_y, &sect);
206                 if (sect.sct_type != SCT_WATER)
207                         pr(" in a %s %s", cname(sect.sct_own),
208                                 dchr[sect.sct_type].d_name);
209                 else
210                         pr(" at sea");
211                 break;
212         case EF_LAND:
213                 lp = &tgp->lnd;
214                 tp->trd_owner = lp->lnd_own;
215                 pr("(%3d)  tech %d %d%% %s [",
216                         tp->trd_owner,
217                         lp->lnd_tech,
218                         lp->lnd_effic,
219                         lchr[(int)lp->lnd_type].l_name);
220                 for (i = 0; i < lp->lnd_nv; i++) {
221                         pr("%c:%d ",
222                                 ichr[lp->lnd_vtype[i] & ~VT_TYPE].i_mnem,
223                                 lp->lnd_vamt[i]);
224                 }
225                 pr("] #%d", tp->trd_unitid);
226                 if (opt_SHOWPLANE) {
227                   snxtitem_all(&ni, EF_PLANE);
228                   while (nxtitem(&ni, (s_char *)&plane))
229                     {
230                       if (plane.pln_land == lp->lnd_uid &&
231                           plane.pln_own != 0)
232                         {
233                           pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
234                              plane.pln_tech,
235                              plane.pln_effic,
236                              plchr[(int)plane.pln_type].pl_name,
237                              plane.pln_uid);
238                           if (plane.pln_nuketype != (s_char)-1)
239                             {
240                               pr("(%s)",
241                                  nchr[(int)plane.pln_nuketype].n_name);
242                             }
243                         }
244                     }
245                 }
246                 getsect(lp->lnd_x, lp->lnd_y, &sect);
247                 break;
248         case EF_PLANE:
249                 pp = &tgp->pln;
250                 tp->trd_owner = pp->pln_own;
251                 pr("(%3d)  tech %d %d%% %s #%d",
252                         tp->trd_owner,
253                         pp->pln_tech,
254                         pp->pln_effic,
255                         plchr[(int)pp->pln_type].pl_name,
256                         tp->trd_unitid);
257                 if (pp->pln_nuketype != (s_char)-1) {
258                         pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
259                 }
260                 break;
261         default:
262                 pr("flaky unit type %d", tp->trd_type);
263                 break;
264         }
265         return 1;
266 }
267
268 int
269 trade_getitem(struct trdstr *tp, union trdgenstr *tgp)
270 {
271         if (!ef_read(tp->trd_type, tp->trd_unitid, (s_char *)tgp))
272                 return 0;
273         return 1;
274 }
275
276 long
277 get_couval(int cnum)
278 {
279     struct sctstr *sp;
280     register int j, k, val;
281     long secttot = 0;
282
283     for (j = 0; NULL != (sp = getsectid(j)); j++) {
284         if (sp->sct_own != cnum)
285             continue;
286         secttot += (long)(dchr[sp->sct_type].d_value *
287                           ((float)sp->sct_effic+100.0));
288         for (k = 0; ichr[k].i_name; k++) {
289             if (ichr[k].i_value == 0 || ichr[k].i_vtype == 0)
290                 continue;
291             val = getvar(ichr[k].i_vtype, (s_char *)sp, EF_SECTOR);
292             secttot += val * ichr[k].i_value;
293         }
294     }
295     return secttot;
296 }
297
298 long
299 get_outstand(int cnum)
300 {
301     struct lonstr loan;
302     int j;
303     long loantot = 0;
304
305     for (j = 0; getloan(j, &loan); j++) {
306         if (loan.l_status == LS_FREE)
307             continue;
308         if (loan.l_lonee == cnum)
309             loantot += loan.l_amtdue;
310     }
311     return loantot;
312 }
313
314 struct ichrstr *
315 whichitem(char p)
316 {
317     register int i;
318     
319     if (p == 0)
320         return 0;
321     for (i = 1; ichr[i].i_mnem != 0; i++)
322         if (p == ichr[i].i_mnem)
323             return (struct ichrstr *)(&ichr[i]);
324     pr("Unrecognized item \"%c\"\n", p);
325     return 0;
326 }
327