]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/show.c
show: Extend show item to show the power value
[empserver] / src / lib / subs / show.c
index bc13833075fe4c6ef2ecba019de72e727760d2de..bab48c074763f9677fb935a635d59d28819564b6 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -31,7 +31,7 @@
  *     Jeff Bailey, 1990
  *     Steve McClure, 1996
  *     Ron Koenderink, 2005-2009
- *     Markus Armbruster, 2006-2011
+ *     Markus Armbruster, 2006-2016
  */
 
 #include <config.h>
@@ -57,79 +57,102 @@ static char *fmttime2822(time_t);
 static void show_load(short[]);
 static void show_capab(int, struct symbol *);
 
-struct look_list {
-    union {
-       struct lchrstr *lp;
-       struct plchrstr *pp;
-       struct mchrstr *mp;
-    } l_u;
+struct chr_index {
+    int type;
     int tech;
 };
 
-/*
- * Change this if there are ever more than 200 ships, plane or land
- * unit types.
- */
-static struct look_list lookup_list[200];
-static int lookup_list_cnt = 0;
+static int
+chr_index_cmp(const void *a, const void *b)
+{
+    const struct chr_index *ca = a, *cb = b;
+    if (ca->tech == cb->tech)
+       return ca->type - cb->type;
+    return ca->tech - cb->tech;
+}
 
-static void
-sort_lookup_list(void)
+static int
+make_mchr_index(struct chr_index chridx[], int tlev)
 {
-    struct natstr *np = getnatp(player->cnum);
-    struct look_list tmp;
-    int i;
-    int j;
+    struct natstr *natp = getnatp(player->cnum);
+    int i, n;
 
-    if (!(np->nat_flags & NF_TECHLISTS))
-       return;
-    for (i = 0; i < lookup_list_cnt; i++) {
-       for (j = i; j < lookup_list_cnt; j++) {
-           if (lookup_list[j].tech < lookup_list[i].tech) {
-               tmp = lookup_list[j];
-               lookup_list[j] = lookup_list[i];
-               lookup_list[i] = tmp;
-           }
-       }
+    n = 0;
+    for (i = 0; mchr[i].m_name; i++) {
+       if (!mchr[i].m_name[0])
+           continue;
+       if (mchr[i].m_tech > tlev)
+           continue;
+       chridx[n].type = i;
+       chridx[n].tech = mchr[i].m_tech;
+       n++;
     }
+    if (natp->nat_flags & NF_TECHLISTS)
+       qsort(chridx, n, sizeof(*chridx), chr_index_cmp);
+    return n;
 }
 
-static void
-make_new_list(int tlev, int type)
+static int
+make_plchr_index(struct chr_index chridx[], int tlev)
 {
-    struct plchrstr *pp;
-    struct lchrstr *lp;
-    struct mchrstr *mp;
+    struct natstr *natp = getnatp(player->cnum);
+    int i, n;
 
-    lookup_list_cnt = 0;
-    if (type == EF_PLANE) {
-       for (pp = plchr; pp->pl_name; pp++) {
-           if (pp->pl_tech > tlev)
-               continue;
-           lookup_list[lookup_list_cnt].l_u.pp = pp;
-           lookup_list[lookup_list_cnt].tech = pp->pl_tech;
-           lookup_list_cnt++;
-       }
-    } else if (type == EF_SHIP) {
-       for (mp = mchr; mp->m_name; mp++) {
-           if (mp->m_tech > tlev)
-               continue;
-           lookup_list[lookup_list_cnt].l_u.mp = mp;
-           lookup_list[lookup_list_cnt].tech = mp->m_tech;
-           lookup_list_cnt++;
-       }
-    } else if (type == EF_LAND) {
-       for (lp = lchr; lp->l_name; lp++) {
-           if (lp->l_tech > tlev)
-               continue;
-           lookup_list[lookup_list_cnt].l_u.lp = lp;
-           lookup_list[lookup_list_cnt].tech = lp->l_tech;
-           lookup_list_cnt++;
-       }
-    } else
-       return;
+    n = 0;
+    for (i = 0; plchr[i].pl_name; i++) {
+       if (!plchr[i].pl_name[0])
+           continue;
+       if (plchr[i].pl_tech > tlev)
+           continue;
+       chridx[n].type = i;
+       chridx[n].tech = plchr[i].pl_tech;
+       n++;
+    }
+    if (natp->nat_flags & NF_TECHLISTS)
+       qsort(chridx, n, sizeof(*chridx), chr_index_cmp);
+    return n;
+}
+
+static int
+make_lchr_index(struct chr_index chridx[], int tlev)
+{
+    struct natstr *natp = getnatp(player->cnum);
+    int i, n;
 
-    sort_lookup_list();
+    n = 0;
+    for (i = 0; lchr[i].l_name; i++) {
+       if (!lchr[i].l_name[0])
+           continue;
+       if (lchr[i].l_tech > tlev)
+           continue;
+       chridx[n].type = i;
+       chridx[n].tech = lchr[i].l_tech;
+       n++;
+    }
+    if (natp->nat_flags & NF_TECHLISTS)
+       qsort(chridx, n, sizeof(*chridx), chr_index_cmp);
+    return n;
+}
+
+static int
+make_nchr_index(struct chr_index chridx[], int tlev)
+{
+    struct natstr *natp = getnatp(player->cnum);
+    int i, n;
+
+    n = 0;
+    for (i = 0; nchr[i].n_name; i++) {
+       if (!nchr[i].n_name[0])
+           continue;
+       if (nchr[i].n_tech > tlev)
+           continue;
+       chridx[n].type = i;
+       chridx[n].tech = nchr[i].n_tech;
+       n++;
+    }
+    if (natp->nat_flags & NF_TECHLISTS)
+       qsort(chridx, n, sizeof(*chridx), chr_index_cmp);
+    return n;
 }
 
 void
@@ -165,18 +188,18 @@ show_nuke_stats(int tlev)
 void
 show_nuke_build(int tlev)
 {
+    struct chr_index chridx[sizeof(nchr) / sizeof(*nchr)];
+    int n = make_nchr_index(chridx, tlev);
+    int i;
     struct nchrstr *np;
-    int avail;
 
     pr("%13s lcm hcm  oil  rad avail tech res $\n", "");
 
-    for (np = nchr; np->n_name; np++) {
-       avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
-       if (np->n_tech > tlev)
-           continue;
+    for (i = 0; i < n; i++) {
+       np = &nchr[chridx[i].type];
        pr("%-13.13s %3d %3d %4d %4d %5d %4d %3.0f $%6d\n",
-          np->n_name, np->n_lcm, np->n_hcm, np->n_oil,
-          np->n_rad, avail, np->n_tech,
+          np->n_name, np->n_mat[I_LCM], np->n_mat[I_HCM],
+          np->n_mat[I_OIL], np->n_mat[I_RAD], np->n_bwork, np->n_tech,
           drnuke_const > MIN_DRNUKE_CONST ?
                ceil(np->n_tech * drnuke_const) : 0.0,
           np->n_cost);
@@ -186,13 +209,14 @@ show_nuke_build(int tlev)
 void
 show_nuke_capab(int tlev)
 {
+    struct chr_index chridx[sizeof(nchr) / sizeof(*nchr)];
+    int n = make_nchr_index(chridx, tlev);
+    int i;
     struct nchrstr *np;
 
     pr("%13s blst dam lbs tech res $%7s abilities\n", "", "");
-
-    for (np = nchr; np->n_name; np++) {
-       if (np->n_tech > tlev)
-           continue;
+    for (i = 0; i < n; i++) {
+       np = &nchr[chridx[i].type];
        pr("%-13.13s %4d %3d %3d %4d %3.0f $%7d",
           np->n_name, np->n_blast, np->n_dam,
           np->n_weight, np->n_tech,
@@ -207,40 +231,33 @@ show_nuke_capab(int tlev)
 void
 show_ship_build(int tlev)
 {
+    struct chr_index chridx[sizeof(mchr) / sizeof(*mchr)];
+    int n = make_mchr_index(chridx, tlev);
+    int i;
     struct mchrstr *mp;
-    int n;
 
     pr("%25s lcm hcm avail tech $\n", "");
-    make_new_list(tlev, EF_SHIP);
-    for (n = 0; n < lookup_list_cnt; n++) {
-       mp = (struct mchrstr *)lookup_list[n].l_u.mp;
-       /* Can't show trade ships unless it's turned on */
-       if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
-           continue;
-
+    for (i = 0; i < n; i++) {
+       mp = &mchr[chridx[i].type];
        pr("%-25.25s %3d %3d %5d %4d $%d\n",
-          mp->m_name, mp->m_lcm, mp->m_hcm,
-          SHP_BLD_WORK(mp->m_lcm, mp->m_hcm), mp->m_tech, mp->m_cost);
+          mp->m_name, mp->m_mat[I_LCM], mp->m_mat[I_HCM],
+          mp->m_bwork, mp->m_tech, mp->m_cost);
     }
 }
 
 void
 show_ship_stats(int tlev)
 {
+    struct chr_index chridx[sizeof(mchr) / sizeof(*mchr)];
+    int n = make_mchr_index(chridx, tlev);
+    int i;
     struct mchrstr *mp;
-    int scount;
 
     pr("%25s      s  v  s  r  f  l  p  h  x\n", "");
     pr("%25s      p  i  p  n  i  n  l  e  p\n", "");
     pr("%25s def  d  s  y  g  r  d  n  l  l\n", "");
-
-    make_new_list(tlev, EF_SHIP);
-    for (scount = 0; scount < lookup_list_cnt; scount++) {
-       mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
-       /* Can't show trade ships unless it's turned on */
-       if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
-           continue;
-
+    for (i = 0; i < n; i++) {
+       mp = &mchr[chridx[i].type];
        pr("%-25.25s %3d %2d %2d %2d %2d %2d %2d %2d %2d %2d\n",
           mp->m_name, m_armor(mp, tlev), m_speed(mp, tlev),
           m_visib(mp, tlev), mp->m_vrnge,
@@ -252,18 +269,14 @@ show_ship_stats(int tlev)
 void
 show_ship_capab(int tlev)
 {
+    struct chr_index chridx[sizeof(mchr) / sizeof(*mchr)];
+    int n = make_mchr_index(chridx, tlev);
+    int i;
     struct mchrstr *mp;
-    int scount;
 
     pr("%25s cargos & capabilities\n", "");
-
-    make_new_list(tlev, EF_SHIP);
-    for (scount = 0; scount < lookup_list_cnt; scount++) {
-       mp = (struct mchrstr *)lookup_list[scount].l_u.mp;
-       /* Can't show trade ships unless it's turned on */
-       if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
-           continue;
-
+    for (i = 0; i < n; i++) {
+       mp = &mchr[chridx[i].type];
        pr("%-25.25s ", mp->m_name);
        show_load(mp->m_item);
        show_capab(mp->m_flags, ship_chr_flags);
@@ -274,13 +287,14 @@ show_ship_capab(int tlev)
 void
 show_plane_stats(int tlev)
 {
+    struct chr_index chridx[sizeof(plchr) / sizeof(*plchr)];
+    int n = make_plchr_index(chridx, tlev);
+    int i;
     struct plchrstr *pp;
-    int pcount;
 
     pr("%25s acc load att def ran fuel stlth\n", "");
-    make_new_list(tlev, EF_PLANE);
-    for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
-       pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
+    for (i = 0; i < n; i++) {
+       pp = &plchr[chridx[i].type];
        pr("%-25.25s %3d %4d %3d %3d %3d %4d %4d%%\n",
           pp->pl_name, pl_acc(pp, tlev), pl_load(pp, tlev),
           pl_att(pp, tlev), pl_def(pp, tlev), pl_range(pp, tlev),
@@ -291,15 +305,15 @@ show_plane_stats(int tlev)
 void
 show_plane_capab(int tlev)
 {
+    struct chr_index chridx[sizeof(plchr) / sizeof(*plchr)];
+    int n = make_plchr_index(chridx, tlev);
+    int i;
     struct plchrstr *pp;
-    int pcount;
 
     pr("%25s capabilities\n", "");
-    make_new_list(tlev, EF_PLANE);
-    for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
-       pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
+    for (i = 0; i < n; i++) {
+       pp = &plchr[chridx[i].type];
        pr("%-25.25s ", pp->pl_name);
-
        show_capab(pp->pl_flags, plane_chr_flags);
        pr("\n");
     }
@@ -308,54 +322,51 @@ show_plane_capab(int tlev)
 void
 show_plane_build(int tlev)
 {
+    struct chr_index chridx[sizeof(plchr) / sizeof(*plchr)];
+    int n = make_plchr_index(chridx, tlev);
+    int i;
     struct plchrstr *pp;
-    int pcount;
 
     pr("%25s lcm hcm crew avail tech $\n", "");
-    make_new_list(tlev, EF_PLANE);
-    for (pcount = 0; pcount < lookup_list_cnt; pcount++) {
-       pp = (struct plchrstr *)lookup_list[pcount].l_u.pp;
+    for (i = 0; i < n; i++) {
+       pp = &plchr[chridx[i].type];
        pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
-          pp->pl_name, pp->pl_lcm,
-          pp->pl_hcm, pp->pl_crew,
-          PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm), pp->pl_tech, pp->pl_cost);
+          pp->pl_name, pp->pl_mat[I_LCM],
+          pp->pl_mat[I_HCM], pp->pl_mat[I_MILIT],
+          pp->pl_bwork, pp->pl_tech, pp->pl_cost);
     }
 }
 
 void
 show_land_build(int tlev)
 {
+    struct chr_index chridx[sizeof(lchr) / sizeof(*lchr)];
+    int n = make_lchr_index(chridx, tlev);
+    int i;
     struct lchrstr *lp;
-    int n;
 
     pr("%25s lcm hcm guns avail tech $\n", "");
-    make_new_list(tlev, EF_LAND);
-    for (n = 0; n < lookup_list_cnt; n++) {
-       lp = (struct lchrstr *)lookup_list[n].l_u.lp;
-       if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
-           continue;
+    for (i = 0; i < n; i++) {
+       lp = &lchr[chridx[i].type];
        pr("%-25.25s %3d %3d %4d %5d %4d $%d\n",
-          lp->l_name, lp->l_lcm,
-          lp->l_hcm,
-          lp->l_gun,
-          LND_BLD_WORK(lp->l_lcm, lp->l_hcm), lp->l_tech, lp->l_cost);
+          lp->l_name, lp->l_mat[I_LCM],
+          lp->l_mat[I_HCM],
+          lp->l_mat[I_GUN],
+          lp->l_bwork, lp->l_tech, lp->l_cost);
     }
 }
 
 void
 show_land_capab(int tlev)
 {
+    struct chr_index chridx[sizeof(lchr) / sizeof(*lchr)];
+    int n = make_lchr_index(chridx, tlev);
+    int i;
     struct lchrstr *lcp;
-    int lcount;
 
     pr("%25s capabilities\n", "");
-
-    make_new_list(tlev, EF_LAND);
-    for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
-       lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
-       if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
-           continue;
-
+    for (i = 0; i < n; i++) {
+       lcp = &lchr[chridx[i].type];
        pr("%-25s ", lcp->l_name);
        show_load(lcp->l_item);
        show_capab(lcp->l_flags, land_chr_flags);
@@ -366,19 +377,16 @@ show_land_capab(int tlev)
 void
 show_land_stats(int tlev)
 {
+    struct chr_index chridx[sizeof(lchr) / sizeof(*lchr)];
+    int n = make_lchr_index(chridx, tlev);
+    int i;
     struct lchrstr *lcp;
-    int lcount;
 
     pr("%25s              s  v  s  r  r  a  f  a  a  x  l\n", "");
     pr("%25s              p  i  p  a  n  c  i  m  a  p  n\n", "");
     pr("%25s att def vul  d  s  y  d  g  c  r  m  f  l  d\n", "");
-
-    make_new_list(tlev, EF_LAND);
-    for (lcount = 0; lcount < lookup_list_cnt; lcount++) {
-       lcp = (struct lchrstr *)lookup_list[lcount].l_u.lp;
-       if ((lcp->l_flags & L_SPY) && !opt_LANDSPIES)
-           continue;
-
+    for (i = 0; i < n; i++) {
+       lcp = &lchr[chridx[i].type];
        pr("%-25s %1.1f %1.1f %3d %2d %2d %2d %2d %2d %2d %2d %2d %2d"
           " %2d %2d\n",
           lcp->l_name,
@@ -478,12 +486,14 @@ show_item(int tlev)
 {
     struct ichrstr *ip;
 
-    pr("item value sell lbs    packing     melt  item\n");
-    pr("mnem                in no wh ur bk deno  name\n");
+    pr("item power value sell lbs    packing     melt  item\n");
+    pr("mnem                      in no wh ur bk deno  name\n");
 
     for (ip = ichr; ip->i_name; ip++) {
-       pr("   %c %5d %4s %3d %2d %2d %2d %2d %2d %4d  %s\n",
-          ip->i_mnem, ip->i_value, ip->i_sell ? "yes" : "no", ip->i_lbs,
+       pr("  %c  %5d %5d %4s %3d %2d %2d %2d %2d %2d %4d  %s\n",
+          ip->i_mnem, ip->i_power,
+          ip->i_value, ip->i_sell ? "yes" : "no",
+          ip->i_lbs,
           ip->i_pkg[IPKG], ip->i_pkg[NPKG], ip->i_pkg[WPKG],
           ip->i_pkg[UPKG], ip->i_pkg[BPKG],
           ip->i_melt_denom, ip->i_name);
@@ -500,11 +510,13 @@ show_product(int tlev)
     pr("product    cost  raw materials  reso dep  level p.e.\n");
 
     for (pp = pchr; pp->p_sname; pp++) {
+       if (!pp->p_sname[0])
+           continue;
        pr("%7.7s %c  $%-3d ",
           pp->p_sname,
           pp->p_type < 0 ? ' ' : ichr[pp->p_type].i_mnem,
           pp->p_cost);
-       (void)CANT_HAPPEN(MAXPRCON > 3); /* output has only three columns */
+       BUILD_ASSERT(MAXPRCON <= 3); /* output has only three columns */
        for (i = 0; i < 3; i++) {
            if (i < MAXPRCON && pp->p_camt[i]
                && pp->p_ctype[i] > I_NONE && pp->p_ctype[i] <= I_MAX)
@@ -593,7 +605,7 @@ show_updates(int n)
 }
 
 /*
- * Return T formatted according to RFC 2822.
+ * Return @t formatted according to RFC 2822.
  * The return value is statically allocated and overwritten on
  * subsequent calls.
  */
@@ -646,14 +658,9 @@ show_load(short item[])
 static void
 show_capab(int flags, struct symbol *table)
 {
-    int i;
-    char *p;
+    char buf[1024];
 
-    for (i = 0; i < 32; i++) {
-       if (!(flags & bit(i)))
-           continue;
-       p = symbol_by_value(bit(i), table);
-       if (p)
-           pr(" %s", p);
-    }
+    symbol_set_fmt(buf, sizeof(buf), flags, table, " ", 0);
+    if (*buf)
+       pr(" %s", buf);
 }