]> git.pond.sub.org Git - empserver/blob - src/lib/commands/powe.c
commands: Rename the command functions
[empserver] / src / lib / commands / powe.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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-2016
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 "nuke.h"
45 #include "optlist.h"
46 #include "plane.h"
47 #include "power.h"
48 #include "ship.h"
49
50 static void prpower(char *, struct powstr *, int);
51 static void out5(double, int, int);
52 static void gen_power(struct powstr *, int);
53 static int powcmp(const void *, const void *);
54 static void addtopow(short *, struct powstr *);
55 static float empobj_power(int, short[], int);
56 static float empunit_power(int, int, short[], int);
57 static float money_power(int);
58 static float power_tech_factor(float);
59 static float item_power(short[]);
60
61 int
62 c_power(void)
63 {
64     struct natstr *natp;
65     int i;
66     time_t pow_time;
67     struct nstr_item ni;
68     int save = 1;
69     int num = MAXNOC;
70     int power_generated = 0;
71     struct natstr nat;
72     struct powstr powbuf[MAXNOC];
73     int targets[MAXNOC];
74     int use_targets = 0;
75     int no_numbers = 0;
76
77     memset(targets, 0, sizeof(targets));
78
79     i = 1;
80     if (player->argp[1]) {
81         switch (player->argp[1][0]) {
82         case 'u':
83             if (player->god)
84                 save = 0;
85             /* fall through */
86         case 'n':
87             i++;
88             natp = getnatp(player->cnum);
89             if (natp->nat_btu < 1)
90                 pr("\n  Insufficient BTUs, using the last report.\n\n");
91             else if (opt_AUTO_POWER && save)
92                 pr("\n  power new is disabled, using the last report.\n\n");
93             else {
94                 gen_power(powbuf, save);
95                 pow_time = time(NULL);
96                 power_generated = 1;
97             }
98         }
99     }
100
101     if (player->argp[i]) {
102         if (player->argp[i][0] == 'c') {
103             if (!snxtitem(&ni, EF_NATION, player->argp[i + 1], NULL))
104                 return RET_SYN;
105             while (nxtitem(&ni, &nat)) {
106                 if (nat.nat_stat == STAT_UNUSED)
107                     continue;
108                 if (!player->god && nat.nat_stat != STAT_ACTIVE)
109                     continue;
110                 targets[nat.nat_cnum] = 1;
111             }
112             use_targets = 1;
113         } else
114             num = atoi(player->argp[i]);
115     }
116
117     if (num < 0) {
118         if (!player->god)
119             return RET_SYN;
120         num = -num;
121         no_numbers = 1;
122     }
123
124     if (!power_generated) {
125         pow_time = ef_mtime(EF_POWER);
126         snxtitem_all(&ni, EF_POWER);
127         if (!nxtitem(&ni, &powbuf[0])) {
128             pr("Power for this game has not been built yet.%s\n",
129                opt_AUTO_POWER ? "" : "  Type 'power new' to build it.");
130             return RET_FAIL;
131         }
132         for (i = 1; i < MAXNOC; i++) {
133             if (!nxtitem(&ni, &powbuf[i])) {
134                 CANT_REACH();
135                 memset(&powbuf[i], 0, sizeof(powbuf[i]));
136             }
137         }
138     }
139
140     pr("     - = [   Empire Power Report   ] = -\n");
141     pr("      as of %s\n         sects  eff civ", ctime(&pow_time));
142     pr("  mil  shell gun pet  iron dust oil  pln ship unit money\n");
143     for (i = 1; i < MAXNOC && num > 0; i++) {
144         if (opt_HIDDEN) {
145             if (!player->god && powbuf[i].p_nation != player->cnum)
146                 continue;
147         }
148         if (use_targets && !targets[powbuf[i].p_nation])
149             continue;
150         if (!use_targets && powbuf[i].p_power <= 0.0)
151             continue;
152         prpower(cname(powbuf[i].p_nation), &powbuf[i],
153                 powbuf[i].p_nation != player->cnum && !player->god);
154         if (player->god && !no_numbers)
155             pr("%9.2f\n", powbuf[i].p_power);
156         num--;
157     }
158     if (!opt_HIDDEN || player->god) {
159         pr("          ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n");
160         prpower("worldwide", &powbuf[0], !player->god);
161         pr("\n");
162     }
163     return RET_OK;
164 }
165
166 static void
167 prpower(char *name, struct powstr *pow, int round_flag)
168 {
169     pr("%9.9s", name);
170     out5(pow->p_sects, 5, round_flag);
171     if (pow->p_sects)
172         pr("%4.0f%%", pow->p_effic / pow->p_sects);
173     else
174         pr("   0%%");
175     out5(pow->p_civil, 50, round_flag);
176     out5(pow->p_milit, 50, round_flag);
177     out5(pow->p_shell, 25, round_flag);
178     out5(pow->p_guns, 5, round_flag);
179     out5(pow->p_petrol, 50, round_flag);
180     out5(pow->p_iron, 50, round_flag);
181     out5(pow->p_dust, 50, round_flag);
182     out5(pow->p_oil, 50, round_flag);
183     out5(pow->p_planes, 10, round_flag);
184     out5(pow->p_ships, 10, round_flag);
185     out5(pow->p_units, 10, round_flag);
186     out5(pow->p_money, 5000, round_flag);
187     pr("\n");
188 }
189
190 static void
191 out5(double value, int round_val, int round_flag)
192 {
193     double aval;
194
195     if (value > round_val && round_flag)
196         value = (int)(value / round_val + 0.5) * round_val;
197     aval = fabs(value);
198     if (aval < 1000.)
199         pr("%4.0f ", value);
200     else if (aval < 9.95e3)
201         pr("%4.1fK", value / 1e3);
202     else if (aval < 999.5e3)
203         pr("%4.0fK", value / 1e3);
204     else if (aval < 9.95e6)
205         pr("%4.1fM", value / 1e6);
206     else if (aval < 999.5e6)
207         pr("%4.0fM", value / 1e6);
208     else
209         pr("%4.0fG", value / 1e9);
210 }
211
212 void
213 update_power(void)
214 {
215     struct powstr powbuf[MAXNOC];
216
217     gen_power(powbuf, 1);
218 }
219
220 static void
221 gen_power(struct powstr *powbuf, int save)
222 {
223     float upower[MAXNOC];
224     float *f_ptr;
225     float *f_pt2;
226     struct powstr *pow;
227     int i, maxpop;
228     struct sctstr sect;
229     struct dchrstr *dcp;
230     struct plnstr plane;
231     struct plchrstr *pcp;
232     struct shpstr ship;
233     struct mchrstr *mcp;
234     struct lndstr land;
235     struct lchrstr *lcp;
236     struct nukstr nuke;
237     struct nchrstr *ncp;
238     struct nstr_item ni;
239     struct nstr_sect ns;
240     struct natstr *natp;
241
242     player->btused += 10;
243     memset(powbuf, 0, MAXNOC * sizeof(*powbuf));
244     memset(upower, 0, sizeof(upower));
245     snxtsct_all(&ns);
246     while (nxtsct(&ns, &sect)) {
247         if (sect.sct_own == 0)
248             continue;
249         dcp = &dchr[sect.sct_type];
250         natp = getnatp(sect.sct_own);
251         pow = &powbuf[sect.sct_own];
252         pow->p_sects += 1.0;
253         pow->p_effic += sect.sct_effic;
254         addtopow(sect.sct_item, pow);
255         pow->p_power += empobj_power(sect.sct_effic,
256                                      dcp->d_mat, dcp->d_cost);
257         maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
258         pow->p_power += (1.0 + maxpop / 1000.0 * 8) * sect.sct_effic / 100.0;
259     }
260     snxtitem_all(&ni, EF_LAND);
261     while (nxtitem(&ni, &land)) {
262         if (land.lnd_own == 0)
263             continue;
264         lcp = &lchr[land.lnd_type];
265         pow = &powbuf[land.lnd_own];
266         addtopow(land.lnd_item, pow);
267         upower[land.lnd_own] += empunit_power(land.lnd_effic,
268                                               land.lnd_tech,
269                                               lcp->l_mat, lcp->l_cost);
270         if (!(lcp->l_flags & L_SPY))
271             pow->p_units += 1.0;
272     }
273     snxtitem_all(&ni, EF_SHIP);
274     while (nxtitem(&ni, &ship)) {
275         if (ship.shp_own == 0)
276             continue;
277         mcp = &mchr[ship.shp_type];
278         pow = &powbuf[ship.shp_own];
279         addtopow(ship.shp_item, pow);
280         upower[ship.shp_own] += empunit_power(ship.shp_effic,
281                                               ship.shp_tech,
282                                               mcp->m_mat, mcp->m_cost);
283         pow->p_ships += 1.0;
284     }
285     snxtitem_all(&ni, EF_PLANE);
286     while (nxtitem(&ni, &plane)) {
287         if (plane.pln_own == 0)
288             continue;
289         pcp = &plchr[plane.pln_type];
290         pow = &powbuf[plane.pln_own];
291         upower[plane.pln_own] += empunit_power(plane.pln_effic,
292                                                plane.pln_tech,
293                                                pcp->pl_mat, pcp->pl_cost);
294         pow->p_planes += 1.0;
295     }
296     snxtitem_all(&ni, EF_NUKE);
297     while (nxtitem(&ni, &nuke)) {
298         if (nuke.nuk_own == 0)
299             continue;
300         ncp = &nchr[nuke.nuk_type];
301         upower[nuke.nuk_own] += empunit_power(nuke.nuk_effic,
302                                               nuke.nuk_tech,
303                                               ncp->n_mat, ncp->n_cost);
304     }
305     for (i = 1; NULL != (natp = getnatp(i)); i++) {
306         pow = &powbuf[i];
307         pow->p_nation = i;
308         if (natp->nat_stat != STAT_ACTIVE) {
309             pow->p_power = 0.;
310             continue;
311         }
312         pow->p_money = natp->nat_money;
313         pow->p_power += money_power(natp->nat_money);
314         pow->p_power *= power_tech_factor(natp->nat_level[NAT_TLEV]);
315         pow->p_power += upower[i];
316         /* ack.  add this vec to the "world power" element */
317         f_pt2 = &powbuf[0].p_sects;
318         f_ptr = &pow->p_sects;
319         while (f_ptr <= &pow->p_power) {
320             *f_pt2 += *f_ptr;
321             f_pt2++;
322             f_ptr++;
323         }
324     }
325     qsort(&powbuf[1], MAXNOC - 1, sizeof(*powbuf), powcmp);
326     if (!save)
327         return;
328     for (i = 0; i < MAXNOC; i++)
329         putpower(i, &powbuf[i]);
330 #ifdef _WIN32
331     /*
332      * At least some versions of Windows fail to update mtime on
333      * write(), they delay it until the write actually hits the disk.
334      * Bad, because `power' displays that time.  Force it.
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 powstr *p1 = a;
344     const struct powstr *p2 = b;
345
346     if (p1->p_power > p2->p_power)
347         return -1;
348     if (p1->p_power < p2->p_power)
349         return 1;
350     return p1->p_nation - p2->p_nation;
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 += item_power(vec);
367 }
368
369 static float
370 empobj_power(int effic, short mat[], int cost)
371 {
372     return (item_power(mat) + money_power(cost)) * (effic / 100.0);
373 }
374
375 static float
376 empunit_power(int effic, int tech, short mat[], int cost)
377 {
378     return empobj_power(effic, mat, cost) * power_tech_factor(tech);
379 }
380
381 static float
382 money_power(int money)
383 {
384     return money / 100.0;
385 }
386
387 static float
388 power_tech_factor(float tech)
389 {
390     return (20 + tech) / 500.0;
391 }
392
393 static float
394 item_power(short item[])
395 {
396     int pow, i;
397
398     pow = 0;
399     for (i = I_NONE + 1; i <= I_MAX; i++)
400         pow += item[i] * ichr[i].i_power;
401
402     return pow / 1000.0;
403 }