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