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 power_tech_factor(float);
55 static float item_power(short[]);
66 int power_generated = 0;
68 struct powstr powbuf[MAXNOC];
73 memset(targets, 0, sizeof(targets));
76 if (player->argp[1]) {
77 switch (player->argp[1][0]) {
84 natp = getnatp(player->cnum);
85 if (natp->nat_btu < 1)
86 pr("\n Insufficient BTUs, using the last report.\n\n");
87 else if (opt_AUTO_POWER && save)
88 pr("\n power new is disabled, using the last report.\n\n");
90 gen_power(powbuf, save);
91 pow_time = time(NULL);
97 if (player->argp[i]) {
98 if (player->argp[i][0] == 'c') {
99 if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
101 while (nxtitem(&ni, &nat)) {
102 if (nat.nat_stat == STAT_UNUSED)
104 if (!player->god && nat.nat_stat != STAT_ACTIVE)
106 targets[nat.nat_cnum] = 1;
110 num = atoi(player->argp[i]);
120 if (!power_generated) {
121 pow_time = ef_mtime(EF_POWER);
122 snxtitem_all(&ni, EF_POWER);
123 if (!nxtitem(&ni, &powbuf[0])) {
124 pr("Power for this game has not been built yet.%s\n",
125 opt_AUTO_POWER ? "" : " Type 'power new' to build it.");
128 for (i = 1; i < MAXNOC; i++) {
129 if (!nxtitem(&ni, &powbuf[i])) {
131 memset(&powbuf[i], 0, sizeof(powbuf[i]));
136 pr(" - = [ Empire Power Report ] = -\n");
137 pr(" as of %s\n sects eff civ", ctime(&pow_time));
138 pr(" mil shell gun pet iron dust oil pln ship unit money\n");
139 for (i = 1; i < MAXNOC && num > 0; i++) {
141 if (!player->god && powbuf[i].p_nation != player->cnum)
144 if (use_targets && !targets[powbuf[i].p_nation])
146 if (!use_targets && powbuf[i].p_power <= 0.0)
148 prpower(cname(powbuf[i].p_nation), &powbuf[i],
149 powbuf[i].p_nation != player->cnum && !player->god);
150 if (player->god && !no_numbers)
151 pr("%9.2f\n", powbuf[i].p_power);
154 if (!opt_HIDDEN || player->god) {
155 pr(" ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
156 prpower("worldwide", &powbuf[0], !player->god);
163 prpower(char *name, struct powstr *pow, int round_flag)
166 out5(pow->p_sects, 5, round_flag);
168 pr("%4.0f%%", pow->p_effic / pow->p_sects);
171 out5(pow->p_civil, 50, round_flag);
172 out5(pow->p_milit, 50, round_flag);
173 out5(pow->p_shell, 25, round_flag);
174 out5(pow->p_guns, 5, round_flag);
175 out5(pow->p_petrol, 50, round_flag);
176 out5(pow->p_iron, 50, round_flag);
177 out5(pow->p_dust, 50, round_flag);
178 out5(pow->p_oil, 50, round_flag);
179 out5(pow->p_planes, 10, round_flag);
180 out5(pow->p_ships, 10, round_flag);
181 out5(pow->p_units, 10, round_flag);
182 out5(pow->p_money, 5000, round_flag);
187 out5(double value, int round_val, int round_flag)
191 if (value > round_val && round_flag)
192 value = (int)(value / round_val + 0.5) * round_val;
196 else if (aval < 9.95e3)
197 pr("%4.1fK", value / 1e3);
198 else if (aval < 999.5e3)
199 pr("%4.0fK", value / 1e3);
200 else if (aval < 9.95e6)
201 pr("%4.1fM", value / 1e6);
202 else if (aval < 999.5e6)
203 pr("%4.0fM", value / 1e6);
205 pr("%4.0fG", value / 1e9);
211 struct powstr powbuf[MAXNOC];
213 gen_power(powbuf, 1);
217 gen_power(struct powstr *powbuf, int save)
219 float upower[MAXNOC];
233 player->btused += 10;
234 memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
235 memset(upower, 0, sizeof(upower));
237 while (nxtsct(&ns, §)) {
238 if (sect.sct_own == 0)
240 pow = &powbuf[sect.sct_own];
242 pow->p_effic += sect.sct_effic;
243 addtopow(sect.sct_item, pow);
245 snxtitem_all(&ni, EF_LAND);
246 while (nxtitem(&ni, &land)) {
247 if (land.lnd_own == 0)
249 pow = &powbuf[land.lnd_own];
250 addtopow(land.lnd_item, pow);
251 f = (lchr[land.lnd_type].l_mat[I_LCM] / 10.0)
252 * (land.lnd_effic / 100.0);
253 f += (lchr[land.lnd_type].l_mat[I_HCM] / 10.0)
254 * (land.lnd_effic / 100.0);
255 upower[land.lnd_own] += f * 2 * power_tech_factor(land.lnd_tech);
256 if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
259 snxtitem_all(&ni, EF_SHIP);
260 while (nxtitem(&ni, &ship)) {
261 if (ship.shp_own == 0)
263 pow = &powbuf[ship.shp_own];
264 addtopow(ship.shp_item, pow);
265 f = (mchr[ship.shp_type].m_mat[I_LCM] / 10.0)
266 * (ship.shp_effic / 100.0);
267 f += (mchr[ship.shp_type].m_mat[I_HCM] / 10.0)
268 * (ship.shp_effic / 100.0);
269 upower[ship.shp_own] += f * 2 * power_tech_factor(ship.shp_tech);
272 snxtitem_all(&ni, EF_PLANE);
273 while (nxtitem(&ni, &plane)) {
274 if (plane.pln_own == 0)
276 pow = &powbuf[plane.pln_own];
277 pow->p_planes += 1.0;
278 natp = getnatp(plane.pln_own);
279 f = 20 * (plane.pln_effic / 100.0) *
280 (20 + natp->nat_level[NAT_TLEV]) / 500.0;
281 upower[plane.pln_own] += f * power_tech_factor(plane.pln_tech);
283 for (i = 1; NULL != (natp = getnatp(i)); i++) {
286 if (natp->nat_stat != STAT_ACTIVE) {
290 pow->p_money = natp->nat_money;
291 pow->p_power += pow->p_money / 100.;
293 if (pow->p_sects > 0)
294 pow->p_power += pow->p_sects
295 * (pow->p_effic / pow->p_sects / 100.0)
297 pow->p_power *= power_tech_factor(natp->nat_level[NAT_TLEV]);
298 pow->p_power += upower[i];
299 /* ack. add this vec to the "world power" element */
300 f_pt2 = &powbuf[0].p_sects;
301 f_ptr = &pow->p_sects;
302 while (f_ptr <= &pow->p_power) {
308 qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
311 for (i = 0; i < MAXNOC; i++)
312 putpower(i, &powbuf[i]);
315 * At least some versions of Windows fail to update mtime on
316 * write(), they delay it until the write actually hits the disk.
317 * Bad, because `power' displays that time. Force it.
319 _commit(empfile[EF_POWER].fd);
324 powcmp(const void *a, const void *b)
326 const struct powstr *p1 = a;
327 const struct powstr *p2 = b;
329 if (p1->p_power > p2->p_power)
331 if (p1->p_power < p2->p_power)
333 return p1->p_nation - p2->p_nation;
337 addtopow(short *vec, struct powstr *pow)
339 pow->p_civil += vec[I_CIVIL];
340 pow->p_milit += vec[I_MILIT];
341 pow->p_shell += vec[I_SHELL];
342 pow->p_guns += vec[I_GUN];
343 pow->p_petrol += vec[I_PETROL];
344 pow->p_iron += vec[I_IRON];
345 pow->p_dust += vec[I_DUST];
346 pow->p_food += vec[I_FOOD];
347 pow->p_oil += vec[I_OIL];
348 pow->p_bars += vec[I_BAR];
349 pow->p_power += item_power(vec);
353 power_tech_factor(float tech)
355 return (20 + tech) / 500.0;
359 item_power(short item[])
364 for (i = I_NONE + 1; i <= I_MAX; i++)
365 pow += item[i] * ichr[i].i_power;