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