]> git.pond.sub.org Git - empserver/blob - src/lib/subs/show.c
Update known contributors comments
[empserver] / src / lib / subs / show.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  *     Ron Koenderink, 2005-2009
35  *     Markus Armbruster, 2006-2008
36  */
37
38 #include <config.h>
39
40 #include <math.h>
41 #include "file.h"
42 #include "game.h"
43 #include "item.h"
44 #include "land.h"
45 #include "nat.h"
46 #include "news.h"
47 #include "nuke.h"
48 #include "optlist.h"
49 #include "plane.h"
50 #include "player.h"
51 #include "product.h"
52 #include "prototypes.h"
53 #include "sect.h"
54 #include "server.h"
55 #include "ship.h"
56
57 static char *fmttime2822(time_t);
58
59 struct look_list {
60     union {
61         struct lchrstr *lp;
62         struct plchrstr *pp;
63         struct mchrstr *mp;
64     } l_u;
65     int tech;
66 };
67
68 /*
69  * Change this if there are ever more than 200 ships, plane or land
70  * unit types.
71  */
72 static struct look_list lookup_list[200];
73 static int lookup_list_cnt = 0;
74
75 static void
76 sort_lookup_list(void)
77 {
78     struct natstr *np = getnatp(player->cnum);
79     struct look_list tmp;
80     int i;
81     int j;
82
83     if (!(np->nat_flags & NF_TECHLISTS))
84         return;
85     for (i = 0; i < lookup_list_cnt; i++) {
86         for (j = i; j < lookup_list_cnt; j++) {
87             if (lookup_list[j].tech < lookup_list[i].tech) {
88                 tmp = lookup_list[j];
89                 lookup_list[j] = lookup_list[i];
90                 lookup_list[i] = tmp;
91             }
92         }
93     }
94 }
95
96 static void
97 make_new_list(int tlev, int type)
98 {
99     struct plchrstr *pp;
100     struct lchrstr *lp;
101     struct mchrstr *mp;
102
103     lookup_list_cnt = 0;
104     if (type == EF_PLANE) {
105         for (pp = plchr; pp->pl_name; pp++) {
106             if (pp->pl_tech > tlev)
107                 continue;
108             lookup_list[lookup_list_cnt].l_u.pp = pp;
109             lookup_list[lookup_list_cnt].tech = pp->pl_tech;
110             lookup_list_cnt++;
111         }
112     } else if (type == EF_SHIP) {
113         for (mp = mchr; mp->m_name; mp++) {
114             if (mp->m_tech > tlev)
115                 continue;
116             lookup_list[lookup_list_cnt].l_u.mp = mp;
117             lookup_list[lookup_list_cnt].tech = mp->m_tech;
118             lookup_list_cnt++;
119         }
120     } else if (type == EF_LAND) {
121         for (lp = lchr; lp->l_name; lp++) {
122             if (lp->l_tech > tlev)
123                 continue;
124             lookup_list[lookup_list_cnt].l_u.lp = lp;
125             lookup_list[lookup_list_cnt].tech = lp->l_tech;
126             lookup_list_cnt++;
127         }
128     } else
129         return;
130
131     sort_lookup_list();
132 }
133
134 void
135 show_bridge(int tlev)
136 {
137     if (tlev < buil_bt)
138         return;
139     pr("Bridges require %g tech, %d hcm, 0 workers,\n",
140        buil_bt, buil_bh);
141     pr("%d available workforce, and cost $%g\n",
142        (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100,
143        buil_bc);
144 }
145
146 void
147 show_tower(int tlev)
148 {
149     if (tlev < buil_tower_bt)
150         return;
151     pr("Bridge Towers require %g tech, %d hcm, 0 workers,\n",
152        buil_tower_bt, buil_tower_bh);
153     pr("%d available workforce, and cost $%g\n",
154        (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100,
155        buil_tower_bc);
156 }
157
158 void
159 show_nuke_stats(int tlev)
160 {
161     show_nuke_capab(tlev);
162 }
163
164 void
165 show_nuke_build(int tlev)
166 {
167     struct nchrstr *np;
168     int avail;
169
170     pr("%13s lcm hcm  oil  rad avail tech res $\n", "");
171
172     for (np = nchr; np->n_name; np++) {
173         avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
174         if (np->n_tech > tlev)
175             continue;
176         pr("%-13.13s %3d %3d %4d %4d %5d %4d %3.0f $%6d\n",
177            np->n_name, np->n_lcm, np->n_hcm, np->n_oil,
178            np->n_rad, avail, np->n_tech,
179            drnuke_const > MIN_DRNUKE_CONST ?
180                 ceil(np->n_tech * drnuke_const) : 0.0,
181            np->n_cost);
182     }
183 }
184
185 void
186 show_nuke_capab(int tlev)
187 {
188     struct nchrstr *np;
189     int i, j;
190     char *p;
191
192     pr("%13s blst dam lbs tech res $%7s abilities\n", "", "");
193
194     for (np = nchr; np->n_name; np++) {
195         if (np->n_tech > tlev)
196             continue;
197         pr("%-13.13s %4d %3d %3d %4d %3.0f $%7d ",
198            np->n_name, np->n_blast, np->n_dam,
199            np->n_weight, np->n_tech,
200            drnuke_const > MIN_DRNUKE_CONST ?
201                 ceil(np->n_tech * drnuke_const) : 0.0,
202            np->n_cost);
203         for (i = j = 0; i < 32; i++) {
204             if (!(np->n_flags & bit(i)))
205                 continue;
206             if (NULL != (p = symbol_by_value(bit(i), nuke_chr_flags))) {
207                 if (j++ > 0)
208                     pr(" ");
209                 pr("%s", p);
210             }
211         }
212         pr("\n");
213     }
214 }
215
216 void
217 show_ship_build(int tlev)
218 {
219     struct mchrstr *mp;
220     int n;
221
222     pr("%25s lcm hcm avail tech $\n", "");
223     make_new_list(tlev, EF_SHIP);
224     for (n = 0; n < lookup_list_cnt; n++) {
225         mp = (struct mchrstr *)lookup_list[n].l_u.mp;
226         /* Can't show trade ships unless it's turned on */
227         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
228             continue;
229
230         pr("%-25.25s %3d %3d %5d %4d $%d\n",
231            mp->m_name, mp->m_lcm, mp->m_hcm,
232            SHP_BLD_WORK(mp->m_lcm, mp->m_hcm), mp->m_tech, mp->m_cost);
233     }
234 }
235
236 void
237 show_ship_stats(int tlev)
238 {
239     struct mchrstr *mp;
240     int scount;
241
242     pr("%25s      s  v  s  r  f  l  p  h  x", "");
243     pr("\n");
244
245     pr("%25s      p  i  p  n  i  n  l  e  p", "");
246     pr("\n");
247
248     pr("%25s def  d  s  y  g  r  d  n  l  l", "");
249     pr("\n");
250
251
252     make_new_list(tlev, EF_SHIP);
253     for (scount = 0; scount < lookup_list_cnt; scount++) {
254         mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
255         /* Can't show trade ships unless it's turned on */
256         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
257             continue;
258
259         pr("%-25.25s %3d %2d %2d %2d %2d %2d ",
260            mp->m_name, m_armor(mp, tlev), m_speed(mp, tlev),
261            m_visib(mp, tlev), mp->m_vrnge,
262            m_frnge(mp, tlev), m_glim(mp, tlev));
263
264         pr("%2d ", mp->m_nland);
265         pr("%2d ", mp->m_nplanes);
266         pr("%2d ", mp->m_nchoppers);
267         pr("%2d ", mp->m_nxlight);
268         pr("\n");
269     }
270 }
271
272 void
273 show_ship_capab(int tlev)
274 {
275     struct mchrstr *mp;
276     i_type i;
277     int j;
278     int scount;
279     int n;
280     char *p;
281
282     pr("%25s cargos & capabilities\n", "");
283
284     make_new_list(tlev, EF_SHIP);
285     for (scount = 0; scount < lookup_list_cnt; scount++) {
286         mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
287         /* Can't show trade ships unless it's turned on */
288         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
289             continue;
290
291         pr("%-25.25s ", mp->m_name);
292
293         for (i = I_NONE + 1; i <= I_MAX; ++i)
294             if (mp->m_item[i])
295                 pr(" %d%c", mp->m_item[i], ichr[i].i_mnem);
296         pr(" ");
297         for (j = n = 0; j < 32; j++) {
298             if (!(mp->m_flags & bit(j)))
299                 continue;
300             if (NULL != (p = symbol_by_value(bit(j), ship_chr_flags))) {
301                 if (n++ > 0)
302                     pr(" ");
303                 pr("%s", p);
304             }
305         }
306         pr("\n");
307     }
308 }
309
310 void
311 show_plane_stats(int tlev)
312 {
313     struct plchrstr *pp;
314     int pcount;
315
316     pr("%25s acc load att def ran fuel stlth\n", "");
317     make_new_list(tlev, EF_PLANE);
318     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
319         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
320         pr("%-25.25s %3d %4d %3d %3d %3d %4d ",
321            pp->pl_name, pl_acc(pp, tlev), pl_load(pp, tlev),
322            pl_att(pp, tlev), pl_def(pp, tlev), pl_range(pp, tlev),
323            pp->pl_fuel);
324         pr("%4d%% ", pp->pl_stealth);
325         pr("\n");
326     }
327 }
328
329 void
330 show_plane_capab(int tlev)
331 {
332     struct plchrstr *pp;
333     int i;
334     int pcount;
335     int n;
336     char *p;
337
338     pr("%25s capabilities\n", "");
339     make_new_list(tlev, EF_PLANE);
340     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
341         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
342         pr("%-25.25s  ", pp->pl_name);
343
344         for (i = n = 0; i < 32; i++) {
345             if (!(pp->pl_flags & bit(i)))
346                 continue;
347             if (NULL != (p = symbol_by_value(bit(i), plane_chr_flags))) {
348                 if (n++ > 0)
349                     pr(" ");
350                 pr("%s", p);
351             }
352         }
353         pr("\n");
354     }
355 }
356
357 void
358 show_plane_build(int tlev)
359 {
360     struct plchrstr *pp;
361     int pcount;
362
363     pr("%25s lcm hcm crew avail tech $\n", "");
364     make_new_list(tlev, EF_PLANE);
365     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
366         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
367         pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
368            pp->pl_name, pp->pl_lcm,
369            pp->pl_hcm, pp->pl_crew,
370            PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm), pp->pl_tech, pp->pl_cost);
371     }
372 }
373
374 void
375 show_land_build(int tlev)
376 {
377     struct lchrstr *lp;
378     int n;
379
380     pr("%25s lcm hcm guns avail tech $\n", "");
381     make_new_list(tlev, EF_LAND);
382     for (n = 0; n < lookup_list_cnt; n++) {
383         lp = (struct lchrstr *)lookup_list[n].l_u.lp;
384         if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
385             continue;
386         pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
387            lp->l_name, lp->l_lcm,
388            lp->l_hcm,
389            lp->l_gun,
390            LND_BLD_WORK(lp->l_lcm, lp->l_hcm), lp->l_tech, lp->l_cost);
391     }
392 }
393
394 void
395 show_land_capab(int tlev)
396 {
397     struct lchrstr *lcp;
398     int lcount;
399     i_type i;
400     int j, n;
401     char *p;
402
403     pr("%25s capabilities\n", "");
404
405     make_new_list(tlev, EF_LAND);
406     for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
407         lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
408         if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
409             continue;
410
411         pr("%-25s ", lcp->l_name);
412
413         for (i = I_NONE + 1; i <= I_MAX; ++i)
414             if (lcp->l_item[i])
415                 pr(" %d%c", lcp->l_item[i], ichr[i].i_mnem);
416         pr(" ");
417         for (j = n = 0; j < 32; j++) {
418             if (!(lcp->l_flags & bit(j)))
419                 continue;
420             if (NULL != (p = symbol_by_value(bit(j), land_chr_flags))) {
421                 if (n++ > 0)
422                     pr(" ");
423                 pr("%s", p);
424             }
425         }
426         pr("\n");
427     }
428 }
429
430 void
431 show_land_stats(int tlev)
432 {
433     struct lchrstr *lcp;
434     int lcount;
435
436     pr("%25s              s  v  s  r  r  a  f  a  a  x  l\n", "");
437     pr("%25s              p  i  p  a  n  c  i  m  a  p  n\n", "");
438     pr("%25s att def vul  d  s  y  d  g  c  r  m  f  l  d\n", "");
439
440     make_new_list(tlev, EF_LAND);
441     for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
442         lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
443         if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
444             continue;
445
446         pr("%-25s %1.1f %1.1f %3d ",
447            lcp->l_name,
448            l_att(lcp, tlev), l_def(lcp, tlev), l_vul(lcp, tlev));
449         pr("%2d %2d %2d %2d ",
450            l_spd(lcp, tlev), lcp->l_vis, lcp->l_spy, lcp->l_rad);
451         pr("%2d %2d %2d %2d %2d ",
452            l_frg(lcp, tlev), l_acc(lcp, tlev), l_dam(lcp, tlev),
453            lcp->l_ammo, lcp->l_aaf);
454         pr("\n");
455     }
456 }
457
458 void
459 show_sect_build(int foo)
460 {
461     int x, first;
462
463     pr("sector type    cost to des    cost for 1%% eff   lcms for 1%%    hcms for 1%%\n");
464     for (x = 0; dchr[x].d_name; x++) {
465         if (dchr[x].d_mnem == 0)
466             continue;
467         if (dchr[x].d_cost < 0)
468             continue;
469         if ((dchr[x].d_cost > 0) || (dchr[x].d_build != 1) ||
470             (dchr[x].d_lcms > 0) || (dchr[x].d_hcms > 0)) {
471             pr("%-14c %-14d %-17d %-14d %d\n",
472                dchr[x].d_mnem, dchr[x].d_cost, dchr[x].d_build,
473                dchr[x].d_lcms, dchr[x].d_hcms);
474         }
475     }
476     pr("other          0              1                 0              0\n");
477
478     first = 1;
479     for (x = 0; intrchr[x].in_name; x++) {
480         if (!intrchr[x].in_enable)
481             continue;
482         if (first)
483             pr("\nInfrastructure building - adding 1 point of efficiency costs:\n"
484                "       type          lcms    hcms    mobility    $$$$\n");
485         pr("%-20s %4d    %4d    %8d    %4d\n",
486            intrchr[x].in_name, intrchr[x].in_lcms, intrchr[x].in_hcms,
487            intrchr[x].in_mcost, intrchr[x].in_dcost);
488         first = 0;
489     }
490 }
491
492 void
493 show_sect_stats(int foo)
494 {
495     int x;
496     struct natstr *natp = getnatp(player->cnum);
497
498     pr("                        mob cost   max   max   naviga    packing   max\n");
499     pr("  sector type            0%% 100%%   off   def   bility      bonus   pop\n");
500
501     for (x = 0; dchr[x].d_name; x++) {
502         if (dchr[x].d_mnem == 0)
503             continue;
504         pr("%c %-21.21s", dchr[x].d_mnem, dchr[x].d_name);
505         if (dchr[x].d_mob0 < 0)
506             pr("  no way ");
507         else
508             pr(" %3.1f  %3.1f", dchr[x].d_mob0, dchr[x].d_mob1);
509         pr("  %5.2f %5.2f %7.7s %10.10s %5d\n",
510            dchr[x].d_ostr, dchr[x].d_dstr,
511            symbol_by_value(dchr[x].d_nav, sector_navigation),
512            symbol_by_value(dchr[x].d_pkg, packing),
513            max_population(natp->nat_level[NAT_RLEV], x, 100));
514     }
515 }
516
517 void
518 show_sect_capab(int foo)
519 {
520     int x;
521
522     pr("  sector type             product  p.e.\n");
523
524     for (x = 0; dchr[x].d_name; x++) {
525         if (dchr[x].d_mnem == 0 || dchr[x].d_prd < 0)
526             continue;
527         pr("%c %-23s %-7s %4d%%\n",
528            dchr[x].d_mnem, dchr[x].d_name, pchr[dchr[x].d_prd].p_sname,
529            dchr[x].d_peffic);
530     }
531 }
532
533 void
534 show_item(int tlev)
535 {
536     struct ichrstr *ip;
537
538     pr("item value sell lbs    packing     melt  item\n");
539     pr("mnem                in no wh ur bk deno  name\n");
540
541     for (ip = ichr; ip->i_name; ip++) {
542         pr("   %c %5d %4s %3d %2d %2d %2d %2d %2d %4d  %s\n",
543            ip->i_mnem, ip->i_value, ip->i_sell ? "yes" : "no", ip->i_lbs,
544            ip->i_pkg[IPKG], ip->i_pkg[NPKG], ip->i_pkg[WPKG],
545            ip->i_pkg[UPKG], ip->i_pkg[BPKG],
546            ip->i_melt_denom, ip->i_name);
547     }
548 }
549
550 void
551 show_product(int tlev)
552 {
553     struct pchrstr *pp;
554     int i;
555     char *lev;
556
557     pr("product    cost  raw materials  reso dep  level p.e.\n");
558
559     for (pp = pchr; pp->p_sname; pp++) {
560         pr("%7.7s %c  $%-3d ",
561            pp->p_sname,
562            pp->p_type < 0 ? ' ' : ichr[pp->p_type].i_mnem,
563            pp->p_cost);
564         (void)CANT_HAPPEN(MAXPRCON > 3); /* output has only three columns */
565         for (i = 0; i < 3; i++) {
566             if (i < MAXPRCON && pp->p_camt[i]
567                 && pp->p_ctype[i] > I_NONE && pp->p_ctype[i] <= I_MAX)
568                 pr(" %2d%c", pp->p_camt[i], ichr[pp->p_ctype[i]].i_mnem);
569             else
570                 pr("    ");
571         }
572         if (pp->p_nrndx)
573             pr("   %5.5s %3d  ",
574                symbol_by_value(pp->p_nrndx, resources), pp->p_nrdep);
575         else
576             pr("              ");
577         if (pp->p_nlndx < 0)
578             pr("1.0\n");
579         else {
580             lev = symbol_by_value(pp->p_nlndx, level);
581             pr("(%.4s%+d)/(%.4s%+d)\n",
582                lev, -pp->p_nlmin, lev, pp->p_nllag - pp->p_nlmin);
583         }
584     }
585 }
586
587 void
588 show_news(int tlev)
589 {
590     int i, j;
591
592     pr("id category           good will\n");
593     pr("    messsages\n");
594
595     for (i = 1; i < N_MAX_VERB + 1; i++) {
596         if (rpt[i].r_newspage == N_NOTUSED)
597             continue;
598         pr("%-2d %-20.20s %4d\n", rpt[i].r_uid,
599             page_headings[rpt[i].r_newspage].name, rpt[i].r_good_will);
600         for (j = 0; j < NUM_RPTS; j++)
601             pr("    %s\n", rpt[i].r_newstory[j]);
602     }
603 }
604
605 /*
606  * Show update policy and up to N scheduled updates.
607  */
608 void
609 show_updates(int n)
610 {
611     struct gamestr *game = game_tick_tick();
612     int demand = 0;
613     int i;
614
615     pr("%s, Turn %d, ETU %d\n", fmttime2822(time(NULL)),
616        game->game_turn, game->game_tick);
617
618     if (update_time[0]) {
619         if (update_demand == UPD_DEMAND_SCHED) {
620             pr("Demand updates occur according to schedule:\n");
621             demand = 1;
622         } else
623             pr("Updates occur according to schedule:\n");
624         for (i = 0; i < n && update_time[i]; i++)
625             pr("%3d.  %s\n", game->game_turn + i,
626                fmttime2822(update_time[i]));
627         if (update_window) {
628             pr("Updates occur within %d seconds after the scheduled time\n",
629                update_window);
630         }
631     } else
632         pr("There are no updates scheduled.\n");
633
634     if (update_demand == UPD_DEMAND_ASYNC) {
635         pr("Demand updates occur right after the demand is set.\n");
636         if (*update_demandtimes != 0) {
637             pr("Demand updates are allowed during: %s\n",
638                update_demandtimes);
639         }
640         demand = 1;
641     }
642
643     if (demand) {
644         pr("Demand updates require %d country(s) to want one.\n",
645            update_wantmin);
646     }
647
648     if (updates_disabled())
649         pr("\nUPDATES ARE DISABLED!\n");
650 }
651
652 /*
653  * Return T formatted according to RFC 2822.
654  * The return value is statically allocated and overwritten on
655  * subsequent calls.
656  */
657 static char *
658 fmttime2822(time_t t)
659 {
660     static char buf[32];
661 #if defined(_WIN32)
662     size_t n;
663     int nn;
664     TIME_ZONE_INFORMATION tzi;
665     long time_offset;
666     struct tm *time;
667
668     time = localtime(&t);
669
670     n = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S", time);
671     if (CANT_HAPPEN(n == 0)) {
672         buf[0] = 0;
673         return buf;
674     }
675     GetTimeZoneInformation(&tzi);
676     time_offset = -(tzi.Bias +
677         (time->tm_isdst ? tzi.DaylightBias : tzi.StandardBias));
678
679     nn = _snprintf(buf + n, sizeof(buf) - n, " %+03d%02d",
680         time_offset/60, abs(time_offset) % 60);
681     if (CANT_HAPPEN(nn <= 0 || nn + n >= sizeof(buf)))
682         buf[0] = 0;
683 #else
684     size_t n = strftime(buf, sizeof(buf), "%a, %d %b %Y %T %z",
685                      localtime(&t));
686     if (CANT_HAPPEN(n == 0))
687         buf[0] = 0;
688 #endif
689     return buf;
690 }