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