]> git.pond.sub.org Git - empserver/blob - src/lib/subs/show.c
Factor show_capab() out of show_ship_capab() & friends
[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_capab(int, struct symbol *);
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
190     pr("%13s blst dam lbs tech res $%7s abilities\n", "", "");
191
192     for (np = nchr; np->n_name; np++) {
193         if (np->n_tech > tlev)
194             continue;
195         pr("%-13.13s %4d %3d %3d %4d %3.0f $%7d",
196            np->n_name, np->n_blast, np->n_dam,
197            np->n_weight, np->n_tech,
198            drnuke_const > MIN_DRNUKE_CONST ?
199                 ceil(np->n_tech * drnuke_const) : 0.0,
200            np->n_cost);
201         show_capab(np->n_flags, nuke_chr_flags);
202         pr("\n");
203     }
204 }
205
206 void
207 show_ship_build(int tlev)
208 {
209     struct mchrstr *mp;
210     int n;
211
212     pr("%25s lcm hcm avail tech $\n", "");
213     make_new_list(tlev, EF_SHIP);
214     for (n = 0; n < lookup_list_cnt; n++) {
215         mp = (struct mchrstr *)lookup_list[n].l_u.mp;
216         /* Can't show trade ships unless it's turned on */
217         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
218             continue;
219
220         pr("%-25.25s %3d %3d %5d %4d $%d\n",
221            mp->m_name, mp->m_lcm, mp->m_hcm,
222            SHP_BLD_WORK(mp->m_lcm, mp->m_hcm), mp->m_tech, mp->m_cost);
223     }
224 }
225
226 void
227 show_ship_stats(int tlev)
228 {
229     struct mchrstr *mp;
230     int scount;
231
232     pr("%25s      s  v  s  r  f  l  p  h  x", "");
233     pr("\n");
234
235     pr("%25s      p  i  p  n  i  n  l  e  p", "");
236     pr("\n");
237
238     pr("%25s def  d  s  y  g  r  d  n  l  l", "");
239     pr("\n");
240
241
242     make_new_list(tlev, EF_SHIP);
243     for (scount = 0; scount < lookup_list_cnt; scount++) {
244         mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
245         /* Can't show trade ships unless it's turned on */
246         if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
247             continue;
248
249         pr("%-25.25s %3d %2d %2d %2d %2d %2d ",
250            mp->m_name, m_armor(mp, tlev), m_speed(mp, tlev),
251            m_visib(mp, tlev), mp->m_vrnge,
252            m_frnge(mp, tlev), m_glim(mp, tlev));
253
254         pr("%2d ", mp->m_nland);
255         pr("%2d ", mp->m_nplanes);
256         pr("%2d ", mp->m_nchoppers);
257         pr("%2d ", mp->m_nxlight);
258         pr("\n");
259     }
260 }
261
262 void
263 show_ship_capab(int tlev)
264 {
265     struct mchrstr *mp;
266     i_type i;
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
280         for (i = I_NONE + 1; i <= I_MAX; ++i)
281             if (mp->m_item[i])
282                 pr(" %d%c", mp->m_item[i], ichr[i].i_mnem);
283         show_capab(mp->m_flags, ship_chr_flags);
284         pr("\n");
285     }
286 }
287
288 void
289 show_plane_stats(int tlev)
290 {
291     struct plchrstr *pp;
292     int pcount;
293
294     pr("%25s acc load att def ran fuel stlth\n", "");
295     make_new_list(tlev, EF_PLANE);
296     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
297         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
298         pr("%-25.25s %3d %4d %3d %3d %3d %4d ",
299            pp->pl_name, pl_acc(pp, tlev), pl_load(pp, tlev),
300            pl_att(pp, tlev), pl_def(pp, tlev), pl_range(pp, tlev),
301            pp->pl_fuel);
302         pr("%4d%% ", pp->pl_stealth);
303         pr("\n");
304     }
305 }
306
307 void
308 show_plane_capab(int tlev)
309 {
310     struct plchrstr *pp;
311     int pcount;
312
313     pr("%25s capabilities\n", "");
314     make_new_list(tlev, EF_PLANE);
315     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
316         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
317         pr("%-25.25s ", pp->pl_name);
318
319         show_capab(pp->pl_flags, plane_chr_flags);
320         pr("\n");
321     }
322 }
323
324 void
325 show_plane_build(int tlev)
326 {
327     struct plchrstr *pp;
328     int pcount;
329
330     pr("%25s lcm hcm crew avail tech $\n", "");
331     make_new_list(tlev, EF_PLANE);
332     for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
333         pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
334         pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
335            pp->pl_name, pp->pl_lcm,
336            pp->pl_hcm, pp->pl_crew,
337            PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm), pp->pl_tech, pp->pl_cost);
338     }
339 }
340
341 void
342 show_land_build(int tlev)
343 {
344     struct lchrstr *lp;
345     int n;
346
347     pr("%25s lcm hcm guns avail tech $\n", "");
348     make_new_list(tlev, EF_LAND);
349     for (n = 0; n < lookup_list_cnt; n++) {
350         lp = (struct lchrstr *)lookup_list[n].l_u.lp;
351         if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
352             continue;
353         pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
354            lp->l_name, lp->l_lcm,
355            lp->l_hcm,
356            lp->l_gun,
357            LND_BLD_WORK(lp->l_lcm, lp->l_hcm), lp->l_tech, lp->l_cost);
358     }
359 }
360
361 void
362 show_land_capab(int tlev)
363 {
364     struct lchrstr *lcp;
365     int lcount;
366     i_type i;
367
368     pr("%25s capabilities\n", "");
369
370     make_new_list(tlev, EF_LAND);
371     for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
372         lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
373         if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
374             continue;
375
376         pr("%-25s ", lcp->l_name);
377
378         for (i = I_NONE + 1; i <= I_MAX; ++i)
379             if (lcp->l_item[i])
380                 pr(" %d%c", lcp->l_item[i], ichr[i].i_mnem);
381         show_capab(lcp->l_flags, land_chr_flags);
382         pr("\n");
383     }
384 }
385
386 void
387 show_land_stats(int tlev)
388 {
389     struct lchrstr *lcp;
390     int lcount;
391
392     pr("%25s              s  v  s  r  r  a  f  a  a  x  l\n", "");
393     pr("%25s              p  i  p  a  n  c  i  m  a  p  n\n", "");
394     pr("%25s att def vul  d  s  y  d  g  c  r  m  f  l  d\n", "");
395
396     make_new_list(tlev, EF_LAND);
397     for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
398         lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
399         if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
400             continue;
401
402         pr("%-25s %1.1f %1.1f %3d ",
403            lcp->l_name,
404            l_att(lcp, tlev), l_def(lcp, tlev), l_vul(lcp, tlev));
405         pr("%2d %2d %2d %2d ",
406            l_spd(lcp, tlev), lcp->l_vis, lcp->l_spy, lcp->l_rad);
407         pr("%2d %2d %2d %2d %2d ",
408            l_frg(lcp, tlev), l_acc(lcp, tlev), l_dam(lcp, tlev),
409            lcp->l_ammo, lcp->l_aaf);
410         pr("\n");
411     }
412 }
413
414 void
415 show_sect_build(int foo)
416 {
417     int i, first;
418
419     pr("                        desig   build 100%% eff  maint\n"
420        "sector type                 $   lcm  hcm     $      $\n");
421     for (i = 0; dchr[i].d_name; i++) {
422         if (dchr[i].d_mnem == 0)
423             continue;
424         if (dchr[i].d_mob0 < 0)
425             continue;
426         if (dchr[i].d_cost <= 0 && dchr[i].d_build == 1
427             && dchr[i].d_lcms == 0 && dchr[i].d_hcms == 0
428             && dchr[i].d_maint == 0)
429             continue;           /* the usual, skip */
430         pr("%c %-21.21s", dchr[i].d_mnem, dchr[i].d_name);
431         if (dchr[i].d_cost < 0)
432             pr(" can't");
433         else
434             pr(" %5d", dchr[i].d_cost);
435         pr(" %5d%5d %5d  %5d\n",
436            100 * dchr[i].d_lcms,
437            100 * dchr[i].d_hcms,
438            100 * dchr[i].d_build,
439            dchr[i].d_maint * etu_per_update);
440     }
441     pr("any other                   0     0    0   100      0\n");
442
443     first = 1;
444     for (i = 0; intrchr[i].in_name; i++) {
445         if (!intrchr[i].in_enable)
446             continue;
447         if (first)
448             pr("\nInfrastructure building - adding 1 point of efficiency costs:\n"
449                "       type          lcms    hcms    mobility    $$$$\n");
450         pr("%-20s %4d    %4d    %8d    %4d\n",
451            intrchr[i].in_name, intrchr[i].in_lcms, intrchr[i].in_hcms,
452            intrchr[i].in_mcost, intrchr[i].in_dcost);
453         first = 0;
454     }
455 }
456
457 void
458 show_sect_stats(int foo)
459 {
460     int i;
461     struct natstr *natp = getnatp(player->cnum);
462
463     pr("                        mob cost   max   max   naviga    packing   max\n");
464     pr("  sector type            0%% 100%%   off   def   bility      bonus   pop\n");
465
466     for (i = 0; dchr[i].d_name; i++) {
467         if (dchr[i].d_mnem == 0)
468             continue;
469         pr("%c %-21.21s", dchr[i].d_mnem, dchr[i].d_name);
470         if (dchr[i].d_mob0 < 0)
471             pr("  no way ");
472         else
473             pr(" %3.1f  %3.1f", dchr[i].d_mob0, dchr[i].d_mob1);
474         pr("  %5.2f %5.2f %7.7s %10.10s %5d\n",
475            dchr[i].d_ostr, dchr[i].d_dstr,
476            symbol_by_value(dchr[i].d_nav, sector_navigation),
477            symbol_by_value(dchr[i].d_pkg, packing),
478            max_population(natp->nat_level[NAT_RLEV], i, 100));
479     }
480 }
481
482 void
483 show_sect_capab(int foo)
484 {
485     int i;
486
487     pr("  sector type             product  p.e.\n");
488
489     for (i = 0; dchr[i].d_name; i++) {
490         if (dchr[i].d_mnem == 0 || dchr[i].d_prd < 0)
491             continue;
492         pr("%c %-23s %-7s %4d%%\n",
493            dchr[i].d_mnem, dchr[i].d_name, pchr[dchr[i].d_prd].p_sname,
494            dchr[i].d_peffic);
495     }
496 }
497
498 void
499 show_item(int tlev)
500 {
501     struct ichrstr *ip;
502
503     pr("item value sell lbs    packing     melt  item\n");
504     pr("mnem                in no wh ur bk deno  name\n");
505
506     for (ip = ichr; ip->i_name; ip++) {
507         pr("   %c %5d %4s %3d %2d %2d %2d %2d %2d %4d  %s\n",
508            ip->i_mnem, ip->i_value, ip->i_sell ? "yes" : "no", ip->i_lbs,
509            ip->i_pkg[IPKG], ip->i_pkg[NPKG], ip->i_pkg[WPKG],
510            ip->i_pkg[UPKG], ip->i_pkg[BPKG],
511            ip->i_melt_denom, ip->i_name);
512     }
513 }
514
515 void
516 show_product(int tlev)
517 {
518     struct pchrstr *pp;
519     int i;
520     char *lev;
521
522     pr("product    cost  raw materials  reso dep  level p.e.\n");
523
524     for (pp = pchr; pp->p_sname; pp++) {
525         pr("%7.7s %c  $%-3d ",
526            pp->p_sname,
527            pp->p_type < 0 ? ' ' : ichr[pp->p_type].i_mnem,
528            pp->p_cost);
529         (void)CANT_HAPPEN(MAXPRCON > 3); /* output has only three columns */
530         for (i = 0; i < 3; i++) {
531             if (i < MAXPRCON && pp->p_camt[i]
532                 && pp->p_ctype[i] > I_NONE && pp->p_ctype[i] <= I_MAX)
533                 pr(" %2d%c", pp->p_camt[i], ichr[pp->p_ctype[i]].i_mnem);
534             else
535                 pr("    ");
536         }
537         if (pp->p_nrndx)
538             pr("   %5.5s %3d  ",
539                symbol_by_value(pp->p_nrndx, resources), pp->p_nrdep);
540         else
541             pr("              ");
542         if (pp->p_nlndx < 0)
543             pr("1.0\n");
544         else {
545             lev = symbol_by_value(pp->p_nlndx, level);
546             pr("(%.4s%+d)/(%.4s%+d)\n",
547                lev, -pp->p_nlmin, lev, pp->p_nllag - pp->p_nlmin);
548         }
549     }
550 }
551
552 void
553 show_news(int tlev)
554 {
555     int i, j;
556
557     pr("id category           good will\n");
558     pr("    messsages\n");
559
560     for (i = 1; i < N_MAX_VERB + 1; i++) {
561         if (rpt[i].r_newspage == N_NOTUSED)
562             continue;
563         pr("%-2d %-20.20s %4d\n", rpt[i].r_uid,
564            page_headings[rpt[i].r_newspage].name, rpt[i].r_good_will);
565         for (j = 0; j < NUM_RPTS; j++)
566             pr("    %s\n", rpt[i].r_newstory[j]);
567     }
568 }
569
570 /*
571  * Show update policy and up to N scheduled updates.
572  */
573 void
574 show_updates(int n)
575 {
576     struct gamestr *game = game_tick_tick();
577     int demand = 0;
578     int i;
579
580     pr("%s, Turn %d, ETU %d\n", fmttime2822(time(NULL)),
581        game->game_turn, game->game_tick);
582
583     if (update_time[0]) {
584         if (update_demand == UPD_DEMAND_SCHED) {
585             pr("Demand updates occur according to schedule:\n");
586             demand = 1;
587         } else
588             pr("Updates occur according to schedule:\n");
589         for (i = 0; i < n && update_time[i]; i++)
590             pr("%3d.  %s\n", game->game_turn + i,
591                fmttime2822(update_time[i]));
592         if (update_window) {
593             pr("Updates occur within %d seconds after the scheduled time\n",
594                update_window);
595         }
596     } else
597         pr("There are no updates scheduled.\n");
598
599     if (update_demand == UPD_DEMAND_ASYNC) {
600         pr("Demand updates occur right after the demand is set.\n");
601         if (*update_demandtimes != 0) {
602             pr("Demand updates are allowed during: %s\n",
603                update_demandtimes);
604         }
605         demand = 1;
606     }
607
608     if (demand) {
609         pr("Demand updates require %d country(s) to want one.\n",
610            update_wantmin);
611     }
612
613     if (updates_disabled())
614         pr("\nUPDATES ARE DISABLED!\n");
615 }
616
617 /*
618  * Return T formatted according to RFC 2822.
619  * The return value is statically allocated and overwritten on
620  * subsequent calls.
621  */
622 static char *
623 fmttime2822(time_t t)
624 {
625     static char buf[32];
626 #if defined(_WIN32)
627     size_t n;
628     int nn;
629     TIME_ZONE_INFORMATION tzi;
630     long time_offset;
631     struct tm *time;
632
633     time = localtime(&t);
634
635     n = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S", time);
636     if (CANT_HAPPEN(n == 0)) {
637         buf[0] = 0;
638         return buf;
639     }
640     GetTimeZoneInformation(&tzi);
641     time_offset = -(tzi.Bias +
642         (time->tm_isdst ? tzi.DaylightBias : tzi.StandardBias));
643
644     nn = _snprintf(buf + n, sizeof(buf) - n, " %+03d%02d",
645                    time_offset / 60, abs(time_offset) % 60);
646     if (CANT_HAPPEN(nn <= 0 || nn + n >= sizeof(buf)))
647         buf[0] = 0;
648 #else
649     size_t n = strftime(buf, sizeof(buf), "%a, %d %b %Y %T %z",
650                         localtime(&t));
651     if (CANT_HAPPEN(n == 0))
652         buf[0] = 0;
653 #endif
654     return buf;
655 }
656
657 static void
658 show_capab(int flags, struct symbol *table)
659 {
660     int i, n;
661     char *p;
662
663     pr(" ");
664     for (i = n = 0; i < 32; i++) {
665         if (!(flags & bit(i)))
666             continue;
667         if (NULL != (p = symbol_by_value(bit(i), table))) {
668             if (n++ > 0)
669                 pr(" ");
670             pr("%s", p);
671         }
672     }
673 }