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