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