2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * powe.c: Do a power report
29 * Known contributors to this file:
32 * Steve McClure, 1998-2000
33 * Markus Armbruster, 2004-2013
34 * Ron Koenderink, 2005-2008
49 static void prpower(char *, struct powstr *, int);
50 static void out5(double, int, int);
51 static void gen_power(struct powstr *, int);
52 static int powcmp(const void *, const void *);
53 static void addtopow(short *, struct powstr *);
64 int power_generated = 0;
66 struct powstr powbuf[MAXNOC];
71 memset(targets, 0, sizeof(targets));
74 if (player->argp[1]) {
75 switch (player->argp[1][0]) {
82 natp = getnatp(player->cnum);
83 if (natp->nat_btu < 1)
84 pr("\n Insufficient BTUs, using the last report.\n\n");
85 else if (opt_AUTO_POWER && save)
86 pr("\n power new is disabled, using the last report.\n\n");
88 gen_power(powbuf, save);
89 pow_time = time(NULL);
95 if (player->argp[i]) {
96 if (player->argp[i][0] == 'c') {
97 if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
99 while (nxtitem(&ni, &nat)) {
100 if (nat.nat_stat == STAT_UNUSED)
102 if (!player->god && nat.nat_stat != STAT_ACTIVE)
104 targets[nat.nat_cnum] = 1;
108 num = atoi(player->argp[i]);
118 if (!power_generated) {
119 pow_time = ef_mtime(EF_POWER);
120 snxtitem_all(&ni, EF_POWER);
121 if (!nxtitem(&ni, &powbuf[0])) {
122 pr("Power for this game has not been built yet.%s\n",
123 opt_AUTO_POWER ? "" : " Type 'power new' to build it.");
126 for (i = 1; i < MAXNOC; i++) {
127 if (!nxtitem(&ni, &powbuf[i])) {
129 memset(&powbuf[i], 0, sizeof(powbuf[i]));
134 pr(" - = [ Empire Power Report ] = -\n");
135 pr(" as of %s\n sects eff civ", ctime(&pow_time));
136 pr(" mil shell gun pet iron dust oil pln ship unit money\n");
137 for (i = 1; i < MAXNOC && num > 0; i++) {
139 if (!player->god && powbuf[i].p_nation != player->cnum)
142 if (use_targets && !targets[powbuf[i].p_nation])
144 if (!use_targets && powbuf[i].p_power <= 0.0)
146 prpower(cname(powbuf[i].p_nation), &powbuf[i],
147 powbuf[i].p_nation != player->cnum && !player->god);
148 if (player->god && !no_numbers)
149 pr("%9.2f\n", powbuf[i].p_power);
152 if (!opt_HIDDEN || player->god) {
153 pr(" ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
154 prpower("worldwide", &powbuf[0], !player->god);
161 prpower(char *name, struct powstr *pow, int round_flag)
164 out5(pow->p_sects, 5, round_flag);
166 pr("%4.0f%%", pow->p_effic / pow->p_sects);
169 out5(pow->p_civil, 50, round_flag);
170 out5(pow->p_milit, 50, round_flag);
171 out5(pow->p_shell, 25, round_flag);
172 out5(pow->p_guns, 5, round_flag);
173 out5(pow->p_petrol, 50, round_flag);
174 out5(pow->p_iron, 50, round_flag);
175 out5(pow->p_dust, 50, round_flag);
176 out5(pow->p_oil, 50, round_flag);
177 out5(pow->p_planes, 10, round_flag);
178 out5(pow->p_ships, 10, round_flag);
179 out5(pow->p_units, 10, round_flag);
180 out5(pow->p_money, 5000, round_flag);
185 out5(double value, int round_val, int round_flag)
189 if (value > round_val && round_flag)
190 value = (int)(value / round_val + 0.5) * round_val;
194 else if (aval < 9.95e3)
195 pr("%4.1fK", value / 1e3);
196 else if (aval < 999.5e3)
197 pr("%4.0fK", value / 1e3);
198 else if (aval < 9.95e6)
199 pr("%4.1fM", value / 1e6);
200 else if (aval < 999.5e6)
201 pr("%4.0fM", value / 1e6);
203 pr("%4.0fG", value / 1e9);
209 struct powstr powbuf[MAXNOC];
211 gen_power(powbuf, 1);
215 gen_power(struct powstr *powbuf, int save)
230 player->btused += 10;
231 memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
233 while (nxtsct(&ns, §)) {
234 if (sect.sct_own == 0)
236 pow = &powbuf[sect.sct_own];
238 pow->p_effic += sect.sct_effic;
239 addtopow(sect.sct_item, pow);
241 snxtitem_all(&ni, EF_LAND);
242 while (nxtitem(&ni, &land)) {
243 if (land.lnd_own == 0)
245 pow = &powbuf[land.lnd_own];
246 addtopow(land.lnd_item, pow);
247 f = (lchr[(int)land.lnd_type].l_lcm / 10.0) * (land.lnd_effic / 100.0);
248 f += (lchr[(int)land.lnd_type].l_hcm / 10.0) * (land.lnd_effic / 100.0);
249 pow->p_power += f * 2;
250 if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
253 snxtitem_all(&ni, EF_SHIP);
254 while (nxtitem(&ni, &ship)) {
255 if (ship.shp_own == 0)
257 pow = &powbuf[ship.shp_own];
258 addtopow(ship.shp_item, pow);
259 f = (mchr[(int)ship.shp_type].m_lcm / 10.0) * (ship.shp_effic / 100.0);
260 f += (mchr[(int)ship.shp_type].m_hcm / 10.0) * (ship.shp_effic / 100.0);
261 pow->p_power += f * 2;
264 snxtitem_all(&ni, EF_PLANE);
265 while (nxtitem(&ni, &plane)) {
266 if (plane.pln_own == 0)
268 pow = &powbuf[plane.pln_own];
269 pow->p_planes += 1.0;
270 natp = getnatp(plane.pln_own);
271 pow->p_power += 20 * (plane.pln_effic / 100.0) *
272 (natp->nat_level[NAT_TLEV] / 500.0);
274 for (i = 1; NULL != (natp = getnatp(i)); i++) {
277 if (natp->nat_stat != STAT_ACTIVE) {
281 pow->p_money = natp->nat_money;
282 pow->p_power += pow->p_money / 100.;
284 pow->p_power += pow->p_petrol / 500.0;
286 pow->p_power += (pow->p_civil + pow->p_milit) / 10.0;
287 pow->p_power += pow->p_shell / 12.5;
288 pow->p_power += pow->p_iron / 100.0;
289 pow->p_power += pow->p_dust / 5 + pow->p_oil / 10 + pow->p_bars;
290 pow->p_power += pow->p_guns / 2.5;
291 if (pow->p_sects > 0)
292 pow->p_power += pow->p_sects
293 * (pow->p_effic / pow->p_sects / 100.0)
295 pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV]) / 500.0;
296 /* ack. add this vec to the "world power" element */
297 f_pt2 = &powbuf[0].p_sects;
298 f_ptr = &pow->p_sects;
299 while (f_ptr <= &pow->p_power) {
305 for (i = 1; i < MAXNOC; i++) {
311 maxpop = max_population(np->nat_level[NAT_RLEV], SCT_MINE, 0);
312 powbuf[i].p_power *= 1.0 + maxpop / 10000.0;
315 qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
318 for (i = 0; i < MAXNOC; i++)
319 putpower(i, &powbuf[i]);
322 * At least some versions of Windows fail to update mtime on
323 * write(), they delay it until the write actually hits the disk.
324 * Bad, because `power' displays that time. Force it.
326 _commit(empfile[EF_POWER].fd);
331 powcmp(const void *a, const void *b)
333 const struct powstr *p1 = a;
334 const struct powstr *p2 = b;
336 if (p1->p_power > p2->p_power)
338 if (p1->p_power < p2->p_power)
340 return p1->p_nation - p2->p_nation;
344 addtopow(short *vec, struct powstr *pow)
346 pow->p_civil += vec[I_CIVIL];
347 pow->p_milit += vec[I_MILIT];
348 pow->p_shell += vec[I_SHELL];
349 pow->p_guns += vec[I_GUN];
350 pow->p_petrol += vec[I_PETROL];
351 pow->p_iron += vec[I_IRON];
352 pow->p_dust += vec[I_DUST];
353 pow->p_food += vec[I_FOOD];
354 pow->p_oil += vec[I_OIL];
355 pow->p_bars += vec[I_BAR];
356 pow->p_power += vec[I_LCM] / 10.0;
357 pow->p_power += vec[I_HCM] / 5.0;