]> git.pond.sub.org Git - empserver/blob - src/lib/commands/strv.c
deity.h is redundant, remove it.
[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 "path.h"
42 #include "file.h"
43 #include <math.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 vec[I_MAX + 1], 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         getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
117 /* This next 2 lines were added to overcompensate for the needy */
118         if (vec[I_FOOD])
119             vec[I_FOOD]--;
120         s = feed_people(vec, etu_per_update, &needed);
121
122         if (s == 0)
123             continue;
124         if (nsect++ == 0)
125             sect_hdr();
126         if (player->god)
127             pr("%3d ", sect.sct_own);
128         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
129         pr(" %c", dchr[sect.sct_type].d_mnem);
130         pr(" %c", (sect.sct_own != sect.sct_oldown ? '*' : ' '));
131         if (sect.sct_newtype != sect.sct_type)
132             pr("%c", dchr[sect.sct_newtype].d_mnem);
133         else
134             pr(" ");
135         pr("%4d%%", sect.sct_effic);
136         pr(" will starve %d people. %d more food needed\n", s, needed);
137     }
138     if (nsect == 0) {
139         if (player->argp[1])
140             pr("%s: No sector(s)\n", player->argp[1]);
141         else
142             pr("%s: No sector(s)\n", "");
143         return;
144     } else
145         pr("%d sector%s\n", nsect, splur(nsect));
146     return;
147 }
148
149 static void
150 sect_hdr(void)
151 {
152     if (player->god)
153         pr("    ");
154     pr("Starvation               \n");
155     if (player->god)
156         pr("own ");
157     pr("  sect         eff ");
158     pr("\n");
159 }
160
161 static void
162 starv_ships(s_char *range)
163 {
164     struct nstr_item ni;
165     struct shpstr ship;
166     int nship = 0;
167     int vec[I_MAX + 1], s, needed;
168
169     if (!snxtitem(&ni, EF_SHIP, range))
170         return;
171
172     while (nxtitem(&ni, (s_char *)&ship)) {
173         if (!player->owner || !ship.shp_own)
174             continue;
175
176         getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
177         s = feed_ship(&ship, vec, etu_per_update, &needed, 0);
178
179         if (s == 0)
180             continue;
181         if (nship++ == 0)
182             ship_hdr();
183         if (player->god)
184             pr("%3d ", ship.shp_own);
185         pr("%5d ", ship.shp_uid);
186         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
187         pr(" will starve %d people. %d more food needed\n", s, needed);
188     }
189     if (nship == 0) {
190         if (range)
191             pr("%s: No ship(s)\n", range);
192         else
193             pr("%s: No ship(s)\n", "");
194         return;
195     } else
196         pr("%d ship%s\n", nship, splur(nship));
197     return;
198 }
199
200 static void
201 ship_hdr(void)
202 {
203     if (player->god)
204         pr("    ");
205     pr("Starvation               \n");
206     if (player->god)
207         pr("own ");
208     pr(" shp#     ship type       \n");
209 }
210
211 static void
212 starv_units(s_char *range)
213 {
214     struct nstr_item ni;
215     struct lndstr land;
216     int nunit = 0;
217     int vec[I_MAX + 1], s, needed;
218
219     if (!snxtitem(&ni, EF_LAND, range))
220         return;
221
222     while (nxtitem(&ni, (s_char *)&land)) {
223         if (!player->owner || !land.lnd_own)
224             continue;
225
226         getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
227         s = feed_land(&land, vec, etu_per_update, &needed, 0);
228
229         if (s == 0)
230             continue;
231         if (nunit++ == 0)
232             unit_hdr();
233         if (player->god)
234             pr("%3d ", land.lnd_own);
235         pr("%5d ", land.lnd_uid);
236         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
237         pr(" will starve %d mil. %d more food needed\n", s, needed);
238     }
239     if (nunit == 0) {
240         if (range)
241             pr("%s: No unit(s)\n", range);
242         else
243             pr("%s: No unit(s)\n", "");
244         return;
245     } else
246         pr("%d unit%s\n", nunit, splur(nunit));
247     return;
248 }
249
250 static void
251 unit_hdr(void)
252 {
253     if (player->god)
254         pr("    ");
255     pr("Starvation               \n");
256     if (player->god)
257         pr("own ");
258     pr(" lnd#     unit type       \n");
259 }