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