src/lib/commands/strv.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  star.c: Do a starvation report
00029  * 
00030  *  Known contributors to this file:
00031  *     
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include <math.h>
00037 #include "commands.h"
00038 #include "item.h"
00039 #include "land.h"
00040 #include "optlist.h"
00041 #include "path.h"
00042 #include "ship.h"
00043 
00044 static void starv_sects(char *range);
00045 static void starv_ships(char *range);
00046 static void starv_units(char *range);
00047 static void sect_hdr(void);
00048 static void ship_hdr(void);
00049 static void unit_hdr(void);
00050 
00051 int
00052 starve(void)
00053 {
00054     int do_sects = 0;
00055     int do_ships = 0;
00056     int do_units = 0;
00057     char *range;
00058 
00059     if (opt_NOFOOD) {           /* no food - no work! */
00060         pr("No food is required in this game\n");
00061         return RET_OK;
00062     }
00063 
00064     range = "*";
00065     if (!player->argp[1]) {
00066         do_sects = do_ships = do_units = 1;
00067     } else if (*(player->argp[1]) == 's') {
00068         do_ships = 1;
00069         if (player->argp[2])
00070             range = player->argp[2];
00071     } else if (*(player->argp[1]) == 'l') {
00072         do_units = 1;
00073         if (player->argp[2])
00074             range = player->argp[2];
00075     } else {
00076         do_sects = 1;
00077         range = player->argp[1];
00078     }
00079     player->simulation = 1;
00080     prdate();
00081     if (do_sects)
00082         starv_sects(range);
00083     if (do_ships)
00084         starv_ships(range);
00085     if (do_units)
00086         starv_units(range);
00087     player->simulation = 0;
00088     return RET_OK;
00089 }
00090 
00091 static void
00092 starv_people(short *vec, int victims)
00093 {
00094     pr(" will starve %d people. %.0f more food needed\n", victims,
00095        ceil(food_needed(vec, etu_per_update) - vec[I_FOOD]));
00096 }
00097 
00098 static void
00099 starv_sects(char *range)
00100 {
00101     struct nstr_sect nstr;
00102     struct sctstr sect;
00103     int nsect = 0;
00104     int s, save;
00105 
00106     if (!snxtsct(&nstr, range))
00107         return;
00108     while (nxtsct(&nstr, &sect)) {
00109         if (!player->owner)
00110             continue;
00111         if (sect.sct_type == SCT_SANCT)
00112             continue;
00113 
00114         /*
00115          * Check for starvation.  Suppress complaints about tiny
00116          * population without food by adding 1f just for the check.
00117          * That's okay because growfood() will grow at least that much
00118          * anyway.
00119          */
00120         save = sect.sct_item[I_FOOD];
00121         if (sect.sct_item[I_FOOD] == 0)
00122             sect.sct_item[I_FOOD] = 1;
00123         s = famine_victims(sect.sct_item, etu_per_update);
00124         sect.sct_item[I_FOOD] = save;
00125         if (s == 0)
00126             continue;
00127 
00128         if (nsect++ == 0)
00129             sect_hdr();
00130         if (player->god)
00131             pr("%3d ", sect.sct_own);
00132         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
00133         pr(" %c", dchr[sect.sct_type].d_mnem);
00134         pr(" %c", sect.sct_own != sect.sct_oldown ? '*' : ' ');
00135         if (sect.sct_newtype != sect.sct_type)
00136             pr("%c", dchr[sect.sct_newtype].d_mnem);
00137         else
00138             pr(" ");
00139         pr("%4d%%", sect.sct_effic);
00140         starv_people(sect.sct_item, s);
00141     }
00142     if (nsect == 0) {
00143         if (player->argp[1])
00144             pr("%s: No sector(s)\n", player->argp[1]);
00145         else
00146             pr("%s: No sector(s)\n", "");
00147         return;
00148     } else
00149         pr("%d sector%s\n", nsect, splur(nsect));
00150     return;
00151 }
00152 
00153 static void
00154 sect_hdr(void)
00155 {
00156     if (player->god)
00157         pr("    ");
00158     pr("Starvation               \n");
00159     if (player->god)
00160         pr("own ");
00161     pr("  sect         eff ");
00162     pr("\n");
00163 }
00164 
00165 static void
00166 starv_ships(char *range)
00167 {
00168     struct nstr_item ni;
00169     struct shpstr ship;
00170     int nship = 0;
00171     int s;
00172 
00173     if (!snxtitem(&ni, EF_SHIP, range))
00174         return;
00175 
00176     while (nxtitem(&ni, &ship)) {
00177         if (!player->owner || !ship.shp_own)
00178             continue;
00179 
00180         s = famine_victims(ship.shp_item, etu_per_update);
00181         if (s == 0)
00182             continue;
00183         if (nship++ == 0)
00184             ship_hdr();
00185         if (player->god)
00186             pr("%3d ", ship.shp_own);
00187         pr("%5d ", ship.shp_uid);
00188         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
00189         starv_people(ship.shp_item, s);
00190     }
00191     if (nship == 0) {
00192         if (range)
00193             pr("%s: No ship(s)\n", range);
00194         else
00195             pr("%s: No ship(s)\n", "");
00196         return;
00197     } else
00198         pr("%d ship%s\n", nship, splur(nship));
00199     return;
00200 }
00201 
00202 static void
00203 ship_hdr(void)
00204 {
00205     if (player->god)
00206         pr("    ");
00207     pr("Starvation               \n");
00208     if (player->god)
00209         pr("own ");
00210     pr(" shp#     ship type       \n");
00211 }
00212 
00213 static void
00214 starv_units(char *range)
00215 {
00216     struct nstr_item ni;
00217     struct lndstr land;
00218     int nunit = 0;
00219     int s;
00220 
00221     if (!snxtitem(&ni, EF_LAND, range))
00222         return;
00223 
00224     while (nxtitem(&ni, &land)) {
00225         if (!player->owner || !land.lnd_own)
00226             continue;
00227 
00228         s = famine_victims(land.lnd_item, etu_per_update);
00229         if (s == 0)
00230             continue;
00231         if (nunit++ == 0)
00232             unit_hdr();
00233         if (player->god)
00234             pr("%3d ", land.lnd_own);
00235         pr("%5d ", land.lnd_uid);
00236         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
00237         starv_people(land.lnd_item, s);
00238     }
00239     if (nunit == 0) {
00240         if (range)
00241             pr("%s: No unit(s)\n", range);
00242         else
00243             pr("%s: No unit(s)\n", "");
00244         return;
00245     } else
00246         pr("%d unit%s\n", nunit, splur(nunit));
00247     return;
00248 }
00249 
00250 static void
00251 unit_hdr(void)
00252 {
00253     if (player->god)
00254         pr("    ");
00255     pr("Starvation               \n");
00256     if (player->god)
00257         pr("own ");
00258     pr(" lnd#     unit type       \n");
00259 }

Generated on Fri Mar 28 11:01:13 2008 for empserver by  doxygen 1.5.2