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