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