]> git.pond.sub.org Git - empserver/blob - src/lib/commands/powe.c
(powe): Simplify evaluation of arg 1. No functional change.
[empserver] / src / lib / commands / powe.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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, 2006
35  */
36
37 #include <config.h>
38
39 #include "commands.h"
40 #include "item.h"
41 #include "land.h"
42 #include "optlist.h"
43 #include "plane.h"
44 #include "power.h"
45 #include "ship.h"
46
47 struct powsort {
48     float powval;
49     natid cnum;
50 };
51
52 static void prpower(char *, struct powstr *, int);
53 static void out5(double, int, int);
54 static void gen_power(void);
55 static int powcmp(const void *, const void *);
56 static void addtopow(short *, struct powstr *);
57
58 int
59 powe(void)
60 {
61     struct natstr *natp;
62     int round_flag;
63     time_t pow_time;
64     struct nstr_item ni;
65     struct powstr pow;
66     int num;
67     int power_generated = 0;
68     struct natstr nat;
69     int targets[MAXNOC];
70     int use_targets = 0;
71     int no_numbers = 0;
72
73     memset(targets, 0, sizeof(targets));
74     natp = getnatp(player->cnum);
75     num = MAXNOC;
76
77     if (player->argp[1]) {
78         if (player->argp[1][0] == 'n') {
79             if (natp->nat_btu < 1)
80                 pr("\n  Insufficient BTUs, using the last report.\n\n");
81             else {
82                 gen_power();
83                 power_generated = 1;
84                 if (player->argp[2])
85                     num = atoi(player->argp[2]);
86             }
87         } else if (player->argp[1][0] == 'c') {
88             snxtitem(&ni, EF_NATION, player->argp[2]);
89             while (nxtitem(&ni, &nat)) {
90                 if (nat.nat_stat != STAT_ACTIVE)
91                     continue;
92                 targets[nat.nat_cnum] = 1;
93             }
94             use_targets = 1;
95         } else
96             num = atoi(player->argp[1]);
97     }
98
99     if (num < 0) {
100         if (!player->god)
101             return RET_SYN;
102         num = -num;
103         no_numbers = 1;
104     }
105
106     if (!power_generated) {
107         snxtitem_all(&ni, EF_POWER);
108         if (!nxtitem(&ni, &pow)) {
109             pr("Power for this game has not been built yet.  Type 'power new' to build it.\n");
110             return RET_FAIL;
111         }
112     }
113
114     pr("     - = [   Empire Power Report   ] = -\n");
115     pow_time = ef_mtime(EF_POWER);
116     pr("      as of %s\n         sects  eff civ", ctime(&pow_time));
117     pr("  mil  shell gun pet  iron dust oil  pln ship unit money\n");
118     snxtitem_all(&ni, EF_POWER);
119     while ((nxtitem(&ni, &pow)) && num > 0) {
120         if (pow.p_nation == 0)
121             continue;
122         if (opt_HIDDEN) {
123             if (!player->god && pow.p_nation != player->cnum)
124                 continue;
125         }
126         if (use_targets && !targets[pow.p_nation])
127             continue;
128         if (!use_targets && pow.p_power <= 0.0)
129             continue;
130         prpower(cname(pow.p_nation), &pow,
131                 pow.p_nation != player->cnum && !player->god);
132         if (player->god && !no_numbers)
133             pr("%9.2f\n", pow.p_power);
134         num--;
135     }
136     if (!opt_HIDDEN || player->god) {
137         pr("          ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
138         getpower(0, &pow);
139         prpower("worldwide", &pow, !player->god);
140         pr("\n");
141     }
142     return RET_OK;
143 }
144
145 static void
146 prpower(char *name, struct powstr *pow, int round_flag)
147 {
148     pr("%9.9s", name);
149     out5(pow->p_sects, 5, round_flag);
150     if (pow->p_sects)
151         pr("%4.0f%%", pow->p_effic / pow->p_sects);
152     else
153         pr("   0%%");
154     out5(pow->p_civil, 50, round_flag);
155     out5(pow->p_milit, 50, round_flag);
156     out5(pow->p_shell, 25, round_flag);
157     out5(pow->p_guns, 5, round_flag);
158     out5(pow->p_petrol, 50, round_flag);
159     out5(pow->p_iron, 50, round_flag);
160     out5(pow->p_dust, 50, round_flag);
161     out5(pow->p_oil, 50, round_flag);
162     out5(pow->p_planes, 10, round_flag);
163     out5(pow->p_ships, 10, round_flag);
164     out5(pow->p_units, 10, round_flag);
165     out5(pow->p_money, 5000, round_flag);
166     pr("\n");
167 }
168
169 static void
170 out5(double value, int round_val, int round_flag)
171 {
172     if (value > round_val && round_flag)
173         value = (int)(value / round_val + 0.5) * round_val;
174     if (value < -9999.5e3)
175         pr("%4.0fM", value / 1e6);
176     else if (value < -9950.)
177         pr("%4.0fK", value / 1000.);
178     else if (value < -999.)
179         pr("%4.1fK", value / 1000.);
180     else if (value < 1000.)
181         pr("%4.0f ", value);
182     else if (value < 9.95e3)
183         pr("%4.1fK", value / 1000.);
184     else if (value < 9999.5e3)
185         pr("%4.0fK", value / 1000.);
186     else if (value < 9.95e6)
187         pr("%4.1fM", value / 1e6);
188     else if (value < 9999.5e6)
189         pr("%4.0fM", value / 1e6);
190     else
191         pr("%4.0fG", value / 1e9);
192 }
193
194 static void
195 gen_power(void)
196 {
197     float *f_ptr;
198     float *f_pt2;
199     struct powstr *pow;
200     int i;
201     struct sctstr sect;
202     struct plnstr plane;
203     struct shpstr ship;
204     struct lndstr land;
205     struct powstr powbuf[MAXNOC];
206     struct nstr_item ni;
207     struct nstr_sect ns;
208     struct powsort order[MAXNOC];
209     struct natstr *natp;
210     float f;
211
212     player->btused += 10;
213     memset(powbuf, 0, sizeof(powbuf));
214     snxtsct_all(&ns);
215     while (nxtsct(&ns, &sect)) {
216         if (sect.sct_own == 0)
217             continue;
218         pow = &powbuf[sect.sct_own];
219         pow->p_sects += 1.0;
220         pow->p_effic += sect.sct_effic;
221         addtopow(sect.sct_item, pow);
222     }
223     snxtitem_all(&ni, EF_LAND);
224     while (nxtitem(&ni, &land)) {
225         if (land.lnd_own == 0)
226             continue;
227         pow = &powbuf[land.lnd_own];
228         addtopow(land.lnd_item, pow);
229         f = (lchr[(int)land.lnd_type].l_lcm / 10.0) * (land.lnd_effic / 100.0);
230         f += (lchr[(int)land.lnd_type].l_hcm / 10.0) * (land.lnd_effic / 100.0);
231         pow->p_power += f * 2;
232         if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
233             pow->p_units += 1.0;
234     }
235     snxtitem_all(&ni, EF_SHIP);
236     while (nxtitem(&ni, &ship)) {
237         if (ship.shp_own == 0)
238             continue;
239         pow = &powbuf[ship.shp_own];
240         addtopow(ship.shp_item, pow);
241         f = (mchr[(int)ship.shp_type].m_lcm / 10.0) * (ship.shp_effic / 100.0);
242         f += (mchr[(int)ship.shp_type].m_hcm / 10.0) * (ship.shp_effic / 100.0);
243         pow->p_power += f * 2;
244         pow->p_ships += 1.0;
245     }
246     snxtitem_all(&ni, EF_PLANE);
247     while (nxtitem(&ni, &plane)) {
248         if (plane.pln_own == 0)
249             continue;
250         pow = &powbuf[plane.pln_own];
251         pow->p_planes += 1.0;
252         natp = getnatp(plane.pln_own);
253         pow->p_power += 20 * (plane.pln_effic / 100.0) *
254             (natp->nat_level[NAT_TLEV] / 500.0);
255     }
256     for (i = 1; NULL != (natp = getnatp(i)); i++) {
257         pow = &powbuf[i];
258         pow->p_nation = i;
259         if (natp->nat_stat != STAT_ACTIVE) {
260             pow->p_power = 0.;
261             continue;
262         }
263         pow->p_money = natp->nat_money;
264         pow->p_power += pow->p_money / 100.;
265
266         pow->p_power += pow->p_petrol / 500.0;
267
268         pow->p_power += (pow->p_civil + pow->p_milit) / 10.0;
269         pow->p_power += pow->p_shell / 12.5;
270         pow->p_power += pow->p_iron / 100.0;
271         pow->p_power += pow->p_dust / 5 + pow->p_oil / 10 + pow->p_bars;
272         pow->p_power += pow->p_guns / 2.5;
273         if (pow->p_sects > 0)
274             pow->p_power += pow->p_sects
275                 * (pow->p_effic / pow->p_sects / 100.0)
276                 * 10.0;
277         pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV] / 500.0);
278         /* ack.  add this vec to the "world power" element */
279         f_pt2 = &powbuf[0].p_sects;
280         f_ptr = &pow->p_sects;
281         while (f_ptr <= &pow->p_power) {
282             *f_pt2 += *f_ptr;
283             f_pt2++;
284             f_ptr++;
285         }
286     }
287     for (i = 1; i < MAXNOC; i++) {
288         struct natstr *np;
289         int maxpop;
290
291         if (opt_RES_POP) {
292             np = getnatp(i);
293             maxpop = max_population(np->nat_level[NAT_RLEV], SCT_MINE, 0);
294             powbuf[i].p_power *= 1.0 + maxpop / 10000.0;
295         }
296         order[i].powval = powbuf[i].p_power;
297         order[i].cnum = i;
298     }
299     qsort(&order[1], MAXNOC - 1, sizeof(*order), powcmp);
300     putpower(0, &powbuf[0]);
301     for (i = 1; i < MAXNOC; i++) {
302         putpower(i, &powbuf[order[i].cnum]);
303     }
304 #ifdef _WIN32
305     /*
306      * At least some versions of Windows fail to update mtime on
307      * write().  Bad, because `power' displays that time.  Attempt to
308      * force an update.
309      */
310     _commit(empfile[EF_POWER].fd);
311 #endif
312 }
313
314 static int
315 powcmp(const void *a, const void *b)
316 {
317     const struct powsort *p1 = a;
318     const struct powsort *p2 = b;
319
320     if (p1->powval > p2->powval)
321         return -1;
322     if (p1->powval < p2->powval)
323         return 1;
324     return 0;
325 }
326
327 static void
328 addtopow(short *vec, struct powstr *pow)
329 {
330     pow->p_civil += vec[I_CIVIL];
331     pow->p_milit += vec[I_MILIT];
332     pow->p_shell += vec[I_SHELL];
333     pow->p_guns += vec[I_GUN];
334     pow->p_petrol += vec[I_PETROL];
335     pow->p_iron += vec[I_IRON];
336     pow->p_dust += vec[I_DUST];
337     pow->p_food += vec[I_FOOD];
338     pow->p_oil += vec[I_OIL];
339     pow->p_bars += vec[I_BAR];
340     pow->p_power += vec[I_LCM] / 10.0;
341     pow->p_power += vec[I_HCM] / 5.0;
342 }