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