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