]> git.pond.sub.org Git - empserver/blob - src/lib/subs/show.c
(intmatch): Assumes that struct size is a multiple of sizeof(int), out
[empserver] / src / lib / subs / show.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  show.c: General show routines
29  * 
30  *  Known contributors to this file:
31  *     Julian Onions, 1988
32  *     Jeff Bailey, 1990
33  *     Steve McClure, 1996
34  */
35
36 /*
37  * general routines that are callable to give info on things.
38  * currently, planes, ships, nukes and bridges. Tanks & regiments one day?
39  *
40  * Added nuke_flags to be consistent. Jeff Bailey 12/15/90
41  *                                    (bailey@mcs.kent.edu)
42  */
43
44 #include <math.h>
45 #include "misc.h"
46 #include "player.h"
47 #include "nuke.h"
48 #include "ship.h"
49 #include "land.h"
50 #include "item.h"
51 #include "plane.h"
52 #include "sect.h"
53 #include "optlist.h"
54 #include "file.h"
55 #include "nat.h"
56 #include "prototypes.h"
57
58 struct look_list {
59     union {
60         struct lchrstr *lp;
61         struct plchrstr *pp;
62         struct mchrstr *mp;
63     } l_u;
64     int tech;
65 } lookup_list[200];             /* Change this if there are ever more than 200 planes, ships
66                                    or land units. */
67 static int lookup_list_cnt = 0;
68
69 static void
70 sort_lookup_list(void)
71 {
72     struct natstr *np = getnatp(player->cnum);
73     struct look_list tmp;
74     int i;
75     int j;
76
77     if (!(np->nat_flags & NF_TECHLISTS))
78         return;
79     for (i = 0; i < lookup_list_cnt; i++) {
80         for (j = i; j < lookup_list_cnt; j++) {
81             if (lookup_list[j].tech < lookup_list[i].tech) {
82                 tmp = lookup_list[j];
83                 lookup_list[j] = lookup_list[i];
84                 lookup_list[i] = tmp;
85             }
86         }
87     }
88 }
89
90 static void
91 make_new_list(int tlev, int type)
92 {
93     struct plchrstr *pp;
94     struct lchrstr *lp;
95     struct mchrstr *mp;
96     int count;
97
98     lookup_list_cnt = 0;
99     if (type == EF_PLANE) {
100         for (pp = plchr, count = 0; count < pln_maxno; count++, pp++) {
101             if (pp->pl_tech > tlev)
102                 continue;
103             if (pp->pl_name == 0 || pp->pl_name[0] == '\0')
104                 continue;
105             lookup_list[lookup_list_cnt].l_u.pp = pp;
106             lookup_list[lookup_list_cnt].tech = pp->pl_tech;
107             lookup_list_cnt++;
108         }
109     } else if (type == EF_SHIP) {
110         for (mp = mchr, count = 0; count < shp_maxno; count++, mp++) {
111             if (mp->m_tech > tlev)
112                 continue;
113             if (mp->m_name == 0 || mp->m_name[0] == '\0')
114                 continue;
115             lookup_list[lookup_list_cnt].l_u.mp = mp;
116             lookup_list[lookup_list_cnt].tech = mp->m_tech;
117             lookup_list_cnt++;
118         }
119     } else if (type == EF_LAND) {
120         for (lp = lchr, count = 0; count < lnd_maxno; count++, lp++) {
121             if (lp->l_tech > tlev)
122                 continue;
123             if (lp->l_name == 0 || lp->l_name[0] == '\0')
124                 continue;
125             lookup_list[lookup_list_cnt].l_u.lp = lp;
126             lookup_list[lookup_list_cnt].tech = lp->l_tech;
127             lookup_list_cnt++;
128         }
129     } else
130         return;
131
132     sort_lookup_list();
133 }
134
135 static char *
136 lookup(int key, struct lookup *table)
137 {
138     int i;
139
140     for (i = 0; table[i].value; i++)
141         if (key == table[i].key)
142             return table[i].value;
143
144     return NULL;
145 }
146
147 void
148 show_bridge(int tlev)
149 {
150     if (tlev < buil_bt)
151         return;
152     pr("Bridges require %g tech,", buil_bt);
153     if (!opt_NO_HCMS)
154         pr(" %d hcm,", buil_bh);
155     else if (!opt_NO_LCMS)
156         pr(" %d lcm,", buil_bh);
157     pr(" %d workers,\n", 0);
158     pr("%d available workforce, and cost $%g\n",
159        (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100,
160        buil_bc);
161 }
162
163 void
164 show_tower(int tlev)
165 {
166     if (tlev < buil_tower_bt)
167         return;
168     pr("Bridge Towers require %g tech,", buil_tower_bt);
169     if (!opt_NO_HCMS)
170         pr(" %d hcm,", buil_tower_bh);
171     else if (!opt_NO_LCMS)
172         pr(" %d lcm,", buil_tower_bh);
173     pr(" %d workers,\n", 0);
174     pr("%d available workforce, and cost $%g\n",
175        (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100,
176        buil_tower_bc);
177 }
178
179 void
180 show_nuke_stats(int tlev)
181 {
182     show_nuke_capab(tlev);
183 }
184
185 void
186 show_nuke_build(int tlev)
187 {
188     struct nchrstr *np;
189     int n;
190     int avail;
191
192     if (opt_DRNUKE)
193         pr("%13s lcm hcm  oil  rad avail tech res $\n", "");
194     else
195         pr("%13s lcm hcm  oil  rad avail tech $\n", "");
196
197     if (opt_NONUKES)
198         return;
199     for (np = nchr, n = 0; n < N_MAXNUKE; np++, n++) {
200         avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
201         if (np->n_tech > tlev)
202             continue;
203         if (np->n_name == 0 || np->n_name[0] == '\0')
204             continue;
205         if (opt_NEUTRON == 0 && (np->n_flags & N_NEUT))
206             continue;
207         if (opt_DRNUKE)
208             pr("%-13.13s %3d %3d %4d %4d %5d %4d %3d $%6d\n",
209                np->n_name, np->n_lcm, np->n_hcm, np->n_oil,
210                np->n_rad, avail, np->n_tech,
211                (int)(np->n_tech * drnuke_const) + 1, np->n_cost);
212         else                    /* not DRNUKE */
213             pr("%-13.13s %3d %3d %4d %4d %5d %4d $%6d\n",
214                np->n_name, np->n_lcm, np->n_hcm, np->n_oil,
215                np->n_rad, avail, np->n_tech, np->n_cost);
216     }
217 }
218
219 void
220 show_nuke_capab(int tlev)
221 {
222     struct nchrstr *np;
223     int i, j, n;
224     char *p;
225
226     if (opt_DRNUKE)
227         pr("%13s blst dam lbs tech res $%7s abilities\n", "", "");
228     else
229         pr("%13s blst dam lbs tech $%7s abilities\n", "", "");
230
231     if (opt_NONUKES)
232         return;
233     for (np = nchr, n = 0; n < N_MAXNUKE; np++, n++) {
234         if (np->n_tech > tlev)
235             continue;
236         if (opt_NEUTRON == 0 && (np->n_flags & N_NEUT))
237             continue;
238         if (np->n_name == 0 || np->n_name[0] == '\0')
239             continue;
240         if (opt_DRNUKE)
241             pr("%-13.13s %4d %3d %3d %4d %3d $%7d ",
242                np->n_name, np->n_blast, np->n_dam,
243                np->n_weight, np->n_tech,
244                (int)(np->n_tech * drnuke_const) + 1, np->n_cost);
245         else                    /* not DRNUKE */
246             pr("%-13.13s %4d %3d %3d %4d $%7d ",
247                np->n_name, np->n_blast, np->n_dam,
248                np->n_weight, np->n_tech, np->n_cost);
249
250         for (i = j = 0; i < 32; i++) {
251             if (!(np->n_flags & bit(i)))
252                 continue;
253             if (NULL != (p = lookup(bit(i), nuke_chr_flags))) {
254                 if (j++ > 0)
255                     pr(" ");
256                 pr(p);
257             }
258         }
259         pr("\n");
260     }
261 }
262
263 void
264 show_ship_build(int tlev)
265 {
266     struct mchrstr *mp;
267     int n;
268
269     pr("%25s lcm hcm avail tech $\n", "");
270     make_new_list(tlev, EF_SHIP);
271     for (n = 0; n < lookup_list_cnt; n++) {
272         mp = (struct mchrstr *)lookup_list[n].l_u.mp;
273         /* Can't show trade ships unless it's turned on */
274         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
275             continue;
276
277         pr("%-25.25s %3d %3d %5d %4d $%d\n",
278            mp->m_name, mp->m_lcm, mp->m_hcm,
279            SHP_BLD_WORK(mp->m_lcm, mp->m_hcm), mp->m_tech, mp->m_cost);
280     }
281 }
282
283 void
284 show_ship_stats(int tlev)
285 {
286     struct mchrstr *mp;
287     int scount;
288     int techdiff;
289
290     pr("%25s      s  v  s  r  f  l  p", "");
291     pr("  h");
292     pr("  x");
293     if (opt_FUEL)
294         pr("  fuel");
295     pr("\n");
296
297     pr("%25s      p  i  p  n  i  n  l", "");
298     pr("  e");
299     pr("  p");
300     if (opt_FUEL)
301         pr("   c/u");
302     pr("\n");
303
304     pr("%25s def  d  s  y  g  r  d  n", "");
305     pr("  l");
306     pr("  l");
307     if (opt_FUEL)
308         pr("      ");
309     pr("\n");
310
311
312     make_new_list(tlev, EF_SHIP);
313     for (scount = 0; scount < lookup_list_cnt; scount++) {
314         mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
315         /* Can't show trade ships unless it's turned on */
316         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
317             continue;
318
319         techdiff = (int)(tlev - mp->m_tech);
320         pr("%-25.25s %3d %2d %2d %2d %2d %2d ",
321            mp->m_name,
322            (short)SHP_DEF(mp->m_armor, techdiff),
323            (short)SHP_SPD(mp->m_speed, techdiff),
324            (short)SHP_VIS(mp->m_visib, techdiff),
325            mp->m_vrnge,
326            (short)SHP_RNG(mp->m_frnge, techdiff),
327            (short)SHP_FIR(mp->m_glim, techdiff));
328
329         pr("%2d ", mp->m_nland);
330         pr("%2d ", mp->m_nplanes);
331         pr("%2d ", mp->m_nchoppers);
332         pr("%2d ", mp->m_nxlight);
333         if (opt_FUEL)
334             pr("%3d/%1d ", mp->m_fuelc, mp->m_fuelu);
335         pr("\n");
336     }
337 }
338
339 void
340 show_ship_capab(int tlev)
341 {
342     struct mchrstr *mp;
343     i_type i;
344     int j;
345     int scount;
346     int n;
347     char *p;
348
349     pr("%25s cargos & capabilities\n", "");
350
351     make_new_list(tlev, EF_SHIP);
352     for (scount = 0; scount < lookup_list_cnt; scount++) {
353         mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
354         /* Can't show trade ships unless it's turned on */
355         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
356             continue;
357
358         pr("%-25.25s ", mp->m_name);
359
360         for (i = I_NONE + 1; i <= I_MAX; ++i)
361             if (mp->m_item[i])
362                 pr(" %d%c", mp->m_item[i], ichr[i].i_mnem);
363         pr(" ");
364         for (j = n = 0; j < 32; j++) {
365             if (!(mp->m_flags & bit(j)))
366                 continue;
367             if (NULL != (p = lookup(bit(j), ship_chr_flags))) {
368                 if (n++ > 0)
369                     pr(" ");
370                 pr(p);
371             }
372         }
373         pr("\n");
374     }
375 }
376
377 void
378 show_plane_stats(int tlev)
379 {
380     struct plchrstr *pp;
381     int pcount;
382
383     pr("%25s acc load att def ran fuel stlth\n", "");
384     make_new_list(tlev, EF_PLANE);
385     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
386         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
387         pr("%-25.25s %3d %4d %3d %3d %3d %4d ",
388            pp->pl_name,
389            (int)PLN_ACC(pp->pl_acc, (int)(tlev - pp->pl_tech)),
390            (int)PLN_LOAD(pp->pl_load, (int)(tlev - pp->pl_tech)),
391            (int)PLN_ATTDEF(pp->pl_att, (int)(tlev - pp->pl_tech)),
392            (int)PLN_ATTDEF(pp->pl_def, (int)(tlev - pp->pl_tech)),
393            (int)PLN_RAN(pp->pl_range, (int)(tlev - pp->pl_tech)),
394            pp->pl_fuel);
395         pr("%4d%% ", pp->pl_stealth);
396         pr("\n");
397     }
398 }
399
400 void
401 show_plane_capab(int tlev)
402 {
403     struct plchrstr *pp;
404     int i;
405     int pcount;
406     int n;
407     char *p;
408
409     pr("%25s capabilities\n", "");
410     make_new_list(tlev, EF_PLANE);
411     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
412         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
413         pr("%-25.25s  ", pp->pl_name);
414
415         for (i = n = 0; i < 32; i++) {
416             if (!(pp->pl_flags & bit(i)))
417                 continue;
418             if (NULL != (p = lookup(bit(i), plane_chr_flags))) {
419                 if (n++ > 0)
420                     pr(" ");
421                 pr(p);
422             }
423         }
424         pr("\n");
425     }
426 }
427
428 void
429 show_plane_build(int tlev)
430 {
431     struct plchrstr *pp;
432     int pcount;
433
434     pr("%25s lcm hcm crew avail tech $\n", "");
435     make_new_list(tlev, EF_PLANE);
436     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
437         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
438         pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
439            pp->pl_name, pp->pl_lcm,
440            pp->pl_hcm, pp->pl_crew,
441            PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm), pp->pl_tech, pp->pl_cost);
442     }
443 }
444
445 void
446 show_land_build(int tlev)
447 {
448     struct lchrstr *lp;
449     int n;
450
451     pr("%25s lcm hcm guns avail tech $\n", "");
452     make_new_list(tlev, EF_LAND);
453     for (n = 0; n < lookup_list_cnt; n++) {
454         lp = (struct lchrstr *)lookup_list[n].l_u.lp;
455         if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
456             continue;
457         pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
458            lp->l_name, lp->l_lcm,
459            lp->l_hcm,
460            lp->l_gun,
461            LND_BLD_WORK(lp->l_lcm, lp->l_hcm), lp->l_tech, lp->l_cost);
462     }
463 }
464
465 void
466 show_land_capab(int tlev)
467 {
468     struct lchrstr *lcp;
469     int lcount;
470     i_type i;
471     int j, n;
472     char *p;
473
474     pr("%25s capabilities\n", "");
475
476     make_new_list(tlev, EF_LAND);
477     for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
478         lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
479         if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
480             continue;
481
482         pr("%-25s ", lcp->l_name);
483
484         for (i = I_NONE + 1; i <= I_MAX; ++i)
485             if (lcp->l_item[i])
486                 pr(" %d%c", lcp->l_item[i], ichr[i].i_mnem);
487         pr(" ");
488         for (j = n = 0; j < 32; j++) {
489             if (!(lcp->l_flags & bit(j)))
490                 continue;
491             if (NULL != (p = lookup(bit(j), land_chr_flags))) {
492                 if (n++ > 0)
493                     pr(" ");
494                 pr(p);
495             }
496         }
497         pr("\n");
498     }
499 }
500
501 void
502 show_land_stats(int tlev)
503 {
504     struct lchrstr *lcp;
505     int lcount;
506     int ourtlev;
507
508     pr("%25s              s  v  s  r  r  a  f  a  a        x  l\n", "");
509     pr("%25s              p  i  p  a  n  c  i  m  a  f  f  p  n\n", "");
510     pr("%25s att def vul  d  s  y  d  g  c  r  m  f  c  u  l  d\n", "");
511
512     make_new_list(tlev, EF_LAND);
513     for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
514         lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
515         if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
516             continue;
517
518         ourtlev = (int)(tlev - lcp->l_tech);
519         pr("%-25s %1.1f %1.1f %3d ",
520            lcp->l_name, (float)LND_ATTDEF(lcp->l_att, ourtlev),
521            (float)LND_ATTDEF(lcp->l_def, ourtlev),
522            (int)LND_VUL(lcp->l_vul, ourtlev));
523         pr("%2d %2d %2d %2d ",
524            (int)LND_SPD(lcp->l_spd, ourtlev),
525            (int)LND_VIS(lcp->l_vis, ourtlev),
526            (int)LND_SPY(lcp->l_spy, ourtlev),
527            (int)LND_RAD(lcp->l_rad, ourtlev));
528         pr("%2d %2d %2d %2d %2d ",
529            (int)LND_FRG(lcp->l_frg, ourtlev),
530            (int)LND_ACC(lcp->l_acc, ourtlev),
531            (int)LND_DAM(lcp->l_dam, ourtlev),
532            (int)LND_AMM(lcp->l_ammo, lcp->l_dam, ourtlev),
533            (int)LND_AAF(lcp->l_aaf, ourtlev));
534         pr("%2d %2d %2d %2d ",
535            (int)LND_FC(lcp->l_fuelc, ourtlev),
536            (int)LND_FU(lcp->l_fuelu, ourtlev),
537            (int)LND_XPL(lcp->l_nxlight, ourtlev),
538            (int)LND_MXL(lcp->l_mxland, ourtlev));
539
540         pr("\n");
541     }
542 }
543
544 void
545 show_sect_build(int foo)
546 {
547     int x, first = 1;
548
549     for (x = 0; x <= SCT_MAXDEF; x++) {
550         if (dchr[x].d_mnem == 0)
551             continue;
552         if (dchr[x].d_cost < 0)
553             continue;
554         if ((dchr[x].d_cost > 0) || (dchr[x].d_build != 1) ||
555             (dchr[x].d_lcms > 0) || (dchr[x].d_hcms > 0)) {
556             if (first) {
557                 pr("sector type    cost to des    cost for 1%% eff   lcms for 1%%    hcms for 1%%\n");
558                 first = 0;
559             }
560             pr("%-14c %-14d %-17d %-14d %d\n",
561                dchr[x].d_mnem, dchr[x].d_cost, dchr[x].d_build,
562                dchr[x].d_lcms, dchr[x].d_hcms);
563         }
564     }
565     pr("\n");
566     pr("Infrastructure building - adding 1 point of efficiency costs:\n");
567     pr("       type          lcms    hcms    mobility    $$$$\n");
568     for (x = 0; intrchr[x].in_name; x++) {
569         pr("%-20s %4d    %4d    %8d    %4d\n", intrchr[x].in_name,
570            intrchr[x].in_lcms, intrchr[x].in_hcms,
571            intrchr[x].in_mcost, intrchr[x].in_dcost);
572     }
573 }
574
575 void
576 show_sect_stats(int foo)
577 {
578     int x, first = 1;
579     struct natstr *natp;
580
581     natp = getnatp(player->cnum);
582     for (x = 0; x <= SCT_MAXDEF; x++) {
583         if (dchr[x].d_mnem == 0)
584             continue;
585         if (first) {
586             pr("                        base     max   max   --  packing bonus  --   max\n");
587             pr("  sector type           mcost    off   def   mil  uw civ bar other   pop\n");
588             first = 0;
589         }
590         pr("%c %-23s %3d  %5.2f %5.2f   %3d %3d %3d %3d %5d %5d\n",
591            dchr[x].d_mnem, dchr[x].d_name,
592            dchr[x].d_mcst, dchr[x].d_ostr,
593            dchr[x].d_dstr,
594            ichr[I_MILIT].i_pkg[dchr[x].d_pkg],
595            ichr[I_UW].i_pkg[dchr[x].d_pkg],
596            ichr[I_CIVIL].i_pkg[dchr[x].d_pkg],
597            ichr[I_BAR].i_pkg[dchr[x].d_pkg],
598            ichr[I_LCM].i_pkg[dchr[x].d_pkg],
599            max_population(natp->nat_level[NAT_RLEV], x, 100));
600     }
601 }
602
603 void
604 show_sect_capab(int foo)
605 {
606     int x, first = 1, i, j;
607     char *tmpstr;
608
609     for (x = 0; x <= SCT_MAXDEF; x++) {
610         if ((dchr[x].d_mnem == 0) || (dchr[x].d_prd == 0))
611             continue;
612         if (first) {
613             pr("                                                 --- level ---          reso \n");
614             pr("  sector type             product use1 use2 use3 level min lag eff%% $$$ dep c\n");
615             first = 0;
616         }
617
618         j = dchr[x].d_prd;
619
620         pr("%c %-23s %-7s ", dchr[x].d_mnem, dchr[x].d_name,
621            pchr[j].p_sname);
622         (void)CANT_HAPPEN(MAXPRCON > 3); /* output has only three columns */
623         for (i = 0; i < 3; i++) {
624             if (i < MAXPRCON
625                 && pchr[j].p_camt[i]
626                 && pchr[j].p_ctype[i] > I_NONE
627                 && pchr[j].p_ctype[i] <= I_MAX) {
628                 pr("%2d %c ", pchr[j].p_camt[i],
629                    ichr[pchr[j].p_ctype[i]].i_name[0]);
630             } else {
631                 pr("     ");
632             }
633         }
634         switch (pchr[j].p_nlndx) {
635         case NAT_TLEV:
636             tmpstr = "tech";
637             break;
638         case NAT_ELEV:
639             tmpstr = "edu";
640             break;
641         case NAT_RLEV:
642             tmpstr = "res";
643             break;
644         case NAT_HLEV:
645             tmpstr = "hap";
646             break;
647         default:
648             tmpstr = " ";
649             break;
650         }
651         pr("%-5s %3d %3d %4d %3d %3d %c",
652            tmpstr,
653            pchr[j].p_nlmin,
654            pchr[j].p_nllag,
655            pchr[j].p_effic, pchr[j].p_cost, pchr[j].p_nrdep,
656            pchr[j].p_type != I_NONE ? ichr[pchr[j].p_type].i_mnem : ' ');
657
658         pr("\n");
659     }
660 }