]> git.pond.sub.org Git - empserver/blob - src/lib/commands/strv.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / strv.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
37 #include "xy.h"
38 #include "sect.h"
39 #include "nsc.h"
40 #include "nat.h"
41 #include "deity.h"
42 #include "path.h"
43 #include "file.h"
44 #include <math.h>
45 #include "item.h"
46 #include "ship.h"
47 #include "optlist.h"
48 #include "land.h"
49 #include "commands.h"
50
51 extern int etu_per_update;
52 static void starv_sects(s_char *range);
53 static void starv_ships(s_char *range);
54 static void starv_units(s_char *range);
55 static void sect_hdr(void);
56 static void ship_hdr(void);
57 static void unit_hdr(void);
58
59 int
60 starve(void)
61 {
62     struct nstr_sect nstr;
63     int do_sects = 0;
64     int do_ships = 0;
65     int do_units = 0;
66     s_char range[1024];
67
68     if (opt_NOFOOD) {           /* no food - no work! */
69         pr("No food is required in this game\n");
70         return RET_OK;
71     }
72
73     strcpy(range, "*");
74     if (!player->argp[1]) {
75         do_sects = do_ships = do_units = 1;
76     } else if (*(player->argp[1]) == 's') {
77         do_ships = 1;
78         if (player->argp[2])
79             strcpy(range, player->argp[2]);
80     } else if (*(player->argp[1]) == 'l') {
81         do_units = 1;
82         if (player->argp[2])
83             strcpy(range, player->argp[2]);
84     } else {
85         if (!snxtsct(&nstr, player->argp[1]))
86             return RET_SYN;
87         do_sects = 1;
88         strcpy(range, player->argp[1]);
89     }
90     player->simulation = 1;
91     prdate();
92     if (do_sects)
93         starv_sects(range);
94     if (do_ships)
95         starv_ships(range);
96     if (do_units)
97         starv_units(range);
98     player->simulation = 0;
99     return RET_OK;
100 }
101
102 static void
103 starv_sects(s_char *range)
104 {
105     struct nstr_sect nstr;
106     struct sctstr sect;
107     int nsect = 0;
108     int vec[I_MAX + 1], s, needed;
109
110     if (!snxtsct(&nstr, range))
111         return;
112     while (nxtsct(&nstr, &sect)) {
113         if (!player->owner)
114             continue;
115         if (sect.sct_type == SCT_SANCT)
116             continue;
117
118         getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
119 /* This next 2 lines were added to overcompensate for the needy */
120         if (vec[I_FOOD])
121             vec[I_FOOD]--;
122         s = feed_people(vec, etu_per_update, &needed);
123
124         if (s == 0)
125             continue;
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, player->cnum);
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         pr(" will starve %d people. %d more food needed\n", s, needed);
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(s_char *range)
165 {
166     struct nstr_item ni;
167     struct shpstr ship;
168     int nship = 0;
169     int vec[I_MAX + 1], s, needed;
170
171     if (!snxtitem(&ni, EF_SHIP, range))
172         return;
173
174     while (nxtitem(&ni, (s_char *)&ship)) {
175         if (!player->owner || !ship.shp_own)
176             continue;
177
178         getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
179         s = feed_ship(&ship, vec, etu_per_update, &needed, 0);
180
181         if (s == 0)
182             continue;
183         if (nship++ == 0)
184             ship_hdr();
185         if (player->god)
186             pr("%3d ", ship.shp_own);
187         pr("%5d ", ship.shp_uid);
188         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
189         pr(" will starve %d people. %d more food needed\n", s, needed);
190     }
191     if (nship == 0) {
192         if (range)
193             pr("%s: No ship(s)\n", range);
194         else
195             pr("%s: No ship(s)\n", "");
196         return;
197     } else
198         pr("%d ship%s\n", nship, splur(nship));
199     return;
200 }
201
202 static void
203 ship_hdr(void)
204 {
205     if (player->god)
206         pr("    ");
207     pr("Starvation               \n");
208     if (player->god)
209         pr("own ");
210     pr(" shp#     ship type       \n");
211 }
212
213 static void
214 starv_units(s_char *range)
215 {
216     struct nstr_item ni;
217     struct lndstr land;
218     int nunit = 0;
219     int vec[I_MAX + 1], s, needed;
220
221     if (!snxtitem(&ni, EF_LAND, range))
222         return;
223
224     while (nxtitem(&ni, (s_char *)&land)) {
225         if (!player->owner || !land.lnd_own)
226             continue;
227
228         getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
229         s = feed_land(&land, vec, etu_per_update, &needed, 0);
230
231         if (s == 0)
232             continue;
233         if (nunit++ == 0)
234             unit_hdr();
235         if (player->god)
236             pr("%3d ", land.lnd_own);
237         pr("%5d ", land.lnd_uid);
238         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
239         pr(" will starve %d mil. %d more food needed\n", s, needed);
240     }
241     if (nunit == 0) {
242         if (range)
243             pr("%s: No unit(s)\n", range);
244         else
245             pr("%s: No unit(s)\n", "");
246         return;
247     } else
248         pr("%d unit%s\n", nunit, splur(nunit));
249     return;
250 }
251
252 static void
253 unit_hdr(void)
254 {
255     if (player->god)
256         pr("    ");
257     pr("Starvation               \n");
258     if (player->god)
259         pr("own ");
260     pr(" lnd#     unit type       \n");
261 }