]> git.pond.sub.org Git - empserver/blob - src/lib/commands/powe.c
dcb0da8ae279da81c4baf0919cc71277a03077d4
[empserver] / src / lib / commands / powe.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  powe.c: Do a power report
28  *
29  *  Known contributors to this file:
30  *     Dave Pare
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998-2000
33  *     Markus Armbruster, 2004-2009
34  *     Ron Koenderink, 2005-2008
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             if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
98                 return RET_SYN;
99             while (nxtitem(&ni, &nat)) {
100                 if (nat.nat_stat == STAT_UNUSED)
101                     continue;
102                 if (!player->god && 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.%s\n",
123                opt_AUTO_POWER ? "" : "  Type 'power new' to build it.");
124             return RET_FAIL;
125         }
126         for (i = 1; i < MAXNOC; i++) {
127             if (!nxtitem(&ni, &powbuf[i])) {
128                 CANT_REACH();
129                 memset(&powbuf[i], 0, sizeof(powbuf[i]));
130             }
131         }
132     }
133
134     pr("     - = [   Empire Power Report   ] = -\n");
135     pr("      as of %s\n         sects  eff civ", ctime(&pow_time));
136     pr("  mil  shell gun pet  iron dust oil  pln ship unit money\n");
137     for (i = 1; i < MAXNOC && num > 0; i++) {
138         if (opt_HIDDEN) {
139             if (!player->god && powbuf[i].p_nation != player->cnum)
140                 continue;
141         }
142         if (use_targets && !targets[powbuf[i].p_nation])
143             continue;
144         if (!use_targets && powbuf[i].p_power <= 0.0)
145             continue;
146         prpower(cname(powbuf[i].p_nation), &powbuf[i],
147                 powbuf[i].p_nation != player->cnum && !player->god);
148         if (player->god && !no_numbers)
149             pr("%9.2f\n", powbuf[i].p_power);
150         num--;
151     }
152     if (!opt_HIDDEN || player->god) {
153         pr("          ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
154         prpower("worldwide", &powbuf[0], !player->god);
155         pr("\n");
156     }
157     return RET_OK;
158 }
159
160 static void
161 prpower(char *name, struct powstr *pow, int round_flag)
162 {
163     pr("%9.9s", name);
164     out5(pow->p_sects, 5, round_flag);
165     if (pow->p_sects)
166         pr("%4.0f%%", pow->p_effic / pow->p_sects);
167     else
168         pr("   0%%");
169     out5(pow->p_civil, 50, round_flag);
170     out5(pow->p_milit, 50, round_flag);
171     out5(pow->p_shell, 25, round_flag);
172     out5(pow->p_guns, 5, round_flag);
173     out5(pow->p_petrol, 50, round_flag);
174     out5(pow->p_iron, 50, round_flag);
175     out5(pow->p_dust, 50, round_flag);
176     out5(pow->p_oil, 50, round_flag);
177     out5(pow->p_planes, 10, round_flag);
178     out5(pow->p_ships, 10, round_flag);
179     out5(pow->p_units, 10, round_flag);
180     out5(pow->p_money, 5000, round_flag);
181     pr("\n");
182 }
183
184 static void
185 out5(double value, int round_val, int round_flag)
186 {
187     double aval;
188
189     if (value > round_val && round_flag)
190         value = (int)(value / round_val + 0.5) * round_val;
191     aval = fabs(value);
192     if (aval < 1000.)
193         pr("%4.0f ", value);
194     else if (aval < 9.95e3)
195         pr("%4.1fK", value / 1e3);
196     else if (aval < 999.5e3)
197         pr("%4.0fK", value / 1e3);
198     else if (aval < 9.95e6)
199         pr("%4.1fM", value / 1e6);
200     else if (aval < 999.5e6)
201         pr("%4.0fM", value / 1e6);
202     else
203         pr("%4.0fG", value / 1e9);
204 }
205
206 void
207 update_power(void)
208 {
209     struct powstr powbuf[MAXNOC];
210
211     gen_power(powbuf, 1);
212 }
213
214 static void
215 gen_power(struct powstr *powbuf, int save)
216 {
217     float *f_ptr;
218     float *f_pt2;
219     struct powstr *pow;
220     int i;
221     struct sctstr sect;
222     struct plnstr plane;
223     struct shpstr ship;
224     struct lndstr land;
225     struct nstr_item ni;
226     struct nstr_sect ns;
227     struct natstr *natp;
228     float f;
229
230     player->btused += 10;
231     memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
232     snxtsct_all(&ns);
233     while (nxtsct(&ns, &sect)) {
234         if (sect.sct_own == 0)
235             continue;
236         pow = &powbuf[sect.sct_own];
237         pow->p_sects += 1.0;
238         pow->p_effic += sect.sct_effic;
239         addtopow(sect.sct_item, pow);
240     }
241     snxtitem_all(&ni, EF_LAND);
242     while (nxtitem(&ni, &land)) {
243         if (land.lnd_own == 0)
244             continue;
245         pow = &powbuf[land.lnd_own];
246         addtopow(land.lnd_item, pow);
247         f = (lchr[(int)land.lnd_type].l_lcm / 10.0) * (land.lnd_effic / 100.0);
248         f += (lchr[(int)land.lnd_type].l_hcm / 10.0) * (land.lnd_effic / 100.0);
249         pow->p_power += f * 2;
250         if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
251             pow->p_units += 1.0;
252     }
253     snxtitem_all(&ni, EF_SHIP);
254     while (nxtitem(&ni, &ship)) {
255         if (ship.shp_own == 0)
256             continue;
257         pow = &powbuf[ship.shp_own];
258         addtopow(ship.shp_item, pow);
259         f = (mchr[(int)ship.shp_type].m_lcm / 10.0) * (ship.shp_effic / 100.0);
260         f += (mchr[(int)ship.shp_type].m_hcm / 10.0) * (ship.shp_effic / 100.0);
261         pow->p_power += f * 2;
262         pow->p_ships += 1.0;
263     }
264     snxtitem_all(&ni, EF_PLANE);
265     while (nxtitem(&ni, &plane)) {
266         if (plane.pln_own == 0)
267             continue;
268         pow = &powbuf[plane.pln_own];
269         pow->p_planes += 1.0;
270         natp = getnatp(plane.pln_own);
271         pow->p_power += 20 * (plane.pln_effic / 100.0) *
272             (natp->nat_level[NAT_TLEV] / 500.0);
273     }
274     for (i = 1; NULL != (natp = getnatp(i)); i++) {
275         pow = &powbuf[i];
276         pow->p_nation = i;
277         if (natp->nat_stat != STAT_ACTIVE) {
278             pow->p_power = 0.;
279             continue;
280         }
281         pow->p_money = natp->nat_money;
282         pow->p_power += pow->p_money / 100.;
283
284         pow->p_power += pow->p_petrol / 500.0;
285
286         pow->p_power += (pow->p_civil + pow->p_milit) / 10.0;
287         pow->p_power += pow->p_shell / 12.5;
288         pow->p_power += pow->p_iron / 100.0;
289         pow->p_power += pow->p_dust / 5 + pow->p_oil / 10 + pow->p_bars;
290         pow->p_power += pow->p_guns / 2.5;
291         if (pow->p_sects > 0)
292             pow->p_power += pow->p_sects
293                 * (pow->p_effic / pow->p_sects / 100.0)
294                 * 10.0;
295         pow->p_power *= MAX(1.0, natp->nat_level[NAT_TLEV]) / 500.0;
296         /* ack.  add this vec to the "world power" element */
297         f_pt2 = &powbuf[0].p_sects;
298         f_ptr = &pow->p_sects;
299         while (f_ptr <= &pow->p_power) {
300             *f_pt2 += *f_ptr;
301             f_pt2++;
302             f_ptr++;
303         }
304     }
305     for (i = 1; i < MAXNOC; i++) {
306         struct natstr *np;
307         int maxpop;
308
309         if (opt_RES_POP) {
310             np = getnatp(i);
311             maxpop = max_population(np->nat_level[NAT_RLEV], SCT_MINE, 0);
312             powbuf[i].p_power *= 1.0 + maxpop / 10000.0;
313         }
314     }
315     qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
316     if (!save)
317         return;
318     for (i = 0; i < MAXNOC; i++)
319         putpower(i, &powbuf[i]);
320 #ifdef _WIN32
321     /*
322      * At least some versions of Windows fail to update mtime on
323      * write(), they delay it until the write actually hits the disk.
324      * Bad, because `power' displays that time.  Force it.
325      */
326     _commit(empfile[EF_POWER].fd);
327 #endif
328 }
329
330 static int
331 powcmp(const void *a, const void *b)
332 {
333     const struct powstr *p1 = a;
334     const struct powstr *p2 = b;
335
336     if (p1->p_power > p2->p_power)
337         return -1;
338     if (p1->p_power < p2->p_power)
339         return 1;
340     return 0;
341 }
342
343 static void
344 addtopow(short *vec, struct powstr *pow)
345 {
346     pow->p_civil += vec[I_CIVIL];
347     pow->p_milit += vec[I_MILIT];
348     pow->p_shell += vec[I_SHELL];
349     pow->p_guns += vec[I_GUN];
350     pow->p_petrol += vec[I_PETROL];
351     pow->p_iron += vec[I_IRON];
352     pow->p_dust += vec[I_DUST];
353     pow->p_food += vec[I_FOOD];
354     pow->p_oil += vec[I_OIL];
355     pow->p_bars += vec[I_BAR];
356     pow->p_power += vec[I_LCM] / 10.0;
357     pow->p_power += vec[I_HCM] / 5.0;
358 }