]> git.pond.sub.org Git - empserver/blob - src/lib/commands/strv.c
(starv_sects): Reported food need was one short when the sector had no
[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 "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "nsc.h"
42 #include "nat.h"
43 #include "path.h"
44 #include "file.h"
45 #include "item.h"
46 #include "ship.h"
47 #include "optlist.h"
48 #include "land.h"
49 #include "commands.h"
50
51 static void starv_sects(char *range);
52 static void starv_ships(char *range);
53 static void starv_units(char *range);
54 static void sect_hdr(void);
55 static void ship_hdr(void);
56 static void unit_hdr(void);
57
58 int
59 starve(void)
60 {
61     int do_sects = 0;
62     int do_ships = 0;
63     int do_units = 0;
64     char *range;
65
66     if (opt_NOFOOD) {           /* no food - no work! */
67         pr("No food is required in this game\n");
68         return RET_OK;
69     }
70
71     range = "*";
72     if (!player->argp[1]) {
73         do_sects = do_ships = do_units = 1;
74     } else if (*(player->argp[1]) == 's') {
75         do_ships = 1;
76         if (player->argp[2])
77             range = player->argp[2];
78     } else if (*(player->argp[1]) == 'l') {
79         do_units = 1;
80         if (player->argp[2])
81             range = player->argp[2];
82     } else {
83         do_sects = 1;
84         range = player->argp[1];
85     }
86     player->simulation = 1;
87     prdate();
88     if (do_sects)
89         starv_sects(range);
90     if (do_ships)
91         starv_ships(range);
92     if (do_units)
93         starv_units(range);
94     player->simulation = 0;
95     return RET_OK;
96 }
97
98 static void
99 starv_people(short *vec, int victims)
100 {
101     pr(" will starve %d people. %.0f more food needed\n", victims,
102        ceil(food_needed(vec, etu_per_update) - vec[I_FOOD]));
103 }
104
105 static void
106 starv_sects(char *range)
107 {
108     struct nstr_sect nstr;
109     struct sctstr sect;
110     int nsect = 0;
111     int s, save;
112
113     if (!snxtsct(&nstr, range))
114         return;
115     while (nxtsct(&nstr, &sect)) {
116         if (!player->owner)
117             continue;
118         if (sect.sct_type == SCT_SANCT)
119             continue;
120
121         /*
122          * Check for starvation.  Suppress complaints about tiny
123          * population without food by adding 1f just for the check.
124          * That's okay because growfood() will grow at least that much
125          * anyway.
126          */
127         save = sect.sct_item[I_FOOD];
128         if (sect.sct_item[I_FOOD] == 0)
129             sect.sct_item[I_FOOD] = 1;
130         s = famine_victims(sect.sct_item, etu_per_update);
131         sect.sct_item[I_FOOD] = save;
132         if (s == 0)
133             continue;
134
135         if (nsect++ == 0)
136             sect_hdr();
137         if (player->god)
138             pr("%3d ", sect.sct_own);
139         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
140         pr(" %c", dchr[sect.sct_type].d_mnem);
141         pr(" %c", sect.sct_own != sect.sct_oldown ? '*' : ' ');
142         if (sect.sct_newtype != sect.sct_type)
143             pr("%c", dchr[sect.sct_newtype].d_mnem);
144         else
145             pr(" ");
146         pr("%4d%%", sect.sct_effic);
147         starv_people(sect.sct_item, s);
148     }
149     if (nsect == 0) {
150         if (player->argp[1])
151             pr("%s: No sector(s)\n", player->argp[1]);
152         else
153             pr("%s: No sector(s)\n", "");
154         return;
155     } else
156         pr("%d sector%s\n", nsect, splur(nsect));
157     return;
158 }
159
160 static void
161 sect_hdr(void)
162 {
163     if (player->god)
164         pr("    ");
165     pr("Starvation               \n");
166     if (player->god)
167         pr("own ");
168     pr("  sect         eff ");
169     pr("\n");
170 }
171
172 static void
173 starv_ships(char *range)
174 {
175     struct nstr_item ni;
176     struct shpstr ship;
177     int nship = 0;
178     int s;
179
180     if (!snxtitem(&ni, EF_SHIP, range))
181         return;
182
183     while (nxtitem(&ni, &ship)) {
184         if (!player->owner || !ship.shp_own)
185             continue;
186
187         s = famine_victims(ship.shp_item, etu_per_update);
188         if (s == 0)
189             continue;
190         if (nship++ == 0)
191             ship_hdr();
192         if (player->god)
193             pr("%3d ", ship.shp_own);
194         pr("%5d ", ship.shp_uid);
195         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
196         starv_people(ship.shp_item, s);
197     }
198     if (nship == 0) {
199         if (range)
200             pr("%s: No ship(s)\n", range);
201         else
202             pr("%s: No ship(s)\n", "");
203         return;
204     } else
205         pr("%d ship%s\n", nship, splur(nship));
206     return;
207 }
208
209 static void
210 ship_hdr(void)
211 {
212     if (player->god)
213         pr("    ");
214     pr("Starvation               \n");
215     if (player->god)
216         pr("own ");
217     pr(" shp#     ship type       \n");
218 }
219
220 static void
221 starv_units(char *range)
222 {
223     struct nstr_item ni;
224     struct lndstr land;
225     int nunit = 0;
226     int s;
227
228     if (!snxtitem(&ni, EF_LAND, range))
229         return;
230
231     while (nxtitem(&ni, &land)) {
232         if (!player->owner || !land.lnd_own)
233             continue;
234
235         s = famine_victims(land.lnd_item, etu_per_update);
236         if (s == 0)
237             continue;
238         if (nunit++ == 0)
239             unit_hdr();
240         if (player->god)
241             pr("%3d ", land.lnd_own);
242         pr("%5d ", land.lnd_uid);
243         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
244         starv_people(land.lnd_item, s);
245     }
246     if (nunit == 0) {
247         if (range)
248             pr("%s: No unit(s)\n", range);
249         else
250             pr("%s: No unit(s)\n", "");
251         return;
252     } else
253         pr("%d unit%s\n", nunit, splur(nunit));
254     return;
255 }
256
257 static void
258 unit_hdr(void)
259 {
260     if (player->god)
261         pr("    ");
262     pr("Starvation               \n");
263     if (player->god)
264         pr("own ");
265     pr(" lnd#     unit type       \n");
266 }