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