]> git.pond.sub.org Git - empserver/blob - src/lib/commands/strv.c
Do not include var.h where no longer needed. Clean up register keywords in these...
[empserver] / src / lib / commands / strv.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "sect.h"
38 #include "nsc.h"
39 #include "nat.h"
40 #include "path.h"
41 #include "file.h"
42 #include <math.h>
43 #include "item.h"
44 #include "ship.h"
45 #include "optlist.h"
46 #include "land.h"
47 #include "commands.h"
48
49 static void starv_sects(s_char *range);
50 static void starv_ships(s_char *range);
51 static void starv_units(s_char *range);
52 static void sect_hdr(void);
53 static void ship_hdr(void);
54 static void unit_hdr(void);
55
56 int
57 starve(void)
58 {
59     struct nstr_sect nstr;
60     int do_sects = 0;
61     int do_ships = 0;
62     int do_units = 0;
63     s_char range[1024];
64
65     if (opt_NOFOOD) {           /* no food - no work! */
66         pr("No food is required in this game\n");
67         return RET_OK;
68     }
69
70     strcpy(range, "*");
71     if (!player->argp[1]) {
72         do_sects = do_ships = do_units = 1;
73     } else if (*(player->argp[1]) == 's') {
74         do_ships = 1;
75         if (player->argp[2])
76             strcpy(range, player->argp[2]);
77     } else if (*(player->argp[1]) == 'l') {
78         do_units = 1;
79         if (player->argp[2])
80             strcpy(range, player->argp[2]);
81     } else {
82         if (!snxtsct(&nstr, player->argp[1]))
83             return RET_SYN;
84         do_sects = 1;
85         strcpy(range, player->argp[1]);
86     }
87     player->simulation = 1;
88     prdate();
89     if (do_sects)
90         starv_sects(range);
91     if (do_ships)
92         starv_ships(range);
93     if (do_units)
94         starv_units(range);
95     player->simulation = 0;
96     return RET_OK;
97 }
98
99 static void
100 starv_sects(s_char *range)
101 {
102     struct nstr_sect nstr;
103     struct sctstr sect;
104     int nsect = 0;
105     int s, needed;
106
107     if (!snxtsct(&nstr, range))
108         return;
109     while (nxtsct(&nstr, &sect)) {
110         if (!player->owner)
111             continue;
112         if (sect.sct_type == SCT_SANCT)
113             continue;
114
115 /* This next 2 lines were added to overcompensate for the needy */
116         if (sect.sct_item[I_FOOD])
117             sect.sct_item[I_FOOD]--;
118         s = feed_people(sect.sct_item, etu_per_update, &needed);
119
120         if (s == 0)
121             continue;
122         if (nsect++ == 0)
123             sect_hdr();
124         if (player->god)
125             pr("%3d ", sect.sct_own);
126         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
127         pr(" %c", dchr[sect.sct_type].d_mnem);
128         pr(" %c", (sect.sct_own != sect.sct_oldown ? '*' : ' '));
129         if (sect.sct_newtype != sect.sct_type)
130             pr("%c", dchr[sect.sct_newtype].d_mnem);
131         else
132             pr(" ");
133         pr("%4d%%", sect.sct_effic);
134         pr(" will starve %d people. %d more food needed\n", s, needed);
135     }
136     if (nsect == 0) {
137         if (player->argp[1])
138             pr("%s: No sector(s)\n", player->argp[1]);
139         else
140             pr("%s: No sector(s)\n", "");
141         return;
142     } else
143         pr("%d sector%s\n", nsect, splur(nsect));
144     return;
145 }
146
147 static void
148 sect_hdr(void)
149 {
150     if (player->god)
151         pr("    ");
152     pr("Starvation               \n");
153     if (player->god)
154         pr("own ");
155     pr("  sect         eff ");
156     pr("\n");
157 }
158
159 static void
160 starv_ships(s_char *range)
161 {
162     struct nstr_item ni;
163     struct shpstr ship;
164     int nship = 0;
165     int s, needed;
166
167     if (!snxtitem(&ni, EF_SHIP, range))
168         return;
169
170     while (nxtitem(&ni, (s_char *)&ship)) {
171         if (!player->owner || !ship.shp_own)
172             continue;
173
174         s = feed_ship(&ship, etu_per_update, &needed, 0);
175         if (s == 0)
176             continue;
177         if (nship++ == 0)
178             ship_hdr();
179         if (player->god)
180             pr("%3d ", ship.shp_own);
181         pr("%5d ", ship.shp_uid);
182         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
183         pr(" will starve %d people. %d more food needed\n", s, needed);
184     }
185     if (nship == 0) {
186         if (range)
187             pr("%s: No ship(s)\n", range);
188         else
189             pr("%s: No ship(s)\n", "");
190         return;
191     } else
192         pr("%d ship%s\n", nship, splur(nship));
193     return;
194 }
195
196 static void
197 ship_hdr(void)
198 {
199     if (player->god)
200         pr("    ");
201     pr("Starvation               \n");
202     if (player->god)
203         pr("own ");
204     pr(" shp#     ship type       \n");
205 }
206
207 static void
208 starv_units(s_char *range)
209 {
210     struct nstr_item ni;
211     struct lndstr land;
212     int nunit = 0;
213     int s, needed;
214
215     if (!snxtitem(&ni, EF_LAND, range))
216         return;
217
218     while (nxtitem(&ni, (s_char *)&land)) {
219         if (!player->owner || !land.lnd_own)
220             continue;
221
222         s = feed_land(&land, etu_per_update, &needed, 0);
223         if (s == 0)
224             continue;
225         if (nunit++ == 0)
226             unit_hdr();
227         if (player->god)
228             pr("%3d ", land.lnd_own);
229         pr("%5d ", land.lnd_uid);
230         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
231         pr(" will starve %d mil. %d more food needed\n", s, needed);
232     }
233     if (nunit == 0) {
234         if (range)
235             pr("%s: No unit(s)\n", range);
236         else
237             pr("%s: No unit(s)\n", "");
238         return;
239     } else
240         pr("%d unit%s\n", nunit, splur(nunit));
241     return;
242 }
243
244 static void
245 unit_hdr(void)
246 {
247     if (player->god)
248         pr("    ");
249     pr("Starvation               \n");
250     if (player->god)
251         pr("own ");
252     pr(" lnd#     unit type       \n");
253 }