]> git.pond.sub.org Git - empserver/blob - src/lib/commands/powe.c
Fix trailing whitespace
[empserver] / src / lib / commands / powe.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  powe.c: Do a power report
29  *
30  *  Known contributors to this file:
31  *     Dave Pare
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1998-2000
34  *     Markus Armbruster, 2004-2008
35  *     Ron Koenderink, 2005-2008
36  */
37
38 #include <config.h>
39 #include <unistd.h>
40 #include <math.h>
41
42 #include "commands.h"
43 #include "item.h"
44 #include "land.h"
45 #include "optlist.h"
46 #include "plane.h"
47 #include "power.h"
48 #include "ship.h"
49
50 static void prpower(char *, struct powstr *, int);
51 static void out5(double, int, int);
52 static void gen_power(struct powstr *, int);
53 static int powcmp(const void *, const void *);
54 static void addtopow(short *, struct powstr *);
55
56 int
57 powe(void)
58 {
59     struct natstr *natp;
60     int i;
61     time_t pow_time;
62     struct nstr_item ni;
63     int save = 1;
64     int num = MAXNOC;
65     int power_generated = 0;
66     struct natstr nat;
67     struct powstr powbuf[MAXNOC];
68     int targets[MAXNOC];
69     int use_targets = 0;
70     int no_numbers = 0;
71
72     memset(targets, 0, sizeof(targets));
73
74     i = 1;
75     if (player->argp[1]) {
76         switch (player->argp[1][0]) {
77         case 'u':
78             if (player->god)
79                 save = 0;
80             /* fall through */
81         case 'n':
82             i++;
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");
88             else {
89                 gen_power(powbuf, save);
90                 pow_time = time(NULL);
91                 power_generated = 1;
92             }
93         }
94     }
95
96     if (player->argp[i]) {
97         if (player->argp[i][0] == 'c') {
98             if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
99                 return RET_SYN;
100             while (nxtitem(&ni, &nat)) {
101                 if (nat.nat_stat == STAT_UNUSED)
102                     continue;
103                 if (!player->god && nat.nat_stat != STAT_ACTIVE)
104                     continue;
105                 targets[nat.nat_cnum] = 1;
106             }
107             use_targets = 1;
108         } else
109             num = atoi(player->argp[i]);
110     }
111
112     if (num < 0) {
113         if (!player->god)
114             return RET_SYN;
115         num = -num;
116         no_numbers = 1;
117     }
118
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.");
125             return RET_FAIL;
126         }
127         for (i = 1; i < MAXNOC; i++) {
128             if (!nxtitem(&ni, &powbuf[i])) {
129                 CANT_REACH();
130                 memset(&powbuf[i], 0, sizeof(powbuf[i]));
131             }
132         }
133     }
134
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++) {
139         if (opt_HIDDEN) {
140             if (!player->god && powbuf[i].p_nation != player->cnum)
141                 continue;
142         }
143         if (use_targets && !targets[powbuf[i].p_nation])
144             continue;
145         if (!use_targets && powbuf[i].p_power <= 0.0)
146             continue;
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);
151         num--;
152     }
153     if (!opt_HIDDEN || player->god) {
154         pr("          ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
155         prpower("worldwide", &powbuf[0], !player->god);
156         pr("\n");
157     }
158     return RET_OK;
159 }
160
161 static void
162 prpower(char *name, struct powstr *pow, int round_flag)
163 {
164     pr("%9.9s", name);
165     out5(pow->p_sects, 5, round_flag);
166     if (pow->p_sects)
167         pr("%4.0f%%", pow->p_effic / pow->p_sects);
168     else
169         pr("   0%%");
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);
182     pr("\n");
183 }
184
185 static void
186 out5(double value, int round_val, int round_flag)
187 {
188     double aval;
189
190     if (value > round_val && round_flag)
191         value = (int)(value / round_val + 0.5) * round_val;
192     aval = fabs(value);
193     if (aval < 1000.)
194         pr("%4.0f ", value);
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);
203     else
204         pr("%4.0fG", value / 1e9);
205 }
206
207 void
208 update_power(void)
209 {
210     struct powstr powbuf[MAXNOC];
211
212     gen_power(powbuf, 1);
213 }
214
215 static void
216 gen_power(struct powstr *powbuf, int save)
217 {
218     float *f_ptr;
219     float *f_pt2;
220     struct powstr *pow;
221     int i;
222     struct sctstr sect;
223     struct plnstr plane;
224     struct shpstr ship;
225     struct lndstr land;
226     struct nstr_item ni;
227     struct nstr_sect ns;
228     struct natstr *natp;
229     float f;
230
231     player->btused += 10;
232     memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
233     snxtsct_all(&ns);
234     while (nxtsct(&ns, &sect)) {
235         if (sect.sct_own == 0)
236             continue;
237         pow = &powbuf[sect.sct_own];
238         pow->p_sects += 1.0;
239         pow->p_effic += sect.sct_effic;
240         addtopow(sect.sct_item, pow);
241     }
242     snxtitem_all(&ni, EF_LAND);
243     while (nxtitem(&ni, &land)) {
244         if (land.lnd_own == 0)
245             continue;
246         pow = &powbuf[land.lnd_own];
247         addtopow(land.lnd_item, pow);
248         f = (lchr[(int)land.lnd_type].l_lcm / 10.0) * (land.lnd_effic / 100.0);
249         f += (lchr[(int)land.lnd_type].l_hcm / 10.0) * (land.lnd_effic / 100.0);
250         pow->p_power += f * 2;
251         if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
252             pow->p_units += 1.0;
253     }
254     snxtitem_all(&ni, EF_SHIP);
255     while (nxtitem(&ni, &ship)) {
256         if (ship.shp_own == 0)
257             continue;
258         pow = &powbuf[ship.shp_own];
259         addtopow(ship.shp_item, pow);
260         f = (mchr[(int)ship.shp_type].m_lcm / 10.0) * (ship.shp_effic / 100.0);
261         f += (mchr[(int)ship.shp_type].m_hcm / 10.0) * (ship.shp_effic / 100.0);
262         pow->p_power += f * 2;
263         pow->p_ships += 1.0;
264     }
265     snxtitem_all(&ni, EF_PLANE);
266     while (nxtitem(&ni, &plane)) {
267         if (plane.pln_own == 0)
268             continue;
269         pow = &powbuf[plane.pln_own];
270         pow->p_planes += 1.0;
271         natp = getnatp(plane.pln_own);
272         pow->p_power += 20 * (plane.pln_effic / 100.0) *
273             (natp->nat_level[NAT_TLEV] / 500.0);
274     }
275     for (i = 1; NULL != (natp = getnatp(i)); i++) {
276         pow = &powbuf[i];
277         pow->p_nation = i;
278         if (natp->nat_stat != STAT_ACTIVE) {
279             pow->p_power = 0.;
280             continue;
281         }
282         pow->p_money = natp->nat_money;
283         pow->p_power += pow->p_money / 100.;
284
285         pow->p_power += pow->p_petrol / 500.0;
286
287         pow->p_power += (pow->p_civil + pow->p_milit) / 10.0;
288         pow->p_power += pow->p_shell / 12.5;
289         pow->p_power += pow->p_iron / 100.0;
290         pow->p_power += pow->p_dust / 5 + pow->p_oil / 10 + pow->p_bars;
291         pow->p_power += pow->p_guns / 2.5;
292         if (pow->p_sects > 0)
293             pow->p_power += pow->p_sects
294                 * (pow->p_effic / pow->p_sects / 100.0)
295                 * 10.0;
296         pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV]) / 500.0;
297         /* ack.  add this vec to the "world power" element */
298         f_pt2 = &powbuf[0].p_sects;
299         f_ptr = &pow->p_sects;
300         while (f_ptr <= &pow->p_power) {
301             *f_pt2 += *f_ptr;
302             f_pt2++;
303             f_ptr++;
304         }
305     }
306     for (i = 1; i < MAXNOC; i++) {
307         struct natstr *np;
308         int maxpop;
309
310         if (opt_RES_POP) {
311             np = getnatp(i);
312             maxpop = max_population(np->nat_level[NAT_RLEV], SCT_MINE, 0);
313             powbuf[i].p_power *= 1.0 + maxpop / 10000.0;
314         }
315     }
316     qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
317     if (!save)
318         return;
319     for (i = 0; i < MAXNOC; i++)
320         putpower(i, &powbuf[i]);
321 #ifdef _WIN32
322     /*
323      * At least some versions of Windows fail to update mtime on
324      * write(), they delay it until the write actually hits the disk.
325      * Bad, because `power' displays that time.  Force it.
326      */
327     fsync(empfile[EF_POWER].fd);
328 #endif
329 }
330
331 static int
332 powcmp(const void *a, const void *b)
333 {
334     const struct powstr *p1 = a;
335     const struct powstr *p2 = b;
336
337     if (p1->p_power > p2->p_power)
338         return -1;
339     if (p1->p_power < p2->p_power)
340         return 1;
341     return 0;
342 }
343
344 static void
345 addtopow(short *vec, struct powstr *pow)
346 {
347     pow->p_civil += vec[I_CIVIL];
348     pow->p_milit += vec[I_MILIT];
349     pow->p_shell += vec[I_SHELL];
350     pow->p_guns += vec[I_GUN];
351     pow->p_petrol += vec[I_PETROL];
352     pow->p_iron += vec[I_IRON];
353     pow->p_dust += vec[I_DUST];
354     pow->p_food += vec[I_FOOD];
355     pow->p_oil += vec[I_OIL];
356     pow->p_bars += vec[I_BAR];
357     pow->p_power += vec[I_LCM] / 10.0;
358     pow->p_power += vec[I_HCM] / 5.0;
359 }