]> git.pond.sub.org Git - empserver/blob - src/lib/commands/strv.c
2811a4dca3a3fbba0a427d197a801508adf76c23
[empserver] / src / lib / commands / strv.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  star.c: Do a starvation report
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include <config.h>
35
36 #include <math.h>
37 #include "commands.h"
38 #include "item.h"
39 #include "land.h"
40 #include "optlist.h"
41 #include "path.h"
42 #include "ship.h"
43
44 static void starv_sects(char *range);
45 static void starv_ships(char *range);
46 static void starv_units(char *range);
47 static void sect_hdr(void);
48 static void ship_hdr(void);
49 static void unit_hdr(void);
50
51 int
52 starve(void)
53 {
54     int do_sects = 0;
55     int do_ships = 0;
56     int do_units = 0;
57     char *range;
58
59     if (opt_NOFOOD) {           /* no food - no work! */
60         pr("No food is required in this game\n");
61         return RET_OK;
62     }
63
64     range = "*";
65     if (!player->argp[1]) {
66         do_sects = do_ships = do_units = 1;
67     } else if (*(player->argp[1]) == 's') {
68         do_ships = 1;
69         if (player->argp[2])
70             range = player->argp[2];
71     } else if (*(player->argp[1]) == 'l') {
72         do_units = 1;
73         if (player->argp[2])
74             range = player->argp[2];
75     } else {
76         do_sects = 1;
77         range = player->argp[1];
78     }
79     player->simulation = 1;
80     prdate();
81     if (do_sects)
82         starv_sects(range);
83     if (do_ships)
84         starv_ships(range);
85     if (do_units)
86         starv_units(range);
87     player->simulation = 0;
88     return RET_OK;
89 }
90
91 static void
92 starv_people(short *vec, int victims)
93 {
94     pr(" will starve %d people. %.0f more food needed\n", victims,
95        ceil(food_needed(vec, etu_per_update) - vec[I_FOOD]));
96 }
97
98 static void
99 starv_sects(char *range)
100 {
101     struct nstr_sect nstr;
102     struct sctstr sect;
103     int nsect = 0;
104     int s, save;
105
106     if (!snxtsct(&nstr, range))
107         return;
108     while (nxtsct(&nstr, &sect)) {
109         if (!player->owner)
110             continue;
111         if (sect.sct_type == SCT_SANCT)
112             continue;
113
114         /*
115          * Check for starvation.  Suppress complaints about tiny
116          * population without food by adding 1f just for the check.
117          * That's okay because growfood() will grow at least that much
118          * anyway.
119          */
120         save = sect.sct_item[I_FOOD];
121         if (sect.sct_item[I_FOOD] == 0)
122             sect.sct_item[I_FOOD] = 1;
123         s = famine_victims(sect.sct_item, etu_per_update);
124         sect.sct_item[I_FOOD] = save;
125         if (s == 0)
126             continue;
127
128         if (nsect++ == 0)
129             sect_hdr();
130         if (player->god)
131             pr("%3d ", sect.sct_own);
132         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
133         pr(" %c", dchr[sect.sct_type].d_mnem);
134         pr(" %c", sect.sct_own != sect.sct_oldown ? '*' : ' ');
135         if (sect.sct_newtype != sect.sct_type)
136             pr("%c", dchr[sect.sct_newtype].d_mnem);
137         else
138             pr(" ");
139         pr("%4d%%", sect.sct_effic);
140         starv_people(sect.sct_item, s);
141     }
142     if (nsect == 0) {
143         if (player->argp[1])
144             pr("%s: No sector(s)\n", player->argp[1]);
145         else
146             pr("%s: No sector(s)\n", "");
147         return;
148     } else
149         pr("%d sector%s\n", nsect, splur(nsect));
150     return;
151 }
152
153 static void
154 sect_hdr(void)
155 {
156     if (player->god)
157         pr("    ");
158     pr("Starvation               \n");
159     if (player->god)
160         pr("own ");
161     pr("  sect         eff ");
162     pr("\n");
163 }
164
165 static void
166 starv_ships(char *range)
167 {
168     struct nstr_item ni;
169     struct shpstr ship;
170     int nship = 0;
171     int s;
172
173     if (!snxtitem(&ni, EF_SHIP, range))
174         return;
175
176     while (nxtitem(&ni, &ship)) {
177         if (!player->owner || !ship.shp_own)
178             continue;
179
180         s = famine_victims(ship.shp_item, etu_per_update);
181         if (s == 0)
182             continue;
183         if (nship++ == 0)
184             ship_hdr();
185         if (player->god)
186             pr("%3d ", ship.shp_own);
187         pr("%5d ", ship.shp_uid);
188         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
189         starv_people(ship.shp_item, s);
190     }
191     if (nship == 0) {
192         if (range)
193             pr("%s: No ship(s)\n", range);
194         else
195             pr("%s: No ship(s)\n", "");
196         return;
197     } else
198         pr("%d ship%s\n", nship, splur(nship));
199     return;
200 }
201
202 static void
203 ship_hdr(void)
204 {
205     if (player->god)
206         pr("    ");
207     pr("Starvation               \n");
208     if (player->god)
209         pr("own ");
210     pr(" shp#     ship type       \n");
211 }
212
213 static void
214 starv_units(char *range)
215 {
216     struct nstr_item ni;
217     struct lndstr land;
218     int nunit = 0;
219     int s;
220
221     if (!snxtitem(&ni, EF_LAND, range))
222         return;
223
224     while (nxtitem(&ni, &land)) {
225         if (!player->owner || !land.lnd_own)
226             continue;
227
228         s = famine_victims(land.lnd_item, etu_per_update);
229         if (s == 0)
230             continue;
231         if (nunit++ == 0)
232             unit_hdr();
233         if (player->god)
234             pr("%3d ", land.lnd_own);
235         pr("%5d ", land.lnd_uid);
236         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
237         starv_people(land.lnd_item, s);
238     }
239     if (nunit == 0) {
240         if (range)
241             pr("%s: No unit(s)\n", range);
242         else
243             pr("%s: No unit(s)\n", "");
244         return;
245     } else
246         pr("%d unit%s\n", nunit, splur(nunit));
247     return;
248 }
249
250 static void
251 unit_hdr(void)
252 {
253     if (player->god)
254         pr("    ");
255     pr("Starvation               \n");
256     if (player->god)
257         pr("own ");
258     pr(" lnd#     unit type       \n");
259 }