]> git.pond.sub.org Git - empserver/blob - src/lib/commands/strv.c
Import of Empire 4.2.12
[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,
139                         needed);
140         }
141         if (nsect == 0) {
142                 if (player->argp[1])
143                         pr("%s: No sector(s)\n", player->argp[1]);
144                 else
145                         pr("%s: No sector(s)\n", "");
146                 return;
147         }else
148                 pr("%d sector%s\n", nsect, splur(nsect));
149         return;
150 }
151
152 static void
153 sect_hdr(void)
154 {
155         if (player->god)
156                 pr("    ");
157         pr("Starvation               \n");
158         if (player->god)
159                 pr("own ");
160         pr("  sect         eff ");
161         pr("\n");
162 }
163
164 static void
165 starv_ships(s_char *range)
166 {
167         struct  nstr_item ni;
168         struct  shpstr ship;
169         int     nship = 0;
170         int     vec[I_MAX+1], s, needed;
171
172         if (!snxtitem(&ni, EF_SHIP, range))
173                 return;
174
175         while (nxtitem(&ni, (s_char *)&ship)) {
176                 if (!player->owner || !ship.shp_own)
177                         continue;
178
179                 getvec(VT_ITEM, vec, (s_char *)&ship, EF_SHIP);
180                 s = feed_ship(&ship, vec, etu_per_update, &needed, 0);
181
182                 if (s==0)
183                         continue;
184                 if (nship++ == 0)
185                         ship_hdr();
186                 if (player->god)
187                         pr("%3d ", ship.shp_own);
188                 pr("%5d ", ship.shp_uid);
189                 pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
190                 pr(" will starve %d people. %d more food needed\n",s,
191                         needed);
192         }
193         if (nship == 0) {
194                 if (range)
195                         pr("%s: No ship(s)\n", range);
196                 else
197                         pr("%s: No ship(s)\n", "");
198                 return;
199         }else
200                 pr("%d ship%s\n", nship, splur(nship));
201         return;
202 }
203
204 static void
205 ship_hdr(void)
206 {
207         if (player->god)
208                 pr("    ");
209         pr("Starvation               \n");
210         if (player->god)
211                 pr("own ");
212         pr(" shp#     ship type       \n");
213 }
214
215 static void
216 starv_units(s_char *range)
217 {
218         struct  nstr_item ni;
219         struct  lndstr land;
220         int     nunit = 0;
221         int     vec[I_MAX+1], s, needed;
222
223         if (!snxtitem(&ni, EF_LAND, range))
224                 return;
225
226         while (nxtitem(&ni, (s_char *)&land)) {
227                 if (!player->owner || !land.lnd_own)
228                         continue;
229
230                 getvec(VT_ITEM, vec, (s_char *)&land, EF_LAND);
231                 s = feed_land(&land, vec, etu_per_update, &needed, 0);
232
233                 if (s==0)
234                         continue;
235                 if (nunit++ == 0)
236                         unit_hdr();
237                 if (player->god)
238                         pr("%3d ", land.lnd_own);
239                 pr("%5d ", land.lnd_uid);
240                 pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
241                 pr(" will starve %d mil. %d more food needed\n",s,
242                         needed);
243         }
244         if (nunit == 0) {
245                 if (range)
246                         pr("%s: No unit(s)\n", range);
247                 else
248                         pr("%s: No unit(s)\n", "");
249                 return;
250         }else
251                 pr("%d unit%s\n", nunit, splur(nunit));
252         return;
253 }
254
255 static void
256 unit_hdr(void)
257 {
258         if (player->god)
259                 pr("    ");
260         pr("Starvation               \n");
261         if (player->god)
262                 pr("own ");
263         pr(" lnd#     unit type       \n");
264 }
265
266
267
268
269
270
271