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