2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2016
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 *);
54 static float item_power(short[]);
65 int power_generated = 0;
67 struct powstr powbuf[MAXNOC];
72 memset(targets, 0, sizeof(targets));
75 if (player->argp[1]) {
76 switch (player->argp[1][0]) {
83 natp = getnatp(player->cnum);
84 if (natp->nat_btu < 1)
85 pr("\n Insufficient BTUs, using the last report.\n\n");
86 else if (opt_AUTO_POWER && save)
87 pr("\n power new is disabled, using the last report.\n\n");
89 gen_power(powbuf, save);
90 pow_time = time(NULL);
96 if (player->argp[i]) {
97 if (player->argp[i][0] == 'c') {
98 if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
100 while (nxtitem(&ni, &nat)) {
101 if (nat.nat_stat == STAT_UNUSED)
103 if (!player->god && nat.nat_stat != STAT_ACTIVE)
105 targets[nat.nat_cnum] = 1;
109 num = atoi(player->argp[i]);
119 if (!power_generated) {
120 pow_time = ef_mtime(EF_POWER);
121 snxtitem_all(&ni, EF_POWER);
122 if (!nxtitem(&ni, &powbuf[0])) {
123 pr("Power for this game has not been built yet.%s\n",
124 opt_AUTO_POWER ? "" : " Type 'power new' to build it.");
127 for (i = 1; i < MAXNOC; i++) {
128 if (!nxtitem(&ni, &powbuf[i])) {
130 memset(&powbuf[i], 0, sizeof(powbuf[i]));
135 pr(" - = [ Empire Power Report ] = -\n");
136 pr(" as of %s\n sects eff civ", ctime(&pow_time));
137 pr(" mil shell gun pet iron dust oil pln ship unit money\n");
138 for (i = 1; i < MAXNOC && num > 0; i++) {
140 if (!player->god && powbuf[i].p_nation != player->cnum)
143 if (use_targets && !targets[powbuf[i].p_nation])
145 if (!use_targets && powbuf[i].p_power <= 0.0)
147 prpower(cname(powbuf[i].p_nation), &powbuf[i],
148 powbuf[i].p_nation != player->cnum && !player->god);
149 if (player->god && !no_numbers)
150 pr("%9.2f\n", powbuf[i].p_power);
153 if (!opt_HIDDEN || player->god) {
154 pr(" ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
155 prpower("worldwide", &powbuf[0], !player->god);
162 prpower(char *name, struct powstr *pow, int round_flag)
165 out5(pow->p_sects, 5, round_flag);
167 pr("%4.0f%%", pow->p_effic / pow->p_sects);
170 out5(pow->p_civil, 50, round_flag);
171 out5(pow->p_milit, 50, round_flag);
172 out5(pow->p_shell, 25, round_flag);
173 out5(pow->p_guns, 5, round_flag);
174 out5(pow->p_petrol, 50, round_flag);
175 out5(pow->p_iron, 50, round_flag);
176 out5(pow->p_dust, 50, round_flag);
177 out5(pow->p_oil, 50, round_flag);
178 out5(pow->p_planes, 10, round_flag);
179 out5(pow->p_ships, 10, round_flag);
180 out5(pow->p_units, 10, round_flag);
181 out5(pow->p_money, 5000, round_flag);
186 out5(double value, int round_val, int round_flag)
190 if (value > round_val && round_flag)
191 value = (int)(value / round_val + 0.5) * round_val;
195 else if (aval < 9.95e3)
196 pr("%4.1fK", value / 1e3);
197 else if (aval < 999.5e3)
198 pr("%4.0fK", value / 1e3);
199 else if (aval < 9.95e6)
200 pr("%4.1fM", value / 1e6);
201 else if (aval < 999.5e6)
202 pr("%4.0fM", value / 1e6);
204 pr("%4.0fG", value / 1e9);
210 struct powstr powbuf[MAXNOC];
212 gen_power(powbuf, 1);
216 gen_power(struct powstr *powbuf, int save)
231 player->btused += 10;
232 memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
234 while (nxtsct(&ns, §)) {
235 if (sect.sct_own == 0)
237 pow = &powbuf[sect.sct_own];
239 pow->p_effic += sect.sct_effic;
240 addtopow(sect.sct_item, pow);
242 snxtitem_all(&ni, EF_LAND);
243 while (nxtitem(&ni, &land)) {
244 if (land.lnd_own == 0)
246 pow = &powbuf[land.lnd_own];
247 addtopow(land.lnd_item, pow);
248 f = (lchr[land.lnd_type].l_mat[I_LCM] / 10.0)
249 * (land.lnd_effic / 100.0);
250 f += (lchr[land.lnd_type].l_mat[I_HCM] / 10.0)
251 * (land.lnd_effic / 100.0);
252 pow->p_power += f * 2;
253 if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
256 snxtitem_all(&ni, EF_SHIP);
257 while (nxtitem(&ni, &ship)) {
258 if (ship.shp_own == 0)
260 pow = &powbuf[ship.shp_own];
261 addtopow(ship.shp_item, pow);
262 f = (mchr[ship.shp_type].m_mat[I_LCM] / 10.0)
263 * (ship.shp_effic / 100.0);
264 f += (mchr[ship.shp_type].m_mat[I_HCM] / 10.0)
265 * (ship.shp_effic / 100.0);
266 pow->p_power += f * 2;
269 snxtitem_all(&ni, EF_PLANE);
270 while (nxtitem(&ni, &plane)) {
271 if (plane.pln_own == 0)
273 pow = &powbuf[plane.pln_own];
274 pow->p_planes += 1.0;
275 natp = getnatp(plane.pln_own);
276 pow->p_power += 20 * (plane.pln_effic / 100.0) *
277 (natp->nat_level[NAT_TLEV] / 500.0);
279 for (i = 1; NULL != (natp = getnatp(i)); i++) {
282 if (natp->nat_stat != STAT_ACTIVE) {
286 pow->p_money = natp->nat_money;
287 pow->p_power += pow->p_money / 100.;
289 if (pow->p_sects > 0)
290 pow->p_power += pow->p_sects
291 * (pow->p_effic / pow->p_sects / 100.0)
293 pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV]) / 500.0;
294 /* ack. add this vec to the "world power" element */
295 f_pt2 = &powbuf[0].p_sects;
296 f_ptr = &pow->p_sects;
297 while (f_ptr <= &pow->p_power) {
303 for (i = 1; i < MAXNOC; i++) {
309 maxpop = max_population(np->nat_level[NAT_RLEV], SCT_MINE, 0);
310 powbuf[i].p_power *= 1.0 + maxpop / 10000.0;
313 qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
316 for (i = 0; i < MAXNOC; i++)
317 putpower(i, &powbuf[i]);
320 * At least some versions of Windows fail to update mtime on
321 * write(), they delay it until the write actually hits the disk.
322 * Bad, because `power' displays that time. Force it.
324 _commit(empfile[EF_POWER].fd);
329 powcmp(const void *a, const void *b)
331 const struct powstr *p1 = a;
332 const struct powstr *p2 = b;
334 if (p1->p_power > p2->p_power)
336 if (p1->p_power < p2->p_power)
338 return p1->p_nation - p2->p_nation;
342 addtopow(short *vec, struct powstr *pow)
344 pow->p_civil += vec[I_CIVIL];
345 pow->p_milit += vec[I_MILIT];
346 pow->p_shell += vec[I_SHELL];
347 pow->p_guns += vec[I_GUN];
348 pow->p_petrol += vec[I_PETROL];
349 pow->p_iron += vec[I_IRON];
350 pow->p_dust += vec[I_DUST];
351 pow->p_food += vec[I_FOOD];
352 pow->p_oil += vec[I_OIL];
353 pow->p_bars += vec[I_BAR];
354 pow->p_power += item_power(vec);
358 item_power(short item[])
360 static float pow_denom[I_MAX + 1] = {
361 10, 10, 12.5, 2.5, 500, 100, 5, 1, 0, 10, 10, 5, 0, 0
367 for (i = I_NONE + 1; i <= I_MAX; i++) {
369 p += item[i] / pow_denom[i];