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